@inglorious/engine 24.6.0 → 24.8.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/behaviors/recorded.js +172 -0
- package/src/core/engine.js +12 -0
- package/types/behaviors/recorded.d.ts +59 -0
- package/types/core/engine.d.ts +3 -0
package/README.md
CHANGED
|
@@ -286,9 +286,9 @@ A renderer checks the entity it draws: `renderText` wants a font size, so a type
|
|
|
286
286
|
|
|
287
287
|
## Documentation
|
|
288
288
|
|
|
289
|
-
The best way to get started is with the official documentation, which includes a **[Quick Start Guide](https://inglorious
|
|
289
|
+
The best way to get started is with the official documentation, which includes a **[Quick Start Guide](https://inglorious.dev/engine/?path=/docs/quick-start--docs)**.
|
|
290
290
|
|
|
291
|
-
Full documentation is available at: **[https://inglorious
|
|
291
|
+
Full documentation is available at: **[https://inglorious.dev/engine/](https://inglorious.dev/engine/)**
|
|
292
292
|
|
|
293
293
|
## Why Functional Programming?
|
|
294
294
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@inglorious/engine",
|
|
3
|
-
"version": "24.
|
|
3
|
+
"version": "24.8.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",
|
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
"url": "git+https://github.com/IngloriousCoderz/inglorious-forge.git",
|
|
10
10
|
"directory": "packages/engine"
|
|
11
11
|
},
|
|
12
|
-
"homepage": "https://inglorious
|
|
12
|
+
"homepage": "https://inglorious.dev/engine/",
|
|
13
13
|
"bugs": {
|
|
14
14
|
"url": "https://github.com/IngloriousCoderz/inglorious-forge/issues"
|
|
15
15
|
},
|
|
@@ -41,7 +41,7 @@
|
|
|
41
41
|
},
|
|
42
42
|
"sideEffects": false,
|
|
43
43
|
"dependencies": {
|
|
44
|
-
"@inglorious/utils": "^5.
|
|
44
|
+
"@inglorious/utils": "^5.8.0"
|
|
45
45
|
},
|
|
46
46
|
"peerDependencies": {
|
|
47
47
|
"@inglorious/store": "^12.9.6",
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createRecorder,
|
|
3
|
+
isSession,
|
|
4
|
+
} from "@inglorious/utils/data-structures/recorder.js"
|
|
5
|
+
import { saveJSON } from "@inglorious/utils/files.js"
|
|
6
|
+
|
|
7
|
+
const NO_TURNS = 0
|
|
8
|
+
const STAMP_START = 0
|
|
9
|
+
const STAMP_LENGTH = 19 // "2026-12-01T13:42:32", to the second
|
|
10
|
+
|
|
11
|
+
// One recorder per entity: kept outside the state, which the store copies on every
|
|
12
|
+
// event, while a recording grows all game long.
|
|
13
|
+
const recorders = new Map()
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Records an entity that holds a deterministic simulation, turned on `tick` (see the
|
|
17
|
+
* `ticks` system), and plays a recording back: runs with what the player did at each
|
|
18
|
+
* turn (see `createRecorder`). Put it last in the type's behaviors, so it wraps the
|
|
19
|
+
* `tick` and the events that start a run.
|
|
20
|
+
*
|
|
21
|
+
* What the simulation is stays the game's, told through the options:
|
|
22
|
+
*
|
|
23
|
+
* - `turn(entity)`: the simulation's turn, or null while there is no simulation. A
|
|
24
|
+
* `tick` that leaves the turn as it was (the game held it still) records nothing.
|
|
25
|
+
* - `state(entity)`: the simulation's state, whose fingerprint tells whether a replay
|
|
26
|
+
* ended where the recording did.
|
|
27
|
+
* - `about(entity)`: what it takes to start the simulation again, as plain data.
|
|
28
|
+
* - `input(entity)`: what the player did before this turn, read before `tick`; null
|
|
29
|
+
* for nothing.
|
|
30
|
+
* - `useInput(entity, input)`: puts a recorded input in place of the player's before
|
|
31
|
+
* `tick`; `undefined` where nothing was done.
|
|
32
|
+
* - `replayRun(entity, about)`: starts the simulation of a recorded run.
|
|
33
|
+
* - `startsRun`: the events after which a new run starts (a level started again, a
|
|
34
|
+
* new seed); the one before ends where it was. Ignored while replaying, so a replay
|
|
35
|
+
* is not knocked off its runs. `create` always starts one.
|
|
36
|
+
*
|
|
37
|
+
* It handles `saveRecording` (the session so far, downloaded as a JSON file),
|
|
38
|
+
* `replayLoad` (a session to play back, typically a file dropped on the page: see
|
|
39
|
+
* `onDroppedJSON` and `engine.notify`; on success it notifies `replayStart`, for the
|
|
40
|
+
* game to leave its menus) and `replayStop`. While replaying, `entity.replay` is
|
|
41
|
+
* `{ run, runs, diverged, over }`, else null; once `over`, `tick` holds the simulation
|
|
42
|
+
* still, and the player's input is still replaced by none.
|
|
43
|
+
*
|
|
44
|
+
* @param {Object} options
|
|
45
|
+
* @param {(entity: Object) => number | null} options.turn
|
|
46
|
+
* @param {(entity: Object) => *} options.state
|
|
47
|
+
* @param {(entity: Object) => *} options.about
|
|
48
|
+
* @param {(entity: Object) => *} options.input
|
|
49
|
+
* @param {(entity: Object, input: *) => void} options.useInput
|
|
50
|
+
* @param {(entity: Object, about: *) => void} options.replayRun
|
|
51
|
+
* @param {string[]} [options.startsRun] - Events that start a new run, besides `create`.
|
|
52
|
+
* @param {(session: Object) => boolean} [options.isPlayable] - Whether the game can play a session's runs (its levels still exist).
|
|
53
|
+
* @param {string} [options.format] - What the recordings are; a session of another format is refused.
|
|
54
|
+
* @param {string} [options.build] - Which build of the game made them.
|
|
55
|
+
* @param {string} [options.name] - The start of a saved file's name, followed by the time.
|
|
56
|
+
* @returns {Function} The behavior.
|
|
57
|
+
*/
|
|
58
|
+
export function recorded({
|
|
59
|
+
turn,
|
|
60
|
+
state,
|
|
61
|
+
about,
|
|
62
|
+
input,
|
|
63
|
+
useInput,
|
|
64
|
+
replayRun,
|
|
65
|
+
startsRun = [],
|
|
66
|
+
isPlayable = () => true,
|
|
67
|
+
format = "recording",
|
|
68
|
+
build,
|
|
69
|
+
name = format,
|
|
70
|
+
}) {
|
|
71
|
+
const recorderOf = (entity) => recorders.get(entity.id)
|
|
72
|
+
|
|
73
|
+
/** Ends the run being recorded where the simulation is. */
|
|
74
|
+
function close(entity) {
|
|
75
|
+
recorderOf(entity).close(turn(entity) ?? NO_TURNS, state(entity))
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** Starts recording a run, if there is a simulation to record. */
|
|
79
|
+
function open(entity) {
|
|
80
|
+
if (turn(entity) != null) recorderOf(entity).open(about(entity))
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function play(entity, run) {
|
|
84
|
+
replayRun(entity, run.about)
|
|
85
|
+
entity.replay = recorderOf(entity).progress()
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
return (type) => {
|
|
89
|
+
const runStarters = Object.fromEntries(
|
|
90
|
+
startsRun.map((event) => [
|
|
91
|
+
event,
|
|
92
|
+
(entity, payload, api) => {
|
|
93
|
+
if (entity.replay) return
|
|
94
|
+
close(entity)
|
|
95
|
+
type[event]?.(entity, payload, api)
|
|
96
|
+
open(entity)
|
|
97
|
+
},
|
|
98
|
+
]),
|
|
99
|
+
)
|
|
100
|
+
|
|
101
|
+
return {
|
|
102
|
+
...runStarters,
|
|
103
|
+
|
|
104
|
+
create(entity, payload, api) {
|
|
105
|
+
recorders.set(entity.id, createRecorder({ format, build }))
|
|
106
|
+
entity.replay = null
|
|
107
|
+
type.create?.(entity, payload, api)
|
|
108
|
+
open(entity)
|
|
109
|
+
},
|
|
110
|
+
|
|
111
|
+
tick(entity, dt, api) {
|
|
112
|
+
if (entity.replay?.over) {
|
|
113
|
+
useInput(entity, undefined) // the player's input is dropped, not saved up
|
|
114
|
+
return
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
const before = turn(entity)
|
|
118
|
+
if (before == null) {
|
|
119
|
+
type.tick?.(entity, dt, api)
|
|
120
|
+
return
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
const recorder = recorderOf(entity)
|
|
124
|
+
const done = entity.replay ? null : input(entity)
|
|
125
|
+
if (entity.replay) useInput(entity, recorder.input(before))
|
|
126
|
+
|
|
127
|
+
type.tick?.(entity, dt, api)
|
|
128
|
+
|
|
129
|
+
const after = turn(entity)
|
|
130
|
+
if (after === before) return
|
|
131
|
+
if (!entity.replay) {
|
|
132
|
+
recorder.record(before, done)
|
|
133
|
+
return
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
if (after < recorder.replayed().ticks) return
|
|
137
|
+
const next = recorder.finish(state(entity))
|
|
138
|
+
if (next) play(entity, next)
|
|
139
|
+
else entity.replay = { ...recorder.progress(), over: true }
|
|
140
|
+
},
|
|
141
|
+
|
|
142
|
+
saveRecording(entity, payload, api) {
|
|
143
|
+
type.saveRecording?.(entity, payload, api)
|
|
144
|
+
const session = recorderOf(entity).session(
|
|
145
|
+
turn(entity) ?? NO_TURNS,
|
|
146
|
+
state(entity),
|
|
147
|
+
)
|
|
148
|
+
const stamp = session.recordedAt
|
|
149
|
+
.slice(STAMP_START, STAMP_LENGTH)
|
|
150
|
+
.replaceAll(":", "-")
|
|
151
|
+
saveJSON(`${name}-${stamp}.json`, session)
|
|
152
|
+
},
|
|
153
|
+
|
|
154
|
+
replayLoad(entity, session, api) {
|
|
155
|
+
type.replayLoad?.(entity, session, api)
|
|
156
|
+
if (!isSession(session, format) || !isPlayable(session)) {
|
|
157
|
+
console.warn(`Not a ${format} this game can play back`)
|
|
158
|
+
return
|
|
159
|
+
}
|
|
160
|
+
close(entity)
|
|
161
|
+
play(entity, recorderOf(entity).load(session))
|
|
162
|
+
api.notify("replayStart")
|
|
163
|
+
},
|
|
164
|
+
|
|
165
|
+
replayStop(entity, payload, api) {
|
|
166
|
+
type.replayStop?.(entity, payload, api)
|
|
167
|
+
recorderOf(entity).stop()
|
|
168
|
+
entity.replay = null
|
|
169
|
+
},
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
}
|
package/src/core/engine.js
CHANGED
|
@@ -124,6 +124,18 @@ export class Engine {
|
|
|
124
124
|
return this._store.getState()
|
|
125
125
|
}
|
|
126
126
|
|
|
127
|
+
/**
|
|
128
|
+
* Tells the game something from outside it, as its own entities would with
|
|
129
|
+
* `api.notify`: a file dropped on the page, a message from the host. The event is
|
|
130
|
+
* handled in the next frame, with the others.
|
|
131
|
+
*
|
|
132
|
+
* @param {string} type - The event.
|
|
133
|
+
* @param {*} [payload] - What comes with it.
|
|
134
|
+
*/
|
|
135
|
+
notify(type, payload) {
|
|
136
|
+
this._store.notify(type, payload)
|
|
137
|
+
}
|
|
138
|
+
|
|
127
139
|
/**
|
|
128
140
|
* Updates the game state.
|
|
129
141
|
*
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
RecordedSession,
|
|
3
|
+
ReplayProgress,
|
|
4
|
+
} from "@inglorious/utils/data-structures/recorder"
|
|
5
|
+
|
|
6
|
+
import type { Decorator, Entity, Handler } from "../index"
|
|
7
|
+
|
|
8
|
+
export interface RecordedEntity extends Entity {
|
|
9
|
+
/** Where a replay is, and whether it is over; null when not replaying. */
|
|
10
|
+
replay?: (ReplayProgress & { over?: boolean }) | null
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export interface RecordedOptions<
|
|
14
|
+
TEntity extends RecordedEntity = RecordedEntity,
|
|
15
|
+
About = unknown,
|
|
16
|
+
Input = unknown,
|
|
17
|
+
> {
|
|
18
|
+
/** The simulation's turn, or null while there is none. */
|
|
19
|
+
turn(entity: TEntity): number | null
|
|
20
|
+
/** The simulation's state, fingerprinted where a run ends. */
|
|
21
|
+
state(entity: TEntity): unknown
|
|
22
|
+
/** What it takes to start the simulation again. */
|
|
23
|
+
about(entity: TEntity): About
|
|
24
|
+
/** What the player did before this turn; null for nothing. */
|
|
25
|
+
input(entity: TEntity): Input | null
|
|
26
|
+
/** Puts a recorded input in place of the player's; undefined where nothing was done. */
|
|
27
|
+
useInput(entity: TEntity, input: Input | undefined): void
|
|
28
|
+
/** Starts the simulation of a recorded run. */
|
|
29
|
+
replayRun(entity: TEntity, about: About): void
|
|
30
|
+
/** Events after which a new run starts, besides `create`. */
|
|
31
|
+
startsRun?: string[]
|
|
32
|
+
/** Whether the game can play a session's runs. */
|
|
33
|
+
isPlayable?(session: RecordedSession<About, Input>): boolean
|
|
34
|
+
format?: string
|
|
35
|
+
build?: string
|
|
36
|
+
/** The start of a saved file's name. */
|
|
37
|
+
name?: string
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Records an entity holding a deterministic simulation turned on `tick`, and plays
|
|
42
|
+
* recordings back. Put it last in the type's behaviors.
|
|
43
|
+
*/
|
|
44
|
+
export function recorded<
|
|
45
|
+
TEntity extends RecordedEntity = RecordedEntity,
|
|
46
|
+
About = unknown,
|
|
47
|
+
Input = unknown,
|
|
48
|
+
>(
|
|
49
|
+
options: RecordedOptions<TEntity, About, Input>,
|
|
50
|
+
): Decorator<
|
|
51
|
+
// and a handler for each of `startsRun`
|
|
52
|
+
Record<string, Handler<TEntity, unknown>> & {
|
|
53
|
+
create: Handler<TEntity, unknown>
|
|
54
|
+
tick: Handler<TEntity, number>
|
|
55
|
+
saveRecording: Handler<TEntity, unknown>
|
|
56
|
+
replayLoad: Handler<TEntity, RecordedSession<About, Input>>
|
|
57
|
+
replayStop: Handler<TEntity, unknown>
|
|
58
|
+
}
|
|
59
|
+
>
|
package/types/core/engine.d.ts
CHANGED
|
@@ -23,6 +23,9 @@ export class Engine {
|
|
|
23
23
|
/** The entities as they are now. */
|
|
24
24
|
getState(): EntitiesState
|
|
25
25
|
|
|
26
|
+
/** Tells the game something from outside it; handled in the next frame. */
|
|
27
|
+
notify(type: string, payload?: unknown): void
|
|
28
|
+
|
|
26
29
|
/** Runs a frame, `dt` seconds after the last one. */
|
|
27
30
|
update(dt: number): void
|
|
28
31
|
}
|