// Chocolate Mountain: a gentle slide run around a chocolate mountain. // She rides a cookie sled down a railed slide (she cannot fall off), steers left and right, // and catches cats that slide along with her. A missed cat just pops up ahead again: no fail state. import * as THREE from 'three'; import { Player } from '../player/player'; import type { InputState } from '../input/input'; import { VoxelWorld } from '../world/voxels'; import { CatRig } from '../cats/catModel'; import { CAT_DEFS } from '../cats/cats'; import type { CatDef } from '../cats/cats'; import { Particles } from '../fx/particles'; import { softMaterial } from '../render/fur'; import { makeSky } from '../render/sky'; import { makeTrophy } from './characters'; import { applyOutfit } from './wardrobe'; import { audio } from '../audio'; import type { SaveV1 } from '../save/save'; import { persist } from '../save/save'; import { clamp, damp, lerp } from '../core/util'; const HALF = 2.4; // half width of the slide floor const BOWL = 2.9; // radius of the half-pipe cross-section const N = 700; // samples along the slide const CANDY_COLORS = ['#ff7a9c', '#ffe57a', '#8fe39b', '#7cc8ff', '#b79bff']; interface SlideCat { def: CatDef; rig: CatRig; s: number; x: number; h: number; vh: number; phase: number; caught: boolean; order: number; sx: number } interface Pickup { s: number; x: number; mesh: THREE.Object3D; taken: boolean; kind: 'candy' | 'boost' | 'ramp' } export class Mountain { scene = new THREE.Scene(); player: Player; fx = new Particles(600); active = false; onExit: () => void = () => {}; private curve: THREE.CatmullRomCurve3; private pts: THREE.Vector3[] = []; private tans: THREE.Vector3[] = []; private rights: THREE.Vector3[] = []; private L = 0; private s = 0; private x = 0; private h = 0; private vh = 0; private v = 0; private cats: SlideCat[] = []; private pickups: Pickup[] = []; private sled: THREE.Group; private ring: THREE.Mesh; private phase: 'count' | 'ride' | 'finish' = 'count'; private countT = 0; private t = 0; private boostT = 0; private runCandy = 0; private runT = 0; private camPos = new THREE.Vector3(); private camLook = new THREE.Vector3(); private ui: HTMLElement; private catRow: HTMLElement; private candyEl: HTMLElement; private endScreen: HTMLElement | null = null; private trophy: THREE.Group | null = null; private tmp = new THREE.Vector3(); private tmp2 = new THREE.Vector3(); constructor(private save: SaveV1, root: HTMLElement, private el: (h: string) => HTMLElement, private toast: (t: string) => void) { this.scene.background = new THREE.Color('#ffe9d6'); this.scene.add(makeSky('0.68,0.83,1.0', '1.0,0.88,0.8')); this.scene.add(new THREE.HemisphereLight('#e8f0ff', '#ffd1b0', 1.6)); const sun = new THREE.DirectionalLight('#fff3e0', 1.6); sun.position.set(30, 60, 20); this.scene.add(sun); this.scene.add(this.fx.mesh); // spiral path: two turns around the mountain, from the top down to the candy meadow const ctl: THREE.Vector3[] = []; for (let i = 0; i <= 40; i++) { const t = i / 40, a = t * Math.PI * 4 + 0.6, r = 13 + 21 * t, y = 58 * (1 - t) + 2; ctl.push(new THREE.Vector3(Math.cos(a) * r, y, Math.sin(a) * r)); } // flat run-out into the finish meadow const last = ctl[ctl.length - 1], dir = ctl[ctl.length - 1].clone().sub(ctl[ctl.length - 2]).setY(0).normalize(); for (let k = 1; k <= 3; k++) ctl.push(last.clone().addScaledVector(dir, k * 6).setY(2)); this.curve = new THREE.CatmullRomCurve3(ctl); this.L = this.curve.getLength(); const up = new THREE.Vector3(0, 1, 0); for (let i = 0; i <= N; i++) { const u = i / N; const p = this.curve.getPointAt(u), tn = this.curve.getTangentAt(u); this.pts.push(p); this.tans.push(tn); this.rights.push(new THREE.Vector3().crossVectors(tn, up).normalize()); } this.buildSlide(); this.buildScenery(); this.player = new Player(new VoxelWorld(4, 4, 4)); this.scene.add(this.player.root); this.sled = this.makeSled(); this.scene.add(this.sled); this.ring = new THREE.Mesh(new THREE.TorusGeometry(0.75, 0.08, 8, 32), new THREE.MeshBasicMaterial({ color: '#ffcf3f' })); this.ring.rotation.x = -Math.PI / 2; this.scene.add(this.ring); // UI this.ui = el(``); root.appendChild(this.ui); const bar = el(`
๐Ÿฌ 0
`); this.ui.appendChild(bar); this.catRow = bar.querySelector('.mt') as HTMLElement; this.candyEl = bar.querySelector('.hc') as HTMLElement; this.ui.addEventListener('click', e => { const a = (e.target as HTMLElement).closest('[data-a]')?.getAttribute('data-a'); if (a === 'exit') { audio.play('click'); this.leave(); } if (a === 'again') { audio.play('click'); this.start(); } }); } // ---------- building ---------- private frameAt(s: number) { const f = clamp(s / this.L, 0, 1) * N, i = Math.min(N - 1, Math.floor(f)), k = f - i; const p = this.tmp.copy(this.pts[i]).lerp(this.pts[i + 1], k); const r = this.tmp2.copy(this.rights[i]).lerp(this.rights[i + 1], k).normalize(); return { p, r, tn: this.tans[i] }; } /** Height of the bowl floor at sideways offset x. */ private bowlY(x: number) { const q = clamp(x / BOWL, -0.95, 0.95); return (1 - Math.cos(Math.asin(q))) * BOWL * 0.75; } private posAt(s: number, x: number, h: number, out: THREE.Vector3) { const { p, r } = this.frameAt(s); return out.copy(p).addScaledVector(r, x).setY(p.y + this.bowlY(x) + h); } private buildSlide() { const SEC = 13, verts: number[] = [], cols: number[] = [], idx: number[] = []; const choc = new THREE.Color('#8a5a3c'), choc2 = new THREE.Color('#a06a44'), cara = new THREE.Color('#f2b86b'); for (let i = 0; i <= N; i++) { const p = this.pts[i], r = this.rights[i]; const stripe = Math.floor((i / N) * this.L / 3) % 2 === 0; for (let j = 0; j <= SEC; j++) { const x = -BOWL * 0.97 + (j / SEC) * BOWL * 1.94; const y = this.bowlY(x); verts.push(p.x + r.x * x, p.y + y - 0.02, p.z + r.z * x); const c = j === 0 || j === SEC ? cara : stripe ? choc : choc2; cols.push(c.r, c.g, c.b); } } for (let i = 0; i < N; i++) for (let j = 0; j < SEC; j++) { const a = i * (SEC + 1) + j, b = a + SEC + 1; idx.push(a, b, a + 1, a + 1, b, b + 1); } const g = new THREE.BufferGeometry(); g.setAttribute('position', new THREE.Float32BufferAttribute(verts, 3)); g.setAttribute('color', new THREE.Float32BufferAttribute(cols, 3)); g.setIndex(idx); g.computeVertexNormals(); const m = new THREE.Mesh(g, new THREE.MeshLambertMaterial({ vertexColors: true, side: THREE.DoubleSide })); this.scene.add(m); // candy-cane rails on both sides for (const side of [-1, 1]) { const rp: THREE.Vector3[] = []; for (let i = 0; i <= N; i += 4) { const x = side * BOWL * 0.97; rp.push(this.pts[i].clone().addScaledVector(this.rights[i], x).setY(this.pts[i].y + this.bowlY(x) + 0.35)); } const rail = new THREE.Mesh(new THREE.TubeGeometry(new THREE.CatmullRomCurve3(rp), 600, 0.16, 8), softMaterial(side < 0 ? '#ff8fb8' : '#ffffff', 0.6)); this.scene.add(rail); // posts const postGeo = new THREE.CylinderGeometry(0.08, 0.08, 0.6, 6); const posts = new THREE.InstancedMesh(postGeo, softMaterial('#fff3d6'), Math.floor(N / 10) + 1); const m4 = new THREE.Matrix4(); let k = 0; for (let i = 0; i <= N; i += 10) { const q = rp[Math.min(rp.length - 1, Math.round(i / 4))]; m4.makeTranslation(q.x, q.y - 0.3, q.z); posts.setMatrixAt(k++, m4); } posts.count = k; this.scene.add(posts); } } private buildScenery() { // the chocolate mountain with frosting on top const mtn = new THREE.Mesh(new THREE.ConeGeometry(24, 66, 40, 6), softMaterial('#7a4a2c', 0.15)); mtn.position.y = 30; this.scene.add(mtn); const frost = new THREE.Mesh(new THREE.ConeGeometry(9.2, 25, 40, 2, true), softMaterial('#fff6fb', 0.4)); frost.position.y = 53; this.scene.add(frost); for (let i = 0; i < 9; i++) { const d = new THREE.Mesh(new THREE.SphereGeometry(0.9, 10, 8), softMaterial('#fff6fb', 0.4)); const a = i / 9 * Math.PI * 2; d.scale.set(1, 1.8, 1); d.position.set(Math.cos(a) * 8.7, 41.5, Math.sin(a) * 8.7); this.scene.add(d); } const cherry = new THREE.Mesh(new THREE.SphereGeometry(1.6, 16, 12), softMaterial('#ff4d6d', 1.2)); cherry.position.y = 66; this.scene.add(cherry); // candy meadow floor const ground = new THREE.Mesh(new THREE.CircleGeometry(90, 48), softMaterial('#a8e6a1', 0.05)); ground.rotation.x = -Math.PI / 2; ground.position.y = 0; this.scene.add(ground); // lollipop trees and gumdrops around the meadow (far from the slide) const cols = ['#ffb3d9', '#b8e6ff', '#d7c2ff', '#fff1a6', '#b9f5d0']; for (let n = 0; n < 40; n++) { const a = Math.random() * Math.PI * 2, r = 42 + Math.random() * 40, x = Math.cos(a) * r, z = Math.sin(a) * r; if (n % 2) { const stick = new THREE.Mesh(new THREE.CylinderGeometry(0.15, 0.15, 4, 6), softMaterial('#ffffff')); stick.position.set(x, 2, z); this.scene.add(stick); const top = new THREE.Mesh(new THREE.SphereGeometry(1.6, 14, 10), softMaterial(cols[n % 5], 0.8)); top.position.set(x, 4.6, z); this.scene.add(top); } else { const gd = new THREE.Mesh(new THREE.SphereGeometry(1.4, 14, 10, 0, Math.PI * 2, 0, Math.PI / 2), softMaterial(cols[n % 5], 1)); gd.position.set(x, 0, z); this.scene.add(gd); } } } private makeSled() { const g = new THREE.Group(); const cookie = new THREE.Mesh(new THREE.CylinderGeometry(0.75, 0.75, 0.16, 24), softMaterial('#e3a55f', 0.2)); cookie.scale.z = 1.35; g.add(cookie); const chips = new THREE.SphereGeometry(0.08, 6, 4); for (let i = 0; i < 9; i++) { const c = new THREE.Mesh(chips, softMaterial('#5a3420')); const a = i * 2.4, r = 0.2 + (i % 3) * 0.17; c.position.set(Math.cos(a) * r, 0.09, Math.sin(a) * r * 1.3); g.add(c); } return g; } // ---------- run ---------- debug() { return { phase: this.phase, s: Math.round(this.s), L: Math.round(this.L), caught: this.cats.filter(c => c.caught).length, candy: this.runCandy }; } /** Test hook: jump ahead along the slide and put the next free cat right next to the player. */ skip(ds: number) { this.s = Math.min(this.L - 9, this.s + ds); const c = this.cats.find(k => !k.caught); if (c) { c.s = this.s + 1; c.x = this.x; } return this.debug(); } enter() { this.active = true; this.ui.hidden = false; applyOutfit(this.player, this.save.outfit); this.start(); } leave() { this.active = false; this.ui.hidden = true; if (this.endScreen) { this.endScreen.remove(); this.endScreen = null; } this.onExit(); } start() { if (this.endScreen) { this.endScreen.remove(); this.endScreen = null; } if (this.trophy) { this.scene.remove(this.trophy); this.trophy = null; } this.s = 9; this.x = 0; this.h = 0; this.vh = 0; this.v = 0; this.phase = 'count'; this.countT = 0; this.runCandy = 0; this.runT = 0; this.boostT = 0; this.candyEl.textContent = '0'; // cats for (const c of this.cats) this.scene.remove(c.rig.root); this.cats = []; const defs = CAT_DEFS.filter(d => d.special !== 'golden' && d.special !== 'statue').slice(0, 8); defs.forEach((def, i) => { const rig = new CatRig({ ...def.look, scale: def.look.scale * 1.15 }); this.scene.add(rig.root); this.cats.push({ def, rig, s: 30 + i * ((this.L - 60) / defs.length) + Math.random() * 8, x: (Math.random() - 0.5) * 3, h: 0, vh: 0, phase: Math.random() * 6, caught: false, order: -1, sx: 0.6 + Math.random() * 0.5 }); }); this.catRow.innerHTML = defs.map(d => `๐Ÿฑ`).join(''); // candies in little lines, rainbow boost arrows and a few jump ramps for (const p of this.pickups) this.scene.remove(p.mesh); this.pickups = []; const candyGeo = new THREE.OctahedronGeometry(0.28, 0); for (let s = 20; s < this.L - 25; s += 9) { const lane = Math.sin(s * 0.05) * 1.6; for (let k = 0; k < 3; k++) { const m = new THREE.Mesh(candyGeo, softMaterial(CANDY_COLORS[(s + k) % 5 | 0], 1, 0.2)); this.scene.add(m); this.pickups.push({ s: s + k * 1.4, x: lane, mesh: m, taken: false, kind: 'candy' }); } } for (let s = 60; s < this.L - 40; s += 55) { const boost = new THREE.Group(); for (let k = 0; k < 3; k++) { const ch = new THREE.Mesh(new THREE.ConeGeometry(0.5, 0.6, 3), softMaterial(CANDY_COLORS[k + 1], 0.8, 0.5)); ch.rotation.x = Math.PI / 2; ch.position.z = k * 0.55; boost.add(ch); } const x = (Math.random() - 0.5) * 2.4; this.scene.add(boost); this.pickups.push({ s, x, mesh: boost, taken: false, kind: 'boost' }); } for (let s = 95; s < this.L - 40; s += 70) { const ramp = new THREE.Mesh(new THREE.BoxGeometry(HALF * 1.6, 0.25, 1.6), softMaterial('#ffe066', 0.6)); ramp.rotation.x = -0.25; this.scene.add(ramp); this.pickups.push({ s, x: 0, mesh: ramp, taken: false, kind: 'ramp' }); } for (const p of this.pickups) { const pos = this.posAt(p.s, p.x, p.kind === 'candy' ? 0.7 : 0.08, new THREE.Vector3()); p.mesh.position.copy(pos); const { tn } = this.frameAt(p.s); p.mesh.rotation.y = Math.atan2(tn.x, tn.z); p.mesh.visible = true; } audio.startMusic('garden'); this.toast('๐Ÿซ 3'); } private catchCat(c: SlideCat) { c.caught = true; c.order = this.cats.filter(k => k.caught).length - 1; audio.play('catch'); audio.meow(c.def.voice, 'happy'); c.rig.flash('happy', 1.5); this.player.happyT = 1.2; this.fx.emit(this.tmp.copy(c.rig.root.position).setY(c.rig.root.position.y + 0.8), 24, { color: ['#ff7fb0', '#ffd1e6', '#ffffff', '#ffe066'], speed: 4, up: 3, size: 0.13 }); const i = this.catRow.querySelector(`[data-id="${c.def.id}"]`); if (i) { i.className = 'got'; i.textContent = c.def.emoji; } this.toast(`๐Ÿ’– ${this.save.names[c.def.id] || c.def.name}!`); if (!this.save.caughtEver.includes(c.def.id)) { this.save.caughtEver.push(c.def.id); } this.addCandy(3); } private addCandy(n: number) { this.runCandy += n; this.save.candy += n; persist(this.save); this.candyEl.textContent = String(this.runCandy); } private finish() { this.phase = 'finish'; const got = this.cats.filter(c => c.caught).length; audio.startMusic('victory'); audio.play('trophy'); this.trophy = makeTrophy(); this.trophy.scale.setScalar(0.8); this.posAt(this.s + 3, 0, 1.5, this.trophy.position); this.scene.add(this.trophy); this.save.trophies.push({ date: new Date().toISOString().slice(0, 10), seconds: Math.round(this.runT), candy: this.runCandy, difficulty: this.save.settings.difficulty }); persist(this.save); setTimeout(() => { if (!this.active) return; this.endScreen = this.el(`
๐Ÿ†

Wheee!

๐Ÿฑ ${got} / ${this.cats.length} ยท ๐Ÿฌ ${this.runCandy}

`); this.ui.appendChild(this.endScreen); }, 3500); } update(dt: number, inp: InputState, camera: THREE.PerspectiveCamera) { this.t += dt; const t = this.t; if (this.phase === 'count') { const before = Math.ceil(2.4 - this.countT); this.countT += dt; const now = Math.ceil(2.4 - this.countT); if (now !== before && now > 0) { this.toast(['', '1', '2', '3'][now] ?? ''); audio.play('click'); } if (this.countT > 2.4) { this.phase = 'ride'; this.toast('๐Ÿพ Go!'); audio.play('whoosh'); } } const riding = this.phase === 'ride'; if (riding) this.runT += dt; // speed: gentle gravity along the slide, faster with joystick up, slower with down; boosts add a burst const target = this.phase === 'finish' ? 0 : riding ? 9.5 + inp.moveY * 3 + (this.boostT > 0 ? 6 : 0) : 0; this.v = lerp(this.v, target, damp(this.phase === 'finish' ? 2 : 1.6, dt)); this.boostT -= dt; this.s = Math.min(this.L - 1, this.s + this.v * dt); this.x = clamp(this.x + inp.moveX * 5.5 * dt * (riding ? 1 : 0), -HALF, HALF); // jump (button) when no cat is close; landing is always safe on the slide let near: SlideCat | null = null, nd = 1e9; for (const c of this.cats) { if (c.caught) continue; const ds = Math.abs(c.s - this.s), dx = Math.abs(c.x - this.x); if (ds < 3.4 && dx < 2.6 && ds + dx < nd) { nd = ds + dx; near = c; } } if (riding && near && (inp.actionPressed || (this.save.settings.difficulty === 0 && nd < 1.8))) { this.catchCat(near); near = null; } else if (riding && inp.actionPressed && this.h <= 0.01) { this.vh = 7; audio.play('jump'); } this.vh -= 22 * dt; this.h = Math.max(0, this.h + this.vh * dt); if (this.h === 0 && this.vh < 0) { if (this.vh < -4) audio.play('land', { volume: 0.4 }); this.vh = 0; } // pickups for (const p of this.pickups) { if (p.taken) continue; if (p.kind === 'candy') p.mesh.rotation.y += dt * 3; if (Math.abs(p.s - this.s) > 1.1 || Math.abs(p.x - this.x) > (p.kind === 'ramp' ? HALF : 1.1)) continue; if (p.kind === 'candy' && this.h < 1.8) { p.taken = true; p.mesh.visible = false; this.addCandy(1); audio.play('candy', { pitch: 1 + (this.runCandy % 8) * 0.06 }); this.fx.emit(p.mesh.position, 8, { color: CANDY_COLORS, speed: 2, size: 0.08 }); } if (p.kind === 'boost' && this.h < 0.5) { p.taken = true; this.boostT = 1.6; audio.play('whoosh'); audio.play('sparkle'); this.toast('๐ŸŒˆ Zoom!'); } if (p.kind === 'ramp' && this.h < 0.3) { p.taken = true; this.vh = 9; audio.play('boing'); this.toast('Wheee!'); } } // cats slide along (slower than her); a missed cat pops up again further down let k = 0; for (const c of this.cats) { if (c.caught) { // the two newest friends ride right behind her; the others ride further back, out of the camera's way const newest = this.cats.filter(k => k.caught).length - 1 - c.order; c.rig.root.visible = newest < 2 || this.phase === 'finish'; c.s = lerp(c.s, this.s - 1.5 * (newest + 1), damp(6, dt)); c.x = lerp(c.x, this.x + (newest % 2 ? 0.9 : -0.9), damp(3, dt)); c.h = this.h > 0 ? this.h * 0.8 : 0; c.rig.pose = 'sit'; c.rig.face = 'happy'; } else { c.phase += dt; if (riding) c.s += this.v * c.sx * 0.72 * dt; const close = c.s - this.s < 10 && c.s > this.s - 2; c.x = close ? lerp(c.x, this.x + Math.sin(c.phase * 2) * 0.6, damp(1.5, dt)) : clamp(Math.sin(c.phase * 0.9) * 2, -HALF, HALF); if (c.s < this.s - 8 && this.s < this.L - 60) { c.s = this.s + 40 + Math.random() * 30; c.x = (Math.random() - 0.5) * 3; this.toast(`๐Ÿ˜น ${this.save.names[c.def.id] || c.def.name}!`); } c.s = Math.min(c.s, this.L - 6 - k * 1.2); c.vh -= 22 * dt; c.h = Math.max(0, c.h + c.vh * dt); if (c.h === 0 && Math.random() < dt * 0.6) c.vh = 5; c.rig.pose = c.h > 0 ? 'wiggle' : 'pancake'; c.rig.face = Math.abs(c.s - this.s) < 6 ? 'smug' : 'happy'; k++; } const pos = this.posAt(c.s, c.x, c.h + 0.05, c.rig.root.position); const { tn } = this.frameAt(c.s); c.rig.root.rotation.y = Math.atan2(tn.x, tn.z); c.rig.root.userData.groundY = pos.y - c.h; c.rig.speed = 3; c.rig.airborne = c.h > 0.05; c.rig.vy = c.vh; c.rig.update(dt); } this.ring.visible = !!near && riding; if (near) { this.ring.position.copy(near.rig.root.position).setY(near.rig.root.position.y + 0.08); this.ring.scale.setScalar(1 + Math.sin(t * 10) * 0.08); } // player on her cookie sled const pp = this.posAt(this.s, this.x, this.h + 0.12, this.player.root.position); const { tn } = this.frameAt(this.s); const yaw = Math.atan2(tn.x, tn.z); this.player.root.rotation.set(0, yaw, -inp.moveX * 0.25); this.player.root.position.y += 0.1; this.sled.position.copy(pp); this.sled.rotation.set(0, yaw, -inp.moveX * 0.2); this.player.happyT = Math.max(this.player.happyT, this.boostT > 0 || this.h > 0.5 ? 0.2 : 0); if (riding && this.v > 8 && Math.random() < dt * 20) this.fx.emit(this.tmp.copy(pp).addScaledVector(tn, -0.8), 1, { color: ['#c58b5a', '#f2b86b', '#ffffff'], speed: 1, up: 1, grav: 4, life: 0.5, size: 0.08 }); if (this.phase !== 'finish' && this.s >= this.L - 8) this.finish(); if (this.trophy) { this.trophy.rotation.y += dt * 1.5; if (Math.random() < dt * 2) this.fx.emit(this.tmp.copy(this.trophy.position).add(new THREE.Vector3((Math.random() - 0.5) * 10, 5 + Math.random() * 4, (Math.random() - 0.5) * 10)), 40, { color: CANDY_COLORS, speed: 6, size: 0.13, life: 1.3, grav: 2 }); } for (const c of this.cats) if (this.phase === 'finish' && c.caught) c.rig.pose = 'dance'; this.fx.update(dt); // camera behind and above, looking down the slide const look = this.posAt(this.s + 6, this.x * 0.5, 0.8, new THREE.Vector3()); const want = this.posAt(Math.max(0, this.s - 7), this.x * 0.4, 4.6, new THREE.Vector3()); if (this.phase === 'finish') { want.copy(pp).add(new THREE.Vector3(Math.sin(t * 0.3) * 7, 4, Math.cos(t * 0.3) * 7)); look.copy(pp).setY(pp.y + 1); } if (this.camPos.lengthSq() === 0 || this.camPos.distanceTo(want) > 30) this.camPos.copy(want); this.camPos.lerp(want, damp(5, dt)); this.camLook.lerp(look, damp(8, dt)); camera.position.copy(this.camPos); camera.lookAt(this.camLook); } }