-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.ts
82 lines (69 loc) · 2.54 KB
/
game.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
import {Dictionary} from "dictionary-types";
import {
CollisionGroupManager,
CollisionResolutionStrategy,
DisplayMode,
Engine,
Physics,
Sound, Texture
} from "excalibur";
import Preloader from "./preloader";
import resources from "./resources";
import GetReady from "./scenes/get-ready/get-ready";
import Intermission from "./scenes/intermission/intermission";
import Level1 from "./scenes/level-1/level-1";
import Level3 from "./scenes/level-3/level-3";
import Title from "./scenes/title/title";
export default class Game {
public readonly width = 384;
public readonly height = 288;
public readonly playWidth = 320;
public readonly playHeight = 200;
public readonly playLeft = (this.width - this.playWidth) * 0.5;
public readonly playTop = (this.height - this.playHeight) * 0.5;
public readonly collisionGroups = {
player: CollisionGroupManager.create("player"),
aliens: CollisionGroupManager.create("aliens")
};
public active = false;
public lives = 4;
public stage = 1;
public score = 0;
public readonly engine = new Engine({
viewport: {width: this.width, height: this.height},
resolution: {width: this.width, height: this.height},
displayMode: DisplayMode.Fixed,
antialiasing: false,
suppressHiDPIScaling: true,
suppressPlayButton: true
});
public start(): void {
const loader = new Preloader();
for (const key of Object.keys(resources)) {
const resource = (resources as Dictionary<Texture | Sound>)[key];
resource.bustCache = false;
loader.addResource(resource);
}
this.engine.input.pointers.primary.on("up", this.onClick);
this.engine.input.keyboard.on("press", this.onClick);
Physics.collisionResolutionStrategy = CollisionResolutionStrategy.Box;
this.reset();
this.engine.start(loader)
.then(() => {
this.engine.add("title", new Title(this));
this.engine.add("get-ready", new GetReady(this));
this.engine.add("intermission", new Intermission(this));
this.engine.add("level-1", new Level1(this));
this.engine.add("level-3", new Level3(this));
this.engine.goToScene("title");
}, reason => console.error("", reason));
}
public reset(): void {
this.lives = 4;
this.stage = 1;
this.score = 0;
}
private readonly onClick = () => {
this.engine.canvas.focus();
}
}