create-ifloom 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +18 -2
- package/bin.js +4 -0
- package/dist/bin.js +391 -0
- package/dist/bin.js.map +1 -0
- package/dist/examples/cafe/index.html +12 -0
- package/dist/examples/cafe/package.json +24 -0
- package/dist/examples/cafe/src/game.ts +41 -0
- package/dist/examples/cafe/src/layout.tsx +49 -0
- package/dist/examples/cafe/src/main.ts +7 -0
- package/dist/examples/cafe/src/rules.ts +38 -0
- package/dist/examples/cafe/src/story/hiring.ts +25 -0
- package/dist/examples/cafe/src/story/opening.ts +19 -0
- package/dist/examples/cafe/src/styles.css +7 -0
- package/dist/examples/cafe/src/world/actions.ts +40 -0
- package/dist/examples/cafe/src/world/menu.ts +14 -0
- package/dist/examples/cafe/src/world/staff.ts +12 -0
- package/dist/examples/cafe/src/world/stats.ts +10 -0
- package/dist/examples/cafe/src/world/triggers.ts +18 -0
- package/dist/examples/cafe/tsconfig.json +9 -0
- package/dist/examples/cafe/vite.config.ts +7 -0
- package/dist/examples/novel/index.html +12 -0
- package/dist/examples/novel/package.json +24 -0
- package/dist/examples/novel/src/game.ts +49 -0
- package/dist/examples/novel/src/layout.tsx +25 -0
- package/dist/examples/novel/src/main.ts +8 -0
- package/dist/examples/novel/src/story/ch1.ts +36 -0
- package/dist/examples/novel/src/story/ch2.ts +42 -0
- package/dist/examples/novel/src/story/ch3.ts +41 -0
- package/dist/examples/novel/src/styles.css +7 -0
- package/dist/examples/novel/src/world/stats.ts +6 -0
- package/dist/examples/novel/tsconfig.json +9 -0
- package/dist/examples/novel/vite.config.ts +7 -0
- package/dist/examples/showcase/.env +4 -0
- package/dist/examples/showcase/index.html +12 -0
- package/dist/examples/showcase/package.json +43 -0
- package/dist/examples/showcase/public/art/character/ava.svg +1 -0
- package/dist/examples/showcase/public/art/character/sam.svg +1 -0
- package/dist/examples/showcase/public/art/photo/ava.svg +1 -0
- package/dist/examples/showcase/public/art/photo/sam.svg +1 -0
- package/dist/examples/showcase/src/game.ts +168 -0
- package/dist/examples/showcase/src/layout.tsx +84 -0
- package/dist/examples/showcase/src/main.tsx +55 -0
- package/dist/examples/showcase/src/place.ts +8 -0
- package/dist/examples/showcase/src/rules.ts +51 -0
- package/dist/examples/showcase/src/story/ava.prose.ts +12 -0
- package/dist/examples/showcase/src/story/ava.ts +199 -0
- package/dist/examples/showcase/src/story/cafe.ts +157 -0
- package/dist/examples/showcase/src/story/flat.ts +90 -0
- package/dist/examples/showcase/src/story/high-street.ts +37 -0
- package/dist/examples/showcase/src/story/moving-day.ts +74 -0
- package/dist/examples/showcase/src/story/office.ts +26 -0
- package/dist/examples/showcase/src/story/park.ts +172 -0
- package/dist/examples/showcase/src/story/sam.ts +27 -0
- package/dist/examples/showcase/src/styles.css +8 -0
- package/dist/examples/showcase/src/ui.ts +44 -0
- package/dist/examples/showcase/src/unlocks/supporter.ts +40 -0
- package/dist/examples/showcase/src/world/achievements.ts +13 -0
- package/dist/examples/showcase/src/world/actions.ts +13 -0
- package/dist/examples/showcase/src/world/cast.ts +46 -0
- package/dist/examples/showcase/src/world/cheats.ts +33 -0
- package/dist/examples/showcase/src/world/phone.ts +88 -0
- package/dist/examples/showcase/src/world/settings.ts +24 -0
- package/dist/examples/showcase/src/world/stats.ts +24 -0
- package/dist/examples/showcase/src/world/strings.ts +7 -0
- package/dist/examples/showcase/src/world/things.ts +59 -0
- package/dist/examples/showcase/tsconfig.json +9 -0
- package/dist/examples/showcase/vite.config.ts +7 -0
- package/dist/templates/next/AGENTS.md +60 -0
- package/dist/templates/next/README.md +33 -0
- package/dist/templates/next/app/Game.tsx +46 -0
- package/dist/templates/next/app/layout.tsx +13 -0
- package/dist/templates/next/app/page.tsx +6 -0
- package/dist/templates/next/gitignore +6 -0
- package/dist/templates/next/next.config.ts +6 -0
- package/dist/templates/next/package.json +32 -0
- package/dist/templates/next/postcss.config.mjs +6 -0
- package/dist/templates/next/src/layout.tsx +73 -0
- package/dist/templates/next/src/styles.css +16 -0
- package/dist/templates/next/tsconfig.json +21 -0
- package/dist/templates/plugin/README.md +81 -0
- package/dist/templates/plugin/gitignore +2 -0
- package/dist/templates/plugin/package.json +64 -0
- package/dist/templates/plugin/src/index.ts +162 -0
- package/dist/templates/plugin/src/react/index.tsx +95 -0
- package/dist/templates/plugin/tests/contracts.test.ts +28 -0
- package/dist/templates/plugin/tests/plugin.test.ts +59 -0
- package/dist/templates/plugin/tests/react.test.tsx +47 -0
- package/dist/templates/plugin/tsconfig.json +21 -0
- package/dist/templates/plugin/tsdown.config.ts +14 -0
- package/dist/templates/react/package.json +28 -0
- package/dist/templates/react/src/layout.tsx +69 -0
- package/dist/templates/react/src/main.ts +11 -0
- package/dist/templates/react/src/styles.css +16 -0
- package/dist/templates/react/vite.config.ts +7 -0
- package/dist/templates/shared/AGENTS.md +58 -0
- package/dist/templates/shared/gitignore +3 -0
- package/dist/templates/shared/index.html +12 -0
- package/dist/templates/shared/src/game.ts +23 -0
- package/dist/templates/shared/src/story/opening.ts +34 -0
- package/dist/templates/shared/src/styles.css +228 -0
- package/dist/templates/shared/src/world/stats.ts +6 -0
- package/dist/templates/shared/tsconfig.json +9 -0
- package/dist/templates/shared/vite.config.ts +5 -0
- package/dist/templates/svelte/package.json +25 -0
- package/dist/templates/svelte/src/App.svelte +11 -0
- package/dist/templates/svelte/src/Beat.svelte +31 -0
- package/dist/templates/svelte/src/InputField.svelte +36 -0
- package/dist/templates/svelte/src/Layout.svelte +84 -0
- package/dist/templates/svelte/src/Line.svelte +26 -0
- package/dist/templates/svelte/src/Minigame.svelte +21 -0
- package/dist/templates/svelte/src/Panel.svelte +27 -0
- package/dist/templates/svelte/src/main.ts +5 -0
- package/dist/templates/svelte/src/runner.ts +29 -0
- package/dist/templates/svelte/vite.config.ts +5 -0
- package/dist/templates/vanilla/package.json +22 -0
- package/dist/templates/vanilla/src/layout.ts +35 -0
- package/dist/templates/vanilla/src/main.ts +253 -0
- package/dist/templates/vue/package.json +25 -0
- package/dist/templates/vue/src/App.vue +12 -0
- package/dist/templates/vue/src/Beat.vue +28 -0
- package/dist/templates/vue/src/InputField.vue +34 -0
- package/dist/templates/vue/src/Layout.vue +84 -0
- package/dist/templates/vue/src/Line.vue +41 -0
- package/dist/templates/vue/src/Minigame.vue +19 -0
- package/dist/templates/vue/src/Panel.vue +30 -0
- package/dist/templates/vue/src/main.ts +5 -0
- package/dist/templates/vue/src/runner.ts +32 -0
- package/dist/templates/vue/vite.config.ts +5 -0
- package/package.json +48 -4
- package/src/bin.ts +18 -0
- package/src/create.ts +89 -0
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import { runner, view } from './runner.ts'
|
|
3
|
+
import Beat from './Beat.svelte'
|
|
4
|
+
import InputField from './InputField.svelte'
|
|
5
|
+
import Minigame from './Minigame.svelte'
|
|
6
|
+
|
|
7
|
+
// The page, in three parts: the side column, the story, and the bottom of the
|
|
8
|
+
// story with the buttons for where you are. Move, add or remove anything; a
|
|
9
|
+
// section is a <details>, so it folds without any code.
|
|
10
|
+
|
|
11
|
+
// One save slot, and what happened the last time it was used.
|
|
12
|
+
let said = $state('')
|
|
13
|
+
async function save() {
|
|
14
|
+
const saved = await runner.save('quick')
|
|
15
|
+
said = saved.ok ? 'Saved.' : saved.reason
|
|
16
|
+
}
|
|
17
|
+
async function load() {
|
|
18
|
+
const loaded = await runner.load('quick')
|
|
19
|
+
said = loaded.ok ? 'Loaded.' : loaded.reason
|
|
20
|
+
}
|
|
21
|
+
</script>
|
|
22
|
+
|
|
23
|
+
<div class="page">
|
|
24
|
+
<aside class="sidebar">
|
|
25
|
+
<h1>{$view.title}</h1>
|
|
26
|
+
<!-- Where the game is in time, if a plugin says. -->
|
|
27
|
+
{#if $view.when}<p class="clock">{$view.when}</p>{/if}
|
|
28
|
+
{#if $view.stats.length}
|
|
29
|
+
<details class="section" open>
|
|
30
|
+
<summary>Stats</summary>
|
|
31
|
+
<dl class="stats">
|
|
32
|
+
{#each $view.stats as stat (stat.key)}<dt>{stat.label}</dt><dd>{stat.text}</dd>{/each}
|
|
33
|
+
</dl>
|
|
34
|
+
</details>
|
|
35
|
+
{/if}
|
|
36
|
+
<!-- Saving. While the game's `saveable` refuses it, Save is greyed out with why (`hide` leaves it off). -->
|
|
37
|
+
<details class="section" open>
|
|
38
|
+
<summary>Saves</summary>
|
|
39
|
+
<div class="saves">
|
|
40
|
+
{#if $view.saving.visible}
|
|
41
|
+
<button disabled={!$view.saving.allowed} title={$view.saving.reason ?? undefined} onclick={save}>Save</button>
|
|
42
|
+
{/if}
|
|
43
|
+
<button onclick={load}>Load</button>
|
|
44
|
+
</div>
|
|
45
|
+
{#if $view.saving.visible && $view.saving.reason}<p class="reason">{$view.saving.reason}</p>{/if}
|
|
46
|
+
{#if said}<p class="said">{said}</p>{/if}
|
|
47
|
+
</details>
|
|
48
|
+
</aside>
|
|
49
|
+
|
|
50
|
+
<main class="story">
|
|
51
|
+
<!-- While a minigame runs, the scene's title and beats are the minigame's. -->
|
|
52
|
+
{#if $view.scene.title}<h2>{$view.scene.title}</h2>{/if}
|
|
53
|
+
<div class="prose">
|
|
54
|
+
{#each $view.scene.beats as beat, at (at)}<Beat {beat} />{/each}
|
|
55
|
+
</div>
|
|
56
|
+
{#if $view.minigame}<Minigame minigame={$view.minigame} />{/if}
|
|
57
|
+
|
|
58
|
+
<div class="inputs">
|
|
59
|
+
{#each $view.inputs as input (input.id)}<InputField {input} />{/each}
|
|
60
|
+
</div>
|
|
61
|
+
|
|
62
|
+
<div class="choices">
|
|
63
|
+
{#each $view.scene.choices as choice, at (at)}
|
|
64
|
+
<button disabled={Boolean(choice.disabled)} title={choice.disabled} onclick={() => runner.apply(choice.command)}>
|
|
65
|
+
{choice.label}
|
|
66
|
+
</button>
|
|
67
|
+
<!-- Why it can't be pressed, such as "You need 4 coins." -->
|
|
68
|
+
{#if choice.disabled}<p class="reason">{choice.disabled}</p>{/if}
|
|
69
|
+
{/each}
|
|
70
|
+
</div>
|
|
71
|
+
|
|
72
|
+
<!-- The bottom of the story: the game's own buttons and its plugins', such as a place's exits and things to do. -->
|
|
73
|
+
{#if $view.actions.length}
|
|
74
|
+
<div class="bottom choices">
|
|
75
|
+
{#each $view.actions as action, at (at)}
|
|
76
|
+
<button disabled={Boolean(action.disabled)} title={action.disabled} onclick={() => runner.apply(action.command)}>
|
|
77
|
+
{action.label}
|
|
78
|
+
</button>
|
|
79
|
+
{#if action.disabled}<p class="reason">{action.disabled}</p>{/if}
|
|
80
|
+
{/each}
|
|
81
|
+
</div>
|
|
82
|
+
{/if}
|
|
83
|
+
</main>
|
|
84
|
+
</div>
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import type { InlineSpan } from '@ifloom/engine'
|
|
3
|
+
import { runner } from './runner.ts'
|
|
4
|
+
|
|
5
|
+
// A line's words, with bold and italics, and its inline actions and tooltips as buttons in it. A tooltip, or a
|
|
6
|
+
// greyed-out action's reason, is a note after the button that shows on hover, on focus
|
|
7
|
+
// and on a tap (see `.note` in styles.css). A UI piece (`[[ui:…]]`) shows its words, styled by its name.
|
|
8
|
+
let { text, spans }: { text: string; spans?: InlineSpan[] } = $props()
|
|
9
|
+
const id = $props.id()
|
|
10
|
+
let open: number | null = $state(null)
|
|
11
|
+
|
|
12
|
+
function press(span: InlineSpan, at: number) {
|
|
13
|
+
if (span.type === 'action' && !span.choice.disabled) runner.apply(span.choice.command)
|
|
14
|
+
else open = open === at ? null : at
|
|
15
|
+
}
|
|
16
|
+
const note = (span: InlineSpan) => (span.type === 'tip' ? span.tip : span.type === 'action' ? span.choice.disabled : undefined)
|
|
17
|
+
</script>
|
|
18
|
+
|
|
19
|
+
{#if !spans}{text}{:else}{#each spans as span, at (at)}{#if span.type === 'text'}{span.text}{:else if span.type === 'bold'}<strong>{span.text}</strong>{:else if span.type === 'italic'}<em>{span.text}</em>{:else if span.type === 'ui'}<span class="ui" data-ui={span.name}>{span.text}</span>{:else}<button
|
|
20
|
+
type="button"
|
|
21
|
+
class={span.type === 'tip' ? 'tip' : 'inline-action'}
|
|
22
|
+
aria-disabled={span.type === 'action' && span.choice.disabled ? 'true' : undefined}
|
|
23
|
+
aria-describedby={note(span) ? `${id}-${at}` : undefined}
|
|
24
|
+
aria-expanded={note(span) ? open === at : undefined}
|
|
25
|
+
onclick={() => press(span, at)}>{span.text}</button
|
|
26
|
+
>{#if note(span)}<span id="{id}-{at}" role="tooltip" class="note">{note(span)}</span>{/if}{/if}{/each}{/if}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import type { MinigameView } from '@ifloom/engine'
|
|
3
|
+
import { runner } from './runner.ts'
|
|
4
|
+
|
|
5
|
+
// The minigame being played: a bar counting down its timer, and a button for each input that does something now,
|
|
6
|
+
// in the words the game's `minigame.<input>` strings give it.
|
|
7
|
+
let { minigame }: { minigame: MinigameView } = $props()
|
|
8
|
+
</script>
|
|
9
|
+
|
|
10
|
+
<section class="minigame" data-state={minigame.state}>
|
|
11
|
+
{#if minigame.timeLeft !== null && minigame.timeTotal}
|
|
12
|
+
<div class="timer" role="progressbar" aria-label="Time left">
|
|
13
|
+
<span style:transform={`scaleX(${minigame.timeLeft / minigame.timeTotal})`}></span>
|
|
14
|
+
</div>
|
|
15
|
+
{/if}
|
|
16
|
+
<div class="choices">
|
|
17
|
+
{#each minigame.inputs as input (input.id)}
|
|
18
|
+
<button onclick={() => runner.apply(input.command)}>{input.label}</button>
|
|
19
|
+
{/each}
|
|
20
|
+
</div>
|
|
21
|
+
</section>
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import type { OpenPanel } from '@ifloom/engine'
|
|
3
|
+
import { runner } from './runner.ts'
|
|
4
|
+
|
|
5
|
+
// The panel open over the story, `view.panel`: a shop's counter, a job board. A `panel` effect opens one, and so does a scene's `panel` while it's open.
|
|
6
|
+
// Draw each one your game opens by its name, from what it was opened with. Closing it sends `closePanel`; a scene's own panel has no close button, since it stays while the scene is open.
|
|
7
|
+
let { panel }: { panel: OpenPanel } = $props()
|
|
8
|
+
|
|
9
|
+
// A modal <dialog>: the browser keeps focus inside it and the page behind it out of reach while it's open.
|
|
10
|
+
let dialog: HTMLDialogElement
|
|
11
|
+
$effect(() => {
|
|
12
|
+
if (!dialog.open) dialog.showModal()
|
|
13
|
+
return () => dialog.close()
|
|
14
|
+
})
|
|
15
|
+
|
|
16
|
+
// Escape closes it, as Close does. A scene's own panel stays.
|
|
17
|
+
function cancel(event: Event) {
|
|
18
|
+
event.preventDefault()
|
|
19
|
+
if (!panel.scene) runner.apply({ type: 'closePanel' })
|
|
20
|
+
}
|
|
21
|
+
</script>
|
|
22
|
+
|
|
23
|
+
<dialog bind:this={dialog} class="panel" aria-label={panel.panel} data-panel={panel.panel} oncancel={cancel}>
|
|
24
|
+
<h2>{panel.panel}</h2>
|
|
25
|
+
<pre>{JSON.stringify(panel.with, null, 2)}</pre>
|
|
26
|
+
{#if !panel.scene}<button onclick={() => runner.apply({ type: 'closePanel' })}>Close</button>{/if}
|
|
27
|
+
</dialog>
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The game's runner, and its snapshot as a Svelte store. Components read
|
|
3
|
+
* `$view` (or the whole `$snapshot`: settings, backlog, undo, saves) and send
|
|
4
|
+
* commands with `runner.apply`; nothing else talks to the engine.
|
|
5
|
+
*/
|
|
6
|
+
import { derived, readable } from 'svelte/store'
|
|
7
|
+
import { startAudio } from '@ifloom/audio'
|
|
8
|
+
import { browserRunner, startClock, startKeys } from '@ifloom/browser'
|
|
9
|
+
import module from './game.ts'
|
|
10
|
+
|
|
11
|
+
// The game module builds the game, and says what the loader found wrong with it.
|
|
12
|
+
const { game, problems } = module.build()
|
|
13
|
+
if (problems.length) console.warn(problems)
|
|
14
|
+
|
|
15
|
+
// A new game with a random seed, undo, saves in localStorage and an autosave each turn.
|
|
16
|
+
export const runner = await browserRunner(game)
|
|
17
|
+
|
|
18
|
+
// A readable store fits the runner: it starts by subscribing, and stops with
|
|
19
|
+
// the function `subscribe` returns. Each listener is handed the new snapshot.
|
|
20
|
+
export const snapshot = readable(runner.snapshot(), (set) => runner.subscribe(set))
|
|
21
|
+
// The view, which is most of what the components draw.
|
|
22
|
+
export const view = derived(snapshot, (now) => now.view)
|
|
23
|
+
|
|
24
|
+
// Plays what the game's `audio` asks for, from the first click or key press.
|
|
25
|
+
// Its `play` plays a file on demand, such as a voice line again.
|
|
26
|
+
export const player = startAudio(runner)
|
|
27
|
+
// Ticks the game's timers and a minigame's, and maps the keyboard: 1 to 9 pick the choices, and a minigame's keys press its inputs.
|
|
28
|
+
startClock(runner)
|
|
29
|
+
startKeys(runner)
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "{{name}}",
|
|
3
|
+
"version": "0.0.0",
|
|
4
|
+
"private": true,
|
|
5
|
+
"type": "module",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"dev": "ifloom dev",
|
|
8
|
+
"build": "ifloom build",
|
|
9
|
+
"check": "ifloom check",
|
|
10
|
+
"typecheck": "tsc --noEmit",
|
|
11
|
+
"playtest": "ifloom playtest --turns 5 --out playtest/run.txt"
|
|
12
|
+
},
|
|
13
|
+
"dependencies": {
|
|
14
|
+
"@ifloom/audio": "^0.1.0",
|
|
15
|
+
"@ifloom/browser": "^0.1.0",
|
|
16
|
+
"@ifloom/engine": "^0.1.0"
|
|
17
|
+
},
|
|
18
|
+
"devDependencies": {
|
|
19
|
+
"@ifloom/cli": "^0.1.0",
|
|
20
|
+
"typescript": "6.0.3"
|
|
21
|
+
}
|
|
22
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The page's regions: a side column, the story, and the bottom of the story
|
|
3
|
+
* for the buttons of where you are. `main.ts` draws into them. Move them
|
|
4
|
+
* around, add one, or drop the side column for a page that's all story.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
/** Where things go on the page. */
|
|
8
|
+
export type Regions = { sidebar: HTMLElement; story: HTMLElement; bottom: HTMLElement }
|
|
9
|
+
|
|
10
|
+
/** Empty `root` and lay the regions out in it. */
|
|
11
|
+
export function layout(root: HTMLElement): Regions {
|
|
12
|
+
const page = document.createElement('div')
|
|
13
|
+
page.className = 'page'
|
|
14
|
+
const sidebar = page.appendChild(document.createElement('aside'))
|
|
15
|
+
sidebar.className = 'sidebar'
|
|
16
|
+
const story = page.appendChild(document.createElement('main'))
|
|
17
|
+
story.className = 'story'
|
|
18
|
+
const bottom = document.createElement('div')
|
|
19
|
+
bottom.className = 'bottom choices'
|
|
20
|
+
root.replaceChildren(page)
|
|
21
|
+
return { sidebar, story, bottom }
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** Which sections the player folded, so a redraw keeps them as they were. */
|
|
25
|
+
const folded = new Set<string>()
|
|
26
|
+
|
|
27
|
+
/** A titled section that folds: a `<details>`, open unless the player closed it. Returns where its content goes. */
|
|
28
|
+
export function section(parent: HTMLElement, id: string, title: string): HTMLElement {
|
|
29
|
+
const details = parent.appendChild(document.createElement('details'))
|
|
30
|
+
details.className = 'section'
|
|
31
|
+
details.open = !folded.has(id)
|
|
32
|
+
details.ontoggle = () => (details.open ? folded.delete(id) : folded.add(id))
|
|
33
|
+
details.appendChild(document.createElement('summary')).textContent = title
|
|
34
|
+
return details
|
|
35
|
+
}
|
|
@@ -0,0 +1,253 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The game drawn with plain DOM, no framework. The whole loop is: ask the
|
|
3
|
+
* runner for its snapshot (the view, the settings, the backlog, undo and the
|
|
4
|
+
* saves), draw it, send a command when a button is pressed, and draw again
|
|
5
|
+
* when the runner hands a new snapshot. Any framework can do the same.
|
|
6
|
+
*/
|
|
7
|
+
import type { InlineSpan, InputView, MinigameView, OpenPanel, RenderedChoice, RunnerSnapshot, SceneBeat, View } from '@ifloom/engine'
|
|
8
|
+
import { startAudio } from '@ifloom/audio'
|
|
9
|
+
import { browserRunner, startClock, startKeys } from '@ifloom/browser'
|
|
10
|
+
import module from './game.ts'
|
|
11
|
+
import { layout, section } from './layout.ts'
|
|
12
|
+
import './styles.css'
|
|
13
|
+
|
|
14
|
+
// The game module builds the game, and says what the loader found wrong with it.
|
|
15
|
+
const { game, problems } = module.build()
|
|
16
|
+
if (problems.length) console.warn(problems)
|
|
17
|
+
|
|
18
|
+
// A new game with a random seed, undo, saves in localStorage and an autosave each turn.
|
|
19
|
+
const runner = await browserRunner(game)
|
|
20
|
+
|
|
21
|
+
// Plays what the game's `audio` asks for, from the first click or key press.
|
|
22
|
+
startAudio(runner)
|
|
23
|
+
// Ticks the game's timers and a minigame's, and maps the keyboard: 1 to 9 pick the choices, and a minigame's keys press its inputs.
|
|
24
|
+
startClock(runner)
|
|
25
|
+
startKeys(runner)
|
|
26
|
+
|
|
27
|
+
const root = document.querySelector<HTMLElement>('#root')!
|
|
28
|
+
|
|
29
|
+
/** An element with a class and some text, added to `parent`. */
|
|
30
|
+
function add<K extends keyof HTMLElementTagNameMap>(parent: HTMLElement, tag: K, text = '', className = ''): HTMLElementTagNameMap[K] {
|
|
31
|
+
const element = parent.appendChild(document.createElement(tag))
|
|
32
|
+
if (text) element.textContent = text
|
|
33
|
+
if (className) element.className = className
|
|
34
|
+
return element
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** The story's region, where the scene is drawn. */
|
|
38
|
+
let story = root
|
|
39
|
+
|
|
40
|
+
function draw(snapshot: RunnerSnapshot): void {
|
|
41
|
+
const { view } = snapshot
|
|
42
|
+
const regions = layout(root)
|
|
43
|
+
story = regions.story
|
|
44
|
+
|
|
45
|
+
// The side column: the title, the time if a plugin says, and the stats in a section that folds.
|
|
46
|
+
add(regions.sidebar, 'h1', view.title)
|
|
47
|
+
if (view.when) add(regions.sidebar, 'p', view.when, 'clock')
|
|
48
|
+
if (view.stats.length) {
|
|
49
|
+
const stats = add(section(regions.sidebar, 'stats', 'Stats'), 'dl', '', 'stats')
|
|
50
|
+
for (const stat of view.stats) {
|
|
51
|
+
add(stats, 'dt', stat.label)
|
|
52
|
+
add(stats, 'dd', stat.text)
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
drawSaves(section(regions.sidebar, 'saves', 'Saves'), view)
|
|
56
|
+
|
|
57
|
+
// While a minigame runs, the scene's title and beats are the minigame's.
|
|
58
|
+
if (view.scene.title) add(story, 'h2', view.scene.title)
|
|
59
|
+
const prose = add(story, 'div', '', 'prose')
|
|
60
|
+
for (const beat of view.scene.beats) drawBeat(prose, beat)
|
|
61
|
+
if (view.minigame) drawMinigame(view.minigame)
|
|
62
|
+
|
|
63
|
+
const inputs = add(story, 'div', '', 'inputs')
|
|
64
|
+
for (const input of view.inputs) drawInput(inputs, input)
|
|
65
|
+
|
|
66
|
+
drawChoices(add(story, 'div', '', 'choices'), view.scene.choices)
|
|
67
|
+
// The bottom of the story: the game's own buttons and its plugins', such as a place's exits and things to do.
|
|
68
|
+
if (view.actions.length) drawChoices(story.appendChild(regions.bottom), view.actions)
|
|
69
|
+
|
|
70
|
+
// A panel content opened, such as a shop's counter, over everything else.
|
|
71
|
+
if (view.panel) drawPanel(view.panel)
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** What happened the last time the save slot was used. */
|
|
75
|
+
let said = ''
|
|
76
|
+
|
|
77
|
+
/** One save slot. While the game's `saveable` refuses saving, Save is greyed out with why (`hide` leaves it off). */
|
|
78
|
+
function drawSaves(parent: HTMLElement, view: View): void {
|
|
79
|
+
const row = add(parent, 'div', '', 'saves')
|
|
80
|
+
const { allowed, reason, visible } = view.saving
|
|
81
|
+
if (visible) {
|
|
82
|
+
const save = add(row, 'button', 'Save')
|
|
83
|
+
save.disabled = !allowed
|
|
84
|
+
if (reason) save.title = reason
|
|
85
|
+
save.onclick = async () => {
|
|
86
|
+
const saved = await runner.save('quick')
|
|
87
|
+
said = saved.ok ? 'Saved.' : saved.reason
|
|
88
|
+
draw(runner.snapshot())
|
|
89
|
+
}
|
|
90
|
+
if (reason) add(parent, 'p', reason, 'reason')
|
|
91
|
+
}
|
|
92
|
+
add(row, 'button', 'Load').onclick = async () => {
|
|
93
|
+
const loaded = await runner.load('quick')
|
|
94
|
+
said = loaded.ok ? 'Loaded.' : loaded.reason
|
|
95
|
+
// A load draws again by itself; a refusal doesn't.
|
|
96
|
+
if (!loaded.ok) draw(runner.snapshot())
|
|
97
|
+
}
|
|
98
|
+
if (said) add(parent, 'p', said, 'said')
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/** Buttons that send their commands, each greyed out with its reason when it can't be pressed. */
|
|
102
|
+
function drawChoices(parent: HTMLElement, choices: RenderedChoice[]): void {
|
|
103
|
+
for (const choice of choices) {
|
|
104
|
+
const button = add(parent, 'button', choice.label)
|
|
105
|
+
button.disabled = Boolean(choice.disabled)
|
|
106
|
+
if (choice.disabled) button.title = choice.disabled
|
|
107
|
+
button.onclick = () => runner.apply(choice.command)
|
|
108
|
+
// Why it can't be pressed, such as "You need 4 coins."
|
|
109
|
+
if (choice.disabled) add(parent, 'p', choice.disabled, 'reason')
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/** One paragraph of a scene: narration, a spoken line, a picture, a clip, a pick made earlier. A sound beat is heard, not drawn. */
|
|
114
|
+
function drawBeat(parent: HTMLElement, beat: SceneBeat): void {
|
|
115
|
+
switch (beat.type) {
|
|
116
|
+
case 'text':
|
|
117
|
+
drawLine(add(parent, 'p'), beat.text, beat.spans)
|
|
118
|
+
break
|
|
119
|
+
case 'say': {
|
|
120
|
+
const line = add(parent, 'p', '', 'beat-say')
|
|
121
|
+
line.dataset.who = beat.who
|
|
122
|
+
// Who says it, over the words. A quote that answers the line before names no one.
|
|
123
|
+
if (beat.name) add(line, 'span', beat.name, 'speaker-name')
|
|
124
|
+
drawLine(add(line, 'span'), beat.text, beat.spans)
|
|
125
|
+
break
|
|
126
|
+
}
|
|
127
|
+
case 'chose':
|
|
128
|
+
// A pick made earlier in a flowing scene, where the decision was.
|
|
129
|
+
add(parent, 'p', beat.label, 'beat-chose')
|
|
130
|
+
break
|
|
131
|
+
case 'image':
|
|
132
|
+
Object.assign(add(parent, 'img', '', 'beat-image'), { src: beat.src, alt: beat.alt ?? '' })
|
|
133
|
+
break
|
|
134
|
+
case 'video':
|
|
135
|
+
Object.assign(add(parent, 'video', '', 'beat-image'), {
|
|
136
|
+
src: beat.src,
|
|
137
|
+
poster: beat.poster ?? '',
|
|
138
|
+
autoplay: beat.autoplay,
|
|
139
|
+
loop: beat.loop,
|
|
140
|
+
muted: beat.muted,
|
|
141
|
+
controls: beat.controls,
|
|
142
|
+
playsInline: true,
|
|
143
|
+
})
|
|
144
|
+
break
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
let notes = 0
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* A line's words, with bold and italics, and its inline actions and tooltips as buttons in it. A
|
|
152
|
+
* tooltip, or a greyed-out action's reason, is a note after the button that
|
|
153
|
+
* shows on hover, on focus and on a tap (see `.note` in styles.css). A UI
|
|
154
|
+
* piece (`[[ui:…]]`) shows its words.
|
|
155
|
+
*/
|
|
156
|
+
function drawLine(line: HTMLElement, text: string, spans: InlineSpan[] | undefined): HTMLElement {
|
|
157
|
+
if (!spans) {
|
|
158
|
+
line.textContent = text
|
|
159
|
+
return line
|
|
160
|
+
}
|
|
161
|
+
for (const span of spans) {
|
|
162
|
+
if (span.type === 'text') {
|
|
163
|
+
line.append(span.text)
|
|
164
|
+
continue
|
|
165
|
+
}
|
|
166
|
+
if (span.type === 'bold' || span.type === 'italic') {
|
|
167
|
+
add(line, span.type === 'bold' ? 'strong' : 'em', span.text)
|
|
168
|
+
continue
|
|
169
|
+
}
|
|
170
|
+
if (span.type === 'ui') {
|
|
171
|
+
// A UI piece (`[[ui:name|words]]`): this page has no components of its own, so it shows the words, styled by name.
|
|
172
|
+
add(line, 'span', span.text, 'ui').dataset.ui = span.name
|
|
173
|
+
continue
|
|
174
|
+
}
|
|
175
|
+
const button = add(line, 'button', span.text, span.type === 'tip' ? 'tip' : 'inline-action')
|
|
176
|
+
button.type = 'button'
|
|
177
|
+
const note = span.type === 'tip' ? span.tip : span.choice.disabled
|
|
178
|
+
if (span.type === 'action') {
|
|
179
|
+
// Greyed out but still focusable, so its reason can be read.
|
|
180
|
+
if (span.choice.disabled) button.setAttribute('aria-disabled', 'true')
|
|
181
|
+
else button.onclick = () => runner.apply(span.choice.command)
|
|
182
|
+
}
|
|
183
|
+
if (!note) continue
|
|
184
|
+
const id = `note-${(notes += 1)}`
|
|
185
|
+
const shown = add(line, 'span', note, 'note')
|
|
186
|
+
Object.assign(shown, { id, role: 'tooltip' })
|
|
187
|
+
button.setAttribute('aria-describedby', id)
|
|
188
|
+
button.setAttribute('aria-expanded', 'false')
|
|
189
|
+
if (!button.onclick) button.onclick = () => button.setAttribute('aria-expanded', String(button.getAttribute('aria-expanded') !== 'true'))
|
|
190
|
+
}
|
|
191
|
+
return line
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/** A value the game asks for, such as the player's name. What they enter goes back as an `input` command. */
|
|
195
|
+
function drawInput(parent: HTMLElement, input: InputView): void {
|
|
196
|
+
if (input.type === 'choice') {
|
|
197
|
+
for (const option of input.options) {
|
|
198
|
+
add(parent, 'button', option.label).onclick = () => runner.apply({ type: 'input', id: input.id, to: option.value })
|
|
199
|
+
}
|
|
200
|
+
return
|
|
201
|
+
}
|
|
202
|
+
const form = add(parent, 'form')
|
|
203
|
+
const label = add(form, 'label', input.label)
|
|
204
|
+
const field = add(label, 'input')
|
|
205
|
+
field.type = input.type === 'number' ? 'number' : 'text'
|
|
206
|
+
field.value = input.value === null ? '' : String(input.value)
|
|
207
|
+
if (input.maxLength !== null) field.maxLength = input.maxLength
|
|
208
|
+
if (input.min !== null) field.min = String(input.min)
|
|
209
|
+
if (input.max !== null) field.max = String(input.max)
|
|
210
|
+
add(form, 'button', 'OK').type = 'submit'
|
|
211
|
+
form.onsubmit = (event) => {
|
|
212
|
+
event.preventDefault()
|
|
213
|
+
runner.apply({ type: 'input', id: input.id, to: input.type === 'number' ? Number(field.value) : field.value })
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/** The minigame being played: a bar counting down its timer, and a button for each input that does something now, in its own words. */
|
|
218
|
+
function drawMinigame(minigame: MinigameView): void {
|
|
219
|
+
const box = add(story, 'section', '', 'minigame')
|
|
220
|
+
box.dataset.state = minigame.state
|
|
221
|
+
if (minigame.timeLeft !== null && minigame.timeTotal) {
|
|
222
|
+
const timer = add(box, 'div', '', 'timer')
|
|
223
|
+
timer.setAttribute('role', 'progressbar')
|
|
224
|
+
timer.setAttribute('aria-label', 'Time left')
|
|
225
|
+
add(timer, 'span').style.transform = `scaleX(${minigame.timeLeft / minigame.timeTotal})`
|
|
226
|
+
}
|
|
227
|
+
// Each input is a button like any other, labelled by the game's `minigame.<input>` strings.
|
|
228
|
+
drawChoices(add(box, 'div', '', 'choices'), minigame.inputs)
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/**
|
|
232
|
+
* The panel open over the story, `view.panel`: a shop's counter, a job board. A `panel` effect opens one, and so does a scene's `panel` while it's open.
|
|
233
|
+
* Draw each one your game opens by its name, from what it was opened with. Closing it sends `closePanel`; a scene's own panel has no close button, since it stays while the scene is open.
|
|
234
|
+
*/
|
|
235
|
+
function drawPanel(panel: OpenPanel): void {
|
|
236
|
+
// A modal <dialog>: the browser keeps focus inside it and the page behind it out of reach while it's open.
|
|
237
|
+
const dialog = add(root, 'dialog', '', 'panel')
|
|
238
|
+
dialog.setAttribute('aria-label', panel.panel)
|
|
239
|
+
dialog.dataset.panel = panel.panel
|
|
240
|
+
add(dialog, 'h2', panel.panel)
|
|
241
|
+
add(dialog, 'pre', JSON.stringify(panel.with, null, 2))
|
|
242
|
+
if (!panel.scene) add(dialog, 'button', 'Close').onclick = () => runner.apply({ type: 'closePanel' })
|
|
243
|
+
// Escape closes it, as Close does. A scene's own panel stays.
|
|
244
|
+
dialog.oncancel = (event) => {
|
|
245
|
+
event.preventDefault()
|
|
246
|
+
if (!panel.scene) runner.apply({ type: 'closePanel' })
|
|
247
|
+
}
|
|
248
|
+
dialog.showModal()
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
// The runner hands every listener a new snapshot whenever anything in it changes.
|
|
252
|
+
runner.subscribe(draw)
|
|
253
|
+
draw(runner.snapshot())
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "{{name}}",
|
|
3
|
+
"version": "0.0.0",
|
|
4
|
+
"private": true,
|
|
5
|
+
"type": "module",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"dev": "ifloom dev",
|
|
8
|
+
"build": "ifloom build",
|
|
9
|
+
"check": "ifloom check",
|
|
10
|
+
"typecheck": "vue-tsc --noEmit",
|
|
11
|
+
"playtest": "ifloom playtest --turns 5 --out playtest/run.txt"
|
|
12
|
+
},
|
|
13
|
+
"dependencies": {
|
|
14
|
+
"@ifloom/audio": "^0.1.0",
|
|
15
|
+
"@ifloom/browser": "^0.1.0",
|
|
16
|
+
"@ifloom/engine": "^0.1.0",
|
|
17
|
+
"vue": "3.5.44"
|
|
18
|
+
},
|
|
19
|
+
"devDependencies": {
|
|
20
|
+
"@ifloom/cli": "^0.1.0",
|
|
21
|
+
"@vitejs/plugin-vue": "6.0.10",
|
|
22
|
+
"typescript": "6.0.3",
|
|
23
|
+
"vue-tsc": "3.3.13"
|
|
24
|
+
}
|
|
25
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
<script setup lang="ts">
|
|
2
|
+
import { view } from './runner.ts'
|
|
3
|
+
import Layout from './Layout.vue'
|
|
4
|
+
import Panel from './Panel.vue'
|
|
5
|
+
</script>
|
|
6
|
+
|
|
7
|
+
<template>
|
|
8
|
+
<!-- The page: the side column, the story and its bottom. See Layout.vue. -->
|
|
9
|
+
<Layout />
|
|
10
|
+
<!-- A panel content opened, such as a shop's counter, over everything else. -->
|
|
11
|
+
<Panel v-if="view.panel" :panel="view.panel" />
|
|
12
|
+
</template>
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
<script setup lang="ts">
|
|
2
|
+
import type { SceneBeat } from '@ifloom/engine'
|
|
3
|
+
import Line from './Line.vue'
|
|
4
|
+
|
|
5
|
+
// One paragraph of a scene: narration, a spoken line, a picture, a clip, a pick made earlier. A sound beat is heard, not drawn.
|
|
6
|
+
defineProps<{ beat: SceneBeat }>()
|
|
7
|
+
</script>
|
|
8
|
+
|
|
9
|
+
<template>
|
|
10
|
+
<p v-if="beat.type === 'text'"><Line :text="beat.text" :spans="beat.spans" /></p>
|
|
11
|
+
<p v-else-if="beat.type === 'say'" class="beat-say" :data-who="beat.who">
|
|
12
|
+
<span v-if="beat.name" class="speaker-name">{{ beat.name }}</span>
|
|
13
|
+
<Line :text="beat.text" :spans="beat.spans" />
|
|
14
|
+
</p>
|
|
15
|
+
<p v-else-if="beat.type === 'chose'" class="beat-chose">{{ beat.label }}</p>
|
|
16
|
+
<img v-else-if="beat.type === 'image'" class="beat-image" :src="beat.src" :alt="beat.alt ?? ''" />
|
|
17
|
+
<video
|
|
18
|
+
v-else-if="beat.type === 'video'"
|
|
19
|
+
class="beat-image"
|
|
20
|
+
:src="beat.src"
|
|
21
|
+
:poster="beat.poster"
|
|
22
|
+
:autoplay="beat.autoplay"
|
|
23
|
+
:loop="beat.loop"
|
|
24
|
+
:muted="beat.muted"
|
|
25
|
+
:controls="beat.controls"
|
|
26
|
+
playsinline
|
|
27
|
+
/>
|
|
28
|
+
</template>
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
<script setup lang="ts">
|
|
2
|
+
import { ref } from 'vue'
|
|
3
|
+
import type { InputView } from '@ifloom/engine'
|
|
4
|
+
import { runner } from './runner.ts'
|
|
5
|
+
|
|
6
|
+
// A value the game asks for, such as the player's name. What they enter goes back as an `input` command.
|
|
7
|
+
const props = defineProps<{ input: InputView }>()
|
|
8
|
+
const value = ref(props.input.value === null ? '' : String(props.input.value))
|
|
9
|
+
|
|
10
|
+
function submit() {
|
|
11
|
+
runner.apply({ type: 'input', id: props.input.id, to: props.input.type === 'number' ? Number(value.value) : value.value })
|
|
12
|
+
}
|
|
13
|
+
</script>
|
|
14
|
+
|
|
15
|
+
<template>
|
|
16
|
+
<div v-if="input.type === 'choice'" class="inputs" role="group" :aria-label="input.label">
|
|
17
|
+
<button v-for="option in input.options" :key="String(option.value)" @click="runner.apply({ type: 'input', id: input.id, to: option.value })">
|
|
18
|
+
{{ option.label }}
|
|
19
|
+
</button>
|
|
20
|
+
</div>
|
|
21
|
+
<form v-else @submit.prevent="submit">
|
|
22
|
+
<label>
|
|
23
|
+
{{ input.label }}
|
|
24
|
+
<input
|
|
25
|
+
v-model="value"
|
|
26
|
+
:type="input.type === 'number' ? 'number' : 'text'"
|
|
27
|
+
:maxlength="input.maxLength ?? undefined"
|
|
28
|
+
:min="input.min ?? undefined"
|
|
29
|
+
:max="input.max ?? undefined"
|
|
30
|
+
/>
|
|
31
|
+
</label>
|
|
32
|
+
<button type="submit">OK</button>
|
|
33
|
+
</form>
|
|
34
|
+
</template>
|