// The Royal Fluff Catcher: a chibi kid in a fluffy cat-ear hoodie, plus movement. import * as THREE from 'three'; import { Body } from '../physics/body'; import type { Bouncer } from '../physics/body'; import type { VoxelWorld } from '../world/voxels'; import { furCone, furMaterial, furSphere, softMaterial } from '../render/fur'; import { angleLerp, clamp, damp, lerp } from '../core/util'; import type { InputState } from '../input/input'; export class Player { root = new THREE.Group(); body: Body; yaw = 0; private pivot = new THREE.Group(); private head = new THREE.Group(); private legs: THREE.Mesh[] = []; private arms: THREE.Group[] = []; private eyes: THREE.Mesh[] = []; private shadow: THREE.Mesh; crownSlot = new THREE.Group(); hatSlot = new THREE.Group(); hood: THREE.ShaderMaterial; drift = { value: new THREE.Vector3() }; private phase = 0; private t = 0; private coyote = 0; private buffer = 0; private squash = 0; floating = false; pounceT = 0; pounceDir = new THREE.Vector3(); hugT = 0; happyT = 0; speedMul = 1; justLanded = false; justJumped = false; private wasGrounded = true; stepDist = 0; constructor(world: VoxelWorld, hoodColor = '#c9a8ff') { this.body = new Body(world); this.body.radius = 0.34; this.body.height = 1.5; this.root.add(this.pivot); const hood = furMaterial({ top: hoodColor, bottom: '#fff2fb', axis: new THREE.Vector3(0, -1, 0), split: 0.6, length: 0.16, density: 26, drift: this.drift }); this.hood = hood; const sph = furSphere(); const torso = new THREE.Mesh(sph, hood); torso.scale.set(0.36, 0.38, 0.32); torso.position.y = 0.6; this.pivot.add(torso); const tummy = new THREE.Mesh(new THREE.SphereGeometry(0.2, 14, 10), softMaterial('#fff2fb')); tummy.scale.set(1, 1.1, 0.5); tummy.position.set(0, 0.55, 0.26); this.pivot.add(tummy); this.head.position.y = 1.18; this.pivot.add(this.head); const face = new THREE.Mesh(new THREE.SphereGeometry(0.34, 24, 18), softMaterial('#ffe2cf', 0.1)); face.scale.set(1, 0.95, 0.95); this.head.add(face); const hoodBack = new THREE.Mesh(sph, hood); hoodBack.scale.set(0.42, 0.42, 0.4); hoodBack.position.set(0, 0.04, -0.07); this.head.add(hoodBack); // trim the hood so the face shows: a cream fluffy rim const rim = new THREE.Mesh(new THREE.TorusGeometry(0.3, 0.07, 8, 24), furMaterial({ top: '#fff7fd', bottom: '#fff7fd', length: 0.5, density: 40, drift: this.drift })); rim.position.z = 0.2; this.head.add(rim); for (const sx of [-1, 1]) { const ear = new THREE.Mesh(furCone(), hood); ear.scale.set(0.13, 0.15, 0.08); ear.position.set(sx * 0.26, 0.42, -0.04); ear.rotation.z = -sx * 0.35; this.head.add(ear); const eye = new THREE.Mesh(new THREE.SphereGeometry(0.055, 12, 10), softMaterial('#3a2340', 1.2)); eye.scale.set(0.85, 1.15, 0.5); eye.position.set(sx * 0.11, 0.02, 0.31); this.head.add(eye); this.eyes.push(eye); const hl = new THREE.Mesh(new THREE.SphereGeometry(0.018, 6, 4), softMaterial('#ffffff', 0, 2)); hl.position.set(sx * 0.11 + 0.015, 0.05, 0.345); this.head.add(hl); const blush = new THREE.Mesh(new THREE.SphereGeometry(0.05, 10, 6), softMaterial('#ff9ec0', 0, 0.2)); blush.scale.set(1, 0.5, 0.3); blush.position.set(sx * 0.19, -0.07, 0.28); this.head.add(blush); const leg = new THREE.Mesh(new THREE.CapsuleGeometry(0.1, 0.18, 4, 10), softMaterial('#7b5cc4')); leg.position.set(sx * 0.14, 0.17, 0); this.pivot.add(leg); this.legs.push(leg); const arm = new THREE.Group(); arm.position.set(sx * 0.33, 0.78, 0); this.pivot.add(arm); const a = new THREE.Mesh(sph, hood); a.scale.set(0.1, 0.2, 0.1); a.position.y = -0.17; arm.add(a); const paw = new THREE.Mesh(new THREE.SphereGeometry(0.09, 10, 8), softMaterial('#fff2fb')); paw.position.y = -0.36; arm.add(paw); arm.rotation.z = sx * 0.4; this.arms.push(arm); } const smile = new THREE.Mesh(new THREE.TorusGeometry(0.06, 0.014, 6, 12, Math.PI), softMaterial('#9a3d5c')); smile.rotation.z = Math.PI; smile.position.set(0, -0.08, 0.32); this.head.add(smile); // fluffy tail on the hoodie const tail = new THREE.Mesh(sph, hood); tail.scale.set(0.13, 0.13, 0.13); tail.position.set(0, 0.42, -0.34); this.pivot.add(tail); this.crownSlot.position.y = 0.42; this.head.add(this.crownSlot); this.hatSlot.position.y = 0.36; this.head.add(this.hatSlot); this.shadow = new THREE.Mesh(new THREE.CircleGeometry(0.45, 20), new THREE.MeshBasicMaterial({ color: '#7a4a6a', transparent: true, opacity: 0.25, depthWrite: false })); this.shadow.rotation.x = -Math.PI / 2; } /** Change the hoodie colours (wardrobe). */ setHood(top: string, bottom = '#fff2fb') { (this.hood.uniforms.uTop.value as THREE.Color).set(top); (this.hood.uniforms.uBottom.value as THREE.Color).set(bottom); } setHat(hat: THREE.Object3D | null) { this.hatSlot.clear(); if (hat) this.hatSlot.add(hat); } /** camYaw = camera yaw; moves relative to camera. Returns horizontal speed. */ update(dt: number, inp: InputState, camYaw: number, bouncers: Bouncer[], allowJump: boolean) { this.t += dt; const b = this.body; this.justLanded = false; this.justJumped = false; const fwdX = -Math.sin(camYaw), fwdZ = -Math.cos(camYaw); const rgtX = Math.cos(camYaw), rgtZ = -Math.sin(camYaw); let wx = fwdX * inp.moveY + rgtX * inp.moveX, wz = fwdZ * inp.moveY + rgtZ * inp.moveX; const mag = Math.min(1, Math.hypot(wx, wz)); const maxSp = 7.2 * this.speedMul; let tx = wx * maxSp, tz = wz * maxSp; if (this.pounceT > 0) { this.pounceT -= dt; tx = this.pounceDir.x * 9; tz = this.pounceDir.z * 9; } const accel = b.grounded ? 14 : 6; b.vel.x = lerp(b.vel.x, tx, damp(accel, dt)); b.vel.z = lerp(b.vel.z, tz, damp(accel, dt)); if (this.hugT > 0) { this.hugT -= dt; b.vel.x *= 0.8; b.vel.z *= 0.8; } if (mag > 0.1 && this.hugT <= 0) this.yaw = angleLerp(this.yaw, Math.atan2(wx, wz), damp(12, dt)); else if (this.pounceT > 0) this.yaw = angleLerp(this.yaw, Math.atan2(this.pounceDir.x, this.pounceDir.z), damp(16, dt)); // jump with coyote time + buffering; hold = fluffy float this.coyote = b.grounded ? 0.13 : this.coyote - dt; this.buffer = inp.actionPressed && allowJump ? 0.15 : this.buffer - dt; if (this.buffer > 0 && this.coyote > 0) { b.vel.y = 10.5; this.coyote = 0; this.buffer = 0; b.grounded = false; this.justJumped = true; } this.floating = !b.grounded && inp.action && b.vel.y < 0; let grav = 27; if (this.floating) { grav = 7; if (b.vel.y < -2.2) b.vel.y = lerp(b.vel.y, -2.2, damp(10, dt)); } else if (b.vel.y > 0 && !inp.action) grav = 42; // short hop if released early b.step(dt, grav, bouncers); if (b.grounded && !this.wasGrounded) { this.justLanded = true; this.squash = 1; } this.wasGrounded = b.grounded; // animate const sp = Math.hypot(b.vel.x, b.vel.z); this.stepDist += sp * dt; this.phase += dt * (3 + sp * 2.1); const run = clamp(sp / 6, 0, 1) * (b.grounded ? 1 : 0.2); this.squash = Math.max(0, this.squash - dt * 4); const sq = Math.sin(this.squash * Math.PI) * 0.22; this.root.position.copy(b.pos); this.root.rotation.y = this.yaw; this.pivot.position.y = Math.abs(Math.sin(this.phase)) * 0.12 * run; const air = b.grounded ? 0 : clamp(b.vel.y * 0.03, -0.15, 0.18); this.pivot.scale.set(1 + sq - air * 0.5, 1 - sq + air, 1 + sq); this.pivot.rotation.x = run * 0.15 + (this.pounceT > 0 ? 0.5 : 0); this.legs.forEach((l, i) => { l.position.z = Math.sin(this.phase + i * Math.PI) * 0.17 * run; l.position.y = 0.17 + Math.max(0, Math.sin(this.phase + i * Math.PI)) * 0.08 * run + (b.grounded ? 0 : 0.06); }); this.arms.forEach((a, i) => { const sx = i ? 1 : -1; a.rotation.x = -Math.sin(this.phase + i * Math.PI) * 0.8 * run + (this.floating ? 0 : 0) + (this.hugT > 0 ? -1.3 : 0) + (this.pounceT > 0 ? -1.6 : 0); a.rotation.z = sx * (0.4 + (this.floating ? 1.9 + Math.sin(this.t * 18) * 0.25 : 0) + (!b.grounded && !this.floating ? 0.8 : 0) + (this.happyT > 0 ? 2.2 + Math.sin(this.t * 14) * 0.3 : 0)) - (this.hugT > 0 ? sx * 0.6 : 0); }); this.happyT = Math.max(0, this.happyT - dt); this.head.rotation.z = Math.sin(this.t * 1.7) * 0.05 + (this.happyT > 0 ? Math.sin(this.t * 10) * 0.15 : 0); const blink = (this.t % 3.7) < 0.12 ? 0.15 : 1; this.eyes.forEach(e => e.scale.y = (this.happyT > 0 ? 0.35 : 1.15) * blink); this.drift.value.set(-b.vel.x * 0.012, -0.02 - (b.grounded ? 0 : b.vel.y * 0.004), -b.vel.z * 0.012); const g = b.world.groundBelow(b.pos.x, b.pos.z, b.pos.y + 0.1); this.shadow.position.set(b.pos.x, (isFinite(g) ? g : b.pos.y) + 0.03, b.pos.z); this.shadow.scale.setScalar(clamp(1 - (b.pos.y - g) * 0.12, 0.3, 1)); return sp; } get shadowMesh() { return this.shadow; } }