@inglorious/engine 0.1.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.
Files changed (256) hide show
  1. package/LICENSE +9 -0
  2. package/README.md +72 -0
  3. package/package.json +76 -0
  4. package/src/docs/ai/movement/dynamic/align.js +131 -0
  5. package/src/docs/ai/movement/dynamic/arrive.js +88 -0
  6. package/src/docs/ai/movement/dynamic/dynamic.mdx +99 -0
  7. package/src/docs/ai/movement/dynamic/dynamic.stories.js +58 -0
  8. package/src/docs/ai/movement/dynamic/evade.js +72 -0
  9. package/src/docs/ai/movement/dynamic/face.js +90 -0
  10. package/src/docs/ai/movement/dynamic/flee.js +38 -0
  11. package/src/docs/ai/movement/dynamic/look-where-youre-going.js +114 -0
  12. package/src/docs/ai/movement/dynamic/match-velocity.js +92 -0
  13. package/src/docs/ai/movement/dynamic/pursue.js +72 -0
  14. package/src/docs/ai/movement/dynamic/seek.js +37 -0
  15. package/src/docs/ai/movement/dynamic/wander.js +71 -0
  16. package/src/docs/ai/movement/kinematic/align.js +122 -0
  17. package/src/docs/ai/movement/kinematic/arrive.js +78 -0
  18. package/src/docs/ai/movement/kinematic/face.js +82 -0
  19. package/src/docs/ai/movement/kinematic/flee.js +36 -0
  20. package/src/docs/ai/movement/kinematic/kinematic.mdx +67 -0
  21. package/src/docs/ai/movement/kinematic/kinematic.stories.js +42 -0
  22. package/src/docs/ai/movement/kinematic/seek.js +34 -0
  23. package/src/docs/ai/movement/kinematic/wander-as-seek.js +62 -0
  24. package/src/docs/ai/movement/kinematic/wander.js +28 -0
  25. package/src/docs/bounds.js +7 -0
  26. package/src/docs/code-reuse.js +35 -0
  27. package/src/docs/collision/circles.js +58 -0
  28. package/src/docs/collision/collision.mdx +27 -0
  29. package/src/docs/collision/collision.stories.js +22 -0
  30. package/src/docs/collision/platform.js +76 -0
  31. package/src/docs/collision/tilemap.js +181 -0
  32. package/src/docs/empty.js +1 -0
  33. package/src/docs/engine.mdx +81 -0
  34. package/src/docs/engine.stories.js +37 -0
  35. package/src/docs/event-handlers.js +68 -0
  36. package/src/docs/framerate.js +37 -0
  37. package/src/docs/game.jsx +15 -0
  38. package/src/docs/image/image.js +19 -0
  39. package/src/docs/image/image.stories.js +22 -0
  40. package/src/docs/image/sprite.js +39 -0
  41. package/src/docs/image/tilemap.js +84 -0
  42. package/src/docs/input/controls.js +67 -0
  43. package/src/docs/input/gamepad.js +67 -0
  44. package/src/docs/input/input.mdx +55 -0
  45. package/src/docs/input/input.stories.js +27 -0
  46. package/src/docs/input/keyboard.js +58 -0
  47. package/src/docs/input/mouse.js +32 -0
  48. package/src/docs/instances.js +49 -0
  49. package/src/docs/player/dynamic/double-jump.js +90 -0
  50. package/src/docs/player/dynamic/dynamic.stories.js +32 -0
  51. package/src/docs/player/dynamic/jump.js +83 -0
  52. package/src/docs/player/dynamic/modern-controls.js +57 -0
  53. package/src/docs/player/dynamic/shooter-controls.js +51 -0
  54. package/src/docs/player/dynamic/tank-controls.js +44 -0
  55. package/src/docs/player/kinematic/double-jump.js +90 -0
  56. package/src/docs/player/kinematic/jump.js +82 -0
  57. package/src/docs/player/kinematic/kinematic.stories.js +32 -0
  58. package/src/docs/player/kinematic/modern-controls.js +56 -0
  59. package/src/docs/player/kinematic/shooter-controls.js +48 -0
  60. package/src/docs/player/kinematic/tank-controls.js +42 -0
  61. package/src/docs/quick-start/first-game.js +49 -0
  62. package/src/docs/quick-start/hello-world.js +1 -0
  63. package/src/docs/quick-start.mdx +127 -0
  64. package/src/docs/quick-start.stories.js +17 -0
  65. package/src/docs/recipes/add-and-remove.js +71 -0
  66. package/src/docs/recipes/add-instance.js +42 -0
  67. package/src/docs/recipes/decision-tree.js +169 -0
  68. package/src/docs/recipes/random-instances.js +25 -0
  69. package/src/docs/recipes/recipes.mdx +81 -0
  70. package/src/docs/recipes/recipes.stories.js +37 -0
  71. package/src/docs/recipes/remove-instance.js +52 -0
  72. package/src/docs/recipes/states.js +64 -0
  73. package/src/docs/ui/button.js +28 -0
  74. package/src/docs/ui/form.stories.js +55 -0
  75. package/src/docs/ui-chooser.jsx +6 -0
  76. package/src/docs/utils/data-structures/object.mdx +47 -0
  77. package/src/docs/utils/data-structures/objects.mdx +30 -0
  78. package/src/docs/utils/functions/functions.mdx +34 -0
  79. package/src/docs/utils/math/geometry/circle.mdx +55 -0
  80. package/src/docs/utils/math/geometry/point.mdx +38 -0
  81. package/src/docs/utils/math/geometry/rectangle.mdx +24 -0
  82. package/src/docs/utils/math/geometry/segment.mdx +55 -0
  83. package/src/docs/utils/math/geometry/triangle.mdx +22 -0
  84. package/src/docs/utils/math/linear-algebra/2d.mdx +22 -0
  85. package/src/docs/utils/math/linear-algebra/quaternion.mdx +21 -0
  86. package/src/docs/utils/math/linear-algebra/quaternions.mdx +22 -0
  87. package/src/docs/utils/math/linear-algebra/vector.mdx +177 -0
  88. package/src/docs/utils/math/linear-algebra/vectors.mdx +58 -0
  89. package/src/docs/utils/math/numbers.mdx +76 -0
  90. package/src/docs/utils/math/random.mdx +35 -0
  91. package/src/docs/utils/math/statistics.mdx +38 -0
  92. package/src/docs/utils/math/trigonometry.mdx +85 -0
  93. package/src/docs/utils/physics/friction.mdx +20 -0
  94. package/src/docs/utils/physics/gravity.mdx +28 -0
  95. package/src/engine/ai/movement/dynamic/align.js +63 -0
  96. package/src/engine/ai/movement/dynamic/arrive.js +43 -0
  97. package/src/engine/ai/movement/dynamic/evade.js +38 -0
  98. package/src/engine/ai/movement/dynamic/face.js +20 -0
  99. package/src/engine/ai/movement/dynamic/flee.js +45 -0
  100. package/src/engine/ai/movement/dynamic/look-where-youre-going.js +17 -0
  101. package/src/engine/ai/movement/dynamic/match-velocity.js +50 -0
  102. package/src/engine/ai/movement/dynamic/pursue.js +38 -0
  103. package/src/engine/ai/movement/dynamic/seek.js +44 -0
  104. package/src/engine/ai/movement/dynamic/wander.js +32 -0
  105. package/src/engine/ai/movement/kinematic/align.js +37 -0
  106. package/src/engine/ai/movement/kinematic/arrive.js +42 -0
  107. package/src/engine/ai/movement/kinematic/face.js +20 -0
  108. package/src/engine/ai/movement/kinematic/flee.js +26 -0
  109. package/src/engine/ai/movement/kinematic/seek.js +26 -0
  110. package/src/engine/ai/movement/kinematic/seek.test.js +42 -0
  111. package/src/engine/ai/movement/kinematic/wander-as-seek.js +31 -0
  112. package/src/engine/ai/movement/kinematic/wander.js +27 -0
  113. package/src/engine/collision/detection.js +115 -0
  114. package/src/engine/loop/animation-frame.js +26 -0
  115. package/src/engine/loop/elapsed.js +23 -0
  116. package/src/engine/loop/fixed.js +28 -0
  117. package/src/engine/loop/flash.js +14 -0
  118. package/src/engine/loop/lag.js +27 -0
  119. package/src/engine/loop.js +15 -0
  120. package/src/engine/movement/dynamic/modern.js +24 -0
  121. package/src/engine/movement/dynamic/tank.js +43 -0
  122. package/src/engine/movement/kinematic/modern.js +16 -0
  123. package/src/engine/movement/kinematic/modern.test.js +27 -0
  124. package/src/engine/movement/kinematic/tank.js +27 -0
  125. package/src/engine/store.js +174 -0
  126. package/src/engine/store.test.js +256 -0
  127. package/src/engine.js +74 -0
  128. package/src/game/animation.js +26 -0
  129. package/src/game/bounds.js +66 -0
  130. package/src/game/decorators/character.js +5 -0
  131. package/src/game/decorators/clamp-to-bounds.js +15 -0
  132. package/src/game/decorators/collisions.js +24 -0
  133. package/src/game/decorators/controls/dynamic/modern.js +48 -0
  134. package/src/game/decorators/controls/dynamic/shooter.js +47 -0
  135. package/src/game/decorators/controls/dynamic/tank.js +55 -0
  136. package/src/game/decorators/controls/kinematic/modern.js +49 -0
  137. package/src/game/decorators/controls/kinematic/shooter.js +45 -0
  138. package/src/game/decorators/controls/kinematic/tank.js +52 -0
  139. package/src/game/decorators/debug/collisions.js +32 -0
  140. package/src/game/decorators/double-jump.js +70 -0
  141. package/src/game/decorators/fps.js +30 -0
  142. package/src/game/decorators/fsm.js +27 -0
  143. package/src/game/decorators/fsm.test.js +56 -0
  144. package/src/game/decorators/game.js +11 -0
  145. package/src/game/decorators/image/image.js +5 -0
  146. package/src/game/decorators/image/sprite.js +5 -0
  147. package/src/game/decorators/image/tilemap.js +5 -0
  148. package/src/game/decorators/input/controls.js +27 -0
  149. package/src/game/decorators/input/gamepad.js +74 -0
  150. package/src/game/decorators/input/input.js +41 -0
  151. package/src/game/decorators/input/keyboard.js +49 -0
  152. package/src/game/decorators/input/mouse.js +65 -0
  153. package/src/game/decorators/jump.js +72 -0
  154. package/src/game/decorators/platform.js +5 -0
  155. package/src/game/decorators/ui/button.js +21 -0
  156. package/src/game/sprite.js +119 -0
  157. package/src/main.js +5 -0
  158. package/src/ui/canvas/absolute-position.js +17 -0
  159. package/src/ui/canvas/character.js +35 -0
  160. package/src/ui/canvas/form/button.js +25 -0
  161. package/src/ui/canvas/fps.js +18 -0
  162. package/src/ui/canvas/image/hitmask.js +37 -0
  163. package/src/ui/canvas/image/image.js +37 -0
  164. package/src/ui/canvas/image/sprite.js +49 -0
  165. package/src/ui/canvas/image/tilemap.js +64 -0
  166. package/src/ui/canvas/mouse.js +37 -0
  167. package/src/ui/canvas/shapes/circle.js +31 -0
  168. package/src/ui/canvas/shapes/rectangle.js +31 -0
  169. package/src/ui/canvas.js +81 -0
  170. package/src/ui/react/game/character/character.module.scss +17 -0
  171. package/src/ui/react/game/character/index.jsx +30 -0
  172. package/src/ui/react/game/cursor/cursor.module.scss +47 -0
  173. package/src/ui/react/game/cursor/index.jsx +20 -0
  174. package/src/ui/react/game/form/fields/field/field.module.scss +5 -0
  175. package/src/ui/react/game/form/fields/field/index.jsx +56 -0
  176. package/src/ui/react/game/form/fields/fields.module.scss +48 -0
  177. package/src/ui/react/game/form/fields/index.jsx +12 -0
  178. package/src/ui/react/game/form/form.module.scss +18 -0
  179. package/src/ui/react/game/form/index.jsx +22 -0
  180. package/src/ui/react/game/fps/index.jsx +16 -0
  181. package/src/ui/react/game/game.jsx +71 -0
  182. package/src/ui/react/game/index.jsx +29 -0
  183. package/src/ui/react/game/platform/index.jsx +30 -0
  184. package/src/ui/react/game/platform/platform.module.scss +7 -0
  185. package/src/ui/react/game/scene/index.jsx +25 -0
  186. package/src/ui/react/game/scene/scene.module.scss +9 -0
  187. package/src/ui/react/game/sprite/index.jsx +58 -0
  188. package/src/ui/react/game/sprite/sprite.module.css +3 -0
  189. package/src/ui/react/game/stats/index.jsx +22 -0
  190. package/src/ui/react/hocs/with-absolute-position/index.jsx +20 -0
  191. package/src/ui/react/hocs/with-absolute-position/with-absolute-position.module.scss +5 -0
  192. package/src/ui/react/index.jsx +9 -0
  193. package/src/utils/algorithms/decision-tree.js +24 -0
  194. package/src/utils/algorithms/decision-tree.test.js +102 -0
  195. package/src/utils/algorithms/path-finding.js +155 -0
  196. package/src/utils/algorithms/path-finding.test.js +151 -0
  197. package/src/utils/algorithms/types.d.ts +28 -0
  198. package/src/utils/data-structures/array.js +83 -0
  199. package/src/utils/data-structures/array.test.js +173 -0
  200. package/src/utils/data-structures/board.js +159 -0
  201. package/src/utils/data-structures/board.test.js +242 -0
  202. package/src/utils/data-structures/boolean.js +9 -0
  203. package/src/utils/data-structures/heap.js +164 -0
  204. package/src/utils/data-structures/heap.test.js +103 -0
  205. package/src/utils/data-structures/object.js +102 -0
  206. package/src/utils/data-structures/object.test.js +121 -0
  207. package/src/utils/data-structures/objects.js +48 -0
  208. package/src/utils/data-structures/objects.test.js +99 -0
  209. package/src/utils/data-structures/tree.js +36 -0
  210. package/src/utils/data-structures/tree.test.js +33 -0
  211. package/src/utils/data-structures/types.d.ts +4 -0
  212. package/src/utils/functions/functions.js +19 -0
  213. package/src/utils/functions/functions.test.js +23 -0
  214. package/src/utils/math/geometry/circle.js +117 -0
  215. package/src/utils/math/geometry/circle.test.js +97 -0
  216. package/src/utils/math/geometry/hitmask.js +39 -0
  217. package/src/utils/math/geometry/hitmask.test.js +84 -0
  218. package/src/utils/math/geometry/line.js +35 -0
  219. package/src/utils/math/geometry/line.test.js +49 -0
  220. package/src/utils/math/geometry/platform.js +42 -0
  221. package/src/utils/math/geometry/platform.test.js +133 -0
  222. package/src/utils/math/geometry/point.js +71 -0
  223. package/src/utils/math/geometry/point.test.js +81 -0
  224. package/src/utils/math/geometry/rectangle.js +45 -0
  225. package/src/utils/math/geometry/rectangle.test.js +42 -0
  226. package/src/utils/math/geometry/segment.js +80 -0
  227. package/src/utils/math/geometry/segment.test.js +183 -0
  228. package/src/utils/math/geometry/triangle.js +15 -0
  229. package/src/utils/math/geometry/triangle.test.js +11 -0
  230. package/src/utils/math/geometry/types.d.ts +23 -0
  231. package/src/utils/math/linear-algebra/2d.js +28 -0
  232. package/src/utils/math/linear-algebra/2d.test.js +17 -0
  233. package/src/utils/math/linear-algebra/quaternion.js +22 -0
  234. package/src/utils/math/linear-algebra/quaternion.test.js +25 -0
  235. package/src/utils/math/linear-algebra/quaternions.js +20 -0
  236. package/src/utils/math/linear-algebra/quaternions.test.js +29 -0
  237. package/src/utils/math/linear-algebra/types.d.ts +4 -0
  238. package/src/utils/math/linear-algebra/vector.js +302 -0
  239. package/src/utils/math/linear-algebra/vector.test.js +257 -0
  240. package/src/utils/math/linear-algebra/vectors.js +122 -0
  241. package/src/utils/math/linear-algebra/vectors.test.js +65 -0
  242. package/src/utils/math/numbers.js +90 -0
  243. package/src/utils/math/numbers.test.js +137 -0
  244. package/src/utils/math/rng.js +44 -0
  245. package/src/utils/math/rng.test.js +39 -0
  246. package/src/utils/math/statistics.js +43 -0
  247. package/src/utils/math/statistics.test.js +47 -0
  248. package/src/utils/math/trigonometry.js +89 -0
  249. package/src/utils/math/trigonometry.test.js +52 -0
  250. package/src/utils/physics/acceleration.js +63 -0
  251. package/src/utils/physics/friction.js +30 -0
  252. package/src/utils/physics/friction.test.js +44 -0
  253. package/src/utils/physics/gravity.js +71 -0
  254. package/src/utils/physics/gravity.test.js +80 -0
  255. package/src/utils/physics/jump.js +41 -0
  256. package/src/utils/physics/velocity.js +38 -0
@@ -0,0 +1,29 @@
1
+ import Engine from "@inglorious/engine"
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
+ }
@@ -0,0 +1,30 @@
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, instance, className, style }) {
10
+ const notify = useDispatch()
11
+
12
+ const [width, height] = instance.size ?? DEFAULT_SIZE
13
+
14
+ const handleClick = (event) => {
15
+ event.stopPropagation()
16
+ notify({ id: "instance:click", 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
+ }
@@ -0,0 +1,7 @@
1
+ .platform {
2
+ width: var(--width);
3
+ height: var(--height);
4
+ box-sizing: border-box;
5
+ background-color: lightgrey;
6
+ border: 1px solid grey;
7
+ }
@@ -0,0 +1,25 @@
1
+ import { track } from "@inglorious/game/decorators/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({ instances, children }) {
8
+ const notify = useDispatch()
9
+
10
+ const [, , width, height] = instances.game.bounds
11
+
12
+ const ref = useRef()
13
+ const mouseHandlers = track(ref.current, { notify })
14
+
15
+ return (
16
+ <div
17
+ className={classes.scene}
18
+ style={{ "--width": `${width}px`, "--height": `${height}px` }}
19
+ ref={ref}
20
+ {...mouseHandlers}
21
+ >
22
+ {children}
23
+ </div>
24
+ )
25
+ }
@@ -0,0 +1,9 @@
1
+ .scene {
2
+ background-color: lightgrey;
3
+ position: relative;
4
+ width: var(--width);
5
+ height: var(--height);
6
+ overflow: hidden;
7
+ cursor: none;
8
+ user-select: none;
9
+ }
@@ -0,0 +1,58 @@
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
+ // const FLIPPED_DIAGONALLY_FLAG = 0x20000000
14
+ // const ROTATED_HEXAGONAL_120_FLAG = 0x10000000
15
+
16
+ export default function Sprite({ instance, className, style: customStyle }) {
17
+ const { image, frames, state, value } = instance.sprite
18
+ const { src, imageSize, tileSize, scale = DEFAULT_SCALE } = image
19
+
20
+ const [imageWidth] = imageSize
21
+ const [tileWidth, tileHeight] = tileSize
22
+ const cols = imageWidth / tileWidth
23
+
24
+ const flaggedTile = frames[state][value]
25
+
26
+ const isFlippedHorizontally = !!(flaggedTile & FLIPPED_HORIZONTALLY_FLAG)
27
+ const isFlippedVertically = !!(flaggedTile & FLIPPED_VERTICALLY_FLAG)
28
+
29
+ let tile = flaggedTile
30
+ tile &= ~FLIPPED_HORIZONTALLY_FLAG
31
+ tile &= ~FLIPPED_VERTICALLY_FLAG
32
+
33
+ const sx = tile % cols
34
+ const sy = Math.floor(tile / cols)
35
+
36
+ let transform = ""
37
+ transform += `translate(
38
+ ${-tileWidth / CENTER_WIDTH}px,
39
+ ${-tileHeight / CENTER_HEIGHT}px
40
+ )`
41
+ transform += `scale(
42
+ ${isFlippedHorizontally ? FLIP : NO_FLIP},
43
+ ${isFlippedVertically ? FLIP : NO_FLIP}
44
+ )`
45
+ transform += `scale(${scale})`
46
+
47
+ const style = {
48
+ ...customStyle,
49
+ width: `${tileWidth}px`,
50
+ height: `${tileHeight}px`,
51
+ backgroundImage: `url(${src})`,
52
+ backgroundRepeat: `no-repeat`,
53
+ backgroundPosition: `-${sx * tileWidth}px -${sy * tileHeight}px`,
54
+ transform,
55
+ }
56
+
57
+ return <div className={className} style={style} />
58
+ }
@@ -0,0 +1,3 @@
1
+ * {
2
+ image-rendering: pixelated;
3
+ }
@@ -0,0 +1,22 @@
1
+ import { toString } from "@inglorious/utils/math/linear-algebra/vector.js"
2
+
3
+ const DECIMALS = 1
4
+
5
+ export default function Stats({ instance, instances, className, style }) {
6
+ const { acceleration, velocity, position, orientation, ay, vy } =
7
+ instances[instance.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
+ }
@@ -0,0 +1,20 @@
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.instances.game.bounds
8
+ const { position = zero() } = props.instance
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
+ }
@@ -0,0 +1,5 @@
1
+ .withAbsolutePosition {
2
+ position: absolute;
3
+ top: var(--y);
4
+ left: var(--x);
5
+ }
@@ -0,0 +1,9 @@
1
+ import ReactDOM from "react-dom/client"
2
+
3
+ import Game from "./game/index.jsx"
4
+
5
+ export function start(game) {
6
+ ReactDOM.createRoot(document.getElementById("root")).render(
7
+ <Game config={game} />,
8
+ )
9
+ }
@@ -0,0 +1,24 @@
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
+ }
@@ -0,0 +1,102 @@
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 instance = { state: "sleeping", position: [0, 0, 0] }
9
+ const target = { position: [10, 0, 0] }
10
+ const tree = {
11
+ test: ({ instance }) => instance.state === "idle",
12
+ true: () => ({
13
+ test: ({ instance, mouse }) => {
14
+ const distance = length(subtract(mouse.position, instance.position))
15
+ return distance < 250
16
+ },
17
+ true: () => "aware",
18
+ }),
19
+ false: () => ({
20
+ test: ({ instance }) => instance.state === "chasing",
21
+ true: () => ({
22
+ test: ({ instance, mouse }) => {
23
+ const distance = length(subtract(mouse.position, instance.position))
24
+ return distance >= 250
25
+ },
26
+ true: () => "idle",
27
+ false: () => ({
28
+ test: ({ instance, mouse }) => {
29
+ const distance = length(subtract(mouse.position, instance.position))
30
+ return distance < 10
31
+ },
32
+ true: () => "sleepy",
33
+ }),
34
+ }),
35
+ false: () => ({
36
+ test: ({ instance }) => ["sleepy", "sleeping"].includes(instance.state),
37
+ true: () => ({
38
+ test: ({ instance, mouse }) => {
39
+ const distance = length(subtract(mouse.position, instance.position))
40
+ return distance >= 10
41
+ },
42
+ true: () => "aware",
43
+ }),
44
+ }),
45
+ }),
46
+ }
47
+
48
+ const state = decide(tree, { instance, mouse: target })
49
+
50
+ expect(state).toBe("aware")
51
+ })
52
+
53
+ test("it should make a decision on a multi-child tree", () => {
54
+ const instance = { state: "sleeping", position: [0, 0, 0] }
55
+ const target = { position: [10, 0, 0] }
56
+ const tree = {
57
+ test: ({ instance }) => instance.state,
58
+ idle: () => ({
59
+ test: ({ instance, target }) => {
60
+ const distance = length(subtract(target.position, instance.position))
61
+ return distance < 250
62
+ },
63
+ true: () => "aware",
64
+ false: ({ instance }) => instance.state,
65
+ }),
66
+ chasing: () => ({
67
+ test: ({ instance, target }) => {
68
+ const distance = length(subtract(target.position, instance.position))
69
+ return distance >= 250
70
+ },
71
+ true: () => "idle",
72
+ false: () => ({
73
+ test: ({ instance, target }) => {
74
+ const distance = length(subtract(target.position, instance.position))
75
+ return distance < 10
76
+ },
77
+ true: () => "sleepy",
78
+ false: ({ instance }) => instance.state,
79
+ }),
80
+ }),
81
+ sleepy: () => ({
82
+ test: ({ instance, target }) => {
83
+ const distance = length(subtract(target.position, instance.position))
84
+ return distance >= 10
85
+ },
86
+ true: () => "aware",
87
+ false: ({ instance }) => instance.state,
88
+ }),
89
+ sleeping: () => ({
90
+ test: ({ instance, target }) => {
91
+ const distance = length(subtract(target.position, instance.position))
92
+ return distance >= 10
93
+ },
94
+ true: () => "aware",
95
+ false: ({ instance }) => instance.state,
96
+ }),
97
+ }
98
+
99
+ const state = decide(tree, { instance, target })
100
+
101
+ expect(state).toBe("aware")
102
+ })
@@ -0,0 +1,155 @@
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
+ }
@@ -0,0 +1,151 @@
1
+ import {
2
+ createBoard,
3
+ down,
4
+ downRight,
5
+ right,
6
+ } from "@inglorious/utils/data-structures/board.js"
7
+ import { expect, test } from "vitest"
8
+
9
+ import { findPath } from "./path-finding.js"
10
+
11
+ test("it should find the shortest path in a trivial example", () => {
12
+ const graph = {
13
+ nodes: {
14
+ A: [0, 0],
15
+ B: [2, 0],
16
+ },
17
+ arcs: [{ from: "A", to: "B", cost: 2 }],
18
+ }
19
+ const start = "A"
20
+ const end = "B"
21
+ const expectedResult = ["A", "B"]
22
+
23
+ expect(findPath(graph, start, end)).toStrictEqual(expectedResult)
24
+ })
25
+
26
+ test("it should find the shortest path between two nodes with default heuristic", () => {
27
+ const graph = {
28
+ nodes: {
29
+ A: [0, 0],
30
+ B: [2, 0],
31
+ C: [3, 2],
32
+ D: [4, 4],
33
+ E: [1, 1],
34
+ F: [2, 3],
35
+ G: [0, 4],
36
+ },
37
+ arcs: [
38
+ { from: "A", to: "B" },
39
+ { from: "B", to: "C" },
40
+ { from: "C", to: "D" },
41
+ { from: "A", to: "E" },
42
+ { from: "E", to: "F" },
43
+ { from: "F", to: "G" },
44
+ { from: "G", to: "D" },
45
+ ],
46
+ }
47
+ const start = "A"
48
+ const end = "D"
49
+ const expectedResult = ["A", "B", "C", "D"]
50
+
51
+ expect(findPath(graph, start, end)).toStrictEqual(expectedResult)
52
+ })
53
+
54
+ test("it should find the shortest path between two nodes with node costs", () => {
55
+ const graph = {
56
+ nodes: [
57
+ { id: "A", position: [0, 0], cost: 0 },
58
+ { id: "B", position: [2, 0], cost: 7 },
59
+ { id: "C", position: [3, 2], cost: 3 },
60
+ { id: "D", position: [4, 4], cost: 2 },
61
+ { id: "E", position: [1, 1], cost: 1.5 },
62
+ { id: "F", position: [2, 3], cost: 2 },
63
+ { id: "G", position: [0, 4], cost: 3 },
64
+ ],
65
+ arcs: [
66
+ { from: "A", to: "B" },
67
+ { from: "B", to: "C" },
68
+ { from: "C", to: "D" },
69
+ { from: "A", to: "E" },
70
+ { from: "E", to: "F" },
71
+ { from: "F", to: "G" },
72
+ { from: "G", to: "D" },
73
+ ],
74
+ }
75
+ const start = "A"
76
+ const end = "D"
77
+ const expectedResult = ["A", "E", "F", "G", "D"]
78
+
79
+ expect(findPath(graph, start, end)).toStrictEqual(expectedResult)
80
+ })
81
+
82
+ test("it should find the shortest path between two nodes with arc costs", () => {
83
+ const graph = {
84
+ nodes: {
85
+ A: [0, 0],
86
+ B: [2, 0],
87
+ C: [3, 2],
88
+ D: [4, 4],
89
+ E: [1, 1],
90
+ F: [2, 3],
91
+ G: [0, 4],
92
+ },
93
+ arcs: [
94
+ { from: "A", to: "B", cost: 7 },
95
+ { from: "B", to: "C", cost: 3 },
96
+ { from: "C", to: "D", cost: 2 },
97
+ { from: "A", to: "E", cost: 1.5 },
98
+ { from: "E", to: "F", cost: 2 },
99
+ { from: "F", to: "G", cost: 3 },
100
+ { from: "G", to: "D", cost: 4 },
101
+ ],
102
+ }
103
+ const start = "A"
104
+ const end = "D"
105
+ const expectedResult = ["A", "E", "F", "G", "D"]
106
+
107
+ expect(findPath(graph, start, end)).toStrictEqual(expectedResult)
108
+ })
109
+
110
+ test("it should find the best path in a board graph", () => {
111
+ const rows = 5
112
+ const columns = 5
113
+ const size = [rows, columns]
114
+ const filler = (i, j) => ({ id: `${i}${j}`, position: [i, j] })
115
+ const board = createBoard(size, filler)
116
+
117
+ const graph = {
118
+ nodes: board,
119
+ arcs: [],
120
+ }
121
+
122
+ for (let i = 0; i < rows; i++) {
123
+ for (let j = 0; j < columns; j++) {
124
+ const current = `${i}${j}`
125
+ if (i + 1 < rows) {
126
+ graph.arcs.push({
127
+ from: current,
128
+ to: down([i, j], size).join(""),
129
+ })
130
+ }
131
+ if (j + 1 < columns) {
132
+ graph.arcs.push({
133
+ from: current,
134
+ to: right([i, j], size).join(""),
135
+ })
136
+ }
137
+ if (i + 1 < rows && j + 1 < columns) {
138
+ graph.arcs.push({
139
+ from: current,
140
+ to: downRight([i, j], size).join(""),
141
+ })
142
+ }
143
+ }
144
+ }
145
+
146
+ const start = "00"
147
+ const end = "44"
148
+ const expectedResult = ["00", "11", "22", "33", "44"]
149
+
150
+ expect(findPath(graph, start, end)).toStrictEqual(expectedResult)
151
+ })
@@ -0,0 +1,28 @@
1
+ export type Conditions = unknown
2
+ export type Outcome = string | boolean
3
+
4
+ export interface DecisionTree {
5
+ test: (conditions: Conditions) => Outcome
6
+ [outcome: Outcome]: () => Outcome | DecisionTree
7
+ }
8
+
9
+ interface NodeMap {
10
+ [id: string]: [number, number] // [x, y]
11
+ }
12
+
13
+ interface Node {
14
+ id: string
15
+ position: [number, number] // [x, y]
16
+ cost?: number
17
+ }
18
+
19
+ interface Arc {
20
+ from: string
21
+ to: string
22
+ cost?: number
23
+ }
24
+
25
+ export interface Graph {
26
+ nodes: NodeMap | Node[]
27
+ arcs: Arc[]
28
+ }