/** * Coleccionables: base `Pickup` (vaivén, giro, destellos, imán hacia el héroe, efecto `collect`, * `item:collected`) y los tipos 🪙 moneda, ❤️ corazón, 💎 diamante y orbe de poder/herramienta. * * Props comunes: `id?` (si se da, se guarda `item.` y no reaparece), `pop?` (sale disparado en * arco, p. ej. desde un cofre). `y` = base (el objeto flota ~0.6 u por encima). */ import * as THREE from 'three'; import { Entity } from '../../core/entity'; import type { PowerId, Props, ToolId } from '../../core/types'; import { rand } from '../../core/math'; import { tallyAdd, tallyAutoId, tallyGot, type TallyKind } from '../../core/tally'; import { blobShadow, emojiMesh, emojiSprite, toonMaterial } from '../../render'; export abstract class Pickup extends Entity { protected readonly holder = new THREE.Group(); protected readonly shadow: THREE.Mesh; protected t = rand(0, 10); protected hover = 0.6; protected magnet = 2.3; protected collectR = 0.75; protected spin = 2.2; protected persistId: string | null; private vx = 0; private vy = 0; private popping = false; private delay = 0; private pull = 0; private groundY: number | null = null; private taken = false; /** Cuenta para el recuento de la zona (moneda/diamante del tesoro repartido). */ private counted = false; /** 'c' moneda, 'd' diamante; null = no cuenta (corazones, orbes). */ protected tallyKind: TallyKind | null = null; constructor(props: Props, shadowR = 0.3) { super(); this.team = 'neutral'; this.tags.add('item'); this.persistId = props.id ? `item.${props.id}` : null; this.view.add(this.holder); this.shadow = blobShadow(shadowR, 0.3); this.view.add(this.shadow); if (props.tally) this.counted = true; if (props.pop) { this.popping = true; this.vy = rand(7, 10); this.vx = props.vx ?? rand(-2.6, 2.6); this.delay = 0.55; } } onAdded(): void { const g = this.game; // monedas y diamantes colocados en el nivel: persistentes y contados por zona if (this.tallyKind && g.loading && !this.popping && !this.counted) { if (!this.persistId) this.persistId = `item.${tallyAutoId(g, this.tallyKind, this.x, this.y)}`; this.counted = true; tallyAdd(g, this.tallyKind); } if (this.persistId && this.game.state.has(this.persistId)) { this.kill(); return; } this.groundY = this.game.physics.groundBelow(this.x, this.y + 0.3, 30); } bounds() { return { x: this.x, y: this.y + this.hover, hw: 0.35, hh: 0.35 }; } /** ¿Puede recogerse ahora? (corazón con vida llena → no). */ protected canCollect(): boolean { return true; } protected abstract onCollect(): void; update(dt: number): void { this.t += dt; const g = this.game; if (this.popping) { this.vy -= 26 * dt; this.x += this.vx * dt; this.y += this.vy * dt; if (g.physics.solidAt(this.x + Math.sign(this.vx) * 0.3, this.y + 0.3)) this.vx = -this.vx * 0.4; if (this.vy < 0) { const gy = g.physics.groundBelow(this.x, this.y + 0.4, 0.6); if (gy !== null && this.y <= gy) { this.y = gy; if (this.vy < -4) { this.vy = -this.vy * 0.35; this.vx *= 0.6; } else { this.popping = false; this.vy = 0; } } } const minY = g.zone?.bounds.minY; if (minY !== undefined && this.y < minY - 2) this.kill(); this.groundY = g.physics.groundBelow(this.x, this.y + 0.3, 30); } this.delay = Math.max(0, this.delay - dt); const hero = g.hero; if (!hero || !hero.alive || this.delay > 0 || this.taken) return; const cy = this.y + this.hover; const dx = hero.x - this.x; const dy = hero.y - cy; const d = Math.hypot(dx, dy); if (!this.canCollect()) { this.pull = 0; return; } if (d < this.collectR) { this.collect(); return; } if (d < this.magnet && this.magnet > 0) { this.popping = false; this.pull = Math.min(18, this.pull + 40 * dt); const step = Math.min(d, this.pull * dt); this.x += (dx / d) * step; this.y += (dy / d) * step; } else { this.pull = 0; } } protected collect(): void { if (this.taken) return; this.taken = true; const g = this.game; const cy = this.y + this.hover; g.vfx.burst('collect', this.x, cy, { count: 10 }); this.onCollect(); if (this.counted && this.tallyKind) tallyGot(g, this.tallyKind); if (this.persistId) g.state.set(this.persistId, true); g.events.emit('item:collected', { type: this.type, id: this.persistId ?? undefined, x: this.x, y: cy }); this.kill(); } render(alpha: number, dt: number): void { super.render(alpha, dt); const tt = this.t; this.holder.position.y = this.hover + Math.sin(tt * 2.6) * 0.12; this.holder.rotation.y += dt * this.spin; const gy = this.groundY; const vy = this.view.position.y; if (gy === null || vy - gy > 6) this.shadow.visible = false; else { const h = vy + this.holder.position.y - gy; this.shadow.visible = true; this.shadow.position.y = gy - vy + 0.02; const k = Math.max(0.3, 1 - h * 0.15); this.shadow.scale.setScalar(k * (1 - Math.sin(tt * 2.6) * 0.08)); } // (sin destellos periódicos: parpadeaban como flashes) } } // ───────────────────────────── Tipos ───────────────────────────── export class CoinItem extends Pickup { constructor(props: Props) { super(props, 0.28); this.holder.add(emojiMesh('🪙', 0.8, { glow: true, glowIntensity: 0.35 })); this.spin = 3.4; this.tallyKind = 'c'; } protected onCollect(): void { const g = this.game; g.state.addCoins(1); g.audio.sfx('star', { x: this.x, pitch: rand(0.95, 1.1) }); g.vfx.floatText('+1 🪙', this.x, this.y + this.hover + 0.5, '#ffe066'); } } export class HeartItem extends Pickup { private full = false; constructor(props: Props) { super(props, 0.28); this.holder.add(emojiMesh('❤️', 0.75, { glow: true, glowIntensity: 0.3 })); this.spin = 0; } protected canCollect(): boolean { const d = this.game.state.data; this.full = d.hp >= d.maxHp; return !this.full; } protected onCollect(): void { const g = this.game; g.hero?.heal(2); g.vfx.floatText('+❤️', this.x, this.y + this.hover + 0.5, '#ff7fa0'); } render(alpha: number, dt: number): void { super.render(alpha, dt); // Late como un corazón en vez de girar. const beat = 1 + Math.max(0, Math.sin(this.t * 6)) ** 6 * 0.18; this.holder.scale.setScalar(beat); this.holder.rotation.y = Math.sin(this.t * 1.7) * 0.45; } } /** * Contenedor de corazón 💖 `item.heartbox { id }`: +1 corazón máximo (2 de vida) y cura entera. * Escondido en rincones secretos (bajo abismos con burbujas, en lo alto, tras paredes cavables). */ export class HeartContainer extends Pickup { private readonly ring: THREE.Mesh; constructor(props: Props) { super(props, 0.36); this.holder.add(emojiMesh('💖', 0.95, { glow: true, glowIntensity: 0.5 })); this.ring = new THREE.Mesh( new THREE.TorusGeometry(0.58, 0.03, 8, 36), toonMaterial({ color: 0xffffff, emissive: 0xff5fa0, emissiveIntensity: 0.9 }), ); this.holder.add(this.ring); this.spin = 0; this.hover = 0.8; this.magnet = 1.6; } onAdded(): void { if (!this.persistId) this.persistId = `item.heartbox.${this.game.zone?.id ?? ''}.${Math.round(this.x)}_${Math.round(this.y)}`; super.onAdded(); if (!this.dead && this.game.zone) { this.game.events.emit('map:poi', { zone: this.game.zone.id, x: this.x, y: this.y + 0.6, emoji: '💖', id: this.persistId }); } } protected onCollect(): void { const g = this.game; const d = g.state.data; d.maxHp = Math.min(20, d.maxHp + 2); d.hp = d.maxHp; g.state.changed(); if (g.zone && this.persistId) delete d.flags[`map.poi.${g.zone.id}.${this.persistId}`]; g.audio.sfx('quest', { volume: 0.8 }); g.vfx.ring(this.x, this.y + this.hover, 0xff5fa0, 2.2); g.vfx.burst('sparkle', this.x, this.y + this.hover, { count: 20, scale: 1.3, color: 0xff9fc8 }); g.vfx.floatText('+1 ❤️ máx.', this.x, this.y + this.hover + 0.7, '#ff7fb0'); g.ui.toast('¡Contenedor de corazón! Tienes un corazón más', '💖'); g.state.save(); } render(alpha: number, dt: number): void { super.render(alpha, dt); const beat = 1 + Math.max(0, Math.sin(this.t * 4)) ** 6 * 0.14; this.holder.scale.setScalar(beat); this.holder.rotation.y = Math.sin(this.t * 1.3) * 0.5; this.ring.rotation.x = 1.2; this.ring.rotation.z += dt * 1.4; } } export class DiamondItem extends Pickup { constructor(props: Props) { super(props, 0.28); this.holder.add(emojiMesh('💎', 0.75, { glow: true, glowIntensity: 0.45, gloss: 1 })); this.spin = 2.6; this.tallyKind = 'd'; } protected onCollect(): void { const g = this.game; g.state.addDiamonds(1); g.audio.sfx('gem', { x: this.x }); g.vfx.floatText('+1 💎', this.x, this.y + this.hover + 0.5, '#9fe8ff'); g.state.save(); } } const POWER_LOOK: Record = { fury: { emoji: '😡', color: 0xf2452e }, ice: { emoji: '🥶', color: 0x3fb4ff }, fire: { emoji: '🔥', color: 0xff8a1e }, cool: { emoji: '😎', color: 0xffc928 }, base: { emoji: '🙂', color: 0xffc928 }, bubbles: { emoji: '🫧', color: 0x7fd8ff }, watergun: { emoji: '🔫', color: 0x2fa8f5 }, flashlight: { emoji: '🔦', color: 0xffe08a }, shovel: { emoji: '🪏', color: 0xc08a50 }, rain: { emoji: '🌧️', color: 0x6fa8ff }, }; let orbGeo: THREE.SphereGeometry | null = null; let ringGeo: THREE.TorusGeometry | null = null; /** Orbe con el emoji de un EmojiPower o herramienta. Props: `{ power }` o `{ tool }`. */ export class PowerupItem extends Pickup { private readonly power: PowerId | null; private readonly tool: ToolId | null; private readonly ring: THREE.Mesh; private readonly orbMat: THREE.Material; private readonly ringMat: THREE.Material; constructor(props: Props) { super({ ...props, id: props.id ?? `powerup.${props.power ?? props.tool}` }, 0.4); this.power = (props.power as PowerId) ?? null; this.tool = this.power ? null : ((props.tool as ToolId) ?? null); const look = POWER_LOOK[this.power ?? this.tool ?? 'base'] ?? POWER_LOOK.base; this.hover = 0.9; this.magnet = 0; this.collectR = 0.9; this.spin = 0; if (!orbGeo) { orbGeo = new THREE.SphereGeometry(0.5, 28, 20); orbGeo.userData.shared = true; ringGeo = new THREE.TorusGeometry(0.62, 0.035, 8, 40); ringGeo.userData.shared = true; } this.orbMat = toonMaterial({ color: look.color, emissive: look.color, emissiveIntensity: 0.35, gloss: 1, rim: 1, transparent: true, opacity: 0.38, depthWrite: false }); const orb = new THREE.Mesh(orbGeo, this.orbMat); this.holder.add(orb); const icon = emojiSprite(look.emoji, 0.62, { glow: 0.3 }); icon.renderOrder = 2; this.holder.add(icon); this.ringMat = toonMaterial({ color: 0xffffff, emissive: look.color, emissiveIntensity: 1.2 }); this.ring = new THREE.Mesh(ringGeo!, this.ringMat); this.holder.add(this.ring); } onAdded(): void { const st = this.game.state; if ((this.power && st.hasPower(this.power)) || (this.tool && st.hasTool(this.tool))) { this.kill(); return; } super.onAdded(); } protected onCollect(): void { const g = this.game; const h = g.hero; g.vfx.ring(this.x, this.y + this.hover, 0xffffff, 2); g.vfx.flash(0xffffff, 0.25); g.hitstop(0.08); if (this.power) h?.grantPower(this.power); else if (this.tool) h?.grantTool(this.tool); g.state.save(); } render(alpha: number, dt: number): void { super.render(alpha, dt); this.ring.rotation.x = 1.1 + Math.sin(this.t) * 0.2; this.ring.rotation.z += dt * 1.6; const s = 1 + Math.sin(this.t * 3) * 0.05; this.holder.scale.setScalar(s); } }