Skip to content

Commit b2022d8

Browse files
committed
Simplify setup
1 parent 4585963 commit b2022d8

File tree

2 files changed

+50
-56
lines changed

2 files changed

+50
-56
lines changed

src/FieldRenderer.ts

Lines changed: 36 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*/
44

55
import * as PIXI from "pixi.js";
6-
import { getTextures, loadTextures } from "./Textures.js";
6+
import { getTextures } from "./Textures.js";
77
import { Controls } from "./Controls";
88
import { CellSprite } from "./CellSprite";
99
import { Cell } from "./Cell.js";
@@ -16,25 +16,28 @@ type CellSprites = Record<
1616
>;
1717

1818
export class FieldRenderer extends PIXI.Application {
19-
private field: Field;
19+
private fieldContainer = new PIXI.Container({
20+
isRenderGroup: true,
21+
interactiveChildren: false,
22+
});
23+
24+
private clickHandler = new PIXI.Container();
2025

2126
public cellSprites: CellSprites = {};
2227

2328
public constructor(
24-
field: Field,
29+
private field: Field,
2530
updateScore: (input: Field) => void,
26-
fieldPersistence: FieldPersistence,
31+
private fieldPersistence: FieldPersistence,
2732
) {
2833
super();
29-
this.field = field;
3034

3135
field.on("cellChanged", (cell) => {
3236
this.updateCell(cell, true);
3337
updateScore(field);
3438
});
3539

36-
// todo use async, move to before fieldRenderer initialisation.
37-
loadTextures().then(() => this.setup(field, fieldPersistence));
40+
this.setup();
3841
}
3942

4043
public updateCell(cell: Cell, playAnimation: boolean) {
@@ -46,7 +49,12 @@ export class FieldRenderer extends PIXI.Application {
4649
cellSprite.update(cell);
4750
} else {
4851
const value = this.field.value(cell.x, cell.y);
49-
cellSprite = new CellSprite(cell, value, fieldContainer, playAnimation);
52+
cellSprite = new CellSprite(
53+
cell,
54+
value,
55+
this.fieldContainer,
56+
playAnimation,
57+
);
5058
this.cellSprites[cell.y]![cell.x] = cellSprite;
5159
}
5260
}
@@ -61,63 +69,37 @@ export class FieldRenderer extends PIXI.Application {
6169
// todo: bug when i reset the game to reset the camera to 0,0, then the cell at 0,0 flashes with the "zoom out to spawn" animation every time the camera moves a pixel.
6270

6371
// todo inline
64-
private setup(
65-
// todo get from textures directly
66-
67-
field: Field,
68-
fieldPersistence: FieldPersistence,
69-
): void {
70-
const closedTexture = getTextures().closed;
71-
72+
private setup(): void {
7273
// todo migrate away from tilingsprite
7374
const background = new PIXI.TilingSprite(
74-
closedTexture,
75-
app.renderer.width,
76-
app.renderer.height,
75+
getTextures().closed,
76+
this.renderer?.width ?? window.innerWidth,
77+
this.renderer?.height ?? window.innerHeight,
7778
);
7879

79-
window.addEventListener("resize", function (_event) {
80-
function resize(width: number, height: number) {
81-
app.renderer.resize(width, height);
82-
background.width = app.renderer.width;
83-
background.height = app.renderer.height;
84-
}
80+
window.addEventListener("resize", () => {
81+
const width = window.innerWidth;
82+
const height = window.innerHeight;
8583

86-
resize(window.innerWidth, window.innerHeight);
87-
});
84+
this.renderer.resize(width, height);
8885

89-
// const width = minesTextures.closed?.width;
86+
background.width = width;
87+
background.height = height;
88+
});
9089

91-
// todo this is deprecated?
9290
background.name = "bg";
93-
fieldContainer.name = "fg";
91+
this.fieldContainer.name = "fg";
9492

95-
clickHandler.addChildAt(background, 0);
96-
clickHandler.addChildAt(fieldContainer, 1);
93+
this.clickHandler.addChildAt(background, 0);
94+
this.clickHandler.addChildAt(this.fieldContainer, 1);
95+
this.clickHandler.eventMode = "static";
9796

98-
new Controls(clickHandler, field, fieldPersistence);
97+
this.stage.addChild(this.clickHandler);
9998

10099
this.updateAllCells();
101100
}
102-
}
103101

104-
const app = new PIXI.Application();
105-
const fieldContainer = new PIXI.Container({
106-
isRenderGroup: true,
107-
interactiveChildren: false,
108-
});
109-
const clickHandler = new PIXI.Container();
110-
111-
(async () => {
112-
await app.init({
113-
resizeTo: window,
114-
backgroundColor: 0x0f0f0f,
115-
});
116-
117-
document.body.appendChild(app.canvas);
118-
119-
PIXI.TextureSource.defaultOptions.scaleMode = "nearest";
120-
121-
clickHandler.eventMode = "static";
122-
app.stage.addChild(clickHandler);
123-
})();
102+
public setupAfterCanvasReady() {
103+
new Controls(this.clickHandler, this.field, this.fieldPersistence);
104+
}
105+
}

src/index.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import { FieldRenderer } from "./FieldRenderer";
33
import { FieldPersistence } from "./FieldPersistence";
44
import "./css/stylesheet.css";
55
import menubutton from "./assets/default/menubutton.png";
6+
import { loadTextures } from "./Textures";
7+
import * as PIXI from "pixi.js";
68

79
const fieldName = (window.fieldName = "defaultSavedFieldv3");
810
const localStorage = window.localStorage;
@@ -31,8 +33,6 @@ if (!field) {
3133
);
3234
}
3335

34-
new FieldRenderer(field, updateScore, fieldStorage);
35-
3636
const button: HTMLImageElement = document.getElementById(
3737
"menubutton",
3838
) as HTMLImageElement;
@@ -50,3 +50,15 @@ self.restart = function () {
5050
console.log("removed: ", fieldName);
5151
window.location.reload();
5252
};
53+
54+
(async () => {
55+
PIXI.TextureSource.defaultOptions.scaleMode = "nearest";
56+
await loadTextures();
57+
const app = new FieldRenderer(field, updateScore, fieldStorage);
58+
await app.init({
59+
resizeTo: window,
60+
backgroundColor: 0x0f0f0f,
61+
});
62+
document.body.appendChild(app.canvas);
63+
app.setupAfterCanvasReady();
64+
})();

0 commit comments

Comments
 (0)