/** * πŸ•·οΈ enemy.spider y boss.spider β€” araΓ±as de la Cueva Cristal. * * `enemy.spider` es una araΓ±ita colgante: baja de un hilo, persigue al hΓ©roe y vuelve a subir. * `boss.spider` es la AraΓ±a Gigante que duerme en el techo. Solo despierta si el hΓ©roe lleva la * LINTERNA. Combate en tres fases (embestida, telaraΓ±as ralentizadoras y techo con estalactitas), * con telegraphs de anillo en el suelo, sin flashes de pantalla ni parpadeos. * * Flags: `spider.awakened`, `spider.defeated`. * Eventos nuevos: `spider:awakened`, `spider:web`. */ import * as THREE from 'three'; import { Entity } from '../../core/entity'; import type { Body, Hit, Props } from '../../core/types'; import { createChibi } from '../../characters/chibi'; import type { ChibiRig } from '../../characters/types'; import { blobShadow, emojiMesh, emojiSprite } from '../../render'; import { Enemy, ENEMY } from './base'; import { haloTexture, meshOf, onSurface, rigBody, sharedGeo, sharedMat, sphereGeo } from './shared'; declare module '../../core/types' { interface GameEvents { 'spider:awakened': { id: 'spider' }; 'spider:web': { x: number; y: number }; } } // ───────────────────────────── Recursos compartidos ───────────────────────────── const legGeo = sharedGeo('spider.leg', () => new THREE.CylinderGeometry(1, 0.82, 1, 8)); const fangGeo = sharedGeo('spider.fang', () => new THREE.ConeGeometry(1, 1, 8)); const rockGeo = sharedGeo('spider.rock', () => new THREE.ConeGeometry(1, 1, 5)); const threadGeo = sharedGeo('spider.threadGeo', () => new THREE.CylinderGeometry(0.028, 0.028, 1, 6)); const eyeGeo = sharedGeo('spider.eye', () => new THREE.SphereGeometry(1, 16, 12)); const webPlane = sharedGeo('spider.webPlane', () => new THREE.PlaneGeometry(2.2, 2.2).rotateX(-Math.PI / 2)); const threadMat = sharedMat('spider.thread', { color: 0xc8c2e8, gloss: 0.1, rim: 0.1, transparent: true, opacity: 0.62 }); const eyeMat = sharedMat('spider.eyeMat', { color: 0x8b5638, emissive: 0x4a1a10, emissiveIntensity: 0.42, gloss: 0.95, rim: 0.2 }); const fangMat = sharedMat('spider.fangMat', { color: 0xf2eaff, gloss: 0.65, rim: 0.2 }); const warnMat = sharedMat('spider.warnMat', { color: 0x35172d, gloss: 0.1, rim: 0 }); interface Leg { pivot: THREE.Group; lower: THREE.Group; side: -1 | 1; row: number; phase: number; } function addLegs(rig: ChibiRig, color: number): Leg[] { const body = rigBody(rig); const mat = sharedMat(`spider.legMat:${color}`, { color, gloss: 0.72, rim: 0.65 }); const joints = sharedMat(`spider.jointMat:${color}`, { color: new THREE.Color(color).multiplyScalar(0.7).getHex(), gloss: 0.8, rim: 0.4 }); const legs: Leg[] = []; const upperLen = 0.52; const lowerLen = 0.58; for (const side of [-1, 1] as const) { for (let row = 0; row < 4; row++) { const pivot = new THREE.Group(); pivot.position.set(side * 0.33, 0.12 - row * 0.105, (row - 1.5) * 0.1); pivot.rotation.z = -side * (0.28 + row * 0.05); const upper = meshOf(legGeo, mat); upper.scale.set(0.045, upperLen, 0.045); upper.rotation.z = -side * Math.PI / 2; upper.position.x = side * upperLen * 0.5; upper.castShadow = true; pivot.add(upper); const joint = meshOf(sphereGeo(), joints); joint.scale.setScalar(0.065); joint.position.x = side * upperLen; pivot.add(joint); const lower = new THREE.Group(); lower.position.x = side * upperLen; lower.rotation.z = side * (0.55 + row * 0.04); const lowerMesh = meshOf(legGeo, mat); lowerMesh.scale.set(0.038, lowerLen, 0.038); lowerMesh.position.y = -lowerLen * 0.5; lowerMesh.castShadow = true; lower.add(lowerMesh); const tip = meshOf(sphereGeo(), joints); tip.scale.setScalar(0.045); tip.position.y = -lowerLen; lower.add(tip); pivot.add(lower); body.add(pivot); legs.push({ pivot, lower, side, row, phase: row * 0.8 + (side > 0 ? Math.PI : 0) }); } } return legs; } function addEyesAndFangs(rig: ChibiRig, boss: boolean): void { const body = rigBody(rig); const s = boss ? 1 : 0.82; const eyes: Array<[number, number, number, number]> = [ [-0.17, 0.12, 0.43, 1.05], [0.17, 0.12, 0.43, 1.05], [-0.29, 0.02, 0.405, 0.65], [0.29, 0.02, 0.405, 0.65], [-0.235, 0.235, 0.39, 0.55], [0.235, 0.235, 0.39, 0.55], ]; for (const [x, y, z, k] of eyes) { const e = meshOf(eyeGeo, eyeMat, false); e.position.set(x * s, y * s, z * s); e.scale.set(0.09 * s * k, 0.125 * s * k, 0.055 * s * k); body.add(e); } for (const side of [-1, 1] as const) { const fang = meshOf(fangGeo, fangMat, false); fang.position.set(side * 0.115 * s, -0.255 * s, 0.455 * s); fang.scale.set(0.032 * s, 0.12 * s, 0.032 * s); fang.rotation.z = Math.PI; body.add(fang); } } function animateLegs(legs: Leg[], t: number, speed: number, active: boolean): void { for (const l of legs) { const swing = active ? Math.sin(t * speed + l.phase) * 0.11 : Math.sin(t * 1.5 + l.phase) * 0.025; l.pivot.rotation.z = -l.side * (0.28 + l.row * 0.05) + swing; l.pivot.rotation.y = Math.sin(t * speed * 0.7 + l.phase) * 0.08; l.lower.rotation.z = l.side * (0.55 + l.row * 0.04) + (active ? Math.cos(t * speed + l.phase) * 0.16 : 0); } } let webTex: THREE.Texture | null = null; function webTexture(): THREE.Texture { if (webTex) return webTex; const n = 256; const c = document.createElement('canvas'); c.width = c.height = n; const g = c.getContext('2d')!; g.clearRect(0, 0, n, n); g.strokeStyle = 'rgba(225,218,255,0.9)'; g.lineWidth = 2.2; const cx = n / 2, cy = n / 2; for (let i = 0; i < 12; i++) { const a = (i / 12) * Math.PI * 2; g.beginPath(); g.moveTo(cx, cy); g.lineTo(cx + Math.cos(a) * n * 0.47, cy + Math.sin(a) * n * 0.47); g.stroke(); } for (let ring = 1; ring <= 5; ring++) { const r = (ring / 5) * n * 0.43; g.beginPath(); for (let i = 0; i <= 12; i++) { const a = (i / 12) * Math.PI * 2; const rr = r * (0.9 + ((i * 7 + ring * 3) % 5) * 0.025); const px = cx + Math.cos(a) * rr; const py = cy + Math.sin(a) * rr; if (i === 0) g.moveTo(px, py); else g.lineTo(px, py); } g.stroke(); } const tex = new THREE.CanvasTexture(c); tex.colorSpace = THREE.SRGBColorSpace; tex.userData.shared = true; webTex = tex; return tex; } // ───────────────────────────── AraΓ±ita comΓΊn ───────────────────────────── const CAVE_SPIDER = { hp: 2, hw: 0.38, hh: 0.34, size: 1.15, hangDrop: 5.4, chaseTime: 5.2, speed: 2.5, range: 6.2, color: 0x5b2c73, }; export class CaveSpider extends Enemy { private home = { x: 0, y: 0, anchorY: 0 }; private floorY = 0; private legs: Leg[] = []; private thread!: THREE.Mesh; private hangT = 0; constructor(props: Props) { super(props); this.hw = CAVE_SPIDER.hw; this.hh = CAVE_SPIDER.hh; this.maxHp = CAVE_SPIDER.hp; this.flying = true; this.ghostly = true; this.freezable = true; this.burnable = true; this.stompable = true; this.baseColor = CAVE_SPIDER.color; this.shadowRadius = 0.32; this.dizzyHeight = 0.16; this.state = 'hang'; } protected spawnCenterY(y: number): number { this.home.anchorY = y; return y - 0.68; } protected afterAdded(): void { this.home.x = this.body.x; this.home.y = this.body.y; const gy = this.game.physics.groundBelow(this.x, this.y + 1, 40); this.floorY = (gy ?? this.y - 4) + this.hh; } protected build(): void { const rig = createChibi({ color: CAVE_SPIDER.color, size: CAVE_SPIDER.size, expression: 'evil', hands: false, feet: false, emissive: 0x2a0d3a, emissiveIntensity: 0.08, }); this.rig = rig; rig.play('sleep', { loop: true }); this.legs = addLegs(rig, CAVE_SPIDER.color); addEyesAndFangs(rig, false); this.thread = meshOf(threadGeo, threadMat, false); this.thread.visible = true; this.view.add(this.thread); } protected restoreExpression(): void { this.rig?.setExpression(this.state === 'hang' ? 'sleepy' : 'evil'); } protected harmful(): boolean { return this.active && this.state === 'chase'; } private climb(): void { if (this.state !== 'climb') { this.setState('climb'); this.rig?.play('float', { loop: true }); this.rig?.setExpression('surprised'); } } protected onHurt(_dir: number): void { super.onHurt(_dir); this.climb(); } protected onDaze(): void { this.climb(); } protected onThaw(): void { this.climb(); } protected think(dt: number): void { const b = this.body; const d = this.heroDelta(); b.noCollide = true; b.vx = 0; b.vy = 0; switch (this.state) { case 'hang': { b.x += (this.home.x - b.x) * Math.min(1, dt * 3); b.y += (this.home.y - b.y) * Math.min(1, dt * 3) + Math.sin(this.t * 1.7) * 0.01; this.hangT += dt; if (d && d.dist < CAVE_SPIDER.range && this.hangT > 0.5) { this.setState('drop'); this.rig?.setExpression('evil'); this.rig?.play('float', { loop: true }); this.game.audio.sfx('whoosh', { pitch: 1.7, volume: 0.45, x: b.x }); this.game.vfx.burst('dust', b.x, b.y + 0.4, { count: 4, scale: 0.6 }); } break; } case 'drop': { b.x += (this.home.x - b.x) * Math.min(1, dt * 2); b.y += (this.floorY - b.y) * Math.min(1, dt * CAVE_SPIDER.hangDrop); if (Math.abs(b.y - this.floorY) < 0.08) { b.y = this.floorY; this.setState('chase'); this.rig?.play('run', { loop: true }); } break; } case 'chase': { const dx = d?.dx ?? 0; b.vx = Math.abs(dx) > 0.35 ? Math.sign(dx) * CAVE_SPIDER.speed : 0; b.y += (this.floorY - b.y) * Math.min(1, dt * 8); if (d && dx !== 0) this.facing = dx > 0 ? 1 : -1; if (!d || d.dist > 11 || this.stateT > CAVE_SPIDER.chaseTime) this.climb(); break; } case 'climb': { b.x += (this.home.x - b.x) * Math.min(1, dt * 2.5); b.y += (this.home.y - b.y) * Math.min(1, dt * 4.5); if (Math.abs(b.y - this.home.y) < 0.12 && Math.abs(b.x - this.home.x) < 0.12) { this.setState('hang'); this.hangT = 0; this.rig?.play('sleep', { loop: true }); this.rig?.setExpression('sleepy'); } break; } default: this.setState('climb'); } } protected animate(dt: number): void { const b = this.body; this.rig?.update(dt, { vx: b.vx, vy: b.vy, onGround: this.state === 'chase', facing: this.facing }); this.visual.rotation.z = this.state === 'chase' ? Math.sin(this.t * 9) * 0.04 : Math.sin(this.t * 1.4) * 0.02; animateLegs(this.legs, this.t, this.state === 'chase' ? 13 : 3.2, this.state === 'chase' || this.state === 'drop'); const top = this.home.anchorY - b.y; this.thread.visible = top > 0.15 && this.state !== 'chase'; if (this.thread.visible) { this.thread.position.y = top * 0.5; this.thread.scale.y = Math.max(0.01, top); } } } // ───────────────────────────── Proyectiles y peligros del jefe ───────────────────────────── class SpiderWebShot extends Entity { private vx = 0; private vy = 0; private targetY = 0; private age = 0; private readonly icon: THREE.Object3D; constructor(startX: number, startY: number, targetX: number, targetY: number, private floorY: number) { super(); this.team = 'enemy'; this.tags.add('projectile'); const dx = targetX - startX; const dy = targetY - startY; const d = Math.max(0.001, Math.hypot(dx, dy)); this.vx = dx / d * 8.2; this.vy = dy / d * 8.2; this.targetY = targetY; this.icon = emojiMesh('πŸ•ΈοΈ', 0.95, { glow: true, glowIntensity: 0.25, shadow: false }); this.view.add(this.icon); this.setPos(startX, startY); } update(dt: number): void { const g = this.game; this.age += dt; this.x += this.vx * dt; this.y += this.vy * dt; this.vy -= 3.2 * dt; this.icon.rotation.z += dt * 3.2; const h = g.hero; if (h && h.alive && Math.hypot(h.x - this.x, h.y - this.y) < 0.8) { g.add(new SpiderWebPatch(this.x, this.floorY, 1.8)); g.events.emit('spider:web', { x: this.x, y: this.floorY }); g.audio.sfx('splash', { volume: 0.25, pitch: 1.36, x: this.x }); this.kill(); return; } if (this.age > 1.5 || this.y < this.floorY + 0.2 || this.y < this.targetY - 2) { g.add(new SpiderWebPatch(this.x, this.floorY, 1.65)); g.events.emit('spider:web', { x: this.x, y: this.floorY }); this.kill(); } } } class SpiderWebPatch extends Entity { private readonly mesh: THREE.Mesh; private readonly mat: THREE.MeshBasicMaterial; private life = 7.5; private readonly radius: number; private readonly floorY: number; constructor(x: number, floorY: number, radius: number) { super(); this.team = 'neutral'; this.radius = radius; this.floorY = floorY; this.tags.add('hazard'); this.mat = new THREE.MeshBasicMaterial({ map: webTexture(), color: 0xd8ceff, transparent: true, opacity: 0.7, depthWrite: false, polygonOffset: true, polygonOffsetFactor: -2, }); this.mesh = new THREE.Mesh(webPlane, this.mat); this.mesh.scale.setScalar(radius / 1.1); this.mesh.position.y = 0.035; this.view.add(this.mesh); this.setPos(x, floorY); } update(dt: number): void { this.life -= dt; const h = this.game.hero; if (h && h.alive) { const dx = Math.abs(h.x - this.x); const feet = h.y - h.body.hh; if (dx < this.radius && feet < this.floorY + 1.1 && feet > this.floorY - 1.2) { h.body.vx *= 0.34; if (h.body.onGround && h.body.vy > 0) h.body.vy *= 0.55; // Un hilo visible en los pies deja claro por quΓ© cuesta moverse. if (Math.random() < dt * 5) this.game.vfx.trail('sparkle', h.x, feet + 0.1, { color: 0xd8ceff, scale: 0.45 }); } } if (this.life <= 0) this.kill(); } render(alpha: number, dt: number): void { super.render(alpha, dt); this.mat.opacity = Math.min(0.7, Math.max(0, this.life * 0.4)); } } class FallingStalactite extends Entity { private warning = 0.95; private vy = 0; private falling = false; private impacted = false; private readonly rock: THREE.Mesh; private readonly shadow: THREE.Mesh; private readonly shadowMat: THREE.MeshBasicMaterial; constructor(startX: number, private startY: number, private floorY: number) { super(); this.team = 'enemy'; this.tags.add('hazard'); this.mat = sharedMat('spider.stalactite', { color: 0x6b4b91, gloss: 0.4, rim: 0.5 }); this.rock = new THREE.Mesh(rockGeo, this.mat); this.rock.scale.set(0.34, 1.35, 0.34); this.rock.rotation.z = Math.PI; this.rock.castShadow = true; this.view.add(this.rock); this.shadowMat = new THREE.MeshBasicMaterial({ color: 0x120818, transparent: true, opacity: 0, depthWrite: false, polygonOffset: true, polygonOffsetFactor: -3, }); this.shadow = new THREE.Mesh(webPlane, this.shadowMat); this.shadow.scale.setScalar(0.62); this.view.add(this.shadow); this.setPos(startX, startY); } private readonly mat: THREE.Material; update(dt: number): void { const g = this.game; if (!this.falling) { this.warning -= dt; if (this.warning <= 0) { this.falling = true; g.audio.sfx('whoosh', { pitch: 0.5, volume: 0.5, x: this.x }); } return; } if (this.impacted) return; this.vy = Math.max(-24, this.vy - 34 * dt); this.y += this.vy * dt; if (this.y <= this.floorY + 0.65) { this.impacted = true; this.y = this.floorY + 0.65; const h = g.hero; if (h && h.alive && Math.abs(h.x - this.x) < 1.1) { h.takeHit({ damage: 1, element: 'none', dirX: h.x < this.x ? -1 : 1, knock: 8, source: this }); } g.gfx.cam.shake(0.28, 0.22); g.audio.sfx('shatter', { volume: 0.7, x: this.x }); g.audio.sfx('land', { pitch: 0.7, volume: 0.5, x: this.x }); g.vfx.burst('shatter', this.x, this.floorY + 0.4, { count: 14, color: 0x9b7bc8, scale: 1.2 }); g.vfx.burst('dust', this.x, this.floorY + 0.1, { count: 10, scale: 1.2 }); this.kill(); } } render(alpha: number, dt: number): void { super.render(alpha, dt); if (this.falling) { this.shadow.position.y = this.floorY - this.y + 0.04; this.shadowMat.opacity = 0.45; this.shadow.scale.setScalar(1.15); } else { const k = 1 - Math.max(0, this.warning) / 0.95; this.shadow.position.y = this.floorY - this.y + 0.04; this.shadowMat.opacity = 0.18 + k * 0.35; this.shadow.scale.setScalar(0.5 + k * 0.65); } this.rock.rotation.y += dt * 0.42; } } // ───────────────────────────── Jefa AraΓ±a Gigante ───────────────────────────── const GIANT = { lives: 7, size: 3.5, radius: 1.85, hover: 3.5, floorOffset: 1.72, intro: 2.4, chargeSpeed: [10.5, 12.4, 14.0], telegraph: [0.95, 0.82, 0.68], stun: [2.5, 2.25, 1.9], webWind: 0.9, ceiling: 3.3, contact: { damage: 1, knock: 9 }, }; type SState = | 'sleep' | 'intro' | 'idle' | 'telegraph' | 'charge' | 'stunned' | 'webWind' | 'webFire' | 'ceiling' | 'summon' | 'hurt' | 'recover' | 'defeat'; export class GiantSpider extends Entity { declare body: Body; readonly props: Props; private rig!: ChibiRig; private legs: Leg[] = []; private thread!: THREE.Mesh; private eyes = new THREE.Group(); private shadow!: THREE.Mesh; private halo!: THREE.Sprite; private state: SState = 'sleep'; private stateT = 0; private t = 0; private lives = GIANT.lives; private arena = { x0: -15, x1: 15, floor: 0 }; private targetX = 0; private attackIndex = 0; private ringT = 0; private noLightToastT = 0; private facing: 1 | -1 = 1; private started = false; private finished = false; private ringDropped = false; private recoverFromY = 0; private webFired = false; private ceilingSpawned = 0; private summoned = false; private readonly threadBase = new THREE.Vector3(); constructor(props: Props) { super(); this.props = props; this.team = 'enemy'; this.tags.add('boss'); this.tags.add('enemy'); this.view.add(this.eyes); } get phase(): 0 | 1 | 2 { return this.lives <= 2 ? 2 : this.lives <= 4 ? 1 : 0; } get vulnerable(): boolean { return this.state === 'stunned'; } private get heroOk() { const h = this.game.hero; return h && h.alive ? h : null; } onAdded(): void { const g = this.game; const a = this.props.arena; const floor = a?.floor ?? g.physics.groundBelow(this.x, this.y + 1, 20) ?? this.y; this.arena = { x0: a?.x0 ?? this.x - 14, x1: a?.x1 ?? this.x + 14, floor }; const sleepY = floor + 7.6; this.body = g.physics.body(this.x, sleepY, GIANT.radius * 0.86, GIANT.floorOffset); this.body.noCollide = true; this.hp = this.lives; const rig = createChibi({ color: 0x3a1d4a, size: GIANT.size, expression: 'evil', hands: false, feet: false, emissive: 0x6b2a9a, emissiveIntensity: 0.16, }); this.rig = rig; rig.play('sleep', { loop: true }); this.legs = addLegs(rig, 0x6a4387); addEyesAndFangs(rig, true); rig.root.position.y = 0; this.view.add(rig.root); // Hilo de seda: se estira hasta el techo y se acorta al bajar. this.thread = meshOf(threadGeo, threadMat, false); this.thread.visible = true; this.view.add(this.thread); this.threadBase.set(0, floor + 15, 0); // Halo violeta estΓ‘tico: separa la silueta oscura del fondo sin flashes. const hm = new THREE.SpriteMaterial({ map: haloTexture(), color: new THREE.Color(0x8a4dff).multiplyScalar(1.25), transparent: true, depthWrite: false, blending: THREE.AdditiveBlending, opacity: 0.26, }); this.halo = new THREE.Sprite(hm); this.halo.scale.set(GIANT.size * 2.3, GIANT.size * 2.3, 1); this.halo.position.z = -1.15; this.view.add(this.halo); this.shadow = blobShadow(1.5, 0.4); this.shadow.position.y = 0.02; this.view.add(this.shadow); for (let i = 0; i < 4; i++) { const s = emojiSprite('⭐', 0.65, { glow: 0.35 }); s.position.set(Math.cos(i * Math.PI / 2) * 1.15, 1.75, 0.3); this.eyes.add(s); } this.eyes.visible = false; this.setPos(this.x, sleepY); this.thread.visible = true; if (g.state.has('spider.defeated')) this.kill(); } onRemoved(): void { this.game.ui.bossBar(null); this.game.gfx.cam.focus(null); this.rig?.dispose(); } private set(s: SState): void { this.state = s; this.stateT = 0; } private bar(): void { this.game.ui.bossBar({ name: 'La AraΓ±a Gigante', emoji: 'πŸ•·οΈ', frac: this.lives / GIANT.lives }); } private beginRecover(): void { this.recoverFromY = this.body.y; this.eyes.visible = false; this.set('recover'); } private wake(): void { if (this.started) return; this.started = true; const g = this.game; this.set('intro'); this.rig.setExpression('angry'); this.rig.play('defeat', { loop: true }); g.state.set('spider.awakened', true); g.state.save(); g.events.emit('spider:awakened', { id: 'spider' }); g.events.emit('boss:start', { id: 'spider' }); g.audio.music('boss'); g.audio.sfx('boss_roar'); g.gfx.cam.focus({ x: this.x, y: this.arena.floor + 5.8 }, 0.92); g.gfx.cam.shake(0.42, 0.6); g.ui.banner('Β‘La AraΓ±a Gigante!', 'La luz la ha despertado. Β‘GolpΓ©ala cuando quede aturdida!', 'πŸ•·οΈ'); this.bar(); g.vfx.burst('dust', this.x, this.arena.floor + 0.2, { count: 18, scale: 1.8 }); } update(dt: number): void { const b = this.body; const g = this.game; const h = this.heroOk; this.t += dt; this.stateT += dt; this.noLightToastT = Math.max(0, this.noLightToastT - dt); if (this.state === 'sleep') { b.vx = 0; b.vy = 0; b.y = this.arena.floor + 7.6 + Math.sin(this.t * 1.2) * 0.17; b.x += (this.x - b.x) * Math.min(1, dt * 2); if (h && h.x > this.arena.x0 + 1.4 && h.x < this.arena.x1 - 1.4) { if (g.state.has('spider.defeated')) { this.kill(); return; } if (g.state.has('tool.flashlight.on')) this.wake(); else if (this.noLightToastT <= 0) { this.noLightToastT = 7; g.ui.toast('EstΓ‘ demasiado oscuro… Enciende la linterna πŸ”¦', 'πŸ•·οΈ'); g.audio.sfx('error', { volume: 0.3 }); } } return; } const floor = this.arena.floor; const hoverY = floor + GIANT.hover + Math.sin(this.t * 1.7) * 0.22; switch (this.state) { case 'intro': { const u = Math.min(1, this.stateT / GIANT.intro); const e = u * u * (3 - 2 * u); b.y = floor + 7.6 + (hoverY - (floor + 7.6)) * e; b.x += (this.x - b.x) * Math.min(1, dt * 2); if (this.stateT > 1.25 && this.stateT - dt <= 1.25) { g.audio.sfx('boss_roar', { pitch: 0.82 }); g.vfx.ring(this.x, floor + 0.08, 0xff5a3a, 2.8); this.rig.squash(0.35); } if (u >= 1) { g.gfx.cam.focus(null); this.set('idle'); this.stateT = 2.0; } break; } case 'idle': { this.floatToward(hoverY, dt); this.followHero(dt); if (this.stateT >= 2.0) this.chooseAttack(); break; } case 'telegraph': { this.floatToward(floor + GIANT.hover + 1.1, dt); this.faceHero(dt); this.ringT -= dt; if (this.ringT <= 0) { this.ringT = 0.24; g.vfx.ring(this.targetX, floor + 0.07, 0xff5a3a, 1.7 + (this.phase + 1) * 0.18); } if (this.stateT >= GIANT.telegraph[this.phase]) this.startCharge(); break; } case 'charge': { const dir = Math.sign(this.targetX - b.x) || this.facing; b.x += dir * GIANT.chargeSpeed[this.phase] * dt; b.y = floor + GIANT.floorOffset + 0.8; g.vfx.trail('dust', b.x - dir * 0.8, b.y - 1.1, { count: 1, scale: 1.2, dirX: -dir }); if ((dir > 0 && b.x >= this.targetX) || (dir < 0 && b.x <= this.targetX) || b.x < this.arena.x0 + GIANT.radius || b.x > this.arena.x1 - GIANT.radius || this.stateT > 1.15) { b.x = Math.max(this.arena.x0 + GIANT.radius, Math.min(this.arena.x1 - GIANT.radius, b.x)); this.impactStun(); } break; } case 'stunned': { b.x += (this.targetX - b.x) * Math.min(1, dt * 3); b.y += (floor + GIANT.floorOffset - b.y) * Math.min(1, dt * 5); if (this.stateT >= GIANT.stun[this.phase]) { this.beginRecover(); this.rig.setExpression(this.phase === 2 ? 'angry' : 'evil'); this.rig.play('float', { loop: true }); } break; } case 'webWind': { this.floatToward(hoverY + 1.3, dt); this.faceHero(dt); if (!this.webFired && this.stateT >= GIANT.webWind) { this.webFired = true; this.fireWebs(); } if (this.stateT >= GIANT.webWind + 0.62) { this.beginRecover(); } break; } case 'webFire': { this.beginRecover(); break; } case 'ceiling': { const ceilingY = floor + 8.7; if (this.stateT < 1.1) { b.y += (ceilingY - b.y) * Math.min(1, dt * 4.4); b.x += (this.x - b.x) * Math.min(1, dt * 2); } else { b.y = ceilingY + Math.sin(this.t * 19) * 0.06 * (this.stateT < GIANT.ceiling - 0.7 ? 1 : 0); if (this.ceilingSpawned < 3 && this.stateT > 1.35 + this.ceilingSpawned * 0.52) { const hx = h ? h.x : (this.arena.x0 + this.arena.x1) * 0.5; const jitter = this.ceilingSpawned === 0 ? 0 : this.ceilingSpawned === 1 ? -2.5 : 2.5; g.add(new FallingStalactite( Math.max(this.arena.x0 + 1.3, Math.min(this.arena.x1 - 1.3, hx + jitter)), floor + 11.5, floor, )); this.ceilingSpawned++; g.gfx.cam.shake(0.12, 0.16); g.audio.sfx('land', { pitch: 0.55, volume: 0.35 }); } } if (this.stateT >= GIANT.ceiling) { this.beginRecover(); this.rig.play('float', { loop: true }); } break; } case 'summon': { this.floatToward(hoverY + 0.8, dt); this.faceHero(dt); if (!this.summoned && this.stateT > 0.55) { this.summoned = true; this.summonSpiders(); } if (this.stateT > 1.15) this.beginRecover(); break; } case 'hurt': { b.y = floor + GIANT.floorOffset + Math.sin(Math.min(1, this.stateT / 0.55) * Math.PI) * 0.55; if (this.stateT >= 0.55) { this.beginRecover(); this.rig.setExpression(this.phase === 2 ? 'angry' : 'evil'); this.rig.play('float', { loop: true }); } break; } case 'recover': { const u = Math.min(1, this.stateT / 0.85); const from = this.recoverFromY; b.y = from + (hoverY - from) * (u * u * (3 - 2 * u)); if (u >= 1) { this.set('idle'); this.stateT = 0; } break; } case 'defeat': this.updateDefeat(dt); return; default: this.set('idle'); } if (this.state === 'idle' || this.state === 'telegraph' || this.state === 'charge') this.checkContact(); } private floatToward(y: number, dt: number): void { this.body.y += (y - this.body.y) * Math.min(1, dt * 3.2); } private faceHero(dt: number): void { const h = this.heroOk; if (h && Math.abs(h.x - this.body.x) > 0.2) this.facing = h.x > this.body.x ? 1 : -1; void dt; } private followHero(dt: number): void { const h = this.heroOk; if (!h) return; const tx = Math.max(this.arena.x0 + GIANT.radius + 0.5, Math.min(this.arena.x1 - GIANT.radius - 0.5, h.x)); this.body.x += (tx - this.body.x) * Math.min(1, dt * 0.9); this.faceHero(dt); } private chooseAttack(): void { const g = this.game; const ph = this.phase; const options: string[] = ['charge']; if (ph >= 1) options.push('web'); if (ph >= 2) options.push('ceiling', 'summon'); const pick = options[this.attackIndex % options.length]; this.attackIndex++; const h = this.heroOk; this.targetX = h ? Math.max(this.arena.x0 + GIANT.radius, Math.min(this.arena.x1 - GIANT.radius, h.x)) : this.body.x; if (pick === 'charge') { this.set('telegraph'); this.ringT = 0; this.rig.setExpression('angry'); this.rig.play('attack', { speed: 1.2 }); g.audio.sfx('boss_roar', { pitch: 1.25, volume: 0.65 }); } else if (pick === 'web') { this.set('webWind'); this.webFired = false; this.rig.setExpression('evil'); this.rig.play('special', { speed: 1.1 }); g.audio.sfx('whoosh', { pitch: 1.45, volume: 0.55, x: this.body.x }); g.vfx.ring(this.body.x, this.arena.floor + 0.07, 0xa455f0, 1.5); } else if (pick === 'ceiling') { this.set('ceiling'); this.ceilingSpawned = 0; this.rig.play('float', { loop: true }); g.audio.sfx('boss_roar', { pitch: 0.9, volume: 0.55 }); g.ui.toast('Β‘La cueva tiembla! Mira las sombras del suelo', 'πŸͺ¨'); } else { this.set('summon'); this.summoned = false; this.rig.setExpression('evil'); g.audio.sfx('boss_roar', { pitch: 1.1, volume: 0.6 }); } } private startCharge(): void { this.set('charge'); this.rig.play('attack', { speed: 1.5 }); this.rig.squash(0.38); this.game.audio.sfx('whoosh', { pitch: 0.72, x: this.body.x }); } private impactStun(): void { const g = this.game; const floor = this.arena.floor; this.set('stunned'); this.targetX = this.body.x; this.rig.play('hurt'); this.rig.setExpression('hurt'); this.rig.squash(-0.4); this.eyes.visible = true; g.gfx.cam.shake(0.6, 0.38); g.audio.sfx('thunder', { volume: 0.65, pitch: 0.8, x: this.body.x }); g.audio.sfx('land', { pitch: 0.55, x: this.body.x }); g.vfx.burst('dust', this.body.x - 1.4, floor + 0.1, { count: 12, scale: 1.5, dirX: -1 }); g.vfx.burst('dust', this.body.x + 1.4, floor + 0.1, { count: 12, scale: 1.5, dirX: 1 }); g.vfx.ring(this.body.x, floor + 0.08, 0xffe066, 2.4); g.vfx.burst('star', this.body.x, this.body.y + 1.3, { count: 8 }); g.ui.toast('Β‘Aturdida! Ahora es tu momento', 'πŸ•·οΈ'); } private fireWebs(): void { const g = this.game; const h = this.heroOk; const b = this.body; const tx = h ? h.x : b.x; const ty = h ? h.y : this.arena.floor + 1; for (let i = -1; i <= 1; i++) { g.add(new SpiderWebShot(b.x, b.y - 0.5, tx + i * 2.2, ty, this.arena.floor)); } this.rig.squash(0.32); g.audio.sfx('splash', { volume: 0.45, pitch: 0.7 }); g.vfx.burst('sparkle', b.x, b.y - 0.5, { count: 12, color: 0xc8b0ff, scale: 1.1 }); } private summonSpiders(): void { const g = this.game; if (!g.hasType('enemy.spider')) return; for (let i = 0; i < 2; i++) { const x = i === 0 ? this.arena.x0 + 2.6 : this.arena.x1 - 2.6; g.vfx.burst('poof', x, this.arena.floor + 1.4, { count: 10, scale: 1.1 }); g.spawn('enemy.spider', x, this.arena.floor + 8.2, { facing: i === 0 ? 1 : -1 }); } g.audio.sfx('teleport', { pitch: 0.7, volume: 0.5 }); g.ui.toast('Β‘La AraΓ±a llama a sus crΓ­as!', 'πŸ•·οΈ'); } private checkContact(): void { const h = this.heroOk; if (!h || this.state === 'sleep' || this.state === 'intro' || this.state === 'defeat') return; const harmless = this.state === 'stunned' || this.state === 'hurt' || this.state === 'recover'; if (harmless) return; const dx = h.x - this.body.x; const dy = h.y - this.body.y; const r = GIANT.radius * 0.86 + 0.42; if (dx * dx + dy * dy <= r * r) { h.takeHit({ damage: GIANT.contact.damage, element: 'none', dirX: dx < 0 ? -1 : 1, knock: GIANT.contact.knock, source: this }); } } takeHit(hit: Hit): boolean { const g = this.game; if (this.state === 'sleep' || this.state === 'intro' || this.state === 'defeat') return false; if (!this.vulnerable || hit.damage <= 0) { if (hit.damage > 0) { g.vfx.burst('spark', this.body.x, this.body.y, { count: 5, color: 0xc8b0ff }); g.audio.sfx('hit', { pitch: 1.55, volume: 0.28, x: this.body.x }); } return false; } this.registerHit(hit); return true; } private registerHit(hit: Hit): void { const g = this.game; const b = this.body; const before = this.phase; this.lives--; this.hp = this.lives; g.hitstop(0.15); g.gfx.cam.shake(0.56, 0.3); g.audio.sfx('boss_hit', { x: b.x }); g.vfx.burst('hit', b.x - (hit.dirX || 0) * 1.25, b.y, { scale: 2.4, dirX: hit.dirX }); g.vfx.burst('star', b.x, b.y + 1.2, { count: 10 }); g.vfx.floatText('-1', b.x, b.y + GIANT.radius + 0.8, '#ffffff'); this.rig.flash(0xffffff, 0.24); this.rig.squash(-0.36); this.rig.play('hurt'); this.eyes.visible = false; this.bar(); if (this.lives <= 0) { this.startDefeat(); return; } this.set('hurt'); if (this.phase !== before) { if (this.phase === 1) g.ui.toast('Β‘La AraΓ±a escupe telaraΓ±as!', 'πŸ•ΈοΈ'); else g.ui.toast('Β‘Fase final: cuidado con el techo!', 'πŸͺ¨'); g.vfx.burst('confetti', b.x, b.y, { count: 16 }); } } private startDefeat(): void { const g = this.game; this.set('defeat'); this.finished = true; this.eyes.visible = false; g.ui.bossBar(null); g.gfx.cam.focus({ x: this.body.x, y: this.arena.floor + 3.6 }, 1.05); g.gfx.cam.shake(0.8, 0.9); g.hitstop(0.18); g.audio.sfx('boss_die'); g.audio.music('victory', 1.5); g.vfx.burst('confetti', this.body.x, this.body.y, { count: 44, scale: 1.8 }); g.vfx.burst('shatter', this.body.x, this.body.y, { count: 18, color: 0xa455f0, scale: 1.6 }); g.state.set('spider.defeated', true); g.state.save(); g.events.emit('boss:defeated', { id: 'spider' }); g.ui.banner('Β‘AraΓ±a Gigante derrotada!', 'El anillo de Emma ha caΓ­do al suelo', 'πŸ’'); this.rig.setExpression('hurt'); this.rig.play('defeat', { loop: true }); } private updateDefeat(dt: number): void { const g = this.game; const b = this.body; b.y += (this.arena.floor + GIANT.floorOffset - b.y) * Math.min(1, dt * 2.4); b.x += Math.sin(this.t * 12) * 0.03; this.rig.squash(Math.sin(this.t * 8) * 0.04); if (!this.ringDropped && this.stateT > 1.15) { this.ringDropped = true; if (g.hasType('item.ring') && !g.state.has('emma.ring')) { g.spawn('item.ring', b.x, this.arena.floor + 0.1, { id: 'emma-ring', pop: true, vx: 0, }); } g.vfx.ring(b.x, this.arena.floor + 0.08, 0xffe066, 2.1); g.gfx.cam.shake(0.2, 0.3); } if (this.stateT > 2.8) { g.gfx.cam.focus(null); this.kill(); } } render(alpha: number, dt: number): void { super.render(alpha, dt); const b = this.body; const top = this.threadBase.y - b.y; this.thread.visible = top > 0.2 && this.state !== 'defeat' ? true : top > 0.2; if (this.thread.visible) { this.thread.position.y = top * 0.5; this.thread.scale.y = Math.max(0.02, top); } this.shadow.position.y = this.arena.floor - b.y + 0.035; this.shadow.scale.setScalar(Math.max(0.35, 1 - (b.y - this.arena.floor) * 0.045)); this.rig.update(dt, { vx: b.vx, vy: b.vy, onGround: false, facing: this.facing }); animateLegs(this.legs, this.t, this.state === 'charge' ? 16 : this.state === 'stunned' ? 2.2 : 5.5, this.state === 'charge' || this.state === 'idle' || this.state === 'webWind'); this.eyes.rotation.y = this.t * 1.2; this.eyes.position.y = 1.7 + Math.sin(this.t * 2.2) * 0.08; if (this.state === 'stunned') this.rig.squash(Math.sin(this.t * 5) * 0.035); } }