-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathapp.js
321 lines (278 loc) · 7.02 KB
/
app.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
// IDEAS:
// Floaty glowing spots in the foreground kinda like the chemical
// brothers video with the glowing face.. Could be some nice depth
// effects. Depth would be good to investigate with the game anyway
// to give hte effect of motion, paralax etc.
(function(){
var SPAWN_TIME = 2000;
var difficultyMultiplier = 1;
var nextEnemyTime = SPAWN_TIME;
// The ship
var ship;
// The score model that we add points to when an enemy is killed
var scoreModel;
// The time that has passed since the last enemy was spawned
var enemyTime = 0;
// If we're in the game over state
var _gameOver = false;
var _running = false;
// An array of enemy type variations that can be selected from
// when spawning a new one
var _enemyTypes;
var _enemiesKilled = _enemiesEscaped = 0;
// All the possible enemy types in order of difficulty
var _allEnemyTypes = [
{
health: 1
},
{
health: 2
},
{
health: 3
},
{
health: 4
},
{
health: 5
}
];
var _allShipPowerups = [
/*{
burstLength: 1,
roundDelay: 90
},*/
{
burstLength: 2,
roundDelay: 90
},
{
burstLength: 3,
roundDelay: 90
},
{
burstLength: 5,
roundDelay: 90
},
{
burstLength: 5,
roundDelay: 60
}
];
var initialize = function(assets, start){
// Add the instruction screen
assets.add(new Instructions({
message: 'Press [Enter] to start!',
bounds: {
top: 0,
right: game.width,
bottom: game.height,
left: 0
},
start: start
}));
}
// setupGameAssets is passed an array of game assets. Add
// to this array to automatically update and draw them
// each frame.
var setupGameAssets = function(assets){
_running = true;
enemyTime = 0;
// Set the initial possible enemy type including the
// first enemy only
_enemyTypes = [_allEnemyTypes[0]];
_enemiesKilled = _enemiesEscaped = 0;
difficultyMultiplier = 1;
scoreModel = new ScoreModel({
// When increaseDifficulty is called add the next difficulty enemy
// to the enemy types pool. This runs through each enemy index as
// difficultyLevel increments one each callback. If we run out of
// enemies to add then the last (most difficult) enemy will be
// added again to increase the likelyhood of it being selected.
increaseDifficulty: function(difficultyLevel){
if(_allEnemyTypes.length > difficultyLevel){
_enemyTypes.push(_allEnemyTypes[_allEnemyTypes.length-1, difficultyLevel]);
}
if(difficultyMultiplier > 0.6){
difficultyMultiplier = Math.max(0.6, difficultyMultiplier - 0.03);
}
},
// When increasePowerup() is called we apply the upgrade for that
// lever to the ship for as long as there are upgrades to apply.
increasePowerup: function(powerupLevel){
if(_allShipPowerups.length > powerupLevel){
ship.powerup(_allShipPowerups[powerupLevel]);
}
}
});
var bounds = {
top: 0,
right: game.width,
bottom: game.height,
left: 0
};
assets.add(new Background({
width: game.width,
height: game.height
}));
// assets.add(new FrameTimer({
// bounds: bounds
// }));
assets.add(new ScoreBoard({
bounds: bounds,
scoreModel: scoreModel
}));
ship = assets.add(new Ship({
x: game.width/2,
y: game.height-135,
bounds: bounds
}));
ship.powerup(_allShipPowerups[0]);
}
// Update anything in addition to registered assets
var update = function(frameTime){
if(_gameOver){
if(Input.restart()){
_gameOver = false;
game.run();
}
return;
}
// Spawn enemies as time passes
enemyTime += frameTime;
if(enemyTime > (nextEnemyTime)){
enemyTime = 0;
// Calculate the number of ms until we spawn another enemy
nextEnemyTime = SPAWN_TIME * difficultyMultiplier;
// Pick a random enemy from the pool, this pool gets more difficult
// enemies added over time as more enemies are killed.
var options = extend({
x: (Math.random() * (game.width-40)) + 20,
y: -20,
bounds: {
top: -20,
right: game.width,
bottom: game.height+20,
left: 0
},
escaped: function(){
scoreModel.resetMultiplier();
++_enemiesEscaped;
}
}, _enemyTypes[Math.floor(Math.random() * (_enemyTypes.length))]);
game.assets.add(new Enemy(options));
}
var bullets = Bullet.instances;
var enemies = Enemy.instances;
// Run through each enemy to check for both bullet and ship collisions.
// Doing this in the same loop saves on CPU time.
for(var i=enemies.length-1; i>-1; --i){
var enemy = enemies[i];
var destroyed = false;
// Check if any bullets hit them
for(var r=bullets.length-1; r>-1; --r){
var bullet = bullets[r];
if(bullet.hits(enemy)){
bullet.destroy();
if(enemy.damage({
damage: 1,
angle: bullet.angle,
speed: bullet.speed * 0.8,
x: bullet.x,
y: bullet.y
})){
// If an enemy is destroyed then we don't need to check collisions
// against the rest of the bullets.
destroyed = true;
scoreModel.add(10);
++_enemiesKilled;
break;
}
}
}
// This enemy has been killed then don't check it for ship collisions
if(destroyed) continue;
// Check if they hit the ship
if(ship.hits(enemy)){
// Kill all the enemies
killAllEnemies();
// Apply the damage to the ship, if it's run out of health as a result then it's game over!
if(ship.damage(1)){
gameOver();
return;
}
}
}
};
// Kill all enemies
var killAllEnemies = function(){
var enemies = Enemy.instances;
for(var i=enemies.length-1; i>-1; --i){
var enemy = enemies[i];
enemy.destroy({
explode: true,
x: enemy.x,
y: enemy.y,
speed: 5,
angle: 0,
angleVariation: 6.28
});
scoreModel.add(10);
++_enemiesKilled;
}
difficultyMultiplier = Math.min(1, Math.max(0.6, difficultyMultiplier + 0.1));
}
var gameOver = function(){
_gameOver = true;
// Destroy the ship
ship.destroy();
// Save the score
scoreModel.save();
// Show Game Over
game.assets.add(new GameOver({
bounds: {
top: 0,
right: game.width,
bottom: game.height,
left: 0
},
scoreModel: scoreModel,
bulletsFired: ship.bulletsFired,
enemiesKilled: _enemiesKilled,
enemiesescaped: _enemiesEscaped
}));
};
// Draw anything in addition to registered assets
var draw = function(ctx){};
// If the pause button is pressed then pause the game loop
document.addEventListener('keydown', function(e){
if(!_gameOver && _running && e.keyCode == 80){
if(game.paused){
game.assets.remove(instructions);
game.pause(false);
}else{
instructions = new Instructions({
state: 'paused',
bounds: {
top: 0,
right: game.width,
bottom: game.height,
left: 0
}
});
game.assets.add(instructions);
game.pause(true);
}
}
});
// Start the game loop
var game = window.game = new GameLoop({
canvas: $('#canvas'),
initialize: initialize,
setupGameAssets: setupGameAssets,
update: update,
draw: draw
});
game.run();
})();