-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
1058 lines (981 loc) · 28.4 KB
/
index.html
File metadata and controls
1058 lines (981 loc) · 28.4 KB
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 PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<style type="text/css">
* {
margin: 0;
padding: 0;
}
a img {
border: none;
}
a:link {color: #b8b8b8}
a:visited {color: #b8b8b8}
a:active {color: #b8b8b8}
a:hover {color: #1a239f}
p {
margin-top: 5px;
margin-left: 14px;
font-family:Arial, Helvetica, sans-serif;
color: #ffffff;
font-size:24px;
font-weight:bold;
}
h1 {
margin-top: 5px;
margin-left: 14px;
font-family:Arial, Helvetica, sans-serif;
color: #b8b8b8;
font-size:16px;
font-weight:normal;
text-align: center;
}
html, body {
background-image:url(StarField.jpg);
background-attachment:fixed;
background-repeat:repeat;
height:100%;
}
#footer {
height: 25px; /* Define height of the footer */
position: absolute;
bottom: 0; /* Sit it on the bottom */
left: 0;
width: 100%; /* As wide as it's allowed */
text-align: center;
font-size: 16px;
font-family: Arial, Helvetica, sans-serif;
color: #b8b8b8;
}
#footerWedge {
width: 1px;
height: 10px;
clear: both;
}
#container {
background-color: rgba(255,255,255,0.4);
width: 950px;
margin: 0 auto;
position: relative;
height: auto !important;
min-height: 100%;
height: 100%;
}
#leftSide {
width:40px;
background-image:url(leftSide.png);
background-repeat:repeat-y;
float:left;
height:100%;
position:absolute;
left: -40px;
}
#rightSide {
width:40px;
background-image:url(rightSide.png);
background-repeat:repeat-y;
float:right;
height:100%;
position:absolute;
left:950px;
}
#content {
margin-top: -10px;
padding-bottom:25px;
}
#header {
margin-top:10px;
margin-left:30px;
padding-top: 10px;
height: 120px;
width: 865px;
}
#header img {
float:left;
}
#spacer {
float:left;
width:10px;
height:64px;
}
#game {
width: 900px;
height: 70px;
margin-left: 25px;
margin-top: 0px;
margin-bottom: 15px;
border: solid 1px #b8b8b8;
-moz-border-radius: 5px;
-webkit-border-radius: 5px;
-khtml-border-radius: 5px;
border-radius: 5px;
background-color: rgba(0,0,0,0.3);
}
#stats {
width: 885px;
height: 182px;
margin-left: 25px;
margin-top: 0px;
margin-bottom: 15px;
padding-top: 10px;
padding-left: 15px;
border: solid 1px #b8b8b8;
-moz-border-radius: 5px;
-webkit-border-radius: 5px;
-khtml-border-radius: 5px;
border-radius: 5px;
background-color: rgba(0,0,0,0.3);
}
#status {
width: 885px;
height: 52px;
margin-left: 25px;
margin-top: 0px;
margin-bottom: 15px;
padding-top: 10px;
padding-left: 15px;
border: solid 1px #b8b8b8;
-moz-border-radius: 5px;
-webkit-border-radius: 5px;
-khtml-border-radius: 5px;
border-radius: 5px;
background-color: rgba(0,0,0,0.3);
font-size: 35px;
text-align: center;
}
#scores {
width: 900px;
height: 70px;
margin-left: 25px;
margin-top: 15px;
margin-bottom: 15px;
padding-top: 20px;
border: solid 1px #b8b8b8;
-moz-border-radius: 5px;
-webkit-border-radius: 5px;
-khtml-border-radius: 5px;
border-radius: 5px;
background-color: rgba(0,0,0,0.3);
font-size: 42px;
text-align: center;
}
#myCanvas {
width: 900px;
height: 450px;
margin-left: 25px;
margin-top: 0px;
border: solid 1px #b8b8b8;
-moz-border-radius: 5px;
-webkit-border-radius: 5px;
-khtml-border-radius: 5px;
border-radius: 5px;
}
</style>
<!--[if IE]>
<style type="text/css">
#container {
filter:progid:DXImageTransform.Microsoft.gradient(startColorstr=#99000000,endColorstr=#99000000);
-ms-filter:progid:DXImageTransform.Microsoft.gradient(startColorstr=#99000000,endColorstr=#99000000);
width: 950px;
margin: 0 auto;
position: relative;
height: auto !important;
min-height: 100%;
height: 100%;
}
#content {
margin-top: -10px;
padding-bottom: 25px;
}
#game {
width: 900px;
height: 70px;
margin-left: 25px;
margin-top: 0px;
margin-bottom: 15px;
border: solid 1px #b8b8b8;
filter:progid:DXImageTransform.Microsoft.gradient(startColorstr=#b3000000,endColorstr=#b3000000);
-ms-filter:progid:DXImageTransform.Microsoft.gradient(startColorstr=#b3000000,endColorstr=#b3000000);
}
#scores {
width: 900px;
height: 70px;
margin-left: 25px;
margin-top: 15px;
margin-bottom: 15px;
border: solid 1px #b8b8b8;
filter:progid:DXImageTransform.Microsoft.gradient(startColorstr=#b3000000,endColorstr=#b3000000);
-ms-filter:progid:DXImageTransform.Microsoft.gradient(startColorstr=#b3000000,endColorstr=#b3000000);
}
</style>
<![endif]-->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=9" />
<title>Adaptive Space Shooter</title>
</head>
<body>
<div id="container">
<div id="content">
<div id="leftSide"></div>
<div id="rightSide"></div>
<div id="header"><img src="Title.png" />
<a href="https://github.com/dangerman/Adaptive-Space-Shooter"><img style="position: absolute; top: 0; left: 0; border: 0; " src="https://a248.e.akamai.net/camo.github.com/6429057dfef9e98189338d22e7f6646c6694f032/687474703a2f2f73332e616d617a6f6e6177732e636f6d2f6769746875622f726962626f6e732f666f726b6d655f6c6566745f6f72616e67655f6666373630302e706e67" alt="Fork me on GitHub"></a>
</div>
<div id="game">
<p>INSTRUCTIONS:<br/>Use arrow keys or mouse to move and click or press space to shoot
</p></div>
<canvas id = "myCanvas" width="900" height="450" tabindex="1">
<!-- Need to set the tab index so it gets keyboard inputs -->
</canvas>
<p id = "scores">
</p>
<p id = "status">
</p>
<p id = "stats">
</p>
<script type="text/javascript">
//Remove curosr
document.getElementById("myCanvas").style.cursor = "none";
//Set up audio
maxchannels = 10; // Allows multiple sounds to be played in parallel
channels = new Array();
for (var i=0;i<maxchannels;i++) {
channels[i]= new Array();
channels[i]['channel'] = new Audio();
channels[i]['finished'] = -1;
}
function playSound(sound) {
for (var i=0;i<channels.length;i++) {
date = new Date();
if (channels[i]['finished'] < date.getTime()) { //if channel finished
channels[i]['finished'] = date.getTime() + document.getElementById(sound).duration*1000
channels[i]['channel'].src = document.getElementById(sound).src;
channels[i]['channel'].load();
channels[i]['channel'].play();
}
}
}
//Set up images
var playerImg = new Image();
playerImg.src = "PlayerShip.png";
var playerImg2 = new Image();
playerImg2.src = "PlayerShip2.png";
var bulletImg = new Image();
bulletImg.src = "Bullet.png";
var enemyBulletImg = new Image();
enemyBulletImg.src = "EnemyBullet.png";
var enemyImg1 = new Image();
enemyImg1.src = "EasyEnemy.png";
var enemyImg2 = new Image();
enemyImg2.src = "MediumEnemy.png";
var enemyImg2b = new Image();
enemyImg2b.src = "MediumEnemy2.png";
var enemyImg3 = new Image();
enemyImg3.src = "HardEnemy.png";
var enemyImg3b = new Image();
enemyImg3b.src = "HardEnemy2.png";
var pickUp1 = new Image();
pickUp1.src = "Life.png";
var pickUpCue1 = new Image();
pickUpCue1.src = "PickUp.png";
var portalImg = new Image();
portalImg.src = "Portal.png";
//Initialise game variables
var running = true;
var starCount = 50; //number of stars
var size = 10; //Size of particle
var gametime = 0;
var score = 0;
var energy = 900;
var levelNo = 1; //0th level is menu
var levelKills = 0;
var levelComplete = false;
var levelFailed = false;
var inMenu = true;
var missedPowerups = 0;
var rank = "C";
var tip = "Try to dodge the enemies and their gunfire!";
//Adaptive variables
var particleSpeed = 0.5;
var targetSpeed;
var targetHP;
var spawnInterval = 50; //how often to spawn enemies
var powerupInterval = 120; //how often to give powerups
var enemyBulletFreq = 120;
var scoreThreshold = 6000; //Kills needed to pass to next level
var enemyHPMultiplier = 1;
var accuracyLevel = 0;
var SPMLevel = 0;
//Storing stats
hits = new Object();
hits.particle1 = 0;
misses = new Object();
misses.particle1 = 0;
var bulletHits = 0;
var bulletMisses = 0;
//Lists of things
var stars = new Array();
var particles = new Array();
var targets = new Array();
var bullets = new Array();
var powerups = new Array();
//Objects
function Star(x) {
this.x = x;
this.y = Math.random()*canvas.height;
this.xvel = 5+Math.random()*5;
this.size = Math.random()*3;
}
function Particle(x,y,xvel,yvel,type, hp) { //Basic enemies - kill player upon contact, destroyed by bullets
this.x = x;
this.y = y;
this.xvel = xvel;
this.yvel = yvel;
this.time = 0; //Time since particle spawned
this.type = type;
this.bulletSpawn = bulletSpawn;
this.hp = hp;
}
function bulletSpawn() {
particles.push(new Particle(this.x,this.y, -20,0,3, 1000));
}
function Bullet(x,y,type) { //Bullets fired by player
this.x = x;
this.y = y;
this.type = type;
}
function Player() {
this.x = 0;
this.y = 0;
this.hp = 100;
this.goingDown = false;
this.goingUp = false;
this.goingRight = false;
this.goingLeft = false;
this.powerup = 0;
this.powerupTime = 0;
}
function Powerup(x,y,type) {
this.x = x;
this.y = y;
this.type = type;
this.time = 600;
}
player = new Player();
//Set up canvas and stuff so we can draw
var canvas = document.getElementById("myCanvas");
var context = canvas.getContext("2d");
context.strokeStyle = "blue";
context.fillStyle="black";
canvas.onmousemove = getPosition;
//Portal object
portal = new Object();
portal.x = canvas.width-150;
portal.y = canvas.height/2;
portal.on = false;
//Set up inputs
canvas.addEventListener("click", shoot, false);
canvas.addEventListener("keydown",keyPressed,false);
canvas.addEventListener("keyup",keyUp,false);
function keyPressed(e) {
console.log("key pressed");
if (e.keyCode==37) {
player.goingLeft = true;
}
if (e.keyCode==38) {
player.goingUp = true;
}
if (e.keyCode==39) {
player.goingRight = true;
}
if (e.keyCode==40) {
player.goingDown = true;
}
if (e.keyCode==32) { //Space bar
if (inMenu) {
inMenu = false;
//save high scores
//update Player model
//generate level variables
if (portal.on && levelComplete)
levelNo++;
updateTip();
document.getElementById("status").innerHTML="Tip: "+tip;
levelKills = 0;
levelComplete = false;
portal.on = false;
energy = 1000; //Need to rest energy for next level
score = 0; ///Reset score for next level
missedPowerups = 0;
gametime = 0;
} else {
shoot();
}
}
}
function keyUp(e) {
if (e.keyCode==37) {
player.goingLeft = false;
}
if (e.keyCode==38) {
player.goingUp = false;
}
if (e.keyCode==39) {
player.goingRight = false;
}
if (e.keyCode==40) {
player.goingDown = false;
}
}
function makeParticle(isBullet, enemy) {
switch (levelNo)
{
case 1 : particles.push(new Particle(canvas.width,(canvas.height * 0.05) + Math.random()*(canvas.height*0.9), -(5+(Math.random()*6)),0,0,1)); break;
case 2 : particles.push(new Particle(canvas.width,(canvas.height * 0.05) + Math.random()*(canvas.height*0.7), -(5+(Math.random()*6)),0,1,1)); break;
case 3 :
{
var rand = Math.random();
if (rand > 0.5) {
particles.push(new Particle(canvas.width,(canvas.height * 0.05) + Math.random()*(canvas.height*0.9), -(5+(Math.random()*6)),0,0,1));
} else if (rand > 0.33) {
particles.push(new Particle(canvas.width,(canvas.height * 0.05) + Math.random()*(canvas.height*0.7), -(5+(Math.random()*6)),0,1,1));
}
} break;
case 4 :
{
var rand = Math.random();
if (rand > 0.1) {
particles.push(new Particle(canvas.width,(canvas.height * 0.05) + Math.random()*(canvas.height*0.9), -(5+(Math.random()*6)),0,0,1));
} else {
particles.push(new Particle(canvas.width,(canvas.height * 0.05) + Math.random()*(canvas.height*0.5), -(5+(Math.random()*6)),0,2,100));
}
} break;
case 5 :
{
var rand = Math.random();
if (rand > 0.2) {
particles.push(new Particle(canvas.width,(canvas.height * 0.05) + Math.random()*(canvas.height*0.9), -(5+(Math.random()*6)),0,0,1));
} else {
particles.push(new Particle(canvas.width,(canvas.height * 0.05) + Math.random()*(canvas.height*0.5), -(5+(Math.random()*6)),0,2,100));
}
} break;
case 6 :
{
var rand = Math.random();
if (rand > 0.66) {
particles.push(new Particle(canvas.width,(canvas.height * 0.05) + Math.random()*(canvas.height*0.9), -(5+(Math.random()*6)),0,0,1));
} else if (rand > 0.15) {
particles.push(new Particle(canvas.width,(canvas.height * 0.05) + Math.random()*(canvas.height*0.7), -(5+(Math.random()*6)),0,1,1));
} else {
particles.push(new Particle(canvas.width,(canvas.height * 0.05) + Math.random()*(canvas.height*0.5), -(5+(Math.random()*6)),0,2,100));
}
} break;
case 7 :
{
var rand = Math.random();
if (rand > 0.90) {
particles.push(new Particle(canvas.width,(canvas.height * 0.05) + Math.random()*(canvas.height*0.9), -(5+(Math.random()*6)),0,0,1));
} else if (rand > 0.15) {
particles.push(new Particle(canvas.width,(canvas.height * 0.05) + Math.random()*(canvas.height*0.7), -(5+(Math.random()*6)),0,1,1));
} else {
particles.push(new Particle(canvas.width,(canvas.height * 0.05) + Math.random()*(canvas.height*0.5), -(5+(Math.random()*6)),0,2,100));
}
} break;
case 8 :
{
var rand = Math.random();
if (rand > 0.85) {
particles.push(new Particle(canvas.width,(canvas.height * 0.05) + Math.random()*(canvas.height*0.9), -(5+(Math.random()*6)),0,0,1));
} else if (rand > 0.30) {
particles.push(new Particle(canvas.width,(canvas.height * 0.05) + Math.random()*(canvas.height*0.7), -(5+(Math.random()*6)),0,1,1));
} else {
particles.push(new Particle(canvas.width,(canvas.height * 0.05) + Math.random()*(canvas.height*0.5), -(5+(Math.random()*6)),0,2,100));
}
} break;
case 9 :
{
var rand = Math.random();
if (rand > 0.70) {
particles.push(new Particle(canvas.width,(canvas.height * 0.05) + Math.random()*(canvas.height*0.9), -(5+(Math.random()*6)),0,0,1));
} else if (rand > 0.60) {
particles.push(new Particle(canvas.width,(canvas.height * 0.05) + Math.random()*(canvas.height*0.7), -(5+(Math.random()*6)),0,1,1));
} else {
particles.push(new Particle(canvas.width,(canvas.height * 0.05) + Math.random()*(canvas.height*0.5), -(5+(Math.random()*6)),0,2,100));
}
} break;
case 10 :
{
var rand = Math.random();
if (rand > 0.90) {
particles.push(new Particle(canvas.width,(canvas.height * 0.05) + Math.random()*(canvas.height*0.9), -(5+(Math.random()*6)),0,0,1));
} else if (rand > 0.80) {
particles.push(new Particle(canvas.width,(canvas.height * 0.05) + Math.random()*(canvas.height*0.7), -(5+(Math.random()*6)),0,1,1));
} else {
particles.push(new Particle(canvas.width,(canvas.height * 0.05) + Math.random()*(canvas.height*0.5), -(5+(Math.random()*6)),0,2,100));
}
} break;
}
/*var rand = Math.random();
if (rand > 0.66) {
particles.push(new Particle(canvas.width,(canvas.height * 0.05) + Math.random()*(canvas.height*0.9), -(5+(Math.random()*6)),0,0,1));
} else if (rand > 0.33) {
particles.push(new Particle(canvas.width,(canvas.height * 0.05) + Math.random()*(canvas.height*0.7), -(5+(Math.random()*6)),0,1,1));
} else {
particles.push(new Particle(canvas.width,(canvas.height * 0.05) + Math.random()*(canvas.height*0.5), -(5+(Math.random()*6)),0,2,100));
}*/
}
function makePowerup() {
powerups.push(new Powerup(Math.random()*canvas.width,20+(Math.random()*(canvas.height-20)),0));
}
function makeStar() {
stars.push(new Star(canvas.width));
}
function makeTarget() {
targets.push(new Target(canvas.width, Math.random()*canvas.height));
}
function shoot(e) { //Called when player clicks
var x = player.x+74;
var y = player.y+36;
console.log("shoot! ",x," ",y);
if (!inMenu)
bullets.push(new Bullet(x, y,1));
}
var t = setInterval("update()", 1000/60); //Essentially sets framerate
//Set up stars
for (var i=0;i<starCount;i++) {
stars.push(new Star(Math.random()*canvas.width));
}
function draw() {
//Draw background
context.fillStyle = "rgba(20,20,20,0.4)";
context.fillRect(0,0,canvas.width, canvas.height);
//Draw stars
for (var i=0;i<stars.length;i++) {
context.fillStyle="white";
context.beginPath();
context.arc(stars[i].x,stars[i].y,stars[i].size,0, Math.PI*2, true);
context.closePath();
context.fill();
}
//Draw targets
for (var i=0;i<targets.length;i++) {
//colour = "darkcyan";
colour = "rgba("+(255-(i*32))+",128,"+(i*32)+", "+(1-(targets[i].hits*0.1))+")";
context.fillStyle=colour;
//context.fillRect(targets[i].x-(canvas.width/8), 0, canvas.width/4, 10);
context.fillRect(targets[i].x-75, 0, 150, 10);
}
//Draw particles
var size;
for (var i =0; i< particles.length;i++) {
//colour = "rgba("+String(particles[i].time%300)+",0,0,0.5)";
if (particles[i].type==1) {
context.drawImage(enemyImg1, particles[i].x-36, particles[i].y-24,87,37.5);
}
else if (particles[i].type==0) {
if (gametime % 6 == 0 || gametime % 6 == 1 || gametime % 6 == 2)
context.drawImage(enemyImg2, particles[i].x-36, particles[i].y-24,94,48);
else
context.drawImage(enemyImg2b, particles[i].x-36, particles[i].y-24,94,48);
}
else if (particles[i].type==2) {
if (gametime % 6 == 0 || gametime % 6 == 1 || gametime % 6 == 2)
context.drawImage(enemyImg3, particles[i].x-36, particles[i].y-24,193,68);
else
context.drawImage(enemyImg3b, particles[i].x-36, particles[i].y-24,193,68);
if (particles[i].hp > 50)
context.fillStyle="lawngreen";
else if (particles[i].hp > 25)
context.fillStyle="orange";
else
context.fillStyle="red";
context.fillRect(particles[i].x+40,particles[i].y - 30,particles[i].hp/2,4);
}
else if (particles[i].type==3) {
context.drawImage(enemyBulletImg, particles[i].x, particles[i].y,50,30);
}
//colour = "Darkorange";
//context.fillStyle=colour;
//context.beginPath();
//context.arc(particles[i].x,particles[i].y, 24, 0, Math.PI*2, true); //size should change over time
//context.closePath();
//context.fill();
}
//Draw player
if (energy > 500)
context.fillStyle="lawngreen";
else if (energy > 250)
context.fillStyle="orange";
else
context.fillStyle="red";
if (gametime % 6 == 0 || gametime % 6 == 1 || gametime % 6 == 2)
context.drawImage(playerImg, player.x, player.y-10,88,62);
else
context.drawImage(playerImg2, player.x, player.y-10,88,62);
context.fillRect(0,0,energy*0.90,4);
//Draw Bullets
for (var i=0;i<bullets.length;i++) {
context.drawImage(bulletImg, bullets[i].x, bullets[i].y-15,50,30);
//context.fillStyle="purple";
//context.beginPath();
//context.arc(bullets[i].x,bullets[i].y, 6, 0, Math.PI*2, true);
//context.closePath();
//context.fill();
}
//Draw powerups
for (var i=0; i<powerups.length;i++) {
//context.fillStyle="green";
//context.fillRect(powerups[i].x, powerups[i].y, 20,20);
if (powerups[i].type==0) {
//console.log(powerups[i].time);
context.globalAlpha = (powerups[i].time / 600);
context.drawImage(pickUp1, powerups[i].x, powerups[i].y,29,25);
context.globalAlpha = 1;
if (powerups[i].time < 301 && missedPowerups > 2) {
context.globalAlpha = 1 - (powerups[i].time / 300);
context.drawImage(pickUpCue1, powerups[i].x - 15, powerups[i].y - 20,57,8);
context.globalAlpha = 1;
console.log("pickupcue displayed");
}
}
}
//Draw portal
if (portal.on) {
context.drawImage(portalImg, portal.x, portal.y);
}
//Print scores
document.getElementById("scores").innerHTML="Score:\t"+score;
if (energy<=0) {
inMenu = true;
document.getElementById("scores").innerHTML="High score: "+score+"\tRefresh page to try again!";
}
}
function update() {
draw();
if (inMenu) {
context.fillStyle="rgba(0,0,0,0.5)"; //Darken background
context.fillRect(0,0,canvas.width, canvas.height);
context.fillStyle="rgba(128,128,128,0.8)";
context.fillRect(0,canvas.height/3,canvas.width, canvas.height/3);
context.fillStyle="black";
context.font="50px sans-serif";
context.textBaseline="middle";
if (levelNo==1) {
context.fillText("Press Space to start",canvas.width/9, canvas.height/2);
} else if (levelComplete){
context.fillText("Level " + levelNo + " complete! Rank: " + rank,canvas.width/8, canvas.height/2);
} else {
context.fillText("You died D:",canvas.width/4, canvas.height/2);
}
}
else {
gametime++; //Update time
//Make new enemies
if (gametime%spawnInterval==0) {
makeParticle();
}
//Make new powerups
if (gametime%powerupInterval==0) {
if (Math.random()<0.5) { //50% chance of powerup
makePowerup();
}
}
var remove = false;
if (score>=scoreThreshold) {
//Create portal
portal.on = true;
}
//Move the player for keyboard input
if (player.goingUp) {
player.y -= 6;
}
if (player.goingDown) {
player.y += 6;
}
if (player.goingLeft) {
player.x -= 6;
}
if (player.goingRight) {
player.x += 6;
}
//Keep player within bounds of canvas
if (player.x<0) {player.x=0;}
if (player.x>canvas.width) {player.x=canvas.width;}
if (player.y<0) {player.y=0;}
if (player.y>canvas.height) {player.y=canvas.height;}
//Stars
for (var i=0;i<stars.length;i++) {
stars[i].x -= stars[i].xvel;
if (stars[i].x < 0) {
stars.splice(i,1);
makeStar();
}
}
//Check for collisions with powerups
for (var j=0;j<powerups.length;j++) {
powerups[j].time--;
if (powerups[j].time < 1) {
powerups.splice(j, 1);
missedPowerups++;
}
if (powerups[j])
{
if (boxCollision(player.x, player.y, 45,62, powerups[j].x-30, powerups[j].y+15,20,20)) {
powerups.splice(j,1);
energy+=50;
}
}
}
//Particles
for (var i=0;i<particles.length;i++) {
remove = false;
if (particles[i].type==0) { //Move type 0 particles -> horizontal
particles[i].x += (particles[i].xvel*particleSpeed);
particles[i].y += (particles[i].yvel*particleSpeed);
}
if (particles[i].type==1) { //Move type 1 particles ->
particles[i].x += (particles[i].xvel*particleSpeed);
particles[i].y += (Math.sin(particles[i].time/10)*10*particleSpeed);
}
if (particles[i].type==2) { //Move type 2 particles ->
particles[i].x += ((particles[i].xvel*particleSpeed)/2);
particles[i].y += (Math.sin(particles[i].time/40)*5*particleSpeed);
if (particles[i].time % enemyBulletFreq == 0)
{
particles[i].bulletSpawn();
}
}
if (particles[i].type==3) { //Move type 2 particles ->
particles[i].x += (particles[i].xvel*particleSpeed);
}
particles[i].time++;
//remove offscreen particles
if (particles[i].x>canvas.width) {
remove = true;
}
else if (particles[i].x<0) { //Player has missed enemy
remove = true;
misses.particle1++;
updateStats();
}
if (particles[i].y<0) { //Not sure this bit is needed at all
for (var j=0;j<targets.length;j++) {
if (particles[i].x<targets[j].x+(canvas.width/8) && particles[i].x>targets[j].x-(canvas.width/8)) {
score++;
//energy++;
targets[j].hits++;
console.log("Hit!");
break;
}
}
remove = true;
}
//Check for collisions with bullets
for (var j=0;j<bullets.length;j++) {
if (particles[i].type == 1) {
if (boxCollision(particles[i].x+5,particles[i].y-25,82,43,bullets[j].x-3,bullets[j].y-3,12,12)) {
bullets.splice(j,1);
remove=true;
score += 100;
hits.particle1++;
bulletHits++;
updateStats();
levelKills++;
}
}
else if (particles[i].type == 0) {
if (boxCollision(particles[i].x+5,particles[i].y-20,94,48,bullets[j].x-3,bullets[j].y-3,12,12)) {
bullets.splice(j,1);
remove=true;
score += 100;
hits.particle1++;
bulletHits++;
updateStats();
levelKills++;
}
}
else if (particles[i].type == 2) {
if (boxCollision(particles[i].x+110,particles[i].y-10,193,40,bullets[j].x-3,bullets[j].y-3,12,12)) {
particles[i].hp = particles[i].hp - 25;
if (particles[i].hp < 1)
{
remove=true;
levelKills++;
}
bullets.splice(j,1);
score += 100;
hits.particle1++;
bulletHits++;
updateStats();
}
}
}
//Collision with player
if (particles[i].type == 1) {
//context.fillRect(particles[i].x-25,particles[i].y-25,82,43);
if (boxCollision(player.x+45,player.y+5,110,25,particles[i].x-25,particles[i].y-25,82,43)) {
energy-=50;
remove = true;
}
}
else if (particles[i].type == 0) {
//context.fillRect(particles[i].x-30,particles[i].y-23,94,48);
if (boxCollision(player.x+45,player.y+5,110,45,particles[i].x-30,particles[i].y-20,94,48)) {
energy-=50;
remove = true;
}
}
else if (particles[i].type == 2) {
//context.fillRect(particles[i].x-30,particles[i].y-20,185,35);
if (boxCollision(player.x+45,player.y+5,170,25,particles[i].x-15,particles[i].y-30,193,68)) {
energy-=50;
remove = true;
}
}
else if (particles[i].type == 3) {
//context.fillRect(particles[i].x-30,particles[i].y-20,185,35);
if (boxCollision(player.x+10,player.y-15,88,50,particles[i].x-3,particles[i].y-3,12,12)) {
energy-=50;
remove = true;
}
}
if (remove) {particles.splice(i,1); energy--;}
}
//Bullets
for (var i=0;i<bullets.length;i++) {
remove = false;
if (bullets[i].type==1) {
bullets[i].x += 20;
}
if (bullets[i].x>canvas.width) {
remove = true;
bulletMisses++;
updateStats();
}
if (remove) {
bullets.splice(i,1);
}
}
//Collisions with portal
if (boxCollision(player.x+45,player.y+5,88,50,portal.x+5,portal.y+5,100,150) && portal.on) {
levelComplete = true;
inMenu = true;
updateLevelVariables();
}
//if (energy<0) {running=false;}
}
}
function getPosition(e) {
var x;
var y;
if (e.pageX || e.pageY) { //pageX and pageY are relative to the window
x = e.pageX;
y = e.pageY;
} else { //sort out offsets and stuff
x = e.clientX+document.body.scrollLeft + document.documentElement.scrollLeft;
y = e.clientY+document.body.scrollTop + document.documentElement.scrollTop;
}
x -=canvas.offsetLeft;
y -= canvas.offsetTop;
player.x=x-44-(document.body.clientWidth/2)+450;
player.y=y-30;
return [x,y];
}
function boxCollision(x,y,w,h,x2,y2,w2,h2 ) { //Top left, width, height corners of boxes
if ((x < x2+w) && (x+h>x2) && (y<y2+h2) && (y+h>y2)) {
return true;
}
else {return false;}
}
function updateStats() {
document.getElementById("stats").innerHTML="Level: "+levelNo+" <br/>Enemy speed: "+particleSpeed+"<br/>Hits: "+hits.particle1+"</br> Missed enemies: "+misses.particle1+"</br>Overall Accuracy: "+Math.floor((bulletHits/(bulletHits+bulletMisses))*100)
+"</br>Score Per Minute: "+Math.floor(score / (gametime/3600));
}
function updateLevelVariables() {
var scorePerMinute = score / (gametime);
var accuracy = Math.floor((bulletHits/(bulletHits+bulletMisses))*100);
if (accuracy < 10)
accuracyLevel = 0;
else if (accuracy < 30)
accuracyLevel = 1;
else
accuracyLevel = 2;
console.log("accuracy " + accuracy + " " + accuracyLevel);
if (scorePerMinute < 5000*(0.5+levelNo/2))
SPMLevel = 0;
else if (scorePerMinute < 6750*(0.5+levelNo/2))
SPMLevel = 1;
else
SPMLevel = 2;
rank = "C"
if (accuracyLevel == 0 && SPMLevel == 0)
{
particleSpeed *= 0.8;
enemyHPMultiplier *= 0.8;
spawnInterval += 10;
powerupInterval -= 10;
enemyBulletFreq *= 0.8;
score -= 500;
rank = "D";
}
else if (accuracyLevel == 2 && SPMLevel == 0)
{
particleSpeed *= 1.2;
enemyHPMultiplier *= 1.2;
spawnInterval -= 10;
powerupInterval += 12;
enemyBulletFreq *= 1.2;
score += 3000;