-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmain.js
1185 lines (1124 loc) · 31.8 KB
/
main.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
var g = {
interval:1000/30,
lastTime:0,
highscore: {
0:0,
1:0,
2:0
},
title: true,
music: false,
images: {
},
story: [
"It's dark. Warnings are flashing everywhere.",
"I'm on some kind of... damaged space station?",
"The reactor's working, but everything's offline.",
"I need to connect the oxygen, my suit won't last long.",
"Then let's see if the escape systems are working..."
],
tooltips: {
main: "Main Power. Connect to systems to power them.",
aux: "Aux Power. Just needs a little kick to get started.",
o2: "Oxygen. Connect hexes to this to make them breathable.",
fuel: "Fuel. Connect to the Escape system to fuel it.",
escape: "Escape Pod. Add fuel then power to operate.",
fabber: "Fabrication Systems. Pick up new conduits from here.",
spare: "Connects empty faces of this hex with a conduit.",
spareNo: "Cannot place conduit in this hex."
},
options: {
invertControls: false,
difficulty: 0,
colorblind: false
},
menuButtons: {
invertControls: new MenuButton(235,125,330,30,function(){ return true; }, function(){ g.options.invertControls = !g.options.invertControls }),
diffNormal: new MenuButton(235,345,330,30,function(){ return g.title || g.showStory; }, function(){ if (g.title || g.showStory) g.options.difficulty = 0 }),
diffHard: new MenuButton(235,385,330,30,function(){ return g.title || g.showStory; }, function(){ if (g.title || g.showStory) g.options.difficulty = 1 }),
diffIronman: new MenuButton(235,425,330,30,function(){ return g.title || g.showStory; }, function(){ if (g.title || g.showStory) g.options.difficulty = 2 }),
colorblind: new MenuButton(220,495,345,30,function(){ return true; }, function(){ g.options.colorblind = !g.options.colorblind })
}
};
function MenuButton(x,y,width,height,selectableWhile,calls){
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.selectableWhile = selectableWhile;
this.calls = calls;
}
function setup(){
document.getElementById("canvasContainer").innerHTML = "";
g.canvas = document.createElement('canvas');
g.canvas.id = "canvas";
g.canvas.width = 800;
g.canvas.height = 600;
g.canvas.font = "24px monospace";
document.getElementById("canvasContainer").appendChild(g.canvas);
g.ctx = g.canvas.getContext("2d");
document.getElementById('canvas').onmousemove = function(e){ handleMouseMove(e); };
document.getElementById('canvas').onmousedown = function(e){ handleMousedown(e); };
document.getElementById('canvas').oncontextmenu = function(e){ return false }; //allow right clicking to not bring up the context menu
//document.onkeypress = function(e){ handleKeyPress(e); };
g.ctx.strokeStyle = "white";
load();
initialiseData();
gameLoop();
}
function load(){
try {
var savegame = localStorage.getItem("offline");
if (savegame) {
if (!isNaN(parseInt(savegame))){ //old old saves
g.highscore[0] = parseInt(savegame)
g.highscore[1] = 0;
g.highscore[2] = 0;
} else {
var s = JSON.parse(savegame);
if (!isNaN(s.highscore)) { //old saves
g.highscore[0] = s.highscore;
g.highscore[1] = 0;
g.highscore[2] = 0;
} else if (s.highscore){
g.highscore = s.highscore;
}
if (s.options.invertControls) g.options.invertControls = true;
if (s.options.difficulty) g.options.invertControls = s.options.difficulty;
if (s.options.colorblind) g.options.colorblind = true;
}
}
} catch(err){};
}
///////////////////////////////////////////////////////////////////////////
function initialiseData(){
g.showStory = false;
g.storyClicks = 0;
g.stars = [];
for (var i=0; i<120; i++){
g.stars.push(new Star(
Math.floor(g.canvas.width * Math.random()),
Math.floor(g.canvas.height * Math.random())
));
}
g.tooltip = false;
g.score = 0;
//g.player = chooseRandom(["👩🚀","👨🚀"]); //why is there no gender neutral astronaut emoji?
var img = new Image();
img.src = "astronaut.gif";
g.images.astronaut = img;
}
function initialiseMap(){
g.spareConduit = false;
g.status = {
"Main Power": false,
"Oxygen": false,
"Aux Power": false,
"Fuel": false,
"Fabber": false,
"Escape": false,
};
g.loc = {
x:0,
y:0
};
g.o2 = 10;
g.escapeSequence = 0;
generateHexes();
//done in this order because there was a very small chance that aux power could become hooked up
//in initial generation and then become true, and then the hex grid could be rejected.
g.status = {
"Main Power": false,
"Oxygen": false,
"Aux Power": false,
"Fuel": false,
"Fabber": false,
"Escape": false,
};
checkConnections();
}
function generateHexes(){
g.hexes = [];
for (var i=-4;i<=4;i++){
for (var j=-4;j<=4;j++){
g.hexes.push(new Hex(i,j));
}
}
//Main Power
var ps = getHex(-1,-1);
ps.cons = [
new Conduit(0,0),
new Conduit(1,1),
new Conduit(2,2),
new Conduit(3,3),
new Conduit(4,4),
new Conduit(5,5)
];
ps.isPowerSource = true;
//Oxygen
var os = getHex(1,1);
os.cons = [
new Conduit(0,0),
new Conduit(1,1),
new Conduit(2,2),
new Conduit(3,3),
new Conduit(4,4),
new Conduit(5,5)
];
os.isO2Source = true;
//Aux Power
var aux = getHex(4,-4);
aux.isAuxPower = true;
aux.rot = 0;
aux.dispRot = 0;
aux.cons = [
new Conduit(0,0),
new Conduit(1,1),
new Conduit(2,2)
];
//Fuel Systems
var fs = getHex(-4,4);
fs.isFuelSource = true;
fs.rot = 0;
fs.dispRot = 0;
fs.cons = [
new Conduit(3,3),
new Conduit(4,4),
new Conduit(5,5)
];
var es = getHex(4,4);
es.isEscape = true;
es.rot = 0;
es.dispRot = 0;
es.cons = [
new Conduit(2,2),
new Conduit(3,3)
];
//Fabber
var fa = getHex(-4,-4);
fa.isFabber = true;
fa.rot = 0;
fa.dispRot = 0;
fa.cons = [
new Conduit(5,5),
new Conduit(0,0)
];
//Corner Hexes
generateValidCornerHexes([[-3,-4],[-4,-3],[3,4],[4,3]]);
checkConnections();
if (g.options.difficulty == 0){
//EASY MODE
if (getHex(1,1).isPowered){ //only accept grids where there is a connection
//then rotate all the hexes around the oxygen out of alignment
var hexes = [[0,1],[0,2],[1,0],[1,2],[2,0],[2,1]];
for (var hex in hexes){
rotateHex(getHex(hexes[hex][0],hexes[hex][1]), Math.random() > 0.5 ? 1 : -1, true);
}
checkConnections();
if (getHex(1,1).isPowered){ //finally make sure it's not still powered after the jumbling
generateHexes();
} else {
return true;
}
} else {
generateHexes();
}
} else {
if (getHex(1,1).isPowered){ //don't make it auto powered
generateHexes();
}
}
if (g.options.difficulty == 2){
//IRONMAN MODE
getHex(3,4).cons = [];
getHex(4,3).cons = [];
}
}
function Star(x,y){
this.x = x;
this.y = y;
this.d = Math.random()/2 + 0.75;
}
function Hex(x,y,rot,cons){
this.x = x;
this.y = y;
this.rot = rot ? rot : Math.floor(Math.random() * 5);
this.dispRot = Math.floor(this.rot);
this.cons = cons ? cons : randomHexCons();
this.isPowered = false;
this.isPowerSource = false;
this.isAuxPower = false;
this.isOxygenated = false;
this.isO2Source = false;
this.isFuelSource = false;
this.isFueled = false;
}
function Conduit(face1,face2){
this.face1 = face1;
this.face2 = face2;
this.hasPower = false;
this.hasO2 = false;
}
function randomHexCons(){
var c1 = randomCon();
var c2 = randomCon(c1);
return [c1, c2];
}
function randomCon(not){
var f1 = chooseRandom([0,1,2,3,4,5]);
var f2 = chooseDiffRandom([0,1,2,3,4,5],f1);
var notFaces = not ? [not.face1,not.face2] : false;
if (typeof not === 'undefined' || (notFaces.indexOf(f1) === -1 && notFaces.indexOf(f2) === -1)){
return new Conduit(f1, f2);
} else {
return randomCon(not);
}
}
function generateValidCornerHexes(hexes){
for (var hex in hexes){
var h = getHex(hexes[hex][0],hexes[hex][1]);
var c1 = new Conduit(0,3);
var c2 = randomCon(c1);
h.cons = [c1,c2];
}
}
function toggleMusic(){
if (!g.music){
when = ac.currentTime;
sequence1.play(when + (60/tempo)*48);
sequence2.play(when + (60/tempo)*16);
sequence3.play(when);
g.music = true;
} else {
sequence1.stop();
sequence2.stop();
sequence3.stop();
g.music = false;
}
}
function toggleMenu(){
g.menu = !g.menu;
}
function moveStars(dist){
for (var star in g.stars){
g.stars[star].x -= dist * g.stars[star].d;
if (g.stars[star].x < 0) g.stars[star].x += g.canvas.width;
}
}
////////////////////////////////////////////////////////////////////////////////
function checkConnections(){
var powerSources = [];
var o2Sources = [];
var fuelSources = [];
g.status["Main Power"] = false;
g.status["Oxygen"] = false;
g.status["Fuel"] = false;
g.status["Fabber"] = false;
g.status["Escape"] = false;
for (var hex in g.hexes){
var h = g.hexes[hex];
h.isPowered = false;
h.isOxygenated = false;
for (var con in h.cons){
h.cons[con].hasPower = false;
h.cons[con].hasO2 = false;
h.cons[con].hasFuel = false;
}
if (h.isPowerSource){
g.status["Main Power"] = true;
for (var con in h.cons){
h.cons[con].hasPower = true;
powerSources.push([h,h.cons[con]]);
}
}
}
spreadPower(powerSources);
for (var hex in g.hexes){
var h = g.hexes[hex];
if (h.isO2Source && h.isPowered){
g.status["Oxygen"] = true;
h.isOxygenated = true;
for (var con in h.cons){
h.cons[con].hasO2 = true;
o2Sources.push([h,h.cons[con]]);
}
}
}
spreadO2(o2Sources);
for (var hex in g.hexes){
var h = g.hexes[hex];
if (h.isFuelSource && h.isPowered){
g.status["Fuel"] = true;
for (var con in h.cons){
h.cons[con].hasFuel = true;
fuelSources.push([h,h.cons[con]]);
}
}
}
spreadFuel(fuelSources);
}
function spreadPower(powerSources){
var outPowerSources = [];
for (var hex in g.hexes){
var h = g.hexes[hex];
for (var con in h.cons){
var hCon = h.cons[con];
if (!hCon.hasPower){
for (var powerSource in powerSources){
var ps = powerSources[powerSource];
if (testForConnection(h, ps[0], hCon, ps[1])){
h.isPowered = true;
hCon.hasPower = true;
outPowerSources.push([h, hCon]);
}
}
}
}
if (h.isAuxPower && h.isPowered){
h.isPowerSource = true;
g.status["Aux Power"] = true;
for (var con in h.cons){
if (!h.cons[con].hasPower){
h.cons[con].hasPower = true;
outPowerSources.push([h,h.cons[con]]);
}
}
}
if (h.isFabber && h.isPowered){
g.status["Fabber"] = true;
}
if (h.isEscape && h.isPowered && h.isFueled){
g.status["Escape"] = true;
}
}
if (powerSources.length > 0) spreadPower(outPowerSources);
}
function spreadO2(o2Sources){
var outO2Sources = [];
for (var hex in g.hexes){
var h = g.hexes[hex];
for (var con in h.cons){
var hCon = h.cons[con];
if (!hCon.hasPower && !hCon.hasO2){ //power always overrides o2
for (var o2Source in o2Sources){
var os = o2Sources[o2Source];
if (testForConnection(h, os[0], hCon, os[1])){
h.isOxygenated = true;
hCon.hasO2 = true;
outO2Sources.push([h, hCon]);
}
}
}
}
if (h.isEscape && h.isPowered){
h.isOxygenated = true;
}
}
if (o2Sources.length > 0) spreadO2(outO2Sources);
}
function spreadFuel(fuelSources){
var outFuelSources = [];
for (var hex in g.hexes){
var h = g.hexes[hex];
for (var con in h.cons){
var hCon = h.cons[con];
if (!hCon.hasPower && !hCon.hasO2 && !hCon.hasFuel){ //power and o2 override fuel
for (var fuelSource in fuelSources){
var fs = fuelSources[fuelSource];
if (testForConnection(h, fs[0], hCon, fs[1])){
if (h.isEscape){
h.isFueled = true;
if (h.isPowered) g.status["Escape"] = true;
}
hCon.hasFuel = true;
outFuelSources.push([h, hCon]);
}
}
}
}
}
if (fuelSources.length > 0) spreadFuel(outFuelSources);
}
function testForConnection(hex1, hex2, con1, con2){
if (hex1.x === hex2.x && hex1.y === hex2.y){
return false;
} else if (!testAdjacency(hex1,hex2)){
return false;
} else {
//face 0 at 0 rot is -1y, +0x
var f1 = returnFaceIndex(hex1, hex2);
var f2 = returnFaceIndex(hex2, hex1);
if (getWrapped(con1.face1 + hex1.rot) === f1 || getWrapped(con1.face2 + hex1.rot) === f1){
if (getWrapped(con2.face1 + hex2.rot) === f2 || getWrapped(con2.face2 + hex2.rot) === f2){
return true;
}
}
return false;
}
}
function testAdjacency(hex1, hex2){
var dX = hex1.x - hex2.x;
var dY = hex1.y - hex2.y;
if (dX > 1 || dX < -1) return false;
if (dY > 1 || dY < -1) return false;
if (Math.abs(dX + dY) > 1) return false;
return true;
}
function returnFaceIndex(fromHex, toHex){
var dX = fromHex.x - toHex.x;
var dY = fromHex.y - toHex.y;
if (dX == 0 && dY == -1) return 0;
if (dX == 1 && dY == -1) return 1;
if (dX == 1 && dY == 0) return 2;
if (dX == 0 && dY == 1) return 3;
if (dX == -1 && dY == 1) return 4;
if (dX == -1 && dY == 0) return 5;
throw new Error("can't find face of hex " + fromHex.x + "," + fromHex.y + " to hex " + toHex.x + "," + toHex.y);
}
function canPlaceConduit(){
var h = getHex(g.loc.x, g.loc.y);
return h.cons.length <= 2 && !h.isFabber && !h.isEscape;
}
function addConduit(){
if (canPlaceConduit()){
var h = getHex(g.loc.x, g.loc.y);
var faces = [];
for (var i=0;i<6;i++){
var face = true;
for (var con in h.cons){
if (h.cons[con].face1 == i) face = false;
if (h.cons[con].face2 == i) face = false;
}
if (face) faces.push(i);
}
var c1 = chooseRandom(faces);
var c2 = chooseDiffRandom(faces,c1);
h.cons.push(new Conduit(c1,c2));
checkConnections();
g.spareConduit = false;
takeTurn();
}
}
function takeTurn(){
var h = getHex(g.loc.x, g.loc.y);
if (h.isOxygenated){
g.o2 = 10;
} else {
g.o2--;
if (g.o2 < 0){
g.gameover = 1;
}
}
g.score++;
}
function checkFabber(){
var h = getHex(g.loc.x,g.loc.y);
if (h.isFabber && h.isPowered){
g.spareConduit = true;
}
}
function checkEscape(){
var h = getHex(g.loc.x,g.loc.y);
if (h.isEscape && h.isFueled && h.isPowered){
escaped();
return true;
}
}
function escaped(){
g.escapeSequence = 1;
if (g.highscore[g.options.difficulty] == 0 || typeof g.highscore[g.options.difficulty] === "undefined" || g.score < g.highscore[g.options.difficulty]){
g.highscore[g.options.difficulty] = g.score;
}
save();
}
function save(){
var s = {
highscore: g.highscore,
options: g.options
}
localStorage.setItem('offline', JSON.stringify(s));
}
///////////////////////////////////////////////////////////////////////////
function gameLoop(){
window.requestAnimationFrame(gameLoop);
var currentTime = (new Date()).getTime();
var delta = currentTime - g.lastTime;
if (delta > g.interval) {
clear();
drawStars();
if (g.menu) {
drawMenu();
} else if (g.gameover){
drawHexMap();
drawPlayer();
drawGameOver();
} else if (g.title){
drawTitle();
} else if (g.showStory){
drawStory(g.storyClicks);
} else if (g.escapeSequence) {
moveStars(0.01 * g.escapeSequence/10)
drawHexMap();
drawEscape(g.escapeSequence);
drawPlayer();
g.loc.x += 0.01 * g.escapeSequence/20;
g.loc.y += 0.01 * g.escapeSequence/20;
g.escapeSequence++;
if (g.escapeSequence > 100){
drawEndTitle(g.escapeSequence);
}
} else {
drawHexMap();
drawPlayer();
drawInterface();
}
drawMenuIcon();
drawMusic();
g.lastTime = currentTime - (delta % g.interval);
}
}
//////////////////////////////////////////////////////////////////////////////
function clear(){
g.ctx.fillStyle = "black";
g.ctx.fillRect(0,0,g.canvas.width,g.canvas.height);
}
function drawMenu(){
g.ctx.fillStyle = "white";
drawFontCenter(g.ctx, "Menu", 50, 5);
drawFontCenter(g.ctx, (g.options.invertControls ? "☑" : "☐") + " Invert Controls", 130, 3);
drawFontCenter(g.ctx, "Click adjacent hex to move", 180, 2);
drawFontCenter(g.ctx, "Click current hex to rotate", 210, 2);
if (!g.options.invertControls) {
drawFontCenter(g.ctx, "(LMB ↶ / RMB ↷)", 240, 2);
} else {
drawFontCenter(g.ctx, "(RMB ↶ / LMB ↷)", 240, 2);
}
drawFontCenter(g.ctx, "Difficulty", 300, 4);
if (!g.title && !g.showStory) g.ctx.fillStyle = "#444";
drawFontCenter(g.ctx, (g.options.difficulty === 0 ? "☑" : "☐") + " Normal", 350, 3);
drawFontCenter(g.ctx, (g.options.difficulty === 1 ? "☑" : "☐") + " Hard", 390, 3);
drawFontCenter(g.ctx, (g.options.difficulty === 2 ? "☑" : "☐") + " Ironman", 430, 3);
g.ctx.fillStyle = "white";
drawFontCenter(g.ctx, (g.options.colorblind ? "☑" : "☐") + " Colourblind Mode", 500, 3);
}
function drawTitle(){
g.ctx.fillStyle = "white";
drawFont(g.ctx, "Systems Offline", 400-270, 300-64, 6);
drawFont(g.ctx, "@dhmstark js13k 2018", 400-18*10, 300+32, 3);
drawFont(g.ctx, "toggle music →", 600, 600-32, 1.7);
drawFont(g.ctx, "← menu", 70, 600-32, 1.7);
}
/*
function drawArrow(){
g.ctx.beginPath();
g.ctx.moveTo(g.canvas.width - 120, g.canvas.height - 50);
g.ctx.quadraticCurveTo(g.canvas.width - 90, g.canvas.height - 30, g.canvas.width - 50, g.canvas.height - 30);
g.ctx.lineTo(g.canvas.width - 58, g.canvas.height - 38);
g.ctx.moveTo(g.canvas.width - 50, g.canvas.height - 30);
g.ctx.lineTo(g.canvas.width - 60, g.canvas.height - 24);
g.ctx.stroke();
}
*/
function drawStory(storyClicks){
g.ctx.beginPath();
if (g.storyClicks >= g.story.length){
g.showStory = false;
g.storyClicks = 0;
initialiseMap();
return false;
}
g.ctx.fillStyle = "white";
var scale = 2;
for (var i=0; i<=storyClicks; i++){
drawFont(g.ctx, g.story[i], g.canvas.width/2 - (g.story[i].length/2*6*scale), 120 + 80*i, scale);
}
}
function drawGameOver(){
g.ctx.fillStyle = "rgba(0,0,0,0.8)";
g.ctx.fillRect(400 - 31/2*6*3, 240, 31*6*3, 42);
g.ctx.fillRect(400 - 18/2*6*3, 340, 18*6*3, 42);
g.ctx.fillStyle = "white";
drawFontCenter(g.ctx,"You ran out of O₂. Game Over.",250,3);
drawFontCenter(g.ctx,"Click to restart",350,3);
if (g.gameover > 1){
initialiseData();
g.title = true;
g.gameover = 0;
}
}
function drawStars(){
g.ctx.fillStyle = "white";
for (var star in g.stars){
var s = g.stars[star];
g.ctx.fillRect(s.x,s.y,1,1);
}
}
function drawHexMap(){
for (var hex in g.hexes){
var h = g.hexes[hex];
drawHex(h, 50);
}
}
function drawHex(hex, rad){
var rot = hex.dispRot;
var x = hexToCanvas(hex.x - g.loc.x, hex.y - g.loc.y).x;
var y = hexToCanvas(hex.x - g.loc.x, hex.y - g.loc.y).y;
g.ctx.beginPath();
g.ctx.moveTo(x + Math.cos(rot * Math.PI/3) * rad, y + Math.sin(rot * Math.PI/3) * rad);
for (var i=1;i<=6;i++){
g.ctx.lineTo(x + Math.cos((rot + i)*Math.PI/3) * rad, y + Math.sin((rot + i)*Math.PI/3) * rad);
}
g.ctx.closePath();
g.ctx.strokeStyle = "white";
g.ctx.stroke();
var fv = 1;
if (hex.selected) fv++;
var fs = "#" + fv + fv + fv + fv;
if (hex.isOxygenated) fv += 2;
fs += "" + fv + fv;
g.ctx.fillStyle = fs;
g.ctx.fill();
for (var con in hex.cons){
var c = hex.cons[con];
drawConnection(hex,rad,c);
}
g.ctx.textAlign = "center";
var powerColor = g.options.colorblind ? "yellow" : "red";
var escapeColor = g.options.colorblind ? "red" : "yellow";
if (hex.isPowerSource){
drawCircle(x,y,20,powerColor,powerColor);
g.ctx.fillStyle = "white";
g.ctx.fillText("⚡",x+2,y+8);
} else if (hex.isO2Source){
drawCircle(x,y,20,"royalblue",hex.isPowered ? "royalblue" : false);
g.ctx.fillStyle = "white";
//g.ctx.fillText("O₂",x+2,y+8);
drawFont(g.ctx,"O₂",x-12,y-10,2.5);
} else if (hex.isAuxPower){
drawCircle(x,y,20,powerColor,hex.isPowered ? powerColor : false);
g.ctx.fillStyle = hex.isPowered ? "white" : powerColor;
g.ctx.fillText("⚡",x+2,y+8);
} else if (hex.isFuelSource){
drawCircle(x,y,20,"green",hex.isPowered ? "green" : false);
g.ctx.fillStyle = hex.isPowered ? "white" : "green";
g.ctx.fillText("🛢",x+2,y+6);
} else if (hex.isEscape){
g.ctx.lineWidth = hex.isFueled ? 2 : 1;
drawCircle(x,y,20,escapeColor,hex.isPowered && hex.isFueled ? escapeColor : hex.isFueled ? "#030" : false);
g.ctx.lineWidth = 1;
g.ctx.fillStyle = "white";
if (!g.escapeSequence) g.ctx.fillText("🚀",x+3,y+8);
} else if (hex.isFabber){
drawCircle(x,y,20,"white",hex.isPowered ? "white" : false);
g.ctx.fillStyle = "grey";
g.ctx.fillText("🏭",x+2,y+6);
}
// DEBUG - Hex Numbers
//g.ctx.textAlign = "center";
//g.ctx.strokeStyle = "white";
//g.ctx.font = "10px monospace";
//g.ctx.strokeText(hex.x + "," + hex.y, x, y);
if (hex.rot != hex.dispRot){
var dir = (hex.rot - hex.dispRot)/Math.abs(hex.rot - hex.dispRot);
hex.dispRot += dir * 0.1;
if (Math.abs(hex.rot - hex.dispRot) < 0.01) hex.dispRot = hex.rot; //goddamnit floating point numbers why are you like this
}
}
function drawConnection(hex,rad,c){
var rot = hex.dispRot;
var x = hexToCanvas(hex.x - g.loc.x, hex.y - g.loc.y).x;
var y = hexToCanvas(hex.x - g.loc.x, hex.y - g.loc.y).y;
var o = Math.cos(-1/6*Math.PI);
g.ctx.beginPath();
g.ctx.moveTo(x + Math.cos((rot + c.face1) * Math.PI/3 + Math.PI/6) * rad * o, y + Math.sin((rot + c.face1) * Math.PI/3 + Math.PI/6) * rad * o);
g.ctx.quadraticCurveTo(
x, y,
x + Math.cos((rot + c.face2) * Math.PI/3 + Math.PI/6) * rad * o,
y + Math.sin((rot + c.face2) * Math.PI/3 + Math.PI/6) * rad * o
);
if (c.hasPower){
g.ctx.strokeStyle = g.options.colorblind ? "yellow" : "red";
g.ctx.shadowColor = g.options.colorblind ? "#cc0" : '#c00';
g.ctx.shadowBlur = 10;
} else if (c.hasO2){
g.ctx.strokeStyle = "royalblue";
} else if (c.hasFuel){
g.ctx.strokeStyle = "green";
} else {
g.ctx.strokeStyle = "grey";
}
g.ctx.lineWidth = 4;
g.ctx.stroke();
//reset stroke styles
g.ctx.shadowBlur = 0;
g.ctx.lineWidth = 1;
}
function drawPlayer(){
var x = g.canvas.width/2;
var y = g.canvas.height/2;
g.ctx.font = "32px monospace";
g.ctx.textAlign = "center";
g.ctx.shadowColor = getO2Color();
g.ctx.shadowBlur = 30;
g.ctx.drawImage(g.images.astronaut, x - 15, y - 17);
//g.ctx.fillText(g.player,x + 3,y + 10);
g.ctx.shadowBlur = 0;
}
function drawInterface(){
var statusOrder = 0;
g.ctx.strokeStyle = "white";
g.ctx.textAlign = "right";
g.ctx.font = "10px monospace";
g.ctx.fillStyle = "black";
//g.ctx.fillRect(14,14,250,30);
for (var i=1; i<=10; i++){
drawCircle(8 + 24*i, 29, 9, "white", g.o2 >= i ? "royalblue" : "black");
g.ctx.beginPath();
g.ctx.arc(8 + 24*i, 29, 6, 0, Math.PI / 3);
g.ctx.stroke();
}
var scale = 1.5;
for (var st in g.status){
g.ctx.fillStyle = "white";
drawFont(g.ctx, st, g.canvas.width - 48 - (st.length*6*scale), 24 + statusOrder * 32, scale);
drawCircle(g.canvas.width - 32, 29 + statusOrder * 32, 9, "white", g.status[st] && g.status["Main Power"] ? "limegreen" : "black");
g.ctx.beginPath();
g.ctx.arc(g.canvas.width - 32, + 29 + statusOrder * 32, 6, 0, Math.PI / 3);
g.ctx.stroke();
statusOrder++;
}
if (g.spareConduit){
g.ctx.strokeStyle = "white";
g.ctx.fillStyle = canPlaceConduit() ? "#333" : "black";
g.ctx.fillRect(24,52,130,26);
g.ctx.strokeRect(24,52,130,26);
g.ctx.fillStyle = canPlaceConduit() ? "white" : "grey";
drawFont(g.ctx, "Spare Conduit", 32, 60, 1.5);
}
if (g.tooltip) drawTooltip(g.tooltips[g.tooltip]);
}
function getO2Color(){
var red = Math.floor(65*g.o2/10);
var green = Math.floor(105*g.o2/10);
var blue = Math.floor(225*g.o2/10);
return "rgb(" + red + ", " + green + ", " + blue + ")";
}
function drawTooltip(tooltip){
g.ctx.fillStyle = "black";
g.ctx.fillRect(50, g.canvas.height - 52, g.canvas.width - 100, 32);
g.ctx.fillStyle = "white";
//g.ctx.textAlign = "center";
//g.ctx.font = "14px monospace";
//g.ctx.shadowColor = "black";
//g.ctx.shadowBlur = 20;
var scale = 1.5;
drawFont(g.ctx, tooltip, g.canvas.width/2 - (tooltip.length/2*6*scale), g.canvas.height - 42, scale)
//g.ctx.fillText(tooltip, g.canvas.width/2, g.canvas.height - 32);
//g.ctx.shadowBlur = 0;
}
function drawMenuIcon(){
g.ctx.textAlign = "left";
g.ctx.font = "24px sans-serif";
g.ctx.fillText("⚙️", 12, g.canvas.height - 16);
}
function drawMusic(){
g.ctx.textAlign = "right";
g.ctx.font = "24px sans-serif";
g.ctx.fillText(g.music ? "🔊" : "🔈 ", g.canvas.width - 8, g.canvas.height - 16);
}
function drawCircle(x,y,radius,strokeColor,fillColor){
g.ctx.lineWidth++;
g.ctx.beginPath();
g.ctx.arc(x, y, radius, 0, 2*Math.PI);
if (strokeColor){
g.ctx.strokeStyle = strokeColor;
g.ctx.stroke();
}
g.ctx.lineWidth--;
g.ctx.beginPath();
g.ctx.arc(x, y, radius-1, 0, 2*Math.PI);
if (fillColor){
g.ctx.fillStyle = fillColor;
g.ctx.fill();
}
}
function drawEscape(timing){
var x = g.canvas.width/2;
var y = g.canvas.height/2;
drawCircle(x,y,25,"yellow", "#111111");
g.ctx.strokeStyle = "#555";
for (var i=0; i<3; i++){
g.ctx.beginPath();
var back = (5*i + timing%5);
g.ctx.arc(x - back, y, 30, (12 + back/4)*Math.PI/16, (20 - back/4)*Math.PI/16);
g.ctx.stroke();
}
}
function drawEndTitle(timing){
g.ctx.fillStyle = "white";
var c = g.canvas.width/2;
var scale = 4;
var message = "Thank you for playing!";
drawFont(g.ctx, message, g.canvas.width/2 - (message.length/2*6*scale), g.canvas.height/2 - 120, scale);
scale = 3;
var hs = g.highscore[g.options.difficulty];
if (timing > 150){
if (g.score == hs) {
var red = Math.cos(((timing*6 )%255)/255 * 2 * Math.PI) * 255;
var green = Math.cos(((timing*6+ 85)%255)/255 * 2 * Math.PI) * 255;
var blue = Math.cos(((timing*6+170)%255)/255 * 2 * Math.PI) * 255;
g.ctx.fillStyle = "rgb(" + red + "," + green + "," + blue + ")";
message = "New high score!";
drawFont(g.ctx, message, g.canvas.width/2 - (message.length/2*6*scale), g.canvas.height/2 + 60, scale);
g.ctx.fillStyle = "white";
message = "You beat "
if (g.options.difficulty == 0) message += "the game";
if (g.options.difficulty == 1) message += "hard mode";
if (g.options.difficulty == 2) message += "ironman mode";
message += " in " + g.score + " moves!";
drawFont(g.ctx, message, g.canvas.width/2 - (message.length/2*6*scale), g.canvas.height/2 + 100, scale);
} else {
message = "You beat ";
if (g.options.difficulty == 0) message += "the game";
if (g.options.difficulty == 1) message += "hard mode";
if (g.options.difficulty == 2) message += "ironman mode";
message += " in " + g.score + " moves!";
drawFont(g.ctx, message, g.canvas.width/2 - (message.length/2*6*scale), g.canvas.height/2 + 60, scale);
message = "Your best was " + hs + " moves.";
drawFont(g.ctx, message, g.canvas.width/2 - (message.length/2*6*scale), g.canvas.height/2 + 100, scale);
}
}
if (timing > 200){
message = "Click to restart";
drawFont(g.ctx, message, g.canvas.width/2 - (message.length/2*6*scale), g.canvas.height/2 + 180, scale);
}
}
/////////////////////////////////////////////////////////////////////////
function hexToCanvas(hexX,hexY){
var y = g.canvas.height/2;
var x = g.canvas.width/2;
var o = Math.cos(-1/6*Math.PI);
y += Math.sin(-1/6*Math.PI)*100*o*hexX;
x += Math.cos(-1/6*Math.PI)*100*o*hexX;
y += Math.sin(1/6*Math.PI)*100*o*hexY;
x += Math.cos(1/6*Math.PI)*100*o*hexY;
return {
x: x,
y: y
}
}
function canvasToHex(x,y){
var hexes = [ //only want the seven around the player to be selectable
[0,0],
[0,1],
[-1,1],
[-1,0],
[0,-1],
[1,-1],
[1,0]
];
var r = Math.cos(-1/6*Math.PI) * 50;
for (var hex in g.hexes){
var h = g.hexes[hex];
var hx = x - hexToCanvas(h.x, h.y).x;
var hy = y - hexToCanvas(h.x, h.y).y;
if (Math.sqrt(hx*hx + hy*hy) < r){
var selectable = false;
for (var i=0;i<hexes.length;i++){
if (h.x === hexes[i][0] && h.y === hexes[i][1]) selectable = true;
}
return {
hex: getHex(h.x + g.loc.x, h.y + g.loc.y),
selectable: selectable
}
}
}
return false;
}
function getHex(x,y){
for (var hex in g.hexes){
if (g.hexes[hex].x === x && g.hexes[hex].y === y) return g.hexes[hex];
}
return false;
}
function getWrapped(n){
return n%6;
}
function chooseDiffRandom(arr,last){
var random = chooseRandom(arr);