-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRACE.JS
More file actions
667 lines (574 loc) · 18.9 KB
/
Copy pathRACE.JS
File metadata and controls
667 lines (574 loc) · 18.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
// =============================================================================
// Name: Radroach Races
// Author: Theeohn Megistus
// License: MIT
// Repository: https://github.com/Theeohn/Radroach-Races
// =============================================================================
// File: RACE.JS (scene)
// Description: Runs a single race. Picks a track, counts down the betting
// timer, simulates the roaches until one reaches the mutfruit, then waits
// for the left knob press to return to the title screen.
// =============================================================================
(function (app) {
const drawLineCached = h.drawLine.bind(h);
const fillRectCached = h.fillRect.bind(h);
const drawImageCached = h.drawImage.bind(h);
const drawCircleCached = h.drawCircle.bind(h);
const drawRectCached = h.drawRect.bind(h);
const drawPolyCached = h.drawPoly.bind(h);
// Asset File Directories
const SFX = {
FLAP: 'HOLO/RADROACH_RACES/FLAP.WAV',
WINNER: 'HOLO/RADROACH_RACES/WINNER.WAV',
};
const dirty = new Int16Array(20);
let mainLoopInterval = null;
let countdownTimer = null;
// Set to 0 for random map, or 1–10 to force a specific map for testing.
const TEST_MAP = 0;
const ROACH_SPEED = 4.5; // Speed of roach in pixels per frame
const ROACH_WALL_SPEED = 3.5; // Speed of roach in pixels per frame after wall bounces
// Game States: COUNTDOWN -> RACING -> GAMEOVER
let gameState = 'COUNTDOWN';
let winnerId = -1;
let countdownValue = 5;
let currentMapId = 0;
let trackWalls = new Int16Array(0);
let goalPos = { x: 0, y: 0, w: 18, h: 18, hitW: 16, hitH: 16 };
let radroaches = [];
let trackDirty = 1;
let lastFlapTime = 0;
let roachImage = null;
// Rotate options
const rotOpts = { rotate: 0 };
const ROACH_NAMES = {
1: 'CIRCLE',
2: 'TRIANGLE',
3: 'DELTA',
4: 'SQUARE',
5: 'DIAMOND',
};
const ROACH_HIT_R = 11.4;
const ROACH_R_SUM = ROACH_HIT_R * 2; // a.hitR + b.hitR is always this
const JITTER_COS = new Float32Array(21);
const JITTER_SIN = new Float32Array(21);
for (let jd = -10; jd <= 10; jd++) {
const jrad = jd * 0.017453292519943295;
JITTER_COS[jd + 10] = Math.cos(jrad);
JITTER_SIN[jd + 10] = Math.sin(jrad);
}
// ─── Track Loading ────────────────────────────────────────────────────────
/**
* Pick a track, copy its walls out of the map data, and spawn the roaches.
* The map data is released again once the track is extracted.
* @returns {void} Nothing
*/
function loadTrack() {
let MAP_DATA = eval(fs.readFileSync('HOLO/RADROACH_RACES/MAPS.JS'));
currentMapId = TEST_MAP > 0 ? TEST_MAP - 1 : Math.randInt(16);
let base = 0;
for (let i = 0; i < currentMapId; i++) {
base += 5 + MAP_DATA[base + 4] * 4;
}
const wallCount = MAP_DATA[base + 4];
trackWalls = new Int16Array(wallCount * 4);
for (let i = 0; i < wallCount; i++) {
const src = base + 5 + i * 4;
const dst = i * 4;
trackWalls[dst] = MAP_DATA[src];
trackWalls[dst + 1] = MAP_DATA[src + 1];
trackWalls[dst + 2] = MAP_DATA[src + 2];
trackWalls[dst + 3] = MAP_DATA[src + 3];
}
goalPos.x = MAP_DATA[base];
goalPos.y = MAP_DATA[base + 1];
const startX = MAP_DATA[base + 2];
const startYBase = MAP_DATA[base + 3];
// Only the extracted track is needed from here on.
MAP_DATA = null;
app.gc();
// Maps 3, 4, 6, 8, 12, 13 (0-based: 2, 3, 5, 7, 11, 12) use horizontal
// spawn spread; all others use vertical.
const horizSpawn =
currentMapId === 2 || currentMapId === 3 || currentMapId === 5 ||
currentMapId === 7 || currentMapId === 11 || currentMapId === 12;
const slots = horizSpawn ? [0, 21, 42, 63, 84] : [0, 23, 44, 65, 86];
for (let s = 4; s > 0; s--) {
const sv = Math.randInt(s + 1);
const tmp = slots[s];
slots[s] = slots[sv];
slots[sv] = tmp;
}
radroaches = [];
for (let i = 0; i < 5; i++) {
radroaches.push({
id: i + 1,
cx: startX + 7 + (horizSpawn ? slots[i] : 0),
cy: startYBase + 7 + (horizSpawn ? 0 : slots[i]),
vx: 0,
vy: 0,
});
setRandomVelocity(radroaches[i], ROACH_SPEED);
}
}
// ─── Countdown ────────────────────────────────────────────────────────────
function tickCountdown() {
if (gameState !== 'COUNTDOWN') return;
h.setColor(0).fillRect(0, 0, 480, 320);
drawTrack();
drawAllRoaches();
if (countdownValue > 0) {
h.setFont('Monofonto96')
.setFontAlign(0, 0)
.setColor(3)
.drawString(countdownValue.toString(), 240, 160);
h.setColor(0)
.fillRect(67, 226, 407, 275)
.setColor(3)
.drawRect(69, 228, 405, 273);
h.setFont('Monofonto36')
.setFontAlign(0, -1)
.setColor(3)
.drawString('PLACE YOUR BETS!!!', 240, 230);
countdownValue--;
} else {
clearInterval(countdownTimer);
countdownTimer = null;
gameState = 'RACING';
trackDirty = 1;
}
}
// ─── Drawing ──────────────────────────────────────────────────────────────
/**
* Draw a roach's type shape, outlined exactly to its collision hitbox
* radius. Shapes are always drawn in a fixed orientation (never rotated
* with travel direction) so Square never gets confused for Diamond, etc.
* @param r The roach to draw the shape for
* @returns {void} Nothing
*/
function drawRoachShape(r) {
'ram';
const cx = r.cx,
cy = r.cy;
// Circle: outline of the hitbox.
if (r.id === 1) {
drawCircleCached(cx, cy, ROACH_HIT_R);
return;
}
// Square: vertices touch the hitbox at the diagonal corners.
if (r.id === 4) {
const half = ROACH_HIT_R * 0.7071067811865476;
drawRectCached(cx - half, cy - half, cx + half, cy + half);
return;
}
// Diamond: same square, rotated so the vertices point at the walls
// instead of the corners.
if (r.id === 5) {
drawPolyCached(
[
cx,
cy - ROACH_HIT_R,
cx + ROACH_HIT_R,
cy,
cx,
cy + ROACH_HIT_R,
cx - ROACH_HIT_R,
cy,
],
true,
);
return;
}
// Triangle (id 2) points up; Delta (id 3) is the same shape inverted.
const rot = r.id === 3 ? 1.5707963267948966 : -1.5707963267948966;
drawPolyCached(
[
cx + ROACH_HIT_R * Math.cos(rot),
cy + ROACH_HIT_R * Math.sin(rot),
cx + ROACH_HIT_R * Math.cos(rot + 2.0943951023931953),
cy + ROACH_HIT_R * Math.sin(rot + 2.0943951023931953),
cx + ROACH_HIT_R * Math.cos(rot - 2.0943951023931953),
cy + ROACH_HIT_R * Math.sin(rot - 2.0943951023931953),
],
true,
);
}
function drawAllRoaches() {
'ram';
// Fallback: sprite failed to load, so just show the hitbox shape.
if (!roachImage) {
h.setColor(3);
for (let i = 0; i < radroaches.length; i++) {
drawRoachShape(radroaches[i]);
}
return;
}
// Shape sits behind the sprite (fixed orientation, never rotated) so
// it peeks out around the roach on top, making each racer's type
// identifiable at a glance.
for (let i = 0; i < radroaches.length; i++) {
const r = radroaches[i];
h.setColor(2);
drawRoachShape(r);
// Head (top of image) points along the direction of travel.
rotOpts.rotate = Math.atan2(r.vy, r.vx) + 1.5707963267948966;
h.setColor(3);
drawImageCached(roachImage, r.cx, r.cy, rotOpts);
}
h.setColor(3);
}
function drawTrack(clipX1, clipY1, clipX2, clipY2) {
'ram';
// No region given -> drawing everything (countdown / full redraw), so
// default to the whole screen and every element passes its overlap test.
if (clipX1 === undefined) {
clipX1 = 0;
clipY1 = 0;
clipX2 = 480;
clipY2 = 320;
}
if (clipX1 <= 465 && clipX2 >= 18 && clipY1 <= 298 && clipY2 >= 18) {
h.setColor(3).drawRect(18, 18, 465, 298);
}
for (let i = 0; i < trackWalls.length; i += 4) {
const lx1 = trackWalls[i],
ly1 = trackWalls[i + 1],
lx2 = trackWalls[i + 2],
ly2 = trackWalls[i + 3];
const wMinX = lx1 < lx2 ? lx1 : lx2,
wMaxX = lx1 < lx2 ? lx2 : lx1;
const wMinY = ly1 < ly2 ? ly1 : ly2,
wMaxY = ly1 < ly2 ? ly2 : ly1;
if (wMaxX < clipX1 || wMinX > clipX2 || wMaxY < clipY1 || wMinY > clipY2)
continue;
drawLineCached(lx1, ly1, lx2, ly2);
}
if (clipX2 >= 225 && clipX1 <= 320 && clipY2 >= 302 && clipY1 <= 316) {
h.setFont('Monofonto14')
.setFontAlign(-1, -1)
.setColor(3)
.drawString('Map ' + (currentMapId + 1), 225, 302);
}
const goalX1 = goalPos.x - 6,
goalY1 = goalPos.y - 8;
const goalX2 = goalPos.x + goalPos.w + 6,
goalY2 = goalPos.y + goalPos.h + 11;
if (
clipX2 >= goalX1 &&
clipX1 <= goalX2 &&
clipY2 >= goalY1 &&
clipY1 <= goalY2
) {
h.setColor(3)
.fillRect(
goalPos.x,
goalPos.y,
goalPos.x + goalPos.w,
goalPos.y + goalPos.h,
)
.setColor(2)
.fillRect(goalPos.x + 6, goalPos.y - 8, goalPos.x + 11, goalPos.y)
.fillRect(goalPos.x - 6, goalPos.y + 6, goalPos.x, goalPos.y + 11);
}
}
// ─── Physics ──────────────────────────────────────────────────────────────
function setRandomVelocity(r, speed) {
const deg = Math.randInt(400);
const rad = deg * 0.017453292519943295;
r.vx = Math.cos(rad) * speed;
r.vy = Math.sin(rad) * speed;
}
function jitterVelocity(r) {
'ram';
const idx = Math.randInt(21); // 0..20 maps to -10..+10 degrees
const c = JITTER_COS[idx],
s = JITTER_SIN[idx];
const vx = r.vx,
vy = r.vy;
r.vx = vx * c - vy * s;
r.vy = vx * s + vy * c;
}
function checkWallCollision(r) {
'ram';
// ── Border walls ─────────────────────────────────────────────────────────
let bounced = false;
const hr = ROACH_HIT_R;
if (r.cx - hr <= 18) {
r.cx = 18 + hr;
r.vx = r.vx < 0 ? -r.vx : r.vx;
bounced = true;
} else if (r.cx + hr >= 465) {
r.cx = 465 - hr;
r.vx = r.vx > 0 ? -r.vx : r.vx;
bounced = true;
}
if (r.cy - hr <= 18) {
r.cy = 18 + hr;
r.vy = r.vy < 0 ? -r.vy : r.vy;
bounced = true;
} else if (r.cy + hr >= 298) {
r.cy = 298 - hr;
r.vy = r.vy > 0 ? -r.vy : r.vy;
bounced = true;
}
// ── Interior walls: circle vs line-segment ────────────────────────────────
for (let i = 0; i < trackWalls.length; i += 4) {
const x1 = trackWalls[i],
y1 = trackWalls[i + 1];
const x2 = trackWalls[i + 2],
y2 = trackWalls[i + 3];
const segDx = x2 - x1,
segDy = y2 - y1;
const lenSq = segDx * segDx + segDy * segDy;
let t =
lenSq > 0 ? ((r.cx - x1) * segDx + (r.cy - y1) * segDy) / lenSq : 0;
if (t < 0) t = 0;
else if (t > 1) t = 1;
const clx = x1 + t * segDx;
const cly = y1 + t * segDy;
const ex = r.cx - clx;
const ey = r.cy - cly;
const distSq = ex * ex + ey * ey;
if (distSq < hr * hr && distSq > 0) {
const dist = Math.sqrt(distSq);
const nx = ex / dist;
const ny = ey / dist;
const pen = hr - dist;
r.cx += nx * pen;
r.cy += ny * pen;
const dot = r.vx * nx + r.vy * ny;
if (dot < 0) {
r.vx -= 2 * dot * nx;
r.vy -= 2 * dot * ny;
const spd = Math.sqrt(r.vx * r.vx + r.vy * r.vy);
if (spd > 0) {
r.vx = (r.vx / spd) * ROACH_WALL_SPEED;
r.vy = (r.vy / spd) * ROACH_WALL_SPEED;
}
jitterVelocity(r);
bounced = true;
}
}
}
if (bounced) {
if (getTime() - lastFlapTime >= 0.2) {
lastFlapTime = getTime();
Pip.audioStart(SFX.FLAP);
}
const vxAbs = r.vx < 0 ? -r.vx : r.vx;
const vyAbs = r.vy < 0 ? -r.vy : r.vy;
let fixed = 0;
if (r.cx - hr <= 19) {
if (vxAbs < 1.5) {
r.vx = 1.5;
fixed = 1;
}
} else if (r.cx + hr >= 464) {
if (vxAbs < 1.5) {
r.vx = -1.5;
fixed = 1;
}
}
if (r.cy - hr <= 19) {
if (vyAbs < 1.5) {
r.vy = 1.5;
fixed = 1;
}
} else if (r.cy + hr >= 297) {
if (vyAbs < 1.5) {
r.vy = -1.5;
fixed = 1;
}
}
if (fixed) {
const spd = Math.sqrt(r.vx * r.vx + r.vy * r.vy);
if (spd > 0) {
r.vx = (r.vx / spd) * ROACH_WALL_SPEED;
r.vy = (r.vy / spd) * ROACH_WALL_SPEED;
}
}
}
}
function checkRoachCollisions() {
'ram';
for (let i = 0; i < radroaches.length; i++) {
for (let j = i + 1; j < radroaches.length; j++) {
const a = radroaches[i];
const b = radroaches[j];
const rSum = ROACH_R_SUM;
const dx = a.cx - b.cx;
const dy = a.cy - b.cy;
const distSq = dx * dx + dy * dy;
if (distSq < rSum * rSum && distSq > 0) {
const dist = Math.sqrt(distSq);
const nx = dx / dist,
ny = dy / dist;
const overlap = rSum - dist;
a.cx += nx * overlap * 0.5;
a.cy += ny * overlap * 0.5;
b.cx -= nx * overlap * 0.5;
b.cy -= ny * overlap * 0.5;
const relVel = (a.vx - b.vx) * nx + (a.vy - b.vy) * ny;
if (relVel >= 0) continue;
const aN = a.vx * nx + a.vy * ny;
const bN = b.vx * nx + b.vy * ny;
a.vx += (bN - aN) * nx;
a.vy += (bN - aN) * ny;
b.vx += (aN - bN) * nx;
b.vy += (aN - bN) * ny;
let sMag = Math.sqrt(a.vx * a.vx + a.vy * a.vy);
if (sMag > 0) {
a.vx = (a.vx / sMag) * ROACH_SPEED;
a.vy = (a.vy / sMag) * ROACH_SPEED;
}
sMag = Math.sqrt(b.vx * b.vx + b.vy * b.vy);
if (sMag > 0) {
b.vx = (b.vx / sMag) * ROACH_SPEED;
b.vy = (b.vy / sMag) * ROACH_SPEED;
}
jitterVelocity(a);
jitterVelocity(b);
if (getTime() - lastFlapTime >= 0.2) {
lastFlapTime = getTime();
Pip.audioStart(SFX.FLAP);
}
}
}
}
}
function updatePhysics() {
'ram';
if (gameState !== 'RACING') return;
if (trackDirty) {
trackDirty = 0;
h.setColor(0).fillRect(0, 0, 480, 320);
drawTrack();
drawAllRoaches();
h.flip();
Pip.lastFlip = getTime();
return;
}
let dIdx = 0;
h.setColor(0);
const hr = ROACH_HIT_R;
for (let i = 0; i < radroaches.length; i++) {
const r = radroaches[i];
const dx1 = r.cx - hr - 4,
dy1 = r.cy - hr - 4;
const dx2 = r.cx + hr + 4,
dy2 = r.cy + hr + 4;
fillRectCached(dx1, dy1, dx2, dy2);
dirty[dIdx++] = dx1;
dirty[dIdx++] = dy1;
dirty[dIdx++] = dx2;
dirty[dIdx++] = dy2;
}
for (let i = 0; i < radroaches.length; i++) {
const r = radroaches[i];
r.cx += r.vx;
r.cy += r.vy;
checkWallCollision(r);
}
checkRoachCollisions();
let minX = dirty[0],
minY = dirty[1],
maxX = dirty[2],
maxY = dirty[3];
for (let i = 4; i < 20; i += 4) {
if (dirty[i] < minX) minX = dirty[i];
if (dirty[i + 1] < minY) minY = dirty[i + 1];
if (dirty[i + 2] > maxX) maxX = dirty[i + 2];
if (dirty[i + 3] > maxY) maxY = dirty[i + 3];
}
h.setClipRect(minX, minY, maxX, maxY);
drawTrack(minX, minY, maxX, maxY);
h.setClipRect(0, 0, 480, 320);
const goalX1 = goalPos.x - hr,
goalX2 = goalPos.x + goalPos.hitW + hr;
const goalY1 = goalPos.y - hr,
goalY2 = goalPos.y + goalPos.hitH + hr;
for (let i = 0; i < radroaches.length; i++) {
const r = radroaches[i];
if (
r.cx >= goalX1 &&
r.cx <= goalX2 &&
r.cy >= goalY1 &&
r.cy <= goalY2
) {
gameState = 'GAMEOVER';
winnerId = r.id;
Pip.audioStart(SFX.WINNER);
break;
}
}
drawAllRoaches();
if (gameState === 'GAMEOVER') {
displayWinner();
}
h.flip();
Pip.lastFlip = getTime();
}
function displayWinner() {
h.setColor(0)
.fillRect(120, 130, 360, 190)
.setColor(3)
.drawRect(122, 132, 358, 188);
h.setFont('Monofonto16')
.setFontAlign(0, -1)
.setColor(3)
.drawString('"' + ROACH_NAMES[winnerId] + '" ROACH WINS!!!', 240, 140)
.setFont('Monofonto14')
.drawString('Press left wheel to race again!', 240, 167);
}
// ─── Main Loop ────────────────────────────────────────────────────────────
function mainLoop() {
'ram';
if (gameState === 'RACING') {
updatePhysics();
}
}
/**
* Handle the left scroll wheel input
* @param dir The direction of the scroll wheel
* @returns {void} Nothing
*/
function onKnob1(dir) {
if (dir !== 0) return;
if (gameState === 'GAMEOVER') {
app.go(app.scenes.TITLE);
}
}
// ─── Init ─────────────────────────────────────────────────────────────────
loadTrack();
// Roach image
// Head (top of image) faces the direction of travel.
try {
const data = JSON.parse(fs.readFileSync('HOLO/RADROACH_RACES/ROACH.JSON'));
roachImage = {
bpp: data.bpp,
buffer: atob(data.buffer),
height: data.height,
transparent: data.transparent,
width: data.width,
};
} catch (e) {
roachImage = null;
}
Pip.onExclusive('knob1', onKnob1);
countdownTimer = setInterval(tickCountdown, 1000);
tickCountdown();
mainLoopInterval = setInterval(mainLoop, 33); // ~30fps
return {
remove: function () {
if (mainLoopInterval) {
clearInterval(mainLoopInterval);
mainLoopInterval = null;
}
if (countdownTimer) {
clearInterval(countdownTimer);
countdownTimer = null;
}
Pip.removeListener('knob1', onKnob1);
},
};
});