@inglorious/engine 24.3.0 → 24.5.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 +1 -1
- package/package.json +2 -2
- package/src/behaviors/input/hover.js +24 -4
- package/src/behaviors/ui/button.js +53 -9
- package/src/collision/detection.js +4 -3
- package/src/core/draw-order.js +22 -0
- package/src/systems/ticks.js +43 -15
- package/types/behaviors/ui/button.d.ts +8 -1
- package/types/core/draw-order.d.ts +5 -0
- package/types/index.d.ts +2 -0
- package/types/systems/ticks.d.ts +8 -3
package/README.md
CHANGED
|
@@ -8,7 +8,7 @@ A JavaScript game engine written with global state, immutability, and pure funct
|
|
|
8
8
|
|
|
9
9
|
The fastest way to start a new Inglorious Engine project is by using the official scaffolding tool.
|
|
10
10
|
|
|
11
|
-
Run the following command in your terminal and follow the prompts to create a new game from a variety of templates
|
|
11
|
+
Run the following command in your terminal and follow the prompts to create a new game from a variety of templates. IngloriousScript, JavaScript with vector math, is the default; plain JavaScript, TypeScript and IngloriousScript with TypeScript are there too:
|
|
12
12
|
|
|
13
13
|
```bash
|
|
14
14
|
pnpm create @inglorious/game
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@inglorious/engine",
|
|
3
|
-
"version": "24.
|
|
3
|
+
"version": "24.5.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.6.0"
|
|
45
45
|
},
|
|
46
46
|
"peerDependencies": {
|
|
47
47
|
"@inglorious/store": "^12.9.5",
|
|
@@ -6,8 +6,10 @@ const DEFAULT_CURSOR = "arrow"
|
|
|
6
6
|
/**
|
|
7
7
|
* A pointer that turns into a hand over anything there is room to press.
|
|
8
8
|
*
|
|
9
|
-
* Anything with a hitbox is something a press would
|
|
10
|
-
* over one of those is the game saying "this does
|
|
9
|
+
* Anything with a hitbox that answers `entityPress` is something a press would do
|
|
10
|
+
* something to, so the pointer going over one of those is the game saying "this does
|
|
11
|
+
* something" the way a desktop does. A hitbox that only blocks, like a veil behind a
|
|
12
|
+
* dialog, keeps the arrow.
|
|
11
13
|
* Written as a decorator rather than as part of the mouse itself, so a game that has
|
|
12
14
|
* nothing to press -- or a mouse that is meant to stay a crosshair whatever it is
|
|
13
15
|
* over -- simply leaves it out.
|
|
@@ -44,7 +46,9 @@ export function hover({ off = DEFAULT_CURSOR } = {}) {
|
|
|
44
46
|
}
|
|
45
47
|
|
|
46
48
|
/**
|
|
47
|
-
* Puts the hand up or takes it down again
|
|
49
|
+
* Puts the hand up or takes it down again, and tells what it went over and off:
|
|
50
|
+
* `entityHoverStart` and `entityHoverEnd` with the entity's id, which `button()` turns
|
|
51
|
+
* into `hovered` so it can light up.
|
|
48
52
|
*
|
|
49
53
|
* Something pressable under the pointer, or something pressed whatever the pointer has
|
|
50
54
|
* wandered off to since: a drag that started on a control should not flicker back
|
|
@@ -53,5 +57,21 @@ export function hover({ off = DEFAULT_CURSOR } = {}) {
|
|
|
53
57
|
function point(entity, api, off) {
|
|
54
58
|
const held = entity.pressedId ?? findCollision(entity, api.getEntities())?.id
|
|
55
59
|
|
|
56
|
-
entity.cursor = held ? HAND : off
|
|
60
|
+
entity.cursor = held && answersPresses(held, api) ? HAND : off
|
|
61
|
+
|
|
62
|
+
if (held === entity.hoveredId) return
|
|
63
|
+
if (entity.hoveredId) api.notify("entityHoverEnd", entity.hoveredId)
|
|
64
|
+
if (held) api.notify("entityHoverStart", held)
|
|
65
|
+
entity.hoveredId = held
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Whether pressing an entity does anything: its type answers `entityPress`. Something
|
|
70
|
+
* can be in the way of the pointer without being pressable, such as a veil behind a
|
|
71
|
+
* dialog that stops clicks reaching the scene, and a hand over it would promise a press
|
|
72
|
+
* that does nothing.
|
|
73
|
+
*/
|
|
74
|
+
function answersPresses(id, api) {
|
|
75
|
+
const entity = api.getEntity(id)
|
|
76
|
+
return Boolean(entity && api.getType(entity.type)?.entityPress)
|
|
57
77
|
}
|
|
@@ -10,23 +10,67 @@ import { fsm } from "@inglorious/store/behaviors/state-machine/fsm.js"
|
|
|
10
10
|
* It announces that it was pressed the way any state machine does -- through
|
|
11
11
|
* `stateChange` -- so whatever the button is *for* is said elsewhere rather than here:
|
|
12
12
|
* a button knows how it looks, not what it does.
|
|
13
|
+
*
|
|
14
|
+
* While the pointer is over it, `hovered` is true (from `hover()` on the mouse), so it
|
|
15
|
+
* can light up.
|
|
16
|
+
*
|
|
17
|
+
* A finger presses it too, from `entityTouchStart` to `entityTouchEnd`: give it a
|
|
18
|
+
* `touch` collision as well as its `hitbox`, since that is what the touch looks for.
|
|
13
19
|
*/
|
|
14
20
|
export function button() {
|
|
15
21
|
return fsm({
|
|
16
22
|
default: {
|
|
17
|
-
entityPress
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
entity.state = "pressed"
|
|
21
|
-
},
|
|
23
|
+
entityPress: press,
|
|
24
|
+
entityTouchStart: press,
|
|
25
|
+
...HOVER,
|
|
22
26
|
},
|
|
23
27
|
|
|
24
28
|
pressed: {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
entity
|
|
29
|
+
...HOVER,
|
|
30
|
+
entityRelease: release,
|
|
31
|
+
entityTouchEnd(entity, { targetId }) {
|
|
32
|
+
release(entity, targetId)
|
|
29
33
|
},
|
|
30
34
|
},
|
|
31
35
|
})
|
|
32
36
|
}
|
|
37
|
+
|
|
38
|
+
function press(entity, id) {
|
|
39
|
+
if (id !== entity.id) return
|
|
40
|
+
|
|
41
|
+
entity.state = "pressed"
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function release(entity, id) {
|
|
45
|
+
if (id !== entity.id) return
|
|
46
|
+
|
|
47
|
+
entity.state = "default"
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* What a button is for: notifies its `action` when it is pressed. A behaviour next to
|
|
52
|
+
* `button()`, which only knows how a button looks.
|
|
53
|
+
*
|
|
54
|
+
* @example
|
|
55
|
+
* ```js
|
|
56
|
+
* types: { Button: [button(), pressable(), { render: renderButton }] }
|
|
57
|
+
* entities: { pause: { type: "Button", action: "togglePause", label: "Pause" } }
|
|
58
|
+
* ```
|
|
59
|
+
*/
|
|
60
|
+
export function pressable() {
|
|
61
|
+
return {
|
|
62
|
+
stateChange(entity, { entityId, to }, api) {
|
|
63
|
+
if (entityId === entity.id && to === "pressed") api.notify(entity.action)
|
|
64
|
+
},
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const HOVER = {
|
|
69
|
+
entityHoverStart(entity, id) {
|
|
70
|
+
if (id === entity.id) entity.hovered = true
|
|
71
|
+
},
|
|
72
|
+
|
|
73
|
+
entityHoverEnd(entity, id) {
|
|
74
|
+
if (id === entity.id) entity.hovered = false
|
|
75
|
+
},
|
|
76
|
+
}
|
|
@@ -7,9 +7,9 @@ import * as segment from "@inglorious/utils/math/segment.js"
|
|
|
7
7
|
import { filter } from "@inglorious/utils/object.js"
|
|
8
8
|
import { add, zero } from "@inglorious/utils/vector.js"
|
|
9
9
|
|
|
10
|
+
import { drawOrder } from "../core/draw-order.js"
|
|
10
11
|
import { anchorOffset, shapeAnchor } from "../physics/anchor.js"
|
|
11
12
|
|
|
12
|
-
const Z = 2
|
|
13
13
|
const RECTANGLE = "rectangle"
|
|
14
14
|
|
|
15
15
|
const Shape = {
|
|
@@ -22,7 +22,8 @@ const Shape = {
|
|
|
22
22
|
}
|
|
23
23
|
|
|
24
24
|
/**
|
|
25
|
-
* Finds the first collision between a point and a list of entities
|
|
25
|
+
* Finds the first collision between a point and a list of entities: the one drawn on
|
|
26
|
+
* top, when several overlap (the reverse of `drawOrder`).
|
|
26
27
|
*
|
|
27
28
|
* @param {Point} entity - The point to check for collisions.
|
|
28
29
|
* @param {Options} options - Options for collision detection.
|
|
@@ -35,7 +36,7 @@ export function findCollision(entity, entities, collisionGroup = "hitbox") {
|
|
|
35
36
|
)
|
|
36
37
|
|
|
37
38
|
return Object.values(otherEntities)
|
|
38
|
-
.toSorted((a, b) => a
|
|
39
|
+
.toSorted((a, b) => drawOrder(b, a))
|
|
39
40
|
.find((target) => collidesWith(entity, target, collisionGroup))
|
|
40
41
|
}
|
|
41
42
|
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
const DEFAULT_LAYER = 0
|
|
2
|
+
const Y = 1
|
|
3
|
+
const Z = 2
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* The order things are drawn in, as a comparator: by `layer`, then from low to high
|
|
7
|
+
* (`y`), then from the back of the scene to the front (high `z` first).
|
|
8
|
+
*
|
|
9
|
+
* Shared by the renderers, which draw in this order, and by hit testing, which looks in
|
|
10
|
+
* the reverse one: what the pointer lands on is what is drawn on top.
|
|
11
|
+
*
|
|
12
|
+
* @param {{ layer?: number, position: number[] }} a
|
|
13
|
+
* @param {{ layer?: number, position: number[] }} b
|
|
14
|
+
* @returns {number}
|
|
15
|
+
*/
|
|
16
|
+
export function drawOrder(a, b) {
|
|
17
|
+
return (
|
|
18
|
+
(a.layer ?? DEFAULT_LAYER) - (b.layer ?? DEFAULT_LAYER) ||
|
|
19
|
+
a.position[Y] - b.position[Y] ||
|
|
20
|
+
b.position[Z] - a.position[Z]
|
|
21
|
+
)
|
|
22
|
+
}
|
package/src/systems/ticks.js
CHANGED
|
@@ -1,25 +1,42 @@
|
|
|
1
|
-
import { dueAt } from "../animation/due.js"
|
|
2
|
-
|
|
3
1
|
const ONE_SECOND = 1
|
|
2
|
+
const DEFAULT_MAX_PER_FRAME = 64
|
|
3
|
+
const NORMAL_SPEED = 1
|
|
4
|
+
const NO_TIME = 0
|
|
5
|
+
// Summed frame times fall a hair short of the interval they should reach.
|
|
6
|
+
const WITHIN = 1e-9
|
|
4
7
|
|
|
5
8
|
/**
|
|
6
9
|
* A clock for a whole world that turns at its own rate: `atFps` for a world
|
|
7
|
-
* rather than an entity.
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
10
|
+
* rather than an entity. Whatever turns with the world handles `tick` (or
|
|
11
|
+
* `event`) instead of `update`, and is left alone between turns while the loop
|
|
12
|
+
* keeps the pointer and controls at screen rate.
|
|
13
|
+
*
|
|
14
|
+
* Each turn covers exactly one interval, `1 / fps` seconds, and is notified on
|
|
15
|
+
* its own: time left over carries on to the next frame, so the world turns at
|
|
16
|
+
* its rate whatever the screen's, and a frame that covers several intervals
|
|
17
|
+
* turns several times. At most `maxPerFrame` turns happen in one frame; a world
|
|
18
|
+
* that cannot keep up drops the time it is behind rather than racing to catch
|
|
19
|
+
* up for ever.
|
|
20
|
+
*
|
|
21
|
+
* `game.timeScale` (1 when absent) is how fast the clock runs: 4 turns the world
|
|
22
|
+
* four times as often, 0 holds it. It hears `pause` and `resume` itself, since
|
|
23
|
+
* the store still asks systems for `update` while halted.
|
|
12
24
|
*
|
|
13
25
|
* @param {Object} [options]
|
|
14
|
-
* @param {number} [options.fps=Infinity] - Turns a second. `Infinity` for one a frame.
|
|
26
|
+
* @param {number} [options.fps=Infinity] - Turns a second. `Infinity` for one a frame, covering the frame.
|
|
15
27
|
* @param {string} [options.event="tick"] - What a turn is called.
|
|
28
|
+
* @param {number} [options.maxPerFrame=64] - The most turns in one frame.
|
|
16
29
|
* @returns {Object} The system.
|
|
17
30
|
*/
|
|
18
|
-
export function ticks({
|
|
19
|
-
|
|
31
|
+
export function ticks({
|
|
32
|
+
fps = Infinity,
|
|
33
|
+
event = "tick",
|
|
34
|
+
maxPerFrame = DEFAULT_MAX_PER_FRAME,
|
|
35
|
+
} = {}) {
|
|
36
|
+
const interval = ONE_SECOND / fps
|
|
20
37
|
|
|
21
38
|
let halted = false
|
|
22
|
-
let clock =
|
|
39
|
+
let clock = NO_TIME
|
|
23
40
|
|
|
24
41
|
return {
|
|
25
42
|
pause() {
|
|
@@ -33,13 +50,24 @@ export function ticks({ fps = Infinity, event = "tick" } = {}) {
|
|
|
33
50
|
update(entities, dt, api) {
|
|
34
51
|
if (halted) return
|
|
35
52
|
|
|
36
|
-
const
|
|
53
|
+
const timeScale = entities.game?.timeScale ?? NORMAL_SPEED
|
|
54
|
+
const covered = dt * timeScale
|
|
55
|
+
|
|
56
|
+
if (!interval) {
|
|
57
|
+
if (covered) api.notify(event, covered)
|
|
58
|
+
return
|
|
59
|
+
}
|
|
37
60
|
|
|
38
|
-
clock
|
|
61
|
+
clock += covered
|
|
39
62
|
|
|
40
|
-
|
|
63
|
+
let turns = 0
|
|
64
|
+
while (clock + interval * WITHIN >= interval && turns < maxPerFrame) {
|
|
65
|
+
api.notify(event, interval)
|
|
66
|
+
clock -= interval
|
|
67
|
+
turns++
|
|
68
|
+
}
|
|
41
69
|
|
|
42
|
-
|
|
70
|
+
if (turns === maxPerFrame) clock = NO_TIME
|
|
43
71
|
},
|
|
44
72
|
}
|
|
45
73
|
}
|
|
@@ -2,7 +2,14 @@ import type { Decorator, Entity, GameType } from "../../index"
|
|
|
2
2
|
|
|
3
3
|
export interface ButtonEntity extends Entity {
|
|
4
4
|
state?: "default" | "pressed"
|
|
5
|
+
/** True while the pointer is over it. */
|
|
6
|
+
hovered?: boolean
|
|
7
|
+
/** What `pressable()` notifies when it is pressed. */
|
|
8
|
+
action?: string
|
|
5
9
|
}
|
|
6
10
|
|
|
7
|
-
/** Looks pressed from `entityPress` to `entityRelease
|
|
11
|
+
/** Looks pressed from `entityPress` to `entityRelease` (or `entityTouchStart` to `entityTouchEnd`), and announces it through `stateChange`. */
|
|
8
12
|
export function button(): Decorator<GameType<ButtonEntity>>
|
|
13
|
+
|
|
14
|
+
/** Notifies the button's `action` when it is pressed. */
|
|
15
|
+
export function pressable(): GameType<ButtonEntity>
|
package/types/index.d.ts
CHANGED
|
@@ -64,6 +64,8 @@ export interface GameEntity extends BaseEntity {
|
|
|
64
64
|
/** Draws collision gizmos; `C` toggles it. */
|
|
65
65
|
debug?: boolean
|
|
66
66
|
paused?: boolean
|
|
67
|
+
/** How fast the `ticks` clock runs: 1 when absent, 4 four times as fast, 0 holds it. */
|
|
68
|
+
timeScale?: number
|
|
67
69
|
/** Stops the engine after the current frame. */
|
|
68
70
|
quit?: boolean
|
|
69
71
|
/** Draws images without smoothing. */
|
package/types/systems/ticks.d.ts
CHANGED
|
@@ -1,12 +1,17 @@
|
|
|
1
1
|
import type { GameSystem } from "../index"
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
|
-
* Notifies `event`
|
|
5
|
-
*
|
|
4
|
+
* Notifies `event` `fps` times a second, for a world that turns at its own rate.
|
|
5
|
+
* Each turn covers exactly `1 / fps` seconds; leftover time carries on to the
|
|
6
|
+
* next frame, and a long frame turns several times, up to `maxPerFrame`.
|
|
7
|
+
* `game.timeScale` speeds the clock up or slows it down. Stops on `pause`,
|
|
8
|
+
* starts again on `resume`.
|
|
6
9
|
*/
|
|
7
10
|
export function ticks(options?: {
|
|
8
|
-
/** Defaults to `Infinity
|
|
11
|
+
/** Defaults to `Infinity`: one turn a frame, covering the frame. */
|
|
9
12
|
fps?: number
|
|
10
13
|
/** Defaults to `"tick"`. */
|
|
11
14
|
event?: string
|
|
15
|
+
/** Defaults to 64. Time beyond it is dropped rather than caught up on. */
|
|
16
|
+
maxPerFrame?: number
|
|
12
17
|
}): GameSystem
|