@inglorious/engine 24.10.0 → 24.11.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.
|
|
3
|
+
"version": "24.11.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
|
+
}
|
|
@@ -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
|