-
Notifications
You must be signed in to change notification settings - Fork 1
/
script.js
870 lines (786 loc) · 33.8 KB
/
script.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
// players
let players = {
fred: { powerup: { toClear: [] }, score: 0, ready: false },
greenlee: { powerup: { toClear: [] }, score: 0, ready: false },
pinkney: { powerup: { toClear: [] }, score: 0, ready: false },
bluebell: { powerup: { toClear: [] }, score: 0, ready: false },
willem: { powerup: { toClear: [] }, score: 0, ready: false },
greydon: { powerup: { toClear: [] }, score: 0, ready: false },
}
let achtung = {
gamemode: 1, // 0 = arcade, 1 = classic
startScreen: true, // are we on the start screen?
gameRunning: false, // are we playing?
gameEnded: true, // are noone alive?
winner: false, // do we have a winner?
sides: 0, // can all players go out of screen and come out the other side
clearSides: 0, // to clear timeone if leftover time from last round
playing: [], // who's playing
powerups: [
"g_slow",
"g_fast",
"g_thin",
"g_robot",
"g_side",
"g_invisible",
"r_slow",
"r_fast",
"r_thick",
"r_robot",
"r_reverse",
"b_clear",
"b_more",
"b_sides",
"o_random",
],
powerupsOnScreen: [], // what powerups are on screen now
}
// variables
let canvasID,
UIcanvas = document.querySelector("#ui_canvas"), // ui canvas
dotsCanvas = document.querySelector("#dots_canvas"), // canvas for player dots
trailsHitboxCanvas = document.querySelector("#trails_hitbox_canvas"), // canvas for player trails
powerupVisualCanvas = document.querySelector("#powerup_visual_canvas"), // canvas for powerup icons
powerupHitboxCanvas = document.querySelector("#powerup_hitbox_canvas"), // canvas for powerup hitboxes
ctxUI = UIcanvas.getContext("2d"),
ctxDO = dotsCanvas.getContext("2d"),
ctxTH = trailsHitboxCanvas.getContext("2d"),
ctxPV = powerupVisualCanvas.getContext("2d"),
ctxPH = powerupHitboxCanvas.getContext("2d"),
yellow = getComputedStyle(document.documentElement).getPropertyValue(`--yellow`), // colors
green = getComputedStyle(document.documentElement).getPropertyValue(`--greenlee`),
greent = getComputedStyle(document.documentElement).getPropertyValue(`--greenlee-t`),
red = getComputedStyle(document.documentElement).getPropertyValue(`--fred`),
redt = getComputedStyle(document.documentElement).getPropertyValue(`--fred-t`),
blue = getComputedStyle(document.documentElement).getPropertyValue(`--blue`),
bluet = getComputedStyle(document.documentElement).getPropertyValue(`--blue-t`),
tFrame = 0, // cur frame in draw
powerupProb = 0.005, // in percent
bridgeProb = 0.005, // in percent
bridgeSize = 10, // in frames
turnSpeed = 0.06, // in radians per frame
w,
h,
w100th,
h100th,
moveSpeed,
playerSize,
hitboxSize,
borderWidth,
iconSize // to be set in newSize()
// when resizing
window.addEventListener("resize", newSize)
function newSize() {
// update canvas sizes and variable sizes to fit new size
const dpr = Math.min(window.devicePixelRatio, 2)
w = Math.round(UIcanvas.getBoundingClientRect().width * dpr)
h = Math.round(UIcanvas.getBoundingClientRect().height * dpr)
UIcanvas.width = w
UIcanvas.height = h
dotsCanvas.width = w
dotsCanvas.height = h
trailsHitboxCanvas.width = w
trailsHitboxCanvas.height = h
powerupVisualCanvas.width = w
powerupVisualCanvas.height = h
powerupHitboxCanvas.width = w
powerupHitboxCanvas.height = h
w100th = w / 100 // 1 percent of canvas width
h100th = h / 100 // 1 percent of canvas height
moveSpeed = w100th * 0.18 // in pixels per frame
playerSize = w100th * 0.7 // in pixels
hitboxSize = playerSize / 1.8 // in pixels
borderWidth = w100th / 2 // in pixels
iconSize = w100th * 2 // in pixels
init() // restart
}
function init() {
achtung.powerupsOnScreen = [] // clear powerups on screen
clearTimeout(achtung.clearSides) // clear timeout if sides powerup leftover time from last round
achtung.sides = 0 // reset sides
for (const player in players) {
// clear timeout if powerup leftover time from last round
for (let i = 0; i < players[player].powerup.toClear.length; i++) {
clearTimeout(players[player].powerup.toClear[i])
}
// reset players object to default values before starting a new round
players[player].x = 0
players[player].y = 0
players[player].dir = 0
players[player].turnL = false
players[player].turnR = false
players[player].color = getComputedStyle(document.documentElement).getPropertyValue(`--${player}`) // colors from css :root object
players[player].alive = true
players[player].winner = false
players[player].bridge = false
players[player].bridgeFrame = 0
players[player].powerup = {} // contains powerup values
players[player].powerup.size = 1
players[player].powerup.robot = 0
players[player].powerup.reverse = 0
players[player].powerup.speed = 1
players[player].powerup.invisible = 0
players[player].powerup.side = 0
players[player].powerup.powerupArray = []
players[player].powerup.toClear = [] // to clear timeout at the end of rounds if leftover time
}
// clear everything
ctxTH.clearRect(0, 0, w, h)
ctxUI.clearRect(0, 0, w, h)
ctxDO.clearRect(0, 0, w, h)
ctxPH.clearRect(0, 0, w, h)
ctxPV.clearRect(0, 0, w, h)
// draw yellow border
ctxDO.lineWidth = borderWidth
ctxDO.strokeStyle = yellow
ctxDO.strokeRect(borderWidth / 2, borderWidth / 2, h - borderWidth, h - borderWidth)
calcRandomStartPos() // calc random start positions
calcRandomStartDir() // calc random start directions
drawGameUI() // draw ui
drawStart() // draw players at start so they can see where they're going
}
document.addEventListener("keydown", (e) => {
// update players turning to true if turning
for (const player in players) {
if (!players[player].alive) continue // if player not alive; skip
if (e.code == players[player].keyL) {
players[player].turnL = true
}
if (e.code == players[player].keyR) {
players[player].turnR = true
}
}
// if keydown == escape, go to start page
if (e.code == "Escape") {
achtung.startScreen = true
achtung.gameEnded = true
achtung.gameRunning = false
startPage.style.display = "block"
window.cancelAnimationFrame(canvasID)
for (const player in players) {
players[player].score = 0
}
init()
}
})
document.addEventListener("keyup", (e) => {
// update players turning to false when keyup
for (const player in players) {
if (!players[player].alive) continue // if player not alive; skip
if (e.code == players[player].keyL) {
players[player].turnL = false
}
if (e.code == players[player].keyR) {
players[player].turnR = false
}
}
})
document.addEventListener("keypress", (e) => {
if (e.code == "Space") {
pressSpace()
}
})
function pressSpace() {
let playingC = 0
for (const player in players) {
if (players[player].ready) playingC++
}
if (achtung.startScreen) {
if (playingC >= 2) {
// start game
for (const player in players) {
if (players[player].active && !players[player].ready) {
resetPlayer(document.querySelector(`.player_wrapper.${player}`))
}
}
achtung.startScreen = false
achtung.gameEnded = true
startPage.style.display = "none"
init()
}
}
if (!achtung.gameEnded) {
if (!achtung.gameRunning) {
// resume game
achtung.gameRunning = true
window.requestAnimationFrame(draw)
} else {
// pause game
achtung.gameRunning = false
window.cancelAnimationFrame(canvasID)
}
} else {
// restart game
if (achtung.winner) {
achtung.winner = false
achtung.startScreen = true
achtung.gameEnded = true
achtung.gameRunning = false
startPage.style.display = "block"
window.cancelAnimationFrame(canvasID)
for (const player in players) {
players[player].score = 0
}
init()
} else {
if (playingC >= 2) {
achtung.gameEnded = false
achtung.gameRunning = false
init()
}
}
}
}
// draw start position so players know where they're going
function drawStart() {
for (const player in players) {
if (!players[player].ready) continue
// draw player dot
ctxDO.fillStyle = yellow
ctxDO.beginPath()
ctxDO.arc(players[player].x, players[player].y, (playerSize / 2 - 0.1) * players[player].powerup.size, 0, r2d(360), true)
ctxDO.fill()
// draw player trail
ctxTH.fillStyle = players[player].color
ctxTH.save()
ctxTH.translate(players[player].x, players[player].y)
ctxTH.rotate(players[player].dir - r2d(270))
ctxTH.fillRect(
(-playerSize / 2) * players[player].powerup.size,
0,
playerSize * players[player].powerup.size,
playerSize * 2 * players[player].powerup.size
)
ctxTH.restore()
}
}
// main loop
function draw() {
canvasID = window.requestAnimationFrame(draw) // to pause: cancelAnimationFrame(CanvasID)
tFrame++ // increment tFrame
// clear
ctxTH.clearRect(h, 0, w - h, h)
ctxDO.clearRect(0, 0, w, h)
ctxDO.fillStyle = "#000000"
ctxDO.fillRect(h, 0, w - h, h)
// draw yellow border
ctxDO.lineWidth = borderWidth
ctxDO.strokeStyle = "#000000"
ctxDO.strokeRect(borderWidth / 2, borderWidth / 2, h - borderWidth, h - borderWidth)
if (achtung.sides != 0) ctxDO.strokeStyle = `rgba(255, 255, 0, ${Math.abs((tFrame % 40) - 20) / 20})`
else ctxDO.strokeStyle = yellow // if sides, border flickers
ctxDO.strokeRect(borderWidth / 2, borderWidth / 2, h - borderWidth, h - borderWidth)
// spawn new powerup if arcade mode and math.random() < powerup probability
if (achtung.gamemode == 1) if (Math.random() < powerupProb) powerupSpawner()
// loop through players and draw them
for (const player in players) {
if (!players[player].ready) continue // continue loop if player not playing
// player pos
let prevprevPosX = players[player].x - mathCos(players[player].dir) * moveSpeed * players[player].powerup.speed,
prevprevPosY = players[player].y - mathSin(players[player].dir) * moveSpeed * players[player].powerup.speed,
prevPosX = players[player].x,
prevPosY = players[player].y,
nextPosX = players[player].x + mathCos(players[player].dir) * moveSpeed * players[player].powerup.speed,
nextPosY = players[player].y + mathSin(players[player].dir) * moveSpeed * players[player].powerup.speed
// draw player dot
if (players[player].powerup.reverse == 0) {
if (players[player].powerup.side == 0) {
ctxDO.fillStyle = yellow
} else ctxDO.fillStyle = `rgba(255, 255, 0, ${Math.abs((tFrame % 40) - 20) / 20})` // flicker dot if side powerup
} else {
if (players[player].powerup.side == 0) {
ctxDO.fillStyle = blue
} else ctxDO.fillStyle = `rgba(0, 0, 255, ${Math.abs((tFrame % 40) - 20) / 20})` // flicker dot if side powerup
}
if (players[player].powerup.robot == 0) {
// draw dot if normal
ctxDO.beginPath()
ctxDO.arc(nextPosX, nextPosY, (playerSize / 2) * players[player].powerup.size, 0, r2d(360), true)
ctxDO.fill()
} else {
// draw square if robot
ctxDO.save()
ctxDO.translate(nextPosX, nextPosY)
ctxDO.rotate(players[player].dir - r2d(270))
ctxDO.fillRect(
(-playerSize / 2) * players[player].powerup.size,
(-playerSize / 2) * players[player].powerup.size,
playerSize * players[player].powerup.size,
playerSize * players[player].powerup.size
)
ctxDO.restore()
}
if (!players[player].alive) continue // continue if player not alive (drawing dot is above, so player dot will still be drawn even if dead)
// update player turning
if (players[player].powerup.robot == 0) {
// if normal
if (players[player].turnL) {
if (players[player].powerup.reverse == 0) players[player].dir -= turnSpeed / Math.pow(players[player].powerup.size, 0.3)
else players[player].dir += turnSpeed / Math.pow(players[player].powerup.size, 0.3)
}
if (players[player].turnR) {
if (players[player].powerup.reverse == 0) players[player].dir += turnSpeed / Math.pow(players[player].powerup.size, 0.3)
else players[player].dir -= turnSpeed / Math.pow(players[player].powerup.size, 0.3)
}
} else {
// if robot
if (players[player].turnL) {
players[player].turnL = false
if (players[player].powerup.reverse == 0) players[player].dir -= r2d(90)
else players[player].dir += r2d(90)
}
if (players[player].turnR) {
players[player].turnR = false
if (players[player].powerup.reverse == 0) players[player].dir += r2d(90)
else players[player].dir -= r2d(90)
}
}
// update player position
prevPosX = players[player].x
prevPosY = players[player].y
players[player].x = nextPosX
players[player].y = nextPosY
// check for player inside playing field
if (achtung.sides != 0 || players[player].powerup.side != 0) {
// player has side powerup of achtung.sides, players can move out of canvas
if (players[player].x < 0) {
players[player].x = h
prevPosX = h
prevprevPosX = h
}
if (players[player].x > h) {
players[player].x = 0
prevPosX = 0
prevprevPosX = 0
}
if (players[player].y < 0) {
players[player].y = h
prevPosY = h
prevprevPosY = h
}
if (players[player].y > h) {
players[player].y = 0
prevPosY = 0
prevprevPosY = 0
}
} else {
if (
// if not, player dead
players[player].x < borderWidth + hitboxSize ||
players[player].x > h - borderWidth - hitboxSize ||
players[player].y < borderWidth + hitboxSize ||
players[player].y > h - borderWidth - hitboxSize
) {
givePoints(players[player])
continue
}
}
// insert bridge
if (!players[player].bridge) {
// if not already bridge
if (Math.random() < bridgeProb) {
// if math.random() less than prob for bridge
players[player].bridge = true
}
players[player].bridgeFrame = tFrame // what frame did bridge start
}
if (players[player].bridgeFrame < tFrame - (bridgeSize / players[player].powerup.speed) * players[player].powerup.size) {
// stop bridge when bridgeSize frame has passed
players[player].bridge = false
}
// draw player trail; don't draw if bridge or invisible
if (!players[player].bridge && players[player].powerup.invisible == 0) {
ctxTH.strokeStyle = players[player].color
ctxTH.lineWidth = playerSize * players[player].powerup.size
ctxTH.beginPath()
if (players[player].powerup.robot != 0) {
ctxTH.lineCap = "round"
ctxTH.moveTo(prevPosX, prevPosY)
} else {
ctxTH.lineCap = "butt"
ctxTH.moveTo(prevprevPosX, prevprevPosY)
}
ctxTH.lineTo(players[player].x, players[player].y)
ctxTH.stroke()
}
// check collision
const pxFront = Math.round(players[player].x + mathCos(players[player].dir) * hitboxSize * players[player].powerup.size)
const pyFront = Math.round(players[player].y + mathSin(players[player].dir) * hitboxSize * players[player].powerup.size)
const pxFront2 = Math.round(players[player].x + mathCos(players[player].dir))
const pyFront2 = Math.round(players[player].y + mathSin(players[player].dir))
const pxLeft = Math.round(players[player].x + mathCos(players[player].dir - r2d(55)) * hitboxSize * players[player].powerup.size)
const pyLeft = Math.round(players[player].y + mathSin(players[player].dir - r2d(55)) * hitboxSize * players[player].powerup.size)
const pxRight = Math.round(players[player].x + mathCos(players[player].dir + r2d(55)) * hitboxSize * players[player].powerup.size)
const pyRight = Math.round(players[player].y + mathSin(players[player].dir + r2d(55)) * hitboxSize * players[player].powerup.size)
const imgDataFrontTH = ctxTH.getImageData(pxFront, pyFront, 1, 1).data
const imgDataFrontPH = ctxPH.getImageData(pxFront, pyFront, 1, 1).data
const imgDataFront2TH = ctxTH.getImageData(pxFront2, pyFront2, 1, 1).data
const imgDataFront2PH = ctxPH.getImageData(pxFront2, pyFront2, 1, 1).data
const imgDataLeftTH = ctxTH.getImageData(pxLeft, pyLeft, 1, 1).data
const imgDataLeftPH = ctxPH.getImageData(pxLeft, pyLeft, 1, 1).data
const imgDataRightTH = ctxTH.getImageData(pxRight, pyRight, 1, 1).data
const imgDataRightPH = ctxPH.getImageData(pxRight, pyRight, 1, 1).data
// uncomment to visualize hitbox
// ctxDO.fillStyle = "#ffffff"
// ctxDO.fillRect(pxFront, pyFront, 1, 1)
// ctxDO.fillRect(pxFront2, pyFront2, 1, 1)
// ctxDO.fillRect(pxLeft, pyLeft, 1, 1)
// ctxDO.fillRect(pxRight, pyRight, 1, 1)
// check collision for every powerup on screen
for (let i = 0; i < achtung.powerupsOnScreen.length; i++) {
if (
(imgDataFrontPH[2] == i * 3 + 1 && imgDataFrontPH[1] == i * 3 + 2 && imgDataFrontPH[0] == i * 3 + 3) ||
(imgDataLeftPH[2] == i * 3 + 1 && imgDataLeftPH[1] == i * 3 + 2 && imgDataLeftPH[0] == i * 3 + 3) ||
(imgDataRightPH[2] == i * 3 + 1 && imgDataRightPH[1] == i * 3 + 2 && imgDataRightPH[0] == i * 3 + 3)
) {
let powName = achtung.powerupsOnScreen[i].pow
players[player].powerup.powerupArray.push(powName)
// remove powerup from screen
achtung.powerupsOnScreen.splice(i, 1)
// do powerup
doPowerups(player, players[player].powerup.powerupArray.length - 1)
// draw powerup
powerupDraw()
}
}
if (!players[player].bridge) {
// don't check collision if making bridge
if (players[player].powerup.invisible == 0) {
// don't check if invisible
if (players[player].powerup.robot == 0) {
// check alpha value of pixels front, front2, left, right
if (imgDataFrontTH[3] == 255 || imgDataFront2TH[3] == 255 || imgDataLeftTH[3] == 255 || imgDataRightTH[3] == 255) {
givePoints(players[player])
continue
}
} else {
if (imgDataFrontTH[3] == 255) {
// if robot only check alpha value of front
givePoints(players[player])
continue
}
}
}
}
}
// drawGameUI()
checkGameState()
}
// check game stats
function checkGameState() {
// how many are alive?
let alive = 0
for (const player in players) {
if (players[player].alive && players[player].ready) {
alive++
}
}
// if all dead
if (alive <= 1) {
// IMPORTANT - change back to 1 -------------------------------------------------------------------------------------------------------------------------------
window.cancelAnimationFrame(canvasID)
achtung.gameEnded = true
}
// did someone win?
if (achtung.gameEnded) {
if (achtung.scoreArray[achtung.scoreArray.length - 1][1] >= achtung.pointGoal) {
if (achtung.scoreArray[achtung.scoreArray.length - 1][1] - achtung.scoreArray[achtung.scoreArray.length - 2][1] > 1) {
let p = achtung.scoreArray[achtung.scoreArray.length - 1][0]
// console.log(p + " wins the game")
achtung.winner = true
// draw winner screen
for (const player in players) {
if (player == p) {
ctxUI.fillStyle = players[player].color.replace("rgb", "rgba").replace(")", ", 0.3)")
ctxUI.fillRect(20 * h100th, 32 * h100th, h - 40 * h100th, h - 64 * h100th)
ctxUI.lineWidth = borderWidth
ctxUI.strokeStyle = players[player].color
ctxUI.strokeRect(20 * h100th, 32 * h100th, h - 40 * h100th, h - 64 * h100th)
ctxUI.textBaseline = "middle"
ctxUI.fillStyle = players[player].color
ctxUI.textAlign = "center"
ctxUI.font = `${w100th * 6}px 'Sarabun'`
ctxUI.fillText("Konec hry", h / 2, h / 2 - h100th * 5) // the legendary "konec hry"
ctxUI.font = `${w100th * 4}px 'Sarabun'`
ctxUI.fillText(`${capitalize(player)} wins!`, h / 2, h / 2 + h100th * 5)
}
}
} else {
// two players are within 1 point; continue playing
// console.log("play on")
}
}
}
}
// updates points for players
function givePoints(p) {
p.alive = false
for (const player in players) {
if (!players[player].ready) continue
if (p != players[player] && players[player].alive) {
players[player].score++
drawGameUI()
}
}
}
// draws game ui
const drawGameUI = () => {
ctxUI.textBaseline = "alphabetic"
ctxUI.clearRect(h, 0, w - h, h)
ctxUI.fillStyle = "#000000"
ctxUI.fillRect(h, 0, w - h, h)
// sort players
achtung.scoreArray = []
for (const player in players) {
if (!players[player].ready) continue
achtung.scoreArray.push([player, players[player].score])
}
achtung.scoreArray.sort((a, b) => a[1] - b[1])
// draw top text
achtung.pointGoal = (achtung.scoreArray.length - 1) * 10 // // // // // // // change back to * 10 -------------------------------------------------------------------------------------
let UIcenter = +h + (w - h) / 2
ctxUI.fillStyle = "#FFFFFF"
ctxUI.textAlign = "center"
ctxUI.letterSpacing = `${w100th * 0.06}px`
ctxUI.font = `${w100th * 3}px 'Sarabun'`
ctxUI.fillText("Race to", UIcenter, w100th * 5)
ctxUI.font = `${w100th * 12}px 'Sarabun'`
ctxUI.fillText(achtung.pointGoal, UIcenter, w100th * 15)
ctxUI.font = `${w100th * 2}px 'Sarabun'`
ctxUI.fillText("2 point difference", UIcenter, w100th * 19)
// draw player names and score
ctxUI.font = `${w100th * 3}px 'Sarabun'`
let playerYOffset = w100th * 32
for (let i = achtung.scoreArray.length - 1; i >= 0; i--) {
let p = achtung.scoreArray[i][0]
ctxUI.fillStyle = players[p].color
ctxUI.textAlign = "start"
ctxUI.fillText(capitalize(p), +h + w100th * 2, playerYOffset)
ctxUI.textAlign = "end"
ctxUI.fillText(players[p].score, +w - w100th * 2, playerYOffset)
playerYOffset += w100th * 5
}
// draw space to continue text
ctxUI.fillStyle = "#FFFFFF"
ctxUI.textAlign = "center"
ctxUI.font = `${w100th * 2}px 'Sarabun'`
ctxUI.fillText("SPACE to play", UIcenter, +h - w100th * 6)
ctxUI.fillText("ESCAPE to quit", UIcenter, +h - w100th * 3)
}
// executes powerups
function doPowerups(puPlayer, index) {
let gTimeout = 8000
let rTimeout = 5000
let powName = players[puPlayer].powerup.powerupArray[index]
// powerup starts
if (powName == "o_random") {
powName = achtung.powerups[Math.floor(Math.random() * achtung.powerups.length)]
}
if (powName == "g_slow") {
players[puPlayer].powerup.speed *= 0.5
players[puPlayer].powerup.toClear[index] = setTimeout(() => (players[puPlayer].powerup.speed *= 2), gTimeout)
}
if (powName == "g_fast") {
players[puPlayer].powerup.speed *= 2
players[puPlayer].powerup.toClear[index] = setTimeout(() => (players[puPlayer].powerup.speed *= 0.5), gTimeout)
}
if (powName == "g_thin") {
players[puPlayer].powerup.size *= 0.5
players[puPlayer].powerup.toClear[index] = setTimeout(() => (players[puPlayer].powerup.size *= 2), gTimeout)
}
if (powName == "g_robot") {
players[puPlayer].powerup.robot++
players[puPlayer].powerup.toClear[index] = setTimeout(() => players[puPlayer].powerup.robot--, gTimeout)
}
if (powName == "g_side") {
players[puPlayer].powerup.side++
players[puPlayer].powerup.toClear[index] = setTimeout(() => players[puPlayer].powerup.side--, gTimeout)
}
if (powName == "g_invisible") {
players[puPlayer].powerup.invisible++
players[puPlayer].powerup.toClear[index] = setTimeout(() => players[puPlayer].powerup.invisible--, gTimeout)
}
if (powName == "r_slow") {
for (const otherPlayers in players) {
if (otherPlayers != puPlayer) {
players[otherPlayers].powerup.speed *= 0.5
players[otherPlayers].powerup.toClear[index] = setTimeout(() => (players[otherPlayers].powerup.speed *= 2), rTimeout)
}
}
}
if (powName == "r_fast") {
for (const otherPlayers in players) {
if (otherPlayers != puPlayer) {
players[otherPlayers].powerup.speed *= 2
players[otherPlayers].powerup.toClear[index] = setTimeout(() => (players[otherPlayers].powerup.speed *= 0.5), rTimeout)
}
}
}
if (powName == "r_thick") {
for (const otherPlayers in players) {
if (otherPlayers != puPlayer) {
players[otherPlayers].powerup.size *= 2
players[otherPlayers].powerup.toClear[index] = setTimeout(() => (players[otherPlayers].powerup.size *= 0.5), rTimeout)
}
}
}
if (powName == "r_robot") {
for (const otherPlayers in players) {
if (otherPlayers != puPlayer) {
players[otherPlayers].powerup.robot++
players[otherPlayers].powerup.toClear[index] = setTimeout(() => players[otherPlayers].powerup.robot--, rTimeout)
}
}
}
if (powName == "r_reverse") {
for (const otherPlayers in players) {
if (otherPlayers != puPlayer) {
players[otherPlayers].powerup.reverse++
players[otherPlayers].powerup.toClear[index] = setTimeout(() => players[otherPlayers].powerup.reverse--, rTimeout)
}
}
}
if (powName == "b_clear") {
ctxTH.clearRect(0, 0, h, h)
}
if (powName == "b_more") {
setTimeout(powerupSpawner, 100)
setTimeout(powerupSpawner, 200)
setTimeout(powerupSpawner, 300)
}
if (powName == "b_sides") {
achtung.sides++
achtung.clearSides = setTimeout(() => achtung.sides--, gTimeout)
}
}
// updates the achtung object with data of a new powerup
function powerupSpawner() {
if (achtung.powerupsOnScreen.length > 30) return
let newPow = Math.floor(Math.random() * achtung.powerups.length),
spawnX = Math.floor(Math.random() * h),
spawnY = Math.floor(Math.random() * h),
powup = achtung.powerups[newPow]
// powup = "r_reverse" // apklsdjalskdjalksdjlaksjdlakfjlæanæoæiuanweifupnaweifunaewæfnakdnfkalsjdfnklajsdfnkaljdnfklajnsdfklajnsdfkajdsnfkajdsnflakjdnf
achtung.powerupsOnScreen[achtung.powerupsOnScreen.length] = {}
achtung.powerupsOnScreen[achtung.powerupsOnScreen.length - 1].pow = powup
achtung.powerupsOnScreen[achtung.powerupsOnScreen.length - 1].xPos = spawnX
achtung.powerupsOnScreen[achtung.powerupsOnScreen.length - 1].yPos = spawnY
// powerupIndex++
powerupDraw()
}
// draws powerups to canvas
function powerupDraw() {
ctxPV.clearRect(0, 0, w, h)
ctxPH.clearRect(0, 0, w, h)
for (let i = 0; i < achtung.powerupsOnScreen.length; i++) {
if (achtung.powerupsOnScreen[i] == 0) continue
let pow = achtung.powerupsOnScreen[i].pow,
spawnX = achtung.powerupsOnScreen[i].xPos,
spawnY = achtung.powerupsOnScreen[i].yPos
// draw hitbox
ctxPH.fillStyle = `rgba(${i * 3 + 3}, ${i * 3 + 2}, ${i * 3 + 1}, 1)`
ctxPH.beginPath()
ctxPH.arc(spawnX, spawnY, iconSize, 0, r2d(360), false)
ctxPH.fill()
let greenGrad = ctxPV.createRadialGradient(0, 0, 0, 0, 0, iconSize)
greenGrad.addColorStop(0, green)
greenGrad.addColorStop(1, greent)
let redGrad = ctxPV.createRadialGradient(0, 0, 0, 0, 0, iconSize)
redGrad.addColorStop(0, red)
redGrad.addColorStop(1, redt)
let blueGrad = ctxPV.createRadialGradient(0, 0, 0, 0, 0, iconSize)
blueGrad.addColorStop(0, blue)
blueGrad.addColorStop(1, bluet)
ctxPV.save()
ctxPV.translate(spawnX, spawnY)
ctxPV.fillStyle = "#000000"
ctxPV.beginPath()
ctxPV.arc(0, 0, iconSize, 0, r2d(360), false)
ctxPV.fill()
if (pow.charAt(0) == "g") {
ctxPV.strokeStyle = green
ctxPV.fillStyle = greenGrad
}
if (pow.charAt(0) == "r") {
ctxPV.strokeStyle = red
ctxPV.fillStyle = redGrad
}
if (pow.charAt(0) == "b") {
ctxPV.strokeStyle = blue
ctxPV.fillStyle = blueGrad
}
if (pow.charAt(0) == "g" || pow.charAt(0) == "r" || pow.charAt(0) == "b") {
// draw bg
ctxPV.beginPath()
ctxPV.arc(0, 0, iconSize, 0, r2d(360), false)
ctxPV.stroke()
ctxPV.beginPath()
ctxPV.arc(0, 0, iconSize, 0, r2d(360), false)
ctxPV.fill()
} else {
// draw random bg
ctxPV.strokeStyle = blue
ctxPV.beginPath()
ctxPV.arc(0, 0, iconSize, 0, r2d(360), false)
ctxPV.stroke()
let line1 = [-65, -200]
let line2 = [-15, -250]
// draw blue section of bg
ctxPV.beginPath()
ctxPV.fillStyle = blueGrad
ctxPV.arc(0, 0, iconSize, r2d(line1[0]), r2d(line1[1]), true)
ctxPV.moveTo(Math.cos(r2d(line1[1])) * iconSize, Math.sin(r2d(line1[1])) * iconSize)
ctxPV.lineTo(Math.cos(r2d(line1[0])) * iconSize, Math.sin(r2d(line1[0])) * iconSize)
ctxPV.fill()
// draw red section of bg
ctxPV.beginPath()
ctxPV.fillStyle = redGrad
ctxPV.arc(0, 0, iconSize, r2d(line2[0]), r2d(line1[0]), true)
ctxPV.moveTo(Math.cos(r2d(line1[0])) * iconSize, Math.sin(r2d(line1[0])) * iconSize)
ctxPV.lineTo(Math.cos(r2d(line1[1])) * iconSize, Math.sin(r2d(line1[1])) * iconSize)
ctxPV.arc(0, 0, iconSize, r2d(line1[1]), r2d(line2[1]), true)
ctxPV.lineTo(Math.cos(r2d(line2[0])) * iconSize, Math.sin(r2d(line2[0])) * iconSize)
ctxPV.fill()
// draw green section of bg
ctxPV.beginPath()
ctxPV.fillStyle = greenGrad
ctxPV.arc(0, 0, iconSize, r2d(line2[1]), r2d(line2[0]), true)
ctxPV.moveTo(Math.cos(r2d(line2[0])) * iconSize, Math.sin(r2d(line2[0])) * iconSize)
ctxPV.lineTo(Math.cos(r2d(line2[1])) * iconSize, Math.sin(r2d(line2[1])) * iconSize)
ctxPV.fill()
}
// draw yellow icon
drawPowerupIcons(pow.slice(2))
ctxPV.restore()
}
}
// calc random start direction
function calcRandomStartDir() {
for (const player in players) {
players[player].dir = round100(Math.random() * Math.PI * 2)
}
}
// calc random start position x and y
function calcRandomStartPos() {
for (const player in players) {
players[player].x = map(calcRandomInt(h), 0, h, borderWidth * 10, h - borderWidth * 10) // map to avoid instant death
players[player].y = map(calcRandomInt(h), 0, h, borderWidth * 10, h - borderWidth * 10) // map to avoid instant death
}
}
// capitalize string
const capitalize = (s) => (typeof s === "string" ? s.charAt(0).toUpperCase() + s.slice(1) : "")
// returns n rounded to .00
const round100 = (n) => Math.round(n * 100) / 100
// returns n round mathCos and mathSin
const mathCos = (n) => round100(Math.cos(n))
const mathSin = (n) => round100(Math.sin(n))
// returns pixel index for alpha value in raw pixel data string
const getAlphaIndexForCoord = (x, y, width) => y * (width * 4) + x * 4 + 3
// returns radians from degree input
const r2d = (deg) => ((Math.PI * 2) / 360) * deg
// returns random int from 0 to n
const calcRandomInt = (int) => Math.floor(Math.random() * int)
// returns n mapped from start1-stop1 to start2-stop2
const map = (n, start1, stop1, start2, stop2) => ((n - start1) / (stop1 - start1)) * (stop2 - start2) + start2
newSize() // calc initial values
init() // start init