/** * Plantilla de previsualización de personajes (abrir /preview.html). * Muestra el patrón de diseño: héroe con sus expresiones, transformaciones, Emma y NPC basados en emojis. */ import '@fontsource/fredoka/500.css'; import '@fontsource/fredoka/600.css'; import '@fontsource/fredoka/700.css'; import * as THREE from 'three'; import { createRenderer } from './render'; import { toonMaterial } from './render/materials'; import { createChibi } from './characters/chibi'; import type { ChibiAnim, ChibiOptions, ChibiRig } from './characters/types'; interface Card { label: string; note?: string; opts: ChibiOptions } const Y = 0xffcf24; const hero = (expression: ChibiOptions['expression'], extra: Partial = {}): ChibiOptions => ({ color: Y, expression, ...extra }); const ROWS: Array<{ title: string; cards: Card[] }> = [ { title: 'Héroe · expresiones', cards: [ { label: '🙂 Base', opts: hero('smile') }, { label: '😊 Feliz', opts: hero('blush') }, { label: '😄 Alegría', opts: hero('laugh') }, { label: '😉 Guiño', opts: hero('wink') }, { label: '😮 Sorpresa', opts: hero('surprised') }, { label: '😣 Daño', opts: hero('hurt') }, { label: '😢 Triste', opts: hero('sad') }, { label: '😍 Amor', opts: hero('love') }, { label: '🤩 Premio', opts: hero('starstruck') }, { label: '😴 Dormido', opts: hero('sleepy') }, ], }, { title: 'Poderes y amigos', cards: [ { label: '🎒 Héroe', note: 'con mochila', opts: hero('laugh', { outfit: 'backpack' }) }, { label: '😡 FURIA', opts: { color: 0xf2452e, expression: 'angry', accessory: 'steam' } }, { label: '🥶 HIELO', opts: { color: 0xa9daf7, expression: 'cold', accessory: 'iceCrown' } }, { label: '🔥 FUEGO', opts: { color: 0xff8a1e, expression: 'fierce', accessory: 'flameHair', emissive: 0xff5a00, emissiveIntensity: 0.18 } }, { label: '😎 COOL', opts: hero('cool', { accessory: 'sunglasses' }) }, { label: '🤗 Emma', opts: hero('blush') }, { label: '👽 Alien', note: 'NPC', opts: { color: 0x66d99a, expression: 'alien' } }, ], }, { title: 'Bosque Susurro · NPC', cards: [ { label: '👩‍🌾 Anna', note: 'enfadada', opts: hero('angry', { outfit: 'farmer', accessory: 'steam' }) }, { label: '👩‍🌾 Anna', note: 'tranquila', opts: hero('blush', { outfit: 'farmer' }) }, { label: '🧙 Sabio', opts: { color: Y, expression: 'smile', outfit: 'sage' } }, { label: '🧝‍♀️ Elfa', opts: hero('determined', { outfit: 'elf' }) }, { label: '🤴 Rey Frío', opts: { color: 0xa9daf7, expression: 'cold', outfit: 'king', accessory: 'crown' } }, { label: '🧙‍♂️ Hechicero', opts: { color: 0x9a7ad8, expression: 'evil', outfit: 'wizard' } }, { label: '🔥 Chispa', note: 'enemigo', opts: { color: 0xff8a1e, expression: 'fierce', feet: false, emissive: 0xff5a00, emissiveIntensity: 0.35 } }, { label: '🪲 Escarabajo', note: 'enemigo', opts: { color: 0x3fbf8f, expression: 'angry' } }, { label: '🧊 Gólem', note: 'enemigo', opts: { color: 0xa9daf7, expression: 'cold' } }, { label: '🐝 Abeja', note: 'enemigo', opts: { color: 0xffc928, expression: 'angry', emissive: 0xffa800, emissiveIntensity: 0.1 } }, { label: '🍄 Seta', note: 'enemigo', opts: { color: 0xf2452e, expression: 'evil' } }, ], }, { title: 'Vecinos · NPC', cards: [ { label: '👵 Abuela', opts: hero('smile', { outfit: 'granny' }) }, { label: '🤖 Robot', opts: { color: 0xf4f6ff, expression: 'smile', shape: 'robot', outfit: 'robot', handColor: 0xf4f6ff } }, { label: '🤠 Vaquero', opts: hero('smile', { outfit: 'cowboy' }) }, { label: '👷 Obrero', opts: hero('smile', { outfit: 'builder' }) }, { label: '👮 Policía', opts: hero('smile', { outfit: 'police' }) }, { label: '👨 Vecino', opts: hero('smile', { outfit: 'man' }) }, { label: '👨‍🍳 Cocinero', opts: hero('laugh', { outfit: 'chef' }) }, { label: '👴 Abuelo', opts: hero('smile', { outfit: 'grandpa' }) }, { label: '👻 Fantasma', note: 'enemigo', opts: { color: 0xe4ddff, shape: 'ghost', expression: 'evil', opacity: 0.88, emissive: 0xb9a8ff, emissiveIntensity: 0.07 } }, { label: '🤖 Robot', note: 'enemigo', opts: { color: 0xf4f6ff, shape: 'robot', expression: 'angry', handColor: 0xd9dcef, footColor: 0x3b3f5c, accessory: 'antenna' } }, ], }, ]; const SIZE = 1.45; const SPACING = 2.3; const ROW_H = 3.15; const canvas = document.getElementById('game') as HTMLCanvasElement; const labels = document.getElementById('labels') as HTMLDivElement; const gfx = createRenderer(canvas); gfx.setEnv('city-day', 0); gfx.setEnv({ skyTop: 0xe4e2ff, skyBottom: 0xf7f5ff, skyHorizon: 0xf7f5ff, fogColor: 0xf3f1ff, fogNear: 200, fogFar: 400, clouds: 0, stars: 0, sun: 0, moon: 0, bloom: 0.3, ambientIntensity: 1.75, sunIntensity: 2.3, ambientColor: 0xffffff, groundColor: 0xfff2d8, vignette: 0.08, warmth: 0.12, saturation: 1.08, }, 0); interface Item { rig: ChibiRig; x: number; base: number; el: HTMLDivElement; phase: number } const items: Item[] = []; const rowTags: Array<{ el: HTMLDivElement; y: number }> = []; const shelfMat = toonMaterial({ color: 0xffffff, gloss: 0.15, rim: 0 }); ROWS.forEach((row, r) => { const base = (ROWS.length - 1 - r) * ROW_H; const shelf = new THREE.Mesh(new THREE.BoxGeometry(SPACING * 10.8, 0.16, 3.4), shelfMat); shelf.position.set(0, base - 0.08, -0.2); shelf.receiveShadow = true; gfx.scene.add(shelf); const tag = document.createElement('div'); tag.className = 'row'; tag.textContent = row.title; labels.appendChild(tag); rowTags.push({ el: tag, y: base + SIZE * 1.42 }); const n = row.cards.length; row.cards.forEach((card, i) => { const x = (i - (n - 1) / 2) * SPACING; const rig = createChibi({ ...card.opts, size: SIZE }); rig.root.position.set(x, base + SIZE * 0.5 + 0.02, 0); gfx.scene.add(rig.root); const el = document.createElement('div'); el.className = 'lbl'; el.innerHTML = card.label + (card.note ? `${card.note}` : ''); labels.appendChild(el); items.push({ rig, x, base, el, phase: i * 0.7 + r * 1.3 }); }); }); // ── Animaciones de muestra ── type Mode = 'idle' | 'run' | 'jump' | 'attack' | 'victory' | 'wave' | 'turn'; const MODES: Array<[Mode, string]> = [ ['idle', 'Quieto'], ['turn', 'Girar'], ['run', 'Correr'], ['jump', 'Saltar'], ['attack', 'Golpear'], ['victory', 'Victoria'], ['wave', 'Saludar'], ]; let mode: Mode = 'idle'; const bar = document.getElementById('bar') as HTMLDivElement; for (const [m, text] of MODES) { const b = document.createElement('button'); b.textContent = text; b.dataset.mode = m; b.onclick = () => setMode(m); bar.appendChild(b); } function setMode(m: Mode): void { mode = m; for (const b of bar.querySelectorAll('button')) b.classList.toggle('on', b.dataset.mode === m); } setMode((new URLSearchParams(location.search).get('mode') as Mode) || 'idle'); const cam = gfx.camera; const centerY = ((ROWS.length - 1) * ROW_H) / 2 + SIZE * 0.55; let override: { x: number; y: number; dist: number } | null = null; function placeCamera(): void { if (override) { cam.position.set(override.x, override.y + override.dist * 0.07, override.dist); cam.lookAt(override.x, override.y, 0); cam.updateMatrixWorld(); return; } const tan = Math.tan(THREE.MathUtils.degToRad(cam.fov / 2)); const halfW = SPACING * 5.3; const halfH = (ROWS.length * ROW_H) / 2 + 0.75; const dist = Math.max(halfW / (tan * cam.aspect), halfH / tan); cam.position.set(0, centerY + dist * 0.07, dist); cam.lookAt(0, centerY - 0.15, 0); cam.updateMatrixWorld(); } const v = new THREE.Vector3(); function toScreen(x: number, y: number, z = 0): [number, number] { v.set(x, y, z).project(cam); return [(v.x * 0.5 + 0.5) * window.innerWidth, (1 - (v.y * 0.5 + 0.5)) * window.innerHeight]; } // ── Inspección: rueda = zoom hacia el cursor, arrastrar = mover, doble clic = vista completa ── function currentView(): { x: number; y: number; dist: number } { if (override) return override; const tan = Math.tan(THREE.MathUtils.degToRad(cam.fov / 2)); const dist = Math.max((SPACING * 5.3) / (tan * cam.aspect), ((ROWS.length * ROW_H) / 2 + 0.75) / tan); return { x: 0, y: centerY - 0.15, dist }; } canvas.addEventListener("wheel", (e) => { e.preventDefault(); const cur = currentView(); const k = Math.exp(e.deltaY * 0.0012); const dist = THREE.MathUtils.clamp(cur.dist * k, 3.2, 60); const tan = Math.tan(THREE.MathUtils.degToRad(cam.fov / 2)); const nx = (e.clientX / window.innerWidth) * 2 - 1; const ny = 1 - (e.clientY / window.innerHeight) * 2; const f = cur.dist - dist; override = { x: cur.x + nx * tan * cam.aspect * f, y: cur.y + ny * tan * f, dist }; }, { passive: false }); let drag: { x: number; y: number } | null = null; canvas.addEventListener("pointerdown", (e) => { drag = { x: e.clientX, y: e.clientY }; canvas.setPointerCapture(e.pointerId); }); canvas.addEventListener("pointerup", () => { drag = null; }); canvas.addEventListener("pointermove", (e) => { if (!drag) return; const cur = currentView(); const perPx = (2 * Math.tan(THREE.MathUtils.degToRad(cam.fov / 2)) * cur.dist) / window.innerHeight; override = { x: cur.x - (e.clientX - drag.x) * perPx, y: cur.y + (e.clientY - drag.y) * perPx, dist: cur.dist }; drag = { x: e.clientX, y: e.clientY }; }); canvas.addEventListener("dblclick", () => { override = null; }); let last = performance.now(); let time = 0; let oneShot = 0; function frame(dt: number): void { time += dt; oneShot -= dt; const fire = oneShot <= 0; if (fire) oneShot = mode === 'victory' ? 2 : mode === 'wave' ? 1.7 : 0.9; for (const it of items) { const t = time + it.phase; let motion = { vx: 0, vy: 0, onGround: true, facing: 1 }; let yaw = Math.sin(t * 0.5) * 0.18; let lift = 0; if (mode === 'turn') yaw = time * 1.2; if (mode === 'run') motion = { vx: 6.5, vy: 0, onGround: true, facing: 1 }; if (mode === 'jump') { const u = (t * 0.9) % 1; lift = Math.sin(u * Math.PI) * 1.1; motion = { vx: 0, vy: Math.cos(u * Math.PI) * 9, onGround: u < 0.04 || u > 0.96, facing: 1 }; } if (fire && (mode === 'attack' || mode === 'victory' || mode === 'wave')) it.rig.play(mode as ChibiAnim); it.rig.root.rotation.y = yaw; it.rig.root.position.y = it.base + SIZE * 0.5 + 0.02 + lift; it.rig.update(dt, motion); } gfx.update(dt); placeCamera(); for (const it of items) { const [sx, sy] = toScreen(it.x, it.base - 0.22, 1.2); it.el.style.left = `${sx}px`; it.el.style.top = `${sy}px`; } for (const r of rowTags) r.el.style.top = `${toScreen(0, r.y)[1]}px`; gfx.draw(); } function loop(now: number): void { requestAnimationFrame(loop); const dt = Math.min(0.05, (now - last) / 1000); last = now; frame(dt); } requestAnimationFrame(loop); (window as any).__preview = { frame, setMode, gfx, items, view: (x: number, y: number, dist: number) => { override = dist > 0 ? { x, y, dist } : null; } };