/** * 🦀 enemy.crab — cangrejo de la orilla (Laguna). Camina de lado y siempre encara al héroe con sus pinzas, * que BLOQUEAN los golpes de frente (¡Clanc!). Se daña: * - por la ESPALDA (salta por encima y golpéale antes de que se gire: tarda en darse la vuelta), * - con AGUA (la pistola de agua lo voltea y le hace daño), * - cayéndole encima (pisotón o picado): queda panza arriba unos segundos y entonces cualquier golpe vale. * Si estás delante y cerca, lanza un pinzazo. Congelable; con COOL queda mareado y sin guardia. * Props: { facing?, range? (6), patrol?: [x0, x1], aggro?, speed? } */ import * as THREE from 'three'; import { createChibi } from '../../characters/chibi'; import type { Hit, Props } from '../../core/types'; import { Enemy, approach, clampPatrol, groundAhead, wallAhead } from './base'; import { meshOf, rigBody, sharedMat, sphereGeo, capsuleGeo } from './shared'; export const CRAB = { hp: 3, hw: 0.55, hh: 0.36, size: 1.0, walk: 1.7, accel: 12, range: 6, turnDelay: 0.75, snap: { reach: 1.7, wind: 0.32, active: 0.16, recover: 0.5, cd: 1.1, damage: 1, knock: 9 }, flipTime: 2.6, color: 0xf2452e, }; export class Crab extends Enemy { private walkDir: 1 | -1 = 1; private turnT = 0; private snapCd = 0; private claws: THREE.Group[] = []; private legs: THREE.Object3D[] = []; private flipT = 0; private flipVis = 0; constructor(props: Props) { super(props); this.hw = CRAB.hw; this.hh = CRAB.hh; this.maxHp = CRAB.hp; this.waterWeak = true; this.baseColor = CRAB.color; this.shadowRadius = 0.6; this.stompable = true; this.walkDir = this.facing; this.state = 'walk'; } get flipped(): boolean { return this.flipT > 0; } protected build(): void { const rig = createChibi({ color: CRAB.color, size: CRAB.size, expression: 'angry', hands: false, feet: false }); this.rig = rig; const body = rigBody(rig); const shell = sharedMat('crab.shell', { color: CRAB.color, gloss: 0.8 }); const tip = sharedMat('crab.tip', { color: 0xffb08a, gloss: 0.6 }); // pinzas: brazo + pinza con dos mandíbulas que se abren for (const side of [-1, 1]) { const arm = new THREE.Group(); arm.position.set(side * 0.5, -0.12, 0.2); const a = meshOf(capsuleGeo(), shell); a.scale.set(0.09, 0.22, 0.09); a.rotation.z = side * 1.1; a.position.set(side * 0.08, 0.02, 0); const claw = new THREE.Group(); claw.position.set(side * 0.26, 0.14, 0.05); const top = meshOf(sphereGeo(), shell); top.scale.set(0.2, 0.11, 0.13); top.position.y = 0.07; const bot = meshOf(sphereGeo(), tip); bot.scale.set(0.17, 0.08, 0.11); bot.position.y = -0.06; claw.add(top, bot); arm.add(a, claw); body.add(arm); this.claws.push(claw); } // patitas for (const side of [-1, 1]) { for (let i = 0; i < 3; i++) { const l = meshOf(capsuleGeo(), shell); l.scale.set(0.05, 0.14, 0.05); l.position.set(side * (0.32 + i * 0.06), -0.42, 0.12 - i * 0.14); l.rotation.z = side * 0.6; rig.root.add(l); this.legs.push(l); } } // ojos en antenitas (además de la cara) const stalk = sharedMat('crab.stalk', { color: 0xd8382a }); for (const side of [-1, 1]) { const s = meshOf(capsuleGeo(), stalk); s.scale.set(0.035, 0.12, 0.035); s.position.set(side * 0.16, 0.52, 0.05); const ball = meshOf(sphereGeo(), sharedMat('crab.eyeball', { color: 0xffffff, gloss: 0.9 })); ball.scale.setScalar(0.07); ball.position.set(side * 0.16, 0.64, 0.07); const pupil = meshOf(sphereGeo(), sharedMat('crab.pupil', { color: 0x1a1030 }), false); pupil.scale.setScalar(0.035); pupil.position.set(side * 0.16, 0.64, 0.13); body.add(s, ball, pupil); } this.visual.scale.set(1, 0.82, 1); } protected restoreExpression(): void { this.rig?.setExpression(this.flipped ? 'hurt' : 'angry'); } protected harmful(): boolean { return this.active && !this.flipped; } private flipOver(): void { const g = this.game; this.flipT = CRAB.flipTime; this.body.vy = 5; this.setState('walk'); this.rig?.setExpression('hurt'); g.audio.sfx('bounce', { pitch: 0.8, x: this.x }); g.vfx.burst('star', this.x, this.y + 0.4, { count: 4 }); g.vfx.floatText('¡Panza arriba!', this.x, this.y + 0.9, '#ffd36a'); } protected onStomp(): void { const h = this.game.hero; if (!h) return; h.body.vy = 12; if (this.flipped) this.damage(1, h.facing); else this.flipOver(); } protected onWater(_dir: number): void { if (!this.flipped && !this.defeated) this.flipOver(); } takeHit(hit: Hit): boolean { if (this.defeated) return false; const src = hit.source; const h = this.game.hero; // golpe desde arriba (picado del héroe): voltea if (src && h && src === h && h.body.vy < 0 && h.body.y - h.body.hh > this.body.y + this.hh * 0.2 && !this.flipped && !this.frozen) { this.flipOver(); return true; } const guarded = !this.flipped && !this.frozen && this.stunT <= 0 && hit.element !== 'water' && hit.element !== 'cool' && hit.element !== 'ice'; // de frente = el golpe viaja contra la cara del cangrejo const fromFront = (hit.dirX || 0) === -this.facing; if (guarded && fromFront && hit.damage > 0) { const g = this.game; g.audio.sfx('hit', { pitch: 1.9, volume: 0.6, x: this.x }); g.vfx.burst('spark', this.x + this.facing * this.hw, this.y + 0.1, { count: 7, dirX: this.facing, color: 0xffe08a }); g.vfx.floatText('¡Clanc!', this.x, this.y + 0.8, '#ffe08a'); for (const c of this.claws) c.scale.setScalar(1.25); this.body.vx = this.facing * -1.5; return true; } return super.takeHit(hit); } protected think(dt: number): void { const b = this.body; const P = this.props; const g = this.game; this.snapCd = Math.max(0, this.snapCd - dt); if (this.flipT > 0) { this.flipT -= dt; b.vx = approach(b.vx, 0, 20 * dt); if (this.flipT <= 0) { b.vy = 6; this.rig?.setExpression('angry'); this.rig?.squash(0.3); g.audio.sfx('jump', { pitch: 1.4, volume: 0.4, x: this.x }); } return; } const d = this.heroDelta(); const range = P.range ?? CRAB.range; const sees = !!d && (P.aggro || Math.abs(d.dx) < range) && Math.abs(d.dy) < 3; // girarse hacia el héroe tarda: ventana para pegarle por la espalda if (sees && d) { const want = d.dx > 0 ? 1 : -1; if (want !== this.facing) { this.turnT += dt; if (this.turnT >= CRAB.turnDelay) { this.facing = want as 1 | -1; this.turnT = 0; this.rig?.squash(0.2); } } else this.turnT = 0; } else this.turnT = 0; switch (this.state) { case 'walk': { const dir = this.walkDir; if (b.onGround && (wallAhead(this, dir) || !groundAhead(this, dir) || clampPatrol(this, P.patrol, dir))) this.walkDir = -dir as 1 | -1; const speed = (P.speed ?? CRAB.walk) * (sees ? 1.2 : 1); b.vx = approach(b.vx, this.walkDir * speed, CRAB.accel * dt); if (sees && d && this.snapCd <= 0 && Math.sign(d.dx) === this.facing && Math.abs(d.dx) < CRAB.snap.reach && Math.abs(d.dy) < 1) { this.setState('wind'); this.rig?.setExpression('evil'); } break; } case 'wind': { b.vx = approach(b.vx, 0, 30 * dt); if (this.stateT >= CRAB.snap.wind) { this.setState('snap'); b.vx = this.facing * 4; g.audio.sfx('punch', { pitch: 1.6, x: this.x }); } break; } case 'snap': { b.vx = approach(b.vx, 0, 25 * dt); const h = this.hero; if (h) { const bx = b.x + this.facing * (this.hw + 0.4); const hb = h.body; if (Math.abs(hb.x - bx) < hb.hw + 0.45 && Math.abs(hb.y - b.y) < hb.hh + 0.35) { h.takeHit({ damage: CRAB.snap.damage, element: 'none', dirX: this.facing, knock: CRAB.snap.knock, source: this }); } } if (this.stateT >= CRAB.snap.active) this.setState('recover'); break; } case 'recover': { b.vx = approach(b.vx, 0, 25 * dt); if (this.stateT >= CRAB.snap.recover) { this.snapCd = CRAB.snap.cd; this.setState('walk'); this.rig?.setExpression('angry'); } break; } default: this.setState('walk'); } } protected animate(dt: number): void { // camina de lado: cara siempre al héroe (facing), sin auto-correr const b = this.body; this.rig?.update(dt, { vx: 0, vy: b.vy, onGround: true, facing: this.facing }); const target = this.flipped ? 1 : 0; this.flipVis += (target - this.flipVis) * Math.min(1, dt * 10); this.visual.rotation.z = this.flipVis * Math.PI; this.visual.position.y = this.flipVis * 0.15; const moving = Math.abs(b.vx) > 0.3 || this.flipped; for (let i = 0; i < this.legs.length; i++) { const side = i < 3 ? -1 : 1; const ph = this.t * (this.flipped ? 22 : 16) + i * 1.3; this.legs[i].rotation.z = side * (0.6 + (moving ? Math.sin(ph) * 0.35 : 0)); } // pinzas: abren y cierran; muy abiertas al preparar el pinzazo const open = this.state === 'wind' ? 0.6 : this.state === 'snap' ? 0 : 0.15 + 0.12 * Math.sin(this.t * 5); for (let i = 0; i < this.claws.length; i++) { const c = this.claws[i]; const s = c.scale.x + (1 - c.scale.x) * Math.min(1, dt * 10); c.scale.setScalar(s); c.children[0].rotation.z = (i === 0 ? -1 : 1) * open * 0.5; c.children[1].rotation.z = -(i === 0 ? -1 : 1) * open * 0.5; c.position.x = (i === 0 ? -1 : 1) * 0.26 + (this.state === 'snap' ? this.facing * 0.15 : 0); } } }