-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgameManager.ts
100 lines (83 loc) · 3.09 KB
/
gameManager.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
100
class GameManager {
private _gameObjects : Array<GameObject> = new Array();
private static _instance : GameManager;
public lose : boolean = false;
public win : boolean = false;
public pause : boolean = false;
public static getInstance() {
if (!GameManager._instance) {
GameManager._instance = new GameManager()
}
return GameManager._instance
}
public addGameObject(obj:GameObject) {
GameManager._instance._gameObjects.push(obj);
}
public removeGameObject(obj:GameObject) {
let i:number = this._gameObjects.indexOf(obj);
if(i != -1) {
this._gameObjects.splice(i, 1);
}
}
public resetLevel() {
this.lose = false;
this.win = false;
for( var i = this._gameObjects.length-1; i >= 0; i-- ) {
this._gameObjects[i].remove();
}
}
public togglePause() {
if (!this.win && !this.lose) {
AudioManager.playPauseSound(this.pause);
this.pause = !this.pause;
}
}
private checkGameStatus() {
if (!this._gameObjects.some((ship) => ship instanceof Ship)) {
this.lose = true;
} else if (!this._gameObjects.some((asteroid) => asteroid instanceof Asteroid)) {
this.win = true;
}
}
public loop() {
if (!this.pause) {
this.checkGameStatus();
for (let obj of this._gameObjects) {
for (let otherobj of this._gameObjects){
// SHIP COLLISION
if (obj instanceof Ship) {
if (otherobj instanceof Asteroid) {
if (obj.hasCollision(otherobj) && !obj.invincable) {
obj.hit();
otherobj.explode();
}
} else if (otherobj instanceof Upgrade) {
if (obj.hasCollision(otherobj)) {
obj.shootBehaviour = otherobj.activate(obj);
otherobj.remove();
}
} else if (otherobj instanceof Bomb) {
if (obj.hasCollision(otherobj)) {
otherobj.notifyObs();
otherobj.remove();
}
}
}
// BULLET COLLISION
if(obj instanceof Bullet) {
if (otherobj instanceof Asteroid) {
if (obj.hasCollision(otherobj)) {
AudioManager.playRandomExplosionSound();
otherobj.explode();
obj.remove();
}
}
}
}
obj.update();
obj.draw();
}
KeyboardInput.getInstance().inputLoop();
}
}
}