@inglorious/engine 0.2.0 → 0.4.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 +76 -75
- package/package.json +14 -25
- 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}/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/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/engine/core/api.js +0 -34
- package/src/engine/core/select.js +0 -26
- package/src/engine/core/store.js +0 -178
- package/src/engine/core/store.test.js +0 -110
- 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,29 +0,0 @@
|
|
|
1
|
-
import { Engine } from "@inglorious/engine/core/engine.js"
|
|
2
|
-
import { useEffect, useMemo, useState } from "react"
|
|
3
|
-
import { Provider } from "react-redux"
|
|
4
|
-
|
|
5
|
-
import GameComponent from "./game.jsx"
|
|
6
|
-
|
|
7
|
-
export default function Game({ config }) {
|
|
8
|
-
const [isReady, setReady] = useState(false)
|
|
9
|
-
|
|
10
|
-
const engine = useMemo(() => new Engine(config), [config])
|
|
11
|
-
|
|
12
|
-
useEffect(() => {
|
|
13
|
-
engine.start()
|
|
14
|
-
setReady(true)
|
|
15
|
-
window.engine = engine
|
|
16
|
-
|
|
17
|
-
return () => engine.stop()
|
|
18
|
-
}, [engine, config])
|
|
19
|
-
|
|
20
|
-
if (!isReady) {
|
|
21
|
-
return null
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
return (
|
|
25
|
-
<Provider store={engine._store}>
|
|
26
|
-
<GameComponent engine={engine} />
|
|
27
|
-
</Provider>
|
|
28
|
-
)
|
|
29
|
-
}
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
/* eslint-disable no-magic-numbers */
|
|
2
|
-
|
|
3
|
-
import { useDispatch } from "react-redux"
|
|
4
|
-
|
|
5
|
-
import classes from "./platform.module.scss"
|
|
6
|
-
|
|
7
|
-
const DEFAULT_SIZE = [80, 20]
|
|
8
|
-
|
|
9
|
-
export default function Platform({ id, entity, className, style }) {
|
|
10
|
-
const dispatch = useDispatch()
|
|
11
|
-
|
|
12
|
-
const [width, height] = entity.size ?? DEFAULT_SIZE
|
|
13
|
-
|
|
14
|
-
const handleClick = (event) => {
|
|
15
|
-
event.stopPropagation()
|
|
16
|
-
dispatch({ id: "entityClick", payload: id })
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
return (
|
|
20
|
-
<div
|
|
21
|
-
className={`${classes.platform} ${className}`}
|
|
22
|
-
style={{
|
|
23
|
-
...style,
|
|
24
|
-
"--width": `${width}px`,
|
|
25
|
-
"--height": `${height}px`,
|
|
26
|
-
}}
|
|
27
|
-
onClick={handleClick}
|
|
28
|
-
/>
|
|
29
|
-
)
|
|
30
|
-
}
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import { track } from "@inglorious/engine/behaviors/input/mouse.js"
|
|
2
|
-
import { useRef } from "react"
|
|
3
|
-
import { useDispatch } from "react-redux"
|
|
4
|
-
|
|
5
|
-
import classes from "./scene.module.scss"
|
|
6
|
-
|
|
7
|
-
export default function Scene({ entities, children }) {
|
|
8
|
-
const dispatch = useDispatch()
|
|
9
|
-
|
|
10
|
-
const [, , width, height] = entities.game.bounds
|
|
11
|
-
|
|
12
|
-
const ref = useRef()
|
|
13
|
-
const mouseHandlers = track(ref.current, {
|
|
14
|
-
notify: (type, payload) => dispatch({ type, payload }),
|
|
15
|
-
})
|
|
16
|
-
|
|
17
|
-
return (
|
|
18
|
-
<div
|
|
19
|
-
className={classes.scene}
|
|
20
|
-
style={{ "--width": `${width}px`, "--height": `${height}px` }}
|
|
21
|
-
ref={ref}
|
|
22
|
-
{...mouseHandlers}
|
|
23
|
-
>
|
|
24
|
-
{children}
|
|
25
|
-
</div>
|
|
26
|
-
)
|
|
27
|
-
}
|
|
@@ -1,60 +0,0 @@
|
|
|
1
|
-
import "./sprite.module.css"
|
|
2
|
-
|
|
3
|
-
const DEFAULT_SCALE = 1
|
|
4
|
-
|
|
5
|
-
const FLIP = -1
|
|
6
|
-
const NO_FLIP = 1
|
|
7
|
-
|
|
8
|
-
const CENTER_WIDTH = 2
|
|
9
|
-
const CENTER_HEIGHT = 2
|
|
10
|
-
|
|
11
|
-
const FLIPPED_HORIZONTALLY_FLAG = 0x80000000
|
|
12
|
-
const FLIPPED_VERTICALLY_FLAG = 0x40000000
|
|
13
|
-
// eslint-disable-next-line no-unused-vars
|
|
14
|
-
const FLIPPED_DIAGONALLY_FLAG = 0x20000000
|
|
15
|
-
// eslint-disable-next-line no-unused-vars
|
|
16
|
-
const ROTATED_HEXAGONAL_120_FLAG = 0x10000000
|
|
17
|
-
|
|
18
|
-
export default function Sprite({ entity, className, style: customStyle }) {
|
|
19
|
-
const { image, frames, state, value } = entity.sprite
|
|
20
|
-
const { src, imageSize, tileSize, scale = DEFAULT_SCALE } = image
|
|
21
|
-
|
|
22
|
-
const [imageWidth] = imageSize
|
|
23
|
-
const [tileWidth, tileHeight] = tileSize
|
|
24
|
-
const cols = imageWidth / tileWidth
|
|
25
|
-
|
|
26
|
-
const flaggedTile = frames[state][value]
|
|
27
|
-
|
|
28
|
-
const isFlippedHorizontally = !!(flaggedTile & FLIPPED_HORIZONTALLY_FLAG)
|
|
29
|
-
const isFlippedVertically = !!(flaggedTile & FLIPPED_VERTICALLY_FLAG)
|
|
30
|
-
|
|
31
|
-
let tile = flaggedTile
|
|
32
|
-
tile &= ~FLIPPED_HORIZONTALLY_FLAG
|
|
33
|
-
tile &= ~FLIPPED_VERTICALLY_FLAG
|
|
34
|
-
|
|
35
|
-
const sx = tile % cols
|
|
36
|
-
const sy = Math.floor(tile / cols)
|
|
37
|
-
|
|
38
|
-
let transform = ""
|
|
39
|
-
transform += `translate(
|
|
40
|
-
${-tileWidth / CENTER_WIDTH}px,
|
|
41
|
-
${-tileHeight / CENTER_HEIGHT}px
|
|
42
|
-
)`
|
|
43
|
-
transform += `scale(
|
|
44
|
-
${isFlippedHorizontally ? FLIP : NO_FLIP},
|
|
45
|
-
${isFlippedVertically ? FLIP : NO_FLIP}
|
|
46
|
-
)`
|
|
47
|
-
transform += `scale(${scale})`
|
|
48
|
-
|
|
49
|
-
const style = {
|
|
50
|
-
...customStyle,
|
|
51
|
-
width: `${tileWidth}px`,
|
|
52
|
-
height: `${tileHeight}px`,
|
|
53
|
-
backgroundImage: `url(${src})`,
|
|
54
|
-
backgroundRepeat: `no-repeat`,
|
|
55
|
-
backgroundPosition: `-${sx * tileWidth}px -${sy * tileHeight}px`,
|
|
56
|
-
transform,
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
return <div className={className} style={style} />
|
|
60
|
-
}
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import { toString } from "@inglorious/utils/math/linear-algebra/vector.js"
|
|
2
|
-
|
|
3
|
-
const DECIMALS = 1
|
|
4
|
-
|
|
5
|
-
export default function Stats({ entity, entities, className, style }) {
|
|
6
|
-
const { acceleration, velocity, position, orientation, ay, vy } =
|
|
7
|
-
entities[entity.target]
|
|
8
|
-
|
|
9
|
-
return (
|
|
10
|
-
<div className={className} style={style}>
|
|
11
|
-
{acceleration && <div>Acceleration: {toString(acceleration)}</div>}
|
|
12
|
-
{velocity && <div>Velocity: {toString(velocity)}</div>}
|
|
13
|
-
{position && <div>Position: {toString(position)}</div>}
|
|
14
|
-
{orientation != null && (
|
|
15
|
-
<div>Orientation: {orientation.toFixed(DECIMALS)}</div>
|
|
16
|
-
)}
|
|
17
|
-
|
|
18
|
-
{ay != null && <div>ay: {ay.toFixed(DECIMALS)}</div>}
|
|
19
|
-
{vy != null && <div>vy: {vy.toFixed(DECIMALS)}</div>}
|
|
20
|
-
</div>
|
|
21
|
-
)
|
|
22
|
-
}
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import { zero } from "@inglorious/utils/math/linear-algebra/vector.js"
|
|
2
|
-
|
|
3
|
-
import classes from "./with-absolute-position.module.scss"
|
|
4
|
-
|
|
5
|
-
export function withAbsolutePosition(Component) {
|
|
6
|
-
return function AbsolutePosition(props) {
|
|
7
|
-
const [, , , screenHeight] = props.entities.game.bounds
|
|
8
|
-
const { position = zero() } = props.entity
|
|
9
|
-
|
|
10
|
-
const [x, y, z] = position
|
|
11
|
-
|
|
12
|
-
return (
|
|
13
|
-
<Component
|
|
14
|
-
{...props}
|
|
15
|
-
className={classes.withAbsolutePosition}
|
|
16
|
-
style={{ "--x": `${x}px`, "--y": `${screenHeight - y - z}px` }}
|
|
17
|
-
/>
|
|
18
|
-
)
|
|
19
|
-
}
|
|
20
|
-
}
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @typedef {import("./types").Conditions} Conditions
|
|
3
|
-
* @typedef {import("./types").Outcome} Outcome
|
|
4
|
-
* @typedef {import("./types").DecisionTree} DecisionTree
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* Traverses a decision tree recursively based on the provided conditions and returns the resulting node.
|
|
9
|
-
*
|
|
10
|
-
* @param {DecisionTree} tree - The decision tree to traverse. Each node contains:
|
|
11
|
-
* - `test` {Function}: A function that evaluates the conditions and determines the next branch.
|
|
12
|
-
* - Branches keyed by the possible outcomes of the `test` function.
|
|
13
|
-
* @param {Conditions} conditions - The conditions object passed to the `test` function at each node.
|
|
14
|
-
* @returns {Outcome | DecisionTree} - The resulting outcome or decision tree itself after traversing the decision tree.
|
|
15
|
-
*/
|
|
16
|
-
export function decide(tree, conditions) {
|
|
17
|
-
if (!tree.test) {
|
|
18
|
-
return tree
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
const value = tree.test(conditions)
|
|
22
|
-
|
|
23
|
-
return tree[value] && decide(tree[value](conditions), conditions)
|
|
24
|
-
}
|
|
@@ -1,153 +0,0 @@
|
|
|
1
|
-
import { length } from "@inglorious/utils/math/linear-algebra/vector.js"
|
|
2
|
-
import { subtract } from "@inglorious/utils/math/linear-algebra/vectors.js"
|
|
3
|
-
import { expect, test } from "vitest"
|
|
4
|
-
|
|
5
|
-
import { decide } from "./decision-tree.js"
|
|
6
|
-
|
|
7
|
-
test("it should make a decision based on a binary decision tree", () => {
|
|
8
|
-
const entity = { state: "sleeping", position: [0, 0, 0] }
|
|
9
|
-
const target = { position: [10, 0, 0] }
|
|
10
|
-
const tree = {
|
|
11
|
-
test({ entity }) {
|
|
12
|
-
return entity.state === "idle"
|
|
13
|
-
},
|
|
14
|
-
true() {
|
|
15
|
-
return {
|
|
16
|
-
test({ entity, target }) {
|
|
17
|
-
const distance = length(subtract(target.position, entity.position))
|
|
18
|
-
return distance < 250
|
|
19
|
-
},
|
|
20
|
-
true() {
|
|
21
|
-
return "aware"
|
|
22
|
-
},
|
|
23
|
-
}
|
|
24
|
-
},
|
|
25
|
-
false() {
|
|
26
|
-
return {
|
|
27
|
-
test({ entity }) {
|
|
28
|
-
return entity.state === "chasing"
|
|
29
|
-
},
|
|
30
|
-
true() {
|
|
31
|
-
return {
|
|
32
|
-
test({ entity, target }) {
|
|
33
|
-
const distance = length(
|
|
34
|
-
subtract(target.position, entity.position),
|
|
35
|
-
)
|
|
36
|
-
return distance >= 250
|
|
37
|
-
},
|
|
38
|
-
true() {
|
|
39
|
-
return "idle"
|
|
40
|
-
},
|
|
41
|
-
false() {
|
|
42
|
-
return {
|
|
43
|
-
test({ entity, target }) {
|
|
44
|
-
const distance = length(
|
|
45
|
-
subtract(target.position, entity.position),
|
|
46
|
-
)
|
|
47
|
-
return distance < 10
|
|
48
|
-
},
|
|
49
|
-
true() {
|
|
50
|
-
return "sleepy"
|
|
51
|
-
},
|
|
52
|
-
}
|
|
53
|
-
},
|
|
54
|
-
}
|
|
55
|
-
},
|
|
56
|
-
false() {
|
|
57
|
-
return {
|
|
58
|
-
test({ entity }) {
|
|
59
|
-
return ["sleepy", "sleeping"].includes(entity.state)
|
|
60
|
-
},
|
|
61
|
-
true() {
|
|
62
|
-
return {
|
|
63
|
-
test({ entity, target }) {
|
|
64
|
-
const distance = length(
|
|
65
|
-
subtract(target.position, entity.position),
|
|
66
|
-
)
|
|
67
|
-
return distance >= 10
|
|
68
|
-
},
|
|
69
|
-
true() {
|
|
70
|
-
return "aware"
|
|
71
|
-
},
|
|
72
|
-
}
|
|
73
|
-
},
|
|
74
|
-
}
|
|
75
|
-
},
|
|
76
|
-
}
|
|
77
|
-
},
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
const state = decide(tree, { entity, target })
|
|
81
|
-
|
|
82
|
-
expect(state).toBe("aware")
|
|
83
|
-
})
|
|
84
|
-
|
|
85
|
-
test("it should make a decision on a multi-child tree", () => {
|
|
86
|
-
const entity = { state: "sleeping", position: [0, 0, 0] }
|
|
87
|
-
const target = { position: [10, 0, 0] }
|
|
88
|
-
const wakeUp = () => ({
|
|
89
|
-
test({ entity, target }) {
|
|
90
|
-
const distance = length(subtract(target.position, entity.position))
|
|
91
|
-
return distance >= 10
|
|
92
|
-
},
|
|
93
|
-
true() {
|
|
94
|
-
return "aware"
|
|
95
|
-
},
|
|
96
|
-
false({ entity }) {
|
|
97
|
-
return entity.state
|
|
98
|
-
},
|
|
99
|
-
})
|
|
100
|
-
|
|
101
|
-
const tree = {
|
|
102
|
-
test({ entity }) {
|
|
103
|
-
return entity.state
|
|
104
|
-
},
|
|
105
|
-
idle() {
|
|
106
|
-
return {
|
|
107
|
-
test({ entity, target }) {
|
|
108
|
-
const distance = length(subtract(target.position, entity.position))
|
|
109
|
-
return distance < 250
|
|
110
|
-
},
|
|
111
|
-
true() {
|
|
112
|
-
return "aware"
|
|
113
|
-
},
|
|
114
|
-
false({ entity }) {
|
|
115
|
-
return entity.state
|
|
116
|
-
},
|
|
117
|
-
}
|
|
118
|
-
},
|
|
119
|
-
chasing() {
|
|
120
|
-
return {
|
|
121
|
-
test({ entity, target }) {
|
|
122
|
-
const distance = length(subtract(target.position, entity.position))
|
|
123
|
-
return distance >= 250
|
|
124
|
-
},
|
|
125
|
-
true() {
|
|
126
|
-
return "idle"
|
|
127
|
-
},
|
|
128
|
-
false() {
|
|
129
|
-
return {
|
|
130
|
-
test({ entity, target }) {
|
|
131
|
-
const distance = length(
|
|
132
|
-
subtract(target.position, entity.position),
|
|
133
|
-
)
|
|
134
|
-
return distance < 10
|
|
135
|
-
},
|
|
136
|
-
true() {
|
|
137
|
-
return "sleepy"
|
|
138
|
-
},
|
|
139
|
-
false({ entity }) {
|
|
140
|
-
return entity.state
|
|
141
|
-
},
|
|
142
|
-
}
|
|
143
|
-
},
|
|
144
|
-
}
|
|
145
|
-
},
|
|
146
|
-
sleepy: wakeUp,
|
|
147
|
-
sleeping: wakeUp,
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
const state = decide(tree, { entity, target })
|
|
151
|
-
|
|
152
|
-
expect(state).toBe("aware")
|
|
153
|
-
})
|
|
@@ -1,155 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @typedef {import("./types").Node} Node
|
|
3
|
-
* @typedef {import("./types").Graph} Graph
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
import {
|
|
7
|
-
contains,
|
|
8
|
-
push,
|
|
9
|
-
remove,
|
|
10
|
-
root,
|
|
11
|
-
} from "@inglorious/utils/data-structures/heap.js"
|
|
12
|
-
import { abs, magnitude } from "@inglorious/utils/math/linear-algebra/vector.js"
|
|
13
|
-
import { subtract } from "@inglorious/utils/math/linear-algebra/vectors.js"
|
|
14
|
-
|
|
15
|
-
const NO_COST = 0
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* Calculates the cost of a path using Dijkstra's algorithm.
|
|
19
|
-
* In this simplified form the cost is simply 0.
|
|
20
|
-
*
|
|
21
|
-
* @returns {number} - The cost of the path (which is 0).
|
|
22
|
-
*/
|
|
23
|
-
export const dijkstra = () => NO_COST
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* Calculates the Euclidean distance between two nodes.
|
|
27
|
-
*
|
|
28
|
-
* @param {Node} a - The first node.
|
|
29
|
-
* @param {Node} b - The second node.
|
|
30
|
-
* @returns {number} - The Euclidean distance between the two nodes.
|
|
31
|
-
*/
|
|
32
|
-
export const eucledianDistance = (a, b) =>
|
|
33
|
-
magnitude(subtract(a.position, b.position))
|
|
34
|
-
|
|
35
|
-
/**
|
|
36
|
-
* Calculates the Manhattan distance between two nodes.
|
|
37
|
-
*
|
|
38
|
-
* @param {Node} a - The first node.
|
|
39
|
-
* @param {Node} b - The second node.
|
|
40
|
-
* @returns {number} - The Manhattan distance between the two nodes.
|
|
41
|
-
*/
|
|
42
|
-
export const manhattanDistance = (a, b) => abs(subtract(a.position, b.position))
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* Compares the cost of two nodes.
|
|
46
|
-
*
|
|
47
|
-
* @param {Node} a - The first node.
|
|
48
|
-
* @param {Node} b - The second node.
|
|
49
|
-
* @returns {number} - The difference in cost between the two nodes.
|
|
50
|
-
*/
|
|
51
|
-
const compareCost = (a, b) => b.cost - a.cost
|
|
52
|
-
|
|
53
|
-
/**
|
|
54
|
-
* Compares the total cost of two nodes.
|
|
55
|
-
*
|
|
56
|
-
* @param {Node} a - The first node.
|
|
57
|
-
* @param {Node} b - The second node.
|
|
58
|
-
* @returns {number} - The difference in total cost between the two nodes.
|
|
59
|
-
*/
|
|
60
|
-
const compareTotalCost = (a, b) => b.totalCost - a.totalCost
|
|
61
|
-
|
|
62
|
-
/**
|
|
63
|
-
* Finds the shortest path in a graph from a start node to an end node using a heuristic.
|
|
64
|
-
*
|
|
65
|
-
* @param {Graph} graph - The graph containing nodes and arcs.
|
|
66
|
-
* @param {string} start - The ID of the start node.
|
|
67
|
-
* @param {string} end - The ID of the end node.
|
|
68
|
-
* @param {(a: Node, b: Node) => number} [heuristic=eucledianDistance] - The heuristic function to estimate the cost.
|
|
69
|
-
* @returns {string[]} - An array of node IDs representing the shortest path.
|
|
70
|
-
*/
|
|
71
|
-
export function findPath(graph, start, end, heuristic = eucledianDistance) {
|
|
72
|
-
const { nodes, arcs } = adaptGraph(graph)
|
|
73
|
-
const findNode = createFindNode(nodes)
|
|
74
|
-
|
|
75
|
-
const startNode = findNode(start)
|
|
76
|
-
const endNode = findNode(end)
|
|
77
|
-
let discoveredNodes = [startNode]
|
|
78
|
-
let evaluatedNodes = []
|
|
79
|
-
let path = []
|
|
80
|
-
|
|
81
|
-
while (discoveredNodes.length) {
|
|
82
|
-
let current = root(discoveredNodes, compareTotalCost)
|
|
83
|
-
|
|
84
|
-
if (current.id === end) {
|
|
85
|
-
while (current != null) {
|
|
86
|
-
path = [current.id, ...path]
|
|
87
|
-
current = current.previous
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
return path
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
discoveredNodes = remove(discoveredNodes, current)
|
|
94
|
-
evaluatedNodes = push(evaluatedNodes, current)
|
|
95
|
-
|
|
96
|
-
const outgoingArcs = arcs.filter(({ from }) => from === current.id)
|
|
97
|
-
const destinations = outgoingArcs.map(({ to }) => findNode(to))
|
|
98
|
-
|
|
99
|
-
for (const destination of destinations) {
|
|
100
|
-
const totalCost =
|
|
101
|
-
current.cost +
|
|
102
|
-
destination.cost +
|
|
103
|
-
heuristic(current, destination) +
|
|
104
|
-
outgoingArcs.find(({ to }) => to === destination.id)?.cost
|
|
105
|
-
|
|
106
|
-
let isNewPathFound = false
|
|
107
|
-
if (contains(discoveredNodes, destination)) {
|
|
108
|
-
if (totalCost < destination.cost) {
|
|
109
|
-
destination.cost = totalCost
|
|
110
|
-
isNewPathFound = true
|
|
111
|
-
}
|
|
112
|
-
} else {
|
|
113
|
-
destination.cost = totalCost
|
|
114
|
-
isNewPathFound = true
|
|
115
|
-
discoveredNodes = push(discoveredNodes, destination, compareCost)
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
if (isNewPathFound) {
|
|
119
|
-
destination.totalCost =
|
|
120
|
-
destination.cost + heuristic(destination, endNode)
|
|
121
|
-
destination.previous = current
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
/**
|
|
128
|
-
* Adapts a graph to ensure all nodes and arcs have a cost property.
|
|
129
|
-
*
|
|
130
|
-
* @param {Graph} graph - The graph to adapt.
|
|
131
|
-
* @returns {Graph} - The adapted graph with cost properties added.
|
|
132
|
-
*/
|
|
133
|
-
function adaptGraph(graph) {
|
|
134
|
-
return {
|
|
135
|
-
...graph,
|
|
136
|
-
nodes: Array.isArray(graph.nodes)
|
|
137
|
-
? graph.nodes.map((node) => ({ ...node, cost: node.cost ?? NO_COST }))
|
|
138
|
-
: Object.entries(graph.nodes).map(([key, value]) => ({
|
|
139
|
-
id: key,
|
|
140
|
-
position: value,
|
|
141
|
-
cost: 0,
|
|
142
|
-
})),
|
|
143
|
-
arcs: graph.arcs.map((arc) => ({ ...arc, cost: arc.cost ?? NO_COST })),
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
/**
|
|
148
|
-
* Creates a function that finds a node by its ID.
|
|
149
|
-
*
|
|
150
|
-
* @param {Node[]} nodes - The array of nodes.
|
|
151
|
-
* @returns {(id: string) => Node} - A function that finds a node by its ID.
|
|
152
|
-
*/
|
|
153
|
-
function createFindNode(nodes) {
|
|
154
|
-
return (id) => nodes.find((node) => node.id === id)
|
|
155
|
-
}
|