-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGame.js
78 lines (63 loc) · 2.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
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
class Game {
constructor(level, obj) {
this.obj = obj;
this.hero = new Hero(this.obj);
this.gameBoard = new GameBoard(level, this.obj);
this.walls = this.gameBoard.getWalls();
this.turrets = this.gameBoard.getTurrets();
this.crosses = this.gameBoard.getCrosses();
this.balls = this.gameBoard.getBalls();
this.scoreElem = this.obj.createDiv('Game in progress');
this.scoreElem.position(0, this.obj.height);
this.scoreElem.class('game-progress');
this.deathCountElem = this.obj.createDiv('Death count: ');
this.deathCountElem.position(160, this.obj.height+5);
this.deathCountElem.class('death-count-before');
this.deathCountNumber = this.obj.createSpan('0');
this.deathCountNumber.position(260, this.obj.height + 9);
this.deathCountElem.class('death-count-after');
}
load() {
//loading boxes and turrets
this.gameBoard.show();
//loading hero
this.hero.show();
this.hero.update(this.walls);
if (this.turrets.length > 0 && bullets.length > 0){
this.hero.isHitByBullets(bullets);
}
if (this.crosses){
this.hero.isHitByCrosses(this.crosses);
}
if (this.balls){
this.hero.isHitByBalls(this.balls);
}
this.checkIfHeroIsAlive();
this.checkIfGameIsCompleted();
}
checkIfHeroIsAlive() {
if (!this.hero.isAlive) {
this.hero.deathCount++;
this.obj.noLoop();
this.scoreElem.html('Game ended!');
this.deathCountNumber.html(this.hero.deathCount);
}
}
checkIfGameIsCompleted() {
let endingBox = this.gameBoard.getEndingBox();
if (this.obj.collideRectRect(this.hero.x, this.hero.y, this.hero.diameter, this.hero.diameter, endingBox.x, endingBox.y, endingBox.w, endingBox.h)) {
this.obj.noLoop();
this.scoreElem.html('Level completed!');
nextLevelButton.removeAttribute('disabled');
Cookies.set('level_'+(activeLevel+1), 'true', { expires: 365 });
}
}
reset() {
this.hero.isAlive = true;
this.hero.color = 'red';
this.hero.x = 25;
this.hero.y = 25;
this.scoreElem.html('Game in progress');
this.obj.loop();
}
}