// Tiny T-rex friends: they wait in pastel eggs, hatch when you come near (or after a while), // follow a cat around with little hops, then play with their own ball. No teeth, no fire, no biting. import * as THREE from 'three'; import { softMaterial } from '../render/fur'; import { Body } from '../physics/body'; import type { VoxelWorld } from '../world/voxels'; import { angleDiff, damp } from '../core/util'; export type RexState = 'egg' | 'crack' | 'follow' | 'play'; export type RexEvent = 'hatch' | 'rawr' | 'wobble' | 'sneeze' | 'ballhit' | null; interface CatLike { pos: THREE.Vector3; active: boolean } const ball = new THREE.SphereGeometry(1, 16, 12); function part(geo: THREE.BufferGeometry, color: string, gloss = 0.2) { return new THREE.Mesh(geo, softMaterial(color, gloss)); } export class TinyRex { root = new THREE.Group(); egg = new THREE.Group(); dino = new THREE.Group(); toy: THREE.Mesh; body: Body; state: RexState = 'egg'; private t = 0; private timer = 0; private rawrT = 3; private hop = 0; private yaw = 0; private copyT = -1; private sneezeT = 12 + Math.random() * 10; private hitCool = 0; /** The cat the ball just bumped (for a funny boing), set when update returns 'ballhit'. */ lastHit: CatLike | null = null; private eggWait: number; private target: CatLike | null = null; private toyVel = new THREE.Vector3(); private head = new THREE.Group(); private mouth: THREE.Mesh; private tail: THREE.Mesh; private armL: THREE.Mesh; private armR: THREE.Mesh; private shellBits: THREE.Mesh[] = []; constructor(private world: VoxelWorld, public home: THREE.Vector3, body: string, belly: string, eggCol: string, spot: string, toyCol: string, private seed: number) { this.body = new Body(world); this.body.radius = 0.25; this.body.height = 0.6; this.eggWait = 22 + seed * 9; // egg: pastel with polka spots const shell = part(ball, eggCol, 0.7); shell.scale.set(0.42, 0.55, 0.42); shell.position.y = 0.55; this.egg.add(shell); for (let i = 0; i < 6; i++) { const s = part(ball, spot, 0.5); const a = i / 6 * Math.PI * 2, h = 0.4 + (i % 2) * 0.3; s.scale.setScalar(0.08); s.position.set(Math.cos(a) * 0.38 * Math.sin(h * 2.2), 0.55 + (h - 0.55) * 0.6, Math.sin(a) * 0.38 * Math.sin(h * 2.2)); this.egg.add(s); } // little shell pieces that fly off when it hatches for (let i = 0; i < 5; i++) { const b = part(ball, eggCol, 0.7); b.scale.set(0.14, 0.06, 0.14); b.visible = false; this.root.add(b); this.shellBits.push(b); } // dino: round body, cream belly, big head, glossy eyes, tiny arms, little tail, pastel back bumps const d = this.dino; const torso = part(ball, body); torso.scale.set(0.3, 0.3, 0.36); torso.position.y = 0.42; d.add(torso); const bel = part(ball, belly); bel.scale.set(0.22, 0.24, 0.2); bel.position.set(0, 0.4, 0.17); d.add(bel); this.head.position.set(0, 0.78, 0.18); d.add(this.head); const hd = part(ball, body); hd.scale.set(0.27, 0.25, 0.3); this.head.add(hd); const snout = part(ball, body); snout.scale.set(0.2, 0.15, 0.18); snout.position.set(0, -0.05, 0.22); this.head.add(snout); for (const sx of [-1, 1]) { const eye = part(ball, '#2b2340', 0.9); eye.scale.setScalar(0.085); eye.position.set(sx * 0.13, 0.06, 0.22); this.head.add(eye); const hl = new THREE.Mesh(ball, new THREE.MeshBasicMaterial({ color: '#ffffff' })); hl.scale.setScalar(0.03); hl.position.set(sx * 0.13 + 0.025, 0.1, 0.295); this.head.add(hl); const blush = part(ball, '#ff9cc0'); blush.scale.set(0.05, 0.03, 0.02); blush.position.set(sx * 0.17, -0.05, 0.26); this.head.add(blush); const leg = part(ball, body); leg.scale.set(0.1, 0.13, 0.12); leg.position.set(sx * 0.15, 0.13, 0.02); d.add(leg); const foot = part(ball, belly); foot.scale.set(0.08, 0.04, 0.1); foot.position.set(sx * 0.15, 0.03, 0.09); d.add(foot); } this.mouth = part(ball, '#c2477a'); this.mouth.scale.set(0.07, 0.035, 0.03); this.mouth.position.set(0, -0.12, 0.39); this.head.add(this.mouth); this.armL = part(ball, body); this.armL.scale.set(0.04, 0.08, 0.04); this.armL.position.set(-0.14, 0.52, 0.3); d.add(this.armL); this.armR = this.armL.clone(); this.armR.position.x = 0.14; d.add(this.armR); this.tail = part(new THREE.ConeGeometry(0.15, 0.55, 10), body); this.tail.rotation.x = -Math.PI / 2 + 0.35; this.tail.position.set(0, 0.36, -0.42); d.add(this.tail); const spikeGeo = new THREE.ConeGeometry(0.06, 0.12, 6); for (let i = 0; i < 4; i++) { const s = part(spikeGeo, i % 2 ? '#ffb3d9' : '#fff1a6', 0.5); s.position.set(0, 0.7 - i * 0.1, 0.05 - i * 0.14); s.rotation.x = -0.4 - i * 0.2; d.add(s); } d.scale.setScalar(1.1); d.visible = false; this.root.add(this.egg, d); this.toy = part(ball, toyCol, 0.8); this.toy.scale.setScalar(0.2); this.toy.visible = false; const stripe = new THREE.Mesh(new THREE.TorusGeometry(1.0, 0.18, 6, 20), softMaterial('#ffffff', 0.6)); this.toy.add(stripe); this.reset(); } reset() { this.state = 'egg'; this.t = 0; this.timer = 0; this.target = null; this.body.pos.copy(this.home); this.body.pos.y = this.world.topAt(this.home.x, this.home.z); this.body.vel.set(0, 0, 0); this.egg.visible = true; this.egg.scale.setScalar(1); this.dino.visible = false; this.toy.visible = false; this.shellBits.forEach(b => (b.visible = false)); } hatchNow() { if (this.state === 'egg') { this.state = 'crack'; this.timer = 0; } } /** Returns 'hatch' when the dino pops out, 'rawr' for a cute squeak, 'wobble' while the egg rocks, else null. */ update(dt: number, playerPos: THREE.Vector3, cats: CatLike[], playing: boolean, playerJumped = false): RexEvent { this.t += dt; this.timer += dt; let ev: RexEvent = null; const p = this.body.pos; if (this.state === 'egg') { const near = Math.hypot(playerPos.x - p.x, playerPos.z - p.z) < 3; this.egg.rotation.z = Math.sin(this.t * 3 + this.seed) * 0.06; if (playing && (near || this.timer > this.eggWait)) { this.state = 'crack'; this.timer = 0; } } else if (this.state === 'crack') { this.egg.rotation.z = Math.sin(this.timer * 30) * 0.25 * (0.4 + this.timer); this.egg.position.y = Math.abs(Math.sin(this.timer * 14)) * 0.1; if (Math.floor(this.timer * 3) !== Math.floor((this.timer - dt) * 3)) ev = 'wobble'; if (this.timer > 1) { this.egg.visible = false; this.dino.visible = true; this.body.vel.y = 6; this.shellBits.forEach((b, i) => { b.visible = true; const a = i / 5 * Math.PI * 2; b.position.set(Math.cos(a) * 0.3, 0.6, Math.sin(a) * 0.3); b.userData.v = new THREE.Vector3(Math.cos(a) * 2, 3 + i * 0.3, Math.sin(a) * 2); }); this.state = 'follow'; this.timer = 0; this.target = null; ev = 'hatch'; } } else { // move: follow a cat, or chase and bump the toy ball let gx = p.x, gz = p.z, speed = 0; if (this.state === 'follow') { if (!this.target || !this.target.active || this.timer < dt || this.target.pos.distanceTo(playerPos) > 9) { const pool = cats.filter(c => c.active && c.pos.distanceTo(playerPos) < 7); this.target = pool.sort((a, b) => a.pos.distanceTo(p) - b.pos.distanceTo(p))[Math.floor(Math.random() * Math.min(3, pool.length))] || null; } if (!this.target) { // no cat nearby: stay close to the player instead const dx = p.x - playerPos.x, dz = p.z - playerPos.z, l = Math.hypot(dx, dz) || 1; gx = playerPos.x + dx / l * 2.4; gz = playerPos.z + dz / l * 2.4; speed = l > 3 ? Math.min(6, 2.4 + l * 0.3) : 0; if (l > 25) { p.set(gx, this.world.topAt(gx, gz) + 1, gz); } } if (this.target) { const tp = this.target.pos, dx = p.x - tp.x, dz = p.z - tp.z, l = Math.hypot(dx, dz) || 1; gx = tp.x + dx / l * 1.3; gz = tp.z + dz / l * 1.3; speed = l > 1.6 ? Math.min(5.5, 2.4 + l * 0.25) : 0; } if (this.timer > 9 + (this.seed % 3) * 1.5) { this.state = 'play'; this.timer = 0; this.toy.visible = true; this.toy.position.set(p.x + Math.sin(this.yaw) * 0.8, p.y + 0.6, p.z + Math.cos(this.yaw) * 0.8); this.toyVel.set(0, 2, 0); ev = 'rawr'; } } else { const tp = this.toy.position, l = Math.hypot(tp.x - p.x, tp.z - p.z); gx = tp.x; gz = tp.z; speed = l > 0.5 ? 2.6 : 0; if (l < 0.55 && this.toyVel.lengthSq() < 0.5) { // bump the ball with the nose const a = this.yaw + (Math.random() - 0.5) * 1.2; this.toyVel.set(Math.sin(a) * 3, 2.5, Math.cos(a) * 3); this.hop = 1; this.armL.rotation.x = this.armR.rotation.x = -1; } // ball physics: bounce on the ground, roll and slow down this.toyVel.y -= 14 * dt; tp.addScaledVector(this.toyVel, dt); const g = this.world.topAt(tp.x, tp.z) + 0.2; if (tp.y < g) { tp.y = g; this.toyVel.y = Math.abs(this.toyVel.y) * 0.35; this.toyVel.x *= 0.92; this.toyVel.z *= 0.92; if (this.toyVel.y < 0.4) this.toyVel.y = 0; } this.toyVel.x *= 1 - dt * 1.2; this.toyVel.z *= 1 - dt * 1.2; // the rolling ball can bonk into a cat: boing! this.hitCool -= dt; if (this.hitCool <= 0 && Math.hypot(this.toyVel.x, this.toyVel.z) > 1.2) { const c = cats.find(k => k.active && Math.hypot(k.pos.x - tp.x, k.pos.z - tp.z) < 0.8 && Math.abs(k.pos.y + 0.4 - tp.y) < 1.2); if (c) { this.lastHit = c; this.hitCool = 2; this.toyVel.x *= -0.6; this.toyVel.z *= -0.6; this.toyVel.y = 3; ev = 'ballhit'; } } if (tp.distanceTo(this.home) > 24) { tp.copy(p); tp.y += 0.6; this.toyVel.set(0, 0, 0); } this.toy.rotation.x += this.toyVel.z * dt * 4; this.toy.rotation.z -= this.toyVel.x * dt * 4; if (this.timer > 7) { this.state = 'follow'; this.timer = 0; this.toy.visible = false; this.target = null; } } const dx = gx - p.x, dz = gz - p.z, l = Math.hypot(dx, dz); if (speed > 0 && l > 0.05) { this.body.vel.x = dx / l * speed; this.body.vel.z = dz / l * speed; this.yaw += angleDiff(this.yaw, Math.atan2(dx, dz)) * damp(8, dt); if (this.body.grounded && Math.random() < dt * 2.5) this.body.vel.y = 3.2; // happy little hops } else { this.body.vel.x *= 0.8; this.body.vel.z *= 0.8; } if (this.body.step(dt, 20) && this.body.grounded) this.body.vel.y = 6; // hop over a block if (p.y < -6) p.copy(this.home).setY(this.world.topAt(this.home.x, this.home.z) + 1); // squeaky rawr now and then, when the player is close enough to see it // copy the player's jumps, a tiny moment later if (playerJumped && playerPos.distanceTo(p) < 9) this.copyT = 0.22 + this.seed * 0.08; if (this.copyT > 0) { this.copyT -= dt; if (this.copyT <= 0 && this.body.grounded) { this.body.vel.y = 6.5; this.hop = 1; } } // a tiny rainbow sneeze now and then this.sneezeT -= dt; if (this.sneezeT < 0) { this.sneezeT = 14 + Math.random() * 12; if (playerPos.distanceTo(p) < 12) { ev = 'sneeze'; this.hop = 1; } } this.rawrT -= dt; if (this.rawrT < 0) { this.rawrT = 4 + Math.random() * 5; if (playerPos.distanceTo(p) < 12) { ev = ev ?? 'rawr'; this.hop = 1; } } } // animate this.hop = Math.max(0, this.hop - dt * 2.5); const walk = Math.hypot(this.body.vel.x, this.body.vel.z); this.dino.rotation.y = this.yaw; this.dino.rotation.z = Math.sin(this.t * 12) * 0.08 * Math.min(1, walk / 2); this.tail.rotation.y = Math.sin(this.t * 7) * 0.4; this.head.rotation.x = -this.hop * 0.5; this.mouth.scale.y = 0.035 + this.hop * 0.06; const flap = Math.sin(this.t * 18) * 0.5 * (this.hop + 0.2); this.armL.rotation.x = this.armL.rotation.x * 0.9 + flap * 0.1; this.armR.rotation.x = this.armR.rotation.x * 0.9 - flap * 0.1; this.root.position.copy(p); for (const b of this.shellBits) { if (!b.visible) continue; const v = b.userData.v as THREE.Vector3; v.y -= 12 * dt; b.position.addScaledVector(v, dt); b.rotation.x += dt * 6; if (b.position.y < -0.5) b.visible = false; } return ev; } }