// Princess picture quests: each princess shows a picture bubble with one small wish. // No words needed, no clock, cannot fail. Walking away never cancels a wish. import * as THREE from 'three'; import { clamp } from '../core/util'; export type QuestEvent = 'candy' | 'bounce' | 'hatch' | 'catch' | 'jump'; interface Wish { icon: string; need: number; done: (s: QuestState) => boolean; progress?: (s: QuestState) => number; reward: number; gift: string } export interface QuestState { counts: Record; friends: number; nearCupcake: boolean; nearRex: boolean; atPrincess: boolean } // "need" counts are measured from the moment the wish starts const WISHES: Wish[] = [ { icon: '🍬', need: 5, done: s => s.counts.candy >= 5, progress: s => s.counts.candy, reward: 8, gift: '🍭' }, { icon: '🐱', need: 1, done: s => s.friends >= 1 && s.atPrincess, reward: 6, gift: '💖' }, { icon: '🤸', need: 3, done: s => s.counts.bounce >= 3, progress: s => s.counts.bounce, reward: 8, gift: '⭐' }, { icon: '🧁', need: 1, done: s => s.nearCupcake, reward: 6, gift: '🧁' }, { icon: '🦖', need: 1, done: s => s.nearRex, reward: 6, gift: '🥚' }, { icon: '🐾', need: 2, done: s => s.counts.catch >= 2, progress: s => s.counts.catch, reward: 10, gift: '🎀' }, { icon: '🦘', need: 10, done: s => s.counts.jump >= 10, progress: s => s.counts.jump, reward: 6, gift: '🌈' }, ]; export interface Asker { root: THREE.Object3D; cheer: number } class PrincessWish { bubble: HTMLElement; wish: Wish; counts: Record = { candy: 0, bounce: 0, hatch: 0, catch: 0, jump: 0 }; doneT = 0; delivered = false; constructor(public who: Asker, ui: HTMLElement, public order: number[], public i = 0) { this.bubble = document.createElement('div'); this.bubble.className = 'wish'; ui.appendChild(this.bubble); this.wish = WISHES[order[0]]; this.render(); } render() { if (this.delivered) { this.bubble.innerHTML = `💖`; return; } const w = this.wish, p = w.progress ? Math.min(w.need, w.progress({ counts: this.counts } as QuestState)) : 0; this.bubble.innerHTML = `${w.icon}${w.need > 1 ? `${'●'.repeat(p)}${'○'.repeat(w.need - p)}` : ''}`; } next() { this.i = (this.i + 1) % this.order.length; this.wish = WISHES[this.order[this.i]]; this.counts = { candy: 0, bounce: 0, hatch: 0, catch: 0, jump: 0 }; this.delivered = false; this.render(); } } export class Quests { private wishes: PrincessWish[] = []; private v = new THREE.Vector3(); visible = true; constructor(ui: HTMLElement, askers: Asker[]) { const orders = [[0, 1, 3, 6], [2, 4, 5, 1]]; askers.forEach((a, k) => this.wishes.push(new PrincessWish(a, ui, orders[k % orders.length]))); } hideAll() { for (const w of this.wishes) w.bubble.hidden = true; } notify(e: QuestEvent) { for (const w of this.wishes) if (!w.delivered) { w.counts[e]++; if (w.wish.progress) w.render(); } } reset() { for (const w of this.wishes) { w.counts = { candy: 0, bounce: 0, hatch: 0, catch: 0, jump: 0 }; w.delivered = false; w.render(); } } /** Returns a finished wish (to pay its reward) when the player brings it to the princess. */ update(dt: number, camera: THREE.Camera, playerPos: THREE.Vector3, ctx: { friends: number; nearCupcake: boolean; nearRex: boolean }, show: boolean): { gift: string; reward: number; who: Asker } | null { let out: { gift: string; reward: number; who: Asker } | null = null; for (const w of this.wishes) { const pr = w.who.root.position; const atPrincess = Math.hypot(pr.x - playerPos.x, pr.z - playerPos.z) < 3 && Math.abs(pr.y - playerPos.y) < 3; const st: QuestState = { counts: w.counts, friends: ctx.friends, nearCupcake: ctx.nearCupcake, nearRex: ctx.nearRex, atPrincess }; if (!w.delivered && w.wish.done(st)) { // wishes that happen far away are "ready": the bubble glows until she visits the princess w.bubble.classList.add('ready'); if (atPrincess) { w.delivered = true; w.doneT = 5; w.bubble.classList.remove('ready'); w.render(); out = { gift: w.wish.gift, reward: w.wish.reward, who: w.who }; } } if (w.delivered) { w.doneT -= dt; if (w.doneT <= 0) w.next(); } // place the bubble over the princess's head this.v.copy(pr).setY(pr.y + 2.6).project(camera); const on = show && this.visible && this.v.z < 1 && Math.abs(this.v.x) < 1.1 && Math.abs(this.v.y) < 1.1; w.bubble.hidden = !on; // keep the bubble fully on screen and below the top bar if (on) { const x = clamp((this.v.x + 1) / 2 * innerWidth, 40, innerWidth - 40), y = clamp((1 - this.v.y) / 2 * innerHeight, 150, innerHeight - 20); w.bubble.style.transform = `translate(${x.toFixed(0)}px, ${y.toFixed(0)}px) translate(-50%, -100%)`; } } return out; } }