Public: anyone can read this; the crew can change it.
Migrated from github.com/amirmushichge/pixel-plays
Add Campfire: keep the fire burning through the night by feeding it the right logs #52
Check · Review · Land · Live done — Landed and live in production.
quill/pixel-plays:quill/32-add-campfire-keep-the-fire-burning-throu → main 3 commits fork 6 threads · 6 unresolved
README.md +2 −1
| @@ -28,6 +28,7 @@ Concept by [abhi](https://x.com/awwbhi2). Game by [AmirMushich](https://x.com/Am | ||
| 28 | 28 | | [](window-washer/) | Five grimy windows, twelve seconds each: wipe the glass clean with a squeegee before the cradle drops a floor, bird droppings need two passes, score the percentage clean (forged by the crew on the engine and shell above, not by the original creators) | Drag; arrows + space | |
| 29 | 29 | | [](market-scale/) | Five customers, 15 seconds each: weigh fruit onto a hanging pan scale to match the weight on the chalk slate, exact scores 3 and within 50 g scores 1 (forged by the crew on the engine and shell above, not by the original creators) | Drag or tap fruit, tap the bell; arrows + enter, space | |
| 30 | 30 | | [](domino-run/) | Twenty dominoes to stand along a curving chalk path in three courtyards, then tip the first: a gap too wide stops the chain, too close topples one early; ringing the bell scores 10 and each domino left over 1 more (forged by the crew on the engine and shell above, not by the original creators) | Tap the path, or arrows + space; tip first button or enter | |
| 31 | | [](campfire/) | A 90-second night: keep a campfire at a good height by dragging twigs, branches and logs into the ring; a log smothers a small fire and rain damps the flames (forged by the crew on the engine and shell above, not by the original creators) | Drag from the pile; arrows or 1/2/3 + space | | |
| 31 | 32 | |
| 32 | 33 | ## Run |
| 33 | 34 | Requires [](https://nodejs.org/); nothing to install. |
| @@ -47,7 +48,7 @@ On the forge, `forge.json` runs `bun serve.mjs` with health check `/`. State is | ||
| 47 | 48 | Every page carries Open Graph and Twitter tags (between `og:begin` and `og:end`), and `og/<id>.png` is a 1200x630 card: the game's own opening frame, scaled 2x on white; `og/home.png` is a grid of all of them. Because the games are static, `bun scripts/og.mjs` makes them without a browser: it runs each page's scripts in a `vm` against a stub DOM, captures the canvas pixels and writes the PNG (zlib only, no dependency) and the tags. Run it after adding a game or changing a scene; `serve.test.mjs` fails when a card or its tags are missing or stale. Scenes are rendered with a seeded `Math.random`, so the cards are byte-for-byte reproducible. Absolute URLs use `ORIGIN` at the top of the script (the forge production site), so change it there if the site moves. |
| 48 | 49 | |
| 49 | 50 | ## Add a game |
| 50 | Create `<id>/index.html`, load `../shell.js`, call `PixelPlays.mount({ id, rules })`, set `window.PP_SCENE`, load `../engine.js`, then add the game to `GAMES` in `shell.js` with a one-line `blurb`; the home page lists every `GAMES` entry as a card with its `kind`, `input` and `accent`, searchable and filterable by kind (a static fallback list for no-JS is kept in sync by `serve.test.mjs`, so add the matching `<li>` to `index.html` too, with the same `data-kind`, `data-input`, `--accent` and tags line). Then run `bun scripts/og.mjs` to make its share card and tags. The page needs a `#mute` button. Put its background track at `<id>/<id>.mp3`, or set `music: false` on its `GAMES` entry when it has none (Stone Skip, Kite Hill, Paper Route, Harbour Crane, Pizza Toss, Sled Run, Pottery Wheel, Row Home, Orchard, Lighthouse, Market Scale, Window Washer and Domino Run play on their own sound effects). Rules that need testing go in a plain script with a `.test.mjs` beside it, as in `kite-hill/model.js`. | |
| 51 | Create `<id>/index.html`, load `../shell.js`, call `PixelPlays.mount({ id, rules })`, set `window.PP_SCENE`, load `../engine.js`, then add the game to `GAMES` in `shell.js` with a one-line `blurb`; the home page lists every `GAMES` entry as a card with its `kind`, `input` and `accent`, searchable and filterable by kind (a static fallback list for no-JS is kept in sync by `serve.test.mjs`, so add the matching `<li>` to `index.html` too, with the same `data-kind`, `data-input`, `--accent` and tags line). Then run `bun scripts/og.mjs` to make its share card and tags. The page needs a `#mute` button. Put its background track at `<id>/<id>.mp3`, or set `music: false` on its `GAMES` entry when it has none (Stone Skip, Kite Hill, Paper Route, Harbour Crane, Pizza Toss, Sled Run, Pottery Wheel, Row Home, Orchard, Lighthouse, Market Scale, Window Washer, Domino Run and Campfire play on their own sound effects). Rules that need testing go in a plain script with a `.test.mjs` beside it, as in `kite-hill/model.js`. | |
| 51 | 52 | |
| 52 | 53 | - `shell.js` provides the game menu, the Rules popup, the credits and the background music. |
| 53 | 54 | - `engine.js` provides the camera, depth buffer, colour ramps, shadows, edges, figures, trees and wind as globals. |
campfire/fire.js added +120
| @@ -0,0 +1,120 @@ | ||
| 1 | 'use strict'; | |
| 2 | // Campfire rules as plain data in, plain data out: heat and how it decays, the fuels with their catch and burn times, | |
| 3 | // smothering a small fire, rain showers, and the score. The page draws from this state. | |
| 4 | // Heat runs 0..100; the flames are drawn from it in whole pixels. | |
| 5 | (function (root) { | |
| 6 | const NIGHT = 90; // seconds | |
| 7 | const HEAT_MAX = 100, START_HEAT = 60; | |
| 8 | const GOOD_MIN = 30, GOOD_MAX = 80; // the fire counts while its heat is in this band | |
| 9 | const EMBERS = 15; // under this the flames are gone and only embers glow | |
| 10 | const DECAY_BASE = 1.5, DECAY_RATE = .045; // heat lost per second: a little, plus more the hotter it is | |
| 11 | const RAIN_DRAIN = 2.5, RAIN_CATCH = .5; // extra heat lost per second in rain; damp wood lights at half speed | |
| 12 | const RING_MAX = 4; // pieces the ring holds at once | |
| 13 | const MAX_FLAME = 38; // tallest flame, px | |
| 14 | const STEP = 1 / 30; | |
| 15 | // power: heat per second while burning. minHeat: the fire it needs; fed to a smaller one it is smothered and takes `chill` heat with it. | |
| 16 | const FUELS = { | |
| 17 | twig: { catchTime: 0, burnTime: 3, power: 12, minHeat: 0, chill: 0 }, | |
| 18 | branch: { catchTime: 1, burnTime: 9, power: 3.2, minHeat: 15, chill: 12 }, | |
| 19 | log: { catchTime: 2.5, burnTime: 26, power: 4.5, minHeat: 35, chill: 30 }, | |
| 20 | }; | |
| 21 | const TYPES = Object.keys(FUELS); | |
| 22 | // each shower shows first as dots on the tent (`warn` seconds), then rains from `start` to `end` | |
| 23 | const SHOWER_WARN = 4; | |
| 24 | const clamp = (v, a, b) => Math.max(a, Math.min(b, v)); | |
| 25 | ||
| 26 | // two showers somewhere in the night, never in the first 15 seconds; rng() is 0..1 | |
| 27 | function showersFrom(rng) { | |
| 28 | const first = 19 + rng() * 13, second = 54 + rng() * 14; | |
| 29 | return [first, second].map(start => ({ warn: start - SHOWER_WARN, start, end: start + 6 + rng() * 3 })); | |
| 30 | } | |
| 31 | function newState(best = 0, rng = Math.random) { | |
| 32 | return { | |
| 33 | phase: 'play', time: 0, heat: START_HEAT, fuel: [], showers: showersFrom(rng), | |
| 34 | score: 0, best: Math.max(0, Math.floor(best) || 0), record: false, sel: 0, outcome: null, rng, | |
| 35 | }; | |
| 36 | } | |
| 37 | // 'dry', 'warning' (dots on the tent) or 'rain' | |
| 38 | function weather(state) { | |
| 39 | for (const s of state.showers) { | |
| 40 | if (state.time >= s.start && state.time < s.end) return 'rain'; | |
| 41 | if (state.time >= s.warn && state.time < s.start) return 'warning'; | |
| 42 | } | |
| 43 | return 'dry'; | |
| 44 | } | |
| 45 | // how far into the warning the dots are, 0..1 | |
| 46 | function warning(state) { | |
| 47 | for (const s of state.showers) if (state.time >= s.warn && state.time < s.start) return (state.time - s.warn) / (s.start - s.warn); | |
| 48 | return 0; | |
| 49 | } | |
| 50 | const isGood = heat => heat >= GOOD_MIN && heat <= GOOD_MAX; | |
| 51 | const flameHeight = heat => (heat < EMBERS ? 0 : Math.round(clamp(heat, 0, HEAT_MAX) / HEAT_MAX * MAX_FLAME)); | |
| 52 | // the pieces now burning (past their catch time) | |
| 53 | const burning = state => state.fuel.filter(f => f.catchLeft <= 0); | |
| 54 | ||
| 55 | // lay a piece in the ring. Returns 'placed', 'smothered' (the fire was too small for it), 'full', or null when refused. | |
| 56 | function feed(state, type) { | |
| 57 | if (state.phase !== 'play' || !Object.hasOwn(FUELS, type)) return null; | |
| 58 | const fuel = FUELS[type]; | |
| 59 | if (state.heat < fuel.minHeat) { | |
| 60 | state.heat = Math.max(0, state.heat - fuel.chill); | |
| 61 | if (state.heat <= 0) end(state, 'out'); | |
| 62 | return 'smothered'; | |
| 63 | } | |
| 64 | if (state.fuel.length >= RING_MAX) return 'full'; | |
| 65 | state.fuel.push({ type, catchLeft: fuel.catchTime, burnLeft: fuel.burnTime }); | |
| 66 | return 'placed'; | |
| 67 | } | |
| 68 | // move the pick along twig, branch, log | |
| 69 | function select(state, by) { | |
| 70 | state.sel = ((state.sel + Math.round(by)) % TYPES.length + TYPES.length) % TYPES.length; | |
| 71 | return TYPES[state.sel]; | |
| 72 | } | |
| 73 | function pick(state, type) { | |
| 74 | const i = TYPES.indexOf(type); | |
| 75 | if (i >= 0) state.sel = i; | |
| 76 | return i >= 0; | |
| 77 | } | |
| 78 | function end(state, outcome) { | |
| 79 | state.phase = 'over'; | |
| 80 | state.outcome = outcome; | |
| 81 | const seconds = Math.floor(state.score); | |
| 82 | if (seconds > state.best) { state.best = seconds; state.record = true; } | |
| 83 | } | |
| 84 | function tick(state, dt) { | |
| 85 | const rain = weather(state) === 'rain'; | |
| 86 | state.time += dt; | |
| 87 | for (const f of state.fuel) { | |
| 88 | if (f.catchLeft > 0) { f.catchLeft -= dt * (rain ? RAIN_CATCH : 1); continue; } | |
| 89 | state.heat += FUELS[f.type].power * dt; | |
| 90 | f.burnLeft -= dt; | |
| 91 | } | |
| 92 | state.fuel = state.fuel.filter(f => f.burnLeft > 0); | |
| 93 | state.heat -= (DECAY_BASE + DECAY_RATE * state.heat + (rain ? RAIN_DRAIN : 0)) * dt; | |
| 94 | state.heat = clamp(state.heat, 0, HEAT_MAX); | |
| 95 | if (isGood(state.heat)) state.score += dt; | |
| 96 | if (state.heat <= 0) end(state, 'out'); | |
| 97 | else if (state.time >= NIGHT) { state.time = NIGHT; end(state, 'dawn'); } | |
| 98 | } | |
| 99 | function step(state, dt) { | |
| 100 | if (state.phase !== 'play' || !(dt > 0)) return state; | |
| 101 | let left = Math.min(dt, 1); | |
| 102 | while (left > 1e-9 && state.phase === 'play') { | |
| 103 | const h = Math.min(STEP, left); | |
| 104 | tick(state, h); | |
| 105 | left -= h; | |
| 106 | } | |
| 107 | return state; | |
| 108 | } | |
| 109 | function restart(state) { | |
| 110 | Object.assign(state, newState(state.best, state.rng), { sel: state.sel }); | |
| 111 | return state; | |
| 112 | } | |
| 113 | ||
| 114 | const api = { | |
| 115 | NIGHT, HEAT_MAX, START_HEAT, GOOD_MIN, GOOD_MAX, EMBERS, RING_MAX, MAX_FLAME, SHOWER_WARN, FUELS, TYPES, | |
| 116 | newState, weather, warning, isGood, flameHeight, burning, feed, select, pick, step, restart, | |
| 117 | }; | |
| 118 | if (typeof module !== 'undefined' && module.exports) module.exports = api; | |
| 119 | else root.Campfire = api; | |
| 120 | })(typeof window !== 'undefined' ? window : globalThis); |
campfire/fire.test.mjs added +166
| @@ -0,0 +1,166 @@ | ||
| 1 | import { test, expect } from 'bun:test'; | |
| 2 | import { createRequire } from 'node:module'; | |
| 3 | ||
| 4 | const C = createRequire(import.meta.url)('./fire.js'); | |
| 5 | const calm = () => { const s = C.newState(0, () => .5); s.showers = []; return s; }; | |
| 6 | const run = (s, seconds) => { for (let i = 0; i < Math.round(seconds * 30) && s.phase === 'play'; i++) C.step(s, 1 / 30); return s; }; | |
| 7 | const embers = () => { const s = calm(); s.heat = 12; return s; }; | |
| 8 | // seconds until the heat first drops under the good band | |
| 9 | const untilWeak = s => { let t = 0; while (s.phase === 'play' && s.heat >= C.GOOD_MIN && t < 80) { C.step(s, 1 / 30); t += 1 / 30; } return t; }; | |
| 10 | ||
| 11 | test('an untended fire burns down and goes out, which ends the night', () => { | |
| 12 | const s = run(calm(), 60); | |
| 13 | expect(s.phase).toBe('over'); | |
| 14 | expect(s.outcome).toBe('out'); | |
| 15 | expect(s.heat).toBe(0); | |
| 16 | expect(s.time).toBeLessThan(60); | |
| 17 | }); | |
| 18 | ||
| 19 | test('score is the seconds the fire spent in the good band', () => { | |
| 20 | const s = calm(); | |
| 21 | s.heat = 95; | |
| 22 | run(s, 1); | |
| 23 | expect(s.score).toBe(0); | |
| 24 | s.heat = 50; | |
| 25 | run(s, 1); | |
| 26 | expect(s.score).toBeCloseTo(1, 1); | |
| 27 | }); | |
| 28 | ||
| 29 | test('a twig on embers brings the fire back', () => { | |
| 30 | const s = embers(); | |
| 31 | expect(C.feed(s, 'twig')).toBe('placed'); | |
| 32 | run(s, 3.5); | |
| 33 | expect(s.phase).toBe('play'); | |
| 34 | expect(s.heat).toBeGreaterThan(C.GOOD_MIN); | |
| 35 | }); | |
| 36 | ||
| 37 | test('a log on embers smothers it and the fire goes out', () => { | |
| 38 | const s = embers(); | |
| 39 | expect(C.feed(s, 'log')).toBe('smothered'); | |
| 40 | expect(s.phase).toBe('over'); | |
| 41 | expect(s.outcome).toBe('out'); | |
| 42 | expect(s.fuel.length).toBe(0); | |
| 43 | }); | |
| 44 | ||
| 45 | test('a branch needs a modest fire and a log a strong one', () => { | |
| 46 | const small = calm(); | |
| 47 | small.heat = 30; | |
| 48 | expect(C.feed(small, 'log')).toBe('smothered'); | |
| 49 | expect(small.heat).toBe(0); | |
| 50 | const mid = calm(); | |
| 51 | mid.heat = 30; | |
| 52 | expect(C.feed(mid, 'branch')).toBe('placed'); | |
| 53 | const strong = calm(); | |
| 54 | strong.heat = 70; | |
| 55 | expect(C.feed(strong, 'log')).toBe('placed'); | |
| 56 | expect(strong.heat).toBe(70); | |
| 57 | }); | |
| 58 | ||
| 59 | test('a log on a strong fire keeps it burning longer than a twig', () => { | |
| 60 | const withTwig = calm(), withLog = calm(); | |
| 61 | withTwig.heat = withLog.heat = 70; | |
| 62 | C.feed(withTwig, 'twig'); | |
| 63 | C.feed(withLog, 'log'); | |
| 64 | expect(untilWeak(withLog)).toBeGreaterThan(untilWeak(withTwig) + 5); | |
| 65 | }); | |
| 66 | ||
| 67 | test('a twig catches at once, a log takes time to catch', () => { | |
| 68 | const s = calm(); | |
| 69 | s.heat = 60; | |
| 70 | C.feed(s, 'twig'); | |
| 71 | C.feed(s, 'log'); | |
| 72 | expect(C.burning(s).map(f => f.type)).toEqual(['twig']); | |
| 73 | run(s, 3.2); | |
| 74 | expect(C.burning(s).map(f => f.type)).toEqual(['log']); | |
| 75 | }); | |
| 76 | ||
| 77 | test('burnt-out pieces leave the ring, and the ring holds only so many', () => { | |
| 78 | const s = calm(); | |
| 79 | s.heat = 60; | |
| 80 | for (let i = 0; i < C.RING_MAX; i++) expect(C.feed(s, 'twig')).toBe('placed'); | |
| 81 | expect(C.feed(s, 'twig')).toBe('full'); | |
| 82 | run(s, C.FUELS.twig.burnTime + .5); | |
| 83 | expect(s.fuel.length).toBe(0); | |
| 84 | }); | |
| 85 | ||
| 86 | test('rain shows first as a warning, then drains the heat and slows catching', () => { | |
| 87 | const s = C.newState(0, () => 0); | |
| 88 | s.showers = [{ warn: 10, start: 14, end: 20 }]; | |
| 89 | expect(C.weather(s)).toBe('dry'); | |
| 90 | s.time = 12; | |
| 91 | expect(C.weather(s)).toBe('warning'); | |
| 92 | expect(C.warning(s)).toBeCloseTo(.5); | |
| 93 | s.time = 15; | |
| 94 | expect(C.weather(s)).toBe('rain'); | |
| 95 | const dry = calm(), wet = calm(); | |
| 96 | wet.showers = [{ warn: 0, start: 0, end: 30 }]; | |
| 97 | dry.heat = wet.heat = 60; | |
| 98 | run(dry, 3); | |
| 99 | run(wet, 3); | |
| 100 | expect(wet.heat).toBeLessThan(dry.heat - 5); | |
| 101 | C.feed(dry, 'branch'); | |
| 102 | C.feed(wet, 'branch'); | |
| 103 | run(dry, 1.2); | |
| 104 | run(wet, 1.2); | |
| 105 | expect(C.burning(dry).length).toBe(1); | |
| 106 | expect(C.burning(wet).length).toBe(0); | |
| 107 | }); | |
| 108 | ||
| 109 | test('showers fall inside the night and never before the first 15 seconds', () => { | |
| 110 | for (const r of [0, .5, .999]) { | |
| 111 | const showers = C.newState(0, () => r).showers; | |
| 112 | expect(showers.length).toBe(2); | |
| 113 | for (const sh of showers) { expect(sh.warn).toBeGreaterThanOrEqual(15); expect(sh.end).toBeLessThan(C.NIGHT); } | |
| 114 | } | |
| 115 | }); | |
| 116 | ||
| 117 | test('surviving the 90 seconds ends the night at dawn and records the best', () => { | |
| 118 | const s = calm(); | |
| 119 | s.best = 5; | |
| 120 | for (let i = 0; i < C.NIGHT * 30 + 10 && s.phase === 'play'; i++) { | |
| 121 | if (s.heat < 50 && s.fuel.length < 2) C.feed(s, 'twig'); | |
| 122 | C.step(s, 1 / 30); | |
| 123 | } | |
| 124 | expect(s.phase).toBe('over'); | |
| 125 | expect(s.outcome).toBe('dawn'); | |
| 126 | expect(s.time).toBe(C.NIGHT); | |
| 127 | expect(s.record).toBe(true); | |
| 128 | expect(s.best).toBe(Math.floor(s.score)); | |
| 129 | }); | |
| 130 | ||
| 131 | test('flame height is whole pixels that shrink with the heat and vanish on embers', () => { | |
| 132 | expect(C.flameHeight(100)).toBe(C.MAX_FLAME); | |
| 133 | expect(Number.isInteger(C.flameHeight(57.3))).toBe(true); | |
| 134 | expect(C.flameHeight(80)).toBeLessThan(C.flameHeight(90)); | |
| 135 | expect(C.flameHeight(C.EMBERS - 1)).toBe(0); | |
| 136 | }); | |
| 137 | ||
| 138 | test('bad input is refused and nothing changes once the night is over', () => { | |
| 139 | const s = calm(); | |
| 140 | expect(C.feed(s, 'coal')).toBe(null); | |
| 141 | expect(C.feed(s, '__proto__')).toBe(null); | |
| 142 | expect(C.feed(s, undefined)).toBe(null); | |
| 143 | s.phase = 'over'; | |
| 144 | expect(C.feed(s, 'twig')).toBe(null); | |
| 145 | const heat = s.heat; | |
| 146 | C.step(s, 1); | |
| 147 | expect(s.heat).toBe(heat); | |
| 148 | }); | |
| 149 | ||
| 150 | test('the pick cycles twig, branch, log and wraps', () => { | |
| 151 | const s = calm(); | |
| 152 | expect(C.select(s, 1)).toBe('branch'); | |
| 153 | expect(C.select(s, 1)).toBe('log'); | |
| 154 | expect(C.select(s, 1)).toBe('twig'); | |
| 155 | expect(C.select(s, -1)).toBe('log'); | |
| 156 | expect(C.pick(s, 'branch')).toBe(true); | |
| 157 | expect(C.pick(s, 'nope')).toBe(false); | |
| 158 | expect(s.sel).toBe(1); | |
| 159 | }); | |
| 160 | ||
| 161 | test('restart begins a fresh night and keeps the best', () => { | |
| 162 | const s = run(calm(), 60); | |
| 163 | s.best = 12; | |
| 164 | C.restart(s); | |
| 165 | expect(s).toMatchObject({ phase: 'play', time: 0, heat: C.START_HEAT, score: 0, best: 12, record: false }); | |
| 166 | }); |
campfire/index.html added +479
| @@ -0,0 +1,479 @@ | ||
| 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>Campfire</title> | |
| 7 | <!-- og:begin --> | |
| 8 | <meta name="description" content="Keep a campfire burning through a 90 second night: drag twigs, branches and logs into the ring, a log smothers a small fire and rain damps the flames"> | |
| 9 | <link rel="canonical" href="https://pixel-plays--forge-trevato-apps.open-platform.sh/campfire/"> | |
| 10 | <meta property="og:type" content="website"> | |
| 11 | <meta property="og:site_name" content="Pixel Plays"> | |
| 12 | <meta property="og:title" content="Campfire"> | |
| 13 | <meta property="og:description" content="Keep a campfire burning through a 90 second night: drag twigs, branches and logs into the ring, a log smothers a small fire and rain damps the flames"> | |
| 14 | <meta property="og:url" content="https://pixel-plays--forge-trevato-apps.open-platform.sh/campfire/"> | |
| 15 | <meta property="og:image" content="https://pixel-plays--forge-trevato-apps.open-platform.sh/og/campfire.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="Campfire: 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; } | |
| 24 | * { box-sizing: border-box; } | |
| 25 | html, body { height: 100%; margin: 0; background: var(); color: var(); } | |
| 26 | body { font: 13px/1.35 ui-monospace, "Cascadia Mono", Consolas, Menlo, monospace; overflow: hidden; } | |
| 27 | #stage { position: fixed; inset: 0; display: grid; place-items: center; } | |
| 28 | canvas { display: block; image-rendering: pixelated; touch-action: none; cursor: pointer; outline: none; } | |
| 29 | .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; } | |
| 30 | #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; } | |
| 31 | #scores b { font-size: 16px; font-weight: 600; font-variant-numeric: tabular-nums; min-width: 5ch; } | |
| 32 | #scores span { color: var(); font-variant-numeric: tabular-nums; min-height: 1.35em; } | |
| 33 | #bottom { bottom: max(14px, env(safe-area-inset-bottom)); align-items: flex-end; } | |
| 34 | #notes { display: grid; gap: 2px; } | |
| 35 | #notes span { color: var(); min-height: 1.35em; } | |
| 36 | #actions { display: flex; gap: 22px; align-items: flex-end; } | |
| 37 | button { pointer-events: auto; font: inherit; color: var(); background: none; border: 0; padding: 10px; margin: -10px; cursor: pointer; } | |
| 38 | button:hover, button:focus-visible, button[aria-pressed="true"] { color: var(); } | |
| 39 | button[aria-pressed="true"].fuel { font-weight: 600; text-decoration: underline; text-underline-offset: 3px; } | |
| 40 | </style> | |
| 41 | </head> | |
| 42 | <body> | |
| 43 | <div id="stage"><canvas id="c" tabindex="0" aria-label="Campfire. Drag a twig, branch or log from the pile into the stone ring, or press left and right arrows to pick one and space to add it. Keep the flames between the marks for 90 seconds: twigs catch at once but burn out fast, a log smothers a small fire, and rain dampens it."></canvas></div> | |
| 44 | <div id="scores"><b id="score">0 s</b><span id="meta"></span></div> | |
| 45 | <div class="hud" id="bottom"> | |
| 46 | <div id="notes"><span id="status" role="status" aria-live="polite"></span><span>keep the flames between the marks</span></div> | |
| 47 | <div id="actions"> | |
| 48 | <button id="pick-twig" class="fuel" type="button" aria-pressed="true">twig</button> | |
| 49 | <button id="pick-branch" class="fuel" type="button" aria-pressed="false">branch</button> | |
| 50 | <button id="pick-log" class="fuel" type="button" aria-pressed="false">log</button> | |
| 51 | <button id="feed" type="button">add</button> | |
| 52 | <button id="mute" type="button" aria-pressed="false">sound on</button> | |
| 53 | </div> | |
| 54 | </div> | |
| 55 | <script src="../shell.js"></script> | |
| 56 | <script> | |
| 57 | PixelPlays.mount({ | |
| 58 | id: 'campfire', | |
| 59 | rules: [ | |
| 60 | 'Drag a twig, branch or log from the pile into the ring, or pick with arrows and press space.', | |
| 61 | 'Twigs catch at once but burn out fast. A log smothers a small fire, so feed it only a strong one.', | |
| 62 | 'Dots on the tent mean rain is coming. Rain damps the flames.', | |
| 63 | 'Score the seconds the flames stay between the marks. Let the fire go out and the night ends.', | |
| 64 | ], | |
| 65 | }); | |
| 66 | </script> | |
| 67 | <script>window.PP_SCENE = { originY: 14, shadowX: 1.1, shadowY: -.3 };</script> | |
| 68 | <script src="../engine.js"></script> | |
| 69 | <script src="fire.js"></script> | |
| 70 | <script> | |
| 71 | 'use strict'; | |
| 72 | ||
| 73 | const BEST_KEY = 'pixel-plays-campfire-best'; | |
| 74 | const HALF = 11, SQ = Math.SQRT1_2; | |
| 75 | const DIRT = [[132, 118, 110], [118, 104, 100], [102, 90, 90]]; | |
| 76 | const STONE = [172, 170, 176], WOOD = [138, 98, 66]; | |
| 77 | const PINE = [[34, 78, 68], [48, 104, 76], [74, 130, 86]], PINE_TRUNK = [96, 74, 62]; | |
| 78 | const TENT = [66, 134, 150], TENT_DARK = [34, 52, 66], DROP = rgb(206, 230, 255), STREAK = rgb(170, 196, 232); | |
| 79 | const FLAME = [rgb(205, 58, 38), rgb(240, 122, 36), rgb(250, 200, 66), rgb(255, 238, 160)]; | |
| 80 | const GOOD = rgb(52, 196, 104), MUTED = rgb(139, 135, 127), ASH = rgb(120, 112, 120), EMBER = [rgb(240, 110, 40), rgb(190, 70, 36)]; | |
| 81 | const CHAR = [26, 24, 28]; | |
| 82 | // screen-axis offsets (right, down) to world axes | |
| 83 | const uv = (du, dv) => [(du + dv) * SQ, (dv - du) * SQ]; | |
| 84 | const FIRE = uv(0, 1.5), TENT_AT = uv(-6.8, -3), PINES = [[uv(-1.6, -7.6), 1], [uv(7.6, -5), .85]]; | |
| 85 | const HEAPS = { twig: uv(3.2, 4.3), branch: uv(5.9, 4.3), log: uv(8.5, 3.7) }; | |
| 86 | const RING_R = 1.7; | |
| 87 | // wood from fresh to charred, one colour per quarter of the burn | |
| 88 | const WOODS = Object.fromEntries(Campfire.TYPES.map(t => [t, [0, 1, 2, 3].map(q => WOOD.map((v, k) => v + (CHAR[k] - v) * q / 3.6))])); | |
| 89 | // piece length and radius in world units | |
| 90 | const SIZE = { twig: [1.1, .07], branch: [1.5, .13], log: [1.9, .26] }; | |
| 91 | ||
| 92 | /* ---------- state ---------- */ | |
| 93 | const best0 = (() => { try { return Number(localStorage.getItem(BEST_KEY)) || 0; } catch (e) { return 0; } })(); | |
| 94 | const G = Campfire.newState(best0); | |
| 95 | let time = 0, status = '', kb = false, drag = null, overFor = 0, lastWeather = 'dry', lastOver = false, smotherHeard = false; | |
| 96 | const slots = new WeakMap(); // which spot in the ring each piece lies in, so a burnt-out one does not move the rest | |
| 97 | const nodes = { score: document.getElementById('score'), meta: document.getElementById('meta'), status: document.getElementById('status') }; | |
| 98 | const picks = Object.fromEntries(Campfire.TYPES.map(t => [t, document.getElementById('pick-' + t)])); | |
| 99 | const feedButton = document.getElementById('feed'); | |
| 100 | ||
| 101 | /* ---------- ground ---------- */ | |
| 102 | function groundColor(x, y, ix, iy, i) { | |
| 103 | const edge = Math.pow(Math.pow(Math.abs(x) / HALF, 4) + Math.pow(Math.abs(y) / HALF, 4), .25); | |
| 104 | const rough = edge + (noise(x * 1.3, y * 1.3) - .5) * .12; | |
| 105 | if (rough > 1.04) return 0; | |
| 106 | if (rough > .93 && hash(ix, iy) < (rough - .93) / .11) return 0; | |
| 107 | const near = Math.hypot(x - FIRE[0], y - FIRE[1]) + (noise(x * 2, y * 2) - .5) * .9; | |
| 108 | const t = noise(x * .9, y * .9) * 1.15 + (hash(ix, iy) - .5) * .5 - .08; | |
| 109 | if (near < 3.1) return rgb(...DIRT[clamp(Math.floor(t * 3.2), 0, 2)]); | |
| 110 | kinds[i] = KIND_GRASS; | |
| 111 | return rgb(...GRASS[clamp(Math.floor(t * 3.4), 0, 2)]); | |
| 112 | } | |
| 113 | ||
| 114 | /* ---------- baked scenery ---------- */ | |
| 115 | const woodOf = c => () => c; | |
| 116 | function stoneRing() { | |
| 117 | for (let k = 0; k < 11; k++) { | |
| 118 | const a = k / 11 * Math.PI * 2 + .2, x = FIRE[0] + Math.cos(a) * RING_R, y = FIRE[1] + Math.sin(a) * RING_R; | |
| 119 | sphere(x, y, .3, .38 + hash(k, 2) * .08, STONE, 'rough'); | |
| 120 | castShadow(x, y, .4, .42, 1, .2); | |
| 121 | } | |
| 122 | } | |
| 123 | function tent() { | |
| 124 | const [cx, cy] = TENT_AT, top = 1.9; | |
| 125 | for (let y = -1.5; y <= 1.5; y += .13) { | |
| 126 | capsule([cx - 1.5, cy + y, .1], [cx, cy + y, top], .17, .17, woodOf(TENT), 'rough'); | |
| 127 | capsule([cx + 1.5, cy + y, .1], [cx, cy + y, top], .17, .17, woodOf(TENT), 'rough'); | |
| 128 | } | |
| 129 | for (let z = .15; z < 1.5; z += .13) { // the dark doorway in the end that faces us | |
| 130 | const half = 1.2 * (1 - z / top); | |
| 131 | capsule([cx - half, cy + 1.62, z], [cx + half, cy + 1.62, z], .1, .1, woodOf(TENT_DARK), 'solid'); | |
| 132 | } | |
| 133 | for (const z of [.4, 1.1, 1.8]) castShadow(cx, cy, z, 1.1 * (1 - z / top) + .5, 1, .2); | |
| 134 | for (let y = -1.2; y <= 1.2; y += .6) castShadow(cx + 1.2, cy + y, .4, .7, 1, .2); | |
| 135 | } | |
| 136 | // the drops that warn of rain: fixed spots on the roof slope that faces us | |
| 137 | const DROPS = Array.from({ length: 14 }, (_, n) => { | |
| 138 | const t = .12 + hash(n, 4) * .72, along = (hash(n, 9) - .5) * 2.5; | |
| 139 | return project(TENT_AT[0] + 1.5 * (1 - t) + .12, TENT_AT[1] + along, 1.9 * t + .1); | |
| 140 | }); | |
| 141 | function pine(pos, scale, seed) { | |
| 142 | const [x, y] = pos; | |
| 143 | capsule([x, y, 0], [x, y, 2.2 * scale], .26 * scale, .14 * scale, woodOf(PINE_TRUNK), 'rough'); | |
| 144 | for (let tier = 0; tier < 5; tier++) { | |
| 145 | const z = (.9 + tier * 1.15) * scale, radius = (1.9 - tier * .36) * scale, count = 12 - tier * 2; | |
| 146 | for (const [ring, share] of [[radius, 1], [radius * .5, .55]]) { | |
| 147 | const n = Math.max(3, Math.round(count * share)); | |
| 148 | for (let j = 0; j < n; j++) { | |
| 149 | const k = seed * 1000 + tier * 100 + j + (ring === radius ? 0 : 50), a = j / n * Math.PI * 2 + hash(k, 1) * .6; | |
| 150 | const px = x + Math.cos(a) * ring, py = y + Math.sin(a) * ring, pz = z - (ring === radius ? .25 : -.1) * scale + (hash(k, 2) - .5) * .2; | |
| 151 | const tone = hash(k, 3) + (tier - 2) * .08 + (ring === radius ? 0 : -.1); | |
| 152 | capture = []; | |
| 153 | sphere(px, py, pz, .4 * scale, PINE[tone > .7 ? 2 : tone > .3 ? 1 : 0], 'rough'); | |
| 154 | leaves.push({ x: px, y: py, z: pz, k, r: .42 * scale, reach: .45 + tier * .12, pixels: capture }); | |
| 155 | capture = null; | |
| 156 | } | |
| 157 | } | |
| 158 | } | |
| 159 | sphere(x, y, (1.15 * 5 + .5) * scale, .3 * scale, PINE[1], 'rough'); | |
| 160 | for (let z = 0; z <= 6; z += 1.2) castShadow(x, y, z * scale, Math.max(.4, (1.9 - z * .28) * scale), 1, .3); | |
outdated
|
||
| 161 | } | |
| 162 | function pile() { | |
| 163 | const [tx, ty] = HEAPS.twig, [bx, by] = HEAPS.branch, [lx, ly] = HEAPS.log, [dx, dy] = uv(1, 0), [ex, ey] = uv(0, 1); | |
| 164 | for (let n = 0; n < 12; n++) { // twigs crossed in a little stack | |
| 165 | const s = n % 2 ? 1 : -1, off = (hash(n, 1) - .5) * .5, lift = .1 + (n >> 2) * .1; | |
| 166 | capsule([tx - dx * .6 * s + ex * off, ty - dy * .6 * s + ey * off, lift], [tx + dx * .6 * s - ex * off, ty + dy * .6 * s - ey * off, lift + .05], .09, .09, woodOf(WOOD), 'rough'); | |
| 167 | } | |
| 168 | for (let n = 0; n < 5; n++) { | |
| 169 | const off = (n - 2) * .26, lift = .14 + (n === 2 ? .2 : 0); | |
| 170 | capsule([bx - dx * .8 + ex * off, by - dy * .8 + ey * off, lift], [bx + dx * .8 + ex * off, by + dy * .8 + ey * off, lift], .17, .16, woodOf(WOOD), 'rough'); | |
| 171 | } | |
| 172 | for (const [off, lift] of [[-.3, .26], [.3, .26], [0, .72]]) { | |
| 173 | capsule([lx - dx * .95 + ex * off, ly - dy * .95 + ey * off, lift], [lx + dx * .95 + ex * off, ly + dy * .95 + ey * off, lift], .27, .27, woodOf(WOOD), 'rough'); | |
| 174 | } | |
| 175 | for (const [x, y] of Object.values(HEAPS)) { castShadow(x, y, .3, .9, 1, .2); castShadow(x, y, .8, .6, 1, .2); } | |
| 176 | } | |
| 177 | function bake() { | |
| 178 | ground.fill(0); shadowMask.fill(0); staticShadow.fill(0); kinds.fill(0); | |
| 179 | tufts.length = 0; leaves.length = 0; | |
| 180 | zb.fill(-1e9); | |
| 181 | pix.fill(rgb(255, 255, 255)); | |
| 182 | for (let iy = 0; iy < H; iy++) for (let ix = 0; ix < W; ix++) { | |
| 183 | const g = unproject(ix + .5, iy + .5), i = iy * W + ix, c = groundColor(g.x, g.y, ix, iy, i); | |
| 184 | if (!c) continue; | |
| 185 | pix[i] = c; ground[i] = 1; | |
| 186 | zb[i] = (g.x + g.y) / R2 * COS; | |
| 187 | } | |
| 188 | growGrass(); | |
| 189 | groundZ.set(zb); | |
| 190 | stoneRing(); | |
| 191 | tent(); | |
| 192 | pile(); | |
| 193 | PINES.forEach(([pos, scale], n) => pine(pos, scale, n + 1)); | |
| 194 | bakeOcclusion(); | |
| 195 | staticShadow.set(shadowMask); | |
| 196 | applyShadows(groundZ); | |
| 197 | bakedPix.set(pix); | |
| 198 | bakedZ.set(zb); | |
| 199 | } | |
| 200 | ||
| 201 | /* ---------- the fire ---------- */ | |
| 202 | const fireAt = project(FIRE[0], FIRE[1], .25); | |
| 203 | const flickerOf = () => (STILL ? 0 : Math.floor(time * 8)); | |
| 204 | function drawPiece(f) { | |
| 205 | if (!slots.has(f)) { | |
| 206 | const taken = new Set(G.fuel.map(g => slots.get(g))); | |
| 207 | let s = 0; | |
| 208 | while (taken.has(s)) s++; | |
| 209 | slots.set(f, s); | |
| 210 | } | |
| 211 | const slot = slots.get(f), [len, r] = SIZE[f.type], a = slot * Math.PI / 2 + .5; | |
| 212 | const burnt = f.catchLeft > 0 ? 0 : Math.min(3, Math.floor((1 - f.burnLeft / Campfire.FUELS[f.type].burnTime) * 4)); | |
| 213 | const cx = FIRE[0] + Math.cos(a) * .55, cy = FIRE[1] + Math.sin(a) * .55, dx = Math.cos(a + 1.1), dy = Math.sin(a + 1.1); | |
| 214 | capsule([cx - dx * len / 2, cy - dy * len / 2, r], [cx + dx * len / 2, cy + dy * len / 2, r + .35], r, r, woodOf(WOODS[f.type][burnt]), 'solid'); | |
| 215 | castShadow(cx, cy, r + .2, len * .3, 1, .2); | |
| 216 | } | |
| 217 | function drawGlow() { // the fire is the only warm light: a dithered pool on the ground, small beside the clearing | |
| 218 | const strength = clamp(G.heat / 100, 0, 1), radius = (1.8 + 3.2 * strength) * S; | |
| 219 | if (G.heat < 4) return; | |
| 220 | const x0 = Math.max(0, Math.floor(fireAt.sx - radius)), x1 = Math.min(W - 1, Math.ceil(fireAt.sx + radius)); | |
| 221 | const y0 = Math.max(0, Math.floor(fireAt.sy - radius * SIN)), y1 = Math.min(H - 1, Math.ceil(fireAt.sy + radius * SIN)); | |
| 222 | for (let iy = y0; iy <= y1; iy++) for (let ix = x0; ix <= x1; ix++) { | |
| 223 | const i = iy * W + ix; | |
| 224 | if (!ground[i] || zb[i] !== bakedZ[i]) continue; | |
| 225 | const d = Math.hypot((ix + .5 - fireAt.sx) / radius, (iy + .5 - fireAt.sy) / (radius * SIN)); | |
| 226 | if (d < 1 && (1 - d) * 1.1 > bayerAt(ix, iy)) tint(i, 1.22, 1.07, .86); | |
| 227 | } | |
| 228 | } | |
| 229 | function setPixel(ix, iy, d, color) { | |
| 230 | if (ix < 0 || iy < 0 || ix >= W || iy >= H) return; | |
| 231 | const i = iy * W + ix; | |
| 232 | if (d <= zb[i]) return; | |
| 233 | pix[i] = color; | |
| 234 | } | |
| 235 | function drawFlames() { | |
| 236 | const flick = flickerOf(), h = Campfire.flameHeight(G.heat), d = fireAt.d + .25, bx = Math.round(fireAt.sx), by = Math.round(fireAt.sy); | |
| 237 | if (!h) { // embers, or ash once it is out | |
| 238 | const out = G.heat <= 0; | |
| 239 | for (let k = 0; k < 7; k++) { | |
| 240 | const lit = out || hash(k, flick) > .35; | |
| 241 | setPixel(bx - 3 + (k * 5) % 7, by - 1 + (k % 3), d, out ? ASH : EMBER[lit ? 0 : 1]); | |
| 242 | } | |
| 243 | return; | |
| 244 | } | |
| 245 | const maxHalf = Math.max(2, Math.round(h * .24)); | |
| 246 | for (let r = 0; r < h; r++) { | |
| 247 | const frac = r / h, sway = Math.round(Math.sin(flick * 1.7 + r * .25) * frac * 1.6); | |
| 248 | const half = Math.max(0, Math.round(maxHalf * Math.sqrt(1 - frac) + (hash(r, flick) > .8 ? 1 : 0) - frac * 1.2)); | |
| 249 | for (let dx = -half; dx <= half; dx++) { | |
| 250 | const heat = frac + (Math.abs(dx) <= half * .45 ? .28 : 0) - (Math.abs(dx) === half && half > 1 ? .22 : 0); | |
| 251 | setPixel(bx + dx + sway, by - r, d, FLAME[clamp(Math.floor(heat * 4), 0, 3)]); | |
| 252 | } | |
| 253 | } | |
| 254 | } | |
| 255 | function drawGauge() { // two ticks and a dotted line: the flames count while their tip sits between them | |
| 256 | const x = Math.round(fireAt.sx) - 18, lo = Campfire.flameHeight(Campfire.GOOD_MIN), hi = Campfire.flameHeight(Campfire.GOOD_MAX); | |
| 257 | const color = Campfire.isGood(G.heat) ? GOOD : MUTED, tip = Campfire.flameHeight(G.heat), dots = []; | |
| 258 | for (let r = lo; r <= hi; r += 2) dots.push([x, Math.round(fireAt.sy) - r]); | |
| 259 | for (const r of [lo, hi]) for (let k = -2; k <= 2; k++) dots.push([x + k, Math.round(fireAt.sy) - r]); | |
| 260 | haloDots(dots); | |
| 261 | drawDots(dots, color); | |
| 262 | drawDots([[x + 3, Math.round(fireAt.sy) - tip], [x + 4, Math.round(fireAt.sy) - tip]], color); | |
| 263 | } | |
| 264 | ||
| 265 | /* ---------- weather ---------- */ | |
| 266 | function drawTentDrops() { | |
| 267 | const weather = Campfire.weather(G); | |
| 268 | const count = weather === 'rain' ? DROPS.length : weather === 'warning' ? Math.ceil(Campfire.warning(G) * DROPS.length) : 0; | |
| 269 | for (let n = 0; n < count; n++) drawDots([[DROPS[n].sx, DROPS[n].sy], [DROPS[n].sx + 1, DROPS[n].sy]], DROP); | |
| 270 | } | |
| 271 | function drawRain() { | |
| 272 | if (Campfire.weather(G) !== 'rain') return; | |
| 273 | const frame = flickerOf(); | |
| 274 | for (let n = 0; n < 90; n++) { | |
| 275 | const x = Math.floor(hash(n, 1) * W) + (frame % 7), y = Math.floor(hash(n, 2) * H + frame * 11 + n * 5) % H; | |
| 276 | for (let k = 0; k < 3; k++) { | |
| 277 | const ix = (x + k) % W, iy = y + k * 2; | |
| 278 | if (iy >= H || bakedPix[iy * W + ix] === 0xffffffff) continue; | |
| 279 | pix[iy * W + ix] = STREAK; | |
| 280 | } | |
| 281 | } | |
| 282 | } | |
| 283 | ||
| 284 | /* ---------- hud ---------- */ | |
| 285 | let shown = ''; | |
| 286 | function renderHud() { | |
| 287 | const left = Math.max(0, Math.ceil(Campfire.NIGHT - G.time)); | |
| 288 | const text = [Math.floor(G.score), G.best, left, G.phase, G.sel, G.outcome].join('|'); | |
| 289 | if (text === shown) return; | |
| 290 | shown = text; | |
| 291 | nodes.score.textContent = Math.floor(G.score) + ' s'; | |
| 292 | nodes.meta.textContent = (G.best ? 'best ' + G.best + ' s · ' : '') + (G.phase === 'play' ? left + ' s of night left' : 'night over'); | |
| 293 | Campfire.TYPES.forEach((t, k) => picks[t].setAttribute('aria-pressed', String(k === G.sel))); | |
| 294 | feedButton.disabled = G.phase !== 'play'; | |
| 295 | } | |
| 296 | function setStatus(text) { | |
| 297 | if (text === status) return; | |
| 298 | status = text; | |
| 299 | nodes.status.textContent = text; | |
| 300 | } | |
| 301 | function statusFor() { | |
| 302 | if (G.phase === 'over') { | |
| 303 | const seconds = Math.floor(G.score) + ' s at a good height' + (G.record ? ' · new best' : ''); | |
| 304 | return (G.outcome === 'dawn' ? 'dawn, the fire held · ' : 'the fire went out · ') + seconds + ' · tap to play again'; | |
| 305 | } | |
| 306 | const weather = Campfire.weather(G); | |
| 307 | if (weather === 'rain') return 'rain · it damps the flames'; | |
| 308 | if (weather === 'warning') return 'dots on the tent · rain is coming'; | |
| 309 | if (G.heat < Campfire.EMBERS + 8) return 'only embers · a twig will bring it back'; | |
| 310 | if (G.heat < Campfire.GOOD_MIN) return 'flames low · feed it'; | |
| 311 | if (G.heat > Campfire.GOOD_MAX) return 'too high · let it burn down'; | |
| 312 | return 'good height'; | |
| 313 | } | |
| 314 | ||
| 315 | /* ---------- actions ---------- */ | |
| 316 | function feed(type) { | |
| 317 | ensureAudio(); | |
| 318 | const how = Campfire.feed(G, type); | |
| 319 | if (how === 'placed') sound(type === 'twig' ? 'twig' : 'log'); | |
| 320 | else if (how === 'smothered') { sound('smother'); smotherHeard = true; } | |
Non-blocking: this sets the flag even when the smother leaves the fire alight. A later burn-out by decay then ends silently. Set it only when |
||
| 321 | return how; | |
| 322 | } | |
| 323 | function again() { | |
| 324 | if (G.phase === 'over' && overFor > .8) { Campfire.restart(G); time = 0; overFor = 0; lastOver = false; smotherHeard = false; lastWeather = 'dry'; } | |
| 325 | } | |
| 326 | function update(dt) { | |
| 327 | time += dt; | |
| 328 | Campfire.step(G, dt); | |
| 329 | const weather = Campfire.weather(G); | |
| 330 | if (weather === 'warning' && lastWeather !== 'warning') sound('ping'); | |
| 331 | lastWeather = weather; | |
| 332 | if (G.phase === 'over') { | |
| 333 | overFor += dt; | |
| 334 | if (!lastOver) { | |
| 335 | lastOver = true; | |
| 336 | if (G.outcome === 'dawn') sound('dawn'); | |
outdated
When a log or branch smothers the fire to 0, |
||
| 337 | else if (!smotherHeard) sound('smother'); | |
| 338 | if (G.record) try { localStorage.setItem(BEST_KEY, String(G.best)); } catch (e) { /* private window: keep it for this visit */ } | |
| 339 | } | |
| 340 | } | |
| 341 | setStatus(statusFor()); | |
| 342 | renderHud(); | |
| 343 | } | |
| 344 | ||
| 345 | /* ---------- drawing ---------- */ | |
| 346 | function drawHeld() { // the piece under the pointer while it is dragged | |
| 347 | if (!drag) return; | |
| 348 | const [len, r] = SIZE[drag.type], n = Math.round(len * 5), w = Math.max(1, Math.round(r * 9)), ink = rgb(...WOOD), dots = []; | |
| 349 | for (let k = -n; k <= n; k++) for (let t = 0; t < w; t++) dots.push([drag.sx + k, drag.sy + Math.round(k * .3) + t]); | |
| 350 | haloDots(dots); | |
| 351 | drawDots(dots, ink); | |
| 352 | } | |
| 353 | function drawPick() { | |
| 354 | const [x, y] = HEAPS[Campfire.TYPES[G.sel]], p = project(x, y, 0), ring = []; | |
outdated
Dead guard: |
||
| 355 | for (let k = 0; k < 18; k++) ring.push([Math.round(p.sx + Math.cos(k * Math.PI / 9) * 17), Math.round(p.sy + Math.sin(k * Math.PI / 9) * 8)]); | |
| 356 | haloDots(ring); | |
| 357 | drawDots(ring, rgb(255, 214, 10)); | |
| 358 | if (kb) drawArrow(p.sx, p.sy - 12, time); | |
| 359 | } | |
| 360 | function draw() { | |
| 361 | pix.set(bakedPix); | |
| 362 | zb.set(bakedZ); | |
| 363 | const windT = STILL ? 0 : Math.floor(time * WIND_FPS) / WIND_FPS; | |
| 364 | drawTufts(windT); | |
| 365 | G.fuel.forEach(drawPiece); | |
| 366 | drawLeaves(windT); | |
| 367 | applyShadows(bakedZ); | |
| 368 | shadeEdges(); | |
| 369 | drawGlow(); | |
| 370 | drawFlames(); | |
| 371 | drawTentDrops(); | |
| 372 | drawRain(); | |
| 373 | if (G.phase === 'play') { drawGauge(); drawPick(); } | |
| 374 | drawHeld(); | |
| 375 | ctx.putImageData(image, 0, 0); | |
| 376 | } | |
| 377 | ||
| 378 | /* ---------- input ---------- */ | |
| 379 | const at = e => { | |
| 380 | const box = canvas.getBoundingClientRect(); | |
| 381 | return { sx: (e.clientX - box.left) * W / box.width, sy: (e.clientY - box.top) * H / box.height }; | |
| 382 | }; | |
| 383 | const inRing = p => Math.hypot((p.sx - fireAt.sx) / 30, (p.sy - fireAt.sy) / 17) <= 1; | |
| 384 | function heapAt(p) { | |
| 385 | let found = null, near = 16; | |
| 386 | for (const t of Campfire.TYPES) { | |
| 387 | const q = project(...HEAPS[t], .4), d = Math.hypot(p.sx - q.sx, (p.sy - q.sy) * 1.4); | |
| 388 | if (d < near) { found = t; near = d; } | |
| 389 | } | |
| 390 | return found; | |
| 391 | } | |
| 392 | canvas.addEventListener('pointerdown', e => { | |
| 393 | canvas.focus({ preventScroll: true }); | |
| 394 | kb = false; | |
| 395 | if (G.phase !== 'play') { again(); return; } | |
| 396 | const p = at(e), type = heapAt(p); | |
| 397 | if (type) { | |
| 398 | Campfire.pick(G, type); | |
| 399 | drag = { type, ...p }; | |
| 400 | canvas.setPointerCapture(e.pointerId); | |
| 401 | } else if (inRing(p)) feed(Campfire.TYPES[G.sel]); | |
| 402 | }); | |
| 403 | canvas.addEventListener('pointermove', e => { if (drag) Object.assign(drag, at(e)); }); | |
| 404 | canvas.addEventListener('pointerup', e => { | |
| 405 | if (drag && inRing(at(e))) feed(drag.type); | |
| 406 | drag = null; | |
| 407 | }); | |
| 408 | canvas.addEventListener('pointercancel', () => { drag = null; }); | |
| 409 | canvas.addEventListener('keydown', e => { | |
| 410 | const by = { ArrowRight: 1, ArrowDown: 1, ArrowLeft: -1, ArrowUp: -1 }[e.key]; | |
| 411 | if (by) { e.preventDefault(); kb = true; if (G.phase === 'play') Campfire.select(G, by); return; } | |
| 412 | const n = { 1: 'twig', 2: 'branch', 3: 'log' }[e.key]; | |
| 413 | if (n) { kb = true; Campfire.pick(G, n); return; } | |
| 414 | if (e.key === ' ' || e.key === 'Enter') { | |
| 415 | e.preventDefault(); | |
| 416 | if (e.repeat) return; | |
| 417 | kb = true; | |
| 418 | if (G.phase === 'play') feed(Campfire.TYPES[G.sel]); else again(); | |
| 419 | } | |
| 420 | }); | |
| 421 | for (const t of Campfire.TYPES) picks[t].addEventListener('click', () => { Campfire.pick(G, t); canvas.focus({ preventScroll: true }); }); | |
| 422 | feedButton.addEventListener('click', () => { feed(Campfire.TYPES[G.sel]); canvas.focus({ preventScroll: true }); }); | |
| 423 | ||
| 424 | /* ---------- sound ---------- */ | |
| 425 | let audio = null, muted = false; | |
| 426 | function ensureAudio() { | |
| 427 | if (audio) { if (audio.state === 'suspended') audio.resume(); return; } | |
| 428 | const AC = window.AudioContext || window.webkitAudioContext; | |
| 429 | if (AC) audio = new AC(); | |
| 430 | } | |
| 431 | function tone(type, from, to, len, vol) { | |
| 432 | const now = audio.currentTime, gain = audio.createGain(), osc = audio.createOscillator(); | |
| 433 | osc.type = type; | |
| 434 | osc.frequency.setValueAtTime(from, now); | |
| 435 | osc.frequency.exponentialRampToValueAtTime(to, now + len); | |
| 436 | gain.gain.setValueAtTime(vol, now); | |
| 437 | gain.gain.exponentialRampToValueAtTime(.0005, now + len); | |
| 438 | osc.connect(gain).connect(audio.destination); | |
| 439 | osc.start(now); osc.stop(now + len + .01); | |
| 440 | } | |
| 441 | function sound(kind) { | |
| 442 | if (!audio || muted) return; | |
| 443 | if (kind === 'twig') tone('square', 880, 1200, .06, .04); | |
| 444 | else if (kind === 'log') tone('triangle', 180, 110, .18, .16); | |
| 445 | else if (kind === 'smother') tone('sawtooth', 300, 50, .45, .08); | |
| 446 | else if (kind === 'ping') tone('sine', 1040, 1000, .3, .08); | |
outdated
Nit: the braces around a single call aren't needed. The 'twig', 'log' and 'smother' branches above don't use them. |
||
| 447 | else if (kind === 'dawn') { tone('sine', 660, 650, .5, .1); tone('sine', 990, 980, .7, .07); } | |
| 448 | } | |
| 449 | const muteButton = document.getElementById('mute'); | |
| 450 | muteButton.addEventListener('click', () => { | |
| 451 | muted = !muted; | |
| 452 | muteButton.textContent = muted ? 'sound off' : 'sound on'; | |
| 453 | muteButton.setAttribute('aria-pressed', String(muted)); | |
| 454 | }); | |
| 455 | ||
| 456 | /* ---------- boot ---------- */ | |
| 457 | function fit() { | |
| 458 | const dpr = window.devicePixelRatio || 1; | |
| 459 | const fitScale = Math.min(innerWidth / W, (innerHeight - 40) / H); | |
| 460 | const scale = Math.max(1, Math.floor(fitScale * dpr)) / dpr; | |
| 461 | canvas.style.width = W * scale + 'px'; | |
| 462 | canvas.style.height = H * scale + 'px'; | |
| 463 | } | |
| 464 | addEventListener('resize', fit); | |
| 465 | fit(); | |
| 466 | bake(); | |
| 467 | setStatus(statusFor()); | |
| 468 | renderHud(); | |
| 469 | let last = performance.now(); | |
| 470 | function frame(now) { | |
| 471 | update(Math.min(.04, (now - last) / 1000)); | |
| 472 | last = now; | |
| 473 | draw(); | |
| 474 | requestAnimationFrame(frame); | |
| 475 | } | |
| 476 | requestAnimationFrame(frame); | |
| 477 | </script> | |
| 478 | </body> | |
| 479 | </html> |
index.html +1
| @@ -78,6 +78,7 @@ footer a::after { content: none; } | ||
| 78 | 78 | <li data-kind="Precision" data-input="Drag" style="--accent:#5fa3c4"><a href="window-washer/">Window Washer</a><span>Squeegee a grimy window clean before the cradle drops a floor: drag across it, bird droppings need two passes</span><span>Game forged by the crew</span><span class="tags">Precision · Drag</span></li> |
| 79 | 79 | <li data-kind="Precision" data-input="Drag" style="--accent:#c4503f"><a href="market-scale/">Market Scale</a><span>Weigh fruit onto a hanging pan scale to match each order: exact scores 3, within 50 g scores 1</span><span>Game forged by the crew</span><span class="tags">Precision · Drag</span></li> |
| 80 | 80 | <li data-kind="Precision" data-input="Tap" style="--accent:#8a5ea8"><a href="domino-run/">Domino Run</a><span>Set out a line of 20 dominoes along a chalk path and tip the first: a gap too wide stops the chain, too close topples one early, ring the bell for 10 plus 1 per domino left</span><span>Game forged by the crew</span><span class="tags">Precision · Tap</span></li> |
| 81 | <li data-kind="Precision" data-input="Drag" style="--accent:#e0793a"><a href="campfire/">Campfire</a><span>Keep a campfire burning through a 90 second night: drag twigs, branches and logs into the ring, a log smothers a small fire and rain damps the flames</span><span>Game forged by the crew</span><span class="tags">Precision · Drag</span></li> | |
| 81 | 82 | </ul> |
| 82 | 83 | <footer>Concept by <a href="https://x.com/awwbhi2">abhi</a>. Game by <a href="https://x.com/AmirMushich">AmirMushich</a>. Visual reference for the style: shawn.'s <a href="https://x.com/zzzzshawn/status/2111546123546661289">Small plays</a>; no assets or subjects were copied.<br><a href="stats/">Most played</a></footer> |
| 83 | 84 | </main> |
og/campfire.png added
og/home.png
serve.test.mjs +7
| @@ -156,6 +156,13 @@ test('Domino Run and its rules script are served without a track', async () => { | ||
| 156 | 156 | expect((await fetch(base + '/domino-run/chain.js')).status).toBe(200); |
| 157 | 157 | }); |
| 158 | 158 | |
| 159 | test('Campfire and its rules script are served without a track', async () => { | |
| 160 | const page = await fetch(base + '/campfire/'); | |
| 161 | expect(page.status).toBe(200); | |
| 162 | expect(page.headers.get('content-type')).toBe('text/html; charset=utf-8'); | |
| 163 | expect((await fetch(base + '/campfire/fire.js')).status).toBe(200); | |
| 164 | }); | |
| 165 | ||
| 159 | 166 | test('every game and the home page carry share-card tags and a served PNG card', async () => { |
| 160 | 167 | const { games, withTags, ORIGIN, CARD, HOME } = await import('./scripts/og.mjs'); |
| 161 | 168 | const { readFileSync } = await import('node:fs'); |
shell.js +1
| @@ -22,6 +22,7 @@ window.PixelPlays = (() => { | ||
| 22 | 22 | { id: 'window-washer', title: 'Window Washer', blurb: 'Squeegee a grimy window clean before the cradle drops a floor: drag across it, bird droppings need two passes', kind: 'Precision', input: 'Drag', accent: '#5fa3c4', music: false, forged: true }, |
| 23 | 23 | { id: 'market-scale', title: 'Market Scale', blurb: 'Weigh fruit onto a hanging pan scale to match each order: exact scores 3, within 50 g scores 1', kind: 'Precision', input: 'Drag', accent: '#c4503f', music: false, forged: true }, |
| 24 | 24 | { id: 'domino-run', title: 'Domino Run', blurb: 'Set out a line of 20 dominoes along a chalk path and tip the first: a gap too wide stops the chain, too close topples one early, ring the bell for 10 plus 1 per domino left', kind: 'Precision', input: 'Tap', accent: '#8a5ea8', music: false, forged: true }, |
| 25 | { id: 'campfire', title: 'Campfire', blurb: 'Keep a campfire burning through a 90 second night: drag twigs, branches and logs into the ring, a log smothers a small fire and rain damps the flames', kind: 'Precision', input: 'Drag', accent: '#e0793a', music: false, forged: true }, | |
| 25 | 26 | ]; |
| 26 | 27 | const MUSIC_VOLUME = .4; |
| 27 | 28 | const CREDITS = [ |
Only the sound-effects list changed. Issue #32 also asks for a Campfire row in the Games table above (after Domino Run): blurb, the "forged by the crew … not by the original creators" note, and the controls column (drag from the pile; arrows or 1/2/3 + space).