-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.js
128 lines (108 loc) · 3.11 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
const Ship = require("./ship.js");
const Bullet = require("./bullet.js");
const Astroid = require("./astroid.js");
function Game (bgImg) {
this.asteroids = [];
this.bullets = [];
this.asteroidsToRemove = [];
this.bulletsToRemove = [];
this.addAsteroids();
this.ship = new Ship({"pos": this.randomPosition(), "game": this});
this.bgImg = bgImg;
}
Game.prototype.addAsteroids = function () {
for(let i = 0; i < Game.NUM_ASTEROIDS; i++){
const newAst = new Astroid({"pos":this.randomPosition(), "game":this});
this.asteroids.push(newAst);
}
};
Game.prototype.randomPosition = function () {
return [Math.random() * Game.DIM_X, Math.random() * Game.DIM_Y];
};
Game.prototype.draw = function (ctx) {
ctx.clearRect(0, 0, Game.DIM_X, Game.DIM_Y);
ctx.drawImage(this.bgImg, 0, 0);
// this.allObjects().forEach(obj => obj.draw(ctx));
this.allObjects().forEach(obj => {
if (obj === this.ship) {
obj.drawShip(ctx);
} else {
obj.draw(ctx);
}
});
};
Game.prototype.moveObjects = function(delta) {
const allObjs = this.allObjects();
for (let i = 0; i < allObjs.length; i++ ) {
allObjs[i].move(delta);
}
};
Game.prototype.wrap = function (pos) {
const wrappedPos = pos.slice();
if (pos[0] < 0){
wrappedPos[0] += Game.DIM_X;
}else if (pos[0] >= Game.DIM_X){
wrappedPos[0] -= Game.DIM_X;
}
if (pos[1] < 0){
wrappedPos[1] += Game.DIM_Y;
}else if (pos[1] >= Game.DIM_Y){
wrappedPos[1] -= Game.DIM_Y;
}
return wrappedPos;
};
Game.prototype.checkCollisons = function () {
const allObjs = this.allObjects();
for (let i = 0; i < allObjs.length - 1; i++) {
for (let j = i + 1; j < allObjs.length; j++) {
if (allObjs[i].isCollidedWith(allObjs[j])){
allObjs[i].collideWith(allObjs[j]);
allObjs[j].collideWith(allObjs[i]);
}
}
}
this.removeObjects();
};
Game.prototype.removeObjects = function () {
this.asteroidsToRemove.forEach( asteroid => {
const i = this.asteroids.indexOf(asteroid);
this.asteroids.splice(i, 1);
});
this.bulletsToRemove.forEach( bullet => {
const i = this.bullets.indexOf(bullet);
this.bullets.splice(i, 1);
});
this.asteroidsToRemove = [];
this.bulletsToRemove = [];
};
Game.prototype.renderGameOver = function () {
$('.retry').html("You win! Try again?");
$('.retry').on('click', this.reloadPage.bind(this));
};
Game.prototype.reloadPage = function () {
document.location.reload();
};
Game.prototype.step = function (delta) {
this.moveObjects(delta);
this.checkCollisons();
if (this.asteroids.length <= 0) {
this.renderGameOver();
}
};
Game.prototype.remove = function (object) {
if (object instanceof Bullet){
this.bulletsToRemove.push(object);
}else if (object instanceof Astroid){
this.asteroidsToRemove.push(object);
}
};
Game.prototype.allObjects = function() {
return this.asteroids.concat(this.bullets).concat([this.ship]);
};
Game.prototype.isOutOfBounds = function (pos) {
return (pos[0] < 0 || pos[0] >= Game.DIM_X || pos[1] < 0 || pos[1] >= Game.DIM_Y);
};
Game.DIM_X = 1100;
Game.DIM_Y = 600;
Game.NUM_ASTEROIDS = 17;
module.exports = Game;