yarn-spinner-runner-ts 0.1.0 → 0.1.1
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/README.md +203 -20
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
# yarn-spinner-ts
|
|
1
|
+
# yarn-spinner-runner-ts
|
|
2
2
|
|
|
3
|
-
TypeScript parser, compiler, and runtime for Yarn Spinner 3.x
|
|
3
|
+
TypeScript parser, compiler, and runtime for Yarn Spinner 3.x with React adapter.
|
|
4
|
+
[Github repository](https://github.com/oleksii-chekhovskyi/yarn-spinner-runner-ts) for more information.
|
|
4
5
|
|
|
5
6
|
## References
|
|
6
7
|
|
|
@@ -15,17 +16,24 @@ TypeScript parser, compiler, and runtime for Yarn Spinner 3.x, with a React adap
|
|
|
15
16
|
- ✅ Compiler: AST → Intermediate Representation (IR)
|
|
16
17
|
- ✅ Runtime with `YarnRunner` class
|
|
17
18
|
- ✅ React hook: `useYarnRunner()`
|
|
18
|
-
- ✅ React
|
|
19
|
+
- ✅ React components: `<DialogueView />`, `<DialogueScene />`, `<DialogueExample />`
|
|
19
20
|
- ✅ Expression evaluator for conditions
|
|
20
|
-
- ✅ Command system with built-in handlers (`<<set>>`, etc.)
|
|
21
|
+
- ✅ Command system with built-in handlers (`<<set>>`, `<<declare>>`, etc.)
|
|
22
|
+
- ✅ Scene system with backgrounds and actor images
|
|
23
|
+
- ✅ Custom CSS styling via `&css{}` attributes
|
|
24
|
+
- ✅ Built-in functions (`visited`, `random`, `min`, `max`, etc.)
|
|
21
25
|
- ✅ Support for:
|
|
22
26
|
- Lines with speakers
|
|
23
27
|
- Options with indented bodies
|
|
24
|
-
-
|
|
28
|
+
- `<<if>>/<<elseif>>/<<else>>/<<endif>>` blocks
|
|
25
29
|
- `<<once>>...<<endonce>>` blocks
|
|
26
30
|
- `<<jump NodeName>>` commands
|
|
27
31
|
- `<<detour NodeName>>` commands
|
|
28
|
-
- Variables and
|
|
32
|
+
- Variables and expressions
|
|
33
|
+
- Enums (`<<enum>>` blocks)
|
|
34
|
+
- Smart variables (`<<declare $var = expr>>`)
|
|
35
|
+
- Node groups with `when:` conditions
|
|
36
|
+
- Tags and metadata on nodes, lines, and options
|
|
29
37
|
- Custom commands
|
|
30
38
|
|
|
31
39
|
## Installation
|
|
@@ -40,9 +48,7 @@ npm run build
|
|
|
40
48
|
### Basic Usage
|
|
41
49
|
|
|
42
50
|
```typescript
|
|
43
|
-
import { parseYarn } from "yarn-spinner-ts";
|
|
44
|
-
import { compile } from "yarn-spinner-ts";
|
|
45
|
-
import { YarnRunner } from "yarn-spinner-ts";
|
|
51
|
+
import { parseYarn, compile, YarnRunner } from "yarn-spinner-runner-ts";
|
|
46
52
|
|
|
47
53
|
const yarnText = `
|
|
48
54
|
title: Start
|
|
@@ -79,7 +85,9 @@ runner.advance(0); // Choose option 0
|
|
|
79
85
|
### React Usage
|
|
80
86
|
|
|
81
87
|
```tsx
|
|
82
|
-
import { parseYarn, compile, useYarnRunner, DialogueView } from "yarn-spinner-ts";
|
|
88
|
+
import { parseYarn, compile, useYarnRunner, DialogueView } from "yarn-spinner-runner-ts";
|
|
89
|
+
import { parseScenes } from "yarn-spinner-runner-ts";
|
|
90
|
+
import type { SceneCollection } from "yarn-spinner-runner-ts";
|
|
83
91
|
|
|
84
92
|
function MyDialogue() {
|
|
85
93
|
const [program] = useState(() => {
|
|
@@ -87,25 +95,45 @@ function MyDialogue() {
|
|
|
87
95
|
return compile(ast);
|
|
88
96
|
});
|
|
89
97
|
|
|
98
|
+
const [scenes] = useState<SceneCollection>(() => {
|
|
99
|
+
return parseScenes(sceneYamlText);
|
|
100
|
+
});
|
|
101
|
+
|
|
90
102
|
const { result, advance } = useYarnRunner(program, {
|
|
91
103
|
startAt: "Start",
|
|
92
104
|
variables: { score: 10 },
|
|
93
105
|
});
|
|
94
106
|
|
|
95
|
-
return
|
|
107
|
+
return (
|
|
108
|
+
<DialogueView
|
|
109
|
+
result={result}
|
|
110
|
+
onAdvance={advance}
|
|
111
|
+
scenes={scenes}
|
|
112
|
+
/>
|
|
113
|
+
);
|
|
96
114
|
}
|
|
97
115
|
```
|
|
98
116
|
|
|
99
|
-
###
|
|
117
|
+
### Full Example Component
|
|
100
118
|
|
|
101
119
|
```tsx
|
|
102
|
-
import { DialogueExample } from "yarn-spinner-ts";
|
|
120
|
+
import { DialogueExample } from "yarn-spinner-runner-ts";
|
|
103
121
|
|
|
104
122
|
function App() {
|
|
105
123
|
return <DialogueExample />;
|
|
106
124
|
}
|
|
107
125
|
```
|
|
108
126
|
|
|
127
|
+
### Browser Demo
|
|
128
|
+
|
|
129
|
+
Run the interactive browser demo:
|
|
130
|
+
|
|
131
|
+
```bash
|
|
132
|
+
npm run demo
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
This starts a Vite dev server with a live Yarn script editor and dialogue system.
|
|
136
|
+
|
|
109
137
|
## API Reference
|
|
110
138
|
|
|
111
139
|
### Parser
|
|
@@ -125,56 +153,211 @@ function App() {
|
|
|
125
153
|
- `setVariable(name: string, value: unknown): void` — Set variable value
|
|
126
154
|
- `getVariables(): Readonly<Record<string, unknown>>` — Get all variables
|
|
127
155
|
|
|
128
|
-
### React
|
|
156
|
+
### React Components
|
|
129
157
|
|
|
130
158
|
- `useYarnRunner(program: IRProgram, options: RunnerOptions)` — React hook
|
|
131
|
-
-
|
|
159
|
+
- Returns: `{ result: RuntimeResult | null, advance: (optionIndex?: number) => void, runner: YarnRunner }`
|
|
160
|
+
- `<DialogueView result={...} onAdvance={...} scenes={...} />` — Ready-to-use dialogue component
|
|
161
|
+
- `<DialogueScene sceneName={...} speaker={...} scenes={...} />` — Scene background and actor display
|
|
162
|
+
- `<DialogueExample />` — Full example with editor
|
|
163
|
+
|
|
164
|
+
### Scene System
|
|
165
|
+
|
|
166
|
+
- `parseScenes(input: string | Record<string, unknown>): SceneCollection` — Parse YAML scene configuration
|
|
167
|
+
- `SceneCollection` — Type for scene configuration
|
|
168
|
+
- `SceneConfig` — Type for individual scene config
|
|
169
|
+
- `ActorConfig` — Type for actor configuration
|
|
170
|
+
|
|
171
|
+
See [Scene and Actor Setup Guide](./docs/scenes-actors-setup.md) for detailed documentation.
|
|
132
172
|
|
|
133
173
|
### Expression Evaluator
|
|
134
174
|
|
|
135
|
-
- `ExpressionEvaluator(variables, functions)` — Safe expression evaluator
|
|
175
|
+
- `ExpressionEvaluator(variables, functions, enums?)` — Safe expression evaluator
|
|
136
176
|
- Supports: `===`, `!==`, `<`, `>`, `<=`, `>=`, `&&`, `||`, `!`
|
|
177
|
+
- Operator aliases: `eq/is`, `neq`, `gt`, `lt`, `lte`, `gte`, `and`, `or`, `not`, `xor`
|
|
137
178
|
- Function calls: `functionName(arg1, arg2)`
|
|
138
179
|
- Variables, numbers, strings, booleans
|
|
180
|
+
- Enum support with shorthand (`MyEnum.Case`)
|
|
139
181
|
|
|
140
182
|
### Commands
|
|
141
183
|
|
|
142
184
|
- `CommandHandler` — Command handler registry
|
|
143
|
-
- Built-in: `<<set variable value>>`
|
|
185
|
+
- Built-in: `<<set variable = value>>`, `<<declare $var = expr>>`
|
|
144
186
|
- Register custom handlers: `handler.register("mycommand", (args) => { ... })`
|
|
187
|
+
- `parseCommand(content: string): ParsedCommand` — Parse command string
|
|
188
|
+
|
|
189
|
+
### Built-in Functions
|
|
190
|
+
|
|
191
|
+
The runtime includes these built-in functions:
|
|
192
|
+
|
|
193
|
+
- `visited(nodeName)` — Check if a node was visited
|
|
194
|
+
- `visited_count(nodeName)` — Get visit count for a node
|
|
195
|
+
- `random()` — Random float 0-1
|
|
196
|
+
- `random_range(min, max)` — Random integer in range
|
|
197
|
+
- `dice(sides)` — Roll a die
|
|
198
|
+
- `min(a, b)`, `max(a, b)` — Min/max values
|
|
199
|
+
- `round(n)`, `round_places(n, places)` — Rounding
|
|
200
|
+
- `floor(n)`, `ceil(n)` — Floor/ceiling
|
|
201
|
+
- `inc(n)`, `dec(n)` — Increment/decrement
|
|
202
|
+
- `decimal(n)` — Convert to decimal
|
|
203
|
+
- `int(n)` — Convert to integer
|
|
204
|
+
- `string(n)`, `number(n)`, `bool(n)` — Type conversions
|
|
145
205
|
|
|
146
206
|
## Example Yarn Script
|
|
147
207
|
|
|
148
208
|
```yarn
|
|
149
209
|
title: Start
|
|
210
|
+
tags: #introduction #tutorial
|
|
150
211
|
---
|
|
151
212
|
Narrator: Welcome!
|
|
152
213
|
<<set score = 10>>
|
|
153
|
-
|
|
214
|
+
<<if score >= 10>>
|
|
154
215
|
Narrator: High score!
|
|
155
|
-
|
|
216
|
+
<<else>>
|
|
156
217
|
Narrator: Low score.
|
|
157
|
-
|
|
218
|
+
<<endif>>
|
|
219
|
+
|
|
220
|
+
<<declare $randomName = random_range(1, 3) == 1 ? "Alice" : "Bob">>
|
|
221
|
+
Narrator: Your name is {$randomName}.
|
|
222
|
+
|
|
158
223
|
-> Ask about features
|
|
159
224
|
Player: What can this do?
|
|
160
225
|
Narrator: Lots of things!
|
|
161
226
|
-> Ask about commands
|
|
162
227
|
Player: Tell me about commands.
|
|
163
228
|
Narrator: Commands modify state.
|
|
229
|
+
|
|
164
230
|
<<once>>
|
|
165
231
|
Narrator: This only shows once!
|
|
166
232
|
<<endonce>>
|
|
233
|
+
|
|
234
|
+
<<jump NextNode>>
|
|
235
|
+
===
|
|
236
|
+
|
|
237
|
+
title: NextNode
|
|
238
|
+
scene: scene1
|
|
239
|
+
---
|
|
240
|
+
Narrator: You've arrived at the next scene!
|
|
167
241
|
===
|
|
168
242
|
```
|
|
169
243
|
|
|
244
|
+
## CSS Styling
|
|
245
|
+
|
|
246
|
+
You can apply custom CSS styles to nodes and options using the `&css{}` attribute:
|
|
247
|
+
|
|
248
|
+
```yarn
|
|
249
|
+
title: StyledNode
|
|
250
|
+
&css{background-color: #ff0000; color: white;}
|
|
251
|
+
---
|
|
252
|
+
Narrator: This node has a red background.
|
|
253
|
+
|
|
254
|
+
-> Option 1 &css{background-color: blue;}
|
|
255
|
+
Narrator: You chose the blue option.
|
|
256
|
+
===
|
|
257
|
+
```
|
|
258
|
+
|
|
259
|
+
Styles are merged with default styles, with custom styles taking precedence.
|
|
260
|
+
|
|
261
|
+
See [CSS Attribute Documentation](./docs/css-attribute.md) for details.
|
|
262
|
+
|
|
263
|
+
## Scene Configuration
|
|
264
|
+
|
|
265
|
+
Configure scenes and actors using YAML:
|
|
266
|
+
|
|
267
|
+
```yaml
|
|
268
|
+
scenes:
|
|
269
|
+
scene1:
|
|
270
|
+
background: https://example.com/background1.jpg
|
|
271
|
+
actors:
|
|
272
|
+
special_npc:
|
|
273
|
+
image: https://example.com/special-npc.png
|
|
274
|
+
|
|
275
|
+
actors:
|
|
276
|
+
Narrator: https://example.com/narrator.png
|
|
277
|
+
Player: https://example.com/player.png
|
|
278
|
+
```
|
|
279
|
+
|
|
280
|
+
Use scenes in Yarn nodes:
|
|
281
|
+
|
|
282
|
+
```yarn
|
|
283
|
+
title: MyNode
|
|
284
|
+
scene: scene1
|
|
285
|
+
---
|
|
286
|
+
Narrator: This scene uses scene1's background and actors.
|
|
287
|
+
===
|
|
288
|
+
```
|
|
289
|
+
|
|
290
|
+
See [Scene and Actor Setup Guide](./docs/scenes-actors-setup.md) for complete documentation.
|
|
291
|
+
|
|
292
|
+
## Project Structure
|
|
293
|
+
|
|
294
|
+
```
|
|
295
|
+
yarn-spinner/
|
|
296
|
+
├── src/
|
|
297
|
+
│ ├── model/ # AST types
|
|
298
|
+
│ ├── parse/ # Lexer and parser
|
|
299
|
+
│ ├── compile/ # Compiler (AST → IR)
|
|
300
|
+
│ ├── runtime/ # Runtime execution
|
|
301
|
+
│ ├── scene/ # Scene system
|
|
302
|
+
│ ├── react/ # React components
|
|
303
|
+
│ └── tests/ # Test files
|
|
304
|
+
├── examples/
|
|
305
|
+
│ ├── yarn/ # Example Yarn scripts
|
|
306
|
+
│ ├── browser/ # Browser demo (Vite)
|
|
307
|
+
│ └── scenes/ # Scene configuration examples
|
|
308
|
+
├── docs/ # Documentation
|
|
309
|
+
└── dist/ # Compiled output
|
|
310
|
+
```
|
|
311
|
+
|
|
170
312
|
## Development
|
|
171
313
|
|
|
172
314
|
```bash
|
|
173
315
|
npm run build # Build TypeScript
|
|
174
316
|
npm run dev # Watch mode
|
|
317
|
+
npm run lint # Run ESLint
|
|
175
318
|
npm test # Run tests
|
|
319
|
+
npm run demo # Start browser demo
|
|
320
|
+
npm run demo:build # Build browser demo
|
|
176
321
|
```
|
|
177
322
|
|
|
323
|
+
## Testing
|
|
324
|
+
|
|
325
|
+
Tests are located in `src/tests/` and cover:
|
|
326
|
+
|
|
327
|
+
- Basic dialogue flow
|
|
328
|
+
- Options and branching
|
|
329
|
+
- Variables and flow control
|
|
330
|
+
- Commands (`<<set>>`, `<<declare>>`, etc.)
|
|
331
|
+
- `<<once>>` blocks
|
|
332
|
+
- `<<jump>>` and `<<detour>>`
|
|
333
|
+
- Full featured Yarn scripts
|
|
334
|
+
|
|
335
|
+
Run tests:
|
|
336
|
+
|
|
337
|
+
```bash
|
|
338
|
+
npm test
|
|
339
|
+
```
|
|
340
|
+
|
|
341
|
+
## Documentation
|
|
342
|
+
|
|
343
|
+
Additional documentation is available in the `docs/` folder:
|
|
344
|
+
|
|
345
|
+
- [Lines, Nodes, and Options](./docs/lines-nodes-and-options.md)
|
|
346
|
+
- [Options](./docs/options.md)
|
|
347
|
+
- [Jumps](./docs/jumps.md)
|
|
348
|
+
- [Detour](./docs/detour.md)
|
|
349
|
+
- [Logic and Variables](./docs/logic-and-variables.md)
|
|
350
|
+
- [Flow Control](./docs/flow-control.md)
|
|
351
|
+
- [Once Blocks](./docs/once.md)
|
|
352
|
+
- [Smart Variables](./docs/smart-variables.md)
|
|
353
|
+
- [Enums](./docs/enums.md)
|
|
354
|
+
- [Commands](./docs/commands.md)
|
|
355
|
+
- [Functions](./docs/functions.md)
|
|
356
|
+
- [Node Groups](./docs/node-groups.md)
|
|
357
|
+
- [Tags and Metadata](./docs/tags-metadata.md)
|
|
358
|
+
- [CSS Attribute](./docs/css-attribute.md)
|
|
359
|
+
- [Scene and Actor Setup](./docs/scenes-actors-setup.md)
|
|
360
|
+
|
|
178
361
|
## License
|
|
179
362
|
|
|
180
363
|
MIT
|