/** * 🤖 enemy.robot — patrulla, gira en bordes y paredes; al ver al héroe se para (el visor parpadea en rojo) * y embiste. Si choca contra una pared queda mareado. Congelable y quemable. * Props: { facing?, range? (visión, 7), patrol?: [x0, x1], aggro? (ve en ambos sentidos y más lejos), speed? } */ import * as THREE from 'three'; import { createChibi } from '../../characters/chibi'; import type { Props } from '../../core/types'; import { Enemy, approach, clampPatrol, groundAhead, wallAhead } from './base'; import { capsuleGeo, meshOf, rigBody, sharedMat, sphereGeo } from './shared'; export const ROBOT = { hp: 3, hw: 0.44, hh: 0.5, size: 1.05, walk: 2.1, accel: 18, range: 7, aggroRange: 14, sightY: 1.6, alert: 0.6, charge: 9.5, chargeAccel: 30, chargeMax: 1.5, recover: 0.8, cooldown: 1.1, wallDaze: 1.4, color: 0xf4f1ff, }; export class Robot extends Enemy { private cd = 0; private brows: THREE.Object3D[] = []; private bulb: THREE.Mesh | null = null; private stepT = 0; private lastEm = 0; constructor(props: Props) { super(props); this.hw = ROBOT.hw; this.hh = ROBOT.hh; this.maxHp = ROBOT.hp; this.baseColor = ROBOT.color; this.shadowRadius = 0.5; this.state = 'patrol'; } protected build(): void { const rig = createChibi({ color: ROBOT.color, size: ROBOT.size, shape: 'robot', expression: 'angry', handColor: 0xd9dcef, footColor: 0x3b3f5c, accessory: 'antenna', }); this.rig = rig; const body = rigBody(rig); // cejas-LED rojas en V sobre el visor: le dan mala uva const browMat = sharedMat('robot.brow', { color: 0xff3a3a, emissive: 0xff2020, emissiveIntensity: 1.6 }); for (const side of [-1, 1]) { const b = meshOf(capsuleGeo(), browMat, false); b.scale.set(0.035, 0.13, 0.035); b.rotation.z = Math.PI / 2 - side * 0.5; b.position.set(side * 0.12, 0.215, 0.47); body.add(b); this.brows.push(b); } // perno lateral + placa del pecho const plate = meshOf(sphereGeo(), sharedMat('robot.plate', { color: 0x2fa8f5, emissive: 0x2fa8f5, emissiveIntensity: 0.5, gloss: 0.9 })); plate.scale.set(0.07, 0.07, 0.03); plate.position.set(0, -0.26, 0.45); body.add(plate); this.bulb = plate; } protected restoreExpression(): void { this.rig?.setExpression('angry'); } protected think(dt: number): void { const b = this.body; const P = this.props; this.cd = Math.max(0, this.cd - dt); const range = P.range ?? (P.aggro ? ROBOT.aggroRange : ROBOT.range); const walk = P.speed ?? ROBOT.walk; switch (this.state) { case 'patrol': { const dir = this.facing; if (b.onGround && (wallAhead(this, dir) || !groundAhead(this, dir) || clampPatrol(this, P.patrol, dir))) { this.facing = -dir as 1 | -1; b.vx = 0; } b.vx = approach(b.vx, this.facing * walk, ROBOT.accel * dt); const d = this.heroDelta(); if (d && this.cd <= 0 && Math.abs(d.dx) < range && Math.abs(d.dy) < ROBOT.sightY) { const side = d.dx > 0 ? 1 : -1; if (side === this.facing || P.aggro) { this.facing = side as 1 | -1; this.setState('alert'); b.vx = 0; this.rig?.squash(0.25); this.rig?.setExpression('evil'); this.game.audio.sfx('laser', { volume: 0.35, pitch: 1.6, x: this.x }); this.game.vfx.burst('emoji', this.x, this.y + this.hh + 0.5, { emoji: '❗', count: 1 }); } } break; } case 'alert': { b.vx = approach(b.vx, 0, 40 * dt); // tiembla y se agacha: anticipación if (this.stateT >= ROBOT.alert) { this.setState('charge'); this.rig?.setExpression('angry'); this.rig?.squash(-0.25); this.game.audio.sfx('dash', { x: this.x, pitch: 0.8 }); this.game.vfx.burst('dust', this.x - this.facing * 0.4, this.y - this.hh, { count: 5, dirX: -this.facing }); } break; } case 'charge': { const dir = this.facing; b.vx = approach(b.vx, dir * ROBOT.charge, ROBOT.chargeAccel * dt); this.stepT -= dt; if (this.stepT <= 0) { this.stepT = 0.06; this.game.vfx.trail('dust', this.x - dir * 0.4, this.y - this.hh + 0.1, { dirX: -dir, scale: 0.7 }); } if (b.onGround && wallAhead(this, dir)) { // ¡pum! contra la pared b.vx = -dir * 4; b.vy = 5; this.game.gfx.cam.shake(0.3, 0.2); this.game.audio.sfx('hit', { x: this.x, pitch: 0.7 }); this.game.vfx.burst('star', this.x + dir * 0.5, this.y + 0.3, { count: 5 }); this.facing = -dir as 1 | -1; this.setState('patrol'); this.cd = ROBOT.cooldown; this.daze(ROBOT.wallDaze); } else if ((b.onGround && !groundAhead(this, dir, 0.4)) || clampPatrol(this, P.patrol, dir) || this.stateT > ROBOT.chargeMax) { this.setState('recover'); } break; } case 'recover': { b.vx = approach(b.vx, 0, 26 * dt); if (b.onGround && !groundAhead(this, this.facing, 0.05) && Math.abs(b.vx) > 0.1) b.vx = 0; if (this.stateT > ROBOT.recover) { this.setState('patrol'); this.cd = ROBOT.cooldown; } break; } default: this.setState('patrol'); } } protected animate(dt: number): void { super.animate(dt); const alert = this.state === 'alert' && this.active; const on = alert ? Math.sin(this.t * 38) > 0 : true; const charging = this.state === 'charge' && this.active; for (const b of this.brows) b.visible = on && !this.frozen; const em = alert && on ? 1 : 0; if (em !== this.lastEm) { this.lastEm = em; this.rig?.setEmissive(em ? 0xff2020 : 0x000000, em ? 0.45 : 0); } if (this.bulb) this.bulb.scale.setScalar(charging ? 0.09 : 0.07); // inclinación al embestir this.visual.rotation.z = charging ? -this.facing * 0.18 : alert ? this.facing * 0.08 * Math.sin(this.t * 50) : 0; this.visual.position.x = alert ? 0.03 * Math.sin(this.t * 70) : 0; } }