generated from lloydevans/tiny-webpack-typescript
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathms-app.ts
99 lines (80 loc) · 2.35 KB
/
ms-app.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import defaults from "lodash-es/defaults";
import * as PIXI from "pixi.js";
import { AppBase } from "./common/app-base";
import { preventContextMenu } from "./common/utils";
import { MSCell } from "./ms-cell";
import type { MSConfig, MSGameConfig } from "./ms-config";
import { MS_CONFIG_DEFAULT } from "./ms-config";
import { MAX_GRID_HEIGHT, MAX_GRID_WIDTH, MSState } from "./ms-state";
import { SceneGame } from "./scene-game";
export const INITIAL_GAME_CONFIG: MSGameConfig = {
startMines: 32,
gridWidth: 26,
gridHeight: 14,
};
/**
* Core App class.
*/
export class MSApp extends AppBase {
public state: MSState = new MSState();
public cellPool: MSCell[] = [];
public config: MSConfig;
public scenes: {
game?: SceneGame;
} = {};
private isLoaded = false;
constructor() {
super();
preventContextMenu();
this.config = { ...MS_CONFIG_DEFAULT };
this.events.on("update", this.onUpdate, this);
this.setReady();
}
public onLoad() {
this.isLoaded = true;
const tilesAtlas = this.getAtlas("tiles");
if (tilesAtlas) {
tilesAtlas.baseTexture.mipmap = PIXI.MIPMAP_MODES.OFF;
tilesAtlas.baseTexture.scaleMode = PIXI.SCALE_MODES.NEAREST;
tilesAtlas.baseTexture.update();
}
const bgAtlas = this.getAtlas("bg");
if (bgAtlas) {
bgAtlas.baseTexture.mipmap = PIXI.MIPMAP_MODES.OFF;
bgAtlas.baseTexture.scaleMode = PIXI.SCALE_MODES.NEAREST;
bgAtlas.baseTexture.update();
}
this.scenes.game = new SceneGame(this);
this.root.addChild(this.scenes.game);
}
private onUpdate(dt: number) {
// Generate cell view instances in the background.
const maxCells = MAX_GRID_WIDTH * MAX_GRID_HEIGHT;
const length = this.cellPool.length;
if (this.isLoaded && length < maxCells) {
for (let i = 0; i < 5; i++) {
const idx = length + i;
if (idx > maxCells - 1) {
break;
}
const [x, y] = this.state.coordsOf(idx);
this.cellPool[idx] = this.createCellView(x, y);
}
}
}
public getCellView(x: number, y: number): MSCell {
const idx = this.state.indexOf(x, y);
const cell = this.cellPool[idx];
if (!cell) {
throw new Error(`Can't find cell view at ${x},${y}`);
}
return cell;
}
private parseConfig(config: Partial<MSConfig> = {}): MSConfig {
return defaults(config, MS_CONFIG_DEFAULT);
}
private createCellView(x: number, y: number): MSCell {
const msCell = new MSCell(this);
return msCell;
}
}