@inglorious/engine 24.15.0 → 24.16.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@inglorious/engine",
3
- "version": "24.15.0",
3
+ "version": "24.16.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",
@@ -0,0 +1,269 @@
1
+ import { v } from "@inglorious/utils/v.js"
2
+
3
+ // A film grammar for story-driven games, from the AGAIN hackathon game: one still picture
4
+ // per shot, cuts that dip through black, a slow push-in on every shot, and title cards.
5
+ // Cheap, and it makes a change of place feel directed rather than swapped.
6
+ //
7
+ // - A dip to black fades the outgoing shot out and the next one in, with black in between:
8
+ // the fade-out/fade-in of film editing (Wikipedia, "Dissolve (filmmaking)", which covers
9
+ // fades to and from black: https://en.wikipedia.org/wiki/Dissolve_(filmmaking)).
10
+ // - A push-in is a slow zoom on a still, the Ken Burns effect (Wikipedia, "Ken Burns
11
+ // effect": "slow zooming and panning effects, and fading transitions between frames",
12
+ // https://en.wikipedia.org/wiki/Ken_Burns_effect).
13
+ // - A title card is a line of text shown over the picture for a moment, the intertitle of
14
+ // silent film (https://en.wikipedia.org/wiki/Intertitle).
15
+ //
16
+ // The state lives in entities, not in the renderer, so that it is saved, replayed and
17
+ // tested like everything else, and so that other entities can read it: a character drawn
18
+ // over the shot fades with it by reading `filmBlack`. Nothing here is random.
19
+ //
20
+ // Like every effect, a cut answers what happened in the game (`lifeStart`, `cardChosen`):
21
+ // the film's own type calls `cut` from its handlers. There is no `cut` event on purpose,
22
+ // for the reason given in `particles.js` for `burst`.
23
+ //
24
+ // Rejected: a crossfade (one shot dissolving into the next). Two pictures half-visible at
25
+ // once read as muddy for busy art, and it costs two full-screen draws per frame for the
26
+ // whole cut; the dip draws one.
27
+
28
+ const DEFAULT_FILM_ID = "film"
29
+ const FILM_TYPE = "Film"
30
+ const DEFAULT_CARD_ID = "titleCard"
31
+ const TITLE_CARD_TYPE = "TitleCard"
32
+ const NOTHING = 0
33
+ const WHOLE = 1
34
+ const HALF = 0.5
35
+ const TWICE = 2
36
+
37
+ // A dip to black lasting 0.6 s in all, as AGAIN's did: long enough to read as a cut,
38
+ // short enough not to hold up play, which is what a game's cuts must not do. Film editors
39
+ // use longer fades (a second or more) because nobody is waiting to press a button.
40
+ const DEFAULT_CUT_SECONDS = 0.6
41
+ // A 6% zoom over 12 s: slow enough to be felt rather than seen, as a Ken Burns move is. A
42
+ // larger zoom or a shorter time reads as the camera moving on purpose, which pulls the eye
43
+ // away from the play. After `pushSeconds` the shot holds.
44
+ const DEFAULT_PUSH_IN = 0.06
45
+ const DEFAULT_PUSH_SECONDS = 12
46
+ // A card stays 2 s: a short line read at a glance. It fades in quickly, so it lands with
47
+ // the moment it names, and out more slowly, so it does not vanish while being read.
48
+ const DEFAULT_CARD_SECONDS = 2
49
+ const CARD_FADE_IN = 0.25
50
+ const CARD_FADE_OUT = 0.5
51
+
52
+ /**
53
+ * An entity showing one shot at a time: a still picture filling its frame, slowly pushing
54
+ * in, that changes through black when the game calls `cut`. A shot is an image id, as
55
+ * entities name images (see `behaviors/images.js`); `null` is black.
56
+ *
57
+ * Pair it with `film()` and draw it with `renderFilm` from
58
+ * `@inglorious/renderer-2d/film.js`, which reads `position` as the frame's bottom left
59
+ * corner and `size` as its width and height (the whole game when there is none), and
60
+ * `focus` as the point the push-in closes on.
61
+ *
62
+ * @example
63
+ * ```js
64
+ * types: {
65
+ * Film: [
66
+ * { render: renderFilm },
67
+ * film(),
68
+ * { lifeStart(entity) { cut(entity, "bedroom") }, personDie(entity) { cut(entity, "grave") } },
69
+ * ],
70
+ * },
71
+ * entities: { film: createFilm({ layer: -20 }) },
72
+ * ```
73
+ *
74
+ * @param {Object} [options]
75
+ * @param {string} [options.id="film"]
76
+ * @param {number} [options.cutSeconds=0.6] - How long a cut lasts, out and in together.
77
+ * @param {number} [options.pushIn=0.06] - How much a shot zooms in: 0.06 is 6%.
78
+ * @param {number} [options.pushSeconds=12] - Over how long; then it holds.
79
+ * @returns {Object} The entity, of type `Film` unless `type` says otherwise, showing black.
80
+ * Any other option (`size`, `focus`, `layer`...) is kept on it for the renderer.
81
+ */
82
+ export function createFilm({
83
+ id = DEFAULT_FILM_ID,
84
+ cutSeconds = DEFAULT_CUT_SECONDS,
85
+ pushIn = DEFAULT_PUSH_IN,
86
+ pushSeconds = DEFAULT_PUSH_SECONDS,
87
+ ...rest
88
+ } = {}) {
89
+ return {
90
+ id,
91
+ type: FILM_TYPE,
92
+ position: v(NOTHING, NOTHING, NOTHING),
93
+ shot: null,
94
+ previous: null,
95
+ shotTime: NOTHING,
96
+ previousTime: NOTHING,
97
+ cutTime: cutSeconds,
98
+ cutSeconds,
99
+ pushIn,
100
+ pushSeconds,
101
+ ...rest,
102
+ }
103
+ }
104
+
105
+ /**
106
+ * The behaviour of a film entity: it keeps time for the shot and the cut. Shots change
107
+ * with `cut`, called by the film type's own handlers.
108
+ */
109
+ export function film() {
110
+ return {
111
+ update(entity, dt) {
112
+ entity.shotTime += dt
113
+ entity.cutTime += dt
114
+ },
115
+ }
116
+ }
117
+
118
+ /**
119
+ * Changes the shot through black: the shot on screen fades out over the first half of
120
+ * the cut, the new one fades in over the second. From black (the first shot, or after a
121
+ * cut to `null`) it only fades in; to `null` it only fades out. Cutting to the shot
122
+ * already showing does nothing, so a handler can call it without checking.
123
+ *
124
+ * @param {Object} entity - A film entity, from `createFilm`.
125
+ * @param {string | null} shot - An image id, or `null` for black.
126
+ */
127
+ export function cut(entity, shot) {
128
+ if (shot === entity.shot) return
129
+
130
+ entity.previous = entity.shot
131
+ entity.previousTime = entity.shotTime
132
+ entity.shot = shot
133
+ entity.shotTime = NOTHING
134
+ entity.cutTime = NOTHING
135
+ }
136
+
137
+ /**
138
+ * How black the frame is now, from 0 (the shot fully visible) to 1. Things drawn over the
139
+ * shot multiply their opacity by `1 - filmBlack(film)` to fade with it.
140
+ *
141
+ * @param {Object} entity - A film entity.
142
+ * @returns {number}
143
+ */
144
+ export function filmBlack(entity) {
145
+ const progress = entity.cutTime / entity.cutSeconds
146
+ if (progress >= WHOLE) return entity.shot === null ? WHOLE : NOTHING
147
+
148
+ // From black there is nothing to fade out: the whole cut fades in.
149
+ if (entity.previous === null) return WHOLE - progress
150
+
151
+ if (progress < HALF) return progress * TWICE
152
+ if (entity.shot === null) return WHOLE
153
+ return TWICE - progress * TWICE
154
+ }
155
+
156
+ /**
157
+ * The shot to draw now and how long it has been on screen: during the first half of a
158
+ * cut, the one going out, still pushing in; otherwise the current one.
159
+ *
160
+ * @param {Object} entity - A film entity.
161
+ * @returns {{ shot: string | null, time: number }}
162
+ */
163
+ export function shownShot(entity) {
164
+ const progress = entity.cutTime / entity.cutSeconds
165
+ if (progress < HALF && entity.previous !== null) {
166
+ return { shot: entity.previous, time: entity.previousTime + entity.cutTime }
167
+ }
168
+ return { shot: entity.shot, time: entity.shotTime }
169
+ }
170
+
171
+ /**
172
+ * How much a shot that has been on screen for `time` seconds is zoomed: 1 when it
173
+ * starts, growing evenly to `1 + pushIn` over `pushSeconds`, then holding.
174
+ *
175
+ * @param {Object} entity - A film entity.
176
+ * @param {number} time - Seconds, from `shownShot`.
177
+ * @returns {number}
178
+ */
179
+ export function pushZoom(entity, time) {
180
+ return WHOLE + entity.pushIn * Math.min(time / entity.pushSeconds, WHOLE)
181
+ }
182
+
183
+ /**
184
+ * An entity showing a title card for a moment: a title, and a smaller subtitle under it
185
+ * if there is one ("Life #3", "Age 18"). A separate entity from the film, so it can be
186
+ * drawn over what stands in front of the shot.
187
+ *
188
+ * Pair it with `titleCard()` and draw it with `renderTitleCard` from
189
+ * `@inglorious/renderer-2d/film.js`, centred on its `position`, with its `size` (the
190
+ * title's font size), `font`, `weight`, `color` and `shade` (the band behind the text).
191
+ *
192
+ * @example
193
+ * ```js
194
+ * types: {
195
+ * TitleCard: [
196
+ * { render: renderTitleCard },
197
+ * titleCard(),
198
+ * { lifeStart(entity, life) { showTitleCard(entity, `Life #${life.number}`, `Age ${life.age}`) } },
199
+ * ],
200
+ * },
201
+ * entities: { titleCard: createTitleCard({ position: v(640, 480, 0), layer: 10 }) },
202
+ * ```
203
+ *
204
+ * @param {Object} [options]
205
+ * @param {string} [options.id="titleCard"]
206
+ * @param {number} [options.cardSeconds=2] - How long a card stays, fades included.
207
+ * @returns {Object} The entity, of type `TitleCard` unless `type` says otherwise, showing
208
+ * nothing. Any other option is kept on it for the renderer.
209
+ */
210
+ export function createTitleCard({
211
+ id = DEFAULT_CARD_ID,
212
+ cardSeconds = DEFAULT_CARD_SECONDS,
213
+ ...rest
214
+ } = {}) {
215
+ return {
216
+ id,
217
+ type: TITLE_CARD_TYPE,
218
+ position: v(NOTHING, NOTHING, NOTHING),
219
+ title: null,
220
+ subtitle: null,
221
+ cardTime: cardSeconds,
222
+ cardSeconds,
223
+ ...rest,
224
+ }
225
+ }
226
+
227
+ /** The behaviour of a title card entity: it keeps time for the card showing. */
228
+ export function titleCard() {
229
+ return {
230
+ update(entity, dt) {
231
+ entity.cardTime += dt
232
+ },
233
+ }
234
+ }
235
+
236
+ /**
237
+ * Shows a card from now, replacing the one showing if any. Call it from the card type's
238
+ * own handlers.
239
+ *
240
+ * @param {Object} entity - A title card entity, from `createTitleCard`.
241
+ * @param {string} title
242
+ * @param {string | null} [subtitle=null]
243
+ */
244
+ export function showTitleCard(entity, title, subtitle = null) {
245
+ entity.title = title
246
+ entity.subtitle = subtitle
247
+ entity.cardTime = NOTHING
248
+ }
249
+
250
+ /**
251
+ * How opaque the card is now: it fades in over a quarter of a second, holds, and fades
252
+ * out over the last half second of `cardSeconds`. 0 when no card is showing.
253
+ *
254
+ * @param {Object} entity - A title card entity.
255
+ * @returns {number}
256
+ */
257
+ export function titleCardOpacity(entity) {
258
+ const { title, cardTime, cardSeconds } = entity
259
+ if (title === null || cardTime >= cardSeconds) return NOTHING
260
+
261
+ return Math.max(
262
+ NOTHING,
263
+ Math.min(
264
+ cardTime / CARD_FADE_IN,
265
+ WHOLE,
266
+ (cardSeconds - cardTime) / CARD_FADE_OUT,
267
+ ),
268
+ )
269
+ }
@@ -0,0 +1,124 @@
1
+ import type { Vector3 } from "@inglorious/utils/vector.js"
2
+
3
+ import type { Entity } from "../index"
4
+
5
+ /** An entity showing one still picture at a time, changing through black. */
6
+ export interface FilmEntity extends Omit<Entity, "size"> {
7
+ position: Vector3
8
+ /** The image id on screen, or `null` for black. */
9
+ shot: string | null
10
+ /** The shot before the last cut. */
11
+ previous: string | null
12
+ /** Seconds the shot has been on screen. */
13
+ shotTime: number
14
+ /** How long the previous shot had been on screen when it was cut. */
15
+ previousTime: number
16
+ /** Seconds since the last cut. */
17
+ cutTime: number
18
+ /** How long a cut lasts, out and in together. */
19
+ cutSeconds: number
20
+ /** How much a shot zooms in: 0.06 is 6%. */
21
+ pushIn: number
22
+ /** Over how long a shot zooms in, then holds. */
23
+ pushSeconds: number
24
+ /** The frame's width and height; the whole game without one. */
25
+ size?: Vector3 | readonly [width: number, height: number]
26
+ /** The point the push-in closes on, as fractions of the frame from its bottom left. */
27
+ focus?: readonly [x: number, y: number]
28
+ layer?: number
29
+ }
30
+
31
+ export interface FilmOptions {
32
+ /** Defaults to `"film"`. */
33
+ id?: string
34
+ type?: string
35
+ /** Defaults to 0.6. */
36
+ cutSeconds?: number
37
+ /** Defaults to 0.06. */
38
+ pushIn?: number
39
+ /** Defaults to 12. */
40
+ pushSeconds?: number
41
+ size?: Vector3 | readonly [width: number, height: number]
42
+ focus?: readonly [x: number, y: number]
43
+ layer?: number
44
+ [key: string]: unknown
45
+ }
46
+
47
+ /** A film entity showing black. */
48
+ export function createFilm(options?: FilmOptions): FilmEntity
49
+
50
+ /** Keeps time for the shot and the cut; shots change with `cut`. */
51
+ export function film(): {
52
+ update: (entity: FilmEntity, dt: number) => void
53
+ }
54
+
55
+ /** Changes the shot through black, from the film type's own handler. Same shot: nothing. */
56
+ export function cut(entity: FilmEntity, shot: string | null): void
57
+
58
+ /** How black the frame is now, from 0 (the shot fully visible) to 1. */
59
+ export function filmBlack(entity: FilmEntity): number
60
+
61
+ /** The shot to draw now (the outgoing one in the first half of a cut) and its time on screen. */
62
+ export function shownShot(entity: FilmEntity): {
63
+ shot: string | null
64
+ time: number
65
+ }
66
+
67
+ /** How much a shot on screen for `time` seconds is zoomed: from 1 to `1 + pushIn`. */
68
+ export function pushZoom(entity: FilmEntity, time: number): number
69
+
70
+ /**
71
+ * An entity showing a title and an optional subtitle for a moment. Its `size` is the
72
+ * title's font size, as for a text entity.
73
+ */
74
+ export interface TitleCardEntity extends Omit<Entity, "size"> {
75
+ position: Vector3
76
+ title: string | null
77
+ subtitle: string | null
78
+ /** Seconds since the card was shown. */
79
+ cardTime: number
80
+ /** How long a card stays, fades included. */
81
+ cardSeconds: number
82
+ /** The title's font size. */
83
+ size?: number
84
+ font?: string
85
+ weight?: string
86
+ color?: string
87
+ /** The band behind the text; `null` for none. */
88
+ shade?: string | null
89
+ layer?: number
90
+ }
91
+
92
+ export interface TitleCardOptions {
93
+ /** Defaults to `"titleCard"`. */
94
+ id?: string
95
+ type?: string
96
+ /** Defaults to 2. */
97
+ cardSeconds?: number
98
+ position?: Vector3
99
+ size?: number
100
+ font?: string
101
+ weight?: string
102
+ color?: string
103
+ shade?: string | null
104
+ layer?: number
105
+ [key: string]: unknown
106
+ }
107
+
108
+ /** A title card entity showing nothing. */
109
+ export function createTitleCard(options?: TitleCardOptions): TitleCardEntity
110
+
111
+ /** Keeps time for the card showing. */
112
+ export function titleCard(): {
113
+ update: (entity: TitleCardEntity, dt: number) => void
114
+ }
115
+
116
+ /** Shows a card from now, from the card type's own handler. */
117
+ export function showTitleCard(
118
+ entity: TitleCardEntity,
119
+ title: string,
120
+ subtitle?: string | null,
121
+ ): void
122
+
123
+ /** How opaque the card is now: in over 0.25 s, out over the last 0.5 s; 0 when none shows. */
124
+ export function titleCardOpacity(entity: TitleCardEntity): number