Refactoring

This commit is contained in:
Dalibor Čarapić 2024-09-28 18:50:48 +02:00
parent fb97f3213d
commit 7f2f1f514e
10 changed files with 69 additions and 29 deletions

View File

@ -6,26 +6,33 @@ export default class Button extends GameObject {
private caption: string; private caption: string;
private color: PIXI.Color; private color: PIXI.Color;
private buttonTexture: PIXI.Texture; private buttonTexture: PIXI.Texture;
private enabled: boolean = true;
setCaption(caption: string) { setCaption(caption: string) {
this.caption = caption; this.caption = caption;
this.drawButton(); this.draw();
} }
constructor(caption: string, bounds: PIXI.Rectangle, color: PIXI.Color) { setEnabled(enabled: boolean) {
this.enabled = enabled;
}
constructor(
caption: string,
bounds: PIXI.Rectangle,
color: PIXI.Color,
enabled: boolean = true
) {
super(bounds); super(bounds);
this.caption = caption; this.caption = caption;
this.color = color; this.color = color;
this.container.interactive = true; this.container.interactive = true;
this.buttonTexture = Assets.ButtonTexture; this.buttonTexture = Assets.ButtonTexture;
this.drawButton(); this.enabled = enabled;
this.draw();
} }
protected triggerBoundsChanged() { protected draw() {
this.drawButton();
}
private drawButton() {
console.log( console.log(
`Drawing button ${this.caption} at ${JSON.stringify(this.bounds)}` `Drawing button ${this.caption} at ${JSON.stringify(this.bounds)}`
); );
@ -60,6 +67,7 @@ export default class Button extends GameObject {
this.container.x = this.bounds.x; this.container.x = this.bounds.x;
this.container.y = this.bounds.y; this.container.y = this.bounds.y;
this.container.on("click", () => { this.container.on("click", () => {
if (!this.enabled) return;
this.events.emit("click"); this.events.emit("click");
}); });
} }

View File

@ -48,12 +48,12 @@ export default class Game extends GameObject {
}; };
protected triggerBoundsChanged(): void { protected triggerBoundsChanged(): void {
this.drawScene();
}
private drawScene() {
if (this._currentScene) { if (this._currentScene) {
this._currentScene.setBounds(0, 0, this.bounds.width, this.bounds.height); this._currentScene.setBounds(0, 0, this.bounds.width, this.bounds.height);
} }
} }
protected draw() {
// Nothing to draw, scene is drawing itself.
}
} }

View File

@ -26,7 +26,11 @@ export default abstract class GameObject {
return this._events; return this._events;
} }
protected abstract triggerBoundsChanged(); protected triggerBoundsChanged() {
this.draw();
}
protected abstract draw(): void;
constructor(bounds: PIXI.Rectangle) { constructor(bounds: PIXI.Rectangle) {
this.bounds = bounds; this.bounds = bounds;

View File

@ -1,5 +1,5 @@
import * as PIXI from "pixi.js"; import * as PIXI from "pixi.js";
import GameObject from "./GameObject.ts"; import GameObject from "../base/GameObject";
export enum CellType { export enum CellType {
Path, Path,
@ -13,9 +13,30 @@ export class Cell extends GameObject {
constructor(bounds: PIXI.Rectangle, type: CellType) { constructor(bounds: PIXI.Rectangle, type: CellType) {
super(bounds); super(bounds);
this.type = type; this.type = type;
this.draw();
} }
protected triggerBoundsChanged() {
protected draw() {
this.container.removeChildren(); this.container.removeChildren();
let g = new PIXI.Graphics();
g.rect(0, 0, this.bounds.width, this.bounds.height);
switch (this.type) {
case CellType.Path:
g.fill(0x00ff00);
break;
case CellType.NoBuild:
g.fill(0xff0000);
break;
case CellType.Build:
g.fill(0x0000ff);
break;
case CellType.Undefined:
g.fill(0x000000);
break;
}
this.container.addChild(g);
this.container.x = this.bounds.x;
this.container.y = this.bounds.y;
} }
} }
@ -23,6 +44,7 @@ export class Grid extends GameObject {
public rows: number; public rows: number;
public columns: number; public columns: number;
public cells: Array<Cell>; public cells: Array<Cell>;
constructor(bounds: PIXI.Rectangle, rows, columns) { constructor(bounds: PIXI.Rectangle, rows, columns) {
super(bounds); super(bounds);
this.rows = rows; this.rows = rows;
@ -43,9 +65,7 @@ export class Grid extends GameObject {
} }
} }
protected triggerBoundsChanged() { protected draw() {}
this.container.removeChildren();
}
private getPixelScalingFactor() { private getPixelScalingFactor() {
const pixelScaleX = this.container.width / this.columns; const pixelScaleX = this.container.width / this.columns;

View File

@ -0,0 +1,11 @@
import GameObject from "../base/GameObject";
import * as PIXI from "pixi.js";
export default class MissionStats extends GameObject {
constructor(bounds: PIXI.Rectangle) {
super(bounds);
this.draw();
}
protected draw() {}
}

View File

@ -13,6 +13,7 @@ export default class GameScene extends SceneBase {
this._ticker.minFPS = 30; this._ticker.minFPS = 30;
this._ticker.add(this.update); this._ticker.add(this.update);
this._ticker.start(); this._ticker.start();
this.draw();
} }
public destroy() { public destroy() {
@ -23,7 +24,7 @@ export default class GameScene extends SceneBase {
public update() {} public update() {}
protected createScene() { protected draw() {
console.log("Creating Game Scene ", this.bounds); console.log("Creating Game Scene ", this.bounds);
this.container.removeChildren(); this.container.removeChildren();
this.container.x = this.bounds.x; this.container.x = this.bounds.x;

View File

@ -8,9 +8,10 @@ export default class MainMenu extends SceneBase {
constructor(bounds: PIXI.Rectangle) { constructor(bounds: PIXI.Rectangle) {
super(bounds); super(bounds);
this.draw();
} }
protected createScene() { protected draw() {
console.log("Creating main menu scene", this.bounds); console.log("Creating main menu scene", this.bounds);
this.container.removeChildren(); this.container.removeChildren();
const g = new PIXI.Graphics(); const g = new PIXI.Graphics();

View File

@ -7,9 +7,10 @@ export default class MissionMenuSelect extends SceneBase {
constructor(bounds: PIXI.Rectangle) { constructor(bounds: PIXI.Rectangle) {
super(bounds); super(bounds);
this.draw();
} }
protected createScene() { protected draw() {
this.container.removeChildren(); this.container.removeChildren();
this._buttons = []; this._buttons = [];
const g = new PIXI.Graphics(); const g = new PIXI.Graphics();

View File

@ -3,12 +3,5 @@ import GameObject from "../base/GameObject";
export default abstract class SceneBase extends GameObject { export default abstract class SceneBase extends GameObject {
constructor(bounds: PIXI.Rectangle) { constructor(bounds: PIXI.Rectangle) {
super(bounds); super(bounds);
this.createScene();
} }
protected triggerBoundsChanged() {
this.createScene();
}
protected abstract createScene();
} }

View File

@ -8,9 +8,10 @@ export default class SettingsMenu extends SceneBase {
constructor(bounds: PIXI.Rectangle) { constructor(bounds: PIXI.Rectangle) {
super(bounds); super(bounds);
this.draw();
} }
protected createScene() { protected draw() {
this.container.removeChildren(); this.container.removeChildren();
const g = new PIXI.Graphics(); const g = new PIXI.Graphics();
g.rect(0, 0, this.bounds.width, this.bounds.height); g.rect(0, 0, this.bounds.width, this.bounds.height);