-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathengine.js
9384 lines (8074 loc) · 266 KB
/
engine.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
// Engine of the browser game Lost In Maze
// Copyright (c) 2014-2024 Feudal Code Limitada
// ### file: animate.js ###
"use strict"
// special animation + callbacks
const portalsToAnimate = [ ] // sqrObjs
const obelisksToAnimate = [ ] // sqrObjs
// for 30 pfs
const obeliskTime = 3
const portalTime = 25
const smokeTime = 45
const bonfireTime = 35
const beachTime = 150
let bonfireClock = bonfireTime
let portalClock = portalTime
let smokeClock = smokeTime
let beachClock = beachTime // also for ocean
let beachTurns = [ "beach-low" ] // , "beach-high" ]
let bonfireTurns = [ "1", "2" ]
let oceanTurns = [ "1", "2", "3" ] // also used to animate lava and pool
let portalTurns = "123456"
let smokeTurns = [ "1", "2", "3", "4" ]
function beachTurn() {
return beachTurns[0]
}
function oceanTurn() {
return oceanTurns[0]
}
function smokeTurn() {
return smokeTurns[0]
}
function bonfireTurn() {
return bonfireTurns[0]
}
function updateAnimation() {
updatePortalTurns()
updateSmokeTurns()
updateBonfireTurns()
updateBeachAndOceanTurns()
//
for (const sqr of obelisksToAnimate) { updateObeliskEffect(sqr) }
}
function updateBeachAndOceanTurns() {
if (LOOP < beachClock) { return }
//
beachClock = LOOP + beachTime
beachTurns.push(beachTurns.shift())
oceanTurns.push(oceanTurns.shift())
}
function updateBonfireTurns() {
if (LOOP < bonfireClock) { return }
//
bonfireClock = LOOP + bonfireTime
bonfireTurns.push(bonfireTurns.shift())
}
function updatePortalTurns() {
if (LOOP < portalClock) { return }
//
portalClock = LOOP + portalTime
const turn = portalTurns[0]
portalTurns = portalTurns.substr(1) + turn
}
function updateSmokeTurns() {
if (LOOP < smokeClock) { return }
//
smokeClock = LOOP + smokeTime
smokeTurns.push(smokeTurns.shift())
}
// portal /////////////////////////////////////////////////////////////////////
// individual effect for each portal
function includePortal(sqr) {
portalsToAnimate.push(sqr)
}
function portalBmp(sqr) {
if (sqr.effect == "activated-portal") { return "portal*!" }
return "portal*" + portalTurns[0]
}
function activatePortal(sqr) {
setSquareEffect(sqr, "activated-portal", LOOP) // LOOP is not necessary
}
// obelisk ////////////////////////////////////////////////////////////////
function includeObelisk(sqr) { obelisksToAnimate.push(sqr) }
function obeliskToSolid(sqr) {
sqr.blocked = true
sqr.walkable = false
setSquareEffect(sqr, "omkigfedc", LOOP)
}
function obeliskToEther(sqr) {
sqr.blocked = false
sqr.walkable = true
setSquareEffect(sqr, "#ccddeeffgghhii^", LOOP)
}
function keepObeliskEthereal(sqr) {
setSquareEffect(sqr, "jjkkllmmnnoopp+", LOOP)
}
function updateObeliskEffect(sqr) {
if (sqr.effect == "") { return }
const timer = sqr.effectClock + obeliskTime
if (LOOP < timer) { return }
//
const head = sqr.effect[0]
sqr.effect = sqr.effect.substr(1)
sqr.effectClock = LOOP
//
if (sqr.effect.endsWith("+")) {
sqr.effect = sqr.effect.replace("+", head + "+")
return
}
if (sqr.effect == "^") { keepObeliskEthereal(sqr) }
}
function obeliskBmp(sqr) {
if (sqr.effect == "") { return "block-e" }
return "block-e*" + sqr.effect[0]
}
// ### file: audio-monovox.js ###
"use strict"
// AudioContext demands any action by the user on the webpage
let audioCtx
let masterGain
let pulse // oscillator
let output // gain
let lfoA // oscillator // modulates pulse
let volA // gain
let lfoB // oscillator // modulates output
let volB // gain
let frequencies = {
"C4" : 261.626, // middle C
"C#4": 277.183,
"D4" : 293.665,
"D#4": 311.127,
"E4" : 329.628,
"F4" : 349.228,
"F#4": 369.994,
"G4" : 391.995,
"G#4": 415.305,
"A4" : 440.000,
"A#4": 466.164,
"B4" : 493.883
}
// init ///////////////////////////////////////////////////////////////////
function maybeInitMonovox() {
//
if (audioCtx) { return } // already done
//
if (typeof AudioContext == "undefined") { return } // not possible
//
audioCtx = new AudioContext()
fillFrequencies()
//
masterGain = audioCtx.createGain()
masterGain.connect(audioCtx.destination)
masterGain.gain.setValueAtTime(5.0, audioCtx.currentTime)
//
output = audioCtx.createGain()
output.connect(masterGain)
output.gain.setValueAtTime(0.0, audioCtx.currentTime)
//
pulse = audioCtx.createOscillator()
pulse.connect(output)
pulse.frequency.setValueAtTime(0.0, audioCtx.currentTime)
pulse.start()
//
volA = audioCtx.createGain()
volA.gain.setValueAtTime(0.0, audioCtx.currentTime)
volA.connect(pulse.frequency)
//
lfoA = audioCtx.createOscillator()
lfoA.frequency.setValueAtTime(0.0, audioCtx.currentTime)
lfoA.connect(volA)
lfoA.start()
//
volB = audioCtx.createGain()
volB.gain.setValueAtTime(0.0, audioCtx.currentTime)
volB.connect(output.gain)
//
lfoB = audioCtx.createOscillator()
lfoB.frequency.setValueAtTime(0.0, audioCtx.currentTime)
lfoB.connect(volB)
lfoB.start()
}
function fillFrequencies() {
const notes = "C,C#,D,D#,E,F,F#,G,G#,A,A#,B".split(",")
for (let n = 0; n < 12; n++) {
const note = notes[n]
frequencies[note + "1"] = frequencies[note + "4"] / 8
frequencies[note + "2"] = frequencies[note + "4"] / 4
frequencies[note + "3"] = frequencies[note + "4"] / 2
frequencies[note + "5"] = frequencies[note + "4"] * 2
frequencies[note + "6"] = frequencies[note + "4"] * 4
frequencies[note + "7"] = frequencies[note + "4"] * 8
frequencies[note + "8"] = frequencies[note + "4"] * 16
}
}
function isValidFrequency(token) {
return frequencies[token] != undefined
}
function monovoxReady() {
return audioCtx != undefined
}
// play - release - mute - decay //////////////////////////////////////////
function playNote(instrument, tone) {
//
cancelAudioScheduledValues()
//
const freq = frequencies[tone]
//
if (instrument == "organ") { playNoteOrgan(freq); return }
if (instrument == "organ2") { playNoteOrgan2(freq); return }
if (instrument == "organ3") { playNoteOrgan3(freq); return }
if (instrument == "organ4") { playNoteOrgan4(freq); return }
if (instrument == "spatial") { playNoteSpatial(freq); return }
if (instrument == "organ-dry") { playNoteOrganDry(freq); return }
if (instrument == "organ-bell") { playNoteOrganBell(freq); return }
if (instrument == "organ-simple") { playNoteOrganSimple(freq); return }
}
function releaseNote(instrument) {
//
cancelAudioScheduledValues()
//
if (instrument == "organ") { releaseNoteOrgan(); return }
if (instrument == "organ2") { releaseNoteOrgan2(); return }
if (instrument == "organ3") { releaseNoteOrgan3(); return }
if (instrument == "organ4") { releaseNoteOrgan4(); return }
if (instrument == "spatial") { releaseNoteSpatial(); return }
if (instrument == "organ-dry") { releaseNoteOrganDry(); return }
if (instrument == "organ-bell") { releaseNoteOrganBell(); return }
if (instrument == "organ-simple") { releaseNoteOrganSimple(); return }
}
function muteNote(duration) {
//
if (! duration) { duration = 0.1 }
//
cancelAudioScheduledValues()
//
output.gain.linearRampToValueAtTime(0.0, audioCtx.currentTime + duration)
}
function decayNote(duration) {
//
if (! duration) { duration = 0.1 }
//
cancelAudioScheduledValues()
//
output.gain.linearRampToValueAtTime(0.5, audioCtx.currentTime + duration)
}
function cancelAudioScheduledValues() {
//
output.gain.cancelScheduledValues(audioCtx.currentTime)
//
pulse.frequency.cancelScheduledValues(audioCtx.currentTime)
//
volA.gain.cancelScheduledValues(audioCtx.currentTime)
volB.gain.cancelScheduledValues(audioCtx.currentTime)
//
lfoA.frequency.cancelScheduledValues(audioCtx.currentTime)
lfoB.frequency.cancelScheduledValues(audioCtx.currentTime)
}
// instrument - organ /////////////////////////////////////////////////////
function playNoteOrgan(freq) {
//
volA.gain.setValueAtTime(0.0, audioCtx.currentTime) // no use
volB.gain.setValueAtTime(0.0, audioCtx.currentTime) // no use
//
pulse.type = "sine"
pulse.frequency.setValueAtTime(freq, audioCtx.currentTime)
//
output.gain.setValueAtTime(2.5, audioCtx.currentTime) // attack (sudden)
output.gain.setTargetAtTime(5.5, audioCtx.currentTime + 0.1, 0.4) // sustain (strong)
}
function releaseNoteOrgan() {
//
pulse.type = "sine"
lfoA.type = "square"
lfoA.frequency.setValueAtTime(10.0, audioCtx.currentTime)
volA.gain.setValueAtTime(0.1, audioCtx.currentTime)
output.gain.linearRampToValueAtTime(0.0, audioCtx.currentTime + 3.0)
}
// instrument - organ2 ////////////////////////////////////////////////////
function playNoteOrgan2(freq) {
//
volB.gain.setValueAtTime(0.0, audioCtx.currentTime) // no use
//
lfoA.type = "sawtooth"
lfoA.frequency.setValueAtTime(3.0, audioCtx.currentTime)
volA.gain.setValueAtTime(1.0, audioCtx.currentTime)
//
pulse.type = "sine"
pulse.frequency.setTargetAtTime(freq, audioCtx.currentTime, 0.01)
//
output.gain.setTargetAtTime(2.5, audioCtx.currentTime, 0.1) // attack
output.gain.setTargetAtTime(5.5, audioCtx.currentTime + 0.1, 0.4) // decay
}
function releaseNoteOrgan2() {
//
output.gain.linearRampToValueAtTime(0.0, audioCtx.currentTime + 2.0)
//
lfoA.type = "square"
lfoA.frequency.linearRampToValueAtTime(6.0, audioCtx.currentTime + 1.0)
}
// instrument - organ3 ////////////////////////////////////////////////////
function playNoteOrgan3(freq) {
//
volB.gain.setValueAtTime(0.0, audioCtx.currentTime) // no use
//
lfoA.type = "sawtooth"
lfoA.frequency.setValueAtTime(7.0, audioCtx.currentTime)
volA.gain.setValueAtTime(1.0, audioCtx.currentTime)
//
pulse.type = "triangle"
pulse.frequency.setTargetAtTime(freq, audioCtx.currentTime, 0.01)
//
output.gain.setTargetAtTime(2.5, audioCtx.currentTime, 0.1) // attack
output.gain.setTargetAtTime(5.5, audioCtx.currentTime + 0.1, 0.4) // decay
}
function releaseNoteOrgan3() {
//
output.gain.linearRampToValueAtTime(0.0, audioCtx.currentTime + 2.0)
//
lfoA.type = "square"
lfoA.frequency.linearRampToValueAtTime(5.0, audioCtx.currentTime + 1.0)
}
// instrument - organ4 ////////////////////////////////////////////////////
function playNoteOrgan4(freq) {
//
volA.gain.setValueAtTime(0.0, audioCtx.currentTime) // no use
//
lfoB.type = "square"
lfoB.frequency.setValueAtTime(5.0, audioCtx.currentTime)
volB.gain.setValueAtTime(0.0, audioCtx.currentTime)
volB.gain.linearRampToValueAtTime(5.0, audioCtx.currentTime + 0.5)
//
pulse.type = "triangle"
pulse.frequency.setTargetAtTime(freq, audioCtx.currentTime, 0.01)
//
output.gain.setTargetAtTime(2.5, audioCtx.currentTime, 0.1) // attack
output.gain.setTargetAtTime(5.5, audioCtx.currentTime + 0.1, 0.4) // decay
}
function releaseNoteOrgan4() {
//
output.gain.linearRampToValueAtTime(0.0, audioCtx.currentTime + 2.0)
volB.gain.linearRampToValueAtTime(0.0, audioCtx.currentTime + 1.5)
}
// instrument - organ dry /////////////////////////////////////////////////
function playNoteOrganDry(freq) {
//
volA.gain.setValueAtTime(0.0, audioCtx.currentTime) // no use
volB.gain.setValueAtTime(0.0, audioCtx.currentTime) // no use
//
pulse.type = "square"
pulse.frequency.setTargetAtTime(freq, audioCtx.currentTime, 0.01)
//
output.gain.setTargetAtTime(5.0, audioCtx.currentTime, 0.1) // attack
output.gain.setTargetAtTime(0.2, audioCtx.currentTime + 0.1, 0.1) // decay
}
function releaseNoteOrganDry() {
output.gain.linearRampToValueAtTime(0.0, audioCtx.currentTime + 0.1)
}
// instrument - spatial ///////////////////////////////////////////////////
function playNoteSpatial(freq) {
//
volB.gain.setValueAtTime(0.0, audioCtx.currentTime) // no use
//
lfoA.type = "sine"
lfoA.frequency.setValueAtTime(3.0, audioCtx.currentTime)
volA.gain.setValueAtTime(10.0, audioCtx.currentTime)
//
pulse.type = "triangle"
pulse.frequency.setTargetAtTime(freq, audioCtx.currentTime, 0.01)
//
output.gain.setValueAtTime(0.3, audioCtx.currentTime) // almost muting previous note
output.gain.setTargetAtTime(2.5, audioCtx.currentTime, 0.1) // attack
output.gain.setTargetAtTime(5.5, audioCtx.currentTime + 0.1, 0.4) // decay
}
function releaseNoteSpatial() {
//
output.gain.linearRampToValueAtTime(0.0, audioCtx.currentTime + 1.0)
//
volA.gain.linearRampToValueAtTime(0.0, audioCtx.currentTime + 1.0)
}
// instrument - organ bell ////////////////////////////////////////////////
function playNoteOrganBell(freq) {
//
volA.gain.setValueAtTime(0.0, audioCtx.currentTime) // no use
volB.gain.setValueAtTime(0.0, audioCtx.currentTime) // no use
//
pulse.type = "triangle"
pulse.frequency.setValueAtTime(freq, audioCtx.currentTime)
//
output.gain.setValueAtTime(2.0, audioCtx.currentTime) // attack (sudden)
}
function releaseNoteOrganBell() {
output.gain.linearRampToValueAtTime(0.0, audioCtx.currentTime + 1.0)
}
// instrument - organ simple //////////////////////////////////////////////
function playNoteOrganSimple(freq) {
volA.gain.setValueAtTime(0.0, audioCtx.currentTime) // no use
volB.gain.setValueAtTime(0.0, audioCtx.currentTime) // no use
//
pulse.type = "sine"
pulse.frequency.setValueAtTime(freq, audioCtx.currentTime)
//
output.gain.setValueAtTime(2.5, audioCtx.currentTime) // attack (sudden)
output.gain.setTargetAtTime(5.5, audioCtx.currentTime + 0.1, 0.4) // sustain (strong)
}
function releaseNoteOrganSimple() {
output.gain.linearRampToValueAtTime(0.0, audioCtx.currentTime + 1.0)
}
// ### file: audio-music.js ###
"use strict"
let musicTokens
let musicInstrument = "organ" // make safe for stopMusic
let musics = {
"success": "@c3 @c1 @c2 @c1 @c2 @c4",
"failure": "@d1 @d2",
"c1": "[organ-dry] (3 A#3 2) (2 A3 2) (3 F3 2) (3 A#3 1) (2 A3 2) (2 F3 2)",
"c2": "[organ-dry] (3 G#2 2) (2 G2 2) (3 D#2 2) (3 G#2 1) (2 G2 2) (2 D#2 2)",
"c3": "[organ-dry] (3 A4 2) (2 G#4 2) (3 E4 2) (3 A4 1) (2 G#4 2) (2 E4 2)",
"c4": "[organ3] (3 A4 9) (1 G#4 9) (1 E4 20) [organ-dry] (5 A4 1) (2 G#4 4) (1 E4 1)",
"d1": "[organ3] (0 F2 34) (16 D#2 2) (17 F2 2) (7 F2 41) (0 G#2 41) (13 F2 27) (3 G#2 1) (1 F2 28) (0 G#2 2) (1 F2 32) (0 D2 2) [organ] (17 D2 2) [organ3] (2 D2 24)",
"d2": "[organ3] (0 G#2 2) (1 F2 3) (3 G#2 2) (1 F2 8) (3 G#2 2) (1 F2 32)",
"e": "[organ3] (3 G#2 4) (5 G2 4) (5 D#2 4) (3 G#2 5) (0 G2 3) (2 D#2 1) (2 D#2 10)"
}
// init ///////////////////////////////////////////////////////////////////
function initMusic() {
//
adjustMusic("success")
adjustMusic("failure")
}
function adjustMusic(key) { // replaces keys for true music segments
//
const tokens = musics[key].split(" ")
//
let s = ""
//
for (const token of tokens) {
//
if (s != "") { s += " " }
//
s += musics[token.substr(1)] // excludes @
}
//
musics[key] = s
}
// stop ///////////////////////////////////////////////////////////////////
function stopMusic() {
if (! monovoxReady()) { return }
//
musicTokens = [ ]
releaseNote(musicInstrument)
}
// play ///////////////////////////////////////////////////////////////////
function playMusic(s) {
maybeInitMonovox()
if (! monovoxReady()) { return }
//
stopMusic()
musicTokens = musics[s].split(" ")
processMusicToken()
}
function processMusicToken() {
//
if (musicTokens.length == 0) { return }
//
const token = musicTokens.shift()
//
// THE ORDER HERE IS IMPORTANT:
//
if (token.startsWith("[")) {
processSpecialMusicToken(token)
processMusicToken()
return
}
//
if (token.startsWith("(")) {
const delay = 100 * parseInt(token.replace("(", ""))
setTimeout(processMusicToken, delay)
return
}
//
if (token.endsWith(")")) {
const duration = 100 * parseInt(token.replace(")", ""))
setTimeout(function () { releaseNote(musicInstrument); processMusicToken() }, duration)
return
}
// must be the tone to be played now (or dirty)
if (isValidFrequency(token)) { playNote(musicInstrument, token) }
processMusicToken()
}
function processSpecialMusicToken(token) {
//
if (token == "[mute]") { muteNote(0.1); return }
if (token == "[decay]") { decayNote(0.1); return }
if (token == "[organ]") { musicInstrument = "organ"; return }
if (token == "[organ2]") { musicInstrument = "organ2"; return }
if (token == "[organ3]") { musicInstrument = "organ3"; return }
if (token == "[organ4]") { musicInstrument = "organ4"; return }
if (token == "[spatial]") { musicInstrument = "spatial"; return }
if (token == "[organ-dry]") { musicInstrument = "organ-dry"; return }
if (token == "[organ-bell]") { musicInstrument = "organ-bell"; return }
if (token == "[organ-simple]") { musicInstrument = "organ-simple"; return }
}
// ### file: avatar.js ###
"use strict"
var avatarPickUpClock = 0
var avatarHasScroll = false
var avatarHotkeyAction = null
var avatarAltarSkullClock = 0
// action by keyboard /////////////////////////////////////////////////////////
function setAvatarHotkeyAction(action) {
//
if (avatarHotkeyAction != null) { return }
//
avatarHotkeyAction = action
}
// script /////////////////////////////////////////////////////////////////////
function defaultAvatarScript() { // this is the default script; other script may be used
//
if (avatar.disabled) { return }
//
if (avatarHotkeyAction != null) { avatarHotkeyAction(); avatarHotkeyAction = null }
//
handleAvatarAttack()
//
handleArrowKeysForAvatar()
}
// square /////////////////////////////////////////////////////////////////////
function isFreeSquareForAvatar(row, col) {
//
const status = squareStatusForAvatar(row, col)
//
if (status == "free") { return true }
if (status == "field") { return true }
//
return false
}
function squareStatusForAvatar(row, col) {
//
// free, field, creature, pusher, block //
//
const sqr = getSquare(row, col)
//
if (sqr == null) { return "block" }
//
// portal
if (sqr.layerC == "portal") {
return (avatarBrightGem ? "free" : "block")
}
// walkable
if (sqr.walkable) {
if (sqr.creature != null) { return "creature" }
if (sqr.layerB == "smoke") { return "field" }
if (sqr.layerB == "bonfire") { return "field" }
return "free"
}
// not walkable
if (sqr.trigger != null) { return "pusher" } // so can push altar (for example) to activate trigger
//
return "block"
}
// monster ahead //////////////////////////////////////////////////////////////
function monsterAheadAvatar() {
//
const creature = creatureAheadAvatar()
//
if (creature == null) { return null }
//
if (creature.name == "Orion") { return null } // orion may be undefined
//
return null
}
function creatureAheadAvatar() {
//
const futureRow = avatar.row + deltaRowFor[avatar.direction]
const futureCol = avatar.col + deltaColFor[avatar.direction]
//
const sqr = getSquare(futureRow, futureCol)
//
if (sqr == null) { return null }
if (sqr.creature == null) { return null }
if (sqr.creature.disabled) { return null }
//
return sqr.creature
}
// pick up ////////////////////////////////////////////////////////////////////
function avatarPickUp() {
//
if (! readyToMove(avatar)) { return }
//
if (LOOP < avatarPickUpClock + 5) { return }
//
const sqr = getSquare(avatar.row, avatar.col)
//
if (sqr.objects == null) { speak(avatar, "No pick up."); return }
//
avatarPickUpClock = LOOP
//
const item = removeObjectFromSquare(avatar.row, avatar.col)
//
avatarPickUp2(item)
//
shallUpdateMainBar = true
}
function avatarPickUp2(item) { // inventory self limits everything to 999!
//
if (item.startsWith("spear-")) {
avatar.spears += 1
speak(avatar, "Grabbing spear.")
return
}
//
if (item == "bright-gem") { scriptFoundBrightGem(); return }
if (item == "wooden-key") { scriptFoundWoodenKey(); return }
if (item == "iron-key") { scriptFoundIronKey(); return }
if (item == "copper-key") { scriptFoundCopperKey(); return }
//
if (item == "health-potion") {
speak(avatar, "Grabbing health potion.")
avatar.healthPotions += 1
return
}
//
if (item == "speed-oil") {
speak(avatar, "Grabbing speed oil.")
avatar.speedOils += 1
return
}
//
if (item == "mana-potion") {
speak(avatar, "Grabbing mana potion.")
avatar.manaPotions += 1
return
}
//
if (item == "antidote-potion") {
speak(avatar, "Grabbing antidote potion.")
avatar.antidotePotions += 1
return
}
//
alert("ERROR while picking up " + item)
}
// ### file: avatar-attack.js ###
"use strict"
var avatarStopAimTimer = 0
var avatarAttackStatus = "none" // "wanting-aim", "aiming"
var avatarAttackFunction = null
// keyboard handlers //////////////////////////////////////////////////////////
function avatarTryMeleeAttack() {
//
if (avatarAttackStatus != "none") { return }
//
if (LOOP < avatar.attackClock + 20) { succinct(avatar, "Too soon!"); return }
//
avatarAttackFunction = avatarMeleeAttack
//
avatarTryAim()
}
function avatarTryThrowSpear() {
//
if (avatarAttackStatus != "none") { return }
//
if (LOOP < avatar.attackClock + 20) { succinct(avatar, "Too soon!"); return }
//
avatarAttackFunction = avatarThrowSpear
//
avatarTryAim()
}
function avatarTryThrowPurpleBubble() {
//
if (avatarAttackStatus != "none") { return }
//
if (LOOP < avatar.attackClock + 20) { succinct(avatar, "Too soon!"); return }
//
avatarAttackFunction = avatarThrowPurpleBubble
//
avatarTryAim()
}
// aim ////////////////////////////////////////////////////////////////////////
function avatarTryAim() {
//
if (avatar.moveStatus != "stand") { avatarAttackStatus = "wanting-aim"; return }
//
avatarStartAim()
}
function avatarStartAim() {
//
avatarAttackStatus = "aiming"
//
avatarStopAimTimer = LOOP + 20
}
function shallDrawAimMark() {
//
return LOOP < avatarStopAimTimer // also draws after aiming
}
// manager ////////////////////////////////////////////////////////////////////
function handleAvatarAttack() {
//
if (avatarAttackStatus == "none") { return }
//
if (avatarAttackStatus == "wanting-aim") {
//
if (avatar.moveStatus == "stand") { avatarStartAim() }
//
return
}
//
if (avatarAttackStatus == "aiming") {
//
if (LOOP < avatarStopAimTimer) { return }
//
avatarAttackStatus = "none"
avatarAttackFunction()
return
}
}
// spear //////////////////////////////////////////////////////////////////////
function avatarThrowSpear() {
//
avatar.attackClock = LOOP
avatarStopAimTimer = LOOP + 10 // so aim mark keeps being drawn
//
if (avatar.spears == 0) { succinct(avatar, "No spear."); return }
//
avatar.spears -= 1
shallUpdateMainBar = true
//
throwSpear(avatar, avatar.row, avatar.col, avatar.direction, 10, 20)
}
// purple bubble //////////////////////////////////////////////////////////////
function avatarThrowPurpleBubble() {
//
avatar.attackClock = LOOP
avatarStopAimTimer = LOOP + 10 // so aim mark keeps being drawn
//
if (avatar.purpleBubbles == 0) { succinct(avatar, "No purple bubble."); return }
//
avatar.purpleBubbles -= 1
shallUpdateMainBar = true
//
throwPurpleBubble(avatar, avatar.row, avatar.col, avatar.direction, 18, 30)
}
// melee //////////////////////////////////////////////////////////////////////
function avatarMeleeAttack() {
//
avatar.attackClock = LOOP
avatarStopAimTimer = LOOP + 2 // so aim mark keeps being drawn
//
if (avatarMeleeWeapon == "none") { succinct(avatar, "No weapon."); return }
//
let target = creatureAheadAvatar()
if (target == null) { succinct(avatar, "Miss."); return }
//
receiveMeleeAttack(avatar, target, 12, 20)
}
// ### file: avatar-inventory.js ###
"use strict"
var avatarArmor = "none"
var avatarShield = "none"
var avatarMeleeWeapon = "none"
var avatarIronKey = false
var avatarWoodenKey = false
var avatarCopperKey = false
var avatarBrightGem = false
// health potion //////////////////////////////////////////////////////////////
function avatarDrinkHealthPotion(altar) {
//
if (avatar.life == avatar.maxLife) { succinct(avatar, "Not hurt!"); return false }
//
if (! altar) {
//
if (avatar.healthPotions == 0) { succinct(avatar, "No potion."); return false }
//
avatar.healthPotions -= 1
}
//
const hurt = avatar.maxLife - avatar.life
const heal = Math.min(50, hurt)
avatar.life += heal
reportDamage(avatar, "heal", heal)
succinctDouble(avatar, "Ahhh.")
shallUpdateMainBar = true
return true
}
// antidote potion ////////////////////////////////////////////////////////////
function avatarDrinkAntidotePotion(altar) {
//
if (avatar.poisoning.length == 0 && avatar.dizziness == 0) {
succinct(avatar, "No need.")
return false
}
//
if (! altar) {
//
if (avatar.antidotePotions == 0) { succinct(avatar, "No potion."); return false }
//
avatar.antidotePotions -= 1
}
//
avatar.poisoning = []
avatar.dizziness = 0
speak(avatar, "Gulp.")
shallUpdateMainBar = true
return true
}
// speed oil //////////////////////////////////////////////////////////////////
function avatarDrinkSpeedOil(altar) {
//
if (avatar.speedEffect == "fast") { succinct(avatar, "Still fast!"); return false }
//
if (! altar) {
//
if (avatar.speedOils == 0) { succinct(avatar, "No oil."); return false }
//
avatar.speedOils -= 1
}
//
avatar.speedEffect = "fast"
avatar.speedTimer = LOOP + 1800 // 60s
speak(avatar, "Zooom!")
shallUpdateMainBar = true
return true
}
// mana potion ////////////////////////////////////////////////////////////////
function avatarDrinkManaPotion(altar) {
//
if (avatar.manaShield > 299) { succinct(avatar, "Mana shield full."); return false }
//
if (! altar) {
//
if (avatar.manaPotions == 0) { succinct(avatar, "No potion."); return false }
//
avatar.manaPotions -= 1
}
//
avatar.manaShield = Math.min(300, avatar.manaShield + 100)
succinct(avatar, "Ssssssss.")
shallUpdateMainBar = true
return true
}
// ### file: avatar-move.js ###
"use strict"
// This system for input is not just for diagonals, it is
// very stable, much better than just respond directly to
// keyboard events during avatar continuous movement...
// maybe because of keyboard delay/speed configuration.
var tryingsToLeaveDiagonal = 0 // makes easy to release arrow keys of diagonal direction //
var lastTryingToLeaveDiagonalClock = 0
var tryingsToStepOnField = 0