-
Notifications
You must be signed in to change notification settings - Fork 20
/
GeoBlasterExtended.html
executable file
·1619 lines (1237 loc) · 42.2 KB
/
GeoBlasterExtended.html
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
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Geo Blaster Basic Game</title>
<script type="text/javascript">
window.addEventListener('load', eventWindowLoaded, false);
function eventWindowLoaded() {
canvasApp();
}
</script>
<script src="modernizr-1.6.min.js"></script>
<script language="Javascript" ">
function canvasSupport () {
return Modernizr.canvas;
}
function supportedAudioFormat(audio) {
var returnExtension = "";
if (audio.canPlayType("audio/ogg") =="probably" || audio.canPlayType("audio/ogg") == "maybe") {
returnExtension = "ogg";
} else if(audio.canPlayType("audio/wav") =="probably" || audio.canPlayType("audio/wav") == "maybe") {
returnExtension = "wav";
} else if(audio.canPlayType("audio/wav") == "probably" || audio.canPlayType("audio/wav") == "maybe") {
returnExtension = "mp3";
}
return returnExtension;
}
function canvasApp(){
if (!canvasSupport()) {
return;
}else{
theCanvas = document.getElementById('canvas');
context = theCanvas.getContext('2d');
}
//sounds
const SOUND_EXPLODE = "explode1";
const SOUND_SHOOT = "shoot1";
const SOUND_SAUCER_SHOOT = "saucershoot"
const MAX_SOUNDS = 9;
var soundPool = new Array();
var explodeSound;
var explodeSound2;
var explodeSound3;
var shootSound;
var shootSound2;
var shootSound3;
var saucershootSound;
var saucershootSound2;
var saucershootSound3;
var audioType;
//application states
const GAME_STATE_INIT=0;
const GAME_STATE_WAIT_FOR_LOAD=5;
const GAME_STATE_TITLE=10;
const GAME_STATE_NEW_GAME=20;
const GAME_STATE_NEW_LEVEL=30;
const GAME_STATE_PLAYER_START=40;
const GAME_STATE_PLAY_LEVEL=50;
const GAME_STATE_PLAYER_DIE=60;
const GAME_STATE_GAME_OVER=70;
var currentGameState=0;
var currentGameStateFunction=null;
//title screen
var titleStarted=false;
//gameover screen
var gameOverStarted=false;
//objects for game play
//game environment
var score=0;
var level=0;
var extraShipAtEach=10000;
var extraShipsEarned=0;
var playerShips=3;
//playfield
var xMin=0;
var xMax=400;
var yMin=0;
var yMax=400;
//score values
var bigRockScore=50;
var medRockScore=75;
var smlRockScore=100;
var saucerScore=300;
//rock scale constants
const ROCK_SCALE_LARGE=1;
const ROCK_SCALE_MEDIUM=2;
const ROCK_SCALE_SMALL=3;
//create game objects and arrays
var player={};
var rocks=[];
var saucers=[];
var playerMissiles=[];
var particles=[];
var saucerMissiles=[];
var particlePool=[];
var maxParticles=200;
var newPartcile;
var tempParticle;
//level specific
var levelRockMaxSpeedAdjust=1;
var levelSaucerMax = 1;
var levelSaucerOccurenceRate=25; //this will be multiplied by the level and a max can be se
var levelSaucerSpeed=1;
var levelSaucerFireDelay=300;
var levelSaucerFireRate=30;
var levelSaucerMissileSpeed=1;
//keyPresses
var keyPressList=[];
//tile sheets
var shipTiles;
var shipTiles2;
var saucerTiles;
var largeRockTiles;
var mediumRockTiles;
var smallRockTiles;
var particleTiles;
//***sounds
function itemLoaded(event) {
loadCount++;
////console.log("loading:" + loadCount)
if (loadCount >= itemsToLoad) {
shootSound.removeEventListener("canplaythrough",itemLoaded, false);
shootSound2.removeEventListener("canplaythrough",itemLoaded, false);
shootSound3.removeEventListener("canplaythrough",itemLoaded, false);
explodeSound.removeEventListener("canplaythrough",itemLoaded,false);
explodeSound2.removeEventListener("canplaythrough",itemLoaded,false);
explodeSound3.removeEventListener("canplaythrough",itemLoaded,false);
saucershootSound.removeEventListener("canplaythrough",itemLoaded,false);
saucershootSound2.removeEventListener("canplaythrough",itemLoaded,false);
saucershootSound3.removeEventListener("canplaythrough",itemLoaded,false);
soundPool.push({name:"explode1", element:explodeSound, played:false});
soundPool.push({name:"explode1", element:explodeSound2, played:false});
soundPool.push({name:"explode1", element:explodeSound3, played:false});
soundPool.push({name:"shoot1", element:shootSound, played:false});
soundPool.push({name:"shoot1", element:shootSound2, played:false});
soundPool.push({name:"shoot1", element:shootSound3, played:false});
soundPool.push({name:"saucershoot", element:saucershootSound, played:false});
soundPool.push({name:"saucershoot", element:saucershootSound2, played:false});
soundPool.push({name:"saucershoot", element:saucershootSound3, played:false});
switchGameState(GAME_STATE_TITLE)
}
}
function playSound(sound,volume) {
ConsoleLog.log("play sound" + sound);
var soundFound = false;
var soundIndex = 0;
var tempSound;
if (soundPool.length> 0) {
while (!soundFound && soundIndex < soundPool.length) {
var tSound = soundPool[soundIndex];
if ((tSound.element.ended || !tSound.played) && tSound.name == sound) {
soundFound = true;
tSound.played = true;
} else {
soundIndex++;
}
}
}
if (soundFound) {
ConsoleLog.log("sound found");
tempSound = soundPool[soundIndex].element;
//tempSound.setAttribute("src", sound + "." + audioType);
//tempSound.loop = false;
//tempSound.volume = volume;
tempSound.play();
} else if (soundPool.length < MAX_SOUNDS){
ConsoleLog.log("sound not found");
tempSound = document.createElement("audio");
tempSound.setAttribute("src", sound + "." + audioType);
tempSound.volume = volume;
tempSound.play();
soundPool.push({name:sound, element:tempSound, type:audioType, played:true});
}
}
function runGame(){
currentGameStateFunction();
}
function switchGameState(newState) {
currentGameState=newState;
switch (currentGameState) {
case GAME_STATE_INIT:
currentGameStateFunction=gameStateInit;
break;
case GAME_STATE_WAIT_FOR_LOAD:
currentGameStateFunction=gameStateWaitForLoad;
break;
case GAME_STATE_TITLE:
currentGameStateFunction=gameStateTitle;
break;
case GAME_STATE_NEW_GAME:
currentGameStateFunction=gameStateNewGame;
break;
case GAME_STATE_NEW_LEVEL:
currentGameStateFunction=gameStateNewLevel;
break;
case GAME_STATE_PLAYER_START:
currentGameStateFunction=gameStatePlayerStart;
break;
case GAME_STATE_PLAY_LEVEL:
currentGameStateFunction=gameStatePlayLevel;
break;
case GAME_STATE_PLAYER_DIE:
currentGameStateFunction=gameStatePlayerDie;
break;
case GAME_STATE_GAME_OVER:
currentGameStateFunction=gameStateGameOver;
break;
}
}
function gameStateWaitForLoad(){
//do nothing while loading events occur
//console.log("doing nothing...")
}
function createObjectPools(){
for (var ctr=0;ctr<maxParticles;ctr++){
var newParticle={};
particlePool.push(newParticle)
}
////console.log("particlePool=" + particlePool.length)
}
function gameStateInit() {
createObjectPools();
loadCount=0;
itemsToLoad = 16;
explodeSound = document.createElement("audio");
document.body.appendChild(explodeSound);
audioType = supportedAudioFormat(explodeSound);
explodeSound.setAttribute("src", "explode1." + audioType);
explodeSound.addEventListener("canplaythrough",itemLoaded,false);
explodeSound2 = document.createElement("audio");
document.body.appendChild(explodeSound2);
explodeSound2.setAttribute("src", "explode1." + audioType);
explodeSound2.addEventListener("canplaythrough",itemLoaded,false);
explodeSound3 = document.createElement("audio");
document.body.appendChild(explodeSound3);
explodeSound3.setAttribute("src", "explode1." + audioType);
explodeSound3.addEventListener("canplaythrough",itemLoaded,false);
shootSound = document.createElement("audio");
audioType = supportedAudioFormat(shootSound);
document.body.appendChild(shootSound);
shootSound.setAttribute("src", "shoot1." + audioType);
shootSound.addEventListener("canplaythrough",itemLoaded,false);
shootSound2 = document.createElement("audio");
document.body.appendChild(shootSound2);
shootSound2.setAttribute("src", "shoot1." + audioType);
shootSound2.addEventListener("canplaythrough",itemLoaded,false);
shootSound3 = document.createElement("audio");
document.body.appendChild(shootSound3);
shootSound3.setAttribute("src", "shoot1." + audioType);
shootSound3.addEventListener("canplaythrough",itemLoaded,false);
saucershootSound = document.createElement("audio");
audioType = supportedAudioFormat(saucershootSound);
document.body.appendChild(saucershootSound);
saucershootSound.setAttribute("src", "saucershoot." + audioType);
saucershootSound.addEventListener("canplaythrough",itemLoaded,false);
saucershootSound2 = document.createElement("audio");
document.body.appendChild(saucershootSound2);
saucershootSound2.setAttribute("src", "saucershoot." + audioType);
saucershootSound2.addEventListener("canplaythrough",itemLoaded,false);
saucershootSound3 = document.createElement("audio");
document.body.appendChild(saucershootSound3);
saucershootSound3.setAttribute("src", "saucershoot." + audioType);
saucershootSound3.addEventListener("canplaythrough",itemLoaded,false);
shipTiles = new Image();
shipTiles.src = "ship_tiles.png";
shipTiles.onload = itemLoaded;
shipTiles2 = new Image();
shipTiles2.src = "ship_tiles2.png";
shipTiles2.onload = itemLoaded;
saucerTiles= new Image();
saucerTiles.src = "saucer.png";
saucerTiles.onload = itemLoaded;
largeRockTiles = new Image();
largeRockTiles.src = "largerocks.png";
largeRockTiles.onload = itemLoaded;
mediumRockTiles = new Image();
mediumRockTiles.src = "mediumrocks.png";
mediumRockTiles.onload = itemLoaded;
smallRockTiles = new Image();
smallRockTiles.src = "smallrocks.png";
smallRockTiles.onload = itemLoaded;
particleTiles = new Image();
particleTiles.src = "parts.png";
particleTiles.onload = itemLoaded;
switchGameState(GAME_STATE_WAIT_FOR_LOAD);
}
function gameStateTitle() {
if (titleStarted !=true){
fillBackground();
setTextStyleTitle();
context.fillText ("Geo Blaster X-ten-d", 120, 70);
setTextStyle();
context.fillText ("Press Space To Play", 130, 140);
setTextStyleCredits();
context.fillText ("An HTML5 Example Game", 125, 200);
context.fillText ("From our upcomming HTML5 Canvas", 100, 215);
context.fillText ("book on O'Reilly Press", 130, 230);
context.fillText ("Game Code - Jeff Fulton", 130, 260);
context.fillText ("Sound Manager - Steve Fulton", 120, 275);
titleStarted=true;
}else{
//wait for space key click
if (keyPressList[32]==true){
ConsoleLog.log("space pressed");
switchGameState(GAME_STATE_NEW_GAME);
titleStarted=false;
}
}
}
function gameStateNewGame(){
//ConsoleLog.log("gameStateNewGame")
//setup new game
level=0;
score=0;
playerShips=3;
player.maxVelocity=5;
player.width=32;
player.height=32;
player.halfWidth=16;
player.halfHeight=16;
player.hitWidth=24;
player.hitHeight=24;
player.rotationalVelocity=10; //how many degrees to turn the ship
player.thrustAcceleration=.05;
player.missileFrameDelay=5;
player.thrust=false;
player.alpha=1;
player.rotation=0;
player.x=0;
player.y=0;
fillBackground();
renderScoreBoard();
switchGameState(GAME_STATE_NEW_LEVEL)
}
function gameStateNewLevel(){
rocks=[];
saucers=[];
playerMissiles=[];
particles=[];
saucerMissiles=[];
level++;
levelRockMaxSpeedAdjust=level*.25;
if (levelRockMaxSpeedAdjust > 3){
levelRockMaxSpeed=3;
}
levelSaucerMax=1+Math.floor(level/10);
if (levelSaucerMax > 5){
levelSaucerMax=5;
}
levelSaucerOccurenceRate=10+3*level;
if (levelSaucerOccurenceRate > 35){
levelSaucerOccurenceRate=35;
}
levelSaucerSpeed=1+.5*level;
if (levelSaucerSpeed>5){
levelSaucerSpeed=5;
}
levelSaucerFireDelay=120-10*level;
if (levelSaucerFireDelay<20) {
levelSaucerFireDelay=20;
}
levelSaucerFireRate=20 + 3*level;
if (levelSaucerFireRate<50) {
levelSaucerFireRate=50;
}
levelSaucerMissileSpeed=1+.2*level;
if (levelSaucerMissileSpeed > 4){
levelSaucerMissileSpeed=4;
}
//create level rocks
for (var newRockctr=0;newRockctr<level+3;newRockctr++){
var newRock={};
newRock.scale=1;
//scale
//1=large
//2=medium
//3=small
//these will be used as the devisor for the new size
//50/1=50
//50/2=25
//50/3=16
newRock.width=64;
newRock.height=64;
newRock.halfWidth=32;
newRock.halfHeight=32;
newRock.hitWidth=48;
newRock.hitHeight=48;
//start all new rocks in upper left for ship safety
newRock.x=Math.floor(Math.random()*50);
//ConsoleLog.log("newRock.x=" + newRock.x);
newRock.y=Math.floor(Math.random()*50);
//ConsoleLog.log("newRock.y=" + newRock.y);
newRock.dx=(Math.random()*2)+levelRockMaxSpeedAdjust;
if (Math.random()<.5){
newRock.dx*=-1;
}
newRock.dy=(Math.random()*2)+levelRockMaxSpeedAdjust;
if (Math.random()<.5){
newRock.dy*=-1;
}
//rotation speed and direction
if (Math.random()<.5){
newRock.rotationInc=-1;
}else{
newRock.rotationInc=1;
}
newRock.animationDelay=Math.floor(Math.random()*3+1);
newRock.animationCount=0;
newRock.scoreValue=bigRockScore;
newRock.rotation=0;
rocks.push(newRock);
//ConsoleLog.log("rock created rotationInc=" + newRock.rotationInc);
}
resetPlayer();
switchGameState(GAME_STATE_PLAYER_START);
}
function gameStatePlayerStart(){
fillBackground();
renderScoreBoard();
if (player.alpha < 1){
player.alpha+=.01;
ConsoleLog.log("player.alpha=" + context.globalAlpha)
}else{
switchGameState(GAME_STATE_PLAY_LEVEL);
player.safe=false; // added chapter 9
}
//renderPlayerShip(player.x, player.y,270,1);
context.globalAlpha=1;
//new in chapter 9
checkKeys();
update();
render(); //added chapter 9
checkCollisions();
checkForExtraShip();
checkForEndOfLevel();
frameRateCounter.countFrames();
}
function gameStatePlayLevel(){
checkKeys();
update();
render();
checkCollisions();
checkForExtraShip();
checkForEndOfLevel();
frameRateCounter.countFrames();
}
function resetPlayer() {
player.rotation=270;
player.x=.5*xMax;
player.y=.5*yMax;
player.facingX=0;
player.facingY=0;
player.movingX=0;
player.movingY=0;
player.alpha=0;
player.missileFrameCount=0;
//added chapter 9
player.safe=true;
}
function checkForExtraShip() {
if (Math.floor(score/extraShipAtEach) > extraShipsEarned) {
playerShips++
extraShipsEarned++;
}
}
function checkForEndOfLevel(){
if (rocks.length==0) {
switchGameState(GAME_STATE_NEW_LEVEL);
}
}
function gameStatePlayerDie(){
if (particles.length >0 || playerMissiles.length>0) {
fillBackground();
renderScoreBoard();
updateRocks();
updateSaucers();
updateParticles();
updateSaucerMissiles();
updatePlayerMissiles();
renderRocks();
renderSaucers();
renderParticles();
renderSaucerMissiles();
renderPlayerMissiles();
frameRateCounter.countFrames();
}else{
playerShips--;
if (playerShips<1) {
switchGameState(GAME_STATE_GAME_OVER);
}else{
//resetPlayer();
switchGameState(GAME_STATE_PLAYER_START);
}
}
}
function gameStateGameOver() {
//ConsoleLog.log("Game Over State");
if (gameOverStarted !=true){
fillBackground();
renderScoreBoard();
setTextStyle();
context.fillText ("Game Over!", 160, 70);
context.fillText ("Press Space To Play", 130, 140);
gameOverStarted=true;
}else{
//wait for space key click
if (keyPressList[32]==true){
ConsoleLog.log("space pressed");
switchGameState(GAME_STATE_TITLE);
gameOverStarted=false;
}
}
}
function fillBackground() {
// draw background and text
context.fillStyle = '#000000';
context.fillRect(xMin, yMin, xMax, yMax);
}
function setTextStyle() {
context.fillStyle = '#ffffff';
context.font = '15px _sans';
context.textBaseline = 'top';
}
function setTextStyleTitle() {
context.fillStyle = '#54ebeb';
context.font = '20px _sans';
context.textBaseline = 'top';
}
function setTextStyleCredits() {
context.fillStyle = '#ffffff';
context.font = '12px _sans';
context.textBaseline = 'top';
}
function renderScoreBoard() {
context.fillStyle = "#ffffff";
context.fillText('Score ' + score, 10, 20);
renderPlayerShip(200,16,270,.75)
context.fillText('X ' + playerShips, 220, 20);
context.fillText('FPS: ' + frameRateCounter.lastFrameCount, 300,20)
}
function checkKeys() {
//check keys
if (keyPressList[38]==true){
//thrust
var angleInRadians = player.rotation * Math.PI / 180;
player.facingX=Math.cos(angleInRadians);
player.facingY=Math.sin(angleInRadians);
var movingXNew=player.movingX+player.thrustAcceleration*player.facingX;
var movingYNew=player.movingY+player.thrustAcceleration*player.facingY;
var currentVelocity= Math.sqrt ((movingXNew*movingXNew) + (movingXNew*movingXNew));
if (currentVelocity < player.maxVelocity) {
player.movingX=movingXNew;
player.movingY=movingYNew;
}
player.thrust=true;
}else{
player.thrust=false;
}
if (keyPressList[37]==true) {
//rotate counter-clockwise
player.rotation-=player.rotationalVelocity;
if (player.rotation <0) {
player.rotation=350
}
}
if (keyPressList[39]==true) {
//rotate clockwise
player.rotation+=player.rotationalVelocity;
if (player.rotation >350) {
player.rotation=10
}
}
if (keyPressList[32]==true) {
//ConsoleLog.log("player.missileFrameCount=" + player.missileFrameCount);
//ConsoleLog.log("player.missileFrameDelay=" + player.missileFrameDelay);
if (player.missileFrameCount>player.missileFrameDelay){
playSound(SOUND_SHOOT,.5);
firePlayerMissile();
player.missileFrameCount=0;
}
}
}
function update() {
updatePlayer();
updatePlayerMissiles();
updateRocks();
updateSaucers();
updateSaucerMissiles();
updateParticles();
}
function render() {
fillBackground();
renderScoreBoard();
renderPlayerShip(player.x,player.y,player.rotation,1);
renderPlayerMissiles();
renderRocks();
renderSaucers();
renderSaucerMissiles();
renderParticles();
}
function updatePlayer() {
player.missileFrameCount++;
player.x+=player.movingX*frameRateCounter.step;
player.y+=player.movingY*frameRateCounter.step;
if (player.x > xMax) {
player.x=-player.width;
}else if (player.x<-player.width){
player.x=xMax;
}
if (player.y > yMax) {
player.y=-player.height;
}else if (player.y<-player.height){
player.y=yMax;
}
}
function updatePlayerMissiles() {
var tempPlayerMissile={};
var playerMissileLength=playerMissiles.length-1;
//ConsoleLog.log("update playerMissileLength=" + playerMissileLength);
for (var playerMissileCtr=playerMissileLength;playerMissileCtr>=0;playerMissileCtr--){
//ConsoleLog.log("update player missile" + playerMissileCtr)
tempPlayerMissile=playerMissiles[playerMissileCtr];
tempPlayerMissile.x+=tempPlayerMissile.dx*frameRateCounter.step;
tempPlayerMissile.y+=tempPlayerMissile.dy*frameRateCounter.step;
if (tempPlayerMissile.x > xMax) {
tempPlayerMissile.x=-tempPlayerMissile.width;
}else if (tempPlayerMissile.x<-tempPlayerMissile.width){
tempPlayerMissile.x=xMax;
}
if (tempPlayerMissile.y > yMax) {
tempPlayerMissile.y=-tempPlayerMissile.height;
}else if (tempPlayerMissile.y<-tempPlayerMissile.height){
tempPlayerMissile.y=yMax;
}
tempPlayerMissile.lifeCtr++;
if (tempPlayerMissile.lifeCtr > tempPlayerMissile.life){
//ConsoleLog.log("removing player missile");
playerMissiles.splice(playerMissileCtr,1)
tempPlayerMissile=null;
}
}
}
function updateRocks(){
var tempRock={};
var rocksLength=rocks.length-1;
//ConsoleLog.log("update rocks length=" + rocksLength);
for (var rockCtr=rocksLength;rockCtr>=0;rockCtr--){
tempRock=rocks[rockCtr]
tempRock.x+=tempRock.dx*frameRateCounter.step;
tempRock.y+=tempRock.dy*frameRateCounter.step;
tempRock.animationCount++;
if (tempRock.animationCount > tempRock.animationDelay){
tempRock.animationCount=0;
tempRock.rotation+=tempRock.rotationInc;
if (tempRock.rotation > 4){
tempRock.rotation=0;
}else if (tempRock.rotation <0){
tempRock.rotation=4;
}
}
if (tempRock.x > xMax) {
tempRock.x=xMin-tempRock.width;
}else if (tempRock.x<xMin-tempRock.width){
tempRock.x=xMax;
}
if (tempRock.y > yMax) {
tempRock.y=yMin-tempRock.width;
}else if (tempRock.y<yMin-tempRock.width){
tempRock.y=yMax;
}
//ConsoleLog.log("update rock "+ rockCtr)
}
}
function updateSaucers() {
//first check to see is we want to add a saucer
if (saucers.length< levelSaucerMax){
if (Math.floor(Math.random()*100)<=levelSaucerOccurenceRate){
//ConsoleLog.log("create saucer")
var newSaucer={};
newSaucer.width=30;
newSaucer.height=13;
newSaucer.halfHeight=6.5;
newSaucer.halfWidth=15;
newSaucer.hitWidth=30;
newSaucer.hitHeight=13;
newSaucer.scoreValue=saucerScore;
newSaucer.fireRate=levelSaucerFireRate;
newSaucer.fireDelay=levelSaucerFireDelay;
newSaucer.fireDelayCount=0;
newSaucer.missileSpeed=levelSaucerMissileSpeed;
newSaucer.dy=(Math.random()*2);
if (Math.floor(Math.random)*2==1){
newSaucer.dy*=-1;
}
//choose betweeen left or right edge to start
if (Math.floor(Math.random()*2)==1){
//start on right and go left
newSaucer.x=450;
newSaucer.dx=-1*levelSaucerSpeed;
}else{
//left to right
newSaucer.x=-50;
newSaucer.dx=levelSaucerSpeed;
}
newSaucer.missileSpeed=levelSaucerMissileSpeed;
newSaucer.fireDelay=levelSaucerFireDelay;
newSaucer.fireRate=levelSaucerFireRate;
newSaucer.y=Math.floor(Math.random()*400);
saucers.push(newSaucer);
}
}
var tempSaucer={};
var saucerLength=saucers.length-1;
//ConsoleLog.log("update rocks length=" + rocksLength);
for (var saucerCtr=saucerLength;saucerCtr>=0;saucerCtr--){
tempSaucer=saucers[saucerCtr];
//should saucer fire
tempSaucer.fireDelayCount++;
if (Math.floor(Math.random()*100) <=tempSaucer.fireRate && tempSaucer.fireDelayCount>tempSaucer.fireDelay ){
playSound(SOUND_SAUCER_SHOOT,.5);
fireSaucerMissile(tempSaucer)
tempSaucer.fireDelayCount=0;
}
var remove=false;
tempSaucer.x+=tempSaucer.dx*frameRateCounter.step;
tempSaucer.y+=tempSaucer.dy*frameRateCounter.step;
//remove saucers on left and right edges
if (tempSaucer.dx > 0 && tempSaucer.x >xMax){
remove=true;
}else if (tempSaucer.dx <0 &&tempSaucer.x<xMin-tempSaucer.width){
remove=true;
}
//bounce saucers off over vertical edges
if (tempSaucer.y > yMax || tempSaucer.y<yMin-tempSaucer.width) {
tempSaucer.dy*=-1
}
if (remove==true) {
//remove the saucer
ConsoleLog.log("saucer removed")
saucers.splice(saucerCtr,1);
tempSaucer=null;
}
}
}
function updateSaucerMissiles() {
var tempSaucerMissile={};
var saucerMissileLength=saucerMissiles.length-1;
for (var saucerMissileCtr=saucerMissileLength;saucerMissileCtr>=0;saucerMissileCtr--){
//ConsoleLog.log("update player missile" + playerMissileCtr)
tempSaucerMissile=saucerMissiles[saucerMissileCtr];
tempSaucerMissile.x+=tempSaucerMissile.dx*frameRateCounter.step;
tempSaucerMissile.y+=tempSaucerMissile.dy*frameRateCounter.step;
if (tempSaucerMissile.x > xMax) {
tempSaucerMissile.x=-tempSaucerMissile.width;
}else if (tempSaucerMissile.x<-tempSaucerMissile.width){
tempSaucerMissile.x=xMax;
}
if (tempSaucerMissile.y > yMax) {
tempSaucerMissile.y=-tempSaucerMissile.height;
}else if (tempSaucerMissile.y<-tempSaucerMissile.height){
tempSaucerMissile.y=yMax;
}
tempSaucerMissile.lifeCtr++;
if (tempSaucerMissile.lifeCtr > tempSaucerMissile.life){
//remove
saucerMissiles.splice(saucerMissileCtr,1)
tempSaucerMissile=null;
}
}
}
function updateParticles() {
var particleLength=particles.length-1;
ConsoleLog.log("particle=" + particleLength)
ConsoleLog.log("particlePool=" + particlePool.length)
for (var particleCtr=particleLength;particleCtr>=0;particleCtr--){
var remove =false;
tempParticle=particles[particleCtr];
tempParticle.x+=tempParticle.dx*frameRateCounter.step;
tempParticle.y+=tempParticle.dy*frameRateCounter.step;
tempParticle.lifeCtr++;
//ConsoleLog.log("particle.lifeCtr=" + tempParticle.lifeCtr);
//try{