@inglorious/engine 0.2.0 → 0.3.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 +75 -75
- package/package.json +13 -23
- package/src/{engine/ai → ai}/movement/dynamic/align.js +63 -63
- package/src/{engine/ai → ai}/movement/dynamic/arrive.js +42 -42
- package/src/{engine/ai → ai}/movement/dynamic/evade.js +38 -38
- package/src/{engine/ai → ai}/movement/dynamic/face.js +19 -19
- package/src/{engine/ai → ai}/movement/dynamic/flee.js +45 -45
- package/src/{engine/ai → ai}/movement/dynamic/look-where-youre-going.js +16 -16
- package/src/{engine/ai → ai}/movement/dynamic/match-velocity.js +51 -51
- package/src/{engine/ai → ai}/movement/dynamic/pursue.js +38 -38
- package/src/{engine/ai → ai}/movement/dynamic/seek.js +44 -44
- package/src/{engine/ai → ai}/movement/dynamic/wander.js +31 -31
- package/src/{engine/ai → ai}/movement/kinematic/align.js +37 -37
- package/src/{engine/ai → ai}/movement/kinematic/arrive.js +42 -42
- package/src/{engine/ai → ai}/movement/kinematic/face.js +19 -19
- package/src/{engine/ai → ai}/movement/kinematic/flee.js +26 -26
- package/src/{engine/ai → ai}/movement/kinematic/seek.js +26 -26
- package/src/{engine/ai → ai}/movement/kinematic/seek.test.js +42 -42
- package/src/{engine/ai → ai}/movement/kinematic/wander-as-seek.js +31 -31
- package/src/{engine/ai → ai}/movement/kinematic/wander.js +27 -27
- package/src/{engine/animation → animation}/sprite.js +101 -101
- package/src/{engine/animation → animation}/ticker.js +38 -38
- package/src/{engine/behaviors → behaviors}/camera.js +68 -68
- package/src/{engine/behaviors → behaviors}/controls/dynamic/modern.js +76 -76
- package/src/{engine/behaviors → behaviors}/controls/dynamic/shooter.js +84 -84
- package/src/{engine/behaviors → behaviors}/controls/dynamic/tank.js +69 -69
- package/src/{engine/behaviors → behaviors}/controls/event-handlers.js +17 -17
- package/src/{engine/behaviors → behaviors}/controls/kinematic/modern.js +76 -76
- package/src/{engine/behaviors → behaviors}/controls/kinematic/shooter.js +82 -82
- package/src/{engine/behaviors → behaviors}/controls/kinematic/tank.js +67 -67
- package/src/behaviors/debug/collision.js +29 -0
- package/src/{engine/behaviors → behaviors}/fps.js +29 -29
- package/src/{engine/behaviors → behaviors}/fsm.js +33 -33
- package/src/{engine/behaviors → behaviors}/fsm.test.js +49 -49
- package/src/{engine/behaviors → behaviors}/game.js +15 -15
- package/src/{engine/behaviors → behaviors}/input/controls.js +37 -37
- package/src/{engine/behaviors → behaviors}/input/gamepad.js +114 -114
- package/src/{engine/behaviors → behaviors}/input/input.js +48 -48
- package/src/{engine/behaviors → behaviors}/input/keyboard.js +64 -64
- package/src/{engine/behaviors → behaviors}/input/mouse.js +91 -91
- package/src/{engine/behaviors → behaviors}/physics/bouncy.js +25 -25
- package/src/{engine/behaviors → behaviors}/physics/clamped.js +36 -36
- package/src/{engine/behaviors → behaviors}/physics/collidable.js +20 -20
- package/src/{engine/behaviors → behaviors}/physics/jumpable.js +145 -145
- package/src/{engine/behaviors → behaviors}/ui/button.js +17 -17
- package/src/{engine/collision → collision}/detection.js +110 -110
- package/src/{engine/core → core}/api.js +34 -34
- package/src/{engine/core → core}/dev-tools.js +135 -135
- package/src/{engine/core → core}/engine.js +119 -119
- package/src/{engine/core → core}/loop.js +15 -15
- package/src/{engine/core → core}/loops/animation-frame.js +25 -25
- package/src/{engine/core → core}/loops/elapsed.js +22 -22
- package/src/{engine/core → core}/loops/fixed.js +27 -27
- package/src/{engine/core → core}/loops/flash.js +13 -13
- package/src/{engine/core → core}/loops/lag.js +26 -26
- package/src/{engine/core → core}/select.js +26 -26
- package/src/{engine/core → core}/store.js +178 -178
- package/src/{engine/core → core}/store.test.js +110 -110
- package/src/main.js +10 -10
- package/src/{engine/movement → movement}/dynamic/modern.js +21 -21
- package/src/{engine/movement → movement}/dynamic/tank.js +43 -43
- package/src/{engine/movement → movement}/kinematic/modern.js +16 -16
- package/src/{engine/movement → movement}/kinematic/modern.test.js +27 -27
- package/src/{engine/movement → movement}/kinematic/tank.js +27 -27
- package/src/{engine/physics → physics}/bounds.js +138 -138
- package/src/{engine/physics → physics}/position.js +43 -43
- package/src/{engine/physics → physics}/position.test.js +80 -80
- package/src/{engine/systems → systems}/sprite-animation.js +27 -27
- package/src/engine/behaviors/debug/collision.js +0 -35
- package/src/renderers/canvas/absolute-position.js +0 -18
- package/src/renderers/canvas/camera.js +0 -13
- package/src/renderers/canvas/canvas-renderer.js +0 -68
- package/src/renderers/canvas/character.js +0 -38
- package/src/renderers/canvas/form/button.js +0 -25
- package/src/renderers/canvas/fps.js +0 -18
- package/src/renderers/canvas/image/hitmask.js +0 -51
- package/src/renderers/canvas/image/image.js +0 -34
- package/src/renderers/canvas/image/sprite.js +0 -49
- package/src/renderers/canvas/image/tilemap.js +0 -66
- package/src/renderers/canvas/mouse.js +0 -37
- package/src/renderers/canvas/rendering-system.js +0 -79
- package/src/renderers/canvas/shapes/circle.js +0 -29
- package/src/renderers/canvas/shapes/rectangle.js +0 -27
- package/src/renderers/react/game/character/character.module.scss +0 -17
- package/src/renderers/react/game/character/index.jsx +0 -20
- package/src/renderers/react/game/cursor/cursor.module.scss +0 -47
- package/src/renderers/react/game/cursor/index.jsx +0 -20
- package/src/renderers/react/game/form/fields/field/field.module.scss +0 -5
- package/src/renderers/react/game/form/fields/field/index.jsx +0 -56
- package/src/renderers/react/game/form/fields/fields.module.scss +0 -48
- package/src/renderers/react/game/form/fields/index.jsx +0 -12
- package/src/renderers/react/game/form/form.module.scss +0 -18
- package/src/renderers/react/game/form/index.jsx +0 -22
- package/src/renderers/react/game/fps/index.jsx +0 -16
- package/src/renderers/react/game/game.jsx +0 -72
- package/src/renderers/react/game/index.jsx +0 -29
- package/src/renderers/react/game/platform/index.jsx +0 -30
- package/src/renderers/react/game/platform/platform.module.scss +0 -7
- package/src/renderers/react/game/scene/index.jsx +0 -27
- package/src/renderers/react/game/scene/scene.module.scss +0 -9
- package/src/renderers/react/game/sprite/index.jsx +0 -60
- package/src/renderers/react/game/sprite/sprite.module.css +0 -3
- package/src/renderers/react/game/stats/index.jsx +0 -22
- package/src/renderers/react/hocs/with-absolute-position/index.jsx +0 -20
- package/src/renderers/react/hocs/with-absolute-position/with-absolute-position.module.scss +0 -5
- package/src/renderers/react/index.jsx +0 -9
- package/src/utils/algorithms/decision-tree.js +0 -24
- package/src/utils/algorithms/decision-tree.test.js +0 -153
- package/src/utils/algorithms/path-finding.js +0 -155
- package/src/utils/algorithms/path-finding.test.js +0 -151
- package/src/utils/algorithms/types.d.ts +0 -28
- package/src/utils/data-structures/array.js +0 -83
- package/src/utils/data-structures/array.test.js +0 -173
- package/src/utils/data-structures/board.js +0 -159
- package/src/utils/data-structures/board.test.js +0 -242
- package/src/utils/data-structures/boolean.js +0 -9
- package/src/utils/data-structures/heap.js +0 -164
- package/src/utils/data-structures/heap.test.js +0 -103
- package/src/utils/data-structures/object.js +0 -138
- package/src/utils/data-structures/object.test.js +0 -218
- package/src/utils/data-structures/objects.js +0 -66
- package/src/utils/data-structures/objects.test.js +0 -99
- package/src/utils/data-structures/tree.js +0 -36
- package/src/utils/data-structures/tree.test.js +0 -33
- package/src/utils/data-structures/types.d.ts +0 -4
- package/src/utils/functions/functions.js +0 -19
- package/src/utils/functions/functions.test.js +0 -23
- package/src/utils/math/geometry/circle.js +0 -70
- package/src/utils/math/geometry/circle.test.js +0 -97
- package/src/utils/math/geometry/hitmask.js +0 -70
- package/src/utils/math/geometry/hitmask.test.js +0 -155
- package/src/utils/math/geometry/line.js +0 -35
- package/src/utils/math/geometry/line.test.js +0 -49
- package/src/utils/math/geometry/point.js +0 -78
- package/src/utils/math/geometry/point.test.js +0 -81
- package/src/utils/math/geometry/rectangle.js +0 -76
- package/src/utils/math/geometry/rectangle.test.js +0 -42
- package/src/utils/math/geometry/segment.js +0 -80
- package/src/utils/math/geometry/segment.test.js +0 -183
- package/src/utils/math/geometry/triangle.js +0 -15
- package/src/utils/math/geometry/triangle.test.js +0 -11
- package/src/utils/math/geometry/types.d.ts +0 -23
- package/src/utils/math/linear-algebra/2d.js +0 -28
- package/src/utils/math/linear-algebra/2d.test.js +0 -17
- package/src/utils/math/linear-algebra/quaternion.js +0 -22
- package/src/utils/math/linear-algebra/quaternion.test.js +0 -25
- package/src/utils/math/linear-algebra/quaternions.js +0 -20
- package/src/utils/math/linear-algebra/quaternions.test.js +0 -29
- package/src/utils/math/linear-algebra/types.d.ts +0 -4
- package/src/utils/math/linear-algebra/vector.js +0 -327
- package/src/utils/math/linear-algebra/vector.test.js +0 -265
- package/src/utils/math/linear-algebra/vectors.js +0 -122
- package/src/utils/math/linear-algebra/vectors.test.js +0 -65
- package/src/utils/math/linear-interpolation.js +0 -9
- package/src/utils/math/numbers.js +0 -90
- package/src/utils/math/numbers.test.js +0 -137
- package/src/utils/math/rng.js +0 -44
- package/src/utils/math/rng.test.js +0 -39
- package/src/utils/math/statistics.js +0 -43
- package/src/utils/math/statistics.test.js +0 -47
- package/src/utils/math/trigonometry.js +0 -89
- package/src/utils/math/trigonometry.test.js +0 -52
- package/src/utils/physics/acceleration.js +0 -61
- package/src/utils/physics/friction.js +0 -28
- package/src/utils/physics/friction.test.js +0 -42
- package/src/utils/physics/gravity.js +0 -69
- package/src/utils/physics/gravity.test.js +0 -77
- package/src/utils/physics/jump.js +0 -31
- package/src/utils/physics/velocity.js +0 -36
|
@@ -1,110 +1,110 @@
|
|
|
1
|
-
import { expect, test } from "vitest"
|
|
2
|
-
|
|
3
|
-
import { createStore } from "./store.js"
|
|
4
|
-
|
|
5
|
-
test("it should process events by mutating state inside handlers", () => {
|
|
6
|
-
const config = {
|
|
7
|
-
types: {
|
|
8
|
-
kitty: {
|
|
9
|
-
feed(entity) {
|
|
10
|
-
entity.isFed = true
|
|
11
|
-
},
|
|
12
|
-
update(entity) {
|
|
13
|
-
entity.isMeowing = true
|
|
14
|
-
},
|
|
15
|
-
},
|
|
16
|
-
},
|
|
17
|
-
entities: {
|
|
18
|
-
kitty1: { type: "kitty" },
|
|
19
|
-
},
|
|
20
|
-
}
|
|
21
|
-
const afterState = {
|
|
22
|
-
entities: {
|
|
23
|
-
kitty1: {
|
|
24
|
-
id: "kitty1",
|
|
25
|
-
type: "kitty",
|
|
26
|
-
isFed: true,
|
|
27
|
-
isMeowing: true,
|
|
28
|
-
},
|
|
29
|
-
},
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
const store = createStore(config)
|
|
33
|
-
store.notify("feed")
|
|
34
|
-
store.update(0, {})
|
|
35
|
-
|
|
36
|
-
const state = store.getState()
|
|
37
|
-
expect(state).toStrictEqual(afterState)
|
|
38
|
-
})
|
|
39
|
-
|
|
40
|
-
test("it should send an event from an entity and process it in the same update cycle", () => {
|
|
41
|
-
const config = {
|
|
42
|
-
types: {
|
|
43
|
-
doggo: {
|
|
44
|
-
update(entity, dt, api) {
|
|
45
|
-
api.notify("bark")
|
|
46
|
-
},
|
|
47
|
-
},
|
|
48
|
-
kitty: {
|
|
49
|
-
bark(entity) {
|
|
50
|
-
entity.position = "far"
|
|
51
|
-
},
|
|
52
|
-
},
|
|
53
|
-
},
|
|
54
|
-
entities: {
|
|
55
|
-
doggo1: { type: "doggo" },
|
|
56
|
-
kitty1: { type: "kitty", position: "near" },
|
|
57
|
-
},
|
|
58
|
-
}
|
|
59
|
-
const afterState = {
|
|
60
|
-
entities: {
|
|
61
|
-
doggo1: { id: "doggo1", type: "doggo" },
|
|
62
|
-
kitty1: { id: "kitty1", type: "kitty", position: "far" },
|
|
63
|
-
},
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
const store = createStore(config)
|
|
67
|
-
const api = { notify: store.notify }
|
|
68
|
-
store.update(0, api)
|
|
69
|
-
|
|
70
|
-
const state = store.getState()
|
|
71
|
-
expect(state).toStrictEqual(afterState)
|
|
72
|
-
})
|
|
73
|
-
|
|
74
|
-
test("it should add an entity via an 'add' event", () => {
|
|
75
|
-
const config = {
|
|
76
|
-
types: {
|
|
77
|
-
kitty: {},
|
|
78
|
-
},
|
|
79
|
-
entities: {},
|
|
80
|
-
}
|
|
81
|
-
const newEntity = { id: "kitty1", type: "kitty" }
|
|
82
|
-
|
|
83
|
-
const store = createStore(config)
|
|
84
|
-
store.notify("add", newEntity)
|
|
85
|
-
store.update(0, {})
|
|
86
|
-
|
|
87
|
-
const state = store.getState()
|
|
88
|
-
expect(state).toStrictEqual({
|
|
89
|
-
entities: {
|
|
90
|
-
kitty1: { id: "kitty1", type: "kitty" },
|
|
91
|
-
},
|
|
92
|
-
})
|
|
93
|
-
})
|
|
94
|
-
|
|
95
|
-
test("it should remove an entity via a 'remove' event", () => {
|
|
96
|
-
const config = {
|
|
97
|
-
types: {},
|
|
98
|
-
entities: {
|
|
99
|
-
kitty1: { type: "kitty" },
|
|
100
|
-
},
|
|
101
|
-
}
|
|
102
|
-
const store = createStore(config)
|
|
103
|
-
|
|
104
|
-
store.notify("remove", "kitty1")
|
|
105
|
-
|
|
106
|
-
store.update(0, {})
|
|
107
|
-
|
|
108
|
-
const state = store.getState()
|
|
109
|
-
expect(state.entities.kitty1).toBeUndefined()
|
|
110
|
-
})
|
|
1
|
+
import { expect, test } from "vitest"
|
|
2
|
+
|
|
3
|
+
import { createStore } from "./store.js"
|
|
4
|
+
|
|
5
|
+
test("it should process events by mutating state inside handlers", () => {
|
|
6
|
+
const config = {
|
|
7
|
+
types: {
|
|
8
|
+
kitty: {
|
|
9
|
+
feed(entity) {
|
|
10
|
+
entity.isFed = true
|
|
11
|
+
},
|
|
12
|
+
update(entity) {
|
|
13
|
+
entity.isMeowing = true
|
|
14
|
+
},
|
|
15
|
+
},
|
|
16
|
+
},
|
|
17
|
+
entities: {
|
|
18
|
+
kitty1: { type: "kitty" },
|
|
19
|
+
},
|
|
20
|
+
}
|
|
21
|
+
const afterState = {
|
|
22
|
+
entities: {
|
|
23
|
+
kitty1: {
|
|
24
|
+
id: "kitty1",
|
|
25
|
+
type: "kitty",
|
|
26
|
+
isFed: true,
|
|
27
|
+
isMeowing: true,
|
|
28
|
+
},
|
|
29
|
+
},
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const store = createStore(config)
|
|
33
|
+
store.notify("feed")
|
|
34
|
+
store.update(0, {})
|
|
35
|
+
|
|
36
|
+
const state = store.getState()
|
|
37
|
+
expect(state).toStrictEqual(afterState)
|
|
38
|
+
})
|
|
39
|
+
|
|
40
|
+
test("it should send an event from an entity and process it in the same update cycle", () => {
|
|
41
|
+
const config = {
|
|
42
|
+
types: {
|
|
43
|
+
doggo: {
|
|
44
|
+
update(entity, dt, api) {
|
|
45
|
+
api.notify("bark")
|
|
46
|
+
},
|
|
47
|
+
},
|
|
48
|
+
kitty: {
|
|
49
|
+
bark(entity) {
|
|
50
|
+
entity.position = "far"
|
|
51
|
+
},
|
|
52
|
+
},
|
|
53
|
+
},
|
|
54
|
+
entities: {
|
|
55
|
+
doggo1: { type: "doggo" },
|
|
56
|
+
kitty1: { type: "kitty", position: "near" },
|
|
57
|
+
},
|
|
58
|
+
}
|
|
59
|
+
const afterState = {
|
|
60
|
+
entities: {
|
|
61
|
+
doggo1: { id: "doggo1", type: "doggo" },
|
|
62
|
+
kitty1: { id: "kitty1", type: "kitty", position: "far" },
|
|
63
|
+
},
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const store = createStore(config)
|
|
67
|
+
const api = { notify: store.notify }
|
|
68
|
+
store.update(0, api)
|
|
69
|
+
|
|
70
|
+
const state = store.getState()
|
|
71
|
+
expect(state).toStrictEqual(afterState)
|
|
72
|
+
})
|
|
73
|
+
|
|
74
|
+
test("it should add an entity via an 'add' event", () => {
|
|
75
|
+
const config = {
|
|
76
|
+
types: {
|
|
77
|
+
kitty: {},
|
|
78
|
+
},
|
|
79
|
+
entities: {},
|
|
80
|
+
}
|
|
81
|
+
const newEntity = { id: "kitty1", type: "kitty" }
|
|
82
|
+
|
|
83
|
+
const store = createStore(config)
|
|
84
|
+
store.notify("add", newEntity)
|
|
85
|
+
store.update(0, {})
|
|
86
|
+
|
|
87
|
+
const state = store.getState()
|
|
88
|
+
expect(state).toStrictEqual({
|
|
89
|
+
entities: {
|
|
90
|
+
kitty1: { id: "kitty1", type: "kitty" },
|
|
91
|
+
},
|
|
92
|
+
})
|
|
93
|
+
})
|
|
94
|
+
|
|
95
|
+
test("it should remove an entity via a 'remove' event", () => {
|
|
96
|
+
const config = {
|
|
97
|
+
types: {},
|
|
98
|
+
entities: {
|
|
99
|
+
kitty1: { type: "kitty" },
|
|
100
|
+
},
|
|
101
|
+
}
|
|
102
|
+
const store = createStore(config)
|
|
103
|
+
|
|
104
|
+
store.notify("remove", "kitty1")
|
|
105
|
+
|
|
106
|
+
store.update(0, {})
|
|
107
|
+
|
|
108
|
+
const state = store.getState()
|
|
109
|
+
expect(state.entities.kitty1).toBeUndefined()
|
|
110
|
+
})
|
package/src/main.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import { Engine } from "@inglorious/engine/core/engine.js"
|
|
2
|
-
import { CanvasRenderer } from "@inglorious/
|
|
3
|
-
import game from "game"
|
|
4
|
-
|
|
5
|
-
const canvas = document.getElementById("canvas")
|
|
6
|
-
window.addEventListener("load", () => {
|
|
7
|
-
const renderer = new CanvasRenderer(canvas)
|
|
8
|
-
const engine = new Engine({ ...game, renderer })
|
|
9
|
-
engine.start()
|
|
10
|
-
})
|
|
1
|
+
import { Engine } from "@inglorious/engine/core/engine.js"
|
|
2
|
+
import { CanvasRenderer } from "@inglorious/renderer-2d/canvas-renderer.js"
|
|
3
|
+
import game from "game"
|
|
4
|
+
|
|
5
|
+
const canvas = document.getElementById("canvas")
|
|
6
|
+
window.addEventListener("load", () => {
|
|
7
|
+
const renderer = new CanvasRenderer(canvas)
|
|
8
|
+
const engine = new Engine({ ...game, renderer })
|
|
9
|
+
engine.start()
|
|
10
|
+
})
|
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
import {
|
|
2
|
-
angle,
|
|
3
|
-
magnitude,
|
|
4
|
-
} from "@inglorious/utils/math/linear-algebra/vector.js"
|
|
5
|
-
import { applyAcceleration } from "@inglorious/utils/physics/acceleration.js"
|
|
6
|
-
|
|
7
|
-
const DEFAULT_ORIENTATION = 0
|
|
8
|
-
|
|
9
|
-
const ORIENTATION_CHANGE_THRESHOLD = 4
|
|
10
|
-
|
|
11
|
-
export function modernMove(entity, dt) {
|
|
12
|
-
const { acceleration, velocity, position } = applyAcceleration(entity, dt)
|
|
13
|
-
|
|
14
|
-
let orientation = entity.orientation ?? DEFAULT_ORIENTATION
|
|
15
|
-
orientation =
|
|
16
|
-
magnitude(velocity) > ORIENTATION_CHANGE_THRESHOLD
|
|
17
|
-
? angle(velocity)
|
|
18
|
-
: orientation
|
|
19
|
-
|
|
20
|
-
return { acceleration, velocity, position, orientation }
|
|
21
|
-
}
|
|
1
|
+
import {
|
|
2
|
+
angle,
|
|
3
|
+
magnitude,
|
|
4
|
+
} from "@inglorious/utils/math/linear-algebra/vector.js"
|
|
5
|
+
import { applyAcceleration } from "@inglorious/utils/physics/acceleration.js"
|
|
6
|
+
|
|
7
|
+
const DEFAULT_ORIENTATION = 0
|
|
8
|
+
|
|
9
|
+
const ORIENTATION_CHANGE_THRESHOLD = 4
|
|
10
|
+
|
|
11
|
+
export function modernMove(entity, dt) {
|
|
12
|
+
const { acceleration, velocity, position } = applyAcceleration(entity, dt)
|
|
13
|
+
|
|
14
|
+
let orientation = entity.orientation ?? DEFAULT_ORIENTATION
|
|
15
|
+
orientation =
|
|
16
|
+
magnitude(velocity) > ORIENTATION_CHANGE_THRESHOLD
|
|
17
|
+
? angle(velocity)
|
|
18
|
+
: orientation
|
|
19
|
+
|
|
20
|
+
return { acceleration, velocity, position, orientation }
|
|
21
|
+
}
|
|
@@ -1,43 +1,43 @@
|
|
|
1
|
-
import {
|
|
2
|
-
clamp,
|
|
3
|
-
multiply,
|
|
4
|
-
rotate,
|
|
5
|
-
zero,
|
|
6
|
-
} from "@inglorious/utils/math/linear-algebra/vector.js"
|
|
7
|
-
import { sum } from "@inglorious/utils/math/linear-algebra/vectors.js"
|
|
8
|
-
import { toRange } from "@inglorious/utils/math/trigonometry.js"
|
|
9
|
-
import { applyFriction } from "@inglorious/utils/physics/friction.js"
|
|
10
|
-
|
|
11
|
-
const DEFAULT_MAX_ACCELERATION = 0
|
|
12
|
-
const DEFAULT_MAX_SPEED = 0
|
|
13
|
-
const DEFAULT_FRICTION = 0
|
|
14
|
-
|
|
15
|
-
const DEFAULT_ORIENTATION = 0
|
|
16
|
-
|
|
17
|
-
const HALF_ACCELERATION = 0.5
|
|
18
|
-
|
|
19
|
-
export function tankMove(entity, dt) {
|
|
20
|
-
const maxAcceleration = entity.maxAcceleration ?? DEFAULT_MAX_ACCELERATION
|
|
21
|
-
const maxSpeed = entity.maxSpeed ?? DEFAULT_MAX_SPEED
|
|
22
|
-
const friction = entity.friction ?? DEFAULT_FRICTION
|
|
23
|
-
|
|
24
|
-
let orientation = entity.orientation ?? DEFAULT_ORIENTATION
|
|
25
|
-
orientation = toRange(orientation)
|
|
26
|
-
|
|
27
|
-
let acceleration = entity.acceleration ?? zero()
|
|
28
|
-
acceleration = rotate(acceleration, orientation)
|
|
29
|
-
acceleration = clamp(acceleration, -maxAcceleration, maxAcceleration)
|
|
30
|
-
|
|
31
|
-
let velocity = entity.velocity ?? zero()
|
|
32
|
-
velocity = sum(velocity, multiply(acceleration, dt))
|
|
33
|
-
velocity = clamp(velocity, -maxSpeed, maxSpeed)
|
|
34
|
-
velocity = applyFriction({ velocity, friction }, dt)
|
|
35
|
-
|
|
36
|
-
const position = sum(
|
|
37
|
-
entity.position,
|
|
38
|
-
multiply(velocity, dt),
|
|
39
|
-
multiply(acceleration, HALF_ACCELERATION * dt * dt),
|
|
40
|
-
)
|
|
41
|
-
|
|
42
|
-
return { velocity, position, orientation }
|
|
43
|
-
}
|
|
1
|
+
import {
|
|
2
|
+
clamp,
|
|
3
|
+
multiply,
|
|
4
|
+
rotate,
|
|
5
|
+
zero,
|
|
6
|
+
} from "@inglorious/utils/math/linear-algebra/vector.js"
|
|
7
|
+
import { sum } from "@inglorious/utils/math/linear-algebra/vectors.js"
|
|
8
|
+
import { toRange } from "@inglorious/utils/math/trigonometry.js"
|
|
9
|
+
import { applyFriction } from "@inglorious/utils/physics/friction.js"
|
|
10
|
+
|
|
11
|
+
const DEFAULT_MAX_ACCELERATION = 0
|
|
12
|
+
const DEFAULT_MAX_SPEED = 0
|
|
13
|
+
const DEFAULT_FRICTION = 0
|
|
14
|
+
|
|
15
|
+
const DEFAULT_ORIENTATION = 0
|
|
16
|
+
|
|
17
|
+
const HALF_ACCELERATION = 0.5
|
|
18
|
+
|
|
19
|
+
export function tankMove(entity, dt) {
|
|
20
|
+
const maxAcceleration = entity.maxAcceleration ?? DEFAULT_MAX_ACCELERATION
|
|
21
|
+
const maxSpeed = entity.maxSpeed ?? DEFAULT_MAX_SPEED
|
|
22
|
+
const friction = entity.friction ?? DEFAULT_FRICTION
|
|
23
|
+
|
|
24
|
+
let orientation = entity.orientation ?? DEFAULT_ORIENTATION
|
|
25
|
+
orientation = toRange(orientation)
|
|
26
|
+
|
|
27
|
+
let acceleration = entity.acceleration ?? zero()
|
|
28
|
+
acceleration = rotate(acceleration, orientation)
|
|
29
|
+
acceleration = clamp(acceleration, -maxAcceleration, maxAcceleration)
|
|
30
|
+
|
|
31
|
+
let velocity = entity.velocity ?? zero()
|
|
32
|
+
velocity = sum(velocity, multiply(acceleration, dt))
|
|
33
|
+
velocity = clamp(velocity, -maxSpeed, maxSpeed)
|
|
34
|
+
velocity = applyFriction({ velocity, friction }, dt)
|
|
35
|
+
|
|
36
|
+
const position = sum(
|
|
37
|
+
entity.position,
|
|
38
|
+
multiply(velocity, dt),
|
|
39
|
+
multiply(acceleration, HALF_ACCELERATION * dt * dt),
|
|
40
|
+
)
|
|
41
|
+
|
|
42
|
+
return { velocity, position, orientation }
|
|
43
|
+
}
|
|
@@ -1,16 +1,16 @@
|
|
|
1
|
-
import {
|
|
2
|
-
angle,
|
|
3
|
-
magnitude,
|
|
4
|
-
} from "@inglorious/utils/math/linear-algebra/vector.js"
|
|
5
|
-
import { applyVelocity } from "@inglorious/utils/physics/velocity.js"
|
|
6
|
-
|
|
7
|
-
const DEFAULT_ORIENTATION = 0
|
|
8
|
-
|
|
9
|
-
export function modernMove(entity, dt) {
|
|
10
|
-
const { velocity, position } = applyVelocity(entity, dt)
|
|
11
|
-
|
|
12
|
-
let orientation = entity.orientation ?? DEFAULT_ORIENTATION
|
|
13
|
-
orientation = magnitude(velocity) ? angle(velocity) : orientation
|
|
14
|
-
|
|
15
|
-
return { velocity, position, orientation }
|
|
16
|
-
}
|
|
1
|
+
import {
|
|
2
|
+
angle,
|
|
3
|
+
magnitude,
|
|
4
|
+
} from "@inglorious/utils/math/linear-algebra/vector.js"
|
|
5
|
+
import { applyVelocity } from "@inglorious/utils/physics/velocity.js"
|
|
6
|
+
|
|
7
|
+
const DEFAULT_ORIENTATION = 0
|
|
8
|
+
|
|
9
|
+
export function modernMove(entity, dt) {
|
|
10
|
+
const { velocity, position } = applyVelocity(entity, dt)
|
|
11
|
+
|
|
12
|
+
let orientation = entity.orientation ?? DEFAULT_ORIENTATION
|
|
13
|
+
orientation = magnitude(velocity) ? angle(velocity) : orientation
|
|
14
|
+
|
|
15
|
+
return { velocity, position, orientation }
|
|
16
|
+
}
|
|
@@ -1,27 +1,27 @@
|
|
|
1
|
-
import { expect, test } from "vitest"
|
|
2
|
-
|
|
3
|
-
import { modernMove } from "./modern.js"
|
|
4
|
-
|
|
5
|
-
test("it should move following its velocity", () => {
|
|
6
|
-
const entity = { maxSpeed: 1, velocity: [1, 0, 0], position: [0, 0, 0] }
|
|
7
|
-
const dt = 1
|
|
8
|
-
const expectedResult = {
|
|
9
|
-
velocity: [1, 0, 0],
|
|
10
|
-
position: [1, 0, 0],
|
|
11
|
-
orientation: 0,
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
expect(modernMove(entity, dt)).toStrictEqual(expectedResult)
|
|
15
|
-
})
|
|
16
|
-
|
|
17
|
-
test("it should limit the velocity to the max speed", () => {
|
|
18
|
-
const entity = { maxSpeed: 1, velocity: [10, 0, 0], position: [0, 0, 0] }
|
|
19
|
-
const dt = 1
|
|
20
|
-
const expectedResult = {
|
|
21
|
-
velocity: [1, 0, 0],
|
|
22
|
-
position: [1, 0, 0],
|
|
23
|
-
orientation: 0,
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
expect(modernMove(entity, dt)).toStrictEqual(expectedResult)
|
|
27
|
-
})
|
|
1
|
+
import { expect, test } from "vitest"
|
|
2
|
+
|
|
3
|
+
import { modernMove } from "./modern.js"
|
|
4
|
+
|
|
5
|
+
test("it should move following its velocity", () => {
|
|
6
|
+
const entity = { maxSpeed: 1, velocity: [1, 0, 0], position: [0, 0, 0] }
|
|
7
|
+
const dt = 1
|
|
8
|
+
const expectedResult = {
|
|
9
|
+
velocity: [1, 0, 0],
|
|
10
|
+
position: [1, 0, 0],
|
|
11
|
+
orientation: 0,
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
expect(modernMove(entity, dt)).toStrictEqual(expectedResult)
|
|
15
|
+
})
|
|
16
|
+
|
|
17
|
+
test("it should limit the velocity to the max speed", () => {
|
|
18
|
+
const entity = { maxSpeed: 1, velocity: [10, 0, 0], position: [0, 0, 0] }
|
|
19
|
+
const dt = 1
|
|
20
|
+
const expectedResult = {
|
|
21
|
+
velocity: [1, 0, 0],
|
|
22
|
+
position: [1, 0, 0],
|
|
23
|
+
orientation: 0,
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
expect(modernMove(entity, dt)).toStrictEqual(expectedResult)
|
|
27
|
+
})
|
|
@@ -1,27 +1,27 @@
|
|
|
1
|
-
import {
|
|
2
|
-
clamp,
|
|
3
|
-
multiply,
|
|
4
|
-
rotate,
|
|
5
|
-
zero,
|
|
6
|
-
} from "@inglorious/utils/math/linear-algebra/vector.js"
|
|
7
|
-
import { sum } from "@inglorious/utils/math/linear-algebra/vectors.js"
|
|
8
|
-
import { toRange } from "@inglorious/utils/math/trigonometry.js"
|
|
9
|
-
|
|
10
|
-
const DEFAULT_MAX_SPEED = 0
|
|
11
|
-
|
|
12
|
-
const DEFAULT_ORIENTATION = 0
|
|
13
|
-
|
|
14
|
-
export function tankMove(entity, dt) {
|
|
15
|
-
const maxSpeed = entity.maxSpeed ?? DEFAULT_MAX_SPEED
|
|
16
|
-
|
|
17
|
-
let orientation = entity.orientation ?? DEFAULT_ORIENTATION
|
|
18
|
-
orientation = toRange(orientation)
|
|
19
|
-
|
|
20
|
-
let velocity = entity.velocity ?? zero()
|
|
21
|
-
velocity = rotate(velocity, orientation)
|
|
22
|
-
velocity = clamp(velocity, -maxSpeed, maxSpeed)
|
|
23
|
-
|
|
24
|
-
const position = sum(entity.position, multiply(velocity, dt))
|
|
25
|
-
|
|
26
|
-
return { velocity, position, orientation }
|
|
27
|
-
}
|
|
1
|
+
import {
|
|
2
|
+
clamp,
|
|
3
|
+
multiply,
|
|
4
|
+
rotate,
|
|
5
|
+
zero,
|
|
6
|
+
} from "@inglorious/utils/math/linear-algebra/vector.js"
|
|
7
|
+
import { sum } from "@inglorious/utils/math/linear-algebra/vectors.js"
|
|
8
|
+
import { toRange } from "@inglorious/utils/math/trigonometry.js"
|
|
9
|
+
|
|
10
|
+
const DEFAULT_MAX_SPEED = 0
|
|
11
|
+
|
|
12
|
+
const DEFAULT_ORIENTATION = 0
|
|
13
|
+
|
|
14
|
+
export function tankMove(entity, dt) {
|
|
15
|
+
const maxSpeed = entity.maxSpeed ?? DEFAULT_MAX_SPEED
|
|
16
|
+
|
|
17
|
+
let orientation = entity.orientation ?? DEFAULT_ORIENTATION
|
|
18
|
+
orientation = toRange(orientation)
|
|
19
|
+
|
|
20
|
+
let velocity = entity.velocity ?? zero()
|
|
21
|
+
velocity = rotate(velocity, orientation)
|
|
22
|
+
velocity = clamp(velocity, -maxSpeed, maxSpeed)
|
|
23
|
+
|
|
24
|
+
const position = sum(entity.position, multiply(velocity, dt))
|
|
25
|
+
|
|
26
|
+
return { velocity, position, orientation }
|
|
27
|
+
}
|