-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameScene.js
501 lines (471 loc) · 17.3 KB
/
GameScene.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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
let gameState = {
player: {},
computer: {},
computerSprite: {},
playerHealthBar: {},
computerHealthBar: {},
attackButton: {},
defendButton: {},
specialButton: {},
information: {},
playerMove: {},
computerMove: {},
waveCount: 0,
opponents: []
};
class GameScene extends Phaser.Scene {
constructor() {
super({
key: 'GameScene'
});
}
preload() {
const baseURL = 'https://content.codecademy.com/courses/learn-phaser/electric-mouse/'
this.load.image('Background', `${baseURL}background.png`);
this.load.image('Attack', `${baseURL}button-attack.png`);
this.load.image('Defend', `${baseURL}button-defend.png`);
this.load.image('Special', `${baseURL}button-special.png`);
const framePxWidth = 1500 / 9;
this.load.spritesheet('Electric Mouse', `${baseURL}electric-mouse.png`, {
frameWidth: framePxWidth,
frameHeight: 128,
endFrame: 8
});
this.load.spritesheet('Owl', `${baseURL}owl.png`, {
frameWidth: framePxWidth,
frameHeight: 132,
endFrame: 8
});
this.load.spritesheet('Red Owl', `${baseURL}owl-red.png`, {
frameWidth: framePxWidth,
frameHeight: 132,
endFrame: 8
});
this.load.spritesheet('Blue Owl', `${baseURL}owl-blue.png`, {
frameWidth: framePxWidth,
frameHeight: 132,
endFrame: 8
});
this.load.spritesheet('Psychic Hairless Cat', `${baseURL}psychic-cat.png`, {
frameWidth: framePxWidth,
frameHeight: 148,
endFrame: 11
});
} // End of Preload
create() {
// Character objects
// Add the player object below:
gameState.player ={
name: 'Electric Mouse',
health: 25,
frames: [{
key: 'playerIdle',
start:0,
end:2
},
{
key: 'playerAttack',
start:3,
end:4
},
{
key: 'playerDefend',
start:5,
end:6
},
{
key: 'playerSpecial',
start:7,
end:8
}],
lives: 2,
maxHealth: 25
}
gameState.opponents = [{
name: 'Owl',
health: 10,
frames: [{
key: 'OwlIdle',
start: 0,
end: 2
},
{
key: 'OwlAttack',
start: 3,
end: 4
},
{
key: 'OwlDefend',
start: 5,
end: 6
},
{
key: 'OwlSpecial',
start: 7,
end: 8
},
],
maxHealth: 10
},
{
name: 'Red Owl',
health: 12,
frames: [{
key: 'Red OwlIdle',
start: 0,
end: 2
},
{
key: 'Red OwlAttack',
start: 3,
end: 4
},
{
key: 'Red OwlDefend',
start: 5,
end: 6
},
{
key: 'Red OwlSpecial',
start: 7,
end: 8
},
],
maxHealth: 12
},
{
name: 'Blue Owl',
health: 15,
frames: [{
key: 'Blue OwlIdle',
start: 0,
end: 2
},
{
key: 'Blue OwlAttack',
start: 3,
end: 4
},
{
key: 'Blue OwlDefend',
start: 5,
end: 6
},
{
key: 'Blue OwlSpecial',
start: 7,
end: 8
},
],
maxHealth: 15
},
{
name: 'Psychic Hairless Cat',
health: 45,
frames: [{
key: 'Psychic Hairless CatIdle',
start: 0,
end: 3
},
{
key: 'Psychic Hairless CatAttack',
start: 4,
end: 7
},
{
key: 'Psychic Hairless CatDefend',
start: 8,
end: 9
},
{
key: 'Psychic Hairless CatSpecial',
start: 10,
end: 11
}
],
maxHealth: 45
}
];
// Set the current opponent below:
gameState.computer = gameState.opponents[0] ;
// Adds in the background
this.add.sprite(240, 320, 'Background').setScale(.5);
// Creates the initial sprites
gameState.player.sprite = this.add.sprite(115, 275, 'Electric Mouse')
gameState.computerSprite = this.add.sprite(375, 275, 'Owl') // first enemy
// Creates all of the player animations
gameState.player.frames.forEach(frame => {
this.anims.create({
key: frame.key,
frames: this.anims.generateFrameNumbers(gameState.player.name, {
start: frame.start,
end: frame.end,
}),
frameRate: 3,
repeat: -1,
yoyo: true
});
});
// Creates all the computer animations for each opponent
gameState.opponents.forEach(opponent => {
opponent.frames.forEach(frame => {
this.anims.create({
key: frame.key,
frames: this.anims.generateFrameNumbers(opponent.name, {
start: frame.start,
end: frame.end
}),
frameRate: 3,
repeat: -1,
yoyo: true
});
});
});
// Plays initial animation
gameState.player.sprite.anims.play('playerIdle');
gameState.computerSprite.anims.play(`${gameState.computer.name}Idle`);
// Renders the buttons in game and allows them to be clicked
gameState.attackButton = this.add.sprite(90, 550, 'Attack').setInteractive();
gameState.defendButton = this.add.sprite(240, 550, 'Defend').setInteractive();
gameState.specialButton = this.add.sprite(390, 550, 'Special').setInteractive();
// Add your information text and styling below:
const style = {
font: '16px Helvetica',
fill: '#000000',
padding: {
x:6,
y:7
}
};
gameState.playerMove = this.add.text(65, 140, '', style);
gameState.computerMove = this.add.text(320, 140, '', style);
gameState.information = this.add.text(140, 80, '', style);
//Health Bars
gameState.playerHealthBar = this.add.text( 45, 45,`HP: ${gameState.player.health}. Lives: ${gameState.player.lives}` , style);
gameState.computerHealthBar = this.add.text(375, 45, `HP: ${gameState.computer.health}`, style);
// update player lives - halth
function updateLive(){
if(gameState.computer.health < 0){
gameState.computer.health = 0
}
if(gameState.computer.health > gameState.computer.maxHealth){
gameState.computer.health = gameState.computer.maxHealth;
}
if(gameState.player.health > gameState.player.maxHealth){
gameState.player.health = gameState.player.maxHealth
}
if (gameState.player.health <= 0 && gameState.player.lives > 0) {
gameState.player.lives -= 1;
gameState.player.health = gameState.player.maxHealth;
gameState.information.text = `The Mouse lose 1 Live!`;
}
}
// Attack button logic:
gameState.attackButton.on('pointerup', () => {
this.pauseIdle()
if (game.input.enabled) {
// Add your code for Electric Mouse attacking below:
let randomMove = Math.floor(Math.random()* 3);
//Player attack and computer attack
if(randomMove === 0){
//characters health update
gameState.player.health -= 1;
gameState.computer.health -= 1;
updateLive();
//characters text update
gameState.playerMove.text = 'Mouse Attack!';
gameState.computerMove.text = 'Enemy Attack!';
// characters health bar update
gameState.computerHealthBar.text = `HP: ${gameState.computer.health}`;
gameState.playerHealthBar.text = `HP: ${gameState.player.health}. Lives: ${gameState.player.lives}`;
//info text update
gameState.information.text = `Ouch! You both lose 1 HP!`;
//characters animation update
gameState.player.sprite.anims.play('playerAttack');
gameState.computerSprite.anims.play(`${gameState.computer.name}Attack`);
}//player attack, computer defend
else if(randomMove === 1){
//characters health update
gameState.player.health -= 1;
//gameState.computer.health -= 0;
updateLive();
//characters text update
gameState.playerMove.text = 'Mouse Attack!';
gameState.computerMove.text = 'Enemy Defend!';
//characters helth bar update
gameState.computerHealthBar.text = `HP: ${gameState.computer.health}`;
gameState.playerHealthBar.text = `HP: ${gameState.player.health}. Lives: ${gameState.player.lives}`;
//info text update
gameState.information.text = `Electric Mouse's attack failed! It loses 1 HP!`;
//characters animation update
gameState.player.sprite.anims.play('playerAttack');
gameState.computerSprite.anims.play(`${gameState.computer.name}Defend`);
}//player attack, computer special attack
else {//randomMove === 2
//characters health update
//gameState.player.health -= 0;
gameState.computer.health -= 5;
updateLive();
//characters text update
gameState.playerMove.text = 'Mouse Attack!';
gameState.computerMove.text = 'Special Attack!';
//characters helth bar update
gameState.computerHealthBar.text = `HP: ${gameState.computer.health}`;
gameState.playerHealthBar.text = `HP: ${gameState.player.health}. Lives: ${gameState.player.lives}`;
//info text update
gameState.information.text = `The ${gameState.computerSprite.texture.key} loses 5 HP!`;
//characters animation update
gameState.player.sprite.anims.play('playerAttack');
gameState.computerSprite.anims.play(`${gameState.computer.name}Special`);
}
}
});
// Defend button logic:
gameState.defendButton.on('pointerup', () => {
this.pauseIdle()
if (game.input.enabled) {
// Add your code for Electric Mouse defending below:
let randomMove = Math.floor(Math.random()* 3);
//Player defend and computer attack
if(randomMove === 0){
//characters health update
gameState.computer.health -= 1;
updateLive();
//characters text update
gameState.playerMove.text = 'Mouse Defend!';
gameState.computerMove.text = 'Enemy Attack!';
//characters helth bar update
gameState.computerHealthBar.text = `HP: ${gameState.computer.health}`;
gameState.playerHealthBar.text = `HP: ${gameState.player.health}. Lives: ${gameState.player.lives}`;
//info text update
gameState.information.text = `The ${gameState.computerSprite.texture.key} lose 1 HP!`;
//characters animation update
gameState.player.sprite.anims.play('playerDefend');
gameState.computerSprite.anims.play(`${gameState.computer.name}Attack`);
}//player defend, computer defend
else if(randomMove === 1){
//characters health update
gameState.player.health += 5;
gameState.computer.health += 5;
updateLive();
//characters text update
gameState.playerMove.text = 'Mouse Defend.';
gameState.computerMove.text = 'Enemy Defend.';
gameState.computerHealthBar.text = `HP: ${gameState.computer.health}`;
gameState.playerHealthBar.text = `HP: ${gameState.player.health}. Lives: ${gameState.player.lives}`;
//info text update
gameState.information.text = `Both healed +5 HP!`;
//gameState.information.text = `Both Defended!`;
//characters animation update
gameState.player.sprite.anims.play('playerDefend');
gameState.computerSprite.anims.play(`${gameState.computer.name}Defend`);
}//player defend, computer special attack
else{ //randomMove === 2
//characters health update
gameState.player.health -= 4;
//gameState.computer.health -= 0;
updateLive();
//characters text update
gameState.playerMove.text = 'Mouse Defend!';
gameState.computerMove.text = 'Special Attack!';
//characters helth bar update
gameState.computerHealthBar.text = `HP: ${gameState.computer.health}`;
gameState.playerHealthBar.text = `HP: ${gameState.player.health}. Lives: ${gameState.player.lives}`;
//info text update
gameState.information.text = `Oach! Mouse loses 4 HP!`;
//characters animation update
gameState.player.sprite.anims.play('playerDefend');
gameState.computerSprite.anims.play(`${gameState.computer.name}Special`);
}
}
});
// Special Attack button logic (use for reference):
gameState.specialButton.on('pointerup', () => {
this.pauseIdle()
if (game.input.enabled) {
let randomMove = Math.floor(Math.random() * 3);
//player special attack, computer attack
if (randomMove === 0) {
gameState.information.text = `The player loses 5 HP!`;
gameState.playerMove.text = 'Special Attack!';
gameState.computerMove.text = 'Attack!';
//update health
updateLive();
gameState.player.health -= 5;
gameState.playerHealthBar.text = `HP: ${gameState.player.health}. Lives: ${gameState.player.lives}`;
gameState.player.sprite.anims.play('playerSpecial');
gameState.computerSprite.anims.play(`${gameState.computer.name}Attack`);
} // player special - enemy defend
else if (randomMove === 1) {
gameState.information.text = `The ${gameState.computerSprite.texture.key} loses 4 HP!`;
gameState.playerMove.text = 'Special Attack!';
gameState.computerMove.text = 'Defend!';
gameState.computer.health -= 4;
//update health
updateLive();
gameState.computerHealthBar.text = `HP: ${gameState.computer.health}`;
gameState.playerHealthBar.text = `HP: ${gameState.player.health}. Lives: ${gameState.player.lives}`;
gameState.player.sprite.anims.play('playerSpecial');
gameState.computerSprite.anims.play(`${gameState.computer.name}Defend`);
} // player special - enemy special
else {
gameState.information.text = `You both lose 10 HP!`;
gameState.playerMove.text = 'Special Attack!';
gameState.computerMove.text = 'Special Attack!';
gameState.player.health -= 10;
gameState.computer.health -= 10;
//update players health
updateLive();
gameState.playerHealthBar.text = `HP: ${gameState.player.health}. Lives: ${gameState.player.lives}`;
gameState.computerHealthBar.text = `HP: ${gameState.computer.health}`;
gameState.player.sprite.anims.play('playerSpecial');
gameState.computerSprite.anims.play(`${gameState.computer.name}Special`);
}
}
});
} // End of Create
update() {
this.waveCheck();
} // End of Update
// Helper Functions
// Check to see that the game is still running and plays the animation of the idle animation current enemy and Electric Mouse
pauseIdle() {
setTimeout(function () {
if (gameState.waveCount !== 4 && gameState.player.lives >= 0) {
gameState.player.sprite.anims.play('playerIdle');
gameState.computerSprite.anims.play(`${gameState.computer.name}Idle`);
}
}, 1000)
}
// Manages the logic of changing the enemy waves
waveCheck() {
if (gameState.computer.health <= 0) {
gameState.waveCount++;
gameState.information.text = `The ${gameState.computer.name} has fainted!`;
gameState.computer = gameState.opponents[gameState.waveCount];
gameState.playerMove.text = '';
gameState.computerMove.text = '';
if (gameState.waveCount > 3) {
this.scene.stop('GameScene');
this.scene.start('EndScene');
} else if (gameState.waveCount < 3) {
game.input.enabled = false;
setTimeout(function () {
const text = gameState.waveCount === 3
? 'The Final Boss, the Psychic Hairless Cat!'
: `A ${gameState.computer.name} appears`;
gameState.information.text = text;
gameState.computerHealthBar.text = `HP: ${gameState.computer.health}`;
gameState.computerSprite.setTexture(gameState.computer.name);
setTimeout(function () {
game.input.enabled = true;
}, 1500);
}, 1000);
}
}
else if (gameState.player.lives <= 0 && gameState.player.health <= 0) {
this.scene.stop('GameScene');
this.scene.start('EndScene');
}
}
} // end GameScen