// Cat personalities and the lightweight behaviour brain (deterministic + probabilistic, no runtime AI calls). import * as THREE from 'three'; import { CatRig } from './catModel'; import type { CatLook, Pose } from './catModel'; import { Body } from '../physics/body'; import type { Bouncer } from '../physics/body'; import type { Level } from '../world/village'; import { angleLerp, clamp, damp, rand } from '../core/util'; import { audio } from '../audio'; import type { Particles } from '../fx/particles'; export type Special = 'tutorial' | 'bouncy' | 'popcorn' | 'hider' | 'tripper' | 'jitter' | 'sleeper' | 'sparkle' | 'statue' | 'golden'; export interface CatDef { id: string; name: string; emoji: string; look: CatLook; voice: number; speed: number; special: Special } const O = '#ff9a3c', C = '#fff1dc'; export const CAT_DEFS: CatDef[] = [ { id: 'pumpkin', name: 'Pumpkin', emoji: '🎃', voice: 0.6, speed: 0.6, special: 'tutorial', look: { top: O, bottom: C, scale: 1, accessory: 'leaf' } }, { id: 'marshmallow', name: 'Marshmallow', emoji: '☁️', voice: 0.1, speed: 0.62, special: 'bouncy', look: { top: '#ffb15c', bottom: '#ffffff', eye: '#2b5a9a', scale: 1.55, split: 0.25, accessory: 'none', earSize: 0.75, furLen: 1.3 } }, { id: 'popcorn', name: 'Popcorn', emoji: '🍿', voice: 1, speed: 0.8, special: 'popcorn', look: { top: '#ffb347', bottom: '#fff1a8', eye: '#6a4a1a', scale: 0.68, accessory: 'tiny', earSize: 1.2 } }, { id: 'whiskers', name: 'Captain Whiskers', emoji: '🧁', voice: 0.45, speed: 0.72, special: 'hider', look: { top: '#f78b2e', bottom: '#dff7ee', stripes: 1, eye: '#2f7a4a', scale: 1.05, accessory: 'hat' } }, { id: 'purr', name: 'Princess Purr', emoji: '👑', voice: 0.75, speed: 0.8, special: 'tripper', look: { top: '#ffa45e', bottom: '#ffd6e8', eye: '#7a3a8a', scale: 0.95, accessory: 'crown', furLen: 1.15 } }, { id: 'jellybean', name: 'Jellybean', emoji: '🍬', voice: 0.85, speed: 0.82, special: 'jitter', look: { top: '#ff8f4a', bottom: '#dcefff', split: 0.1, eye: '#2b5a9a', scale: 0.85, accessory: 'bow' } }, { id: 'cinnamon', name: 'Cinnamon', emoji: '💤', voice: 0.3, speed: 1.15, special: 'sleeper', look: { top: '#d9772f', bottom: '#ffe9cc', scale: 1.05, accessory: 'nightcap', stripes: 1 } }, { id: 'sparkle', name: 'Sparkle', emoji: '✨', voice: 0.9, speed: 0.78, special: 'sparkle', look: { top: '#ffab52', bottom: '#eee2ff', eye: '#5a3a9a', scale: 0.9, accessory: 'star', glow: 0.18 } }, { id: 'fluffington', name: 'Sir Fluffington', emoji: '🧐', voice: 0.2, speed: 0.75, special: 'statue', look: { top: '#f29240', bottom: C, stripes: 1, split: -0.3, scale: 1.2, accessory: 'monocle' } }, { id: 'golden', name: 'Golden Fluff', emoji: '🌟', voice: 0.7, speed: 0.95, special: 'golden', look: { top: '#ffd23f', bottom: '#fffbe8', scale: 1.1, accessory: 'wings', glow: 0.35, furLen: 1.25, eye: '#2b3a7a' } }, ]; export type CatState = 'wander' | 'idle' | 'flee' | 'tired' | 'sleep' | 'hide' | 'statue' | 'tease' | 'tag' | 'parade' | 'dance' | 'party' | 'bonk' | 'waiting' | 'seek'; export interface CatWorld { level: Level; playerPos: THREE.Vector3; playerSpeed: number; cats: Cat[]; fx: Particles; difficulty: number; bouncers: Bouncer[]; trail: THREE.Vector3[]; paused: boolean; } const tmp = new THREE.Vector3(); let lastMeow = 0; export class Cat { rig: CatRig; body: Body; state: CatState = 'wander'; timer = 0; fleeT = 0; caught = false; home: THREE.Vector3; target = new THREE.Vector3(); yaw = rand() * 6.28; paradeIndex = -1; tagTarget: Cat | null = null; braveT = 0; jumpQueue: number[] = []; specialT = 0; hideSpot: THREE.Vector3 | null = null; highlight = 0; id: string; active = true; meowT = 2 + rand() * 6; constructor(public def: CatDef, public world: CatWorld, spot: THREE.Vector3) { this.id = def.id; this.rig = new CatRig(def.look); this.body = new Body(world.level.world); this.body.radius = 0.3 * def.look.scale; this.body.height = 0.9 * def.look.scale; this.body.pos.copy(spot).y += 0.5; this.home = spot.clone(); this.target.copy(spot); if (def.special === 'sleeper') this.state = 'sleep'; if (def.special === 'statue') this.state = 'statue'; if (def.special === 'golden') { this.active = false; this.rig.root.visible = false; } } get pos() { return this.body.pos; } /** Resting cats are caught just by touching them (easy for small hands). */ get resting() { return ['tired', 'sleep', 'bonk', 'statue', 'hide', 'idle', 'party', 'seek'].includes(this.state); } get catchable() { return this.active && !this.caught && this.state !== 'waiting'; } private say(mood: 'happy' | 'tease' | 'sleepy' | 'surprised' | 'proud', vol = 1) { const d = this.pos.distanceTo(this.world.playerPos); const v = clamp(1 - d / 16, 0, 1) * vol; const now = performance.now(); if (now - lastMeow < 450) return; // never stack meows: keeps the mix clean on phones if (v > 0.08) { lastMeow = now; audio.meow(this.def.voice, mood, { volume: v * 0.8 }); this.rig.flash('meow', 0.4); } } private setState(s: CatState, t = 0) { this.state = s; this.timer = t; } startParade(index: number) { this.caught = true; this.paradeIndex = index; this.setState('parade'); this.rig.root.visible = true; } update(dt: number) { if (!this.active) return; const W = this.world, p = this.pos, pl = W.playerPos; const dx = p.x - pl.x, dz = p.z - pl.z; const d = Math.hypot(dx, dz); const diff = W.difficulty; // 0 easy, 1 medium, 2 hard const fleeR = (this.braveT > 0 ? 3.2 : 6.5) + diff * 1.2; const baseSpeed = 7.2 * this.def.speed * (0.68 + diff * 0.15); this.timer -= dt; this.braveT -= dt; this.specialT -= dt; this.meowT -= dt; let moveX = 0, moveZ = 0, speed = 0; let pose: Pose = 'stand'; const rig = this.rig; const sp = this.def.special; switch (this.state) { case 'parade': { const tr = W.trail; const idx = Math.min(tr.length - 1, 3 + (this.paradeIndex + 1) * 7); const tgt = tr[idx] ?? pl; tmp.set(tgt.x - p.x, 0, tgt.z - p.z); const l = tmp.length(); if (l > 0.4) { moveX = tmp.x / l; moveZ = tmp.z / l; speed = clamp(l * 3.2, 0, 10); } if (l > 14) { p.copy(tgt); p.y += 0.5; } if (this.jumpQueue.length && this.jumpQueue[0] <= 0) { this.jumpQueue.shift(); if (this.body.grounded) this.body.vel.y = 9; } this.jumpQueue = this.jumpQueue.map(v => v - dt); rig.face = 'happy'; rig.wag = 2; if (W.playerSpeed < 0.3 && l < 1.5) pose = 'sit'; // never crowd the player: keep a little circle of space around her and between friends if (d < 1.8) { moveX += dx / (d || 1) * 1.5; moveZ += dz / (d || 1) * 1.5; speed = Math.max(speed, 3); } for (const o of W.cats) { if (o === this || o.state !== 'parade') continue; const ox = p.x - o.pos.x, oz = p.z - o.pos.z, od = Math.hypot(ox, oz); if (od < 1.1 && od > 0.001) { moveX += ox / od; moveZ += oz / od; speed = Math.max(speed, 2.5); } } { const ml = Math.hypot(moveX, moveZ); if (ml > 1) { moveX /= ml; moveZ /= ml; } } break; } case 'dance': { pose = 'dance'; rig.face = 'happy'; break; } case 'seek': { // hide and seek: sit very still in the hiding place, tail swishing, giggling now and then pose = 'sit'; rig.face = d < 6 ? 'smug' : 'normal'; rig.wag = 2.5; if (this.meowT <= 0) { if (d < 7) { audio.play('giggle', { volume: clamp(1 - d / 7, 0, 0.5) }); rig.flash('smug', 0.8); } this.meowT = 3 + rand() * 3; } break; } case 'party': { // a surprise dance party: cats boogie together until the player comes close pose = 'dance'; rig.face = 'happy'; rig.wag = 3; if (this.specialT <= 0) { W.fx.emit(tmp.copy(p).setY(p.y + 1.3), 2, { color: ['#ff7fb0', '#7cc8ff', '#ffe066'], speed: 0.6, up: 1.5, grav: 0, life: 1.2, size: 0.12 }); this.specialT = 0.35; } if (this.timer <= 0 || d < 3.4) { this.setState(d < 3.4 ? 'flee' : 'wander'); this.fleeT = 0; } break; } case 'statue': { pose = 'statue'; rig.face = 'smug'; if (d < 3.2) { // the statue sneezes and topples over audio.play('sneeze', { volume: 0.9 }); W.fx.emit(tmp.copy(p).setY(p.y + 0.9), 24, { color: ['#ff7a9c', '#ffe57a', '#8fe39b', '#7cc8ff', '#b79bff'], speed: 4, size: 0.1 }); rig.flash('surprised', 0.8); this.setState('tired', 5); } break; } case 'sleep': { pose = 'sleep'; rig.face = 'sleep'; if (this.specialT <= 0) { if (d < 10) audio.play('snore', { volume: clamp(1 - d / 10, 0, 1) * 0.6 }); this.specialT = 2.6; } if (sp === 'sleeper' && d < 2.4 && this.wakes < 1) { // zoomies! (only once, then she stays sleepy and easy to catch) this.wakes++; rig.flash('surprised', 0.6); this.say('surprised'); this.setState('flee', 3); this.fleeT = 0; this.specialT = 99; } break; } case 'hide': { pose = 'sit'; rig.face = 'smug'; if (this.hideSpot) { p.set(this.hideSpot.x, this.hideSpot.y + 2.6, this.hideSpot.z); this.body.vel.set(0, 0, 0); } rig.wag = 3; if ((diff > 0 && d < 2.2) || this.timer <= 0) { audio.play('boing'); this.body.vel.set(dx / (d || 1) * 4, 9, dz / (d || 1) * 4); this.say('tease'); this.setState('flee', 0); this.fleeT = 0; this.hideSpot = null; this.braveT = 0; } break; } case 'tired': { pose = 'flop'; rig.face = 'dizzy'; if (this.timer <= 0) { this.setState('wander'); this.braveT = 3.5; } break; } case 'bonk': { pose = 'flop'; rig.face = 'dizzy'; if (this.timer <= 0) { this.setState('flee'); this.fleeT = 0; } break; } case 'tease': { pose = 'wiggle'; rig.face = 'smug'; this.yaw = angleLerp(this.yaw, Math.atan2(-dx, -dz) + Math.PI, damp(8, dt)); if (this.timer <= 0 || d < 3.4) { this.setState('flee'); } break; } case 'tag': { const t = this.tagTarget; if (!t || this.timer <= 0 || t.state !== 'flee' || t.caught) { this.setState('wander'); this.tagTarget = null; break; } tmp.set(t.pos.x - p.x, 0, t.pos.z - p.z); const l = tmp.length(); moveX = tmp.x / (l || 1); moveZ = tmp.z / (l || 1); speed = baseSpeed * 0.85; rig.face = 'happy'; if (l < 0.9) { // tag! roles swap audio.play('pop', { volume: clamp(1 - d / 18, 0, 0.6) }); W.fx.emit(tmp.copy(t.pos).setY(t.pos.y + 1), 10, { color: '#ffe066', size: 0.1 }); t.tagTarget = this; t.setState('tag', 3); this.setState('flee', 0); this.fleeT = 3; this.tagTarget = null; } if (d < fleeR) this.setState('flee'); break; } case 'flee': { this.fleeT += dt; // flee from player, or from a tagging cat let ax = dx, az = dz; const chaser = W.cats.find(c => c.state === 'tag' && c.tagTarget === this); if (d > fleeR + 4 && chaser) { ax = p.x - chaser.pos.x; az = p.z - chaser.pos.z; } const al = Math.hypot(ax, az) || 1; let fx = ax / al, fz = az / al; const r = Math.hypot(p.x, p.z); if (r > 22) { fx -= p.x / r * (r - 22) * 0.5; fz -= p.z / r * (r - 22) * 0.5; } // stay on the island if (sp === 'jitter' && this.specialT <= 0) { this.yaw += (rand() - 0.5) * 2.5; this.specialT = 0.7 + rand(); audio.play('giggle', { volume: clamp(1 - d / 16, 0, 0.6) }); } const want = Math.atan2(fx, fz); this.yaw = angleLerp(this.yaw, want, damp(sp === 'jitter' ? 2 : 4, dt)); moveX = Math.sin(this.yaw); moveZ = Math.cos(this.yaw); speed = baseSpeed * (sp === 'sleeper' && this.timer > 0 ? 1.15 : 1); rig.face = 'happy'; if (sp === 'popcorn' && this.body.grounded) { this.body.vel.y = diff > 0 ? 7 + rand() * 3 : 4 + rand() * 1.5; if (d < 9) audio.play('pop', { volume: clamp(1 - d / 9, 0, 0.4), pitch: 1.4 }); } if (sp === 'tripper' && this.fleeT > 2.2 && this.specialT <= 0 && rand() < dt * 0.6) { this.specialT = 5; this.say('surprised'); audio.play('bump'); this.setState('bonk', 1.6); break; } if (sp === 'sparkle' && this.specialT <= 0) { W.fx.emit(tmp.copy(p).setY(p.y + 0.4), 3, { color: ['#fff07a', '#ffffff', '#ffb3e6'], speed: 0.5, up: 0.6, grav: -0.3, life: 1.8, size: 0.09 }); this.specialT = 0.05; } if (sp === 'hider' && this.fleeT > 1.2) { const hs = W.level.hideSpots.find(h => Math.hypot(h.x - p.x, h.z - p.z) < 7 && Math.hypot(h.x - pl.x, h.z - pl.z) > 4); if (hs) { tmp.set(hs.x - p.x, 0, hs.z - p.z); this.yaw = angleLerp(this.yaw, Math.atan2(tmp.x, tmp.z), damp(10, dt)); moveX = Math.sin(this.yaw); moveZ = Math.cos(this.yaw); if (tmp.length() < 1.6) { this.hideSpot = hs; audio.play('pop'); this.setState('hide', 14); break; } } } if (sp === 'bouncy' && diff > 0 && d < 2.6 && this.body.grounded && this.specialT <= 0) { this.body.vel.y = 12; this.specialT = 3; audio.play('boing', { pitch: 0.7 }); } if (this.meowT <= 0) { this.say(rand() < 0.5 ? 'tease' : 'happy', 0.8); this.meowT = 2.5 + rand() * 3; } const tire = (sp === 'tutorial' ? 3 : 3.8 + diff * 1.4) + rand() * 0.5; if (this.fleeT > tire || (sp === 'sleeper' && this.timer <= 0 && this.timer > -1 && this.fleeT > 2.5)) { if (sp === 'sleeper') { this.setState('sleep'); this.specialT = 0.5; } else { this.setState('tired', 4.5 - diff); this.say('sleepy', 0.7); } this.fleeT = 0; break; } if (d > fleeR + 6 && !chaser) { this.setState('wander'); this.fleeT = Math.max(0, this.fleeT - 2); } break; } case 'idle': case 'wander': default: { if (this.state === 'idle') { pose = this.timer > 1.5 ? 'sit' : 'stand'; if (this.timer <= 0) this.setState('wander'); } else { tmp.set(this.target.x - p.x, 0, this.target.z - p.z); const l = tmp.length(); if (l < 0.6 || this.timer < -6) { const a = rand() * Math.PI * 2, rr = 2 + rand() * 6; this.target.set(this.home.x + Math.cos(a) * rr, 0, this.home.z + Math.sin(a) * rr); if (Math.hypot(this.target.x, this.target.z) > 23) this.target.multiplyScalar(0.7); this.setState(rand() < 0.5 ? 'idle' : 'wander', 2 + rand() * 2); } else { moveX = tmp.x / l; moveZ = tmp.z / l; speed = 1.6 * (sp === 'popcorn' ? 1.5 : 1); if (sp === 'popcorn' && this.body.grounded && rand() < dt * 2) this.body.vel.y = 6; } } rig.face = 'normal'; // surprise dance party when three friends meet if (rand() < dt * 0.05 && d > 8) { const group = W.cats.filter(c => c.active && !c.caught && (c.state === 'wander' || c.state === 'idle') && c.pos.distanceTo(p) < 7); if (group.length >= 3) { for (const c of group.slice(0, 4)) c.setState('party', 6); audio.play('sparkle', { volume: clamp(1 - d / 25, 0.15, 0.6) }); } } // play tag with friends if (rand() < dt * 0.08) { const other = W.cats.find(c => c !== this && c.active && !c.caught && (c.state === 'wander' || c.state === 'idle') && c.pos.distanceTo(p) < 6); if (other) { this.tagTarget = other; this.setState('tag', 3.5); other.setState('flee', 0); other.fleeT = 4; audio.play('giggle', { volume: clamp(1 - d / 18, 0, 0.6) }); } } if (d < fleeR) { if (d > 3.5 && rand() < 0.5) { this.say('tease'); this.setState('tease', 0.9 + rand() * 0.6); } else { this.say('surprised', 0.7); this.setState('flee'); } } if (this.meowT <= 0) { this.say('happy', 0.5); this.meowT = 5 + rand() * 6; } break; } } // pile-up bonks between fleeing cats if (this.state === 'flee') for (const o of W.cats) { if (o === this || !o.active || o.caught || o.state !== 'flee') continue; if (o.pos.distanceTo(p) < (this.body.radius + o.body.radius) * 1.3 && rand() < 0.5) { audio.play('bump'); W.fx.emit(tmp.copy(p).lerp(o.pos, 0.5).setY(p.y + 1), 8, { color: '#ffe066' }); this.setState('bonk', 1.2); o.setState('bonk', 1.2); this.say('surprised'); break; } } // movement & physics const b = this.body; const tx = moveX * speed, tz = moveZ * speed; b.vel.x += (tx - b.vel.x) * damp(b.grounded ? 10 : 3, dt); b.vel.z += (tz - b.vel.z) * damp(b.grounded ? 10 : 3, dt); if (this.state === 'hide') b.vel.set(0, 0, 0); const blocked = this.state === 'hide' ? false : b.step(dt, 24, W.bouncers); if (b.bounced) { audio.play('boing', { volume: clamp(1 - d / 18, 0, 0.7) }); this.say('happy', 0.6); } if (blocked && b.grounded && speed > 0.5) { // try a jump over the obstacle, otherwise turn const ahead = b.world.topAt(p.x + moveX * 0.8, p.z + moveZ * 0.8); if (ahead - p.y <= 2.6 && rand() < 0.7) b.vel.y = 9.5; else { this.yaw += (rand() < 0.5 ? 1 : -1) * 1.4; this.target.copy(this.home); } } if (p.y < -6) { p.copy(this.home); p.y += 1; b.vel.set(0, 0, 0); } if (speed > 0.3) this.yaw = angleLerp(this.yaw, Math.atan2(b.vel.x, b.vel.z), damp(10, dt)); if (this.state === 'tease') this.yaw = Math.atan2(-dx, -dz) + Math.PI; rig.pose = pose; rig.speed = Math.hypot(b.vel.x, b.vel.z); rig.airborne = !b.grounded; rig.vy = b.vel.y; rig.root.position.copy(p); rig.root.rotation.y = this.yaw; rig.root.userData.groundY = isFinite(b.groundY) ? b.world.groundBelow(p.x, p.z, p.y + 0.1) : p.y; // look at the player when near const toP = Math.atan2(pl.x - p.x, pl.z - p.z); rig.lookYaw = d < 9 ? ((toP - this.yaw + Math.PI * 3) % (Math.PI * 2)) - Math.PI : 0; rig.setMotionDrift(b.vel.x, b.vel.z); rig.update(dt); if (b.grounded && rig.airborne === false && Math.abs(b.vel.y) < 0.01 && this.landed === false) { rig.land(); } this.landed = b.grounded; } private landed = true; wakes = 0; }