-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathGame.js
1262 lines (1042 loc) · 52.1 KB
/
Game.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
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
BasicGame.Game = function (game) {
// When a State is added to Phaser it automatically has the following properties set on it, even if they already exist:
this.game; // a reference to the currently runningthis.game
this.add; // used to add sprites, text, groups, etc
this.camera; // a reference to thethis.game camera
this.cache; // thethis.game cache
this.input; // the global input manager (you can access this.input.keyboard, this.input.mouse, as well from it)
this.load; // for preloading assets
this.math; // lots of useful common math operations
this.sound; // the sound manager - add a sound, play one, set-up markers, etc
this.stage; // thethis.game stage
this.time; // the clock
this.tweens; // the tween manager
this.state; // the state manager
this.world; // thethis.game world
this.particles; // the particle manager
this.physics; // the physics manager
this.rnd; // the repeatable random number generator
// statuses
this.isRunning;
this.isDead;
this.loseTimeout;
this.winTimeout;
// world settings
this.endingEnvironment;
this.levelLength;
this.tubeHeight = 200;
this.flatStartLength = 2500;
this.flatEndLength = 1000;
this.pixelStep = 300;
this.startPos;
this.distanceFromEnd;
this.bottomWall;
this.car_collisionGroup;
this.tube_collisionGroup;
this.pusher_collisionGroup;
this.groundMaterial;
this.playerMaterial;
this.wheelMaterial;
this.tunnelPhysicsData;
this.CG_world;
this.playedBefore;
this.showInstructions;
// environment
this.environment;
this.background;
this.midground;
this.foreground;
// global vars
this.menuButton;
this.muteButton;
this.cursors;
this.loseflag;
this.winflag;
this.pusherCounter;
// tunnel
this.tunnelGroup;
// pod settings
this.carGroup;
this.carBody;
this.wheel_front;
this.wheel_back;
this.wheelSpeed = 10;
this.totalPower = 68;
// pusher settings
this.pusherBody;
this.pusher_wheel_front;
this.pusher_wheel_back;
// GUI
this.livesRect;
this.winStage_graphic;
this.stranded_text;
this.Level_text;
this.Timer_text;
this.Speed_text;
this.Health_text;
this.slowDown_text;
this.stabilise_text;
this.speedUp_text;
this.pause_text;
//Audio
this.sound_click;
this.sound_music;
this.sound_explosion;
this.sound_hit;
this.music_volume;
this.sound_volume;
this.sound_muted;
// Snow
this.is_snowing = false;
this.snow_var = 0;
this.update_interval = 4 * 60;
this.max = 0;
this.front_emitter;
this.mid_emitter;
this.back_emitter;
};
BasicGame.Game.prototype = {
init: function () {
console.log("init")
// use debug plugin
//this.add.plugin(Phaser.Plugin.Debug);
var envs = this.game['GameData'].environments,
totalEnvs = envs.length;
//var levelSelect = Math.floor(Math.random() * totalEnvs);
var levelSelect = 0;
if (this.game['GameData'].cLevel == 1) {
console.log("First level!")
levelSelect = this.game['GameData'].startingEnvironment;
} else {
levelSelect = Math.floor(Math.random() * totalEnvs);
}
//levelSelect = totalEnvs - 1
this.game['GameData'].endingEnvironment = levelSelect;
this.environment = envs[levelSelect];
console.log("Level: "+levelSelect)
this.is_snowing = this.environment.isSnowing || false; // set the snowing flag
this.levelLength = this.game['GameData'].baseLevelLength * (Math.random() + 1);
this.death_speed = this.game['GameData'].death_speed;
this.min_speed = this.game['GameData'].min_speed;
this.max_speed = this.game['GameData'].max_speed;
this.playedBefore = this.game['GameData'].playedBefore;
this.distanceFromEnd = this.game['GameData'].distanceFromEnd;
//control
this.cursors = this.input.keyboard.createCursorKeys();
//------------------------------------//
// Non real time controls
//------------------------------------//
// ESC pause game
var escapeKey = this.input.keyboard.addKey(Phaser.Keyboard.ESC);
escapeKey.onDown.add(this.togglePauseGame, this);
// ESC pause game
var spacekey = this.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR);
spacekey.onDown.add(this.restartGame , this);
// set world settings and player start position
this.startPos = { "x": 150, "y": (this.world.height / 2) + 47 };
this.stage.backgroundColor = "#0c9fc7";
this.world.setBounds(0, 0, this.levelLength + this.flatStartLength + this.flatEndLength, 600);
this.physics.startSystem(Phaser.Physics.P2JS);
this.physics.p2.gravity.y = 800;
this.physics.p2.restitution = 0.2;
// Variable setup
this.time.reset();
this.loseflag = false;
this.winflag = false;
this.isDead = false;
this.pusherCounter = 0;
//gametime
this.time.advancedTiming = true;
this.groundMaterial = this.physics.p2.createMaterial('ground');
this.playerMaterial = this.physics.p2.createMaterial('player');
this.wheelMaterial = this.physics.p2.createMaterial('wheel');
this.physics.p2.createContactMaterial(this.playerMaterial, this.groundMaterial, { friction: 0.1, restitution: 0 });
this.physics.p2.createContactMaterial(this.wheelMaterial, this.groundMaterial, { friction: 0.1, restitution: 0 });
// numberOfHills, start_y, hill_max_height, tube_length, tube_height, pixelStep
this.tunnelPhysicsData = this.generateTubePoints(15, (this.world.height / 2) + 100, 580, this.levelLength, this.tubeHeight, this.pixelStep);
},
create: function () {
console.log("create")
this.tube_collisionGroup = this.physics.p2.createCollisionGroup();
this.car_collisionGroup = this.physics.p2.createCollisionGroup();
// create normal group
this.tunnelGroup = this.add.group();
//Audio
//this.sound_music = this.add.sound('level1Music');
var trackIndex = Math.floor(10*Math.random() + 1); // select random index for track
if (!this.sound_music || !this.sound_music.isPlaying) {
this.sound_music = this.game.add.sound('track'+trackIndex, 1, true);
}
this.sound_explosion = this.add.sound('explosion');
this.sound_hit1 = this.add.sound('hit1');
this.sound_hit2 = this.add.sound('hit1');
this.sound_hit3 = this.add.sound('hit1');
this.sound_click = this.add.sound('click');
this.music_volume = 0.5;
this.sound_volume = 0.3;
this.sound_music.volume = this.music_volume;
this.sound_explosion.volume = this.sound_volume;
this.sound_hit1.volume = this.sound_volume;
this.sound_hit2.volume = this.sound_volume;
this.sound_hit3.volume = this.sound_volume;
this.sound_muted = false;
this.sound_music.loop = true;
this.sound_music.play();
// create background first so that it goes to the back most position
this.addBackground();
this.addMidground();
var graphics = this.add.graphics(0, 0); // create a graphics object and prepare generate tube and rest of environment
this.addTunnel(graphics);
//this.addPickups();
this.addCar();
//this.addPusher();
this.addPylons();
this.carBody.body.onBeginContact.add(this.podCollision, this);
this.addForeground();
// GUI - create this last so it overlays on top of everything else
this.lives = this.add.sprite(this.camera.x + this.camera.width - 120, 25, 'lives');
this.livesRect = new Phaser.Rectangle( this.lives.width - (this.game['GameData'].currentLives * this.lives.height), //x
0, //y
this.lives.width, //w
this.lives.height); //h
this.lives.crop(this.livesRect);
// this.lives.updateCrop();
this.topUI = this.add.sprite(0, 0, 'topUI');
this.trackProgressorBackground = this.add.sprite(0, this.camera.y + 517, 'progressorBackground');
this.trackProgressorBackground.anchor.setTo(0, 0);
this.trackProgressorMarker = this.add.sprite(120, this.trackProgressorBackground.y + 64, 'progressorMarker');
this.trackProgressorMarker.anchor.setTo(0.5, 0.5);
this.menuButton = this.add.button(this.camera.x + 2, this.camera.y + 5, 'menu_button', this.quitGame, this, 'over', 'out', 'down');
//this.menuButton.scale.set(1, 1);
this.muteButton = this.add.button(this.camera.x + 8, this.camera.y + 43, 'mute_button', this.toggleMuteAudio, this, 'over', 'out', 'down');
this.muteButton.scale.set(0.4, 0.4);
this.instructions = this.add.sprite(this.camera.x + this.camera.width/2,this.camera.y + 430, 'instructions');
this.instructions.anchor.set(0.5, 0.5);
this.Health_indicator = this.add.sprite(this.camera.x + this.camera.width - 8, this.camera.height - 45, 'health_indicator');
this.Health_indicator.tint = 0x970000;
this.Health_indicator.anchor.set(1, 0.5);
this.Health_indicator.scale.set(100,1)
this.power_indicator = this.add.sprite(this.camera.x + this.camera.width - 143, this.camera.height - 13, 'health_indicator');
this.power_indicator.tint = 0x008a00;
this.power_indicator.scale.set(this.totalPower,0.5)
this.power_indicator.anchor.set(0, 0.5);
// Displays
this.Level_text = this.add.bitmapText(this.camera.x + 85, this.camera.y + 9, 'basic_font_white', 'Level ' + this.game['GameData'].cLevel, 25);
this.Speed_text = this.add.bitmapText(this.camera.x + 10, this.camera.y + 558, 'basic_font_white', "0 m/s", 30);
this.Speed_text.anchor.set(0, 0.5);
this.Timer_text = this.add.bitmapText(this.camera.x + 10, this.camera.y + 565, 'basic_font_white', "00:00:00", 30);
this.slowDown_text = this.add.bitmapText(this.camera.x + this.camera.width/2, this.camera.y +this.camera.height/2, 'basic_font_white', "Slow down!", 30);
this.slowDown_text.anchor.set(0.5, 0.5);
this.slowDown_text.tint = 0xFF0000;
this.speedUp_text = this.add.bitmapText(this.camera.x + this.camera.width/2, this.camera.y +this.camera.height/2, 'basic_font_white', "Speed up!", 30);
this.speedUp_text.anchor.set(0.5, 0.5);
this.speedUp_text.tint = 0xFF0000;
this.stabilise_text = this.add.bitmapText(this.camera.x + this.camera.width/2, this.camera.y +this.camera.height/2 - 50, 'basic_font_white', "Stabilize the pod now!", 30);
this.stabilise_text.anchor.set(0.5, 0.5);
this.stabilise_text.tint = 0xFF0000;
this.pause_text = this.add.bitmapText(this.camera.x + this.camera.width/2, this.camera.y +this.camera.height/2, 'basic_font_white', "Game paused", 30);
this.pause_text.anchor.set(0.5, 0.5);
this.stranded_text = this.add.bitmapText(this.camera.x + this.camera.width/2, this.camera.y +this.camera.height/2,'basic_font_white', 'You are stranded!', 30);
this.stranded_text.anchor.set(0.5, 0.5);
this.power_text = this.add.bitmapText(this.camera.x + this.camera.width/2, this.camera.y +this.camera.height/2 + 50,'basic_font_white', 'Low on power!', 30);
this.power_text.anchor.set(0.5, 0.5);
this.power_text.tint = 0xFF0000;
this.winStage_text = this.add.bitmapText(this.camera.x + this.camera.width/2, this.camera.y +this.camera.height/2,'basic_font_white', 'Stage complete!', 50);
this.winStage_text.anchor.set(0.5, 0.5);
//fix elements to camera
this.lives.fixedToCamera = true;
this.topUI.fixedToCamera = true;
this.trackProgressorBackground.fixedToCamera = true; // Setting this to true made the indicator go backwards/slow when accelerating
this.trackProgressorMarker.fixedToCamera = true;
this.menuButton.fixedToCamera = true;
this.muteButton.fixedToCamera = true;
this.instructions.fixedToCamera = true;
this.Health_indicator.fixedToCamera = true;
this.power_indicator.fixedToCamera = true;
this.Level_text.fixedToCamera = true;
this.Timer_text.fixedToCamera = true;
this.Speed_text.fixedToCamera = true;
this.slowDown_text.fixedToCamera = true;
this.speedUp_text.fixedToCamera = true;
this.stabilise_text.fixedToCamera = true;
this.pause_text.fixedToCamera = true;
this.stranded_text.fixedToCamera = true;
this.power_text.fixedToCamera = true;
this.winStage_text.fixedToCamera = true;
// GUI Initial visibility
this.slowDown_text.visible = false;
this.speedUp_text.visible = false;
this.pause_text.visible = false;
this.stabilise_text.visible = false;
this.showInstructions = (this.playedBefore)?false:true;
this.stranded_text.visible = false;
this.power_text.visible = false;
this.winStage_text.visible = false;
// Snow
if (this.is_snowing) {
this.back_emitter = this.add.emitter(this.camera.width * 5, -32, 1000);
this.back_emitter.makeParticles('snowflakes', [0, 1, 2, 3, 4, 5]);
this.back_emitter.maxParticleScale = 0.6;
this.back_emitter.minParticleScale = 0.2;
this.back_emitter.setYSpeed(20, 100);
this.back_emitter.gravity = 0;
this.back_emitter.width = this.camera.width * 10;
this.back_emitter.minRotation = 0;
this.back_emitter.maxRotation = 40;
this.back_emitter.fixedToCamera = true;
this.mid_emitter = this.add.emitter(this.camera.width * 5, -32, 400);
this.mid_emitter.makeParticles('snowflakes', [0, 1, 2, 3, 4, 5]);
this.mid_emitter.maxParticleScale = 1.2;
this.mid_emitter.minParticleScale = 0.8;
this.mid_emitter.setYSpeed(50, 150);
this.mid_emitter.gravity = 0;
this.mid_emitter.width = this.camera.width * 10;
this.mid_emitter.minRotation = 0;
this.mid_emitter.maxRotation = 40;
this.mid_emitter.fixedToCamera = true;
this.front_emitter = this.add.emitter(this.camera.width * 5, -32, 100);
this.front_emitter.makeParticles('snowflakes_large', [0, 1, 2, 3, 4, 5]);
this.front_emitter.maxParticleScale = 1;
this.front_emitter.minParticleScale = 0.5;
this.front_emitter.setYSpeed(100, 200);
this.front_emitter.gravity = 0;
this.front_emitter.width = this.camera.width * 10;
this.front_emitter.minRotation = 0;
this.front_emitter.maxRotation = 40;
this.back_emitter.fixedToCamera = true;
this.changeWindDirection();
this.back_emitter.start(false, 14000, 20);
this.mid_emitter.start(false, 12000, 40);
this.front_emitter.start(false, 6000, 1000);
}
},
update: function () {
this.carBody.frame = 0;
//this.currentblock.text = this.getBlockIndex() + "/" + this.getBlocks();
this.updateTunnel();
// handle inputs
if (!this.loseflag ) { this.handleInput(); };
// camera follow pod
if (!this.loseflag ) { this.camera.x = this.carBody.body.x - 200; };
// update background
//this.background.x = this.camera.x;
// update midground
var camera = this.camera;
this.midground.forEach(function (item) {
if (item.type === 5) { // tileable sprite
//item.tilePosition += item.velocity;
if (item.velocity.x != 0 || item.velocity.y != 0) {
item.tilePosition.x += item.velocity.x;
item.tilePosition.y += item.velocity.y;
} else {
item.tilePosition.x = -(camera.x * item.parallax) + item.offset.x;
item.tilePosition.y = item.offset.y - item.velocity.y;
}
} else if (item.type === 0) {
console.log(camera.x * item.parallax + item.offset.x)
item.x = -(camera.x * item.parallax) + item.offset.x;
//item.y = item.offset.y - item.velocity.y;
}
})
//---------------------
// Pod status check
//---------------------
this.power_indicator.x = this.camera.x + this.camera.width - 100;
this.power_indicator.y = this.camera.height - 48;
// if below death speed, the player is stranded so go to lose state
if ( (this.carBody.body.velocity.x <= this.death_speed && this.power_indicator.width <= 10) && this.pusherCounter > 50 && !this.loseflag) {
console.log("You're stranded");
this.loseflag = true;
this.speedUp_text.visible = false;
this.slowDown_text.visible = false;
this.stabilise_text.visible = false;
this.stranded_text.visible = true;
this.loseStranded();
}
if (!this.isDead && !this.loseflag) {
// set visibility of slowdown/speedup text
this.slowDown_text.visible = (this.carBody.body.velocity.x >= this.max_speed);
this.speedUp_text.visible = (this.carBody.body.velocity.x <= this.min_speed && this.pusherCounter > 50);
// Check pod's angle, explode if out of bounds
if ( this.carBody.body.angle < 50 && this.carBody.body.angle > -50) {
this.stabilise_text.visible = false;
} else {
this.stabilise_text.visible = true;
}
if ( this.carBody.body.angle > 135 || this.carBody.body.angle < -135) {
console.log('You exploded!');
this.loseExplode();
}
}
// check if pod reached end
if (this.carBody.body.x >= (this.levelLength + this.flatStartLength + this.flatEndLength - this.distanceFromEnd) & !this.winflag & !this.isDead) {
this.winflag = true;
this.game['GameData'].currentStageScore = this.carBody.body.x; // set current stage score to current score
this.winStage();
}
//---------------------
// Marker progress
//---------------------
// update marker on track progressor
var ProgressMultiplier = this.carBody.x / (this.levelLength + this.flatStartLength + this.flatEndLength);
if (ProgressMultiplier > 1) { ProgressMultiplier = 1; }
this.trackProgressorMarker.cameraOffset.x = 120 + (ProgressMultiplier * 410);
if (ProgressMultiplier != 1 && !this.loseflag) {
// Timer
var minutes = Math.floor(this.game.time.totalElapsedSeconds() / 60);
var seconds = Math.floor(this.game.time.totalElapsedSeconds()) % 60;
var miliseconds = Math.floor((this.game.time.totalElapsedSeconds() - Math.floor(this.game.time.totalElapsedSeconds())) * 100);
if (seconds < 10) seconds = '0' + seconds;
if (minutes < 10) minutes = '0' + minutes;
if (miliseconds < 10) miliseconds = '0' + miliseconds;
this.Timer_text.setText(minutes + ':' + seconds + ':' + miliseconds);
// Speed
var pod_velcity = this.carBody.body.velocity.x; // in pixels per second
pod_velcity = pod_velcity / 6; // could set this as a constant somewhere...pixels/meter
pod_velcity = Math.floor(pod_velcity);
this.Speed_text.setText(pod_velcity + ' m/s');
} else if (this.loseflag) {
this.Speed_text.setText('Signal Lost!');
this.Speed_text.fontSize = 20;
}
// Snow
if (this.is_snowing) {
this.snow_var++;
if (this.snow_var === this.update_interval) {
this.changeWindDirection();
update_interval = Math.floor(Math.random() * 20) * 60; // 0 - 20sec @ 60fps
this.snow_var = 0;
}
}
if (this.pusherCounter++ <= 50) {
//this.pusherBody.body.force.x = 8000;
this.carGroup.setAll('body.force.x', 4000);
} else if (this.pusherCounter++ > 50 && this.pusherCounter <= 200) {
//this.pusherBody.body.force.x = -50000;
} else {
this.showInstructions = false;
}
if (this.showInstructions) {
this.instructions.visible = true;
} else {
this.instructions.visible = false;
}
},
addTunnel: function (graphics) {
var blockIndex = 0;
var points = this.tunnelPhysicsData;
var currentBlock = points['bottom'][blockIndex];
var currentBlockTop = points['top'][blockIndex];
//var polygonCollisionSprite = this.add.sprite(0, 0, 'wall');
var polygonCollisionSprite = this.tunnelGroup.create(0, 0, 'wall');
this.physics.p2.enable(polygonCollisionSprite);
polygonCollisionSprite.name = 'wall_bot';
polygonCollisionSprite.body.addPolygon({}, currentBlock.shape);
polygonCollisionSprite.body.static = true;
polygonCollisionSprite.body.debug = false;
polygonCollisionSprite.body.setMaterial(this.groundMaterial);
polygonCollisionSprite.body.setCollisionGroup(this.tube_collisionGroup);
polygonCollisionSprite.body.collides(this.car_collisionGroup);
var top_polygonCollisionSprite = this.tunnelGroup.create(0, 0, 'wall');
this.physics.p2.enable(top_polygonCollisionSprite);
top_polygonCollisionSprite.name = 'wall_top';
top_polygonCollisionSprite.body.addPolygon({}, currentBlockTop.shape);
top_polygonCollisionSprite.body.static = true;
top_polygonCollisionSprite.body.debug = false;
top_polygonCollisionSprite.body.setMaterial(this.groundMaterial);
top_polygonCollisionSprite.body.setCollisionGroup(this.tube_collisionGroup);
top_polygonCollisionSprite.body.collides(this.car_collisionGroup);
this.tunnelPhysicsData['bottom'][blockIndex]['drawn'] = true; // make sure it doesn't get redrawn later*/
//==================//
// draw tube
//==================//
graphics.beginFill(0xAAAAAA, 0.1);
var totalPoints = points['bottom'].length;
graphics.beginFill(0xAAAAAA, 0.1);
btm_prevx = points['bottom'][0]['shape'][4];
btm_prevy = points['bottom'][0]['shape'][5];
top_prevx = points['top'][0]['shape'][4];
top_prevy = points['top'][0]['shape'][5];
for (var i = 1; i < totalPoints; i++) {
var btm_x = points['bottom'][i]['shape'][4],
btm_y = points['bottom'][i]['shape'][5],
top_x = points['top'][i]['shape'][4],
top_y = points['top'][i]['shape'][5];
// Solid gray background
graphics.lineStyle(6, this.environment.tunnel_background_colour, 0);
graphics.drawPolygon([btm_prevx, btm_prevy, top_prevx, top_prevy, top_x, top_y, btm_x, btm_y]);
// Black outline
graphics.lineStyle(6, this.environment.tunnel_colour, 1);
graphics.moveTo(btm_prevx, btm_prevy);
graphics.lineTo(btm_x, btm_y);
graphics.moveTo(top_prevx, top_prevy);
graphics.lineTo(top_x, top_y);
btm_prevx = btm_x;
btm_prevy = btm_y;
top_prevx = top_x;
top_prevy = top_y;
}
},
addPickups: function (graphics) {
var blockIndex = 0;
var points = this.tunnelPhysicsData;
var totalPoints = points['bottom'].length;
btm_prevx = points['bottom'][0]['shape'][4];
btm_prevy = points['bottom'][0]['shape'][5];
for (var i = 1; i < totalPoints; i++) {
var btm_x = points['bottom'][i]['shape'][4],
btm_y = points['bottom'][i]['shape'][5];
var pickup = this.add.sprite(btm_x, btm_y - 70, 'power_pickup');
pickup.anchor.set(0.5, 0);
pickup.scale.set(0.7, 0.7)
btm_prevx = btm_x;
btm_prevy = btm_y;
}
},
addPylons: function () {
var points = this.tunnelPhysicsData;
var totalPylons = points['pylons'].length;
for (var i = 0; i < totalPylons; i++) {
var x = points['pylons'][i]['position'].x,
y = points['pylons'][i]['position'].y;
var pylon = this.add.sprite(x, y - this.tubeHeight - 6, 'pylon');
//pylon.anchor.setTo(0.5, 0.1);
}
},
updateTunnel: function () {
var blockIndex = this.getBlockIndex();
var advanceBlocks = 4;
for (var i = 0; i <= advanceBlocks; i++) {
blockIndex += i;
var currentBlock = this.tunnelPhysicsData['bottom'][blockIndex];
var currentBlockTop = this.tunnelPhysicsData['top'][blockIndex];
if (currentBlock && !currentBlock['drawn']) {
//var polygonCollisionSprite = this.add.sprite(0, 0, 'wall');
var polygonCollisionSprite = this.tunnelGroup.create(0, 0, 'wall');
this.physics.p2.enable(polygonCollisionSprite);
polygonCollisionSprite.name = 'wall_bot';
polygonCollisionSprite.body.addPolygon({}, currentBlock.shape);
polygonCollisionSprite.body.static = true;
polygonCollisionSprite.body.debug = false;
polygonCollisionSprite.body.setMaterial(this.groundMaterial);
polygonCollisionSprite.body.setCollisionGroup(this.tube_collisionGroup);
polygonCollisionSprite.body.collides(this.car_collisionGroup);
polygonCollisionSprite['blockIndex'] = blockIndex
var top_polygonCollisionSprite = this.tunnelGroup.create(0, 0, 'wall');
this.physics.p2.enable(top_polygonCollisionSprite);
top_polygonCollisionSprite.name = 'wall_top';
top_polygonCollisionSprite.body.addPolygon({}, currentBlockTop.shape);
top_polygonCollisionSprite.body.static = true;
top_polygonCollisionSprite.body.debug = false;
top_polygonCollisionSprite.body.setMaterial(this.groundMaterial);
top_polygonCollisionSprite.body.setCollisionGroup(this.tube_collisionGroup);
top_polygonCollisionSprite.body.collides(this.car_collisionGroup);
top_polygonCollisionSprite['blockIndex'] = blockIndex
//==================//
// add physics
//==================//s
this.carBody.body.collides(this.tube_collisionGroup);
this.wheel_front.body.collides(this.tube_collisionGroup);
this.wheel_back.body.collides(this.tube_collisionGroup);
currentBlock['drawn'] = true;
}
}
// clean up
this.tunnelGroup.forEach(function(child){
//console.log(child)
if (!isNaN(child.x)) {
//console.log("child:", child.x, ", camera:", this.camera.x)
if (child.x < (this.camera.x - 2800) && !child.inCamera) {
child.destroy();
}
} else {
//console.log(child.blockIndex)
}
}, this, false)
//console.log(this.tunnelGroup)
},
getBlockIndex: function () {
var blocks = this.tunnelPhysicsData['bottom'];
var position = this.carBody.x
var length = blocks.length;
var diffMidIndex;
function mid(minIndex, maxIndex) { // left/right array index
var diffIndex = maxIndex + minIndex
diffMidIndex = (diffIndex%2 == 0) ? (diffIndex / 2) : ( ((diffIndex - 1) / 2)); // handle odd/even array length
midPosWorld = blocks[diffMidIndex]['shape'][2];
if (diffIndex == 1 || diffIndex == 2 || position === midPosWorld || minIndex === diffMidIndex) {
return diffMidIndex;
} else if ( position > midPosWorld ) {
return mid( diffMidIndex, maxIndex);
} else if ( position < midPosWorld ){
return mid( minIndex, diffMidIndex);
}
}
return mid(0, length-1);
},
getBlocks: function () {
return Math.floor( (this.levelLength + this.flatStartLength + this.flatEndLength) / this.pixelStep );
},
generateBlock: function () {
},
changeWindDirection: function () {
var multi = Math.floor((this.max + 200) / 4),
frag = (Math.floor(Math.random() * 100) - multi);
this.max = this.max + frag;
if (this.max > 200) this.max = 150;
if (this.max < -200) this.max = -150;
this.setXSpeed(this.back_emitter, this.max);
this.setXSpeed(this.mid_emitter, this.max);
this.setXSpeed(this.front_emitter, this.max);
},
setXSpeed: function (emitter, max) {
emitter.setXSpeed(max - 20, max);
emitter.forEachAlive(this.setParticleXSpeed, this, max);
},
setParticleXSpeed: function (particle, max) {
particle.body.velocity.x = max - Math.floor(Math.random() * 30);
},
addBackground: function () {
var environmentBackground = this.environment['background'];
var backgroundGroup = this.background = this.add.group();
for (var key in environmentBackground) {
if (environmentBackground.hasOwnProperty(key)) {
if (environmentBackground[key].type === "unique") {
var unique = backgroundGroup.create(environmentBackground[key].position.x, environmentBackground[key].position.y, environmentBackground[key].texture);
unique.fixedToCamera = environmentBackground[key].fixedToCamera;
} else if (environmentBackground[key].type === "unique_randomized") {
var textures = environmentBackground[key].textures;
var texture_index = Math.floor(textures.length*Math.random());
var texture_name = textures[texture_index];
var unique = backgroundGroup.create(environmentBackground[key].position.x, environmentBackground[key].position.y, texture_name);
unique.fixedToCamera = environmentBackground[key].fixedToCamera;
}
}
}
},
addMidground: function () {
var environmentMidground = this.environment['midground'];
var midgroundGroup = this.midground = this.add.group();
for (var key in environmentMidground) {
if (environmentMidground.hasOwnProperty(key)) {
if (environmentMidground[key].type === "unique") {
midgroundGroup.create(environmentMidground[key].position.x, environmentMidground[key].position.y, environmentMidground[key].texture);
} else if (environmentMidground[key].type === "repeat") {
var tileable = this.add.tileSprite(environmentMidground[key].position.x, environmentMidground[key].position.y, this.camera.width, this.cache.getImage(environmentMidground[key].texture).height, environmentMidground[key].texture);
tileable.fixedToCamera = true;
tileable.tileScale = { "x": environmentMidground[key].tileScale.x, "y": environmentMidground[key].tileScale.y };
tileable.tilePosition = { "x": environmentMidground[key].tilePosition.x, "y": environmentMidground[key].tilePosition.y };
// need to pass some data to the update function so store it on the object
tileable['parallax'] = environmentMidground[key].parallax;
tileable['offset'] = environmentMidground[key].tilePosition;
tileable['velocity'] = environmentMidground[key].velocity;
midgroundGroup.add(tileable);
} else if (environmentMidground[key].type === "repeat_unique_randomized") {
var group = this.add.group();
var textures = environmentMidground[key].textures;
var anchors = environmentMidground[key].anchors;
var total_instances = (this.levelLength * (environmentMidground[key].parallax + 1))/environmentMidground[key].position_offset.x;
var instance_position = environmentMidground[key].position;
for (var i = 0; i < total_instances; i++) {
var offset = environmentMidground[key].position_offset.x * (environmentMidground[key].position_random_factor.x*Math.random());
instance_position.x += offset;
var texture_index = Math.floor(textures.length*Math.random());
var texture_name = textures[texture_index];
var sprite_instance = this.add.sprite( instance_position.x , environmentMidground[key].position.y, texture_name ); // choose random texture from "textures" array
sprite_instance.anchor.set(environmentMidground[key].anchors[texture_index].x, environmentMidground[key].anchors[texture_index].y)
group.add(sprite_instance);
}
group['parallax'] = environmentMidground[key].parallax;
group['offset'] = offset;
group['velocity'] = environmentMidground[key].velocity;
midgroundGroup.add(group);
} else if (environmentMidground[key].type === "unique_randomized") {
var textures = environmentMidground[key].textures;
var texture_index = Math.floor(textures.length*Math.random());
var texture_name = textures[texture_index];
midgroundGroup.create(environmentMidground[key].position.x, environmentMidground[key].position.y, texture_name);
}
}
}
},
addForeground: function () {
this.foreground = this.add.group();
},
generateTubePoints: function (numberOfHills, start_y, hill_max_height, tube_length, tube_height, pixelStep) {
var hillStartY = start_y,
hillWidth = tube_length / numberOfHills,
hillSlices = hillWidth / pixelStep,
tunnelPhysicsData = { "top": [], "bottom": [], "pylons": [] },
prevx,
prevy;
// Generate flat at beginning
var rect = {
"density": 2, "friction": 0, "bounce": 0,
"filter": { "categoryBits": 1, "maskBits": 65535 },
"shape": [this.flatStartLength, hillStartY + 100, 0, hillStartY + 100, 0, hillStartY, this.flatStartLength, hillStartY]
};
tunnelPhysicsData['bottom'].push(rect);
var topRect = {
"density": 2, "friction": 0, "bounce": 0,
"filter": { "categoryBits": 1, "maskBits": 65535 },
"shape": [this.flatStartLength, hillStartY - tube_height - 100, this.flatStartLength, hillStartY - tube_height, 0, hillStartY - tube_height, 0, hillStartY - tube_height - 100]
};
tunnelPhysicsData['top'].push(topRect);
prevx = this.flatStartLength;
prevy = start_y;
for (var i = 0; i < numberOfHills; i++) {
var randomHeight = Math.random() * 78.5;
// this is necessary to make all hills (execept the first one) begin where the previous hill ended
if (i == 0) {
hillStartY = prevy - randomHeight;
} else {
hillStartY -= randomHeight;
}
// looping through hill slices
for (var j = 0; j <= hillSlices; j++) {
var x = j * pixelStep + hillWidth * i + this.flatStartLength,
y = hillStartY + randomHeight * Math.cos(2 * Math.PI / hillSlices * j),
height = y - prevy,
length = Math.sqrt((pixelStep * pixelStep) + (height * height)),
hillPoint = { "x": x, "y": y },
angle = Math.atan2(y - prevy, x - prevx)
var rect = {
"density": 2, "friction": 0, "bounce": 0,
"filter": { "categoryBits": 1, "maskBits": 65535 },
"shape": [prevx, hillPoint.y + 100, prevx, prevy, hillPoint.x, hillPoint.y, hillPoint.x, hillPoint.y + 100]
};
tunnelPhysicsData['bottom'].push(rect);
var topRect = {
"density": 2, "friction": 0, "bounce": 0,
"filter": { "categoryBits": 1, "maskBits": 65535 },
"shape": [prevx, hillPoint.y - tube_height - 100, hillPoint.x, hillPoint.y - tube_height - 100, hillPoint.x, hillPoint.y - tube_height, prevx, prevy - tube_height]
//"shape": [prevx, hillPoint.y - tube_height, prevx, hillPoint.y - tube_height - 10, hillPoint.x, hillPoint.y - tube_height-10, hillPoint.x, hillPoint.y - tube_height]
};
tunnelPhysicsData['top'].push(topRect);
prevx = x;
prevy = y;
}
tunnelPhysicsData['pylons'].push({ position: { "x": prevx, "y": prevy } });
// this is also necessary to make all hills (execept the first one) begin where the previous hill ended
hillStartY = hillStartY + randomHeight;
}
//prevx += pixelStep;
// Generate flat at end
var rect = {
"density": 2, "friction": 0, "bounce": 0,
"filter": { "categoryBits": 1, "maskBits": 65535 },
"shape": [prevx, prevy + 100, prevx, prevy, prevx + this.flatEndLength, prevy, prevx + this.flatEndLength, prevy + 100]
};
tunnelPhysicsData['bottom'].push(rect);
var topRect = {
"density": 2, "friction": 0, "bounce": 0,
"filter": { "categoryBits": 1, "maskBits": 65535 },
"shape": [prevx, prevy - tube_height - 100, prevx + this.flatEndLength, prevy - tube_height - 100, prevx + this.flatEndLength, prevy - tube_height, prevx, prevy - tube_height]
};
tunnelPhysicsData['top'].push(topRect);
return tunnelPhysicsData;
},
render: function () {
this.game.debug.text(this.game.time.fps || '--', 2, 14, "#00ff00");
},
resize: function (width, height) {
//this.menuButton.x = 25;
//this.menuButton.y = 25;
},
handleInput: function () {
if (this.cursors.up.isDown) {
if (this.power_indicator.width > 10) {
this.power_indicator.width -= 1;
this.carGroup.setAll('body.force.x', 7000);
if (this.power_indicator.width > 80) {
this.power_indicator.tint = 0x008a00;
} else if (this.power_indicator.width > 50) {
this.power_indicator.tint = 0xFF9900;
} else if (this.power_indicator.width > 20) {
this.power_indicator.tint = 0xFF0000;
}
} else {
this.speedUp_text.visible = false;
this.slowDown_text.visible = false;
this.stabilise_text.visible = false;
this.stranded_text.visible = false;
this.power_text.visible = true;
this.power_indicator.width = 5;
this.power_indicator.tint = 0xFF0000;
}
}
if (this.cursors.down.isDown) {
this.carGroup.setAll('body.velocity.x', this.carBody.body.velocity.x*0.98);
}
if (this.carBody.body.velocity.x < 0) {
this.carBody.body.velocity.x = 0;
}
if (this.cursors.right.isDown) {
this.carBody.body.angularVelocity = 2;
}
if (this.cursors.left.isDown) {
this.carBody.body.angularVelocity = -2;
}
},
togglePauseGame: function (pointer) {
this.sound_click.play();
var res = true;
this.sound_music.pause();
this.instructions.visible = true;
if (this.game.paused) {
res = false;
this.instructions.visible = false;
this.sound_music.resume()
}
this.game.paused = res;
this.pause_text.visible = res;
},
toggleMuteAudio: function(pointer) {
if (this.sound_muted) {
this.sound_click.play();
this.sound_muted = false;
this.sound_music.volume = this.music_volume;
this.sound_explosion.volume = this.sound_volume;
this.sound_hit1.volume = this.sound_volume;
this.sound_hit2.volume = this.sound_volume;
this.sound_hit3.volume = this.sound_volume;
this.muteButton.loadTexture('mute_button');
} else {
this.sound_click.play();
this.sound_music.volume = 0;
this.sound_explosion.volume = 0;
this.sound_hit1.volume = 0;
this.sound_hit2.volume = 0;
this.sound_hit3.volume = 0;
this.sound_muted = true;
this.muteButton.loadTexture('mute_button_muted');
}
},
quitGame: function (pointer) {
// Here you should destroy anything you no longer need.
// Stop music, delete sprites, purge caches, free resources, all that good stuff.
this.sound_click.play();
this.sound_music.stop();
this.state.start('MainMenu');
},
restartGame: function (pointer) {
if (this.game['GameData'].currentLives > 0) {
clearTimeout(this.winTimeout);
clearTimeout(this.loseTimeout);
this.sound_music.stop();
this.state.start('Game');
}