@inglorious/engine 24.0.0 → 24.1.1
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 +4 -4
- package/src/ai/movement/dynamic/arrive.js +2 -1
- package/src/ai/movement/dynamic/evade.js +2 -1
- package/src/ai/movement/dynamic/face.js +2 -1
- package/src/ai/movement/dynamic/look-where-youre-going.js +2 -1
- package/src/ai/movement/dynamic/pursue.js +2 -1
- package/src/ai/movement/dynamic/wander.js +2 -1
- package/src/ai/movement/kinematic/face.js +2 -1
- package/src/ai/movement/kinematic/wander-as-seek.js +2 -1
- package/src/animation/sprite.js +2 -1
- package/src/behaviors/at-fps.js +1 -1
- package/src/behaviors/audio.js +24 -24
- package/src/behaviors/camera.js +2 -1
- package/src/behaviors/controls/dynamic/modern.js +1 -1
- package/src/behaviors/controls/dynamic/shooter.js +2 -2
- package/src/behaviors/controls/dynamic/tank.js +1 -1
- package/src/behaviors/controls/kinematic/modern.js +1 -1
- package/src/behaviors/controls/kinematic/shooter.js +2 -2
- package/src/behaviors/controls/kinematic/tank.js +1 -1
- package/src/behaviors/fps.js +2 -1
- package/src/behaviors/input/hover.js +1 -1
- package/src/behaviors/input/mouse.js +3 -2
- package/src/behaviors/input/touch.js +3 -2
- package/src/behaviors/physics/clamped.js +2 -1
- package/src/behaviors/physics/jumpable.js +2 -1
- package/src/collision/detection.js +23 -22
- package/src/core/engine.js +3 -3
- package/src/core/middlewares/entity-pool/entity-pool-middleware.js +2 -2
- package/src/core/middlewares/entity-pool/entity-pools.js +1 -1
- package/src/main.js +2 -1
- package/src/physics/bounds.js +2 -5
- package/src/storage.js +4 -4
- package/src/systems/sprite-animation.js +1 -1
- package/src/systems/ticks.js +61 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@inglorious/engine",
|
|
3
|
-
"version": "24.
|
|
3
|
+
"version": "24.1.1",
|
|
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",
|
|
@@ -33,14 +33,14 @@
|
|
|
33
33
|
},
|
|
34
34
|
"sideEffects": false,
|
|
35
35
|
"dependencies": {
|
|
36
|
-
"@inglorious/store": "
|
|
37
|
-
"@inglorious/utils": "5.
|
|
36
|
+
"@inglorious/store": "12.0.0",
|
|
37
|
+
"@inglorious/utils": "5.1.0"
|
|
38
38
|
},
|
|
39
39
|
"peerDependencies": {
|
|
40
40
|
"vitest": "^5.0.0"
|
|
41
41
|
},
|
|
42
42
|
"devDependencies": {
|
|
43
|
-
"@inglorious/eslint-config": "1.
|
|
43
|
+
"@inglorious/eslint-config": "1.2.0",
|
|
44
44
|
"prettier": "^3.9.6",
|
|
45
45
|
"vite": "^8.2.2",
|
|
46
46
|
"vitest": "^5.0.0"
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import { matchVelocity } from "@inglorious/engine/ai/movement/dynamic/match-velocity.js"
|
|
2
1
|
import { magnitude, setMagnitude, subtract } from "@inglorious/utils/vector.js"
|
|
3
2
|
|
|
3
|
+
import { matchVelocity } from "./match-velocity.js"
|
|
4
|
+
|
|
4
5
|
export const DEFAULT_TARGET_RADIUS = 1
|
|
5
6
|
export const DEFAULT_SLOW_RADIUS = 100
|
|
6
7
|
export const DEFAULT_TIME_TO_TARGET = 0.1
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { flee } from "@inglorious/engine/ai/movement/dynamic/flee.js"
|
|
2
1
|
import {
|
|
3
2
|
magnitude,
|
|
4
3
|
multiply,
|
|
@@ -7,6 +6,8 @@ import {
|
|
|
7
6
|
zero,
|
|
8
7
|
} from "@inglorious/utils/vector.js"
|
|
9
8
|
|
|
9
|
+
import { flee } from "./flee.js"
|
|
10
|
+
|
|
10
11
|
export const DEFAULT_MAX_PREDICTION = 10
|
|
11
12
|
|
|
12
13
|
export function evade(
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import { align } from "@inglorious/engine/ai/movement/dynamic/align.js"
|
|
2
1
|
import { angle, magnitude, subtract } from "@inglorious/utils/vector.js"
|
|
3
2
|
|
|
3
|
+
import { align } from "./align.js"
|
|
4
|
+
|
|
4
5
|
export function face(entity, target, dt, options) {
|
|
5
6
|
const direction = subtract(target.position, entity.position)
|
|
6
7
|
const distance = magnitude(direction)
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import { align } from "@inglorious/engine/ai/movement/dynamic/align.js"
|
|
2
1
|
import { angle, magnitude, zero } from "@inglorious/utils/vector.js"
|
|
3
2
|
|
|
3
|
+
import { align } from "./align.js"
|
|
4
|
+
|
|
4
5
|
export function lookWhereYoureGoing(entity, dt, options) {
|
|
5
6
|
const velocity = entity.velocity ?? zero()
|
|
6
7
|
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { seek } from "@inglorious/engine/ai/movement/dynamic/seek.js"
|
|
2
1
|
import {
|
|
3
2
|
magnitude,
|
|
4
3
|
multiply,
|
|
@@ -7,6 +6,8 @@ import {
|
|
|
7
6
|
zero,
|
|
8
7
|
} from "@inglorious/utils/vector.js"
|
|
9
8
|
|
|
9
|
+
import { seek } from "./seek.js"
|
|
10
|
+
|
|
10
11
|
export const DEFAULT_MAX_PREDICTION = 10
|
|
11
12
|
|
|
12
13
|
export function pursue(
|
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import { seek } from "@inglorious/engine/ai/movement/dynamic/seek.js"
|
|
2
1
|
import { randomBinomial } from "@inglorious/utils/math/rng.js"
|
|
3
2
|
import { createVector, sum } from "@inglorious/utils/vector.js"
|
|
4
3
|
|
|
4
|
+
import { seek } from "./seek.js"
|
|
5
|
+
|
|
5
6
|
export const DEFAULT_WANDER_OFFSET = 100
|
|
6
7
|
export const DEFAULT_WANDER_RADIUS = 100
|
|
7
8
|
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import { align } from "@inglorious/engine/ai/movement/kinematic/align.js"
|
|
2
1
|
import { angle, magnitude, subtract } from "@inglorious/utils/vector.js"
|
|
3
2
|
|
|
3
|
+
import { align } from "./align.js"
|
|
4
|
+
|
|
4
5
|
export function face(entity, target, dt, options) {
|
|
5
6
|
const direction = subtract(target.position, entity.position)
|
|
6
7
|
const distance = magnitude(direction)
|
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import { seek } from "@inglorious/engine/ai/movement/kinematic/seek.js"
|
|
2
1
|
import { randomBinomial } from "@inglorious/utils/math/rng.js"
|
|
3
2
|
import { fromAngle, multiply, sum } from "@inglorious/utils/vector.js"
|
|
4
3
|
|
|
4
|
+
import { seek } from "./seek.js"
|
|
5
|
+
|
|
5
6
|
export const DEFAULT_WANDER_RADIUS = 10
|
|
6
7
|
|
|
7
8
|
const DEFAULT_MAX_ANGULAR_SPEED = 0
|
package/src/animation/sprite.js
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
/* eslint-disable no-magic-numbers */
|
|
2
|
-
import { Ticker } from "@inglorious/engine/animation/ticker.js"
|
|
3
2
|
import { mod } from "@inglorious/utils/math/number.js"
|
|
4
3
|
import { pi, toRange } from "@inglorious/utils/math/trigonometry.js"
|
|
5
4
|
|
|
5
|
+
import { Ticker } from "./ticker.js"
|
|
6
|
+
|
|
6
7
|
export const Sprite = {
|
|
7
8
|
fromAngle,
|
|
8
9
|
move2,
|
package/src/behaviors/at-fps.js
CHANGED
package/src/behaviors/audio.js
CHANGED
|
@@ -1,29 +1,5 @@
|
|
|
1
1
|
const DEFAULT_VOLUME = 1
|
|
2
2
|
|
|
3
|
-
/**
|
|
4
|
-
* Fetches and decodes one sound. A sound that is missing, or that the browser
|
|
5
|
-
* cannot decode, is reported and skipped: losing one effect is not worth refusing
|
|
6
|
-
* to start.
|
|
7
|
-
*/
|
|
8
|
-
async function load(context, cache, name, url) {
|
|
9
|
-
try {
|
|
10
|
-
const response = await fetch(url)
|
|
11
|
-
|
|
12
|
-
if (!response.ok) {
|
|
13
|
-
console.warn(`Sound '${name}' could not be fetched from ${url}`)
|
|
14
|
-
return
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
const audioBuffer = await context.decodeAudioData(
|
|
18
|
-
await response.arrayBuffer(),
|
|
19
|
-
)
|
|
20
|
-
|
|
21
|
-
cache.set(name, audioBuffer)
|
|
22
|
-
} catch (error) {
|
|
23
|
-
console.warn(`Sound '${name}' could not be loaded from ${url}`, error)
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
|
|
27
3
|
export function audio() {
|
|
28
4
|
const audioBufferCache = new Map()
|
|
29
5
|
const activeSources = new Map()
|
|
@@ -117,3 +93,27 @@ export function audio() {
|
|
|
117
93
|
destroy: release,
|
|
118
94
|
}
|
|
119
95
|
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Fetches and decodes one sound. A sound that is missing, or that the browser
|
|
99
|
+
* cannot decode, is reported and skipped: losing one effect is not worth refusing
|
|
100
|
+
* to start.
|
|
101
|
+
*/
|
|
102
|
+
async function load(context, cache, name, url) {
|
|
103
|
+
try {
|
|
104
|
+
const response = await fetch(url)
|
|
105
|
+
|
|
106
|
+
if (!response.ok) {
|
|
107
|
+
console.warn(`Sound '${name}' could not be fetched from ${url}`)
|
|
108
|
+
return
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
const audioBuffer = await context.decodeAudioData(
|
|
112
|
+
await response.arrayBuffer(),
|
|
113
|
+
)
|
|
114
|
+
|
|
115
|
+
cache.set(name, audioBuffer)
|
|
116
|
+
} catch (error) {
|
|
117
|
+
console.warn(`Sound '${name}' could not be loaded from ${url}`, error)
|
|
118
|
+
}
|
|
119
|
+
}
|
package/src/behaviors/camera.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import { arrive } from "@inglorious/engine/ai/movement/dynamic/arrive.js"
|
|
2
1
|
import { lerp } from "@inglorious/utils/math/linear-interpolation.js"
|
|
3
2
|
import { defaults, extend, merge } from "@inglorious/utils/object.js"
|
|
4
3
|
|
|
4
|
+
import { arrive } from "../ai/movement/dynamic/arrive.js"
|
|
5
|
+
|
|
5
6
|
const DEFAULT_PARAMS = {
|
|
6
7
|
zoom: 1,
|
|
7
8
|
zoomSpeed: 0.1,
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { modernMove } from "@inglorious/engine/movement/dynamic/modern.js"
|
|
2
1
|
import { extend, merge } from "@inglorious/utils/object.js"
|
|
3
2
|
import { zero } from "@inglorious/utils/vector.js"
|
|
4
3
|
|
|
4
|
+
import { modernMove } from "../../../movement/dynamic/modern.js"
|
|
5
5
|
import { createMovementEventHandlers } from "../event-handlers.js"
|
|
6
6
|
|
|
7
7
|
const DEFAULT_PARAMS = {
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import { face } from "@inglorious/engine/ai/movement/dynamic/face.js"
|
|
2
|
-
import { tankMove } from "@inglorious/engine/movement/dynamic/tank.js"
|
|
3
1
|
import { pi } from "@inglorious/utils/math/trigonometry.js"
|
|
4
2
|
import { extend, merge } from "@inglorious/utils/object.js"
|
|
5
3
|
import { zero } from "@inglorious/utils/vector.js"
|
|
6
4
|
|
|
5
|
+
import { face } from "../../../ai/movement/dynamic/face.js"
|
|
6
|
+
import { tankMove } from "../../../movement/dynamic/tank.js"
|
|
7
7
|
import { createMovementEventHandlers } from "../event-handlers.js"
|
|
8
8
|
|
|
9
9
|
const FULL_CIRCLE = 2
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { tankMove } from "@inglorious/engine/movement/dynamic/tank.js"
|
|
2
1
|
import { extend, merge } from "@inglorious/utils/object.js"
|
|
3
2
|
import { zero } from "@inglorious/utils/vector.js"
|
|
4
3
|
|
|
4
|
+
import { tankMove } from "../../../movement/dynamic/tank.js"
|
|
5
5
|
import { createMovementEventHandlers } from "../event-handlers.js"
|
|
6
6
|
|
|
7
7
|
const DEFAULT_PARAMS = {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { modernMove } from "@inglorious/engine/movement/kinematic/modern.js"
|
|
2
1
|
import { extend, merge } from "@inglorious/utils/object.js"
|
|
3
2
|
import { zero } from "@inglorious/utils/vector.js"
|
|
4
3
|
|
|
4
|
+
import { modernMove } from "../../../movement/kinematic/modern.js"
|
|
5
5
|
import { createMovementEventHandlers } from "../event-handlers.js"
|
|
6
6
|
|
|
7
7
|
const DEFAULT_PARAMS = {
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import { face } from "@inglorious/engine/ai/movement/kinematic/face.js"
|
|
2
|
-
import { tankMove } from "@inglorious/engine/movement/kinematic/tank.js"
|
|
3
1
|
import { pi } from "@inglorious/utils/math/trigonometry.js"
|
|
4
2
|
import { extend, merge } from "@inglorious/utils/object.js"
|
|
5
3
|
import { zero } from "@inglorious/utils/vector.js"
|
|
6
4
|
|
|
5
|
+
import { face } from "../../../ai/movement/kinematic/face.js"
|
|
6
|
+
import { tankMove } from "../../../movement/kinematic/tank.js"
|
|
7
7
|
import { createMovementEventHandlers } from "../event-handlers.js"
|
|
8
8
|
|
|
9
9
|
const FULL_CIRCLE = 2
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { tankMove } from "@inglorious/engine/movement/kinematic/tank.js"
|
|
2
1
|
import { extend, merge } from "@inglorious/utils/object.js"
|
|
3
2
|
import { zero } from "@inglorious/utils/vector.js"
|
|
4
3
|
|
|
4
|
+
import { tankMove } from "../../../movement/kinematic/tank.js"
|
|
5
5
|
import { createMovementEventHandlers } from "../event-handlers.js"
|
|
6
6
|
|
|
7
7
|
const DEFAULT_PARAMS = {
|
package/src/behaviors/fps.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import { findCollision } from "@inglorious/engine/collision/detection.js"
|
|
2
|
-
import { clampToBounds } from "@inglorious/engine/physics/bounds.js"
|
|
3
1
|
import { zero } from "@inglorious/utils/vector.js"
|
|
4
2
|
|
|
3
|
+
import { findCollision } from "../../collision/detection.js"
|
|
4
|
+
import { clampToBounds } from "../../physics/bounds.js"
|
|
5
|
+
|
|
5
6
|
export function mouse() {
|
|
6
7
|
return {
|
|
7
8
|
create(entity) {
|
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
import { findCollision } from "@inglorious/engine/collision/detection.js"
|
|
2
|
-
import { clampToBounds } from "@inglorious/engine/physics/bounds.js"
|
|
3
1
|
import { v } from "@inglorious/utils/v.js"
|
|
4
2
|
import { magnitude, subtract } from "@inglorious/utils/vector.js"
|
|
5
3
|
|
|
4
|
+
import { findCollision } from "../../collision/detection.js"
|
|
5
|
+
import { clampToBounds } from "../../physics/bounds.js"
|
|
6
|
+
|
|
6
7
|
const MOVEMENT_THRESHOLD = 5
|
|
7
8
|
|
|
8
9
|
export function touch() {
|
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
import { findCollision } from "@inglorious/engine/collision/detection.js"
|
|
2
1
|
import { defaults, extend } from "@inglorious/utils/object.js"
|
|
3
2
|
import { applyGravity } from "@inglorious/utils/physics/gravity.js"
|
|
4
3
|
import { jump } from "@inglorious/utils/physics/jump.js"
|
|
5
4
|
import { angle, magnitude, zero } from "@inglorious/utils/vector.js"
|
|
6
5
|
|
|
6
|
+
import { findCollision } from "../../collision/detection.js"
|
|
7
|
+
|
|
7
8
|
const DEFAULT_PARAMS = {
|
|
8
9
|
maxSpeed: 250,
|
|
9
10
|
maxJump: 100,
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { anchorOffset, shapeAnchor } from "@inglorious/engine/physics/anchor.js"
|
|
2
1
|
import * as circle from "@inglorious/utils/math/circle.js"
|
|
3
2
|
import * as hitmask from "@inglorious/utils/math/hitmask.js"
|
|
4
3
|
import * as line from "@inglorious/utils/math/line.js"
|
|
@@ -8,6 +7,8 @@ import * as segment from "@inglorious/utils/math/segment.js"
|
|
|
8
7
|
import { filter } from "@inglorious/utils/object.js"
|
|
9
8
|
import { add, zero } from "@inglorious/utils/vector.js"
|
|
10
9
|
|
|
10
|
+
import { anchorOffset, shapeAnchor } from "../physics/anchor.js"
|
|
11
|
+
|
|
11
12
|
const Z = 2 // Z-axis index.
|
|
12
13
|
const RECTANGLE = "rectangle"
|
|
13
14
|
|
|
@@ -49,27 +50,6 @@ export function collidesWith(entity, target, collisionGroup = "hitbox") {
|
|
|
49
50
|
return shapeCollidesWith(entityShape, targetShape)
|
|
50
51
|
}
|
|
51
52
|
|
|
52
|
-
function shapeCollidesWith(entity, target) {
|
|
53
|
-
const shapeFns = Shape[entity.shape]
|
|
54
|
-
|
|
55
|
-
switch (target.shape) {
|
|
56
|
-
case "circle":
|
|
57
|
-
return shapeFns.intersectsCircle(entity, target)
|
|
58
|
-
|
|
59
|
-
case "line":
|
|
60
|
-
return shapeFns.intersectsLine(entity, target)
|
|
61
|
-
|
|
62
|
-
case "point":
|
|
63
|
-
return shapeFns.intersectsPoint(entity, target)
|
|
64
|
-
|
|
65
|
-
case "rectangle":
|
|
66
|
-
return shapeFns.intersectsRectangle(entity, target)
|
|
67
|
-
|
|
68
|
-
case "segment":
|
|
69
|
-
return shapeFns.intersectsSegment(entity, target)
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
|
|
73
53
|
export function findCollisions(entity, target, collisionGroup = "hitbox") {
|
|
74
54
|
const entityShape = getCollisionShape(entity, collisionGroup)
|
|
75
55
|
const targetShape = getCollisionShape(target, collisionGroup)
|
|
@@ -138,3 +118,24 @@ function getCollisionShape(entity, collisionGroup = "hitbox") {
|
|
|
138
118
|
radius: collision.radius ?? entity.radius,
|
|
139
119
|
}
|
|
140
120
|
}
|
|
121
|
+
|
|
122
|
+
function shapeCollidesWith(entity, target) {
|
|
123
|
+
const shapeFns = Shape[entity.shape]
|
|
124
|
+
|
|
125
|
+
switch (target.shape) {
|
|
126
|
+
case "circle":
|
|
127
|
+
return shapeFns.intersectsCircle(entity, target)
|
|
128
|
+
|
|
129
|
+
case "line":
|
|
130
|
+
return shapeFns.intersectsLine(entity, target)
|
|
131
|
+
|
|
132
|
+
case "point":
|
|
133
|
+
return shapeFns.intersectsPoint(entity, target)
|
|
134
|
+
|
|
135
|
+
case "rectangle":
|
|
136
|
+
return shapeFns.intersectsRectangle(entity, target)
|
|
137
|
+
|
|
138
|
+
case "segment":
|
|
139
|
+
return shapeFns.intersectsSegment(entity, target)
|
|
140
|
+
}
|
|
141
|
+
}
|
package/src/core/engine.js
CHANGED
|
@@ -1,6 +1,3 @@
|
|
|
1
|
-
import { audio } from "@inglorious/engine/behaviors/audio.js"
|
|
2
|
-
import { game } from "@inglorious/engine/behaviors/game.js"
|
|
3
|
-
import { images } from "@inglorious/engine/behaviors/images.js"
|
|
4
1
|
import { createApi } from "@inglorious/store/api.js"
|
|
5
2
|
import { createDevtools } from "@inglorious/store/client/devtools.js"
|
|
6
3
|
import { multiplayerMiddleware } from "@inglorious/store/client/multiplayer-middleware.js"
|
|
@@ -15,6 +12,9 @@ import { extendWith, isObject } from "@inglorious/utils/object.js"
|
|
|
15
12
|
import { v } from "@inglorious/utils/v.js"
|
|
16
13
|
import { isVector } from "@inglorious/utils/vector.js"
|
|
17
14
|
|
|
15
|
+
import { audio } from "../behaviors/audio.js"
|
|
16
|
+
import { game } from "../behaviors/game.js"
|
|
17
|
+
import { images } from "../behaviors/images.js"
|
|
18
18
|
import { coreEvents } from "./core-events.js"
|
|
19
19
|
import { Loop } from "./loops/index.js"
|
|
20
20
|
import { entityPoolMiddleware } from "./middlewares/entity-pool/entity-pool-middleware.js"
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { findCollision } from "@inglorious/engine/collision/detection.js"
|
|
2
1
|
import { createApi } from "@inglorious/store/api.js"
|
|
3
2
|
import { EventMap } from "@inglorious/store/event-map.js"
|
|
4
3
|
|
|
5
|
-
import {
|
|
4
|
+
import { findCollision } from "../../../collision/detection.js"
|
|
5
|
+
import { EntityPools } from "./entity-pools.js"
|
|
6
6
|
|
|
7
7
|
export function entityPoolMiddleware() {
|
|
8
8
|
return (store) => {
|
package/src/main.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import { Engine } from "@inglorious/engine/core/engine.js"
|
|
2
1
|
import { createRenderer } from "@inglorious/renderer-2d/index.js"
|
|
3
2
|
import game from "game"
|
|
4
3
|
|
|
4
|
+
import { Engine } from "./core/engine.js"
|
|
5
|
+
|
|
5
6
|
window.addEventListener("load", async () => {
|
|
6
7
|
const canvas = document.getElementById("canvas")
|
|
7
8
|
const renderer = createRenderer(canvas)
|
package/src/physics/bounds.js
CHANGED
|
@@ -1,8 +1,3 @@
|
|
|
1
|
-
import {
|
|
2
|
-
anchorMargins,
|
|
3
|
-
entityAnchor,
|
|
4
|
-
shapeAnchor,
|
|
5
|
-
} from "@inglorious/engine/physics/anchor.js"
|
|
6
1
|
import { abs } from "@inglorious/utils/math/number.js"
|
|
7
2
|
import { v } from "@inglorious/utils/v.js"
|
|
8
3
|
import {
|
|
@@ -15,6 +10,8 @@ import {
|
|
|
15
10
|
zero,
|
|
16
11
|
} from "@inglorious/utils/vector.js"
|
|
17
12
|
|
|
13
|
+
import { anchorMargins, entityAnchor, shapeAnchor } from "./anchor.js"
|
|
14
|
+
|
|
18
15
|
const ORIGIN = 0
|
|
19
16
|
const DOUBLE = 2
|
|
20
17
|
const X = 0
|
package/src/storage.js
CHANGED
|
@@ -1,9 +1,5 @@
|
|
|
1
1
|
const DEFAULT_STORAGE = () => globalThis.localStorage
|
|
2
2
|
|
|
3
|
-
function store(storage) {
|
|
4
|
-
return storage ?? DEFAULT_STORAGE()
|
|
5
|
-
}
|
|
6
|
-
|
|
7
3
|
/**
|
|
8
4
|
* Reads a value back as JSON.
|
|
9
5
|
*
|
|
@@ -51,3 +47,7 @@ export function writeJSON(key, value, storage) {
|
|
|
51
47
|
return false
|
|
52
48
|
}
|
|
53
49
|
}
|
|
50
|
+
|
|
51
|
+
function store(storage) {
|
|
52
|
+
return storage ?? DEFAULT_STORAGE()
|
|
53
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { dueAt } from "../animation/due.js"
|
|
2
|
+
|
|
3
|
+
const ONE_SECOND = 1
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* A clock for a whole world that turns at a rate of its own.
|
|
7
|
+
*
|
|
8
|
+
* This is `atFps` for a world rather than for an entity. A board where everything moves
|
|
9
|
+
* together -- a generation of Life, a day for foxes and rabbits -- has one clock, kept
|
|
10
|
+
* here, and notifies `tick` whenever a turn is due. Whatever turns with the world handles
|
|
11
|
+
* `tick` instead of `update`, and the loop stays as fast as the screen takes it, so the
|
|
12
|
+
* pointer and the controls never wait on the world.
|
|
13
|
+
*
|
|
14
|
+
* It is also what lets a large world be cheap between turns. An entity with `atFps` is
|
|
15
|
+
* still handed every `update` to keep its own time, so every one of them is written to on
|
|
16
|
+
* every frame; an entity that only handles `tick` is left alone until the world turns.
|
|
17
|
+
*
|
|
18
|
+
* The event carries the time the turn covers, as `atFps` does: six frames of a sixtieth
|
|
19
|
+
* of a second are a tenth of a second, and a world moving by velocity times dt has to
|
|
20
|
+
* move by all of it.
|
|
21
|
+
*
|
|
22
|
+
* Halting the store stops the entities updating, but systems are still asked, so the
|
|
23
|
+
* clock hears the pause for itself. Without that the world would carry on turning
|
|
24
|
+
* underneath a pause that everything else is honouring.
|
|
25
|
+
*
|
|
26
|
+
* @param {Object} [options] - How the world turns.
|
|
27
|
+
* @param {number} [options.fps=Infinity] - Turns a second. `Infinity` for one a frame.
|
|
28
|
+
* @param {string} [options.event="tick"] - What a turn is called.
|
|
29
|
+
* @returns {Object} The system.
|
|
30
|
+
*/
|
|
31
|
+
export function ticks({ fps = Infinity, event = "tick" } = {}) {
|
|
32
|
+
const isDue = dueAt(ONE_SECOND / fps)
|
|
33
|
+
|
|
34
|
+
let halted = false
|
|
35
|
+
let clock = 0
|
|
36
|
+
|
|
37
|
+
return {
|
|
38
|
+
pause() {
|
|
39
|
+
halted = true
|
|
40
|
+
},
|
|
41
|
+
|
|
42
|
+
resume() {
|
|
43
|
+
halted = false
|
|
44
|
+
},
|
|
45
|
+
|
|
46
|
+
update(entities, dt, api) {
|
|
47
|
+
if (halted) return
|
|
48
|
+
|
|
49
|
+
const { since, elapsed } = isDue(clock, dt)
|
|
50
|
+
|
|
51
|
+
clock = since
|
|
52
|
+
|
|
53
|
+
if (elapsed === undefined) return
|
|
54
|
+
|
|
55
|
+
// Notified rather than called, so that the turn reaches entities and systems alike,
|
|
56
|
+
// in the same pass: the store drains the queue before it publishes, and anything
|
|
57
|
+
// the turn notifies in its own right follows it in.
|
|
58
|
+
api.notify(event, elapsed)
|
|
59
|
+
},
|
|
60
|
+
}
|
|
61
|
+
}
|