@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,45 @@
1
+ import {
2
+ angle,
3
+ clamp,
4
+ magnitude,
5
+ multiply,
6
+ setMagnitude,
7
+ zero,
8
+ } from "@inglorious/utils/math/linear-algebra/vector.js"
9
+ import { subtract, sum } from "@inglorious/utils/math/linear-algebra/vectors.js"
10
+
11
+ const DEFAULT_MAX_ACCELERATION = 0
12
+ const DEFAULT_MAX_SPEED = 0
13
+
14
+ const MIN_SPEED = 0
15
+
16
+ const HALF_ACCELERATION = 0.5
17
+
18
+ export default function flee(instance, target, { dt }) {
19
+ const maxAcceleration = instance.maxAcceleration ?? DEFAULT_MAX_ACCELERATION
20
+ const maxSpeed = instance.maxSpeed ?? DEFAULT_MAX_SPEED
21
+
22
+ let velocity = instance.velocity ?? zero()
23
+
24
+ const direction = subtract(instance.position, target.position)
25
+ const distance = magnitude(direction)
26
+
27
+ if (!distance) {
28
+ return instance
29
+ }
30
+
31
+ const acceleration = setMagnitude(direction, maxAcceleration)
32
+
33
+ velocity = sum(velocity, multiply(acceleration, dt))
34
+ velocity = clamp(velocity, MIN_SPEED, maxSpeed)
35
+
36
+ const position = sum(
37
+ instance.position,
38
+ multiply(velocity, dt),
39
+ multiply(acceleration, HALF_ACCELERATION * dt * dt),
40
+ )
41
+
42
+ const orientation = angle(velocity)
43
+
44
+ return { velocity, position, orientation }
45
+ }
@@ -0,0 +1,17 @@
1
+ import {
2
+ angle,
3
+ magnitude,
4
+ zero,
5
+ } from "@inglorious/utils/math/linear-algebra/vector.js"
6
+
7
+ import align from "./align.js"
8
+
9
+ export default function lookWhereYoureGoing(instance, options) {
10
+ const velocity = instance.velocity ?? zero()
11
+
12
+ if (!magnitude(velocity)) {
13
+ return instance
14
+ }
15
+
16
+ return align(instance, { orientation: angle(velocity) }, options)
17
+ }
@@ -0,0 +1,50 @@
1
+ import {
2
+ angle,
3
+ clamp,
4
+ divide,
5
+ magnitude,
6
+ multiply,
7
+ zero,
8
+ } from "@inglorious/utils/math/linear-algebra/vector.js"
9
+ import { subtract, sum } from "@inglorious/utils/math/linear-algebra/vectors.js"
10
+
11
+ export const DEFAULT_TIME_TO_TARGET = 0.1
12
+
13
+ const DEFAULT_MAX_ACCELERATION = 0
14
+ const DEFAULT_MAX_SPEED = 0
15
+ const DEFAULT_ORIENTATION = 0
16
+
17
+ const MIN_ACCELERATION = 0
18
+ const MIN_SPEED = 0
19
+
20
+ const HALF_ACCELERATION = 0.5
21
+
22
+ export default function matchVelocity(
23
+ instance,
24
+ target,
25
+ { dt, timeToTarget = DEFAULT_TIME_TO_TARGET },
26
+ ) {
27
+ const maxAcceleration = instance.maxAcceleration ?? DEFAULT_MAX_ACCELERATION
28
+ const maxSpeed = instance.maxSpeed ?? DEFAULT_MAX_SPEED
29
+
30
+ let orientation = instance.orientation ?? DEFAULT_ORIENTATION
31
+
32
+ let velocity = instance.velocity ?? zero()
33
+ const velocityDelta = subtract(target.velocity, velocity)
34
+
35
+ let acceleration = divide(velocityDelta, timeToTarget)
36
+ acceleration = clamp(acceleration, MIN_ACCELERATION, maxAcceleration)
37
+
38
+ velocity = sum(velocity, multiply(acceleration, dt))
39
+ velocity = clamp(velocity, MIN_SPEED, maxSpeed)
40
+
41
+ const position = sum(
42
+ instance.position,
43
+ multiply(velocity, dt),
44
+ multiply(acceleration, HALF_ACCELERATION * dt * dt),
45
+ )
46
+
47
+ orientation = magnitude(velocity) ? angle(velocity) : orientation
48
+
49
+ return { acceleration, velocity, position, orientation }
50
+ }
@@ -0,0 +1,38 @@
1
+ import {
2
+ magnitude,
3
+ multiply,
4
+ zero,
5
+ } from "@inglorious/utils/math/linear-algebra/vector.js"
6
+ import { subtract, sum } from "@inglorious/utils/math/linear-algebra/vectors.js"
7
+
8
+ import seek from "./seek.js"
9
+
10
+ export const DEFAULT_MAX_PREDICTION = 10
11
+
12
+ export default function pursue(
13
+ instance,
14
+ target,
15
+ { dt, maxPrediction = DEFAULT_MAX_PREDICTION },
16
+ ) {
17
+ const velocity = instance.velocity ?? zero()
18
+
19
+ const direction = subtract(target.position, instance.position)
20
+ const distance = magnitude(direction)
21
+
22
+ if (!distance) {
23
+ return instance
24
+ }
25
+
26
+ const speed = magnitude(velocity)
27
+
28
+ let prediction
29
+ if (speed <= distance / maxPrediction) {
30
+ prediction = maxPrediction
31
+ } else {
32
+ prediction = distance / speed
33
+ }
34
+
35
+ const position = sum(target.position, multiply(target.velocity, prediction))
36
+
37
+ return seek(instance, { ...target, position }, { dt })
38
+ }
@@ -0,0 +1,44 @@
1
+ import {
2
+ angle,
3
+ clamp,
4
+ magnitude,
5
+ multiply,
6
+ setMagnitude,
7
+ zero,
8
+ } from "@inglorious/utils/math/linear-algebra/vector.js"
9
+ import { subtract, sum } from "@inglorious/utils/math/linear-algebra/vectors.js"
10
+
11
+ const DEFAULT_MAX_ACCELERATION = 0
12
+ const DEFAULT_MAX_SPEED = 0
13
+
14
+ const MIN_SPEED = 0
15
+
16
+ const HALF_ACCELERATION = 0.5
17
+
18
+ export default function seek(instance, target, { dt }) {
19
+ const maxAcceleration = instance.maxAcceleration ?? DEFAULT_MAX_ACCELERATION
20
+ const maxSpeed = instance.maxSpeed ?? DEFAULT_MAX_SPEED
21
+
22
+ const direction = subtract(target.position, instance.position)
23
+ const distance = magnitude(direction)
24
+
25
+ if (!distance) {
26
+ return instance
27
+ }
28
+
29
+ const acceleration = setMagnitude(direction, maxAcceleration)
30
+
31
+ let velocity = instance.velocity ?? zero()
32
+ velocity = sum(velocity, multiply(acceleration, dt))
33
+ velocity = clamp(velocity, MIN_SPEED, maxSpeed)
34
+
35
+ const position = sum(
36
+ instance.position,
37
+ multiply(velocity, dt),
38
+ multiply(acceleration, HALF_ACCELERATION * dt * dt),
39
+ )
40
+
41
+ const orientation = angle(velocity)
42
+
43
+ return { velocity, position, orientation }
44
+ }
@@ -0,0 +1,32 @@
1
+ import { createVector } from "@inglorious/utils/math/linear-algebra/vector.js"
2
+ import { sum } from "@inglorious/utils/math/linear-algebra/vectors.js"
3
+ import { randomBinomial } from "@inglorious/utils/math/rng.js"
4
+
5
+ import seek from "./seek.js"
6
+
7
+ export const DEFAULT_WANDER_OFFSET = 100
8
+ export const DEFAULT_WANDER_RADIUS = 100
9
+
10
+ const DEFAULT_MAX_ANGULAR_SPEED = 0
11
+
12
+ const DEFAULT_ORIENTATION = 0
13
+
14
+ export default function wander(
15
+ instance,
16
+ {
17
+ wanderOffset = DEFAULT_WANDER_OFFSET,
18
+ wanderRadius = DEFAULT_WANDER_RADIUS,
19
+ ...options
20
+ },
21
+ ) {
22
+ const maxAngularSpeed = instance.maxAngularSpeed ?? DEFAULT_MAX_ANGULAR_SPEED
23
+
24
+ let orientation = instance.orientation ?? DEFAULT_ORIENTATION
25
+
26
+ let position = sum(instance.position, createVector(wanderOffset, orientation))
27
+
28
+ orientation += randomBinomial() * maxAngularSpeed
29
+ position = sum(position, createVector(wanderRadius, orientation))
30
+
31
+ return seek(instance, { position }, options)
32
+ }
@@ -0,0 +1,37 @@
1
+ import { abs, clamp } from "@inglorious/utils/math/numbers.js"
2
+ import { toRange } from "@inglorious/utils/math/trigonometry.js"
3
+
4
+ export const DEFAULT_TARGET_RADIUS = 0.1
5
+ export const DEFAULT_TIME_TO_TARGET = 0.1
6
+
7
+ const DEFAULT_MAX_ANGULAR_SPEED = 0
8
+
9
+ const DEFAULT_ORIENTATION = 0
10
+
11
+ export default function align(
12
+ instance,
13
+ target,
14
+ {
15
+ dt,
16
+ targetRadius = DEFAULT_TARGET_RADIUS,
17
+ timeToTarget = DEFAULT_TIME_TO_TARGET,
18
+ },
19
+ ) {
20
+ const maxAngularSpeed = instance.maxAngularSpeed ?? DEFAULT_MAX_ANGULAR_SPEED
21
+
22
+ let orientation = instance.orientation ?? DEFAULT_ORIENTATION
23
+
24
+ const direction = toRange(target.orientation - orientation)
25
+ const distance = abs(direction)
26
+
27
+ if (distance < targetRadius) {
28
+ return instance
29
+ }
30
+
31
+ let angularVelocity = direction / timeToTarget
32
+ angularVelocity = clamp(angularVelocity, -maxAngularSpeed, maxAngularSpeed)
33
+
34
+ orientation += angularVelocity * dt
35
+
36
+ return { orientation }
37
+ }
@@ -0,0 +1,42 @@
1
+ import {
2
+ angle,
3
+ clamp,
4
+ divide,
5
+ magnitude,
6
+ multiply,
7
+ } from "@inglorious/utils/math/linear-algebra/vector.js"
8
+ import { subtract, sum } from "@inglorious/utils/math/linear-algebra/vectors.js"
9
+
10
+ export const DEFAULT_TARGET_RADIUS = 1
11
+ export const DEFAULT_TIME_TO_TARGET = 0.1
12
+
13
+ const DEFAULT_MAX_SPEED = 0
14
+
15
+ const MIN_SPEED = 0
16
+
17
+ export default function arrive(
18
+ instance,
19
+ target,
20
+ {
21
+ dt,
22
+ targetRadius = DEFAULT_TARGET_RADIUS,
23
+ timeToTarget = DEFAULT_TIME_TO_TARGET,
24
+ },
25
+ ) {
26
+ const maxSpeed = instance.maxSpeed ?? DEFAULT_MAX_SPEED
27
+
28
+ const direction = subtract(target.position, instance.position)
29
+ const distance = magnitude(direction)
30
+
31
+ if (distance < targetRadius) {
32
+ return instance
33
+ }
34
+
35
+ let velocity = divide(direction, timeToTarget)
36
+ velocity = clamp(velocity, MIN_SPEED, maxSpeed)
37
+
38
+ const position = sum(instance.position, multiply(velocity, dt))
39
+ const orientation = angle(velocity)
40
+
41
+ return { velocity, position, orientation }
42
+ }
@@ -0,0 +1,20 @@
1
+ import {
2
+ angle,
3
+ magnitude,
4
+ } from "@inglorious/utils/math/linear-algebra/vector.js"
5
+ import { subtract } from "@inglorious/utils/math/linear-algebra/vectors.js"
6
+
7
+ import align from "./align.js"
8
+
9
+ export default function face(instance, target, options) {
10
+ const direction = subtract(target.position, instance.position)
11
+ const distance = magnitude(direction)
12
+
13
+ if (!distance) {
14
+ return instance
15
+ }
16
+
17
+ const orientation = angle(direction)
18
+
19
+ return align(instance, { ...target, orientation }, options)
20
+ }
@@ -0,0 +1,26 @@
1
+ import {
2
+ angle,
3
+ magnitude,
4
+ multiply,
5
+ setMagnitude,
6
+ } from "@inglorious/utils/math/linear-algebra/vector.js"
7
+ import { subtract, sum } from "@inglorious/utils/math/linear-algebra/vectors.js"
8
+
9
+ const DEFAULT_MAX_SPEED = 0
10
+
11
+ export default function flee(instance, target, { dt }) {
12
+ const maxSpeed = instance.maxSpeed ?? DEFAULT_MAX_SPEED
13
+
14
+ const direction = subtract(instance.position, target.position)
15
+ const distance = magnitude(direction)
16
+
17
+ if (!distance) {
18
+ return instance
19
+ }
20
+
21
+ const velocity = setMagnitude(direction, maxSpeed)
22
+ const position = sum(instance.position, multiply(velocity, dt))
23
+ const orientation = angle(velocity)
24
+
25
+ return { velocity, position, orientation }
26
+ }
@@ -0,0 +1,26 @@
1
+ import {
2
+ angle,
3
+ magnitude,
4
+ multiply,
5
+ setMagnitude,
6
+ } from "@inglorious/utils/math/linear-algebra/vector.js"
7
+ import { subtract, sum } from "@inglorious/utils/math/linear-algebra/vectors.js"
8
+
9
+ const DEFAULT_MAX_SPEED = 0
10
+
11
+ export default function seek(instance, target, { dt }) {
12
+ const maxSpeed = instance.maxSpeed ?? DEFAULT_MAX_SPEED
13
+
14
+ const direction = subtract(target.position, instance.position)
15
+ const distance = magnitude(direction)
16
+
17
+ if (!distance) {
18
+ return instance
19
+ }
20
+
21
+ const velocity = setMagnitude(direction, maxSpeed)
22
+ const position = sum(instance.position, multiply(velocity, dt))
23
+ const orientation = angle(velocity)
24
+
25
+ return { velocity, position, orientation }
26
+ }
@@ -0,0 +1,42 @@
1
+ import { expect, test } from "vitest"
2
+
3
+ import seek from "./seek.js"
4
+
5
+ test("it should move toward the target", () => {
6
+ const instance = { maxSpeed: 1, position: [0, 0, 0] }
7
+ const target = { position: [2, 0, 0] }
8
+ const options = { dt: 1 }
9
+ const expectedResult = {
10
+ position: [1, 0, 0],
11
+ velocity: [1, 0, 0],
12
+ orientation: 0,
13
+ }
14
+
15
+ expect(seek(instance, target, options)).toStrictEqual(expectedResult)
16
+ })
17
+
18
+ test("it should eventually reach the target", () => {
19
+ const instance = { maxSpeed: 1, position: [0, 0, 0] }
20
+ const target = { position: [2, 0, 0] }
21
+ const options = { dt: 2 }
22
+ const expectedResult = {
23
+ position: [2, 0, 0],
24
+ velocity: [1, 0, 0],
25
+ orientation: 0,
26
+ }
27
+
28
+ expect(seek(instance, target, options)).toStrictEqual(expectedResult)
29
+ })
30
+
31
+ test("it should overshoot when speed is too high", () => {
32
+ const instance = { maxSpeed: 10, position: [0, 0, 0] }
33
+ const target = { position: [2, 0, 0] }
34
+ const options = { dt: 1 }
35
+ const expectedResult = {
36
+ position: [10, 0, 0],
37
+ velocity: [10, 0, 0],
38
+ orientation: 0,
39
+ }
40
+
41
+ expect(seek(instance, target, options)).toStrictEqual(expectedResult)
42
+ })
@@ -0,0 +1,31 @@
1
+ import {
2
+ fromAngle,
3
+ multiply,
4
+ } from "@inglorious/utils/math/linear-algebra/vector.js"
5
+ import { sum } from "@inglorious/utils/math/linear-algebra/vectors.js"
6
+ import { randomBinomial } from "@inglorious/utils/math/rng.js"
7
+
8
+ import seek from "./seek.js"
9
+
10
+ export const DEFAULT_WANDER_RADIUS = 10
11
+
12
+ const DEFAULT_MAX_ANGULAR_SPEED = 0
13
+
14
+ const DEFAULT_ORIENTATION = 0
15
+
16
+ export default function wander(
17
+ instance,
18
+ { wanderRadius = DEFAULT_WANDER_RADIUS, ...options },
19
+ ) {
20
+ const maxAngularSpeed = instance.maxAngularSpeed ?? DEFAULT_MAX_ANGULAR_SPEED
21
+
22
+ let orientation = instance.orientation ?? DEFAULT_ORIENTATION
23
+ orientation += randomBinomial() * maxAngularSpeed
24
+
25
+ const position = sum(
26
+ instance.position,
27
+ multiply(fromAngle(orientation), wanderRadius),
28
+ )
29
+
30
+ return seek(instance, { position }, options)
31
+ }
@@ -0,0 +1,27 @@
1
+ import {
2
+ angle,
3
+ createVector,
4
+ multiply,
5
+ } from "@inglorious/utils/math/linear-algebra/vector.js"
6
+ import { sum } from "@inglorious/utils/math/linear-algebra/vectors.js"
7
+ import { randomBinomial } from "@inglorious/utils/math/rng.js"
8
+
9
+ const DEFAULT_MAX_SPEED = 0
10
+ const DEFAULT_MAX_ANGULAR_SPEED = 0
11
+
12
+ const DEFAULT_ORIENTATION = 0
13
+
14
+ export default function wander(instance, { dt }) {
15
+ const maxSpeed = instance.maxSpeed ?? DEFAULT_MAX_SPEED
16
+ const maxAngularSpeed = instance.maxAngularSpeed ?? DEFAULT_MAX_ANGULAR_SPEED
17
+
18
+ let orientation = instance.orientation ?? DEFAULT_ORIENTATION
19
+ orientation += randomBinomial() * maxAngularSpeed
20
+
21
+ const velocity = createVector(maxSpeed, orientation)
22
+
23
+ const position = sum(instance.position, multiply(velocity, dt))
24
+ orientation = angle(velocity)
25
+
26
+ return { velocity, position, orientation }
27
+ }
@@ -0,0 +1,115 @@
1
+ import { filter } from "@inglorious/utils/data-structures/object.js"
2
+ import * as circle from "@inglorious/utils/math/geometry/circle.js"
3
+ import * as hitmask from "@inglorious/utils/math/geometry/hitmask.js"
4
+ import * as line from "@inglorious/utils/math/geometry/line.js"
5
+ import * as platform from "@inglorious/utils/math/geometry/platform.js"
6
+ import * as point from "@inglorious/utils/math/geometry/point.js"
7
+ import * as rectangle from "@inglorious/utils/math/geometry/rectangle.js"
8
+ import * as segment from "@inglorious/utils/math/geometry/segment.js"
9
+ import { zero } from "@inglorious/utils/math/linear-algebra/vector.js"
10
+ import { add } from "@inglorious/utils/math/linear-algebra/vectors.js"
11
+
12
+ const Z = 2 // Z-axis index.
13
+
14
+ const Shape = {
15
+ circle,
16
+ line,
17
+ platform,
18
+ point,
19
+ rectangle,
20
+ segment,
21
+ hitmask,
22
+ }
23
+
24
+ /**
25
+ * Finds the first collision between a point and a list of instances.
26
+ *
27
+ * @param {Point} instance - The point to check for collisions.
28
+ * @param {Options} options - Options for collision detection.
29
+ * @returns {Instance | undefined} The first instance that collides with the point, or undefined if none are found.
30
+ */
31
+ export function findCollision(instance, options = {}) {
32
+ const { instances, collisionType = "hitbox" } = options
33
+
34
+ const otherInstances = filter(
35
+ instances,
36
+ (id, { collisions }) => id !== instance.id && collisions?.[collisionType],
37
+ )
38
+
39
+ return Object.values(otherInstances)
40
+ .toSorted((a, b) => a.position[Z] - b.position[Z])
41
+ .find((target) => collidesWith(instance, target, collisionType))
42
+ }
43
+
44
+ export function collidesWith(instance, target, collisionType = "hitbox") {
45
+ const instanceCollision = instance.collisions[collisionType]
46
+ const instanceShape = {
47
+ ...instanceCollision,
48
+ position: add(instance.position, instanceCollision.position ?? zero()),
49
+ size: instanceCollision.size ?? instance.size,
50
+ radius: instanceCollision.radius ?? instance.radius,
51
+ }
52
+
53
+ const targetCollision = target.collisions[collisionType]
54
+ const targetShape = {
55
+ ...targetCollision,
56
+ position: add(target.position, targetCollision.position ?? zero()),
57
+ size: targetCollision.size ?? target.size,
58
+ radius: targetCollision.radius ?? target.radius,
59
+ }
60
+
61
+ return shapeCollidesWith(instanceShape, targetShape)
62
+ }
63
+
64
+ function shapeCollidesWith(instance, target) {
65
+ const shapeFns = Shape[instance.shape]
66
+
67
+ switch (target.shape) {
68
+ case "circle":
69
+ return shapeFns.intersectsCircle(instance, target)
70
+
71
+ case "line":
72
+ return shapeFns.intersectsLine(instance, target)
73
+
74
+ case "platform":
75
+ return shapeFns.intersectsPlatform(instance, target)
76
+
77
+ case "point":
78
+ return shapeFns.intersectsPoint(instance, target)
79
+
80
+ case "rectangle":
81
+ return shapeFns.intersectsRectangle(instance, target)
82
+
83
+ case "segment":
84
+ return shapeFns.intersectsSegment(instance, target)
85
+ }
86
+ }
87
+
88
+ export function findCollisions(instance, target, collisionType = "hitbox") {
89
+ const instanceCollision = instance.collisions[collisionType]
90
+ const shapeFns = Shape[instanceCollision.shape]
91
+ const instanceShape = {
92
+ ...instanceCollision,
93
+ position: add(instance.position, instanceCollision.offset ?? zero()),
94
+ heights: flipUpsideDown(
95
+ instanceCollision.heights,
96
+ instanceCollision.columns,
97
+ ),
98
+ }
99
+
100
+ const targetCollision = target.collisions[collisionType]
101
+ const targetShape = {
102
+ ...targetCollision,
103
+ position: add(target.position, targetCollision.offset ?? zero()),
104
+ }
105
+
106
+ return shapeFns.findCollisions(instanceShape, targetShape)
107
+ }
108
+
109
+ export function flipUpsideDown(grid, columns) {
110
+ const rows = []
111
+ for (let i = 0; i < grid.length; i += columns) {
112
+ rows.push(grid.slice(i, i + columns))
113
+ }
114
+ return rows.reverse().flat()
115
+ }
@@ -0,0 +1,26 @@
1
+ const ONE_SECOND = 1000
2
+
3
+ export default class AnimationFrameLoop {
4
+ _id = null
5
+ _previousTime = new Date()
6
+
7
+ start(engine) {
8
+ this._tick(engine)
9
+ }
10
+
11
+ stop() {
12
+ window.cancelAnimationFrame(this._id)
13
+ this._id = null
14
+ }
15
+
16
+ _tick(engine) {
17
+ const currentTime = new Date()
18
+ this._id = window.requestAnimationFrame(() => this._tick(engine))
19
+ const dt = currentTime - this._previousTime
20
+
21
+ engine.update(dt / ONE_SECOND)
22
+ engine.render(dt / ONE_SECOND)
23
+
24
+ this._previousTime = currentTime
25
+ }
26
+ }
@@ -0,0 +1,23 @@
1
+ const ONE_SECOND = 1000
2
+
3
+ export default class ElapsedLoop {
4
+ _shouldStop = false
5
+
6
+ start(engine) {
7
+ let previousTime = Date.now()
8
+
9
+ while (!this._shouldStop) {
10
+ const currentTime = Date.now()
11
+ const dt = currentTime - previousTime
12
+
13
+ engine.update(dt / ONE_SECOND)
14
+ engine.render(dt / ONE_SECOND)
15
+
16
+ previousTime = currentTime
17
+ }
18
+ }
19
+
20
+ stop() {
21
+ this._shouldStop = true
22
+ }
23
+ }
@@ -0,0 +1,28 @@
1
+ const ONE_SECOND = 1000
2
+
3
+ export default class NapLoop {
4
+ _shouldStop = false
5
+
6
+ async start(engine, msPerUpdate) {
7
+ let previousTime = Date.now()
8
+
9
+ while (!this._shouldStop) {
10
+ const currentTime = Date.now()
11
+ const dt = currentTime - previousTime
12
+
13
+ engine.update(dt / ONE_SECOND)
14
+ engine.render(dt / ONE_SECOND)
15
+
16
+ previousTime = currentTime
17
+ await sleep(Date.now() - currentTime + msPerUpdate)
18
+ }
19
+ }
20
+
21
+ stop() {
22
+ this._shouldStop = true
23
+ }
24
+ }
25
+
26
+ function sleep(ms) {
27
+ return new Promise((resolve) => setTimeout(resolve, ms))
28
+ }