-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
608 lines (568 loc) · 13.7 KB
/
index.html
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
<!doctype html>
<html lang="en">
<head>
<title>SMB3 Physics</title>
<style>
body, html {
background-color: #000;
margin: 0;
padding: 0;
color: #aaa;
text-align: center;
font-family: monospace;
font-size: 18px;
}
a {
color: #fff;
text-decoration: none;
}
a:hover, a:active {
text-decoration: underline;
}
#cnv {
display: block;
width: 768px;
height: 448px;
margin: 20px auto;
border-radius: 5px;
}
button {
width: 70px;
font-size: 15px;
font-family: monospace;
}
</style>
</head>
<body>
<canvas id="cnv" width="1536" height="896"></canvas>
<p>Controls: Arrow keys, X (Jump), Z (Run), Enter (Size), Mouse (Draw)</p>
<p>
Speed:
<button type="button" onClick="targetFPS=15">
0.25x
</button>
<button type="button" onClick="targetFPS=30">
0.5x
</button>
<button type="button" onClick="targetFPS=45">
0.75x
</button>
<button type="button" onClick="targetFPS=60">
1x
</button>
<button type="button" onClick="targetFPS=90">
1.5x
</button>
Zoom:
<button type="button" onClick="zoom(1)">
+
</button>
<button type="button" onClick="zoom(-1)">
−
</button>
</p>
<p>SMB3 Physics <a href="https://github.com/velipso/smb3-physics">Source Code</a> /
<a href="https://sean.fun/">Home</a></p>
<p>Based on <a href="http://www.sonicepoch.com/sm3mix/">Southbird</a>
disassembly on <a href="https://github.com/captainsouthbird/smb3">GitHub</a></p>
<script>
const cnv = document.getElementById('cnv');
const ctx = cnv.getContext('2d');
let dpr = 4;
ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
function zoom(zin) {
dpr = Math.max(4, dpr += zin);
ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
}
let mpos = [-1, -1];
let mdown = 0;
const inputEmpty = () => ({
up: false,
right: false,
down: false,
left: false,
a: false,
b: false,
start: false,
select: false,
});
const inputKeys = ['up', 'right', 'down', 'left', 'a', 'b', 'start', 'select'];
const keyboardMap = {
ArrowUp: 'up',
ArrowRight: 'right',
ArrowDown: 'down',
ArrowLeft: 'left',
Enter: 'start',
Backspace: 'select',
z: 'b',
x: 'a',
' ': 'a',
};
let input = inputEmpty();
let hit = inputEmpty();
let drawFuncs = [];
function highlightPoint(sx, sy, horiz) {
drawFuncs.push(() => {
ctx.save();
ctx.globalAlpha = 1;
ctx.fillStyle = horiz ? '#0f0' : '#f00';
ctx.fillRect(sx, sy, 1, 1);
ctx.restore();
});
}
function highlightTile(tx, ty, horiz) {
tx *= 16;
ty *= 16;
const n = 0.5 + 0.5/4;
drawFuncs.push(() => {
ctx.save();
ctx.beginPath();
if (horiz) {
ctx.moveTo(tx + n, ty + n);
ctx.lineTo(tx + n, ty + n + 15);
ctx.moveTo(tx + n + 15, ty + n);
ctx.lineTo(tx + n + 15, ty + n + 15);
} else {
ctx.moveTo(tx + n, ty + n);
ctx.lineTo(tx + n + 15, ty + n);
ctx.moveTo(tx + n, ty + n + 15);
ctx.lineTo(tx + n + 15, ty + n + 15);
}
ctx.strokeStyle = horiz ? '#0f0' : '#f00';
ctx.globalAlpha = 0.3;
ctx.stroke();
ctx.restore();
});
}
function drawScreen({
playerX,
playerY,
leftEdgeOffset,
rightEdgeOffset,
heightOffset,
}) {
for (let y = 0; y < world.length; y++) {
const row = world[y];
for (let x = 0; x < row.length; x++) {
const ch = row[x];
const isSolid = ch === '#';
ctx.fillStyle = isSolid ? '#aaa' : '#333';
ctx.beginPath();
ctx.rect(
x * 16,
y * 16,
16, 16
);
ctx.fill();
}
}
ctx.fillStyle = '#0ff';
ctx.fillRect(
Math.floor(playerX) + leftEdgeOffset,
Math.floor(playerY) + heightOffset + 1,
rightEdgeOffset - leftEdgeOffset,
31 - heightOffset
);
for (const f of drawFuncs) {
f();
}
ctx.fillStyle = '#fff';
ctx.fillRect(Math.floor(playerX), Math.floor(playerY), 1, 1);
ctx.strokeStyle = '#ddd';
ctx.beginPath();
ctx.rect(mpos[0] * 16, mpos[1] * 16, 16, 16);
ctx.stroke();
}
function start() {
let frameCount = 0;
let nextFrame = Date.now();
const run = () => {
const now = Date.now();
let i = 0;
while (nextFrame < now) {
drawFuncs = [];
tick();
hit = inputEmpty();
frameCount++;
nextFrame += 1000 / targetFPS;
i++;
if (i >= 4) {
// too slow :-(
nextFrame = Date.now();
}
}
setTimeout(run, 0);
}
run();
}
function onKey(key, down){
const k = keyboardMap[key];
if (k) {
if (!input[k] && down) {
hit[k] = true;
}
input[k] = down;
return true;
}
return false;
}
function onKeyDown(e){
if (onKey(e.key, true)) {
e.preventDefault();
e.stopPropagation();
return false;
}
}
function onKeyUp(e){
if (onKey(e.key, false)) {
e.preventDefault();
e.stopPropagation();
return false;
}
}
function onBlur(){
input = inputEmpty();
hit = inputEmpty();
}
function mousePos(e) {
const rect = cnv.getBoundingClientRect();
return [
Math.floor((e.clientX - rect.left) / (8 * dpr)),
Math.floor((e.clientY - rect.top) / (8 * dpr))
];
}
function onMouseMove(e, hitDown, isOut) {
mpos = isOut ? [-1, -1] : mousePos(e);
const get = () => world[mpos[1]][mpos[0]];
const set = (v) => world[mpos[1]][mpos[0]] = v;
if (hitDown) {
mdown = get() === '#' ? 1 : 2;
}
if (mdown) {
set(mdown === 1 ? ' ' : '#');
}
}
window.addEventListener('keydown', onKeyDown);
window.addEventListener('keyup', onKeyUp);
window.addEventListener('blur', onBlur);
cnv.addEventListener('mousemove', (e) => {
onMouseMove(e, false, false);
});
cnv.addEventListener('mouseout', (e) => {
mdown = 0;
onMouseMove(e, false, true);
});
cnv.addEventListener('mousedown', (e) => {
onMouseMove(e, true, false);
});
cnv.addEventListener('mouseup', (e) => {
mdown = 0;
onMouseMove(e, false, false);
});
//
// Main code
//
var targetFPS = 60;
let playerX = 20;
let playerY = 32;
let playerDX = 0;
let playerDY = 0;
let playerInAir = false;
let playerIsBig = true;
let playerIsDucking = false;
const GRAVITY_SLOW = 1;
const GRAVITY_FAST = 5;
const JUMP_FORCE = [-3.5, -3.625, -3.75, -4];
const world = `
########################
# #
# #
# #
# ###
# ## #
# #
# ########### #
# ##
## ##
## ### ##
## ### ##
## ##
########################
`.trim().split('\n').map(n => n.split(''));
const tileCollisionOffsets = {
big: {
downLeft: {
// Not small or ducking moving downward - Left half
vert: [
{ x: 0x04, y: 0x20 }, // Ground left
{ x: 0x0B, y: 0x20 }, // Ground right
],
horiz: [
{ x: 0x0E, y: 0x1B }, // In-front lower
{ x: 0x0E, y: 0x0E }, // In-front upper
],
},
downRight: {
// Not small or ducking moving downward - Right half
vert: [
{ x: 0x04, y: 0x20 }, // Ground left
{ x: 0x0B, y: 0x20 }, // Ground right
],
horiz: [
{ x: 0x01, y: 0x1B }, // In-front lower
{ x: 0x01, y: 0x0E }, // In-front upper
],
},
upLeft: {
// Not small or ducking moving upward - Left half
vert: [
{ x: 0x08, y: 0x06 }, // Ground left
{ x: 0x08, y: 0x06 }, // Ground right
],
horiz: [
{ x: 0x0E, y: 0x1B }, // In-front lower
{ x: 0x0E, y: 0x0E }, // In-front upper
],
},
upRight: {
// Not small or ducking moving upward - Right half
vert: [
{ x: 0x08, y: 0x06 }, // Ground left
{ x: 0x08, y: 0x06 }, // Ground right
],
horiz: [
{ x: 0x01, y: 0x1B }, // In-front lower
{ x: 0x01, y: 0x0E }, // In-front upper
],
},
},
small: {
downLeft: {
// Small or ducking moving downward - Left half
vert: [
{ x: 0x04, y: 0x20 }, // Ground left
{ x: 0x0B, y: 0x20 }, // Ground right
],
horiz: [
{ x: 0x0D, y: 0x1B }, // In-front lower
{ x: 0x0D, y: 0x14 }, // In-front upper
],
},
downRight: {
// Small or ducking moving downward - Right half
vert: [
{ x: 0x04, y: 0x20 }, // Ground left
{ x: 0x0B, y: 0x20 }, // Ground right
],
horiz: [
{ x: 0x02, y: 0x1B }, // In-front lower
{ x: 0x02, y: 0x14 }, // In-front upper
],
},
upLeft: {
// Small or ducking moving upward - Left half
vert: [
{ x: 0x08, y: 0x10 }, // Ground left
{ x: 0x08, y: 0x10 }, // Ground right
],
horiz: [
{ x: 0x0D, y: 0x1B }, // In-front lower
{ x: 0x0D, y: 0x14 }, // In-front upper
],
},
upRight: {
// Small or ducking moving upward - Right half
vert: [
{ x: 0x08, y: 0x10 }, // Ground left
{ x: 0x08, y: 0x10 }, // Ground right
],
horiz: [
{ x: 0x02, y: 0x1B }, // In-front lower
{ x: 0x02, y: 0x14 }, // In-front upper
],
},
}
};
function mod16(n) {
// `n % 16` doesn't work well for negative numbers, so calculate euclidian mod instead:
return n - Math.floor(n / 16) * 16;
}
function tick() {
if (hit.start) {
playerIsBig = !playerIsBig;
}
if (!playerIsBig) {
// disable any ducking
playerIsDucking = false;
} else if (!playerInAir) {
// set player ducking if they hit ONLY down
playerIsDucking =
!input.left &&
!input.right &&
!input.up &&
input.down;
}
let heightOffset = !playerIsBig || playerIsDucking
? 20
: 10;
const tileAbove = getTileFromPlayer({ x: 8, y: heightOffset }, false);
const lowClearance = tileAbove && !playerInAir;
if (lowClearance) {
// player is stuck in wall
playerDX = 0;
playerX++;
}
// advance player
playerX += Math.max(-4, Math.min(4, playerDX));
if (playerInAir) {
playerY += Math.min(4, playerDY);
}
// handle horizontal movement
const topSpeed = input.b ? 2.5 : 1.5;
const accelFric = playerIsBig ? 14 : 10;
const accelSkid = 32;
const accelNormal = 14;
const hitDir = input.left ? -1 : input.right ? 1 : 0;
if (hitDir === 0) {
if (!playerInAir) {
if (playerDX < 0) {
playerDX += accelFric / 256;
if (playerDX > 0) {
playerDX = 0;
}
} else if (playerDX > 0) {
playerDX -= accelFric / 256;
if (playerDX < 0) {
playerDX = 0;
}
}
}
} else {
const absDX = Math.abs(playerDX)
if (
(playerDX > 0 && hitDir < 0) ||
(playerDX < 0 && hitDir > 0)
) {
// player is voluntarily slowing down (skidding), so let them!
playerDX += hitDir * accelSkid / 256;
} else if (absDX < topSpeed) {
playerDX += hitDir * accelNormal / 256;
} else if (absDX > topSpeed) {
if (!playerInAir) {
playerDX -= hitDir * accelFric / 256;
}
}
}
// handle vertical movement
if (hit.a && !playerInAir) {
const dx = Math.floor(Math.abs(playerDX));
playerDY = JUMP_FORCE[dx];
playerInAir = true;
}
if (playerInAir) {
if (playerDY < -2 && input.a) {
playerDY += GRAVITY_SLOW / 16;
} else {
playerDY += GRAVITY_FAST / 16;
}
}
// collision detection setup
const playerIsMovingUp = playerDY < 0;
const playerIsLeftHalf = mod16(playerX) < 8;
const colOffsets = !playerIsBig || playerIsDucking
? tileCollisionOffsets.small
: tileCollisionOffsets.big;
const { vert: tVert, horiz: tHoriz } = playerIsMovingUp
? (
playerIsLeftHalf
? colOffsets.upLeft
: colOffsets.upRight
) : (
playerIsLeftHalf
? colOffsets.downLeft
: colOffsets.downRight
);
const solidVert1 = getTileFromPlayer(tVert[0], false);
const solidVert2 = getTileFromPlayer(tVert[1], false);
const solidVert = solidVert1 || solidVert2;
const solidHoriz1 = getTileFromPlayer(tHoriz[0], true);
const solidHoriz2 = getTileFromPlayer(tHoriz[1], true);
const solidHoriz = solidHoriz1 || solidHoriz2;
// horizontal collision detection
const leftEdgeOffset = playerIsBig ? 2 : 3;
const rightEdgeOffset = playerIsBig ? 14 : 13;
if (solidHoriz && !lowClearance) {
const dir = playerIsLeftHalf ? -1 : 1;
const edx = playerIsLeftHalf
? leftEdgeOffset
: rightEdgeOffset;
const edgeX = playerX + edx;
const localX = mod16(edgeX);
if (Math.floor(localX) !== 0) {
// eject player from wall
playerX += dir;
if (
(playerDX < 0 && dir === 1) ||
(playerDX >= 0 && dir === -1)
) {
playerDX = 0;
}
}
}
// vertical collision detection
if (playerDY >= 0 || !playerInAir) {
if (solidVert) {
// on solid ground
const localY = mod16(Math.floor(playerY));
if (localY < 6) {
// snap to ground
if (localY === 1) {
playerY--;
} else if (localY !== 0) {
playerY -= 2;
}
playerInAir = false;
playerDY = 0;
}
} else if (!playerInAir) {
// walked off ledge, so now in air
playerDY = 0;
playerInAir = true;
}
} else {
// moving up or in air
heightOffset = tVert[0].y;
if (solidVert) { // hit head on something
playerDY = GRAVITY_SLOW / 16;
}
}
drawScreen({
playerX,
playerY,
leftEdgeOffset,
rightEdgeOffset,
heightOffset,
});
}
function getTileFromPlayer({ x: dx, y: dy }, horiz) {
const sx = Math.floor(playerX) + dx;
const sy = Math.floor(playerY) + dy;
highlightPoint(sx, sy, horiz);
const tx = Math.floor(sx / 16);
let ty = Math.floor(sy / 16);
if (ty < 0) {
ty = 0;
} else if (ty >= world.length) {
return true;
}
const row = world[ty];
if (tx < 0 || tx >= row.length) {
return true;
}
//highlightTile(tx, ty, horiz);
return row[tx] === '#';
}
start();
</script>
</body>
</html>