-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
539 lines (442 loc) · 17.7 KB
/
main.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
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
var Main = function(game) {
console.log('Main State Loaded');
this.X_GAMESPEED = 500; // Speed of the game in pixels per second
this.ACCELERATION_FACTOR = 10 / this.X_GAMESPEED; // Used to calculate acceleration while in endless mode
this.IS_DEBUG_MODE = false; // Used to track whether game is in debug mode or not
this.isVictory = false; // Flag to keep track of victory on level
this.jumpCount = 0; // Tracks current amount of jumps, for double jumping
this.score = 0; // Keeps track of the player's score internally
this.scoreText = ''; // Text display element of the score
this.isEndlessMode = false; // Flag to toggle endless mode differences
this.timer = null; // Stores the phaser timer created to increment the score
this.blockType = 1; // 1 = brick, 2: spike, 3: coin, 4: floor
this.quantity = 1; // Quantity for the level editor
this.secondsElapsed = 0; // Keeps track of the seconds elapsed on a level
this.lastScore = 0; // Variable that stores the last score for display
this.bestScore = 0; // Variable that stores / retrieves the best score from localStorage
this.banner = null; // Variable to store the flag's banner
};
Main.prototype = {
create: function() {
game.time.advancedTiming = false;
// Bounds of the world defined
game.world.setBounds(0, 0, 35000, 750);
// Start physics engine
game.physics.startSystem(Phaser.Physics.ARCADE);
// Add the player to the game, set its anchor
this.player = game.add.sprite(100, game.world.height - 150, 'ninja');
this.player.anchor.setTo(0.4);
// Enable physics to the player
game.physics.arcade.enable(this.player);
this.player.body.gravity.y = 4000;
this.player.body.collideWorldBounds = false;
this.player.body.velocity.x = this.X_GAMESPEED;
// INPUT
// Added mouse / touch functionality for jumping
var keyboard = game.input.keyboard;
this.input.onDown.add(this.onMouseOrTouch, this); // Click and touch handler
// Add scroll buttons for level editor
this.cursors = keyboard.createCursorKeys();
keyboard.addKey(Phaser.Keyboard.UP).onDown.add(this.jump, this);
keyboard.addKey(Phaser.Keyboard.TILDE).onDown.add(this.debugToggle, this);
// Add various functionality for debugging purposes
if (this.IS_DEBUG_MODE) {
this.player.body.velocity.x = 0;
keyboard.addKey(Phaser.Keyboard.A).onDown.add(this.brickToggle, this);
keyboard.addKey(Phaser.Keyboard.S).onDown.add(this.spikeToggle, this);
keyboard.addKey(Phaser.Keyboard.D).onDown.add(this.coinToggle, this);
keyboard.addKey(Phaser.Keyboard.F).onDown.add(this.floorToggle, this);
keyboard.addKey(Phaser.Keyboard.ONE).onDown.add(this.oneSwitch, this);
keyboard.addKey(Phaser.Keyboard.TWO).onDown.add(this.twoSwitch, this);
keyboard.addKey(Phaser.Keyboard.THREE).onDown.add(this.threeSwitch, this);
keyboard.addKey(Phaser.Keyboard.FOUR).onDown.add(this.fourSwitch, this);
keyboard.addKey(Phaser.Keyboard.FIVE).onDown.add(this.fiveSwitch, this);
keyboard.addKey(Phaser.Keyboard.SIX).onDown.add(this.sixSwitch, this);
keyboard.addKey(Phaser.Keyboard.SEVEN).onDown.add(this.sevenSwitch, this);
keyboard.addKey(Phaser.Keyboard.EIGHT).onDown.add(this.eightSwitch, this);
keyboard.addKey(Phaser.Keyboard.NINE).onDown.add(this.nineSwitch, this);
game.add.text(50, 130, 'DEBUG MODE').fixedToCamera = true;
this.xText = game.add.text(50, 160, 'X Secs: ', {
fontSize: '20px',
fill: '#000'
});
this.yText = game.add.text(50, 180, 'Y: 0' + this.yPos, {
fontSize: '20px',
fill: '#000'
});
this.qtyText = game.add.text(50, 200, 'Qty: 0' + this.quantity, {
fontSize: '20px',
fill: '#000'
});
this.blockText = game.add.text(50, 220, 'Type: ' + this.blockType, {
fontSize: '20px',
fill: '#000'
});
this.xText.fixedToCamera = true;
this.yText.fixedToCamera = true;
this.qtyText.fixedToCamera = true;
this.blockText.fixedToCamera = true;
} else {
// Initialize timer for scoring if not debug mode
this.timer = this.game.time.create();
this.timer.loop(1000, this.updateTimer, this);
this.timer.start();
}
// Initialize Physics for obstacles
this.platforms = this.add.physicsGroup();
this.spikes = this.add.physicsGroup();
this.flag = this.add.physicsGroup();
// Initialize floor
this.floor = game.add.group();
this.floor.enableBody = true;
this.coins = game.add.group();
this.coins.enableBody = true;
// Initialize Scoreboard and on screen text
this.scoreText = game.add.text(50, 16, 'Score: 0', {
font: 'Aldrich',
fontSize: '32px',
fill: '#000'
});
this.score = 0;
// Get local storage to set the best score
if (localStorage.getItem(globals.stage) !== null) {
this.bestScore = localStorage.getItem(globals.stage);
}
var bestScoreText = game.add.text(50, 60, 'Best: ' + this.bestScore, {
font: 'Aldrich',
fontSize: '20px',
fill: '#000'
});
var lastScoreText = game.add.text(50, 84, 'Last: ' + this.lastScore, {
font: 'Aldrich',
fontSize: '20px',
fill: '#000'
});
this.scoreText.fixedToCamera = true;
bestScoreText.fixedToCamera = true;
lastScoreText.fixedToCamera = true;
// Display the back button
this.backButton = this.game.add.button(1265, 50, 'xButton', this.backToMenu, this);
this.backButton.anchor.set(0.5);
this.backButton.scale.set(0.9, 0.9);
this.backButton.fixedToCamera = true;
// Iniitialize audio
this.coinSound = game.add.audio('coin');
this.coinSound.volume = 0.2;
this.deathSound = game.add.audio('death');
this.jumpSound = game.add.audio('jump');
this.jumpSound.volume = 0.5;
if (!this.music && !this.IS_DEBUG_MODE) {
this.music = game.add.audio('music');
this.music.loopFull();
}
this.isEndlessMode = false; // Reset endless mode flag
// Create the world objects depending on stage selected
switch (globals.stage) {
case 0:
this.isEndlessMode = true;
EndlessLevel.start.call(this);
break;
case 1:
Level1.start.call(this); // bind the 'this' argument to Main
break;
case 2:
Level2.start.call(this);
break;
default:
this.backToMenu();
break;
}
this.isVictory = false;
this.platforms.setAll('body.allowGravity', false);
this.platforms.setAll('body.immovable', true);
this.platforms.setAll('body.friction.x', 0);
this.floor.setAll('body.allowGravity', false);
this.floor.setAll('body.immovable', true);
this.floor.setAll('body.friction.x', 0);
this.flag.setAll('body.immovable', true);
this.flag.setAll('body.gravity.y', 4000);
this.flag.setAll('body.moves', false);
if (this.isEndlessMode) {
this.spikes.setAll('body.allowGravity', true);
this.spikes.setAll('body.immovable', false);
this.spikes.setAll('body.gravity.y', 4000);
}
// Set acceleration for player if endless mode
if (this.isEndlessMode && !this.IS_DEBUG_MODE) {
this.player.body.acceleration.x = 10;
}
},
update: function() {
var arcade = game.physics.arcade;
var player = this.player;
var playerBody = player.body;
var coins = this.coins;
var spikes = this.spikes;
var floor = this.floor;
var platforms = this.platforms;
// In debug, display X, Y, Qty, Type for level editing
if (this.IS_DEBUG_MODE) {
this.xText.text = 'X Secs : ' + (game.camera.x + game.input.x) / 500;
this.yText.text = 'Y : ' + game.input.y;
this.qtyText.text = 'Qty : ' + this.quantity;
this.blockText.text = 'Type: ' + this.blockType;
// Scrolling keybinds for level editor
if (this.cursors.left.isDown) {
game.camera.x -= 50;
} else if (this.cursors.right.isDown) {
game.camera.x += 50;
}
} else { // Camera to follow player with offset
game.camera.focusOnXY(player.x + 400, player.y);
// If player gets stopped by brick
if (playerBody.velocity.x < 500 && !this.isVictory) {
if (this.isEndlessMode) { // Endless mode, uses current x position to recalculate approx speed
playerBody.velocity.x = this.X_GAMESPEED + (playerBody.x * this.ACCELERATION_FACTOR);
} else { // Normal mode sets player speed back to default.
playerBody.velocity.x = this.X_GAMESPEED;
}
} else if (player.y > game.world.height + 500) {
this.die();
}
}
// Collide player with world objects
arcade.collide(player, floor, this.resetJump, null, this);
arcade.collide(player, platforms, this.resetJump, null, this);
arcade.collide(player, this.flag, this.victory, null, this);
arcade.collide(player, spikes, this.die, null, this);
arcade.overlap(player, coins, this.collectCoin, null, this);
// In endess mode, make additional physics collisions
if (this.isEndlessMode) {
arcade.collide(spikes, floor);
arcade.collide(spikes, platforms);
// Collision for coins
// arcade.collide(coins, platforms);
// arcade.collide(coins, floor);
}
},
// Function called when player clicks screen or presses UP key
onMouseOrTouch: function() {
// In debug mode, it will place an object. In regular, it will jump the player
if (this.IS_DEBUG_MODE) {
var itemX = (game.camera.x + game.input.x) / 500;
var itemY = game.input.y;
switch (this.blockType) {
case 1:
console.log('this.createBrick(' + itemX + ', ' + itemY + ', ' + this.quantity + ');');
this.createBrick(itemX, itemY, this.quantity);
break;
case 2:
console.log('this.createSpike(' + itemX + ', ' + itemY + ', ' + this.quantity + ');');
this.createSpike(itemX, itemY, this.quantity);
break;
case 3:
console.log('this.createCoin(' + itemX + ', ' + itemY + ', ' + this.quantity + ');');
this.createCoin(itemX, itemY, this.quantity);
break;
case 4:
console.log('this.createFloor(' + itemX + ', ' + itemY + ', ' + this.quantity + ');');
this.createFloor(itemX, this.quantity);
break;
}
} else {
this.jump();
}
},
// Function that handles the jump logic
jump: function() {
switch (this.jumpCount) {
case 0:
this.jumpSound.play();
this.player.body.velocity.y = -1000;
this.jumpCount++;
break;
case 1:
this.player.body.velocity.y = -1000;
this.jumpCount++;
game.add.tween(this.player).to({
angle: 360
}, 400, Phaser.Easing.Linear.None, true);
break;
default:
break;
}
},
// Function called when player touches floor, resetting jump counter to 0
resetJump: function() {
if (this.player.body.touching.down) {
this.jumpCount = 0;
}
},
// Death function upon player impact with spike or through floor
die: function() {
console.log('Player has died');
this.lastScore = this.score;
if (this.lastScore > this.bestScore) {
localStorage.setItem(globals.stage, this.lastScore);
}
this.deathSound.play();
this.resetGame();
},
// Function to reset the game. Usually called upon death.
resetGame: function() {
this.score = 0;
this.secondsElapsed = 0;
game.state.start('Main');
},
collectCoin: function(player, coin) {
coin.kill();
this.coinSound.play();
// Add and update the score
this.score += 10;
this.scoreText.text = 'Score: ' + this.score;
},
createBrick: function(seconds, y, length, height) {
var x = seconds * this.X_GAMESPEED;
if (length === undefined) {
length = 1;
}
if (height === undefined) {
height = 1;
}
this.platforms.create(x, y, 'brick').scale.setTo(length, height);
},
createFloor: function(seconds, length) {
var x = seconds * this.X_GAMESPEED;
if (length === undefined) {
length = 1;
}
for (var i = 0; i < length; i++) {
this.floor.create(x, game.world.height - 112, 'grass');
x += 500; // each floor tile is 500px wide
}
},
createSpike: function(seconds, y, num) {
var x = seconds * this.X_GAMESPEED;
if (num === undefined) {
num = 1;
}
for (var i = 0; i < num; i++) {
this.spikes.create(x, y, 'spike');
x += 32;
}
},
createCoin: function(seconds, y, num) {
var x = seconds * this.X_GAMESPEED;
if (num === undefined) {
num = 1;
}
for (var i = 0; i < num; i++) {
this.coins.create(x, y, 'coin');
x += 50;
}
},
placeFlag: function(seconds) {
var x = seconds * this.X_GAMESPEED;
this.flag.create(x, 256, 'flagpole');
this.banner = game.add.sprite(x + 3, 270, 'banner');
this.banner.anchor.setTo(1, 0);
},
// Function called when player touches flag at end of level
victory: function() {
var playerWalkTween = function() {
var walkTo = this.player.body.x + 200;
game.add.tween(this.player)
.to({x : walkTo}, 1000, Phaser.Easing.Linear.In)
.start()
.onComplete.add(victoryJump, this);
};
var victoryJump = function() {
var victoryText;
var nextLevelButton;
var cX = game.width/2;
var cY = this.game.world.centerY;
// Congratulatory text
victoryText = game.add.text(cX, cY - 100, 'Level Complete!', {
font: 'Aldrich',
fontSize: '64px',
fill: '#333'
});
victoryText.fixedToCamera = true;
victoryText.anchor.setTo(0.5);
// Sprite for the next level button
globals.stage++;
nextLevelButton = this.game.add.button(cX, cY + 150, 'playButton', game.state.states.Main.resetGame , this);
nextLevelButton.fixedToCamera = true;
nextLevelButton.anchor.set(0.5);
nextLevelButton.scale.set(0.7);
this.jump();
game.time.events.add(Phaser.Timer.SECOND, this.jump, this);
};
this.isVictory = true;
this.player.body.velocity.x = 0;
this.timer.stop();
// Tween that handles the flag dropping down
var bannerTween = this.game.add.tween(this.banner)
.to({y: 575}, 1500, Phaser.Easing.Linear.In).start();
bannerTween.onComplete.add(playerWalkTween, this);
},
// Function that is called every second to increase the score and total seconds elapsed
updateTimer: function() {
this.score += 100;
this.secondsElapsed++;
this.scoreText.text = 'Score: ' + this.score;
},
// Function to go back to Title state
backToMenu: function() {
this.game.state.start('Title');
this.music.stop();
this.music = null;
},
// Function that toggles the debug mode. Executed with ~ keypress
debugToggle: function() {
if (this.IS_DEBUG_MODE) {
console.log('Debug Mode : OFF');
this.IS_DEBUG_MODE = false;
} else {
console.log('Debug Mode : ON');
this.IS_DEBUG_MODE = true;
}
this.game.state.start('Main');
},
// DEBUG FUNCTIONS - Helper functions for level design
brickToggle: function() {
this.blockType = 1;
},
spikeToggle: function() {
this.blockType = 2;
},
coinToggle: function() {
this.blockType = 3;
},
floorToggle: function() {
this.blockType = 4;
},
oneSwitch: function() {
this.quantity = 1;
},
twoSwitch: function() {
this.quantity = 2;
},
threeSwitch: function() {
this.quantity = 3;
},
fourSwitch: function() {
this.quantity = 4;
},
fiveSwitch: function() {
this.quantity = 5;
},
sixSwitch: function() {
this.quantity = 6;
},
sevenSwitch: function() {
this.quantity = 7;
},
eightSwitch: function() {
this.quantity = 8;
},
nineSwitch: function() {
this.quantity = 9;
}
};