Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
FogNetwork
GitHub Repository: FogNetwork/Tsunami
Path: blob/main/public/games/files/gopher-kart/js/stateMain.js
1036 views
1
var StateMain = {
2
3
preload: function () {
4
//Music
5
game.load.audio("title", "assets/music/racingMain-compressed.m4a");
6
//Sound FX
7
game.load.audio("coinBeep", "assets/music/sfx/coin.wav");
8
game.load.audio("npc_explosion", "assets/music/sfx/npc_explosion.wav");
9
game.load.audio("drive", "assets/music/sfx/drive.wav");
10
game.load.audio("accelerate", "assets/music/sfx/accelerate.wav");
11
game.load.audio("countdownBeep", "assets/music/sfx/countdown.wav");
12
13
game.stage.backgroundColor = 0xe9fffe;
14
15
//Gopher sprites
16
game.load.spritesheet("blue", "assets/gopher-blue-updated.png", 64, 60, 15);
17
game.load.spritesheet("pink", "assets/gopher-pink.png", 64, 60, 15);
18
game.load.spritesheet("purple", "assets/gopher-purple.png", 64, 60, 15);
19
//Road
20
game.load.image("road", "assets/road-tile.png");
21
//Top rail
22
game.load.image("topRail", "assets/top-rail-long.png");
23
//Scene Extras
24
game.load.image("extras", "assets/signs.png");
25
//Light Posts
26
game.load.image("posts", "assets/light-posts.png");
27
//Truck
28
game.load.image("truck", "assets/truck.png");
29
//Bottom rail
30
game.load.image("bottomRail", "assets/bottom-rail-long.png");
31
//Add background
32
game.load.image("sky", "assets/clouds-re-colored.png");
33
game.load.image("city", "assets/city-re-colored.png");
34
game.load.image("mtn", "assets/mountains-recolored.png");
35
//Add coins
36
game.load.spritesheet("coin", "assets/coin-shadow.png", 16, 19, 6);
37
//NPCs
38
game.load.spritesheet("npc", "assets/other-gophers.png", 64, 60, 20);
39
//Explosion
40
game.load.spritesheet("explosion", "assets/explosion.png", 64, 60, 4);
41
//Add hearts
42
game.load.spritesheet("heart", "assets/heart-17x16.png", 17, 16, 6);
43
//Countdown Sprites
44
game.load.image("countDown3", "assets/three.png");
45
game.load.image("countDown2", "assets/two.png");
46
game.load.image("countDown1", "assets/one.png");
47
game.load.image("countDownGo", "assets/go.png");
48
game.load.image("background", "assets/bg-color.png");
49
50
//Font
51
game.load.bitmapFont('pixelFont', 'assets/fonts/bitmapFonts/pixelFont.png', 'assets/fonts/bitmapFonts/pixelFont.xml');
52
var timeText;
53
},
54
55
create: function () {
56
57
game.world.setBounds(0, 0, 35000, this.game.height);
58
59
//MUSIC
60
this.titleSong = game.add.audio("title");
61
this.titleSong.volume = 0.4;
62
this.titleSong.play('', 0, 1, true);
63
64
//SFX
65
this.drivingSound = game.add.audio("drive");
66
this.accelerateSound = game.add.audio("accelerate");
67
this.countdownSound = game.add.audio("countdownBeep");
68
this.countdownSound.play('', 0, 1, false);
69
this.drivingSound.loopFull(1.3);
70
71
var background = game.add.tileSprite(0, 0, this.world.width, 432, "background");
72
73
//Prevents pausing of game when use clicks out of the game
74
game.stage.disableVisibilityChange = true;
75
76
//Start Physics Engine
77
game.physics.startSystem(Phaser.Physics.ARCADE);
78
79
//TIMER
80
setInterval(function(){
81
count += 1;
82
count.toString();
83
if(count < 10){
84
timeElapsed = "0" + count;
85
} else {
86
timeElapsed = count;
87
}
88
}, 1000);
89
90
//Increase score every second
91
setInterval(function(){
92
score += 3;
93
},1000);
94
95
96
this.timer = this.game.time.create(this.game);
97
this.timer.add(this.delay, this.readyForAction, this);
98
this.timer.start();
99
100
//Set top and bottom boundaries for Gopher
101
//To prevent going past rails
102
this.top = game.height - 200;
103
this.bottom = game.height - 80;
104
105
106
//Random lane logic for NPC spawn
107
this.lane = function () {
108
return availLanes[Math.floor(Math.random()*availLanes.length)];
109
};
110
//Random NPC logic
111
this.pickNPC = function(){
112
return availNpcGophers[Math.floor(Math.random()*availNpcGophers.length)];
113
};
114
// this.npc = game.add.sprite(game.width, this.lane, this.pickNPC);
115
// console.log("NEW NPC ADDED: " + this.lane() + " , " + this.pickNPC());
116
117
// BACKGROUND IMAGE TILES
118
sky = game.add.tileSprite(0, 6, this.world.width, 78, "sky");
119
mtn = game.add.tileSprite(0, 62, this.world.width, 133, "mtn");
120
city = game.add.tileSprite(0, 107, this.world.width, 90, "city");
121
truck = game.add.tileSprite(0, 84, this.world.width, 142, "truck");
122
road = game.add.tileSprite(0, 226, this.world.width, 159, "road");
123
posts = game.add.tileSprite(0, 15, this.world.width, 182, "posts");
124
extras = game.add.tileSprite(0, 120, this.world.width, 84, "extras");
125
topRail = game.add.tileSprite(0, 197, this.world.width, 29, "topRail");
126
bottomRail = game.add.tileSprite(0, 385, this.world.width, 47, "bottomRail");
127
128
sky.fixedToCamera = true;
129
mtn.fixedToCamera = true;
130
city.fixedToCamera = true;
131
truck.fixedToCamera = true;
132
road.fixedToCamera = true;
133
posts.fixedToCamera = true;
134
extras.fixedToCamera = true;
135
topRail.fixedToCamera = true;
136
bottomRail.fixedToCamera = true;
137
138
139
//EMPTY LIVES
140
this.emptyHeart1 = game.add.sprite(10, game.world.centerY-205, "heart");
141
this.emptyHeart1.fixedToCamera = true;
142
this.emptyHeart1.frame = 5;
143
this.emptyHeart2 = game.add.sprite(30, game.world.centerY-205, "heart");
144
this.emptyHeart2.fixedToCamera = true;
145
this.emptyHeart2.frame = 5;
146
this.emptyHeart3 = game.add.sprite(50, game.world.centerY-205, "heart");
147
this.emptyHeart3.fixedToCamera = true;
148
this.emptyHeart3.frame = 5;
149
150
151
//LIVES
152
this.heart1 = game.add.sprite(10, game.world.centerY-205, "heart");
153
this.heart1.fixedToCamera = true;
154
this.heart2 = game.add.sprite(30, game.world.centerY-205, "heart");
155
this.heart2.fixedToCamera = true;
156
this.heart3 = game.add.sprite(50, game.world.centerY-205, "heart");
157
this.heart3.fixedToCamera = true;
158
159
this.heartGroup = game.add.group();
160
this.heartGroup.add(this.heart1);
161
this.heartGroup.add(this.heart2);
162
this.heartGroup.add(this.heart3);
163
164
//COINS
165
this.coins=game.add.group();
166
this.coins.createMultiple(40, 'coin');
167
this.coins.setAll('checkWorldBounds', true);
168
this.coins.setAll('outOfBoundsKill', true);
169
170
// OTHER RACERS
171
this.npcRacers = game.add.group();
172
this.npcRacers.createMultiple(40, 'npc');
173
this.npcRacers.setAll('checkWorldBounds', true);
174
this.npcRacers.setAll('outOfBoundsKill', true);
175
176
//Main racer
177
this.sprite = game.add.sprite(50, 289, character);
178
this.sprite.anchor.set(0.5, 0.5);
179
this.sprite.animations.add("crash", [2,3,4,5,6], 9, false);
180
this.sprite.animations.add("idle", [0, 1], 9, true);
181
this.sprite.animations.play("idle");
182
game.physics.arcade.enable([this.sprite, this.coins, this.npcRacers]);
183
game.camera.follow(this.sprite);
184
this.sprite.body.collideWorldBounds = true;
185
this.sprite.body.immovable = true;
186
this.sprite.body.width = 60;
187
this.sprite.body.height = 30;
188
this.sprite.body.offset.setTo(3, 30);
189
190
//Add chosen racer to npcRacer group
191
//to allow for sorting and proper "z-index" effect
192
this.npcRacers.add(this.sprite);
193
194
// console.log("You chose the " + character + " racer!");
195
196
//Background image scroll speed
197
road.autoScroll(-390, 0);
198
topRail.autoScroll(-370, 0);
199
bottomRail.autoScroll(-380, 0);
200
sky.autoScroll(-5,0);
201
city.autoScroll(-30,0);
202
mtn.autoScroll(-15,0);
203
truck.autoScroll(-550, 0);
204
posts.autoScroll(-330, 0);
205
extras.autoScroll(-330, 0);
206
207
//COUNTDOWN SPRITES
208
this.countDown1 = game.add.sprite(game.world.bounds.height - 135, game.world.centerY, 'countDown1');
209
this.countDown1.anchor.setTo(0.5, 0.5);
210
this.countDown1.alpha = 0;
211
212
this.countDown2 = game.add.sprite(game.world.bounds.height - 135, game.world.centerY, 'countDown2');
213
this.countDown2.anchor.setTo(0.5, 0.5);
214
this.countDown2.alpha = 0;
215
216
217
this.countDown3 = game.add.sprite(game.world.bounds.height - 135, game.world.centerY, 'countDown3');
218
this.countDown3.anchor.setTo(0.5, 0.5);
219
this.countDown3.alpha = 0;
220
221
222
this.countDownGo = game.add.sprite(game.world.bounds.height - 80, game.world.centerY, 'countDownGo');
223
this.countDownGo.anchor.setTo(0.5, 0.5);
224
this.countDownGo.alpha = 0;
225
this.countDownGo.fixedToCamera = true;
226
227
//COUNTDOWN GROUP
228
this.countGroup = game.add.group();
229
this.countGroup.add(this.countDown1);
230
this.countGroup.add(this.countDown2);
231
this.countGroup.add(this.countDown3);
232
this.countGroup.add(this.countDownGo);
233
234
//TWEENS for 3..2..1..GO!
235
var tween1 = game.add.tween(this.countDown1).to({alpha: 1}, 500, Phaser.Easing.Linear.None, false,
236
0).to({alpha: 0}, 500, Phaser.Easing.Linear.None, false, 0);
237
var tween2 = game.add.tween(this.countDown2).to({alpha: 1}, 500, Phaser.Easing.Linear.None, false,
238
0).to({alpha: 0}, 500, Phaser.Easing.Linear.None, false, 0);
239
var tween3 = game.add.tween(this.countDown3).to({alpha: 1}, 500, Phaser.Easing.Linear.None, false,
240
0).to({alpha: 0}, 500, Phaser.Easing.Linear.None, false, 0);
241
var tweenGo = game.add.tween(this.countDownGo).to({alpha: 1}, 500, Phaser.Easing.Linear.None, false,
242
0).to({alpha: 0}, 500, Phaser.Easing.Linear.None, false, 0);
243
tween3.chain(tween2);
244
tween2.chain(tween1);
245
tween1.chain(tweenGo);
246
tween3.start();
247
248
//Set cursors to accept input from the keyboard
249
cursors = game.input.keyboard.createCursorKeys();
250
251
//Prevent user from moving until after "GO!"
252
game.input.enabled = false;
253
254
setTimeout(function(){
255
//Turn input back on
256
game.input.enabled = true;
257
},3000);
258
259
//TEXT
260
var scoreLabel = game.add.bitmapText(585, 7, 'pixelFont', 'SCORE', 21);
261
scoreLabel.anchor.set(1.0 , 0);
262
scoreText = game.add.bitmapText(570, 27, 'pixelFont', '0', 21);
263
scoreText.anchor.set(1.0 , 0);
264
var timeLabel;
265
timeLabel = game.add.bitmapText(275, 7, 'pixelFont', 'TIME', 21);
266
timeText = game.add.bitmapText(290, 27, 'pixelFont', timeElapsed, 21);
267
268
//Fix game status elements (w/e you want to call these :D) to the camera
269
this.timer.fixedToCamera = scoreText.fixedToCamera = timeLabel.fixedToCamera =
270
timeText.fixedToCamera = scoreLabel.fixedToCamera = true;
271
272
// game.debug.bodyInfo(this.npcRacers);
273
274
this.setListeners();
275
},
276
277
setListeners: function(){
278
//Spawn coins and NPCs
279
game.time.events.loop(Phaser.Timer.SECOND * coinSpawnRate, this.loadCoin, this);
280
game.time.events.loop(Phaser.Timer.SECOND * npcSpawnRate, this.loadNPC, this);
281
282
},
283
284
285
//NPC SPAWN
286
loadNPC: function (){
287
var newNpc = this.npcRacers.getFirstDead();
288
//maintains that npc spawns just to right of screen
289
var xx = this.camera.view.right + 20;
290
var yy = this.lane();
291
// newNpc.key = this.pickNPC();
292
newNpc.anchor.set(0.5 , 0.5);
293
newNpc.reset(xx, yy);
294
newNpc.enabled = true;
295
newNpc.body.velocity.x = -200;
296
newNpc.animations.add("idle", this.pickNPC(), 12, true);
297
newNpc.animations.play("idle");
298
newNpc.body.immovable = false;
299
newNpc.body.checkCollision.up = false;
300
newNpc.body.checkCollision.down = false;
301
newNpc.body.width = 60;
302
newNpc.body.height = 30;
303
newNpc.body.offset.setTo(3, 30);
304
},
305
306
//COIN SPAWN
307
loadCoin: function (){
308
var coin = this.coins.getFirstDead();
309
//y position
310
var yy = game.rnd.integerInRange(240, game.height-70);
311
//x position - maintains that coin spawns just to right of screen
312
var xx = this.camera.view.right + 20;
313
314
315
coin.reset(xx, yy);
316
coin.enabled = true;
317
coin.body.velocity.x = -200;
318
coin.animations.add("spin", [0, 1, 2, 3, 4, 5], 12, true);
319
coin.animations.play("spin");
320
},
321
322
//COIN PICK UP
323
onPickUp: function (sprite, coin){
324
coin.kill();
325
score += 10;
326
// console.log("Your score is --> " + score);
327
this.coinBeep = game.add.audio("coinBeep");
328
this.coinBeep.play('', 0, 1, false);
329
this.coinBeep.volume = 0.3;
330
},
331
332
333
//COLLISION HANDLER
334
onCrash: function (sprite, npc){
335
sprite.animations.play("crash");
336
this.npc_explosion = game.add.audio("npc_explosion");
337
this.npc_explosion.play('', 0, 1, false);
338
this.npc_explosion.volume = 0.3;
339
sprite.events.onAnimationComplete.add(function(){
340
// console.log("Crash animation complete");
341
sprite.animations.play("idle");
342
}, this);
343
lives -= 1;
344
var heart = this.heartGroup.getFirstAlive();
345
346
heart.animations.add("drain", [0,1,2,3,4,5],12, false);
347
heart.animations.play("drain");
348
349
350
//DECREASE SPEED OF GOPHER UPON COLLISON ... not working ??
351
sprite.body.velocity.x = -500;
352
setTimeout(function(){
353
heart.kill();
354
}, 1000);
355
356
//EXPLOSION
357
explosion = this.game.add.sprite(npc.body.x,npc.body.y,"explosion");
358
explosion.anchor.setTo(0.1,0.5);
359
explosion.animations.add("explosion", [0, 1, 2, 3], 12, false);
360
explosion.animations.play("explosion", 12, false, true);
361
npc.kill();
362
console.log("You have " + lives + "lives left!");
363
364
if(lives === 0){
365
this.titleSong.stop();
366
this.drivingSound.stop();
367
game.state.start("StateOver");
368
}
369
},
370
371
update: function (){
372
this.sprite.body.maxVelocity.x= 500;
373
this.sprite.body.maxVelocity.y= 500;
374
//Allows for correct "z-index" of gopher
375
this.npcRacers.sort('y', Phaser.Group.SORT_ASCENDING);
376
377
378
//Collision checks
379
game.physics.arcade.collide(this.sprite, this.coins, null, this.onPickUp, this);
380
game.physics.arcade.collide(this.npcRacers, this.coins, null, this.NpcPickUp, this);
381
game.physics.arcade.collide(this.sprite, this.npcRacers, null, this.onCrash, this);
382
// timeText.text = '' + Math.round(game.time.now);
383
timeText.text = timeElapsed;
384
scoreText.text = score;
385
386
//Cursors - Keyboard key check ⌨️
387
if (game.input.keyboard.downDuration(Phaser.Keyboard.RIGHT)) {
388
this.accelerateSound.play('', 0, 1.4, false, true);
389
this.drivingSound.volume = 1.6;
390
391
if(this.drivingSound._sound) {
392
this.drivingSound._sound.playbackRate.value = 1.4;
393
}
394
}
395
396
if(cursors.right.isDown) {
397
this.sprite.body.velocity.x = 80;
398
399
sky.tilePosition.x -=0.5;
400
mtn.tilePosition.x -=0.7;
401
city.tilePosition.x -=0.9;
402
truck.tilePosition.x -=2;
403
road.tilePosition.x -=2;
404
bottomRail.tilePosition.x -= 2;
405
posts.tilePosition.x -=5;
406
extras.tilePosition.x -=5;
407
topRail.tilePosition.x -=2;
408
409
}
410
411
if(cursors.right.isUp) {
412
this.sprite.body.velocity.x = -150;
413
this.drivingSound.volume = 1.3;
414
415
if(this.drivingSound._sound) {
416
this.drivingSound._sound.playbackRate.value = 1.0;
417
}
418
}
419
420
if(cursors.up.isDown) {
421
this.sprite.body.velocity.y = -90;
422
}
423
424
if(cursors.up.isUp) {
425
this.sprite.body.velocity.y = 0;
426
}
427
428
if(cursors.down.isDown) {
429
this.sprite.body.velocity.y = 90;
430
}
431
432
if(cursors.left.isDown) {
433
this.sprite.body.velocity.x = -250;
434
}
435
436
if(this.sprite.y<this.top) {
437
this.sprite.y=this.top;
438
}
439
440
if(this.sprite.y>this.bottom) {
441
this.sprite.y = this.bottom;
442
}
443
444
},
445
446
render: function () {
447
// game.debug.body(this.sprite);
448
// game.debug.text('Sprite z-depth: ' + this.sprite.z, 10, 20);
449
// console.log('NPCs z-depth: ' + this.npcRacers.z);
450
},
451
452
};//END StateMain
453
454