-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathgame.js
52 lines (47 loc) · 1.13 KB
/
game.js
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
/**
* game类
* by littlefean
*/
class Game {
/**
*
* @param world {World}
*/
constructor(world) {
this.gameSpeed = 25; // 迭代一步需要多少毫秒,这个数字越大,速度越慢,必须大于零
this.gameWorld = world;
this.gameCanvasEle = null;
this.isEnd = false; // 是否是结束状态
this.isPaused = false; // 是否是暂停状态
}
/**
* 开启游戏循环
*/
start() {
let main = setInterval(() => {
if (this.isPaused) {
} else {
this.gameWorld.goTick();
this.gameWorld.render(this.gameCanvasEle);
if (this.isGameFalse()) {
this.gameEndFunc();
clearInterval(main);
}
}
}, this.gameSpeed);
}
/**
* 失败回调函数
*/
gameEndFunc() {
alert("you fail!");
location.reload();
}
/**
* 判断是否失败
* @returns {boolean}
*/
isGameFalse() {
return this.gameWorld.rootBuilding.isDead() || this.isEnd;
}
}