// The Royal Cat Sanctuary: a cozy garden where every cat you have befriended lives. // Pet them, give them treats, rename them, and buy toys and playground pieces with candy. // No hunger meters, no neglect, no timers: cats are always happy to see you. import * as THREE from 'three'; import { VoxelWorld, B } from '../world/voxels'; import { Body } from '../physics/body'; import type { Bouncer } from '../physics/body'; import { Player } from '../player/player'; import type { InputState } from '../input/input'; import { CatRig } from '../cats/catModel'; import type { Pose } 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 { audio } from '../audio'; import type { SaveV1 } from '../save/save'; import { persist } from '../save/save'; import { makeTrophy } from './characters'; import { HATS, HOODS, applyOutfit } from './wardrobe'; import { TinyRex } from './trex'; import { angleDiff, angleLerp, clamp, damp, lerp } from '../core/util'; type Kind = 'bed' | 'yarn' | 'trampoline' | 'tower' | 'slide' | 'pit' | 'fountain' | 'rainbow' | 'wand'; export interface DecoDef { id: Kind; name: string; emoji: string; price: number; about: string } export const DECOS: DecoDef[] = [ { id: 'bed', name: 'Cloud bed', emoji: '๐Ÿ›๏ธ', price: 0, about: 'A puffy bed for nap time.' }, { id: 'yarn', name: 'Yarn ball', emoji: '๐Ÿงถ', price: 0, about: 'Cats roll it around.' }, { id: 'trampoline', name: 'Trampoline', emoji: '๐Ÿคธ', price: 15, about: 'Boing! Cats and you can bounce.' }, { id: 'wand', name: 'Feather wand', emoji: '๐Ÿชถ', price: 20, about: 'Hold ๐Ÿชถ and the cats chase you.' }, { id: 'tower', name: 'Cat tower', emoji: '๐Ÿ—ผ', price: 25, about: 'Cats climb to the top and nap.' }, { id: 'slide', name: 'Rainbow slide', emoji: '๐Ÿ›', price: 30, about: 'Wheee! Cats slide down.' }, { id: 'pit', name: 'Candy ball pit', emoji: '๐ŸŽˆ', price: 35, about: 'Cats dive in and pop out.' }, { id: 'fountain', name: 'Fish fountain', emoji: 'โ›ฒ', price: 40, about: 'Sparkly water to watch.' }, { id: 'rainbow', name: 'Rainbow arch', emoji: '๐ŸŒˆ', price: 50, about: 'A magic rainbow over the garden.' }, ]; interface Deco { def: DecoDef; group: THREE.Group; spot: THREE.Vector3; users: number; anim?: (t: number, dt: number) => void; bouncer?: Bouncer } type HState = 'wander' | 'goto' | 'use' | 'sit' | 'sleep' | 'eat' | 'petted' | 'chase' | 'boop' | 'hello'; class HomeCat { rig: CatRig; body: Body; state: HState = 'wander'; timer = 0; yaw = Math.random() * 6.28; target = new THREE.Vector3(); deco: Deco | null = null; pal: HomeCat | null = null; // scripted moves (climb, slide, dive) ignore physics path: THREE.Vector3[] = []; pathT = 0; pathPose: Pose = 'stand'; petCount = 0; treatT = 0; constructor(public def: CatDef, world: VoxelWorld, at: THREE.Vector3) { this.rig = new CatRig(def.look); this.body = new Body(world); this.body.radius = 0.3 * def.look.scale; this.body.height = 0.9 * def.look.scale; this.body.pos.copy(at); this.target.copy(at); } get pos() { return this.body.pos; } active = true; } const DECO_SPOTS: [number, number][] = [[-6, -2], [6, -2], [-9, 5], [9, 5], [-10, -4], [10, -4], [-7, -9], [7, -9], [0, -4.6]]; const KIND_ORDER: Kind[] = ['bed', 'yarn', 'trampoline', 'wand', 'tower', 'slide', 'pit', 'fountain', 'rainbow']; export class Sanctuary { scene = new THREE.Scene(); world: VoxelWorld; player: Player; fx = new Particles(500); cats: HomeCat[] = []; decos = new Map(); bouncers: Bouncer[] = []; private t = 0; private camYaw = 0; private camPitch = 0.5; private manualCamT = 0; private camPos = new THREE.Vector3(0, 10, 16); private camLook = new THREE.Vector3(); private near: HomeCat | null = null; private ui: HTMLElement; private bar: HTMLElement; private card: HTMLElement; private candyEl: HTMLElement; private wandBtn: HTMLElement; private wandHeld = false; private overlay: HTMLElement | null = null; private sign: THREE.Group; private tmp = new THREE.Vector3(); private trophies: { g: THREE.Group; spin: number; i: number }[] = []; private ghost: THREE.Group | null = null; private rexes: TinyRex[] = []; private showOffT = 0; onExit: () => void = () => {}; active = false; constructor(private save: SaveV1, root: HTMLElement, private el: (h: string) => HTMLElement, private toast: (t: string) => void) { this.scene.background = new THREE.Color('#ffe6f2'); this.scene.add(makeSky('0.7,0.84,1.0', '1.0,0.9,0.95')); this.scene.add(new THREE.HemisphereLight('#dfe9ff', '#ffc9d9', 1.6)); const sun = new THREE.DirectionalLight('#fff3e0', 1.6); sun.position.set(20, 40, 15); this.scene.add(sun); this.world = this.buildGarden(); this.scene.add(this.world.buildMesh()); // a grass sheet just under the block tops hides the hairline gaps between soft blocks const under = new THREE.Mesh(new THREE.CircleGeometry(13.6, 40), softMaterial('#9fe6a0', 0)); under.rotation.x = -Math.PI / 2; under.position.y = 1.9; this.scene.add(under); this.scene.add(this.fx.mesh); this.player = new Player(this.world); applyOutfit(this.player, save.outfit); this.scene.add(this.player.root, this.player.shadowMesh); this.sign = this.makeSign(); this.scene.add(this.sign); // the tiny T-rex friends have a nest here too, and play with the cats const looks: [string, string, string, string, string][] = [['#9be39b', '#fff4c9', '#fff4c9', '#8fd3ff', '#ff7fb0'], ['#b9a6ff', '#ffe6f2', '#ffe6f2', '#ffcf3f', '#7cc8ff'], ['#ffb37a', '#fff1d6', '#e6f7ff', '#ff9cc9', '#8fe39b']]; looks.forEach((l, i) => { const r = new TinyRex(this.world, new THREE.Vector3(-2 + i * 2, 0, 2), l[0], l[1], l[2], l[3], l[4], i); r.hatchNow(); this.scene.add(r.root, r.toy); this.rexes.push(r); }); // UI (hidden until the player comes home) this.ui = el(``); root.appendChild(this.ui); this.bar = el(`
๐Ÿก Cat Home
๐Ÿฌ 0
`); this.ui.appendChild(this.bar); this.candyEl = this.bar.querySelector('.hc') as HTMLElement; this.card = el(``); this.ui.appendChild(this.card); this.wandBtn = el(``); this.ui.appendChild(this.wandBtn); const wandOn = (e: Event) => { e.preventDefault(); e.stopPropagation(); this.wandHeld = true; }; const wandOff = () => { this.wandHeld = false; }; this.wandBtn.addEventListener('pointerdown', wandOn); this.wandBtn.addEventListener('pointerup', wandOff); this.wandBtn.addEventListener('pointercancel', wandOff); this.wandBtn.addEventListener('pointerleave', wandOff); addEventListener('keydown', e => { if (this.active && e.code === 'KeyF' && !this.overlay) this.wandHeld = true; if (this.active && e.code === 'KeyT' && !this.overlay) this.treat(); }); addEventListener('keyup', e => { if (e.code === 'KeyF') this.wandHeld = false; }); 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 === 'shop') this.openShop(); if (a === 'name') this.openRename(); if (a === 'treat') this.treat(); }); } // ---------- world ---------- private buildGarden(): VoxelWorld { const S = 40, w = new VoxelWorld(S, 12, S), c = S / 2; for (let i = 0; i < S; i++) for (let k = 0; k < S; k++) { const x = i - c + 0.5, z = k - c + 0.5, d = Math.hypot(x, z) + Math.sin(Math.atan2(z, x) * 6) * 0.8; if (d > 16) continue; let h = 2; if (d > 14) h = 1; const ring = Math.abs(Math.hypot(x, z) - 4.5) < 0.8; const top = Math.hypot(x, z) < 3 ? ((Math.floor(x) + Math.floor(z)) % 2 ? B.SUGAR : B.PATH) : ring ? B.PATH : (Math.sin(x * 0.5) + Math.cos(z * 0.4) > 1.2 ? B.MINT : B.GRASS); w.fillColumn(i, k, h, top, B.COOKIE); } // the cats' cottage at the back const ci = (x: number) => Math.floor(x + c), ck = (z: number) => Math.floor(z + c); w.box(ci(-4), 2, ck(-14), ci(4), 5, ck(-11), B.SUGAR); // a solid backing inside the cottage hides the hairline gaps between its soft blocks { const back = new THREE.Mesh(new THREE.BoxGeometry(8.6, 3.6, 3.6), softMaterial("#f5e2b8", 0)); back.position.set(0.5, 4.0, -12); this.scene.add(back); } for (const [w2, d2, y2, col] of [[10.6, 5.6, 6.5, '#ff7aa2'], [8.6, 3.6, 7.5, '#ff7aa2'], [4.6, 1.6, 8.5, '#f59bbd']] as [number, number, number, string][]) { const r = new THREE.Mesh(new THREE.BoxGeometry(w2, 0.6, d2), softMaterial(col, 0)); r.position.set(0.5, y2, -12); this.scene.add(r); } w.box(ci(-1), 2, ck(-11), ci(0), 3, ck(-11), 0); w.box(ci(-5), 6, ck(-15), ci(5), 6, ck(-10), B.BERRY); w.box(ci(-4), 7, ck(-14), ci(4), 7, ck(-11), B.BERRY); w.box(ci(-2), 8, ck(-13), ci(2), 8, ck(-12), B.PINK); // candy-cane fence posts around the edge for (let a = 0; a < 28; a++) { const ang = a / 28 * Math.PI * 2; const x = Math.cos(ang) * 14.5, z = Math.sin(ang) * 14.5; if (z < -9 && Math.abs(x) < 6) continue; w.box(ci(x), 2, ck(z), ci(x), 2, ck(z), a % 2 ? B.BERRY : B.CLOUD); } // flowers const fcols = ['#ff7fb0', '#ffe066', '#b79bff', '#7cc8ff', '#ffffff']; const fgeo = new THREE.IcosahedronGeometry(0.16, 0); for (let n = 0; n < 70; n++) { const a = Math.random() * Math.PI * 2, r = 5.5 + Math.random() * 8.5, x = Math.cos(a) * r, z = Math.sin(a) * r; const f = new THREE.Mesh(fgeo, softMaterial(fcols[n % fcols.length], 0.3)); f.position.set(x, w.topAt(x, z) + 0.15, z); this.scene.add(f); } return w; } private makeSign(): THREE.Group { // a friendly wooden sign that shows when no cats live here yet const g = new THREE.Group(); const post = new THREE.Mesh(new THREE.BoxGeometry(0.2, 1.4, 0.2), softMaterial('#e8b98a')); post.position.y = 0.7; g.add(post); const board = new THREE.Mesh(new THREE.BoxGeometry(2.2, 0.9, 0.15), softMaterial('#fff3d6', 0.3)); board.position.y = 1.5; g.add(board); const heart = new THREE.Mesh(new THREE.SphereGeometry(0.22, 12, 8), softMaterial('#ff7fb0', 0.6)); heart.position.set(0, 1.5, 0.12); heart.scale.set(1, 0.9, 0.4); g.add(heart); g.position.set(0, 2, -4); return g; } private buildDeco(kind: Kind, spot: THREE.Vector3): Deco { const def = DECOS.find(d => d.id === kind)!; const g = new THREE.Group(); g.position.copy(spot); const d: Deco = { def, group: g, spot, users: 0 }; const M = (geo: THREE.BufferGeometry, col: string, gloss = 0.3) => new THREE.Mesh(geo, softMaterial(col, gloss)); if (kind === 'bed') { const base = M(new THREE.CylinderGeometry(1.1, 1.2, 0.35, 24), '#c9b6ff'); base.position.y = 0.18; g.add(base); const rim = M(new THREE.TorusGeometry(1.0, 0.28, 10, 28), '#ffffff', 0.2); rim.rotation.x = Math.PI / 2; rim.position.y = 0.4; g.add(rim); const pillow = M(new THREE.SphereGeometry(0.35, 12, 8), '#ffd1e6'); pillow.scale.set(1.2, 0.5, 0.8); pillow.position.set(0, 0.45, -0.55); g.add(pillow); } else if (kind === 'yarn') { const ball = M(new THREE.SphereGeometry(0.4, 16, 12), '#ff7fb0', 0.2); ball.position.y = 0.4; g.add(ball); for (let i = 0; i < 3; i++) { const r = M(new THREE.TorusGeometry(0.4, 0.035, 6, 24), '#ffb3d1'); r.rotation.set(i, i * 1.3, 0); r.position.y = 0.4; g.add(r); } d.anim = (t) => { ball.rotation.z = Math.sin(t * 0.7) * 0.6; }; } else if (kind === 'trampoline') { const frame = M(new THREE.TorusGeometry(1.1, 0.14, 8, 28), '#7cc8ff'); frame.rotation.x = Math.PI / 2; frame.position.y = 0.5; g.add(frame); const mat = M(new THREE.CylinderGeometry(1.05, 1.05, 0.08, 28), '#ffe066', 0.5); mat.position.y = 0.5; g.add(mat); for (let i = 0; i < 4; i++) { const leg = M(new THREE.CylinderGeometry(0.06, 0.06, 0.5, 6), '#ffffff'); const a = i / 4 * Math.PI * 2 + 0.4; leg.position.set(Math.cos(a), 0.25, Math.sin(a)); g.add(leg); } d.bouncer = { x: spot.x, z: spot.z, r: 1.1, top: spot.y + 0.55, power: 15, squash: 0 }; this.bouncers.push(d.bouncer); const b = d.bouncer; d.anim = (_t, dt) => { b.squash = Math.max(0, b.squash - dt * 4); mat.scale.y = 1 - b.squash * 0.6; mat.position.y = 0.5 - b.squash * 0.15; }; } else if (kind === 'tower') { const post = M(new THREE.CylinderGeometry(0.22, 0.26, 3.2, 10), '#f5e2b8'); post.position.y = 1.6; g.add(post); [0.9, 1.9, 2.9].forEach((h, i) => { const p = M(new THREE.CylinderGeometry(0.75 - i * 0.1, 0.75 - i * 0.1, 0.2, 18), ['#ffb3d1', '#b8e6ff', '#d7c2ff'][i]); p.position.set(i === 1 ? 0.35 : -0.2, h, 0); g.add(p); }); const top = M(new THREE.SphereGeometry(0.6, 14, 10), '#fff1a6'); top.scale.y = 0.35; top.position.y = 3.15; g.add(top); } else if (kind === 'slide') { const ladder = M(new THREE.BoxGeometry(0.9, 2.6, 0.3), '#ffffff'); ladder.position.set(0, 1.3, -1.4); g.add(ladder); const deck = M(new THREE.BoxGeometry(1.2, 0.2, 1.0), '#ffe066'); deck.position.set(0, 2.6, -1.0); g.add(deck); const ramp = new THREE.Group(); ramp.position.set(0, 2.6, -0.5); const cols = ['#ff7a9c', '#ffe57a', '#8fe39b', '#7cc8ff', '#b79bff']; cols.forEach((c, i) => { const s = M(new THREE.BoxGeometry(1.0 - 0.05, 0.08, 0.66), c, 0.6); s.position.set(0, -i * 0.5, 0.33 + i * 0.66); ramp.add(s); }); ramp.rotation.x = 0; g.add(ramp); // make the ramp a smooth incline ramp.children.forEach((s, i) => { s.position.y = -i * 0.5 - 0.1; }); } else if (kind === 'pit') { const tub = M(new THREE.CylinderGeometry(1.3, 1.2, 0.7, 24, 1, true), '#ffb3d1'); tub.position.y = 0.35; g.add(tub); const rim = M(new THREE.TorusGeometry(1.3, 0.12, 8, 28), '#ffffff'); rim.rotation.x = Math.PI / 2; rim.position.y = 0.7; g.add(rim); const cols = ['#ff7a9c', '#ffe57a', '#8fe39b', '#7cc8ff', '#b79bff']; const bg = new THREE.SphereGeometry(0.16, 8, 6); for (let i = 0; i < 46; i++) { const b = M(bg, cols[i % 5], 0.7); const a = Math.random() * 6.28, r = Math.random() * 1.1; b.position.set(Math.cos(a) * r, 0.45 + Math.random() * 0.2, Math.sin(a) * r); g.add(b); } } else if (kind === 'fountain') { const bowl = M(new THREE.CylinderGeometry(1.3, 1.0, 0.5, 24), '#e6d4ff'); bowl.position.y = 0.25; g.add(bowl); const water = M(new THREE.CylinderGeometry(1.15, 1.15, 0.05, 24), '#9fdcff', 0.9); water.position.y = 0.5; g.add(water); const col = M(new THREE.CylinderGeometry(0.16, 0.2, 1.0, 10), '#e6d4ff'); col.position.y = 1.0; g.add(col); const fish = M(new THREE.SphereGeometry(0.28, 12, 8), '#ff9a3c', 0.5); fish.scale.set(0.8, 1, 0.6); fish.position.y = 1.65; g.add(fish); const tail = M(new THREE.ConeGeometry(0.18, 0.3, 8), '#ff9a3c'); tail.position.y = 1.95; g.add(tail); const at = spot.clone().setY(spot.y + 2); d.anim = (t, dt) => { water.rotation.y = t * 0.3; fish.rotation.y = Math.sin(t) * 0.3; if (Math.random() < dt * 20) this.fx.emit(at, 1, { color: ['#9fdcff', '#ffffff'], speed: 1.2, up: 2.5, grav: 6, life: 0.8, size: 0.06 }); }; } else if (kind === 'rainbow') { const cols = ['#ff7a9c', '#ffb36b', '#ffe57a', '#8fe39b', '#7cc8ff', '#b79bff']; cols.forEach((c, i) => { const a = M(new THREE.TorusGeometry(3.2 - i * 0.22, 0.12, 8, 40, Math.PI), c, 0.4); a.position.y = 0.1; g.add(a); }); d.anim = (t, dt) => { if (Math.random() < dt * 4) this.fx.emit(this.tmp.set(spot.x + (Math.random() - 0.5) * 6, spot.y + 3, spot.z), 1, { color: cols, speed: 0.3, up: 0.5, grav: 0.5, life: 2, size: 0.08 }); }; } if (kind !== 'wand') this.scene.add(g); return d; } private placeDecos() { for (const kind of KIND_ORDER) { if (!this.save.owned.includes(kind) || this.decos.has(kind)) continue; const i = KIND_ORDER.indexOf(kind), [x, z] = DECO_SPOTS[i]; const spot = new THREE.Vector3(x, this.world.topAt(x, z), z); const d = this.buildDeco(kind, spot); if (kind === 'slide' || kind === 'rainbow') d.group.rotation.y = Math.atan2(-x, -z) + Math.PI; this.decos.set(kind, d); } } // ---------- coming and going ---------- enter() { this.active = true; this.ui.hidden = false; this.placeDecos(); // every befriended cat moves in (and stays forever) for (const id of this.save.caughtEver) { if (this.cats.some(c => c.def.id === id)) continue; const def = CAT_DEFS.find(d => d.id === id); if (!def) continue; const a = Math.random() * Math.PI * 2, r = 3 + Math.random() * 6; const at = new THREE.Vector3(Math.cos(a) * r, 2.5, Math.sin(a) * r); const c = new HomeCat(def, this.world, at); this.cats.push(c); this.scene.add(c.rig.root); } this.buildTrophies(); this.sign.visible = this.cats.length === 0; this.player.body.pos.set(0, 2.2, 6); this.player.body.vel.set(0, 0, 0); this.player.yaw = Math.PI; this.camYaw = 0; this.camPos.set(0, 9, 16); this.wandBtn.hidden = !this.save.owned.includes('wand'); this.refreshCandy(); audio.startMusic('home'); // everyone runs over to say hello for (const c of this.cats) { c.state = 'hello'; c.timer = 2.5 + Math.random(); } this.toast(this.cats.length ? '๐Ÿก Welcome home!' : '๐Ÿก Catch cats in the kingdom and they come to live here!'); } /** Trophy shelf in front of the cottage: one trophy per win, plus a see-through one for the next win. */ private buildTrophies() { const n = this.save.trophies.length; const bases = ['#b07dff', '#ff7fb0', '#7cc8ff', '#8fe39b', '#ffb36b']; const slot = (i: number) => { const row = Math.floor(i / 7), col = i % 7; const x = -4.5 + col * 1.5, z = -8.6 + row * 1.6; return new THREE.Vector3(x, this.world.topAt(x, z), z); }; while (this.trophies.length < Math.min(n, 21)) { const i = this.trophies.length, g = makeTrophy(); g.scale.setScalar(0.5); g.position.copy(slot(i)); const base = g.children[0] as THREE.Mesh; base.material = softMaterial(bases[i % bases.length], 0.8); this.scene.add(g); this.trophies.push({ g, spin: 0, i }); } if (this.ghost) this.scene.remove(this.ghost); if (n < 21) { const g = makeTrophy(); g.scale.setScalar(0.5); g.position.copy(slot(n)); const glass = new THREE.MeshBasicMaterial({ color: '#ffffff', transparent: true, opacity: 0.35, depthWrite: false }); g.traverse(o => { if ((o as THREE.Mesh).isMesh) (o as THREE.Mesh).material = glass; }); this.ghost = g; this.scene.add(g); } } private celebrate(tr: { g: THREE.Group; spin: number; i: number }) { tr.spin = 1.4; audio.play('trophy', { volume: 0.7 }); const w = this.save.trophies[tr.i]; this.fx.emit(this.tmp.copy(tr.g.position).setY(tr.g.position.y + 1.4), 70, { color: ['#ff7a9c', '#ffe57a', '#8fe39b', '#7cc8ff', '#b79bff', '#ffffff'], speed: 5, up: 4, size: 0.12, life: 1.5 }); if (w) this.toast(`๐Ÿ† #${tr.i + 1} ยท โฑ ${Math.floor(w.seconds / 60)}:${String(w.seconds % 60).padStart(2, '0')} ยท ๐Ÿฌ ${w.candy}`); this.player.happyT = 1.5; } leave() { this.active = false; this.ui.hidden = true; this.closeOverlay(); this.onExit(); } private refreshCandy() { this.candyEl.textContent = String(this.save.candy); } nameOf(def: CatDef) { return this.save.names[def.id] || def.name; } // ---------- interactions ---------- private pet(c: HomeCat) { c.petCount++; const poses: Pose[] = ['roll', 'flop', 'wiggle', 'spin', 'pancake']; c.state = 'petted'; c.timer = 1.6; c.pathPose = poses[c.petCount % poses.length]; c.rig.flash('happy', 1.8); audio.play('pet'); audio.play('purr', { volume: 0.7 }); if (c.petCount % 3 === 0) audio.meow(c.def.voice, 'happy'); this.player.hugT = 0.4; this.player.happyT = 1.2; this.fx.emit(this.tmp.copy(c.pos).setY(c.pos.y + 1), 10, { color: ['#ff7fb0', '#ff4d88', '#ffd1e6'], speed: 1.2, up: 1.6, grav: 0, life: 1.3, size: 0.13 }); } treat() { const c = this.near; if (!c || c.treatT > 0) return; c.treatT = 2.5; c.state = 'eat'; c.timer = 2.2; c.rig.flash('happy', 2.4); audio.play('treat'); this.fx.emit(this.tmp.copy(c.pos).setY(c.pos.y + 0.6), 14, { color: ['#e8b98a', '#ffe066', '#ffffff'], speed: 1.5, up: 2, size: 0.08 }); setTimeout(() => { audio.meow(c.def.voice, 'happy'); this.fx.emit(this.tmp.copy(c.pos).setY(c.pos.y + 1.2), 8, { color: ['#ff7fb0', '#ff4d88'], speed: 0.8, up: 1.4, grav: 0, life: 1.4, size: 0.13 }); }, 1500); } private closeOverlay() { if (this.overlay) { this.overlay.remove(); this.overlay = null; } this.ui.classList.remove('covered'); } private openRename() { const c = this.near; if (!c) return; audio.play('click'); this.closeOverlay(); const o = this.el(`

${c.def.emoji} New name

`); const inp = o.querySelector('input') as HTMLInputElement; inp.value = this.nameOf(c.def); o.addEventListener('click', e => { const x = (e.target as HTMLElement).closest('[data-x]')?.getAttribute('data-x'); if (x === 'ok') { const v = inp.value.replace(/[<>&"]/g, '').trim().slice(0, 14); if (v) { this.save.names[c.def.id] = v; persist(this.save); (this.card.querySelector('.nm') as HTMLElement).textContent = `${c.def.emoji} ${v}`; this.toast(`๐Ÿ’– Hello, ${v}!`); audio.meow(c.def.voice, 'happy'); } this.closeOverlay(); } if (x === 'no') this.closeOverlay(); }); inp.addEventListener('keydown', e => { if (e.key === 'Enter') (o.querySelector('[data-x="ok"]') as HTMLElement).click(); }); this.ui.appendChild(o); this.overlay = o; this.ui.classList.add('covered'); setTimeout(() => inp.focus(), 50); } private openShop() { audio.play('click'); this.closeOverlay(); const card = (key: string, emoji: string, name: string, about: string, price: number, own: boolean, wearing = false) => ``; const home = DECOS.filter(d => d.price > 0).map(d => card(d.id, d.emoji, d.name, d.about, d.price, this.save.owned.includes(d.id))).join(''); const hoods = HOODS.map(h => card('hood:' + h.id, h.emoji, h.name, '', h.price, h.price === 0 || this.save.owned.includes('hood:' + h.id), this.save.outfit.hood === h.id)).join(''); const hats = HATS.map(h => card('hat:' + h.id, h.emoji, h.name, '', h.price, h.price === 0 || this.save.owned.includes('hat:' + h.id), this.save.outfit.hat === h.id)).join(''); const o = this.el(`

Candy Shop

You have ๐Ÿฌ ${this.save.candy}

๐Ÿก For the cats

${home}

๐Ÿ‘— For you

${hoods}${hats}
`); o.addEventListener('click', e => { const id = (e.target as HTMLElement).closest('[data-buy]')?.getAttribute('data-buy'); if (!id) return; if (id === 'close') { audio.play('click'); this.closeOverlay(); return; } if (id.includes(':')) { this.wardrobe(id); return; } const d = DECOS.find(k => k.id === id)!; if (this.save.owned.includes(d.id)) return; if (this.save.candy < d.price) { audio.play('bump'); this.toast(`๐Ÿฌ ${d.price - this.save.candy} more candy!`); return; } this.save.candy -= d.price; this.save.owned.push(d.id); persist(this.save); audio.play('buy'); audio.play('unlock'); this.closeOverlay(); this.placeDecos(); this.refreshCandy(); this.wandBtn.hidden = !this.save.owned.includes('wand'); const deco = this.decos.get(d.id); if (deco && d.id !== 'wand') { this.fx.emit(this.tmp.copy(deco.spot).setY(deco.spot.y + 1.5), 80, { color: ['#ff7a9c', '#ffe57a', '#8fe39b', '#7cc8ff', '#b79bff'], speed: 5, up: 3, size: 0.13 }); } this.toast(`${d.emoji} ${d.name}!`); // a curious cat comes to try it right away const c = this.cats.find(k => k.state !== 'use'); if (c && deco && d.id !== 'wand') this.sendTo(c, deco); }); this.ui.appendChild(o); this.overlay = o; this.ui.classList.add('covered'); } /** Buy or put on a hoodie or hat. */ private wardrobe(key: string) { const [kind, id] = key.split(':'); const item = kind === 'hood' ? HOODS.find(h => h.id === id) : HATS.find(h => h.id === id); if (!item) return; const own = item.price === 0 || this.save.owned.includes(key); if (!own) { if (this.save.candy < item.price) { audio.play('bump'); this.toast(`๐Ÿฌ ${item.price - this.save.candy} more candy!`); return; } this.save.candy -= item.price; this.save.owned.push(key); audio.play('buy'); } if (kind === 'hood') this.save.outfit.hood = id; else this.save.outfit.hat = id; persist(this.save); applyOutfit(this.player, this.save.outfit); this.refreshCandy(); audio.play('wear'); this.player.happyT = 2; this.showOffT = 3; this.fx.emit(this.tmp.copy(this.player.body.pos).setY(this.player.body.pos.y + 1.2), 40, { color: ['#ffffff', '#ffe066', '#ff7fb0', '#b8e6ff'], speed: 3, up: 2, size: 0.1 }); this.toast(`${item.emoji} ${item.name}!`); this.closeOverlay(); } private sendTo(c: HomeCat, d: Deco) { c.deco = d; c.state = 'goto'; c.timer = 12; c.target.copy(d.spot); d.users++; } private freeDeco(c: HomeCat) { if (c.deco) { c.deco.users = Math.max(0, c.deco.users - 1); c.deco = null; } } /** Begin using a decoration: physical (trampoline) or a scripted path (tower, slide, ball pit). */ private use(c: HomeCat, d: Deco) { c.state = 'use'; c.path = []; c.pathT = 0; const s = d.spot, k = d.def.id; const v = (x: number, y: number, z: number) => new THREE.Vector3(s.x + x, s.y + y, s.z + z); if (k === 'bed') { c.timer = 7 + Math.random() * 5; c.pathPose = 'sleep'; c.path = [v((Math.random() - 0.5) * 0.6, 0.35, (Math.random() - 0.5) * 0.6)]; } else if (k === 'yarn') { c.timer = 5; c.pathPose = 'roll'; } else if (k === 'fountain' || k === 'rainbow') { c.timer = 5; c.pathPose = 'sit'; } else if (k === 'trampoline') { c.timer = 5; c.pathPose = 'stand'; c.target.copy(s); } else if (k === 'tower') { c.timer = 8; c.pathPose = 'sit'; c.path = [v(-0.2, 1.0, 0.4), v(0.35, 2.0, 0.3), v(0, 3.25, 0)]; } else if (k === 'slide') { const r = d.group.rotation.y, rot = (x: number, y: number, z: number) => { const p = new THREE.Vector3(x, y, z).applyAxisAngle(new THREE.Vector3(0, 1, 0), r); return p.add(s); }; c.timer = 3.6; c.pathPose = 'pancake'; c.path = [rot(0, 0, -1.7), rot(0, 2.7, -1.2), rot(0, 2.6, -0.3), rot(0, 1.4, 1.2), rot(0, 0.2, 3.3)]; } else if (k === 'pit') { c.timer = 3.2; c.pathPose = 'roll'; c.path = [v(0, 1.6, 0), v(0, 0.1, 0), v(0, 0.1, 0), v(0.3, 1.8, 0.3), v(1.8, 0, 1.8)]; } } // ---------- frame ---------- update(dt: number, inp: InputState, camera: THREE.PerspectiveCamera) { this.t += dt; const t = this.t; const overlay = !!this.overlay; const pin = overlay ? { ...inp, moveX: 0, moveY: 0, action: false, actionPressed: false } : inp; // nearest cat for petting const pp = this.player.body.pos; let near: HomeCat | null = null, nd = 2.4; const cur = this.near; if (cur && cur.path.length === 0 && Math.hypot(cur.pos.x - pp.x, cur.pos.z - pp.z) < 3.0) { near = cur; nd = -1; } for (const c of this.cats) { const d = Math.hypot(c.pos.x - pp.x, c.pos.z - pp.z); if (d < nd && Math.abs(c.pos.y - pp.y) < 2.5 && c.path.length === 0) { nd = d; near = c; } } // a cat next to you stops to say hello, so pets, treats and names go to the cat you see on the card if (near && (near.state === 'wander' || near.state === 'goto' || near.state === 'boop')) { this.freeDeco(near); near.state = 'hello'; near.timer = 3; } if (near !== this.near) { this.near = near; this.card.hidden = !near; if (near) (this.card.querySelector('.nm') as HTMLElement).textContent = `${near.def.emoji} ${this.nameOf(near.def)}`; } let allowJump = true; if (near && pin.actionPressed) { allowJump = false; this.pet(near); } else if (pin.actionPressed) { const tr = this.trophies.find(k => Math.hypot(k.g.position.x - pp.x, k.g.position.z - pp.z) < 1.3); if (tr) { allowJump = false; this.celebrate(tr); } } for (const tr of this.trophies) { tr.spin = Math.max(0, tr.spin - dt); tr.g.rotation.y += dt * (0.4 + tr.spin * 9); tr.g.position.y = this.world.topAt(tr.g.position.x, tr.g.position.z) + Math.sin(Math.min(1, tr.spin / 1.4) * Math.PI) * 0.8; } if (this.ghost) this.ghost.rotation.y += dt * 0.4; this.player.update(dt, pin, this.camYaw, this.bouncers, allowJump); if (this.player.justJumped) audio.play('jump'); if (this.player.body.bounced) audio.play('boing'); if (pp.y < -4) { pp.set(0, 3, 6); this.player.body.vel.set(0, 0, 0); } const wand = this.wandHeld && this.save.owned.includes('wand') && !overlay; if (wand && Math.random() < dt * 10) this.fx.emit(this.tmp.copy(pp).setY(pp.y + 1.6), 1, { color: ['#ffffff', '#ffd1e6', '#b8e6ff'], speed: 0.6, up: 0.4, grav: 1, life: 0.9, size: 0.07 }); for (const c of this.cats) this.updateCat(c, dt, wand); for (const d of this.decos.values()) d.anim?.(t, dt); for (const r of this.rexes) { const ev = r.update(dt, pp, this.cats, true, this.player.justJumped); if (ev === 'rawr') audio.play('rawr', { volume: 0.5 }); else if (ev === 'hatch') { r.root.visible = true; } } if (this.sign.visible) this.sign.rotation.y = Math.sin(t) * 0.1; this.fx.update(dt); // camera: same feel as the kingdom if (Math.abs(inp.camX) + Math.abs(inp.camY) > 0) { this.manualCamT = 2; this.camYaw -= inp.camX * 0.006; this.camPitch = clamp(this.camPitch + inp.camY * 0.004, 0.15, 1.1); } this.manualCamT -= dt; const speed = Math.hypot(this.player.body.vel.x, this.player.body.vel.z); if (this.manualCamT <= 0 && speed > 1) this.camYaw += angleDiff(this.camYaw, this.player.yaw + Math.PI) * damp(1.0, dt) * clamp(Math.abs(inp.moveX) + 0.25, 0, 1); this.showOffT -= dt; if (this.showOffT > 0) { this.player.yaw = this.camYaw; this.player.root.rotation.y = this.camYaw; } const dist = this.showOffT > 0 ? 4.2 : 8.5, p = this.showOffT > 0 ? 0.3 : this.camPitch; const want = this.tmp.set(pp.x + Math.sin(this.camYaw) * Math.cos(p) * dist, pp.y + 1.2 + Math.sin(p) * dist, pp.z + Math.cos(this.camYaw) * Math.cos(p) * dist); this.camPos.lerp(want, damp(6, dt)); this.camLook.lerp(this.tmp.copy(pp).setY(pp.y + 1), damp(10, dt)); camera.position.copy(this.camPos); camera.lookAt(this.camLook); const ax = this.camPos.x, az = this.camPos.z, vx = pp.x - ax, vz = pp.z - az, vl = vx * vx + vz * vz || 1; for (const d of this.decos.values()) { const px = d.spot.x - ax, pz = d.spot.z - az, u = (px * vx + pz * vz) / vl; const r = d.def.id === 'rainbow' ? 3.6 : 1.8; d.group.visible = !(u > 0 && u < 0.95 && Math.hypot(px - vx * u, pz - vz * u) < r); } for (const c of this.cats) { const px = c.pos.x - ax, pz = c.pos.z - az, u = (px * vx + pz * vz) / vl; c.rig.root.visible = !(u > 0 && u < 0.85 && Math.hypot(px - vx * u, pz - vz * u) < 1.3); } } private updateCat(c: HomeCat, dt: number, wand: boolean) { const b = c.body, p = b.pos, rig = c.rig, pp = this.player.body.pos; c.timer -= dt; c.treatT -= dt; let pose: Pose = 'stand', speed = 0; const dPlayer = Math.hypot(pp.x - p.x, pp.z - p.z); // the feather wand wins over everything except a scripted move if (wand && c.path.length === 0 && dPlayer < 14 && c.state !== 'chase') { this.freeDeco(c); c.state = 'chase'; } if (!wand && c.state === 'chase') { c.state = 'sit'; c.timer = 1.5; rig.flash('happy', 1); } switch (c.state) { case 'hello': { const n = this.cats.length, ha = this.camYaw + Math.PI + (n > 1 ? (this.cats.indexOf(c) / (n - 1) - 0.5) : 0) * 2.6; c.target.set(pp.x + Math.sin(ha) * 2.3, 0, pp.z + Math.cos(ha) * 2.3); } speed = Math.hypot(c.target.x - p.x, c.target.z - p.z) > 0.5 ? 4 : 0; if (speed === 0) this.yawTo(c, pp.x, pp.z, dt); if (speed === 0) { pose = 'sit'; rig.wag = 2; } if (c.timer < 0) { c.state = 'wander'; c.timer = 1; } break; case 'chase': { // run in playful circles around the player, pouncing at the feather const a = this.t * 1.5 + c.def.voice * 6; c.target.set(pp.x + Math.cos(a) * 1.8, 0, pp.z + Math.sin(a) * 1.8); speed = 5.5; if (b.grounded && Math.random() < dt * 1.2) { b.vel.y = 7; rig.flash('happy', 0.6); } break; } case 'wander': speed = Math.hypot(c.target.x - p.x, c.target.z - p.z) > 0.6 ? 2 : 0; if (c.timer < 0) this.decide(c); break; case 'goto': speed = 3.2; if (c.deco && Math.hypot(c.target.x - p.x, c.target.z - p.z) < (c.deco.def.id === 'trampoline' ? 0.6 : 1.4)) this.use(c, c.deco); else if (c.timer < 0) { this.freeDeco(c); c.state = 'wander'; c.timer = 1; } break; case 'use': { const k = c.deco?.def.id; pose = c.pathPose; if (k === 'trampoline') { c.target.copy(c.deco!.spot); speed = 1.2; pose = b.grounded ? 'stand' : 'wiggle'; if (b.bounced) { rig.flash('happy', 0.8); if (Math.random() < 0.3) audio.play('boing', { volume: 0.35 }); } } if (k === 'yarn' && c.deco) { const s = c.deco.spot; const a = this.t * 2; c.target.set(s.x + Math.cos(a) * 0.9, 0, s.z + Math.sin(a) * 0.9); speed = 1.5; pose = Math.sin(this.t * 3) > 0 ? 'roll' : 'wiggle'; } if ((k === 'fountain' || k === 'rainbow') && c.deco) { const s = c.deco.spot; this.yawTo(c, s.x, s.z, dt); } if (c.timer < 0 && c.path.length === 0) { this.freeDeco(c); c.state = 'wander'; c.timer = 1 + Math.random() * 2; this.pickWander(c); } break; } case 'sit': pose = 'sit'; if (c.timer < 0) this.decide(c); break; case 'sleep': pose = 'sleep'; rig.face = 'sleep'; if (c.timer < 0) this.decide(c); break; case 'eat': pose = 'eat'; this.yawTo(c, pp.x, pp.z, dt); if (c.timer < 0) { c.state = 'sit'; c.timer = 1.5; } break; case 'petted': pose = c.pathPose; if (c.timer < 0) { c.state = 'sit'; c.timer = 2; this.yawTo(c, pp.x, pp.z, 1); } break; case 'boop': { const pal = c.pal; if (!pal) { c.state = 'wander'; break; } c.target.copy(pal.pos); const d = Math.hypot(pal.pos.x - p.x, pal.pos.z - p.z); speed = d > 0.9 ? 2.6 : 0; if (d <= 0.9) { pose = 'sit'; this.yawTo(c, pal.pos.x, pal.pos.z, dt); if (c.timer > 0.5 && c.timer < 0.6) { this.fx.emit(this.tmp.copy(p).lerp(pal.pos, 0.5).setY(p.y + 1), 6, { color: ['#ff7fb0', '#ff4d88'], speed: 0.6, up: 1.2, grav: 0, life: 1.2, size: 0.12 }); c.rig.flash('happy', 1.2); pal.rig.flash('happy', 1.2); } } if (c.timer < 0) { c.pal = null; c.state = 'wander'; c.timer = 1; this.pickWander(c); } break; } } if (c.path.length) { // scripted move along a path (climb, slide, dive) const seg = c.deco?.def.id === 'slide' ? 0.55 : 0.7; c.pathT += dt / seg; const i = Math.min(Math.floor(c.pathT), c.path.length - 1); const from = i === 0 ? (c.path.length > 1 ? c.path[0] : p.clone()) : c.path[i - 1]; const to = c.path[i], f = clamp(c.pathT - i, 0, 1); if (c.path.length === 1) { p.lerp(to, damp(6, dt)); } else { const hop = Math.sin(f * Math.PI) * (c.deco?.def.id === 'slide' && i >= 3 ? 0 : 0.5); p.lerpVectors(from, to, f).y += hop; if (to.x !== from.x || to.z !== from.z) this.yawTo(c, to.x + (to.x - from.x), to.z + (to.z - from.z), dt); } pose = c.pathPose; if (c.deco?.def.id === 'slide' && i >= 3) { pose = 'pancake'; rig.flash('happy', 0.3); if (Math.random() < dt * 30) this.fx.emit(this.tmp.copy(p).setY(p.y + 0.3), 1, { color: ['#ff7a9c', '#ffe57a', '#8fe39b', '#7cc8ff'], speed: 1, size: 0.08 }); } if (c.deco?.def.id === 'pit' && i === 2 && f < 0.1) { audio.play('pop', { volume: 0.5 }); this.fx.emit(this.tmp.copy(p).setY(p.y + 0.7), 20, { color: ['#ff7a9c', '#ffe57a', '#8fe39b', '#7cc8ff', '#b79bff'], speed: 3, up: 4, size: 0.15 }); } const last = c.path.length === 1 ? c.timer < 0 : c.pathT >= c.path.length - (c.deco?.def.id === 'tower' ? 0 : 0); if (c.deco?.def.id === 'tower' && c.pathT >= c.path.length) { pose = 'sit'; if (c.timer < 0) { c.path = [p.clone(), c.deco.spot.clone().add(new THREE.Vector3(1.3, 0, 1.3))]; c.pathT = 0; c.timer = 99; c.deco = (this.freeDeco(c), null); c.pathPose = 'wiggle'; } } else if (last && c.pathT >= c.path.length) { c.path = []; b.vel.set(0, 0, 0); if (c.state === 'use' && c.deco?.def.id !== 'bed') { this.freeDeco(c); c.state = 'wander'; c.timer = 1.5; this.pickWander(c); } else if (!c.deco) { c.state = 'wander'; c.timer = 1; } } else if (c.path.length === 1 && c.timer < 0) { c.path = []; } b.vel.set(0, 0, 0); } else { const dx = c.target.x - p.x, dz = c.target.z - p.z, l = Math.hypot(dx, dz) || 1; const mx = speed > 0 ? dx / l : 0, mz = speed > 0 ? dz / l : 0; b.vel.x += (mx * speed - b.vel.x) * damp(b.grounded ? 10 : 3, dt); b.vel.z += (mz * speed - b.vel.z) * damp(b.grounded ? 10 : 3, dt); const blocked = b.step(dt, 24, this.bouncers); if (blocked && b.grounded && speed > 0.5) b.vel.y = 8; if (speed > 0.3) c.yaw = angleLerp(c.yaw, Math.atan2(b.vel.x, b.vel.z), damp(10, dt)); if (p.y < -4) p.set(0, 3, 0); } rig.pose = pose; rig.speed = c.path.length ? 0 : Math.hypot(b.vel.x, b.vel.z); rig.airborne = c.path.length ? false : !b.grounded; rig.vy = b.vel.y; rig.root.position.copy(p); rig.root.rotation.y = c.yaw; rig.root.userData.groundY = this.world.groundBelow(p.x, p.z, p.y + 0.1); const toP = Math.atan2(pp.x - p.x, pp.z - p.z); rig.lookYaw = dPlayer < 7 && pose !== 'sleep' ? ((toP - c.yaw + Math.PI * 3) % (Math.PI * 2)) - Math.PI : 0; rig.setMotionDrift(b.vel.x, b.vel.z); rig.update(dt); if (c === this.near) rig.root.scale.setScalar(c.rig.scale * (1 + Math.sin(this.t * 10) * 0.03)); else rig.root.scale.setScalar(c.rig.scale); } private yawTo(c: HomeCat, x: number, z: number, dt: number) { c.yaw = angleLerp(c.yaw, Math.atan2(x - c.pos.x, z - c.pos.z), clamp(damp(8, dt), 0, 1)); } private pickWander(c: HomeCat) { const a = Math.random() * Math.PI * 2, r = 2 + Math.random() * 9; c.target.set(Math.cos(a) * r, 0, Math.sin(a) * r); } private decide(c: HomeCat) { const r = Math.random(); const decos = [...this.decos.values()].filter(d => d.def.id !== 'wand' && d.users < (d.def.id === 'bed' || d.def.id === 'pit' || d.def.id === 'tower' ? 2 : 3)); if (r < 0.55 && decos.length) { this.sendTo(c, decos[Math.floor(Math.random() * decos.length)]); return; } if (r < 0.7 && this.cats.length > 1) { const pal = this.cats.filter(k => k !== c && k.path.length === 0)[0 | (Math.random() * (this.cats.length - 1))]; if (pal) { c.pal = pal; c.state = 'boop'; c.timer = 4; return; } } if (r < 0.82) { c.state = 'sit'; c.timer = 2 + Math.random() * 3; return; } if (r < 0.9) { c.state = 'sleep'; c.timer = 4 + Math.random() * 4; return; } c.state = 'wander'; c.timer = 3 + Math.random() * 3; this.pickWander(c); } } export { lerp };