This commit is contained in:
Dalibor Čarapić 2024-09-28 18:36:41 +02:00
parent b011e3b388
commit fb97f3213d
9 changed files with 67 additions and 54 deletions

View File

@ -9,7 +9,7 @@ export default class Button extends GameObject {
setCaption(caption: string) { setCaption(caption: string) {
this.caption = caption; this.caption = caption;
this.createButton(); this.drawButton();
} }
constructor(caption: string, bounds: PIXI.Rectangle, color: PIXI.Color) { constructor(caption: string, bounds: PIXI.Rectangle, color: PIXI.Color) {
@ -18,26 +18,41 @@ export default class Button extends GameObject {
this.color = color; this.color = color;
this.container.interactive = true; this.container.interactive = true;
this.buttonTexture = Assets.ButtonTexture; this.buttonTexture = Assets.ButtonTexture;
this.createButton(); this.drawButton();
} }
protected triggerBoundsChanged() { protected triggerBoundsChanged() {
this.createButton(); this.drawButton();
} }
private createButton() { private drawButton() {
console.log(
`Drawing button ${this.caption} at ${JSON.stringify(this.bounds)}`
);
this.container.removeChildren(); this.container.removeChildren();
// const button = new PIXI.Graphics(); // const button = new PIXI.Graphics();
// button.rect(0, 0, this.bounds.width, this.bounds.height); // button.rect(0, 0, this.bounds.width, this.bounds.height);
// button.fill(this.color); // button.fill(this.color);
console.log(this.buttonTexture); //console.log(this.buttonTexture);
const button = new PIXI.Sprite(this.buttonTexture); // TODO: sliced texture const button = new PIXI.NineSliceSprite({
texture: this.buttonTexture,
leftWidth: 100,
topHeight: 100,
rightWidth: 100,
bottomHeight: 100,
});
button.x = 0; button.x = 0;
button.y = 0; button.y = 0;
button.width = this.bounds.width; button.width = this.bounds.width;
button.height = this.bounds.height; button.height = this.bounds.height;
this.container.addChild(button); this.container.addChild(button);
const text = new PIXI.Text({ text: this.caption }); const text = new PIXI.Text({
text: this.caption,
style: new PIXI.TextStyle({
fill: 0xffffff,
fontSize: 24,
}),
});
this.container.addChild(text); this.container.addChild(text);
text.anchor.set(0.5, 0.5); text.anchor.set(0.5, 0.5);
text.x = this.bounds.width / 2; text.x = this.bounds.width / 2;

View File

@ -3,11 +3,10 @@ import MissionMenuSelect from "../scenes/MissionSelectMenu";
import GameScene from "../scenes/GameScene"; import GameScene from "../scenes/GameScene";
import GameObject from "./GameObject"; import GameObject from "./GameObject";
import * as PIXI from "pixi.js"; import * as PIXI from "pixi.js";
import Scene from "../scenes/Base"; import SceneBase from "../scenes/SceneBase";
export default class Game extends GameObject { export default class Game extends GameObject {
private _currentScene: Scene | null = null; private _currentScene: SceneBase | null = null;
public ticker: PIXI.Ticker | null = null;
constructor(bounds: PIXI.Rectangle) { constructor(bounds: PIXI.Rectangle) {
super(bounds); super(bounds);
@ -21,6 +20,16 @@ export default class Game extends GameObject {
this.setScene(mainScene); this.setScene(mainScene);
} }
private setScene(scene: SceneBase) {
if (this._currentScene) {
this.container.removeChild(this._currentScene.container);
this._currentScene.destroy();
}
this._currentScene = scene;
console.log("Setting scene", this._currentScene.constructor);
this.container.addChild(scene.container);
}
private onNewGame = () => { private onNewGame = () => {
console.log("New Game"); console.log("New Game");
const missionSelectScene = new MissionMenuSelect(this.bounds); const missionSelectScene = new MissionMenuSelect(this.bounds);
@ -38,33 +47,6 @@ export default class Game extends GameObject {
console.log("Settings"); console.log("Settings");
}; };
private setScene(scene: Scene) {
if (this._currentScene) {
this.container.removeChild(this._currentScene.container);
}
this._currentScene = scene;
console.log(this._currentScene.constructor);
this.container.addChild(scene.container);
if (this._currentScene.constructor.name == "GameScene") {
// dont change GameScene name XD
this.ticker = new PIXI.Ticker();
this.ticker.maxFPS = 60;
this.ticker.minFPS = 30;
this.ticker.add(this.update);
console.log(this._currentScene);
this.ticker.start();
} else {
if (this.ticker != null) {
this.ticker.stop();
this.ticker.destroy();
}
}
}
private update(time) {
this._currentScene.update();
}
protected triggerBoundsChanged(): void { protected triggerBoundsChanged(): void {
this.drawScene(); this.drawScene();
} }

View File

@ -6,15 +6,18 @@ export default abstract class GameObject {
private _events: PIXI.EventEmitter = new PIXI.EventEmitter(); private _events: PIXI.EventEmitter = new PIXI.EventEmitter();
public getBounds(): PIXI.Rectangle {
return this.bounds;
}
public setBounds(x: number, y: number, width: number, height: number) { public setBounds(x: number, y: number, width: number, height: number) {
this.bounds = new PIXI.Rectangle(x, y, width, height); this.bounds = new PIXI.Rectangle(x, y, width, height);
this.triggerBoundsChanged(); // GameObject implements this. this.triggerBoundsChanged(); // GameObject implements this.
} }
public destroy() {
this._events.removeAllListeners();
if (this._container.parent)
this._container.parent.removeChild(this._container);
this._container.destroy();
}
public get container(): PIXI.Container { public get container(): PIXI.Container {
return this._container; return this._container;
} }

View File

@ -9,6 +9,7 @@ import Assets from "./base/Assets";
resizeTo: document.body, resizeTo: document.body,
backgroundColor: "white", backgroundColor: "white",
sharedTicker: true, sharedTicker: true,
preference: "webgl",
}); });
document.body.appendChild(app.canvas); document.body.appendChild(app.canvas);

View File

@ -1,12 +1,24 @@
import Button from "../base/Button"; import Button from "../base/Button";
import Scene from "./Base"; import SceneBase from "./SceneBase";
import * as PIXI from "pixi.js"; import * as PIXI from "pixi.js";
export default class GameScene extends Scene { export default class GameScene extends SceneBase {
private grid; private _ticker: PIXI.Ticker;
// DO NOT CHANGE NAME, WILL BREAK Game.ts
constructor(bounds: PIXI.Rectangle) { constructor(bounds: PIXI.Rectangle) {
super(bounds); super(bounds);
this._ticker = new PIXI.Ticker();
this._ticker = new PIXI.Ticker();
this._ticker.maxFPS = 60;
this._ticker.minFPS = 30;
this._ticker.add(this.update);
this._ticker.start();
}
public destroy() {
super.destroy();
this._ticker.stop();
this._ticker.destroy();
} }
public update() {} public update() {}

View File

@ -1,8 +1,8 @@
import Button from "../base/Button"; import Button from "../base/Button";
import Scene from "./Base"; import SceneBase from "./SceneBase";
import * as PIXI from "pixi.js"; import * as PIXI from "pixi.js";
export default class MainMenu extends Scene { export default class MainMenu extends SceneBase {
private _newGameButton: Button; private _newGameButton: Button;
private _settingsButton: Button; private _settingsButton: Button;

View File

@ -1,8 +1,8 @@
import Button from "../base/Button"; import Button from "../base/Button";
import Scene from "./Base"; import SceneBase from "./SceneBase";
import * as PIXI from "pixi.js"; import * as PIXI from "pixi.js";
export default class MissionMenuSelect extends Scene { export default class MissionMenuSelect extends SceneBase {
private _buttons: Button[] = []; private _buttons: Button[] = [];
constructor(bounds: PIXI.Rectangle) { constructor(bounds: PIXI.Rectangle) {

View File

@ -1,14 +1,14 @@
import * as PIXI from "pixi.js"; import * as PIXI from "pixi.js";
import GameObject from "../base/GameObject"; import GameObject from "../base/GameObject";
export default abstract class Scene extends GameObject { export default abstract class SceneBase extends GameObject {
constructor(bounds: PIXI.Rectangle) { constructor(bounds: PIXI.Rectangle) {
super(bounds); super(bounds);
this.createScene(); this.createScene();
} }
protected triggerBoundsChanged() { protected triggerBoundsChanged() {
this.createScene(); this.createScene();
} }
protected abstract createScene();
public update() {} protected abstract createScene();
} }

View File

@ -1,8 +1,8 @@
import Button from "../base/Button"; import Button from "../base/Button";
import Scene from "./Base"; import SceneBase from "./SceneBase";
import * as PIXI from "pixi.js"; import * as PIXI from "pixi.js";
export default class SettingsMenu extends Scene { export default class SettingsMenu extends SceneBase {
private _newGameButton: Button; private _newGameButton: Button;
private _settingsButton: Button; private _settingsButton: Button;