@inglorious/engine 24.11.0 → 24.12.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/README.md
CHANGED
|
@@ -136,12 +136,12 @@ both at once, which is the difference between moving the thing and moving its ou
|
|
|
136
136
|
|
|
137
137
|
### Pausing
|
|
138
138
|
|
|
139
|
-
`api.notify("pause")` stops the store handing out `update` events, and `notify("resume")` starts it again:
|
|
139
|
+
`api.notify("pause")` stops the store handing out `update` events, and `notify("resume")` starts it again. `pauseToggle` sends whichever of the two is due, for a single key or button (`Space: "pauseToggle"`); a state that does more on it, such as playing a sound, answers it itself:
|
|
140
140
|
|
|
141
141
|
```javascript
|
|
142
142
|
fsm({
|
|
143
143
|
play: {
|
|
144
|
-
|
|
144
|
+
pauseToggle(entity, _, api) {
|
|
145
145
|
api.notify("soundPlay", "pause")
|
|
146
146
|
api.notify(entity.paused ? "resume" : "pause")
|
|
147
147
|
},
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@inglorious/engine",
|
|
3
|
-
"version": "24.
|
|
3
|
+
"version": "24.12.0",
|
|
4
4
|
"description": "A JavaScript game engine written with global state, immutability, and pure functions in mind. Have fun(ctional programming) with it!",
|
|
5
5
|
"author": "Matteo Antony Mistretta <antony.mistretta@gmail.com> (https://ingloriouscoderz.it)",
|
|
6
6
|
"license": "MIT",
|
package/src/behaviors/game.js
CHANGED
|
@@ -8,6 +8,11 @@ export function game() {
|
|
|
8
8
|
entity.paused = false
|
|
9
9
|
},
|
|
10
10
|
|
|
11
|
+
// one key or button for both: pauses a running world, resumes a paused one
|
|
12
|
+
pauseToggle(entity, payload, api) {
|
|
13
|
+
api.notify(entity.paused ? "resume" : "pause")
|
|
14
|
+
},
|
|
15
|
+
|
|
11
16
|
quit(entity) {
|
|
12
17
|
entity.quit = true
|
|
13
18
|
},
|
|
@@ -40,7 +40,7 @@ const storedAt = new Map()
|
|
|
40
40
|
* new seed); the one before ends where it was. Ignored while replaying, so a replay
|
|
41
41
|
* is not knocked off its runs. `create` always starts one.
|
|
42
42
|
*
|
|
43
|
-
* It handles `
|
|
43
|
+
* It handles `recordingSave` (the session so far, downloaded as a JSON file),
|
|
44
44
|
* `replayLoad` (a session to play back, typically a file dropped on the page: see
|
|
45
45
|
* `onDroppedJSON` and `engine.notify`; on success it notifies `replayStart`, for the
|
|
46
46
|
* game to leave its menus) and `replayStop`. While replaying, `entity.replay` is
|
|
@@ -188,8 +188,8 @@ export function recorded({
|
|
|
188
188
|
else entity.replay = { ...recorder.progress(), over: true }
|
|
189
189
|
},
|
|
190
190
|
|
|
191
|
-
|
|
192
|
-
type.
|
|
191
|
+
recordingSave(entity, payload, api) {
|
|
192
|
+
type.recordingSave?.(entity, payload, api)
|
|
193
193
|
const session = recorderOf(entity).session(
|
|
194
194
|
turn(entity) ?? NO_TURNS,
|
|
195
195
|
state(entity),
|
|
@@ -48,19 +48,22 @@ function release(entity, id) {
|
|
|
48
48
|
}
|
|
49
49
|
|
|
50
50
|
/**
|
|
51
|
-
* What a button is for: notifies its `
|
|
52
|
-
* `button()`, which only knows how a button looks.
|
|
51
|
+
* What a button is for: notifies its `event`, with its `payload` if it has one, when
|
|
52
|
+
* it is pressed. A behaviour next to `button()`, which only knows how a button looks.
|
|
53
|
+
* The payload lets buttons that differ by one value share an event: a level picker's
|
|
54
|
+
* buttons all notify `levelPlay`, each with its own level.
|
|
53
55
|
*
|
|
54
56
|
* @example
|
|
55
57
|
* ```js
|
|
56
58
|
* types: { Button: [button(), pressable(), { render: renderButton }] }
|
|
57
|
-
* entities: { pause: { type: "Button",
|
|
59
|
+
* entities: { pause: { type: "Button", event: "pauseToggle", label: "Pause" } }
|
|
58
60
|
* ```
|
|
59
61
|
*/
|
|
60
62
|
export function pressable() {
|
|
61
63
|
return {
|
|
62
64
|
stateChange(entity, { entityId, to }, api) {
|
|
63
|
-
if (entityId === entity.id && to === "pressed")
|
|
65
|
+
if (entityId === entity.id && to === "pressed")
|
|
66
|
+
api.notify(entity.event, entity.payload)
|
|
64
67
|
},
|
|
65
68
|
}
|
|
66
69
|
}
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import type { GameEntity, Handler } from "../index"
|
|
2
2
|
|
|
3
|
-
/** The `game` entity's own handlers: pause, resume, quit, and `C` and `D` for debug and dev mode. */
|
|
3
|
+
/** The `game` entity's own handlers: pause, resume, pause toggle, quit, and `C` and `D` for debug and dev mode. */
|
|
4
4
|
export function game(): {
|
|
5
5
|
pause: Handler<GameEntity>
|
|
6
6
|
resume: Handler<GameEntity>
|
|
7
|
+
pauseToggle: Handler<GameEntity>
|
|
7
8
|
quit: Handler<GameEntity>
|
|
8
9
|
keyboardKeyUp: Handler<GameEntity, string>
|
|
9
10
|
}
|
|
@@ -50,7 +50,7 @@ export function recorded<
|
|
|
50
50
|
Record<string, Handler<TEntity, unknown>> & {
|
|
51
51
|
create: Handler<TEntity, unknown>
|
|
52
52
|
tick: Handler<TEntity, number>
|
|
53
|
-
|
|
53
|
+
recordingSave: Handler<TEntity, unknown>
|
|
54
54
|
replayLoad: Handler<TEntity, RecordedSession<About, Input>>
|
|
55
55
|
replayStop: Handler<TEntity, unknown>
|
|
56
56
|
}
|
|
@@ -4,12 +4,14 @@ export interface ButtonEntity extends Entity {
|
|
|
4
4
|
state?: "default" | "pressed"
|
|
5
5
|
/** True while the pointer is over it. */
|
|
6
6
|
hovered?: boolean
|
|
7
|
-
/**
|
|
8
|
-
|
|
7
|
+
/** The event `pressable()` notifies when it is pressed. */
|
|
8
|
+
event?: string
|
|
9
|
+
/** What that event carries, if anything. */
|
|
10
|
+
payload?: unknown
|
|
9
11
|
}
|
|
10
12
|
|
|
11
13
|
/** Looks pressed from `entityPress` to `entityRelease` (or `entityTouchStart` to `entityTouchEnd`), and announces it through `stateChange`. */
|
|
12
14
|
export function button(): Decorator<GameType<ButtonEntity>>
|
|
13
15
|
|
|
14
|
-
/** Notifies the button's `
|
|
16
|
+
/** Notifies the button's `event`, with its `payload`, when it is pressed. */
|
|
15
17
|
export function pressable(): GameType<ButtonEntity>
|