Public: anyone can read this; the crew can change it.
Migrated from github.com/amirmushichge/pixel-plays
Add Tug of War: pull in rhythm with your team to drag the rope's ribbon over your line at the village fete #97
Check · Review · Land · Live done — Landed and live in production.
quill/pixel-plays:quill/76-add-tug-of-war-pull-in-rhythm-with-your → main 5 commits fork 3 threads · 3 unresolved
10 files
changed +713
−0
README.md +1
| @@ -41,6 +41,7 @@ Concept by [abhi](https://x.com/awwbhi2). Game by [AmirMushich](https://x.com/Am | ||
| 41 | 41 | | [](espresso-bar/) | Twenty cups slide past under an espresso spout, each with a painted fill line and an order card (espresso a short line, latte a tall one): hold to pour, and the stream keeps falling for a quarter second after you let go; within 3% of the line scores 3, within 10% scores 1, over the brim spills and costs a life, and a cup that passes untouched costs a life; cups come faster as the round goes, three lives end it (forged by the crew on the engine and shell above, not by the original creators) | Hold space, or press and hold the canvas | |
| 42 | 42 | | [](goalkeeper/) | Ten penalties in a village football ground: each taker runs up and their hips and plant foot lean toward the side they will shoot (arms spread wide is down the middle), readable for a short tell window before the kick that shrinks with every penalty; tap the left third of the canvas to dive left, the right third to dive right and the middle third to stay up, and a dive to the right side before the ball arrives saves it, while diving before the tell window opens lets the taker switch side half the time; seven saves wins; the concept is the ordinary football penalty, with no particular game as its source (forged by the crew on the engine and shell above, not by the original creators) | Tap left, middle or right; Left and Right arrows dive, Up stays | |
| 43 | 43 | | [](soapbox-derby/) | A 360 m cobbled hill in a wooden cart that rolls and gathers speed by itself: steer between the gaps in gates of hay bales that narrow down the hill (the cart answers late, so steer early); a bale slows it hard and costs one of three lives, a smooth flagstone strip gives a short boost, and the banner scores your time against your best (forged by the crew on the engine and shell above, not by the original creators) | Drag left or right; left/right arrows | |
| 44 | | [](tug-of-war/) | A village fete: two teams of four on a rope, a ribbon tied at its middle and a chalk line at each end. A caller beats a steady heave shown as a pulsing marker; a tap within 150 ms of the beat pulls the ribbon towards your line, a tap well off it (over 300 ms) makes your team stumble and the rope slips the other way, and not tapping lets the rival pull steadily, stronger each round. The ribbon crossing your line wins the pull, crossing theirs loses it; five pulls, three won is a win. The marker shows as a lit dot without animation under reduced motion, so it plays muted too (forged by the crew on the engine and shell above, not by the original creators; rules in `tug-of-war/rope.js`) | Tap anywhere on the picture; space | | |
| 44 | 45 | | [](sheepdog/) | A 90-second dusk over three hillside fields, five sheep each, scattered at the top: drag the dog (or use the arrow keys) so they flee it inside a set radius, flock together and wander when calm, and push one too hard and it bolts the wrong way; each sheep that goes through the dry-stone wall's gate and then into the hurdle pen scores, one that goes round the gate scores nothing once penned; the gate moves every field, the sun lowers with the clock, and 10 or more of 15 wins (forged by the crew on the engine and shell above, not by the original creators) | Drag the dog; arrow keys | |
| 45 | 46 | | [](firefly-jar/) | A 90-second summer night in a back garden: tap a firefly while it glows to catch it into the jar (+1, golden +3), a dim one is scared off and breaks the streak, shoo the moths before they reach the lantern; the third moth ends the night (forged by the crew on the engine and shell above, not by the original creators) | Tap; Tab or arrows + space | |
| 46 | 47 |
index.html +1
| @@ -91,6 +91,7 @@ footer a::after { content: none; } | ||
| 91 | 91 | <li data-kind="Precision" data-input="Hold" style="--accent:#6f4a33"><a href="espresso-bar/">Espresso Bar</a><span>Pour twenty coffees to the line as cups slide past: hold to pour, let go early, over the brim costs a life</span><span>Game forged by the crew</span><span class="tags">Precision · Hold</span></li> |
| 92 | 92 | <li data-kind="Sport" data-input="Tap" style="--accent:#3f7a3a"><a href="goalkeeper/">Goalkeeper</a><span>Read the taker and dive left or right to save ten penalties</span><span>Game forged by the crew</span><span class="tags">Sport · Tap</span></li> |
| 93 | 93 | <li data-kind="Racing" data-input="Drag" style="--accent:#b5562b"><a href="soapbox-derby/">Soapbox Derby</a><span>Steer a wooden cart down a cobbled hill between the hay bales, fastest time wins</span><span>Game forged by the crew</span><span class="tags">Racing · Drag</span></li> |
| 94 | <li data-kind="Sport" data-input="Tap" style="--accent:#c2402f"><a href="tug-of-war/">Tug of War</a><span>Tap on the caller's beat to heave the ribbon over your line, five pulls at the fete</span><span>Game forged by the crew</span><span class="tags">Sport · Tap</span></li> | |
| 94 | 95 | <li data-kind="Catching" data-input="Drag" style="--accent:#5b8a3a"><a href="sheepdog/">Sheepdog</a><span>Drag the dog to steer five sheep through the gate and into the pen before dusk</span><span>Game forged by the crew</span><span class="tags">Catching · Drag</span></li> |
| 95 | 96 | <li data-kind="Catching" data-input="Tap" style="--accent:#c9b43a"><a href="firefly-jar/">Firefly Jar</a><span>Tap the fireflies into a jar while they glow, a dim one flies off, shoo the moths before they reach the lantern</span><span>Game forged by the crew</span><span class="tags">Catching · Tap</span></li> |
| 96 | 97 | </ul> |
og/home.png
Binary file not shown.
og/tug-of-war.png added
Binary file not shown.
rounds.mjs +27
| @@ -81,6 +81,28 @@ export const ROUNDS = { | ||
| 81 | 81 | } }, |
| 82 | 82 | 'stari-most': { over: /dive again/, outcome: /total [\d.]+/, beat: g => { g.hold(...MID, .4); g.run(.5); g.hold(...MID, .6); g.run(2); g.tap(...MID); } }, |
| 83 | 83 | 'stone-skip': { over: /play again/, outcome: /\d+ bounces/, beat: g => { g.drag([180, 140], [140, 170], .4); g.run(3); g.tap(...MID); } }, |
| 84 | // pour each cup to its line: start as it slides under the spout, hold just long enough for the stream's tail to finish it | |
| 85 | 'espresso-bar': { over: /out of lives|twenty cups poured/, outcome: /(out of lives|twenty cups poured) · [1-9]\d* · /, beat: g => { | |
| 86 | if (g.read('R.phase') === 'play' && !g.read('R.done') && g.read('R.fill') === 0 && g.read('R.x') > -.9) { | |
| 87 | g.press(' ', g.read('Pour.lineOf(R.cup)') / g.read('Pour.RATE') - g.read('Pour.DELAY')); | |
| 88 | } else g.step(1); | |
| 89 | } }, | |
| 90 | // tap a firefly while it glows | |
| 91 | 'firefly-jar': { over: /another night/, outcome: /[1-9]\d* in the jar/, beat: g => { | |
| 92 | const p = g.read('(() => { const f = R.flies.find(f => !f.fled && Night.glow(f).on); return f && scr(f.u, f.v, f.z); })()'); | |
| 93 | if (p) g.tap(p.sx, p.sy); else g.step(1); | |
| 94 | } }, | |
| 95 | // pull a wrong lever once the lamp has shown the sequence: three trains lost | |
| 96 | 'signal-box': { over: /run again/, outcome: /\d+ trains routed/, beat: g => { | |
| 97 | if (g.read('P.phase') === 'input') g.press(String((g.read('P.seq[P.got]') + 1) % 5 + 1)); | |
| 98 | g.run(.5); | |
| 99 | } }, | |
| 100 | // steer for the middle of the next gap, early, with the arrow keys | |
| 101 | 'soapbox-derby': { over: /roll again/, outcome: /banner! [\d.]+ s/, beat: g => { | |
| 102 | const want = g.read('(() => { for (let n = 0; n < Cart.GATES; n++) { const t = Cart.gateAt(n, R.seed); if (t.x + Cart.GATE_DEPTH > R.x - 2) return t.centre / Cart.LANE_MAX; } return 0; })()'); | |
| 103 | const err = want - g.read('G.steer'); | |
| 104 | if (Math.abs(err) > .04) g.press(err > 0 ? 'ArrowRight' : 'ArrowLeft', Math.min(.3, Math.abs(err) / 1.8)); else g.step(2); | |
| 105 | } }, | |
| 84 | 106 | // brake for the mark by the distance it takes to stop, drive off when the doors close |
| 85 | 107 | 'tram-line': { over: /drive again/, outcome: /end of the line · \+\d+ for time/, beat: g => { |
| 86 | 108 | const d = g.read('Line.distanceToMark(R)'), v = g.read('R.v'); |
| @@ -111,6 +133,11 @@ export const ROUNDS = { | ||
| 111 | 133 | g.step(1); |
| 112 | 134 | }; |
| 113 | 135 | })() }, |
| 136 | // heave when the beat lands, once per beat | |
| 137 | 'tug-of-war': { over: /play again|pull again/, outcome: /you (win|lose): \d of 5 pulls/, beat: g => { | |
| 138 | const { index, offset } = g.read('Rope.nearestBeat(R.t)'); | |
| 139 | if (g.read('R.phase') === 'pull' && Math.abs(offset) < .05 && g.read('R.lastBeat') !== index) g.press(' ', .04); else g.step(1); | |
| 140 | } }, | |
| 114 | 141 | 'window-washer': { over: /play again/, outcome: /[1-9]\d* of 500/, beat: g => g.drag([60, 100], [300, 100], .6) }, |
| 115 | 142 | }; |
| 116 | 143 |
serve.test.mjs +11
| @@ -448,3 +448,14 @@ test('every game page turns off text selection and the iOS touch callout', async | ||
| 448 | 448 | expect(html, d.name).toMatch(/html, body \{[^}]*user-select: none/); |
| 449 | 449 | } |
| 450 | 450 | }); |
| 451 | ||
| 452 | test('Tug of War and its rules script are served without a track', async () => { | |
| 453 | const page = await fetch(base + '/tug-of-war/'); | |
| 454 | expect(page.status).toBe(200); | |
| 455 | expect(page.headers.get('content-type')).toBe('text/html; charset=utf-8'); | |
| 456 | const rules = await fetch(base + '/tug-of-war/rope.js'); | |
| 457 | expect(rules.status).toBe(200); | |
| 458 | expect(rules.headers.get('content-type')).toMatch(/javascript/); | |
| 459 | expect(rules.headers.get('x-content-type-options')).toBe('nosniff'); | |
| 460 | expect(await page.text()).toContain('canvas:focus-visible'); | |
| 461 | }); |
shell.js +1
| @@ -35,6 +35,7 @@ window.PixelPlays = (() => { | ||
| 35 | 35 | { id: 'espresso-bar', title: 'Espresso Bar', blurb: 'Pour twenty coffees to the line as cups slide past: hold to pour, let go early, over the brim costs a life', kind: 'Precision', input: 'Hold', accent: '#6f4a33', music: false, forged: true }, |
| 36 | 36 | { id: 'goalkeeper', title: 'Goalkeeper', blurb: 'Read the taker and dive left or right to save ten penalties', kind: 'Sport', input: 'Tap', accent: '#3f7a3a', music: false, forged: true }, |
| 37 | 37 | { id: 'soapbox-derby', title: 'Soapbox Derby', blurb: 'Steer a wooden cart down a cobbled hill between the hay bales, fastest time wins', kind: 'Racing', input: 'Drag', accent: '#b5562b', music: false, forged: true }, |
| 38 | { id: 'tug-of-war', title: 'Tug of War', blurb: 'Tap on the caller\'s beat to heave the ribbon over your line, five pulls at the fete', kind: 'Sport', input: 'Tap', accent: '#c2402f', music: false, forged: true }, | |
| 38 | 39 | { id: 'sheepdog', title: 'Sheepdog', blurb: 'Drag the dog to steer five sheep through the gate and into the pen before dusk', kind: 'Catching', input: 'Drag', accent: '#5b8a3a', music: false, forged: true }, |
| 39 | 40 | { id: 'firefly-jar', title: 'Firefly Jar', blurb: 'Tap the fireflies into a jar while they glow, a dim one flies off, shoo the moths before they reach the lantern', kind: 'Catching', input: 'Tap', accent: '#c9b43a', music: false, forged: true }, |
| 40 | 41 | ]; |
tug-of-war/index.html added +400
| @@ -0,0 +1,400 @@ | ||
| 1 | ||
| 2 | <html lang="en"> | |
| 3 | <head> | |
| 4 | <meta charset="utf-8"> | |
| 5 | <meta name="viewport" content="width=device-width,initial-scale=1,viewport-fit=cover"> | |
| 6 | <title>Tug of War</title> | |
| 7 | <!-- og:begin --> | |
| 8 | <meta name="description" content="Tap on the caller's beat to heave the ribbon over your line, five pulls at the fete"> | |
| 9 | <link rel="canonical" href="https://pixel-plays--forge-trevato-apps.open-platform.sh/tug-of-war/"> | |
| 10 | <meta property="og:type" content="website"> | |
| 11 | <meta property="og:site_name" content="Pixel Plays"> | |
| 12 | <meta property="og:title" content="Tug of War"> | |
| 13 | <meta property="og:description" content="Tap on the caller's beat to heave the ribbon over your line, five pulls at the fete"> | |
| 14 | <meta property="og:url" content="https://pixel-plays--forge-trevato-apps.open-platform.sh/tug-of-war/"> | |
| 15 | <meta property="og:image" content="https://pixel-plays--forge-trevato-apps.open-platform.sh/og/tug-of-war.png"> | |
| 16 | <meta property="og:image:type" content="image/png"> | |
| 17 | <meta property="og:image:width" content="1200"> | |
| 18 | <meta property="og:image:height" content="630"> | |
| 19 | <meta property="og:image:alt" content="Tug of War: the opening scene of the game"> | |
| 20 | <meta name="twitter:card" content="summary_large_image"> | |
| 21 | <!-- og:end --> | |
| 22 | <style> | |
| 23 | :root { : #1c1b19; : #8b877f; : #fff; : #c2402f; } | |
| 24 | * { box-sizing: border-box; } | |
| 25 | html, body { -webkit-user-select: none; user-select: none; -webkit-touch-callout: none; -webkit-tap-highlight-color: transparent; } | |
| 26 | html, body { height: 100%; margin: 0; background: var(); color: var(); } | |
| 27 | body { font: 13px/1.35 ui-monospace, "Cascadia Mono", Consolas, Menlo, monospace; overflow: hidden; } | |
| 28 | #stage { position: fixed; inset: 0; display: grid; place-items: center; } | |
| 29 | canvas { display: block; image-rendering: pixelated; touch-action: none; cursor: pointer; outline: none; } | |
|
||
| 30 | canvas:focus-visible { outline: 2px solid var(); outline-offset: -2px; } | |
| 31 | .hud { position: fixed; left: max(16px, env(safe-area-inset-left)); right: max(16px, env(safe-area-inset-right)); display: flex; justify-content: space-between; gap: 16px; pointer-events: none; } | |
| 32 | #scores { position: fixed; top: max(14px, env(safe-area-inset-top)); left: max(16px, env(safe-area-inset-left)); display: grid; gap: 2px; pointer-events: none; } | |
| 33 | .row { display: flex; align-items: center; gap: 8px; color: var(); } | |
| 34 | .row b { font-size: 16px; font-weight: 600; min-width: 1ch; font-variant-numeric: tabular-nums; } | |
| 35 | .row span { color: var(); } | |
| 36 | #bottom { bottom: max(14px, env(safe-area-inset-bottom)); align-items: flex-end; } | |
| 37 | #status { color: var(); min-height: 1.35em; } | |
| 38 | button { pointer-events: auto; font: inherit; color: var(); background: none; border: 0; padding: 10px; margin: -10px; cursor: pointer; } | |
| 39 | button:hover, button:focus-visible { color: var(); } | |
| 40 | </style> | |
| 41 | </head> | |
| 42 | <body> | |
| 43 | <div id="stage"><canvas id="c" tabindex="0" aria-label="Tug of war: tap or press space on the beat to pull the ribbon to your line"></canvas></div> | |
| 44 | <div id="scores"> | |
| 45 | <div class="row"><b id="pull">1</b><span>pull of 5</span></div> | |
| 46 | <div class="row"><b id="ribbon">0.0</b><span>ribbon, m toward you</span></div> | |
| 47 | <div class="row"><b id="won">0</b><span>pulls won</span></div> | |
| 48 | <div class="row"><b id="best">0</b><span>best</span></div> | |
| 49 | </div> | |
| 50 | <div class="hud" id="bottom"> | |
| 51 | <div id="status" role="status" aria-live="polite"></div> | |
| 52 | <button id="mute" type="button" aria-pressed="false">sound on</button> | |
| 53 | </div> | |
| 54 | <script src="../shell.js"></script> | |
| 55 | <script> | |
| 56 | PixelPlays.mount({ | |
| 57 | id: 'tug-of-war', | |
| 58 | rules: [ | |
| 59 | 'Tap the canvas, or press space, as the caller\'s marker lands on the beat: each tap on the beat heaves the ribbon toward your line.', | |
| 60 | 'A tap well off the beat makes your team stumble and the rope slips the other way; not tapping lets the rival pull. The rival gets stronger every pull.', | |
| 61 | 'Five pulls. The ribbon over your line wins a pull, over the rival\'s loses it. Three pulls won is a win.', | |
| 62 | ], | |
| 63 | }); | |
| 64 | </script> | |
| 65 | <script>window.PP_SCENE = { originY: 10, shadowX: 1.3, shadowY: -.12 };</script> | |
| 66 | <script src="../engine.js"></script> | |
| 67 | <script src="rope.js"></script> | |
| 68 | <script> | |
| 69 | 'use strict'; | |
| 70 | ||
| 71 | const BEST_KEY = 'pixel-plays-tug-of-war-best'; | |
| 72 | const LINE = 6, TEAM_SHIFT = 1.4; // the chalk lines sit at a = -LINE (yours) and +LINE; the ribbon runs between them | |
| 73 | const ROPE_Z = 1.75, ROPE_END = 14.6, FIG = 1.2; | |
| 74 | const HALF_A = 17, HALF_B = 7.6; // the field block | |
| 75 | const PITCH = [[40, 74, 50], [52, 88, 52], [70, 106, 54], [88, 122, 56]]; | |
| 76 | const MOWN = [[60, 100, 56], [74, 118, 60], [92, 136, 64], [108, 150, 68]]; | |
| 77 | const CHALK = [238, 238, 228], ROPE = [196, 160, 100], RIBBON = [210, 52, 44]; | |
| 78 | const US = { shirt: [200, 62, 52], legs: [236, 232, 222] }, THEM = { shirt: [64, 118, 190], legs: [44, 50, 60] }; | |
| 79 | const SKIN = [[222, 170, 132], [176, 124, 92], [236, 190, 150], [150, 100, 72]]; | |
| 80 | const HAIR = [[74, 50, 34], [36, 30, 28], [196, 150, 70], [60, 40, 30]]; | |
| 81 | const FLAGS = [[200, 62, 52], [240, 232, 210], [64, 118, 190], [236, 196, 60]]; | |
| 82 | const CROWD = [[236, 196, 60], [150, 70, 160], [232, 140, 50], [240, 232, 210], [64, 118, 190], [200, 62, 52]]; | |
| 83 | // local field space: a runs across (screen right is +), b away from the far side toward the camera, z up; the world axes sit 45 degrees off | |
| 84 | const toWorld = (a, b, z) => [(a + b) / R2, (b - a) / R2, z]; | |
| 85 | const toLocal = (x, y) => ({ a: (x - y) / R2, b: (x + y) / R2 }); | |
| 86 | const mix = (p, q, t) => [lerp(p[0], q[0], t), lerp(p[1], q[1], t), lerp(p[2], q[2], t)]; | |
| 87 | const easeOut = t => 1 - (1 - t) * (1 - t); | |
| 88 | ||
| 89 | /* ---------- the field ---------- */ | |
| 90 | function chalkAt(a, b) { | |
| 91 | const w = .13; | |
| 92 | if (Math.abs(Math.abs(a) - LINE) < w && Math.abs(b) <= 4.2) return true; // the two lines | |
| 93 | return Math.abs(a) < w && Math.abs(b) <= .9 && Math.floor((b + 1) / .45) % 2 === 0; // dashes at the middle | |
| 94 | } | |
| 95 | function groundColor(x, y, ix, iy) { | |
| 96 | const { a, b } = toLocal(x, y); | |
| 97 | const edge = Math.pow(Math.pow(Math.abs(a) / HALF_A, 4) + Math.pow(Math.abs(b) / HALF_B, 4), .25); | |
| 98 | const rough = edge + (noise(a * 1.3, b * 1.3) - .5) * .1; | |
| 99 | if (rough > 1.04) return 0; | |
| 100 | if (rough > .94 && hash(ix, iy) < (rough - .94) / .1) return 0; | |
| 101 | if (chalkAt(a, b) && rough < .96) return rgb(...CHALK); | |
| 102 | const band = Math.floor((a + 40) / 2.2) % 2, fine = hash(ix, iy); | |
| 103 | const t = noise(a * .9, b * .9) * .6 + (fine - .5) * .3 + band * .28 + (bayerAt(ix, iy) - .5) * .25; | |
| 104 | return rgb(...(band ? MOWN : PITCH)[clamp(Math.floor(t * 3.4), 0, 3)]); | |
| 105 | } | |
| 106 | const POSTS = [[-15.2, -6], [15.2, -6]], STRING_Z = 4.6, SAG = 1.1; | |
| 107 | function bakePosts() { | |
| 108 | for (const [a, b] of POSTS) { | |
| 109 | capsule(toWorld(a, b, 0), toWorld(a, b, STRING_Z + .2), .09, .07, () => [128, 94, 62], 'solid'); | |
| 110 | sphere(...toWorld(a, b, STRING_Z + .3), .13, [226, 206, 140], 'solid'); | |
| 111 | for (let k = 0; k <= 20; k++) castShadow(...toWorld(a, b, (STRING_Z + .2) * k / 20), .1, 1, 0); | |
| 112 | } | |
| 113 | // the rope's iron pegs at either end | |
| 114 | for (const s of [-1, 1]) { | |
| 115 | capsule(toWorld(s * ROPE_END, 0, 0), toWorld(s * ROPE_END, 0, ROPE_Z), .1, .1, () => [128, 94, 62], 'solid'); | |
| 116 | for (let k = 0; k <= 8; k++) castShadow(...toWorld(s * ROPE_END, 0, ROPE_Z * k / 8), .1, 1, 0); | |
| 117 | } | |
| 118 | } | |
| 119 | function bake() { | |
| 120 | ground.fill(0); shadowMask.fill(0); staticShadow.fill(0); kinds.fill(0); | |
| 121 | zb.fill(-1e9); | |
| 122 | pix.fill(PAPER); | |
| 123 | for (let iy = 0; iy < H; iy++) for (let ix = 0; ix < W; ix++) { | |
| 124 | const g = unproject(ix + .5, iy + .5), i = iy * W + ix, c = groundColor(g.x, g.y, ix, iy); | |
| 125 | if (!c) continue; | |
| 126 | pix[i] = c; ground[i] = 1; | |
| 127 | zb[i] = (g.x + g.y) / R2 * COS; | |
| 128 | } | |
| 129 | groundZ.set(zb); | |
| 130 | bakePosts(); | |
| 131 | bakeOcclusion(); | |
| 132 | staticShadow.set(shadowMask); | |
| 133 | applyShadows(groundZ); | |
| 134 | bakedPix.set(pix); | |
| 135 | bakedZ.set(zb); | |
| 136 | } | |
| 137 | ||
| 138 | /* ---------- state ---------- */ | |
| 139 | const best0 = (() => { try { return Number(localStorage.getItem(BEST_KEY)) || 0; } catch (e) { return 0; } })(); | |
| 140 | let R = Rope.newGame(best0); | |
| 141 | const G = { time: 0, status: '', noteUntil: 0, heaveAt: -9, stumbleAt: -9, tickBeat: 0 }; | |
| 142 | const nodes = {}; | |
| 143 | for (const id of ['pull', 'ribbon', 'won', 'best', 'status']) nodes[id] = document.getElementById(id); | |
| 144 | let shown = ''; | |
| 145 | function renderHud() { | |
| 146 | const text = [R.pull, R.won, Math.max(R.best, R.won), Math.round(R.ribbon * LINE * 10)].join('|'); | |
| 147 | if (text === shown) return; | |
| 148 | shown = text; | |
| 149 | nodes.pull.textContent = Math.min(R.pull + 1, Rope.PULLS); | |
| 150 | nodes.ribbon.textContent = (R.ribbon > 0 ? '+' : '') + (R.ribbon * LINE).toFixed(1); | |
| 151 | nodes.won.textContent = R.won; | |
| 152 | nodes.best.textContent = Math.max(R.best, R.won); | |
| 153 | } | |
| 154 | function setStatus(text) { | |
| 155 | if (text === G.status) return; | |
| 156 | G.status = text; | |
| 157 | nodes.status.textContent = text; | |
| 158 | } | |
| 159 | function note(text) { setStatus(text); G.noteUntil = G.time + 1.4; } | |
| 160 | const HINT = 'tap or press space as the marker lands'; | |
| 161 | ||
| 162 | /* ---------- people ---------- */ | |
| 163 | const add = (a, b, r0, r1, color) => dynamicAdd(a, b, r0, r1, () => color); | |
| 164 | // one figure in field space. o: roll (radians, + tilts toward screen right), shift, crouch, stride (-1..1), hands [[la, z], [la, z]] | |
| 165 | function person(base, o) { | |
| 166 | const bone = (p, q, r0, r1, c) => add(p, q, r0 * FIG, r1 * FIG, c); | |
| 167 | const pal = o.pal, roll = o.roll || 0, cs = Math.cos(roll), sn = Math.sin(roll), z0 = .1, crouch = o.crouch || 0; | |
| 168 | const at = (la, lb, z) => { | |
| 169 | const dz = z - z0; | |
| 170 | return toWorld(base.a + (o.shift || 0) + (la * cs + dz * sn) * FIG, base.b + lb * FIG, Math.max(.12, (z0 + dz * cs - la * sn) * FIG)); | |
| 171 | }; | |
| 172 | const hipZ = 1.3 - crouch * .5, stride = o.stride || 0; | |
| 173 | const hipL = at(-.17, 0, hipZ), hipR = at(.17, 0, hipZ); | |
| 174 | const footL = at(-.2 + stride * .6, 0, .12), footR = at(.2 + stride * .6, 0, .12); | |
| 175 | const kneeL = mix(hipL, footL, .5), kneeR = mix(hipR, footR, .5); | |
| 176 | kneeL[1] += .12; kneeR[1] += .12; | |
| 177 | bone(hipL, kneeL, .17, .13, pal.legs); bone(kneeL, footL, .13, .1, pal.legs); | |
| 178 | bone(hipR, kneeR, .17, .13, pal.legs); bone(kneeR, footR, .13, .1, pal.legs); | |
| 179 | bone(footL, [footL[0] + .08, footL[1] + .08, footL[2] - .03], .1, .09, SHOE); | |
| 180 | bone(footR, [footR[0] + .08, footR[1] + .08, footR[2] - .03], .1, .09, SHOE); | |
| 181 | const mid = at(0, 0, hipZ), neck = at(0, 0, 2.2 - crouch * .35), head = at(0, 0, 2.58 - crouch * .4); | |
| 182 | bone(mid, neck, .3, .32, pal.shirt); | |
| 183 | bone(neck, head, .1, .11, pal.skin); | |
| 184 | bone(head, head, .25, .25, pal.skin); | |
| 185 | bone([head[0] - .06, head[1] - .06, head[2] + .13], [head[0] - .06, head[1] - .06, head[2] + .13], .22, .22, pal.hair); | |
| 186 | const shZ = 2.1 - crouch * .35; | |
| 187 | o.hands.forEach(([la, z], k) => { | |
| 188 | const sh = at((k ? 1 : -1) * .38, 0, shZ), hand = at(la, 0, z); | |
| 189 | bone(sh, mix(sh, hand, .5), .12, .1, pal.shirt); | |
| 190 | bone(mix(sh, hand, .5), hand, .09, .08, pal.skin); | |
| 191 | }); | |
| 192 | } | |
| 193 | // four on a side: team side -1 is yours (left, pulling left), +1 the rival's | |
| 194 | function drawTeam(side, pal, lean, crouch, beat) { | |
| 195 | for (let k = 0; k < 4; k++) { | |
| 196 | const a = side * (LINE + 2.3 + k * 1.75) + shiftA(), b = (k % 2 ? .35 : -.2); | |
| 197 | const stride = Math.sin(k * 1.9 + beat * 3) * .12; | |
| 198 | person({ a, b }, { | |
| 199 | pal: { ...pal, skin: SKIN[(k + (side > 0 ? 2 : 0)) % 4], hair: HAIR[(k * 3 + (side > 0 ? 1 : 0)) % 4] }, | |
| 200 | roll: side * lean, crouch, stride, | |
| 201 | hands: [[-side * .9 - .25, 1.5], [-side * .9 + .25, 1.5]], | |
| 202 | }); | |
| 203 | } | |
| 204 | } | |
| 205 | const shiftA = () => -R.ribbon * TEAM_SHIFT; | |
| 206 | ||
| 207 | /* ---------- the rope, the ribbon, the bunting and the crowd ---------- */ | |
| 208 | const ropeZ = a => ROPE_Z - .12 * (1 - Math.pow(Math.abs(a) / ROPE_END, 2)); | |
| 209 | function drawRope() { | |
| 210 | for (let a = -ROPE_END; a < ROPE_END; a += .5) capsule(toWorld(a, 0, ropeZ(a)), toWorld(a + .5, 0, ropeZ(a + .5)), .09, .09, () => ROPE, 'solid'); | |
| 211 | for (let a = -ROPE_END; a < ROPE_END; a += 1) castShadow(...toWorld(a, 0, ropeZ(a)), .1, 1, 0); | |
| 212 | // the ribbon: a knot on the rope and two tails of red cloth, the only thing on it that moves is where it sits | |
| 213 | const ra = -R.ribbon * LINE, z = ropeZ(ra); | |
| 214 | sphere(...toWorld(ra, 0, z), .2, RIBBON, 'solid'); | |
| 215 | capsule(toWorld(ra, 0, z - .1), toWorld(ra - .12, 0, z - 1.05), .13, .08, () => RIBBON, 'solid'); | |
| 216 | capsule(toWorld(ra, 0, z - .1), toWorld(ra + .22, 0, z - .85), .12, .07, () => RIBBON, 'solid'); | |
| 217 | for (let k = 0; k <= 4; k++) castShadow(...toWorld(ra, 0, z - k * .25), .13, 1, 0); | |
| 218 | } | |
| 219 | function drawBunting(t) { | |
| 220 | const [[a0, b0], [a1, b1]] = POSTS, n = 22; | |
| 221 | for (let k = 0; k <= n; k++) { | |
| 222 | const s = k / n, a = lerp(a0, a1, s), b = lerp(b0, b1, s) + (STILL ? 0 : Math.sin(t * 2.4 + k * .7) * .05), sag = SAG * 4 * s * (1 - s); | |
| 223 | plot(...toWorld(a, b, STRING_Z - sag), rgb(...CHALK)); | |
| 224 | if (k % 2 === 0 && k > 0 && k < n) { | |
| 225 | const swing = STILL ? 0 : Math.sin(t * 3 + k) * .08, color = rgb(...FLAGS[(k / 2) % 4]); | |
| 226 | for (let d = 0; d < 1.0; d += .06) { | |
| 227 | const w = (1 - d) * .32; | |
| 228 | for (let q = -w; q <= w; q += .06) plot(...toWorld(a + q, b + swing * d, STRING_Z - sag - d - .05), color); | |
| 229 | } | |
| 230 | if (k % 4 === 0) castShadow(...toWorld(a, b, STRING_Z - sag - .4), .2, 1, .2); | |
| 231 | } | |
| 232 | } | |
| 233 | } | |
| 234 | function drawCrowd(t) { | |
| 235 | const step = STILL ? 0 : Math.floor(t * 3); | |
| 236 | for (let k = 0; k < 14; k++) { | |
| 237 | const a = -13 + k * 2 + hash(k, 3) * .8, b = -5.2 - hash(k, 9) * 1.2, up = (!STILL && (step + k * 5) % 7 < 2) ? .35 : 0; | |
| 238 | const col = CROWD[k % CROWD.length], skin = SKIN[k % 4]; | |
| 239 | capsule(toWorld(a, b, .1 + up), toWorld(a, b, 1.1 + up), .32, .3, () => col, 'solid'); | |
| 240 | sphere(...toWorld(a, b, 1.5 + up), .28, skin, 'solid'); | |
| 241 | sphere(...toWorld(a, b + .04, 1.65 + up), .24, HAIR[(k * 3) % 4], 'solid'); | |
| 242 | if (k % 3 === 0) { pendingShadows.push(toWorld(a, b, .1), .3, toWorld(a, b, 1.6 + up), .28); } | |
| 243 | } | |
| 244 | } | |
| 245 | ||
| 246 | /* ---------- the caller's marker, drawn on the picture ---------- */ | |
| 247 | const MX = 180, MY = 20; | |
| 248 | function disc(cx, cy, r, color) { | |
| 249 | for (let y = -r; y <= r; y++) for (let x = -r; x <= r; x++) { | |
| 250 | if (x * x + y * y <= r * r + r * .5) { const ix = cx + x, iy = cy + y; if (ix >= 0 && iy >= 0 && ix < W && iy < H) pix[iy * W + ix] = color; } | |
| 251 | } | |
| 252 | } | |
| 253 | function ring(cx, cy, r, color) { | |
| 254 | for (let a = 0; a < 6.283; a += 1 / Math.max(6, r)) { | |
| 255 | const ix = Math.round(cx + Math.cos(a) * r), iy = Math.round(cy + Math.sin(a) * r); | |
| 256 | if (ix >= 0 && iy >= 0 && ix < W && iy < H) pix[iy * W + ix] = color; | |
| 257 | } | |
| 258 | } | |
| 259 | // the target ring waits; a ring closes on it and lands at each beat. Under reduced motion nothing closes: the dot is lit while the beat is on. | |
| 260 | function drawMarker() { | |
| 261 | if (R.phase !== 'pull') return; | |
| 262 | const { offset } = Rope.nearestBeat(R.t), lit = Math.abs(offset) <= Rope.ON_BEAT; | |
| 263 | disc(MX, MY, 12, PAPER); | |
| 264 | ring(MX, MY, 9, INK); | |
| 265 | if (!STILL) { | |
| 266 | const toBeat = 1 - Rope.phaseOf(R.t); // 1 right after a beat, 0 as the next lands | |
| 267 | ring(MX, MY, 9 + Math.round(toBeat * 14), lit ? SIGNAL : INK); | |
| 268 | } | |
| 269 | disc(MX, MY, lit ? 6 : 3, lit ? SIGNAL : INK); | |
| 270 | if (lit) ring(MX, MY, 7, INK); | |
| 271 | } | |
| 272 | ||
| 273 | /* ---------- drawing ---------- */ | |
| 274 | function draw() { | |
| 275 | pix.set(bakedPix); | |
| 276 | zb.set(bakedZ); | |
| 277 | pendingShadows.length = 0; | |
| 278 | const t = STILL ? 0 : Math.floor(G.time * WIND_FPS) / WIND_FPS; | |
| 279 | const heave = easeOut(clamp(1 - (G.time - G.heaveAt) / .3, 0, 1)), slip = easeOut(clamp(1 - (G.time - G.stumbleAt) / .5, 0, 1)); | |
| 280 | const beat = STILL ? 0 : t; | |
| 281 | drawTeam(-1, US, .3 + heave * .14 - slip * .55, .12 + heave * .1, beat); | |
| 282 | drawTeam(1, THEM, .3 + .12 * Rope.rivalPull(R.pull) / .1, .12, beat); | |
| 283 | drawRope(); | |
| 284 | drawCrowd(t); | |
| 285 | drawBunting(t); | |
| 286 | flushShadows(); | |
| 287 | applyShadows(bakedZ); | |
| 288 | shadeEdges(); | |
| 289 | drawMarker(); | |
| 290 | ctx.putImageData(image, 0, 0); | |
| 291 | } | |
| 292 | ||
| 293 | /* ---------- game ---------- */ | |
| 294 | function act(events) { | |
| 295 | for (const e of events) { | |
| 296 | if (e.type === 'heave') { | |
| 297 | G.heaveAt = G.time; | |
| 298 | note(e.quality === 'on' ? 'on the beat · heave' : (e.offset < 0 ? 'a touch early' : 'a touch late') + ' · half a heave'); | |
| 299 | sound('heave'); | |
| 300 | } else if (e.type === 'stumble') { | |
| 301 | G.stumbleAt = G.time; | |
| 302 | note('off the beat · the team stumbles'); | |
| 303 | sound('stumble'); | |
| 304 | } else if (e.type === 'pull-won' || e.type === 'pull-lost') { | |
| 305 | note(e.type === 'pull-won' ? 'the ribbon is over your line · pull won' : 'the ribbon is over their line · pull lost'); | |
| 306 | sound(e.type); | |
| 307 | } else if (e.type === 'start') { | |
| 308 | note(`pull ${e.pull + 1} of ${Rope.PULLS} · the rival pulls harder`); | |
| 309 | } else if (e.type === 'over') { | |
| 310 | if (R.record) try { localStorage.setItem(BEST_KEY, String(R.best)); } catch (err) { /* private window: keep it for this visit */ } | |
| 311 | const line = e.win ? `you win: ${e.won} of ${Rope.PULLS} pulls, the fete is yours` : `you lose: ${e.won} of ${Rope.PULLS} pulls, three win`; | |
| 312 | G.noteUntil = 1e9; | |
| 313 | setStatus(`${line}${R.record ? ' · new best' : ''} · tap or press space to pull again`); | |
| 314 | } | |
| 315 | } | |
| 316 | renderHud(); | |
| 317 | } | |
| 318 | function update(dt) { | |
| 319 | G.time += dt; | |
| 320 | act(Rope.advance(R, dt)); | |
| 321 | if (R.phase === 'pull') { | |
| 322 | const n = Math.floor(R.t / Rope.BEAT); | |
| 323 | if (n > G.tickBeat) { G.tickBeat = n; sound('tick'); } | |
| 324 | if (G.time >= G.noteUntil) setStatus(`pull ${R.pull + 1} of ${Rope.PULLS} · ${HINT}`); | |
| 325 | } | |
| 326 | renderHud(); | |
| 327 | } | |
| 328 | function restart() { | |
| 329 | const next = Rope.again(R); | |
| 330 | if (!next) return R.phase === 'over'; | |
| 331 | R = next; G.tickBeat = 0; G.noteUntil = 0; shown = ''; | |
| 332 | note(HINT); | |
| 333 | renderHud(); | |
| 334 | return true; | |
| 335 | } | |
| 336 | function pull() { | |
| 337 | ensureAudio(); | |
| 338 | if (restart()) return; | |
| 339 | act(Rope.tap(R)); | |
| 340 | } | |
| 341 | ||
| 342 | /* ---------- input ---------- */ | |
| 343 | canvas.addEventListener('pointerdown', e => { canvas.focus({ preventScroll: true }); pull(); }); | |
| 344 | canvas.addEventListener('contextmenu', e => e.preventDefault()); | |
| 345 | canvas.addEventListener('keydown', e => { | |
| 346 | if (e.key !== ' ' && e.key !== 'Enter') return; | |
| 347 | e.preventDefault(); | |
| 348 | if (!e.repeat) pull(); | |
| 349 | }); | |
| 350 | ||
| 351 | /* ---------- sound ---------- */ | |
| 352 | let audio = null, muted = false; | |
| 353 | function ensureAudio() { | |
| 354 | if (audio) { if (audio.state === 'suspended') audio.resume(); return; } | |
| 355 | const AC = window.AudioContext || window.webkitAudioContext; | |
| 356 | if (AC) audio = new AC(); | |
| 357 | } | |
| 358 | function tone(type, from, to, len, vol) { | |
| 359 | const now = audio.currentTime, gain = audio.createGain(), osc = audio.createOscillator(); | |
| 360 | osc.type = type; | |
| 361 | osc.frequency.setValueAtTime(from, now); | |
| 362 | osc.frequency.exponentialRampToValueAtTime(to, now + len); | |
| 363 | gain.gain.setValueAtTime(vol, now); | |
| 364 | gain.gain.exponentialRampToValueAtTime(.0005, now + len); | |
| 365 | osc.connect(gain).connect(audio.destination); | |
| 366 | osc.start(now); osc.stop(now + len + .01); | |
| 367 | } | |
| 368 | function sound(kind) { | |
| 369 | if (!audio || muted) return; | |
| 370 | if (kind === 'tick') tone('square', 520, 480, .04, .025); | |
| 371 | else if (kind === 'heave') tone('triangle', 200, 110, .12, .08); | |
| 372 | else if (kind === 'stumble') tone('sawtooth', 160, 70, .25, .05); | |
| 373 | else if (kind === 'pull-won') { tone('triangle', 520, 780, .25, .07); tone('triangle', 780, 1040, .25, .05); } | |
| 374 | else if (kind === 'pull-lost') tone('sawtooth', 240, 70, .35, .05); | |
| 375 | } | |
| 376 | const muteButton = document.getElementById('mute'); | |
| 377 | muteButton.addEventListener('click', () => { | |
| 378 | muted = !muted; | |
| 379 | muteButton.textContent = muted ? 'sound off' : 'sound on'; | |
| 380 | muteButton.setAttribute('aria-pressed', String(muted)); | |
| 381 | }); | |
| 382 | ||
| 383 | /* ---------- boot ---------- */ | |
| 384 | addEventListener('resize', fit); | |
| 385 | fit(); | |
| 386 | bake(); | |
| 387 | note(HINT); | |
| 388 | renderHud(); | |
| 389 | canvas.focus({ preventScroll: true }); | |
| 390 | let last = performance.now(); | |
| 391 | function frame(now) { | |
| 392 | update(Math.min(.04, (now - last) / 1000)); | |
| 393 | last = now; | |
| 394 | draw(); | |
| 395 | requestAnimationFrame(frame); | |
| 396 | } | |
| 397 | requestAnimationFrame(frame); | |
| 398 | </script> | |
| 399 | </body> | |
| 400 | </html> |
tug-of-war/rope.js added +93
| @@ -0,0 +1,93 @@ | ||
| 1 | 'use strict'; | |
| 2 | // The tug of war rules: a caller beats a steady heave, the player taps on the beat, and a ribbon tied to the middle of | |
| 3 | // the rope travels between two chalk lines. The ribbon runs from -1 (the rival's line, a loss) to +1 (the player's line, | |
| 4 | // a win); 0 is the middle. Times are in seconds from the start of the pull. Plain numbers in, plain numbers out, no DOM. | |
| 5 | (function (root) { | |
| 6 | const PULLS = 5, WIN_AT = 3; // one game is five pulls; three won or more is a win | |
| 7 | const BEAT = 1; // seconds between the caller's beats; the first beat is at BEAT | |
| 8 | const ON_BEAT = .15; // a tap within this many seconds of a beat, early or late, is on the beat (at least 120 ms) | |
| 9 | const OFF_BEAT = .3; // a tap further than this from every beat is well off it and the team stumbles | |
| 10 | const HEAVE = .2, HALF_HEAVE = .1, STUMBLE = .1; // ribbon moved by an on-beat tap, a tap between the two windows, a stumble | |
| 11 | const RIVAL_BASE = .04, RIVAL_RISE = .015; // the rival pulls steadily at RIVAL_BASE + RIVAL_RISE * pull, ribbon units per second | |
| 12 | const REST = 1.6, RESTART_AFTER = .6; // seconds between pulls; seconds the end card stays before a tap restarts | |
| 13 | ||
| 14 | const finite = v => (Number.isFinite(v) ? v : 0); | |
| 15 | const rivalPull = n => RIVAL_BASE + RIVAL_RISE * Math.max(0, Math.min(PULLS - 1, Math.floor(finite(n)))); | |
| 16 | const clampRibbon = v => Math.max(-1, Math.min(1, v)); | |
| 17 | const won = s => s.won >= WIN_AT; | |
| 18 | ||
| 19 | // the beat nearest to time t, as its number (the first is 1) and the signed distance to it (negative is early) | |
| 20 | function nearestBeat(t) { | |
| 21 | const index = Math.max(1, Math.round(finite(t) / BEAT)); | |
| 22 | return { index, offset: finite(t) - index * BEAT }; | |
| 23 | } | |
| 24 | // 'on', 'near' or 'off' for a tap this far from its beat | |
| 25 | function judge(offset) { | |
| 26 | const d = Math.abs(finite(offset)); | |
| 27 | return d <= ON_BEAT + 1e-9 ? 'on' : d <= OFF_BEAT + 1e-9 ? 'near' : 'off'; | |
| 28 | } | |
| 29 | // where the heave marker is in its cycle: 0 at a beat, rising to just under 1 before the next | |
| 30 | const phaseOf = t => { const u = finite(t) / BEAT; return u - Math.floor(u); }; | |
| 31 | ||
| 32 | function newGame(best = 0) { | |
| 33 | return { phase: 'pull', pull: 0, t: 0, ribbon: 0, won: 0, lost: 0, results: [], lastBeat: 0, timer: 0, best: Math.max(0, finite(best)), record: false }; | |
| 34 | } | |
| 35 | ||
| 36 | function settle(s, win, events) { | |
| 37 | s.results.push(win); | |
| 38 | if (win) s.won++; else s.lost++; | |
| 39 | events.push({ type: win ? 'pull-won' : 'pull-lost', pull: s.pull, won: s.won }); | |
| 40 | s.timer = 0; | |
| 41 | if (s.results.length >= PULLS) { | |
| 42 | s.phase = 'over'; | |
| 43 | if (s.won > s.best) { s.best = s.won; s.record = true; } | |
| 44 | events.push({ type: 'over', won: s.won, win: won(s), record: s.record }); | |
| 45 | } else s.phase = 'rest'; | |
| 46 | } | |
| 47 | function crossed(s, events) { | |
| 48 | if (s.ribbon >= 1) { s.ribbon = 1; settle(s, true, events); } | |
| 49 | else if (s.ribbon <= -1) { s.ribbon = -1; settle(s, false, events); } | |
| 50 | } | |
| 51 | ||
| 52 | // the player taps now. Returns the events: a heave on the beat (full or half) or a stumble, and the pull's end if it crossed a line | |
| 53 | function tap(s) { | |
| 54 | const events = []; | |
| 55 | if (s.phase !== 'pull') return events; | |
| 56 | const { index, offset } = nearestBeat(s.t); | |
| 57 | let quality = judge(offset); | |
| 58 | if (quality !== 'off' && index === s.lastBeat) quality = 'off'; // a second tap for the same beat is not a heave | |
| 59 | if (quality !== 'off') s.lastBeat = index; | |
| 60 | const step = quality === 'on' ? HEAVE : quality === 'near' ? HALF_HEAVE : -STUMBLE; | |
| 61 | s.ribbon = clampRibbon(s.ribbon + step); | |
| 62 | events.push({ type: quality === 'off' ? 'stumble' : 'heave', quality, offset, beat: index, ribbon: s.ribbon }); | |
| 63 | crossed(s, events); | |
| 64 | return events; | |
| 65 | } | |
| 66 | ||
| 67 | // move the clock and the rival's steady pull over dt | |
| 68 | function advance(s, dt) { | |
| 69 | const events = []; | |
| 70 | dt = Math.max(0, Math.min(.1, finite(dt))); | |
| 71 | if (s.phase === 'rest') { | |
| 72 | s.timer += dt; | |
| 73 | if (s.timer >= REST) { s.phase = 'pull'; s.pull++; s.t = 0; s.ribbon = 0; s.lastBeat = 0; s.timer = 0; events.push({ type: 'start', pull: s.pull }); } | |
| 74 | return events; | |
| 75 | } | |
| 76 | if (s.phase === 'over') { s.timer += dt; return events; } | |
| 77 | s.t += dt; | |
| 78 | s.ribbon = clampRibbon(s.ribbon - rivalPull(s.pull) * dt); | |
| 79 | crossed(s, events); | |
| 80 | return events; | |
| 81 | } | |
| 82 | ||
| 83 | // a tap after the end card has been up for a moment starts a new game; otherwise null | |
| 84 | function again(s) { | |
| 85 | return s.phase === 'over' && s.timer > RESTART_AFTER ? newGame(s.best) : null; | |
| 86 | } | |
| 87 | ||
| 88 | const api = { | |
| 89 | PULLS, WIN_AT, BEAT, ON_BEAT, OFF_BEAT, HEAVE, HALF_HEAVE, STUMBLE, RIVAL_BASE, RIVAL_RISE, REST, | |
| 90 | rivalPull, nearestBeat, judge, phaseOf, won, newGame, tap, advance, again, | |
| 91 | }; | |
| 92 | if (typeof module !== 'undefined') module.exports = api; else root.Rope = api; | |
| 93 | })(typeof window !== 'undefined' ? window : globalThis); |
tug-of-war/rope.test.mjs added +179
| @@ -0,0 +1,179 @@ | ||
| 1 | import { test, expect } from 'bun:test'; | |
| 2 | import { PULLS, WIN_AT, BEAT, ON_BEAT, OFF_BEAT, HEAVE, HALF_HEAVE, STUMBLE, rivalPull, nearestBeat, judge, phaseOf, won, newGame, tap, advance, again } from './rope.js'; | |
| 3 | ||
| 4 | const DT = 1 / 60; | |
| 5 | const run = (s, seconds) => { const out = []; for (let t = 0; t < seconds - 1e-9; t += DT) out.push(...advance(s, DT)); return out; }; | |
| 6 | // a game whose clock stands at t seconds into the first pull | |
| 7 | const at = t => { const s = newGame(); s.t = t; return s; }; | |
| 8 | // win or lose the current pull outright | |
| 9 | const winPull = s => { s.ribbon = .95; return tap(at0(s)); }; | |
| 10 | const at0 = s => { s.t = BEAT; s.lastBeat = 0; return s; }; | |
| 11 | const losePull = s => { s.ribbon = -.99999; return advance(s, .1); }; | |
| 12 | ||
| 13 | test('the on-beat window is at least 120 ms either side of the beat', () => { | |
| 14 | expect(ON_BEAT).toBeGreaterThanOrEqual(.12); | |
| 15 | expect(judge(.12)).toBe('on'); | |
| 16 | expect(judge(-.12)).toBe('on'); | |
| 17 | expect(judge(ON_BEAT)).toBe('on'); | |
| 18 | expect(judge(ON_BEAT + .02)).toBe('near'); | |
| 19 | expect(judge(-ON_BEAT - .02)).toBe('near'); | |
| 20 | expect(judge(OFF_BEAT + .02)).toBe('off'); | |
| 21 | expect(judge(NaN)).toBe('on'); | |
| 22 | }); | |
| 23 | ||
| 24 | test('the nearest beat is found early and late, and the first beat is the first one', () => { | |
| 25 | expect(nearestBeat(BEAT - .1)).toEqual({ index: 1, offset: expect.closeTo(-.1, 6) }); | |
| 26 | expect(nearestBeat(2 * BEAT + .1).index).toBe(2); | |
| 27 | expect(nearestBeat(.1).index).toBe(1); | |
| 28 | expect(phaseOf(0)).toBe(0); | |
| 29 | expect(phaseOf(BEAT * 2.5)).toBeCloseTo(.5, 6); | |
| 30 | }); | |
| 31 | ||
| 32 | test('a tap on the beat pulls the rope toward the player', () => { | |
| 33 | for (const off of [0, -ON_BEAT, ON_BEAT - .01]) { | |
| 34 | const s = at(BEAT + off); | |
| 35 | const events = tap(s); | |
| 36 | expect(s.ribbon).toBeCloseTo(HEAVE, 6); | |
| 37 | expect(events[0]).toMatchObject({ type: 'heave', quality: 'on', beat: 1 }); | |
| 38 | } | |
| 39 | }); | |
| 40 | ||
| 41 | test('a tap between the windows is a half heave', () => { | |
| 42 | const s = at(BEAT + ON_BEAT + .05); | |
| 43 | expect(tap(s)[0]).toMatchObject({ type: 'heave', quality: 'near' }); | |
| 44 | expect(s.ribbon).toBeCloseTo(HALF_HEAVE, 6); | |
| 45 | }); | |
| 46 | ||
| 47 | test('a tap well off the beat makes the team stumble and the rope slips the other way', () => { | |
| 48 | const s = at(BEAT * 1.5); | |
| 49 | const events = tap(s); | |
| 50 | expect(events[0]).toMatchObject({ type: 'stumble', quality: 'off' }); | |
| 51 | expect(s.ribbon).toBeCloseTo(-STUMBLE, 6); | |
| 52 | }); | |
| 53 | ||
| 54 | test('a second tap for the same beat is a stumble, so mashing does not work', () => { | |
| 55 | const s = at(BEAT); | |
| 56 | tap(s); | |
| 57 | s.t = BEAT + .05; | |
| 58 | expect(tap(s)[0].type).toBe('stumble'); | |
| 59 | expect(s.ribbon).toBeCloseTo(HEAVE - STUMBLE, 6); | |
| 60 | s.t = 2 * BEAT; | |
| 61 | expect(tap(s)[0].type).toBe('heave'); | |
| 62 | }); | |
| 63 | ||
| 64 | test('not tapping lets the rival pull steadily toward their line', () => { | |
| 65 | const s = newGame(); | |
| 66 | run(s, 2); | |
| 67 | expect(s.ribbon).toBeCloseTo(-2 * rivalPull(0), 2); | |
| 68 | run(s, 2); | |
| 69 | expect(s.ribbon).toBeCloseTo(-4 * rivalPull(0), 2); | |
| 70 | }); | |
| 71 | ||
| 72 | test('the rival is stronger every round', () => { | |
| 73 | for (let n = 1; n < PULLS; n++) expect(rivalPull(n)).toBeGreaterThan(rivalPull(n - 1)); | |
| 74 | const s = newGame(); s.pull = PULLS - 1; | |
| 75 | run(s, 1); | |
| 76 | expect(s.ribbon).toBeCloseTo(-rivalPull(PULLS - 1), 2); | |
| 77 | }); | |
| 78 | ||
| 79 | test('the ribbon crossing the player\'s line wins the pull', () => { | |
| 80 | const s = newGame(); | |
| 81 | s.ribbon = 1 - HEAVE + .01; s.t = BEAT; | |
| 82 | const events = tap(s); | |
| 83 | expect(events.map(e => e.type)).toEqual(['heave', 'pull-won']); | |
| 84 | expect(s.ribbon).toBe(1); | |
| 85 | expect(s.won).toBe(1); | |
| 86 | expect(s.results).toEqual([true]); | |
| 87 | expect(s.phase).toBe('rest'); | |
| 88 | }); | |
| 89 | ||
| 90 | test('the ribbon crossing the rival\'s line loses the pull', () => { | |
| 91 | const s = newGame(); | |
| 92 | s.ribbon = -.999; | |
| 93 | const events = run(s, .5); | |
| 94 | expect(events.map(e => e.type)).toEqual(['pull-lost']); | |
| 95 | expect(s.ribbon).toBe(-1); | |
| 96 | expect(s.lost).toBe(1); | |
| 97 | expect(s.won).toBe(0); | |
| 98 | expect(s.phase).toBe('rest'); | |
| 99 | }); | |
| 100 | ||
| 101 | test('a stumble at the edge loses the pull', () => { | |
| 102 | const s = at(BEAT * 1.5); | |
| 103 | s.ribbon = -1 + STUMBLE / 2; | |
| 104 | expect(tap(s).map(e => e.type)).toEqual(['stumble', 'pull-lost']); | |
| 105 | }); | |
| 106 | ||
| 107 | test('taps do nothing between pulls, and the next pull starts fresh after the rest', () => { | |
| 108 | const s = newGame(); | |
| 109 | losePull(s); | |
| 110 | expect(tap(s)).toEqual([]); | |
| 111 | const events = run(s, 2); | |
| 112 | expect(events.map(e => e.type)).toEqual(['start']); | |
| 113 | expect(s).toMatchObject({ phase: 'pull', pull: 1 }); | |
| 114 | expect(s.ribbon).toBeGreaterThan(-.1); | |
| 115 | expect(s.lastBeat).toBe(0); | |
| 116 | }); | |
| 117 | ||
| 118 | test('one game is five pulls, then it ends', () => { | |
| 119 | const s = newGame(); | |
| 120 | for (let i = 0; i < PULLS; i++) { | |
| 121 | expect(s.phase).toBe('pull'); | |
| 122 | expect(s.pull).toBe(i); | |
| 123 | losePull(s); | |
| 124 | run(s, 2); | |
| 125 | } | |
| 126 | expect(s.phase).toBe('over'); | |
| 127 | expect(s.results.length).toBe(PULLS); | |
| 128 | expect(advance(s, .1)).toEqual([]); | |
| 129 | }); | |
| 130 | ||
| 131 | function play(wins) { | |
| 132 | const s = newGame(); | |
| 133 | const events = []; | |
| 134 | for (let i = 0; i < PULLS; i++) { | |
| 135 | events.push(...(i < wins ? winPull(s) : losePull(s))); | |
| 136 | run(s, 2); | |
| 137 | } | |
| 138 | return { s, over: events.find(e => e.type === 'over') }; | |
| 139 | } | |
| 140 | ||
| 141 | test('three pulls won is a win, two is a loss', () => { | |
| 142 | expect(WIN_AT).toBe(3); | |
| 143 | for (const [wins, win] of [[0, false], [1, false], [2, false], [3, true], [4, true], [5, true]]) { | |
| 144 | const { s, over } = play(wins); | |
| 145 | expect(s.won).toBe(wins); | |
| 146 | expect(won(s)).toBe(win); | |
| 147 | expect(over).toMatchObject({ type: 'over', won: wins, win }); | |
| 148 | } | |
| 149 | }); | |
| 150 | ||
| 151 | test('the best is the most pulls won, a record only when beaten', () => { | |
| 152 | expect(play(3).s.best).toBe(3); | |
| 153 | expect(play(3).s.record).toBe(true); | |
| 154 | const s = newGame(4); | |
| 155 | for (let i = 0; i < PULLS; i++) { i < 3 ? winPull(s) : losePull(s); run(s, 2); } | |
| 156 | expect(s.best).toBe(4); | |
| 157 | expect(s.record).toBe(false); | |
| 158 | }); | |
| 159 | ||
| 160 | test('a tap restarts only after the end card has been up a moment, keeping the best', () => { | |
| 161 | const { s } = play(3); | |
| 162 | s.timer = 0; | |
| 163 | expect(again(s)).toBeNull(); | |
| 164 | run(s, 1); | |
| 165 | const next = again(s); | |
| 166 | expect(next).toMatchObject({ phase: 'pull', pull: 0, won: 0, ribbon: 0, best: 3, record: false }); | |
| 167 | expect(again(newGame())).toBeNull(); | |
| 168 | }); | |
| 169 | ||
| 170 | test('garbage input is ignored, not trusted', () => { | |
| 171 | const s = newGame(NaN); | |
| 172 | expect(s.best).toBe(0); | |
| 173 | expect(advance(s, NaN)).toEqual([]); | |
| 174 | expect(advance(s, Infinity)).toEqual([]); | |
| 175 | expect(Number.isFinite(s.ribbon)).toBe(true); | |
| 176 | s.t = NaN; | |
| 177 | expect(tap(s)[0].type).toBeDefined(); | |
| 178 | expect(Number.isFinite(s.ribbon)).toBe(true); | |
| 179 | }); |
new above
offline
This comment belongs to
tram-line, but the new entries were inserted between it and its game. Move it back abovetram-line.