421 lines
15 KiB
Svelte
421 lines
15 KiB
Svelte
<script lang="ts">
|
|
import { Button } from '@domi/ui';
|
|
import {
|
|
careEvent, goodMoment, lightAt, localDay, newCare, newOpened, petalAt, petalsToday, pour, roseAt, ROSE, sentenceOpened, touch, TOUCH,
|
|
UNTOUCHED, weedsToday, type Mood, type PetalNow, type Place, type RoseNow, type Stage, type TouchState
|
|
} from '@rosea/engine';
|
|
import type { CareEvent, Gardener as Look, SentenceOpened } from '@rosea/schema';
|
|
import { tick } from 'svelte';
|
|
import Planet from '../scene/Planet.svelte';
|
|
import { pourRate } from '../scene/gestures.js';
|
|
import { FIRST_LOOK } from '../scene/look.js';
|
|
import { en } from '../copy/en.js';
|
|
import KeepSafe from './KeepSafe.svelte';
|
|
import Petal from './Petal.svelte';
|
|
import type { RoseWiring } from './types.js';
|
|
// The art board's stand-ins until real art is accepted (rose.md R10, docs/ART.md).
|
|
import bud from '../../../../art/placeholders/rose-bud.svg?url';
|
|
import opening from '../../../../art/placeholders/rose-opening.svg?url';
|
|
import halfBloom from '../../../../art/placeholders/rose-half-bloom.svg?url';
|
|
import fullBloom from '../../../../art/placeholders/rose-full-bloom.svg?url';
|
|
import thirsty from '../../../../art/placeholders/rose-thirsty.svg?url';
|
|
import overwatered from '../../../../art/placeholders/rose-overwatered.svg?url';
|
|
import windblown from '../../../../art/placeholders/rose-windblown.svg?url';
|
|
import blushing from '../../../../art/placeholders/rose-blushing.svg?url';
|
|
import waiting from '../../../../art/placeholders/rose-waiting.svg?url';
|
|
|
|
/**
|
|
* The rose. Everything she is comes from the engine's replay of her care log
|
|
* (rose.md); this renders it and turns the player's hands into care events.
|
|
*/
|
|
let {
|
|
wiring,
|
|
timeZone,
|
|
place = null,
|
|
look = FIRST_LOOK,
|
|
onchangegardener
|
|
}: { wiring: RoseWiring; timeZone: string; place?: Place | null; look?: Look; onchangegardener?: () => void } = $props();
|
|
|
|
const STAGE_PICTURE: Record<Stage, string> = { bud, opening, 'half-bloom': halfBloom, 'full-bloom': fullBloom };
|
|
const MOOD_PICTURE: Record<Mood, string> = { content: fullBloom, thirsty, 'too-full': overwatered, windblown, blushing, waiting };
|
|
|
|
let seed = $state<string | null>(null);
|
|
/** Keys she was named by on another device (keep-her-safe.md K2). */
|
|
let pastKeys = $state.raw<readonly string[]>([]);
|
|
let showSafe = $state(false);
|
|
let events = $state.raw<readonly CareEvent[]>([]);
|
|
/** Her opened petals (day-sentence.md). */
|
|
let opened = $state.raw<readonly SentenceOpened[]>([]);
|
|
let now = $state(0);
|
|
let kept = $state(true);
|
|
let showNumbers = $state(false);
|
|
/** The petal was opened at the good moment in this visit: she sends the player out to the sky. */
|
|
let lookUp = $state(false);
|
|
|
|
$effect(() => {
|
|
const w = wiring;
|
|
now = w.now();
|
|
Promise.all([w.identity(), w.loadCare(), w.pastKeys(), w.loadOpened()]).then(
|
|
([id, log, past, petals]) => {
|
|
seed = id.publicKey;
|
|
events = log;
|
|
pastKeys = past;
|
|
opened = petals;
|
|
if (!id.kept) kept = false;
|
|
},
|
|
(error: unknown) => {
|
|
console.error('rose: could not read her care log', error);
|
|
kept = false;
|
|
}
|
|
);
|
|
// Wind comes and goes, and a blush fades: look again now and then.
|
|
const timer = setInterval(() => (now = w.now()), 30_000);
|
|
return () => clearInterval(timer);
|
|
});
|
|
|
|
const rose = $derived<RoseNow | null>(seed === null ? null : roseAt(events, [seed, ...pastKeys], now, timeZone));
|
|
const petal = $derived<PetalNow>(seed === null ? { kind: 'none' } : petalAt(wiring.sentences, opened, [seed, ...pastKeys], now, timeZone));
|
|
const sunIsLow = $derived(place !== null && now > 0 && goodMoment(now, place));
|
|
|
|
function openPetal(): void {
|
|
if (seed === null || petal.kind !== 'folded') return;
|
|
now = wiring.now();
|
|
const event = sentenceOpened({ now, timeZone, from: seed, messageId: petal.id, clientVersion: wiring.clientVersion });
|
|
lookUp = place !== null && goodMoment(now, place);
|
|
opened = [...opened, event];
|
|
wiring.appendOpened(event).catch((error: unknown) => {
|
|
console.error('rose: the device could not keep the opened petal', error);
|
|
kept = false;
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Read her log and keys again: after she is brought back from a file, or when another
|
|
* tab wrote (two-tabs.md). What this tab already shows is kept: the log only grows,
|
|
* and this tab's own write may still be on its way when the read finishes.
|
|
*/
|
|
function reload(): void {
|
|
Promise.all([wiring.loadCare(), wiring.pastKeys(), wiring.loadOpened()]).then(
|
|
([log, past, petals]) => {
|
|
events = [...log, ...newCare(log, events)];
|
|
opened = [...petals, ...newOpened(petals, opened)];
|
|
pastKeys = [...new Set([...past, ...pastKeys])];
|
|
now = wiring.now();
|
|
},
|
|
(error: unknown) => console.error('rose: could not read her log again', error)
|
|
);
|
|
}
|
|
|
|
$effect(() => wiring.changes?.(reload));
|
|
|
|
function care(act: CareEvent['act'], amount?: number): void {
|
|
if (seed === null) return;
|
|
now = wiring.now();
|
|
const event = careEvent({ now, timeZone, from: seed, to: seed, act, clientVersion: wiring.clientVersion, ...(amount === undefined ? {} : { amount }) });
|
|
events = [...events, event];
|
|
wiring.appendCare(event).catch((error: unknown) => {
|
|
console.error('rose: the device could not keep this care', error);
|
|
kept = false;
|
|
});
|
|
}
|
|
|
|
// --- Water by feel: hold to pour (GAMEPLAY §1) ---------------------------
|
|
/** A full can in this long, held. */
|
|
const FULL_CAN_MS = 3000;
|
|
const TICK_MS = 50;
|
|
let amount = $state(0);
|
|
let pouring = $state(false);
|
|
/** How far the can is tipped (planet-garden.md G4): a held button pours at a steady 0.55. */
|
|
let tilt = $state(0.55);
|
|
let pourTimer: ReturnType<typeof setInterval> | undefined;
|
|
const can = $derived(rose ? pour(rose.water, amount) : null);
|
|
|
|
function startPour(tipped = 0.55): void {
|
|
if (pouring || rose === null) return;
|
|
pouring = true;
|
|
amount = 0;
|
|
tilt = tipped;
|
|
pourTimer = setInterval(() => (amount = Math.min(1, amount + (TICK_MS / FULL_CAN_MS) * pourRate(tilt))), TICK_MS);
|
|
}
|
|
function stopPour(): void {
|
|
if (!pouring) return;
|
|
clearInterval(pourTimer);
|
|
pouring = false;
|
|
if (amount > 0.01) care('water', amount);
|
|
amount = 0;
|
|
}
|
|
$effect(() => () => clearInterval(pourTimer));
|
|
|
|
// --- Touching her (thorns.md): nothing is recorded ----------------------
|
|
let touchState = $state.raw<TouchState>(UNTOUCHED);
|
|
let touchLine = $state('');
|
|
let touchBlush = $state(false);
|
|
let fondSaid = 0;
|
|
let touchTimer: ReturnType<typeof setTimeout> | undefined;
|
|
|
|
function touchHer(): void {
|
|
const { reply, state } = touch(touchState, wiring.now());
|
|
touchState = state;
|
|
if (reply.kind === 'nothing') return;
|
|
const fond = en.rose.touch.fond;
|
|
touchLine = reply.kind === 'thorns' ? en.rose.touch.thorns : (fond[fondSaid++ % fond.length] ?? '');
|
|
if (reply.kind === 'fond' && reply.blush) touchBlush = true;
|
|
clearTimeout(touchTimer);
|
|
// Her words hold the bubble while she would rest, then her usual line returns (T6).
|
|
touchTimer = setTimeout(() => {
|
|
touchLine = '';
|
|
touchBlush = false;
|
|
}, TOUCH.restMs);
|
|
}
|
|
$effect(() => () => clearTimeout(touchTimer));
|
|
|
|
const isPourKey = (e: KeyboardEvent): boolean => e.key === ' ' || e.key === 'Enter';
|
|
|
|
// --- Her planet and the ring of care (planet-garden.md) ------------------
|
|
let planet = $state<Planet>();
|
|
let roseTarget = $state<SVGElement>();
|
|
let ringOpen = $state(false);
|
|
let ring = $state<HTMLElement>();
|
|
const today = $derived(localDay(now, timeZone));
|
|
const weedCount = $derived(rose?.needs.weeds && seed ? weedsToday(seed, today) : 0);
|
|
const petalCount = $derived(rose?.needs.petals && seed ? petalsToday(seed, today) : 0);
|
|
const light = $derived(place && now > 0 ? lightAt(now, place) : 'day');
|
|
|
|
async function openRing(): Promise<void> {
|
|
ringOpen = !ringOpen;
|
|
await tick();
|
|
if (ringOpen) ring?.querySelector<HTMLElement>('button')?.focus();
|
|
else roseTarget?.focus();
|
|
}
|
|
async function closeRing(): Promise<void> {
|
|
ringOpen = false;
|
|
await tick();
|
|
roseTarget?.focus();
|
|
}
|
|
/** A ring button: the gardener does it, and it is the same act as ever. */
|
|
function careBy(act: 'shade' | 'weed' | 'petals'): void {
|
|
planet?.act(act === 'weed' ? 'weeds' : act === 'shade' ? 'shade' : 'petals');
|
|
care(act);
|
|
}
|
|
|
|
// --- What she says: one line, or nothing --------------------------------
|
|
const says = $derived.by(() => {
|
|
if (!rose) return '';
|
|
if (touchLine) return touchLine;
|
|
const s = en.rose.says;
|
|
if (rose.mood === 'blushing') return s.thanks;
|
|
if (rose.mood === 'too-full') return s.tooFull;
|
|
const need = rose.needs.wind ? s.wind
|
|
: rose.water < ROSE.thirsty ? s.thirsty
|
|
: rose.needs.water ? s.dry
|
|
: rose.needs.weeds ? s.weeds
|
|
: rose.needs.petals ? s.petals
|
|
: '';
|
|
// The petal only when she needs nothing else: one line at a time (day-sentence.md §1).
|
|
const p = en.rose.petal;
|
|
const offer = need !== '' ? '' : petal.kind === 'folded' ? (sunIsLow ? p.goodMoment : p.waiting) : petal.kind === 'open' && lookUp ? p.lookUp : '';
|
|
return [rose.drift > 0 ? s.back : '', need || offer].filter(Boolean).join(' ');
|
|
});
|
|
|
|
// A touch's blush shows as her mood does: on her face in full bloom (thorns.md T4).
|
|
const mood = $derived<Mood | null>(rose ? (touchBlush ? 'blushing' : rose.mood) : null);
|
|
const picture = $derived(rose && mood ? (rose.stage === 'full-bloom' ? MOOD_PICTURE[mood] : STAGE_PICTURE[rose.stage]) : bud);
|
|
const alt = $derived(rose && mood ? en.rose.picture(en.rose.stageWords[rose.stage], en.rose.moodWords[mood]) : '');
|
|
const percent = (x: number): string => `${Math.round(Math.max(0, Math.min(1, x)) * 100)} %`;
|
|
</script>
|
|
|
|
{#if rose}
|
|
<section class="rose">
|
|
{#if says}
|
|
<p class="bubble" aria-live="polite">{says}</p>
|
|
{/if}
|
|
<!-- Her planet: the gardener, and every care by the player's hand (planet-garden.md). -->
|
|
<Planet
|
|
bind:this={planet}
|
|
bind:roseTarget
|
|
{picture}
|
|
{alt}
|
|
turned={rose.drift > 0}
|
|
{light}
|
|
{look}
|
|
weeds={weedCount}
|
|
petals={petalCount}
|
|
wind={rose.needs.wind}
|
|
{pouring}
|
|
{tilt}
|
|
{ringOpen}
|
|
onrose={openRing}
|
|
onstroke={touchHer}
|
|
onpourstart={() => startPour(0)}
|
|
onpourtilt={(t) => (tilt = t)}
|
|
onpourend={stopPour}
|
|
onweeds={() => care('weed')}
|
|
onpetals={() => care('petals')}
|
|
onshade={() => care('shade')}
|
|
/>
|
|
|
|
{#if ringOpen}
|
|
<!-- svelte-ignore a11y_no_noninteractive_element_interactions (Escape anywhere in the ring closes it) -->
|
|
<div
|
|
class="care"
|
|
role="group"
|
|
aria-label={en.scene.ring}
|
|
bind:this={ring}
|
|
tabindex="-1"
|
|
onkeydown={(e) => {
|
|
if (e.key === 'Escape') void closeRing();
|
|
}}
|
|
>
|
|
<!-- Button runs a passed onkeydown and onblur before its own since @domi/ui 1.4.0 (the fix from here, domi-ui.md
|
|
D3): the pour needs both. -->
|
|
<Button
|
|
class={pouring ? 'water pouring' : 'water'}
|
|
aria-describedby="rose-water-hint"
|
|
onpointerdown={(e) => {
|
|
e.preventDefault();
|
|
(e.currentTarget as HTMLElement).setPointerCapture?.(e.pointerId);
|
|
startPour();
|
|
}}
|
|
onpointerup={stopPour}
|
|
onpointercancel={stopPour}
|
|
onkeydown={(e) => {
|
|
if (!isPourKey(e)) return;
|
|
e.preventDefault();
|
|
if (!e.repeat) startPour();
|
|
}}
|
|
onkeyup={(e) => {
|
|
if (isPourKey(e)) stopPour();
|
|
}}
|
|
onblur={stopPour}
|
|
>{en.rose.water}</Button>
|
|
{#if rose.needs.wind}
|
|
<Button onclick={() => careBy('shade')}>{en.rose.shade}</Button>
|
|
{/if}
|
|
{#if rose.needs.weeds}
|
|
<Button onclick={() => careBy('weed')}>{en.rose.weed}</Button>
|
|
{/if}
|
|
{#if rose.needs.petals}
|
|
<Button onclick={() => careBy('petals')}>{en.rose.petals}</Button>
|
|
{/if}
|
|
<Button
|
|
onclick={() => {
|
|
planet?.act('touch');
|
|
touchHer();
|
|
}}
|
|
onkeydown={(e) => {
|
|
// A held key is one touch, not a rush (thorns.md T2).
|
|
if (e.repeat) e.preventDefault();
|
|
}}
|
|
>{en.rose.touch.label}</Button>
|
|
<Button variant="quiet" onclick={() => void closeRing()}>{en.scene.close}</Button>
|
|
</div>
|
|
<p id="rose-water-hint" class="hint">{en.rose.waterHint}</p>
|
|
{/if}
|
|
|
|
{#if pouring && can}
|
|
<div class="can">
|
|
<meter min="0" max="1.2" low={ROSE.enough} high={ROSE.tooFull} optimum="0.7" value={Math.min(1.2, can.after)} aria-label={en.rose.waterLevel}></meter>
|
|
</div>
|
|
{/if}
|
|
<p class="said" role="status" aria-live="assertive">{pouring && can ? (can.tooFull ? en.rose.plenty : can.leavesLift ? en.rose.leavesLift : '') : ''}</p>
|
|
|
|
<Petal {petal} sentences={wiring.sentences} onopen={openPetal} />
|
|
|
|
{#if !kept}
|
|
<p class="hint">{en.rose.notKept}</p>
|
|
{/if}
|
|
|
|
<div class="more">
|
|
<Button variant="quiet" aria-expanded={showNumbers} aria-controls="rose-numbers" onclick={() => (showNumbers = !showNumbers)}>{en.rose.numbers}</Button>
|
|
<Button variant="quiet" aria-expanded={showSafe} onclick={() => (showSafe = !showSafe)}>{en.rose.safe.title}</Button>
|
|
{#if onchangegardener}
|
|
<Button variant="quiet" onclick={onchangegardener}>{en.gardener.change}</Button>
|
|
{/if}
|
|
</div>
|
|
{#if showNumbers}
|
|
<dl id="rose-numbers" class="numbers">
|
|
<dt>{en.rose.waterLevel}</dt><dd>{percent(rose.water)}</dd>
|
|
<dt>{en.rose.daysOfCare}</dt><dd>{rose.daysOfCare}</dd>
|
|
<dt>{en.rose.warmth}</dt><dd>{en.rose.warmthDays(rose.warmth)}</dd>
|
|
<dt>{en.rose.trend}</dt><dd>{en.rose.trendWords[rose.trend]}</dd>
|
|
</dl>
|
|
{/if}
|
|
{#if showSafe && seed}
|
|
<KeepSafe {wiring} currentKey={seed} today={localDay(now, timeZone)} onrestored={reload} />
|
|
{/if}
|
|
</section>
|
|
{/if}
|
|
|
|
<style>
|
|
.rose {
|
|
display: grid;
|
|
justify-items: center;
|
|
gap: var(--space-2);
|
|
}
|
|
|
|
.bubble {
|
|
max-width: 22rem;
|
|
margin: 0;
|
|
padding: var(--space-2) var(--space-3);
|
|
border-radius: 1rem;
|
|
background: color-mix(in srgb, var(--rose) 12%, var(--paper));
|
|
}
|
|
|
|
.care {
|
|
display: flex;
|
|
flex-wrap: wrap;
|
|
justify-content: center;
|
|
gap: var(--space-2);
|
|
}
|
|
|
|
/* Water is held, not clicked: the page must not scroll or select under the finger (rose.md). */
|
|
.care :global(.water) {
|
|
touch-action: none;
|
|
user-select: none;
|
|
}
|
|
|
|
/* While she is being watered the plate stays pressed in (@domi/ui's own press, held). */
|
|
.care :global(.water.pouring) {
|
|
background: var(--domi-plate-press);
|
|
box-shadow: var(--domi-plate-shadow-press);
|
|
}
|
|
|
|
.hint,
|
|
.said {
|
|
margin: 0;
|
|
color: var(--ink-soft);
|
|
font-size: 0.9rem;
|
|
}
|
|
|
|
.said:empty {
|
|
display: none;
|
|
}
|
|
|
|
meter {
|
|
width: 12rem;
|
|
}
|
|
|
|
/* The shared buttons keep their words on one line, so the row wraps instead (a phone is 360 px wide). */
|
|
.more {
|
|
display: flex;
|
|
flex-wrap: wrap;
|
|
justify-content: center;
|
|
gap: var(--space-1) var(--space-3);
|
|
}
|
|
|
|
.numbers {
|
|
display: grid;
|
|
grid-template-columns: auto auto;
|
|
gap: var(--space-1) var(--space-3);
|
|
margin: 0;
|
|
}
|
|
|
|
dt {
|
|
color: var(--ink-soft);
|
|
}
|
|
|
|
dd {
|
|
margin: 0;
|
|
}
|
|
</style>
|