[ui] PlanetGarden: Her round planet, the player's gardener, the ring of care, and care by gesture
This commit is contained in:
@@ -1,9 +1,13 @@
|
||||
<script lang="ts">
|
||||
import {
|
||||
careEvent, goodMoment, localDay, newCare, newOpened, petalAt, pour, roseAt, ROSE, sentenceOpened, touch, TOUCH, UNTOUCHED,
|
||||
type Mood, type PetalNow, type Place, type RoseNow, type Stage, type TouchState
|
||||
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, SentenceOpened } from '@rosea/schema';
|
||||
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';
|
||||
@@ -23,7 +27,13 @@
|
||||
* 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 }: { wiring: RoseWiring; timeZone: string; place?: Place | null } = $props();
|
||||
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 };
|
||||
@@ -114,14 +124,17 @@
|
||||
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(): void {
|
||||
function startPour(tipped = 0.55): void {
|
||||
if (pouring || rose === null) return;
|
||||
pouring = true;
|
||||
amount = 0;
|
||||
pourTimer = setInterval(() => (amount = Math.min(1, amount + TICK_MS / FULL_CAN_MS)), TICK_MS);
|
||||
tilt = tipped;
|
||||
pourTimer = setInterval(() => (amount = Math.min(1, amount + (TICK_MS / FULL_CAN_MS) * pourRate(tilt))), TICK_MS);
|
||||
}
|
||||
function stopPour(): void {
|
||||
if (!pouring) return;
|
||||
@@ -157,6 +170,33 @@
|
||||
|
||||
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 '';
|
||||
@@ -185,27 +225,46 @@
|
||||
|
||||
{#if rose}
|
||||
<section class="rose">
|
||||
<figure>
|
||||
<div class="picture">
|
||||
<img src={picture} {alt} width="120" height="320" class:turned={rose.drift > 0} />
|
||||
<!-- Her picture keeps its description; the touch is its own button over it (thorns.md T5). -->
|
||||
<button
|
||||
type="button"
|
||||
class="touch"
|
||||
aria-label={en.rose.touch.label}
|
||||
onclick={touchHer}
|
||||
onkeydown={(e) => {
|
||||
// A held key is one touch, not a rush.
|
||||
if (e.repeat) e.preventDefault();
|
||||
}}
|
||||
></button>
|
||||
</div>
|
||||
{#if says}
|
||||
<figcaption class="bubble" aria-live="polite">{says}</figcaption>
|
||||
{/if}
|
||||
</figure>
|
||||
{#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')}
|
||||
/>
|
||||
|
||||
<div class="care" role="group" aria-label={en.rose.care}>
|
||||
{#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
|
||||
type="button"
|
||||
class="water"
|
||||
@@ -229,16 +288,29 @@
|
||||
onblur={stopPour}
|
||||
>{en.rose.water}</button>
|
||||
{#if rose.needs.wind}
|
||||
<button type="button" onclick={() => care('shade')}>{en.rose.shade}</button>
|
||||
<button type="button" onclick={() => careBy('shade')}>{en.rose.shade}</button>
|
||||
{/if}
|
||||
{#if rose.needs.weeds}
|
||||
<button type="button" onclick={() => care('weed')}>{en.rose.weed}</button>
|
||||
<button type="button" onclick={() => careBy('weed')}>{en.rose.weed}</button>
|
||||
{/if}
|
||||
{#if rose.needs.petals}
|
||||
<button type="button" onclick={() => care('petals')}>{en.rose.petals}</button>
|
||||
<button type="button" onclick={() => careBy('petals')}>{en.rose.petals}</button>
|
||||
{/if}
|
||||
<button
|
||||
type="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 type="button" class="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">
|
||||
@@ -256,6 +328,9 @@
|
||||
<div class="more">
|
||||
<button type="button" class="quiet" aria-expanded={showNumbers} aria-controls="rose-numbers" onclick={() => (showNumbers = !showNumbers)}>{en.rose.numbers}</button>
|
||||
<button type="button" class="quiet" aria-expanded={showSafe} onclick={() => (showSafe = !showSafe)}>{en.rose.safe.title}</button>
|
||||
{#if onchangegardener}
|
||||
<button type="button" class="quiet" onclick={onchangegardener}>{en.gardener.change}</button>
|
||||
{/if}
|
||||
</div>
|
||||
{#if showNumbers}
|
||||
<dl id="rose-numbers" class="numbers">
|
||||
@@ -278,39 +353,9 @@
|
||||
gap: var(--space-2);
|
||||
}
|
||||
|
||||
figure {
|
||||
margin: 0;
|
||||
display: grid;
|
||||
justify-items: center;
|
||||
gap: var(--space-2);
|
||||
}
|
||||
|
||||
img {
|
||||
height: 13rem;
|
||||
width: auto;
|
||||
transition: transform 0.6s ease;
|
||||
}
|
||||
|
||||
.picture {
|
||||
position: relative;
|
||||
}
|
||||
|
||||
.touch {
|
||||
position: absolute;
|
||||
inset: 0;
|
||||
border: none;
|
||||
border-radius: 1rem;
|
||||
background: transparent;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
/* Drift is drawn as waiting: turned a little away, never hurt (CLAUDE.md §8). */
|
||||
img.turned {
|
||||
transform: rotate(-4deg) scaleX(-1);
|
||||
}
|
||||
|
||||
.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));
|
||||
|
||||
Reference in New Issue
Block a user