forked from udacity/frontend-nanodegree-arcade-game
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
358 lines (275 loc) · 10.7 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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
// used to jump in and out of animation loop
var gameReady = false;
// Rocks that can be climbed on
var Rock = function(x, direction, row, rate, url) {
this.direction = direction;
this.x = x;
this.y = row * 32;
this.rate = rate;
this.sprite = Resources.get(url);
}
Rock.prototype.update = function(dt) {
Enemy.prototype.update.call(this, dt);
// needs to be able to increment player.x and player.y by the same incredmental rate*dt as the rock
// only if player is on top of that particular rock...
}
Rock.prototype.render = function() {
ctx.drawImage(this.sprite, this.x, this.y);
}
function instantiateRocks() {
var rocksRow0 = [
new Rock(0, 'right', 2, 60, 'images/Rock-quad.png'),
new Rock(175, 'right', 2, 60, 'images/Rock-quad.png'),
new Rock(350, 'right', 2, 60, 'images/Rock-quad.png')
];
var rocksRow1 = [
new Rock(550, 'left', 3, 50, 'images/Rock-double-brown.png'),
new Rock(450, 'left', 3, 50, 'images/Rock-double-brown.png'),
new Rock(350, 'left', 3, 50, 'images/Rock-double-brown.png'),
new Rock(250, 'left', 3, 50, 'images/Rock-double-brown.png'),
new Rock(150, 'left', 3, 50, 'images/Rock-double-brown.png')
];
var rocksRow2 = [
new Rock(-100, 'right', 4, 75, 'images/Rock-six.png'),
new Rock(450, 'right', 4, 75, 'images/Rock-double.png'),
new Rock(300, 'right', 4, 75, 'images/Rock-six.png')
];
var rocksRow3 = [
new Rock(100, 'left', 5, 40, 'images/Rock-double-brown.png'),
new Rock(225, 'left', 5, 40, 'images/Rock-double-brown.png'),
new Rock(400, 'left', 5, 40, 'images/Rock-double-brown.png')
];
var rocksRow4 = [
new Rock(475, 'right', 6, 50, 'images/Rock-triple.png'),
new Rock(300, 'right', 6, 50, 'images/Rock-triple.png'),
new Rock(125, 'right', 6, 50, 'images/Rock-triple.png'),
new Rock(-50, 'right', 6, 50, 'images/Rock-triple.png')
];
window.allRocks = [].concat(rocksRow0, rocksRow1, rocksRow2, rocksRow3, rocksRow4);
}
// Enemies the player must avoid
var Enemy = function(x, direction, row, url) {
this.direction = direction;
this.row = row;
this.x = x;
this.y = row * 32;
this.sprite = Resources.get(url);
};
Enemy.prototype.rate = 50;
// Update the enemy's position, required method for game
// Parameter: dt, a time delta between ticks
Enemy.prototype.update = function(dt) {
// enemies will be able to travel 'off' canvas by the defined # of pixels so they don't start wrapping
// as soon as they exit on side
var offCanvasLength = 100;
var spriteWidth = this.sprite.width;
// if movement would be out of bounds,
// wrap to next side
if ( this.direction === 'left' ) {
if ( this.x > - offCanvasLength) {
this.x -= this.rate * dt;
} else {
this.x = canvas.width + offCanvasLength;
this.x -= this.rate * dt;
}
}
if ( this.direction === 'right') {
if ( this.x < canvas.width + offCanvasLength ) {
this.x += this.rate * dt;
} else {
this.x = - ( offCanvasLength + spriteWidth );
this.x += this.rate * dt;
}
}
};
Enemy.prototype.render = function() {
ctx.drawImage(this.sprite, this.x, this.y);
};
function instantiateEnemies() {
// Define Enemy Instances;
var enemy1_row9 = new Enemy(400, 'left', 8, 'images/enemy-bug-left-Length3.png');
var enemy2_row9 = new Enemy(200, 'left', 8, 'images/enemy-bug-left-Length3.png');
var enemy3_row9 = new Enemy(0, 'left', 8, 'images/enemy-bug-left-Length3.png');
var enemy1_row10 = new Enemy(50, 'right', 9, 'images/enemy-bug-right-Length1.png');
var enemy2_row10 = new Enemy(175, 'right', 9, 'images/enemy-bug-right-Length1.png');
var enemy3_row10 = new Enemy(300, 'right', 9, 'images/enemy-bug-right-Length1.png');
var enemy4_row10 = new Enemy(425, 'right', 9, 'images/enemy-bug-right-Length1.png');
var enemy1_row11 = new Enemy(400, 'left', 10, 'images/enemy-bug-left-Length1.png');
var enemy2_row11 = new Enemy(275, 'left', 10, 'images/enemy-bug-left-Length1.png');
var enemy3_row11 = new Enemy(150, 'left', 10, 'images/enemy-bug-left-Length1.png');
var enemy4_row11 = new Enemy(0, 'left', 10, 'images/enemy-bug-left-Length1.png');
var enemy1_row12 = new Enemy(0, 'right', 11, 'images/enemy-bug-right-Length1.png');
var enemy2_row12 = new Enemy(150, 'right', 11, 'images/enemy-bug-right-Length1.png');
var enemy3_row12 = new Enemy(300, 'right', 11, 'images/enemy-bug-right-Length1.png');
var enemy4_row12 = new Enemy(450, 'right', 11, 'images/enemy-bug-right-Length1.png');
var enemy1_row13 = new Enemy(450, 'left', 12, 'images/enemy-bug-left-Length1.png');
var enemy2_row13 = new Enemy(300, 'left', 12, 'images/enemy-bug-left-Length1.png');
var enemy3_row13 = new Enemy(150, 'left', 12, 'images/enemy-bug-left-Length1.png');
// [refactor:] this is a bit cumbersone, not very maintainable...adding new enemies requires a bit of manual updating of the array
// an easier way would be to start by defining an array for each row, and then pushing each array into the allEnemies array
window.allEnemies = [
[enemy1_row9, enemy2_row9, enemy3_row9],
[enemy1_row10, enemy2_row10, enemy3_row10, enemy4_row10],
[enemy1_row11, enemy2_row11, enemy3_row11],
[enemy1_row12, enemy2_row12, enemy3_row12, enemy4_row11],
[enemy1_row13, enemy2_row13, enemy3_row13]
]
}
// Now write your own player class
// This class requires an update(), render() and
// a handleInput() method.
var Player = function() {
this.x = 224;
this.y = 416;
this.score = 0;
this.numLives = 3;
//fetches player selected imgUrl and stores imgObj on this.sprite
this.sprite = Resources.get(Resources.playerChoice);
}
Player.prototype.update = function() {
// might not need dt? incremental movement?
// not sure why I need an update
}
Player.prototype.render = function() {
ctx.drawImage(this.sprite, this.x, this.y);
}
Player.prototype.hasKey = function() {
if (player.y === 32) {
if (
(player.x >= key.x && player.x <= key.x + key.sprite.width)
||
(player.x + player.sprite.width >= key.x && player.x + player.sprite.width <= key.x + key.sprite.width)
)
{
console.log("player has the key");
return true;
}
}
}
Player.prototype.updateAfterScore = function() {
var now = Math.floor(Date.now() / 1000);
// this.lastUpdated - now != 0, is a workout to prevent this from running 60 times/second
// current not working the way I want nor intuitive, I need score to only register once, not every loop of the animation frame
if ( player.hasKey() && this.lastUpdated - now != 0 ) {
this.score += 100;
setTimeout(function() {
player.x = 224;
player.y = 416;
}, 300);
this.lastUpdated = now;
increaseGameRate();
}
}
Player.prototype.handleInput = function(pressedKey) {
switch (pressedKey) {
case 'left':
if (this.x - 1 >= 0) {
this.x -= 32;
}
break;
case 'up':
if (this.y - 1 >= 0) {
this.y -= 32;
}
break;
case 'right':
if (this.x + 33 < canvas.width) {
this.x += 32;
}
break;
case 'down':
if (this.y + 33 < canvas.height - 2 * 32) {
this.y += 32;
}
break;
}
}
Player.prototype.reset = function() {
this.x = 224;
this.y = 416;
this.score = 0;
this.numLives = 3;
//this needs to be uddated to be able to handle different player choices...
// this.sprite = Resources.get("images/char-boy-dead.png");
}
Player.prototype.isDead = false;
Player.prototype.lastUpdated;
Player.prototype.updateAfterDeath = function() {
var originalSpriteWidth = this.sprite.width;
var deadCharImgUrl = Resources.playerChoice.slice(0, -4) + "-dead.png";
this.isDead = true;
this.sprite = Resources.get(deadCharImgUrl);
// this adjusts for the different size of the sprites, so that it renders in the correct 'adjusted' position
this.x -= (this.sprite.width - originalSpriteWidth) / 2;
this.render();
// delays the 'reset'
// code getting spaghetti, need to refactor and abstract into other functions
// [solved:] why does the this context change within setTimeout? (b/c setTimeout itself is a property on window so by calling this becomes the window)
setTimeout(function() {
player.isDead = false;
player.x = 224;
player.y = 416;
player.sprite = Resources.get(Resources.playerChoice);
}, 400);
}
// Place the player object in a variable called player
function createPlayer() {
window.player = new Player();
}
function Key() {
this.x = 0;
this.y = 32;
this.sprite = Resources.get('images/Key.png');
this.lastUpdated;
this.update = function() {
var now = Math.floor(Date.now() / 1000); //integer # of seconds (relative)
// updates every 10 seconds, moves to random location; this.lastUpdated - now != 0 prevents it from running 60times/second instead only does once/second
if ( now % 10 === 0 && this.lastUpdated - now != 0) {
console.log("moved key");
this.x = Math.floor(Math.random() * 14) * 32;
this.lastUpdated = now;
}
};
this.render = function() {
ctx.drawImage(this.sprite, this.x, this.y);
};
}
function createKey() {
window.key = new Key();
}
function PointsBubble() {
this.y = 0;
this.sprite = Resources.get("images/100Points.png");
this.render = function() {
if ( player.hasKey() ) {
ctx.drawImage(this.sprite, player.x + player.sprite.width, this.y);
}
};
}
function createPointsBubble() {
window.pointsBubble = new PointsBubble();
}
function increaseGameRate() {
var ratePercentIncrease = 1.03;
allEnemies.forEach(function(row) {
row.forEach(function(enemy) {
enemy.rate *= ratePercentIncrease;
});
});
allRocks.forEach(function(rock) {
rock.rate *= ratePercentIncrease;
});
}
// This listens for key presses and sends the keys to your
// Player.handleInput() method. You don't need to modify this.
function addKeydownEventListener() {
document.addEventListener('keydown', function(e) {
var allowedKeys = {
37: 'left',
38: 'up',
39: 'right',
40: 'down'
};
player.handleInput(allowedKeys[e.keyCode]);
});
}