-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
940 lines (847 loc) · 25.6 KB
/
index.php
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
<!DOCTYPE html>
<html>
<head>
<title>X-Quest</title>
<script src='jquery.js' type='text/javascript'></script>
<script type="text/javascript">
function setCharAt(str,index,chr) {
if(index > str.length-1 || index < 0) return str;
return str.substr(0,index) + chr + str.substr(index+1);
}
function getRandomInt (min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
function replaceAll(find, replace, str) {
return str.replace(new RegExp(find, 'g'), replace);
}
function Contains(x, min, max) {
return x >= min && x <= max;
}
/* phpjs number_format */
function Format(number, decimals, dec_point, thousands_sep) {
// discuss at: http://phpjs.org/functions/number_format/
number = (number + '')
.replace(/[^0-9+\-Ee.]/g, '');
var n = !isFinite(+number) ? 0 : +number,
prec = !isFinite(+decimals) ? 0 : Math.abs(decimals),
sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep,
dec = (typeof dec_point === 'undefined') ? '.' : dec_point,
s = '',
toFixedFix = function(n, prec) {
var k = Math.pow(10, prec);
return '' + (Math.round(n * k) / k)
.toFixed(prec);
};
// Fix for IE parseFloat(0.55).toFixed(0) = 0;
s = (prec ? toFixedFix(n, prec) : '' + Math.round(n)).split('.');
if (s[0].length > 3) {
s[0] = s[0].replace(/\B(?=(?:\d{3})+(?!\d))/g, sep);
}
if ((s[1] || '').length < prec) {
s[1] = s[1] || '';
s[1] += new Array(prec - s[1].length + 1).join('0');
}
return s.join(dec);
}
Game = {};
Game.Updates = [
[ 'v0.9',
'September 12th 2014',
'Added a changelog.',
'More layout changes.',
'Pellets now give less score from leveling up.',
'Time played is now tracked.',
'Added sound effects.'
],
[ 'v0.81',
'June 16th 2014',
'Pausing during a game over will cause it to revive you.',
'You couldn't shoot ship bullets.'
],
[ 'v0.8',
'June 13th 2014',
'Ships fly on and off the screen instead of disappearing',
'Added tabbed windows',
'Statistics window now tracks many more types of statistics',
'New save file storage system (All previous saves had to be wiped)'
]
];
Game.Active = false;
Game.CHEAT = false;
Game.Version = 0.9;
Game.BaseSpeed = 120; //milliseconds
Game.PositivePhrases = [
"Good Luck!", "Having fun yet?", "Have fun!", "Kill 'em", "You can do it!",
"Run run run", "Don't die", "Stay on the road", "Stay positive", "I'm gonna do an internet!",
"The time has come", "Your quest begins", "pow pow kachow", "",
"You are an X", "Indubitably"
];
/* Called each time a new game begins; sets game variables */
Game.Start = function() {
if (Game.Active == true)
return false;
Game.Active = true;
Game.Paused = false;
Game.Invincible = 0;
Game.Distortion = 0;
Game.Text = [];
Game.Level = 1;
Game.map = [];
Game.LineLength = [];
Game.LineReset = [];
Game.LineEntered = [];
Game.RoadTile = '<span class="c'+Game.Level+'">|</span>';
Game.HighScore = false;
Game.Stats = {
Score: 0,
Lines: 0,
ShipsDestroyed: 0,
ShotsDestroyed: 0,
Powerups: 0,
Moves: 0,
ShotsFired: 0,
Time: 0,
Deaths: {
Shot: 0,
Wall: 0,
Normal: 0
}
};
/* "Destroying" = Create empty objects */
Game.DestroyBullet('player');
Game.DestroySpaceship();
/* Player spawns in the middle location */
var mid = Math.floor((Game.LineSize - 1) / 2);
Game.PlayerX = mid;
for(var i = 0; i < Game.LineSize; i++) {
// if the location is one of the 3 middle columns then make a line there
if (Contains(i, mid-1, mid+1))
x = 1;
else x = 0;
Game.LineLength.push(x*25);
Game.LineReset.push(x);
Game.LineEntered.push(x);
}
$('#linesize').css('display','none');
$('#level').html(Game.Level);
/* Generate the first 20 lines */
for (y = 0; y <= 20; y++) {
var Line = Game.GenerateLine();
Game.map[y] = Line;
}
/* Add a positive phrase in the text field */
Game.AddText( Game.PositivePhrases[getRandomInt(0, (Game.PositivePhrases.length-1))] );
/* Lets get this party started */
Game.CreateInterval(Game.BaseSpeed);
console.log('Game Started');
};
/* Generates the line (as text) */
Game.GenerateLine = function () {
var Line = Game.BaseLine;
/* Randomly start a new line every once in a while (Higher line size = less new lines) */
if (getRandomInt(1, 30+Game.LineSize) == 1) {
var x = getRandomInt(0,Game.LineSize-1);
Game.LineReset[x] = 1;
Game.LineLength[x] = getRandomInt(6, 22-Game.Level);
}
/* Generate tiles and manage lines */
for(i=0; i<Game.LineSize; i++) {
if (Game.LineLength[i] != 0) {
var road = "`";
if ((Game.Stats.Lines+1) % 250 == 0 && Game.Level != 9) {
road = "L";
} else if (getRandomInt(1, 1100) == 1) {
road = "I";
} else if (getRandomInt(1, 300) == 1) {
road = "P";
} else if (getRandomInt(1, 900) === 1) {
road = "D";
}
Line = setCharAt(Line, i, road);
/* Vertical line handler */
Game.LineLength[i] -= 1;
if (Game.LineLength[i] < getRandomInt(2, 6 - Math.floor(Game.Level/4)) && Game.LineReset[i] != 0) {
Game.LineReset[i] = 0;
if ((getRandomInt(1, 2) == 1 && i != Game.LineSize - 1) || i == 0) {
Game.LineReset[i+1] = 1;
Game.LineLength[i+1] = getRandomInt(6, 22 - Game.Level);
} else {
Game.LineReset[i-1] = 1;
Game.LineLength[i-1] = getRandomInt(6, 20 - Game.Level);
}
}
}
}
return Line;
};
/* Adds a new line to the map array; called each tick */
Game.AddLine = function() {
var newMap = [];
for (y = 1; y <= 20; y++) {
newMap[y-1] = Game.map[y];
}
Game.map = newMap;
Game.map[20] = Game.GenerateLine();
Game.Stats.Lines+=1;
return false;
};
/* Render the game map from the Game.map array of lines */
Game.DisplayMap = function(Text) {
var Map = "";
for (y = 20; y >= 0; y--) {
var Line = Game.map[y];
if (y == Game.Bullet.y)
Line = setCharAt(Line, Game.Bullet.x, "^");
if (y == Game.Spaceship.Bullet.y)
Line = setCharAt(Line, Game.Spaceship.Bullet.x, "v");
if (y == 2)
Line = setCharAt(Line, Game.PlayerX, "X");
if (y == Game.Spaceship.y) {
Line = setCharAt(Line, Game.Spaceship.x, Game.Spaceship.display[0]);
Line = setCharAt(Line, Game.Spaceship.x+1, Game.Spaceship.display[1]);
Line = setCharAt(Line, Game.Spaceship.x+2, Game.Spaceship.display[2]);
}
if (Text != false) {
for (i=0;i<Text.length;i++) {
if (Text[i].y == y)
Line = Text[i].text.substr(0,Game.LineSize);
}
}
Map += "#"+replaceAll('`', Game.RoadTile, replaceAll('@', ' ', Line))+"#<br>";
}
return Map;
};
/* Main Game loop, main function is to process tiles and update map */
Game.CreateInterval = function(speed) {
/* If a game loop exists already; kill it */
clearInterval(Game.Interval);
Game.CurrentSpeed = speed/1000;
Game.Interval = setInterval(function() {
Game.AddLine();
$("#GameWindow").html(Game.DisplayMap(false));
Game.Stats.Time += Game.CurrentSpeed;
/* Create lines by the player for scoring purposes */
for(x=0; x<Game.LineSize; x++) {
if (Game.map[3].charAt(x) == '@')
Game.LineEntered[x] = 0;
else if (Game.LineEntered[x] == 0)
Game.LineEntered[x] = 1;
}
var Tile = Game.map[2].split('')[Game.PlayerX];
switch (Tile) {
case '|': break;
case 'P':
Game.PlaySound('bonus.wav');
Game.Stats.Score += (Game.Level*2)+2;
Game.Stats.Powerups += 1;
Game.AddText("+"+((Game.Level*2)+2)+" Score");
Game.map[2] = setCharAt(Game.map[2], Game.PlayerX, "`");
break;
case 'I':
Game.Invincible = 50;
Game.Stats.Powerups += 1;
Game.map[2] = setCharAt(Game.map[2], Game.PlayerX, "`");
Game.PlaySound('power.ogg');
break;
case 'L':
Game.Level += 1;
Game.RoadTile = '<span class="c'+Game.Level+'">|</span>';
Game.AddText("<b>LEVEL "+Game.Level+"</b>");
$('#level').html(Game.Level);
Game.map[2] = setCharAt(Game.map[2], Game.PlayerX, "`");
Game.PlaySound('level.wav');
break;
case 'D':
Game.Distortion = 25;
Game.Stats.Powerups += 1;
Game.CreateInterval(Game.BaseSpeed*2);
Game.map[2] = setCharAt(Game.map[2], Game.PlayerX, "`");
Game.PlaySound('power.ogg');
break;
case '@':
if (Game.Invincible == 0) {
Game.Over('Normal');
}
break;
}
/* Bullet handling */
if (Game.Bullet.exists == true) {
Game.Bullet.y += 1;
if (Game.Bullet.y == 21)
Game.DestroyBullet('player');
if (Game.Spaceship.exists == true &&
Game.Bullet.y == Game.Spaceship.y &&
Contains(Game.Bullet.x, Game.Spaceship.x, Game.Spaceship.x+2)) {
Game.DestroyBullet('player');
Game.Stats.Score += 10;
Game.Stats.ShipsDestroyed += 1;
Game.AddText("Hit! +10 Score");
Game.DestroySpaceship();
Game.PlaySound('explosion.ogg');
}
if ((Game.Spaceship.Bullet.y == Game.Bullet.y || Game.Spaceship.Bullet.y+1 ==Game.Bullet.y) &&
Game.Bullet.x == Game.Spaceship.Bullet.x && Game.Bullet.y != -1) {
Game.DestroyBullet('player');
Game.DestroyBullet('Spaceship');
Game.Stats.ShotsDestroyed += 1;
}
}
/* Spaceship handling */
if (Game.Spaceship.exists == true) {
if ((Game.Stats.Lines % 30) == 0) Game.Spaceship.y -= 1;
/* Spaceship movement */
if (Game.Spaceship.move == false) {
Game.Spaceship.move = true;
} else {
if (Game.Spaceship.flyaway == false) {
if(Game.Spaceship.x >= getRandomInt(Math.floor(Game.LineSize*.7), Game.LineSize-3)
&& Game.Spaceship.direction == 1) {
Game.Spaceship.direction *= -1;
}
if(Game.Spaceship.x <= getRandomInt(1, Math.floor(Game.LineSize*.3)) && Game.Spaceship.direction == -1) {
Game.Spaceship.direction *= -1;
}
}
Game.Spaceship.move = false;
Game.Spaceship.x += Game.Spaceship.direction;
}
/* Spaceship bullet handling */
if (Game.Spaceship.Bullet.exists == false && getRandomInt(1, 5) == 1 && Game.Spaceship.flyaway == false) {
Game.Spaceship.Bullet = {
exists: true,
x: Game.Spaceship.x,
y: Game.Spaceship.y
}
} else {
Game.Spaceship.Bullet.y -= 1;
if (Game.Spaceship.Bullet.y == 0) Game.DestroyBullet('Spaceship');
if (Game.Spaceship.Bullet.y == 1 && Game.Spaceship.Bullet.x == Game.PlayerX && Game.Invincible == 0) {
Game.DestroyBullet('Spaceship');
Game.Over('Spaceship');
}
}
/* After 150 lines after the spaceship has spawned make it fly away (now) */
if ((Game.Spaceship.start+150 ) < Game.Stats.Lines) {
Game.Spaceship.flyaway = true;
}
/* Destroy spaceship after it has flown off the screen */
if (Game.Spaceship.x == -3 || Game.Spaceship.x == (Game.LineSize+3)) {
Game.DestroySpaceship();
}
} else {
/* Randomly generate spaceships every 100 lines at 1/4 chance */
if ((1+Game.Stats.Lines) % 100 == 0 &&
(getRandomInt(1, 4) == 1 || Game.Stats.Lines < 150)) {
Game.CreateSpaceship();
}
}
$('#score').html(Format(Game.Stats.Score));
$('#lines').html(Format(Game.Stats.Lines));
if (Game.isNewRecord() && Game.HighScore == false) {
Game.HighScore = true;
Game.AddText("<b>High Score!</b>");
}
if (Game.Invincible != 0) {
Game.Invincible -= 1;
$('#GameWindow').append('<br><b>INVINCIBILITY:</b> '+Game.Invincible);
}
if (Game.Distortion != 0) {
Game.Distortion -= 1;
if (Game.Distortion == 0) {
Game.CreateInterval(Game.BaseSpeed);
}
$('#GameWindow').append('<br><b>Distortion:</b> '+Game.Distortion);
}
Game.ProcessText();
}, speed);
};
Game.AddText = function ( txt ) {
Game.Text.unshift( { ticks: 20, text: txt } );
};
Game.ProcessText = function () {
for (i=0;i<Game.Text.length;i++) {
if (Game.Text[i] != []) {
Game.Text[i].ticks -= 1;
if (Game.Text[i].ticks == 0) {
Game.Text.pop();
} else {
$('#GameWindow').append("<br>"+Game.Text[i].text);
}
}
}
};
Game.Move = function (direction){
if (Game.Active == true && Game.Paused == false) {
switch(direction) {
case 'right':
Game.PlayerX += 1;
if (Game.PlayerX > Game.LineSize-1)
Game.Over('Wall');
break;
case 'left':
Game.PlayerX -= 1;
if (Game.PlayerX < 0)
Game.Over('Wall');
break;
}
if (Game.LineEntered[Game.PlayerX] == 1) {
Game.LineEntered[Game.PlayerX] = 2;
Game.Stats.Score += 1;
Game.PlaySound('score.ogg');
} else Game.PlaySound('noscore.ogg');
Game.Stats.Moves += 1;
}
};
Game.DestroyBullet = function(type) {
var bullet = {
exists:false,
x:0,
y:-1
};
switch (type) {
case 'player': Game.Bullet = bullet; break;
case 'Spaceship': Game.Spaceship.Bullet = bullet; break;
}
};
Game.FireBullet = function(type) {
if (Game.Paused == false && Game.Active == true) {
if (type == 'player' && Game.Bullet.exists == false) {
Game.PlaySound('shoot.ogg');
Game.Stats.ShotsFired += 1;
Game.Bullet = {
exists: true,
x: Game.PlayerX,
y: 2
};
} else if (type == 'Spaceship' && Game.Spaceship.Bullet.exists == false) {
Game.PlaySound('shoot.ogg');
Game.Bullet = {
exists: true,
x: Game.Spaceship.x,
y: Game.Spaceship.y
};
}
}
};
Game.CreateSpaceship = function() {
if (getRandomInt(0, 1) == 0) {
var x = Game.LineSize + 2;
var dir = -1;
} else {
var x = -2;
var dir = 1;
}
if (getRandomInt(0, 20) == 0) {
var disp = "^_~";
} else var disp = "<_>";
Game.Spaceship = {
exists: true,
move:false,
flyaway:false,
direction: dir,
start:Game.Stats.Lines,
display:disp,
Bullet:{
exists:false,
x:0,
y:-1
},
x: x,
y: getRandomInt(16,20)
}
};
Game.DestroySpaceship = function() {
Game.Spaceship = {
exists:false,move:false,flyaway:false,start:0,direction:0,x:0,y:-1,Bullet:{exists:false,x:0,y:-1}
};
}
Game.UpdateSpeed = function (speed) {
Game.BaseSpeed = speed;
Game.CHEAT = true;
console.log("Updated");
};
Game.UpdateSound = function (sound) {
Game.SaveFile.Sound = sound;
Game.Save();
console.log("Updated Sound");
};
Game.UpdateSize = function(size) {
Game.LineSize = parseInt(size);
var l = "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@";
Game.BaseLine = l.substr(0,Game.LineSize);
if (Game.LineSize == 27) {
$('#GameWindow').css('width', '250px');
} else {
$('#GameWindow').css('width', '200px');
}
$('#GameWindow').html("Press spacebar to begin.");
return false;
};
/* Toggles pause on and off */
Game.TogglePause = function () {
if (Game.Active == true) {
if (Game.Paused == false) {
Game.Paused = true;
clearInterval(Game.Interval);
$("#GameWindow").html(Game.DisplayMap([{y:10,text:"@PAUSED@@@@@@@@@@@@@@@@@@@@@@@@@"}]));
} else {
Game.Paused = false;
Game.CreateInterval(Game.BaseSpeed);
}
}
};
Game.isNewRecord = function () {
if (Game.SaveFile.Record.Score < Game.Stats.Score)
return true;
else
return false;
}
Game.Over = function(DeathType) {
Game.PlaySound('gameover.ogg');
$('#linesize').css('display','inline');
Game.Active = false;
clearInterval(Game.Interval);
var Text = [
{y:12, text:"@@@Game Over@@@@@@@@@@@@@@@@@@@@@"},
{y:11, text:"@Score: "+Format(Game.Stats.Score)+"@@@@@@@@@@@@@@@@@@@@@@@@"},
{y:10, text:"@Lines: "+Format(Game.Stats.Lines)+"@@@@@@@@@@@@@@@@@@@@@@@@"},
{y:9, text:"@Level: "+Format(Game.Level)+"@@@@@@@@@@@@@@@@@@@@@@@@"}
];
if (Game.isNewRecord()) Text.push({y:8, text:"@@High Score!@@@@@@@@@@@@@@@@@@@@@@@@@@@"});
$("#GameWindow").html(Game.DisplayMap(Text));
if(Game.CHEAT == false) {
switch(DeathType) {
case 'Spaceship': Game.SaveFile.Totals.Deaths.Shot++; break;
case 'Wall': Game.SaveFile.Totals.Deaths.Wall++; break;
case 'Normal': Game.SaveFile.Totals.Deaths.Normal++; break;
}
Game.SaveFile.Totals.GamesPlayed += 1;
Game.SaveFile.Totals.Score += Game.Stats.Score;
Game.SaveFile.Totals.Lines += Game.Stats.Lines;
Game.SaveFile.Totals.ShipsDestroyed += Game.Stats.ShipsDestroyed;
Game.SaveFile.Totals.Powerups += Game.Stats.Powerups;
Game.SaveFile.Totals.Moves += Game.Stats.Moves;
Game.SaveFile.Totals.Time += Game.Stats.Time;
Game.SaveFile.Totals.ShotsFired += Game.Stats.ShotsFired;
Game.SaveFile.Totals.ShotsDestroyed += Game.Stats.ShotsDestroyed;
if (Game.isNewRecord()) {
Game.SaveFile.Record = {
Score: Game.Stats.Score,
Lines: Game.Stats.Lines
}
$('#high-score').html(Format(Game.SaveFile.Record.Score));
$('#high-lines').html(Format(Game.SaveFile.Record.Lines));
}
$('#total-score').html(Format(Game.SaveFile.Totals.Score));
$('#total-lines').html(Format(Game.SaveFile.Totals.Lines));
Game.Save();
Game.UpdateStatistics();
}
};
Game.CreateNewSaveFile = function() {
if (localStorage["xquest_HighScore"] !== undefined) {
var Record = {Score: parseInt(localStorage["xquest_HighScore"]),Lines: parseInt(localStorage["xquest_HighLines"])};
} else {
var Record = { Score:0, Lines: 0};
}
Game.SaveFile = {
Version: Game.Version,
Totals: {
GamesPlayed: 0,
Score: 0,
Lines: 0,
ShipsDestroyed: 0,
Powerups: 0,
Moves: 0,
Time: 0,
ShotsFired: 0,
ShotsDestroyed: 0,
Deaths: {
Shot: 0,
Wall: 0,
Normal: 0
}
},
Record: Record,
Achievements: {
}
};
};
/* Save Game.SaveFile into localStorage */
Game.Save = function() {
try {
localStorage.setItem("XQuest", JSON.stringify(Game.SaveFile));
}
catch(e) {
console.log(e);
alert("Save Failed!");
}
}
/* Load the save file from localStorage */
Game.Load = function() {
var SaveFile = 'Invalid';
try {
SaveFile = localStorage.getItem("XQuest");
}
catch(err) {
if (err instanceof SecurityError)
alert("Browser security settings blocked access to local storage.");
else
alert("Cannot access localStorage - browser may not support localStorage, or storage may be corrupt");
return false;
}
/* If a save file does not exist, create a new one */
if (!SaveFile) {
Game.CreateNewSaveFile();
Game.Save();
Game.Load();
return false;
}
if (SaveFile == 'Invalid') {
alert("Save file not loaded.");
return false;
}
Game.SaveFile = JSON.parse(SaveFile);
if (Game.SaveFile.Version < 0.9) {
Game.SaveFile.Totals.Time = Game.SaveFile.Totals.Lines * 0.12;
Game.SaveFile.Sound = 'y';
}
console.log("Game Loaded");
};
Game.PlaySound = function(sound) {
document.getElementById("Sound").src = "Sound/"+sound;
if (Game.SaveFile.Sound == 'y') {
document.getElementById("Sound").play();
}
return true;
}
Game.UpdateStatistics = function() {
var Time = Game.SaveFile.Totals.Time;
if(Time < 600) {
Time = Format(Time, 1) + " Seconds";
} else if (Time < 36000) {
Time = Format(Time/60, 1) + " Minutes";
} else {
Time = Format(Time / 3600, 1) + " Hours";
}
$('#Statistics').html(
'<b>Games Played:</b> '+Format(Game.SaveFile.Totals.GamesPlayed)+'<br>'+
'<b>Score:</b> '+Format(Game.SaveFile.Totals.Score)+'<br>'+
'<b>Lines:</b> '+Format(Game.SaveFile.Totals.Lines)+'<br>'+
'<b>Times Moved:</b> '+Format(Game.SaveFile.Totals.Moves)+'<br>'+
'<b>Shots Fired:</b> '+Format(Game.SaveFile.Totals.ShotsFired)+'<br>'+
'<b>Shots Destroyed:</b> '+Format(Game.SaveFile.Totals.ShotsDestroyed)+'<br>'+
'<b>Ships Destroyed:</b> '+Format(Game.SaveFile.Totals.ShipsDestroyed)+'<br>'+
'<b>Powerups Collected:</b> '+Format(Game.SaveFile.Totals.Powerups)+'<br>'+
'<b>Deaths To The Abyss:</b> '+Format(Game.SaveFile.Totals.Deaths.Normal)+'<br>'+
'<b>Deaths To The Ship:</b> '+Format(Game.SaveFile.Totals.Deaths.Shot)+'<br>'+
'<b>Deaths To The Wall:</b> '+Format(Game.SaveFile.Totals.Deaths.Wall)+'<br>'+
'<b>Time Played:</b> '+Time+'<br>'
);
};
Game.CreateChangelog = function() {
var Changelog = '';
for(q=0;q<Game.Updates.length;q++) {
Changelog += 'X-Quest '+Game.Updates[q][0]+' : '+Game.Updates[q][1]+'<br>'
for(w=2;w<Game.Updates[q].length;w++) {
Changelog += ' • '+Game.Updates[q][w]+'<br>';
}
Changelog += '<br>';
}
return Changelog;
};
$(document).ready(function() {
Game.Load();
/* Add the total score and lines from localStorage */
$('#high-score').html(Format(Game.SaveFile.Record.Score));
$('#high-lines').html(Format(Game.SaveFile.Record.Lines));
$('#total-score').html(Format(Game.SaveFile.Totals.Score));
$('#total-lines').html(Format(Game.SaveFile.Totals.Lines));
/* Default to medium size */
Game.UpdateSize(15);
Game.DestroyBullet('player');
Game.DestroySpaceship();
Game.map = [];
for(i=0;i<=20;i++) {
Game.map[i] = '@@@@@@@@@@@@@@@';
}
$('#GameWindow').html(Game.DisplayMap([
{y:11,text:"@@Press@Space@@"},
{y:10,text:"@@@to@start.@@@"},
{y:2 ,text:"@@@@@@@@@@@@@@@"}
]));
$('tab').click(function (e) {
ToggleTab($(this).attr('id'));
});
Game.UpdateStatistics();
$("#Changelog").html(Game.CreateChangelog());
});
$(document).keydown(function(e) {
var code = e.keyCode;
switch (code) {
case 82: Game.Active = false; Game.Start(); break;
case 37: case 65: Game.Move('left'); break; //Right arrow or "a"
case 39: case 68: Game.Move('right'); break; //Left arrow or "d"
case 38: case 87: Game.FireBullet('player'); break; //Up arrow or "w"
case 32:
if (Game.Active == false) {
Game.Start();//Spacebar to start
} else {
Game.TogglePause(); //Spacebar to pause
}
break;
default: return true;
}
return false;
});
function ToggleTab(tab){
for (i=0;i<=5;i++) {
$('[tab='+i+']').css('display', 'none');
$('[id='+i+']').attr('class', 'x');
}
$('[tab='+tab+']').css('display', 'inline-block');
$('[id='+tab+']').attr('class', 'selected');
}
</script>
</head>
<body>
<style>
.c1,.c2,.c3,.c4,.c5,.c6,.c7,.c8,.c9,.header{font-weight:bold;}
.c1 { color: blue; }
.c2 { color: #FF0000; }
.c3 { color: green; }
.c4 { color: orange; }
.c5 { color: magenta; }
.c6 { color: purple; }
.c7 { color: brown; }
.c8 { color: black; }
.c9 { color: #0000FF; }
body{
font-family:monospace;
font-size:1em;
margin:0px;
}
.Window {
float:left;
min-height:400px;
}
.header {
font-size:20px;
}
.TabContainer tab {
float:left;
width:147px;
text-align:center;
margin-bottom:10px;
font-size:16px;
padding-top:4px;
padding-bottom:4px;
color:black;
cursor:pointer;
font-weight:bold;
-webkit-touch-callout: none;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
border-right:1px solid black;
border-bottom:1px solid black;
}
.TabContainer .selected {
color:blue;
}
.TabWindow {
display:none;
}
#GameInfo {
width:750px;
text-align:left;
}
</style>
<audio id="Sound" src="" style="display:none;"></audio>
<div style="width:1200px;">
<div class="TabContainer">
<tab id="0" style="width:398px;"> X-Quest v0.9 </tab>
<tab id="1" class="selected">Instructions</tab>
<tab id="2">Options</tab>
<tab id="3">Achievements</tab>
<tab id="4">Statistics</tab>
<tab id="5">Changelog</tab>
</div>
</div>
<div id="container" style="width:1200px;text-align:center;">
<div id="outer_container">
<div class="Window" style="width:200px;">
<b>Current Game:</b><br>
<div style="width:200px;">
<div style="width:120px;text-align:right;float:left">Score: </div>
<div style="width:80px;text-align:left;float:left" id="score">0</div>
</div>
<div style="width:200px;">
<div style="width:120px;text-align:right;float:left">Lines Passed: </div>
<div style="width:80px;text-align:left;float:left" id="lines">0</div>
</div>
<div style="width:200px;">
<div style="width:120px;text-align:right;float:left">Level: </div>
<div style="width:80px;text-align:left;float:left" id="level">0</div>
</div><br><br><br><br>
<b>High Score:</b><br>
<div style="width:200px;">
<div style="width:120px;text-align:right;float:left">Score: </div>
<div style="width:80px;text-align:left;float:left" id="high-score">0</div>
</div>
<div style="width:200px;">
<div style="width:120px;text-align:right;float:left">Lines Passed: </div>
<div style="width:80px;text-align:left;float:left" id="high-lines">0</div>
</div><br><br><br><br>
</div>
<div class="Window" style="margin-top:2px;" id="GameWindow">
X-Quest required JavaScript to be enabled.
</div>
<div class="Window" id="GameInfo">
<div class="TabWindow" tab="0">
Easter Egg?
</div>
<div class="TabWindow" style="display:inline;" tab="1">
You are a X that goes on a quest.<br>
The quest is to stay on the road. (Don't get shot either)<br>
Score is earned by moving to a new line.<br>
Each level makes the lines progressively shorter.<br><br>
Controls:<br>
Left and Right (or A and D) to move your character left and right.<br>
Up (or W) to shoot the spaceships (or spaceship bullets).<br>
Spacebar to start/reset the game.<br><br>
Special Tiles:<br>
L (Level): Land on this tile to level up<br>
I (Invulnerable): Invincibility for 50 lines<br>
P (Pellet): Score bonus.<br>
D (Distortion): Halves game speed for 25 turns<br>
<br>X-Quest v0.9 by <a href="http://tpkrpg.net/">B0sh</a> © 2014
</div>
<div class="TabWindow" tab="2">
<b>Sound:</b> <select id="sound" onchange="Game.UpdateSound($('#sound').val());">
<option value="y"> Enabled </option>
<option value="n"> Disabled </option>
</select><br><br>
<b>Board Size:</b> <select id="linesize" onchange="Game.UpdateSize($('#linesize').val());">
<option value="9"> Small </option>
<option value="15" selected> Medium </option>
<option value="21"> Long </option>
<option value="27"> Extra Long </option>
</select><br><br>
For each of the below options, changing them will result in statistics (ex: high score) not being recorded. Refreshing will reset these.<br><br>
<b>Game Speed:</b> <input type="text" value="120" size="2" id="speed" onchange="Game.UpdateSpeed($('#speed').val());" /> (ms)
</div>
<div class="TabWindow" tab="3">
Achievements have not been coded yet.
</div>
<div class="TabWindow" id="Statistics" tab="4">
</div>
<div class="TabWindow" tab="5" id="Changelog"style="overflow:scroll;overflow-x:hidden;height:350px;width:740px;">
</div>
</div>
</div>
</div>
</body>
</html>