Spaces:
Running
Running
File size: 9,792 Bytes
1f56a51 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 | // Gummy Jar core: a merge-drop jar with real circle physics, no DOM (runs in the page and in node tests).
// Drop a gummy, two of the same size touch and plop into the next size up. Fixed 60 Hz ticks, 6 substeps, seeded
// pieces: everyone gets the same 50 drops in the Daily Jar. (c) 2026 CyberMax. All rights reserved.
export const HZ = 60;
export const W = 360; // world units (the canvas scales this to the screen)
export const H = 540;
export const L = 14, R = W - 14; // jar walls
export const FLOOR = H - 14;
export const LINE = 104; // the top line: a settled gummy above it for 2 s ends the round
export const DROP_Y = 58;
export const SUB = 6; // physics substeps per tick
export const G = 1500; // gravity, units/sยฒ
export const COOLDOWN = 24; // ticks between drops (0.4 s)
export const DAILY_DROPS = 50;
export const OVER_TICKS = 120; // 2 s above the line
export const MAX_TIER = 10;
export const RADII = [12, 16, 21, 26, 32, 39, 47, 56, 66, 77, 89];
export const NAMES = ['Speck', 'Pip', 'Drop', 'Bean', 'Bun', 'Puff', 'Plum', 'Dome', 'Loaf', 'Mallow', 'Sunny'];
export const COLORS = ['#FF6FAE', '#FF9A3C', '#FFC93C', '#86D93F', '#2FD39A', '#38C6F4', '#4A7DFF', '#9A66FF', '#DB5CF2', '#FF5A5F', '#FFB800'];
export const EMOJI = ['๐ฉท', '๐งก', '๐', '๐', '๐ฉต', '๐', '๐', '๐ฎ', '๐ธ', '๐', '๐'];
// points for making tier k (triangular numbers, like the classic merge games): 1, 3, 6, 10, 15 ...; a Sunny pair bursts for 100
export const PTS = RADII.map((_, k) => ((k + 1) * (k + 2)) / 2);
export const BURST = 100;
const SPAWN_W = [30, 28, 22, 12, 8]; // the first five sizes drop; bigger ones are only made by merging
function hash(str) {
let h = 0x811c9dc5;
for (let i = 0; i < str.length; i++) { h ^= str.charCodeAt(i); h = Math.imul(h, 0x01000193); }
return h >>> 0;
}
export function rng(seed) {
let a = typeof seed === 'string' ? hash(seed) : seed >>> 0;
return function () {
a = (a + 0x6d2b79f5) >>> 0;
let t = a;
t = Math.imul(t ^ (t >>> 15), t | 1);
t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
};
}
/** The drop order for a seed: the same list for everyone (the daily jar is `daily:<YYYY-MM-DD>`). */
export function pieces(seed, n = DAILY_DROPS + 2) {
const r = rng(`plopwick:${seed}`);
const tot = SPAWN_W.reduce((a, b) => a + b, 0);
const out = [];
for (let i = 0; i < n; i++) {
// the first three drops are small so the first merge comes within seconds
let x = r() * tot, k = 0;
while (x >= SPAWN_W[k]) { x -= SPAWN_W[k]; k++; }
out.push(i < 3 ? Math.min(k, 1) : k);
}
return out;
}
export const clampX = (x, tier) => Math.max(L + RADII[tier], Math.min(R - RADII[tier], x));
/**
* One jar. opts.limit = number of drops (Daily Jar 50; 0 = endless practice).
* Returns an object; the page calls drop(x) on release and tick() 60 times a second.
*/
export function makeGame(seed, opts = {}) {
const limit = opts.limit ?? DAILY_DROPS;
const order = pieces(seed, (limit || 400) + 2);
const g = {
seed, limit, balls: [], t: 0, score: 0, drops: 0, cool: 0, state: 'run', danger: 0, overT: 0,
chain: 0, lastMerge: -999, best: 0, merges: 0, bigChain: 0, bursts: 0, made: new Array(MAX_TIER + 1).fill(0),
events: [], endT: 0, nextId: 1,
cur: () => order[g.drops] ?? 0,
next: () => order[g.drops + 1] ?? 0,
left: () => (limit ? Math.max(0, limit - g.drops) : Infinity),
canDrop: () => g.state === 'run' && g.cool <= 0 && (!limit || g.drops < limit),
};
g.made[0] = 0;
g.drop = (x) => {
if (!g.canDrop()) return false;
const tier = g.cur();
const cx = clampX(x, tier);
g.balls.push(ball(cx, DROP_Y, tier));
g.drops++; g.cool = COOLDOWN;
g.best = Math.max(g.best, tier);
g.made[tier]++;
g.events.push({ type: 'drop', x: cx, tier });
return true;
};
function ball(x, y, tier, vx = 0, vy = 0) {
const dt = 1 / (HZ * SUB);
return { id: g.nextId++, x, y, px: x - vx * dt, py: y - vy * dt, r: RADII[tier], tier, age: 0, born: g.t, fresh: true, dead: false };
}
g.tick = () => {
if (g.state !== 'run' && g.state !== 'settle') return;
g.t++;
if (g.cool > 0) g.cool--;
const dt = 1 / (HZ * SUB);
for (let s = 0; s < SUB; s++) {
for (const b of g.balls) {
let vx = (b.x - b.px) * 0.998, vy = (b.y - b.py) * 0.998;
const max = 1300 * dt; // speed clamp keeps stacks calm
const sp = Math.hypot(vx, vy);
if (sp > max) { vx *= max / sp; vy *= max / sp; }
b.px = b.x; b.py = b.y;
b.x += vx; b.y += vy + G * dt * dt;
}
collide();
walls();
}
for (const b of g.balls) { b.age++; if (b.r < RADII[b.tier]) b.r = Math.min(RADII[b.tier], b.r + 1.5); } // a fresh merge grows into its size
g.balls = g.balls.filter((b) => !b.dead);
if (g.t - g.lastMerge > 45) g.chain = 0;
// over the line: any gummy older than 1 s whose top is above the line
const high = g.balls.some((b) => b.age > HZ && b.y - b.r < LINE);
g.overT = high ? g.overT + 1 : Math.max(0, g.overT - 3);
const top = g.balls.reduce((m, b) => (b.age > 20 ? Math.min(m, b.y - b.r) : m), H);
g.danger = Math.max(g.overT / OVER_TICKS, top < LINE + 60 ? (LINE + 60 - top) / 60 * 0.5 : 0);
if (g.overT >= OVER_TICKS) { g.state = 'over'; g.endT = g.t; g.events.push({ type: 'over' }); return; }
if (g.limit && g.drops >= g.limit && g.state === 'run') { g.state = 'settle'; g.endT = g.t; }
if (g.state === 'settle') {
const calm = g.balls.every((b) => Math.hypot(b.x - b.px, b.y - b.py) * HZ * SUB < 30);
if ((calm && g.t - g.endT > 45) || g.t - g.endT > 240) { g.state = 'done'; g.events.push({ type: 'done' }); }
}
};
function walls() {
for (const b of g.balls) {
if (b.x - b.r < L) { b.x = L + b.r; b.px = b.x + (b.x - b.px) * 0.2; }
if (b.x + b.r > R) { b.x = R - b.r; b.px = b.x + (b.x - b.px) * 0.2; }
if (b.y + b.r > FLOOR) {
const vy = b.y - b.py;
if (b.fresh && vy * HZ * SUB > 120) g.events.push({ type: 'land', x: b.x, y: FLOOR, tier: b.tier, v: vy * HZ * SUB });
b.fresh = false;
b.y = FLOOR - b.r;
b.py = b.y + vy * 0.15; // small bounce
b.px = b.x - (b.x - b.px) * 0.9; // floor friction
}
}
}
function collide() {
const bs = g.balls;
for (let i = 0; i < bs.length; i++) {
const a = bs[i];
if (a.dead) continue;
for (let j = i + 1; j < bs.length; j++) {
const b = bs[j];
if (b.dead) continue;
const dx = b.x - a.x, dy = b.y - a.y, rr = a.r + b.r;
const d2 = dx * dx + dy * dy;
if (d2 >= rr * rr || d2 === 0) continue;
if (a.tier === b.tier) { merge(a, b); if (a.dead) break; continue; }
const d = Math.sqrt(d2), o = Math.min(rr - d, 3) / d; // capped push: a big overlap never launches a gummy
const ma = a.r * a.r, mb = b.r * b.r, k = 0.5;
const fa = (mb / (ma + mb)) * o * k * 2, fb = (ma / (ma + mb)) * o * k * 2;
a.x -= dx * fa * 0.5; a.y -= dy * fa * 0.5;
b.x += dx * fb * 0.5; b.y += dy * fb * 0.5;
if (a.fresh || b.fresh) {
const v = Math.hypot(a.y - a.py, b.y - b.py) * HZ * SUB;
if (v > 120) g.events.push({ type: 'land', x: (a.x + b.x) / 2, y: (a.y + b.y) / 2, tier: Math.max(a.tier, b.tier), v });
a.fresh = false; b.fresh = false;
}
}
}
}
function merge(a, b) {
a.dead = true; b.dead = true;
const tier = a.tier + 1;
const x = (a.x + b.x) / 2, y = (a.y + b.y) / 2;
g.chain = g.t - g.lastMerge <= 45 ? g.chain + 1 : 1;
g.lastMerge = g.t;
g.bigChain = Math.max(g.bigChain, g.chain);
g.merges++;
const mult = Math.min(g.chain, 5);
if (tier > MAX_TIER) { // two Sunnies: they burst
g.score += BURST * mult; g.bursts++;
g.events.push({ type: 'burst', x, y, tier: MAX_TIER, chain: g.chain, pts: BURST * mult });
return;
}
const pts = PTS[tier] * mult;
g.score += pts;
g.best = Math.max(g.best, tier);
g.made[tier]++;
const vx = ((a.x - a.px) + (b.x - b.px)) / 2, vy = ((a.y - a.py) + (b.y - b.py)) / 2;
const n = ball(clampX(x, tier), y, tier);
n.r = RADII[tier - 1] + 2;
n.px = n.x - vx; n.py = n.y - vy + 0.8; // a little upward pop
n.fresh = false; n.age = HZ; // a merged gummy counts as settled for the top-line rule
g.balls.push(n);
g.events.push({ type: 'merge', x: n.x, y: n.y, tier, chain: g.chain, pts, id: n.id });
}
return g;
}
/** Spoiler-free share text: the size ladder you reached (Wordle-style), score and best chain. */
export function shareText(g, label) {
const ladder = EMOJI.map((e, k) => (k <= g.best ? e : 'โช')).join('');
const lines = [`Gummy Jar ${label} ยท ${g.score.toLocaleString('en-US')} pts`, ladder];
const extra = [`Biggest: ${NAMES[g.best]}`];
if (g.bigChain >= 2) extra.push(`${g.bigChain}x chain`);
if (g.bursts) extra.push(`โ๏ธ burst x${g.bursts}`);
lines.push(extra.join(' ยท '));
return lines.join('\n');
}
/** Test/bot helper: a drop x that puts the next gummy on a twin of the same size when one is reachable. */
export function botX(g) {
const t = g.cur();
const same = g.balls.filter((b) => b.tier === t).sort((a, b) => a.y - b.y);
if (same.length) return same[0].x;
// else the lowest spot: the column whose highest gummy sits lowest
let bestX = W / 2, bestTop = -1;
for (let x = L + RADII[t]; x <= R - RADII[t]; x += 12) {
const top = g.balls.reduce((m, b) => (Math.abs(b.x - x) < b.r + RADII[t] ? Math.min(m, b.y - b.r) : m), FLOOR);
if (top > bestTop) { bestTop = top; bestX = x; }
}
return bestX;
}
|