@inglorious/engine 24.10.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 +2 -2
- package/package.json +3 -3
- package/src/animation/timeline.js +37 -0
- package/src/behaviors/game.js +5 -0
- package/src/behaviors/recorded.js +3 -3
- package/src/behaviors/ui/button.js +7 -4
- package/types/animation/timeline.d.ts +12 -0
- package/types/behaviors/game.d.ts +2 -1
- package/types/behaviors/recorded.d.ts +1 -1
- package/types/behaviors/ui/button.d.ts +5 -3
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",
|
|
@@ -41,7 +41,7 @@
|
|
|
41
41
|
},
|
|
42
42
|
"sideEffects": false,
|
|
43
43
|
"dependencies": {
|
|
44
|
-
"@inglorious/utils": "^5.
|
|
44
|
+
"@inglorious/utils": "^5.10.0"
|
|
45
45
|
},
|
|
46
46
|
"peerDependencies": {
|
|
47
47
|
"@inglorious/store": "^12.9.6",
|
|
@@ -51,7 +51,7 @@
|
|
|
51
51
|
"@inglorious/eslint-config": "1.2.2",
|
|
52
52
|
"prettier": "^3.10.1",
|
|
53
53
|
"typescript": "~7.0.2",
|
|
54
|
-
"vite": "^8.4.
|
|
54
|
+
"vite": "^8.4.1",
|
|
55
55
|
"vitest": "^5.1.2"
|
|
56
56
|
},
|
|
57
57
|
"engines": {
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
const NO_TIME = 0
|
|
2
|
+
const DONE = 1
|
|
3
|
+
const LAST = -1
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* How long a timeline lasts: the sum of its phases' seconds.
|
|
7
|
+
*
|
|
8
|
+
* @param {Record<string, number>} phases - Each phase's name and seconds, in order.
|
|
9
|
+
* @returns {number} Seconds.
|
|
10
|
+
*/
|
|
11
|
+
export function durationOf(phases) {
|
|
12
|
+
return Object.values(phases).reduce((sum, seconds) => sum + seconds, NO_TIME)
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Where a timeline is after `time` seconds: which phase, and how far into it
|
|
17
|
+
* (`progress`, 0 to 1). Phases follow each other in the order they are written,
|
|
18
|
+
* so a cutscene reads as its script: `{ closeIn: 1, look: 0.5, pullBack: 1 }`.
|
|
19
|
+
* Past the end it stays on the last phase, finished, with `isDone` set.
|
|
20
|
+
*
|
|
21
|
+
* @param {Record<string, number>} phases - Each phase's name and seconds, in order.
|
|
22
|
+
* @param {number} time - Seconds since the timeline began.
|
|
23
|
+
* @returns {{ name: string, progress: number, isDone: boolean }}
|
|
24
|
+
*/
|
|
25
|
+
export function phaseAt(phases, time) {
|
|
26
|
+
const entries = Object.entries(phases)
|
|
27
|
+
let start = NO_TIME
|
|
28
|
+
for (const [name, seconds] of entries) {
|
|
29
|
+
if (time < start + seconds) {
|
|
30
|
+
const progress = Math.max(NO_TIME, (time - start) / seconds)
|
|
31
|
+
return { name, progress, isDone: false }
|
|
32
|
+
}
|
|
33
|
+
start += seconds
|
|
34
|
+
}
|
|
35
|
+
const [last] = entries.at(LAST) ?? []
|
|
36
|
+
return { name: last, progress: DONE, isDone: true }
|
|
37
|
+
}
|
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
|
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/** Where a timeline is: the phase, how far into it (0 to 1), and whether the timeline is over. */
|
|
2
|
+
export interface Phase {
|
|
3
|
+
name: string
|
|
4
|
+
progress: number
|
|
5
|
+
isDone: boolean
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
/** The seconds a timeline lasts: its phases' seconds added up. */
|
|
9
|
+
export function durationOf(phases: Record<string, number>): number
|
|
10
|
+
|
|
11
|
+
/** The phase a timeline of named phases, in order, is in after `time` seconds. */
|
|
12
|
+
export function phaseAt(phases: Record<string, number>, time: number): Phase
|
|
@@ -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>
|