@inglorious/engine 0.1.0 → 0.2.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 (292) hide show
  1. package/README.md +39 -36
  2. package/package.json +20 -33
  3. package/src/engine/ai/movement/dynamic/align.js +63 -63
  4. package/src/engine/ai/movement/dynamic/arrive.js +42 -43
  5. package/src/engine/ai/movement/dynamic/evade.js +38 -38
  6. package/src/engine/ai/movement/dynamic/face.js +19 -20
  7. package/src/engine/ai/movement/dynamic/flee.js +45 -45
  8. package/src/engine/ai/movement/dynamic/look-where-youre-going.js +16 -17
  9. package/src/engine/ai/movement/dynamic/match-velocity.js +51 -50
  10. package/src/engine/ai/movement/dynamic/pursue.js +38 -38
  11. package/src/engine/ai/movement/dynamic/seek.js +44 -44
  12. package/src/engine/ai/movement/dynamic/wander.js +31 -32
  13. package/src/engine/ai/movement/kinematic/align.js +37 -37
  14. package/src/engine/ai/movement/kinematic/arrive.js +42 -42
  15. package/src/engine/ai/movement/kinematic/face.js +19 -20
  16. package/src/engine/ai/movement/kinematic/flee.js +26 -26
  17. package/src/engine/ai/movement/kinematic/seek.js +26 -26
  18. package/src/engine/ai/movement/kinematic/seek.test.js +42 -42
  19. package/src/engine/ai/movement/kinematic/wander-as-seek.js +31 -31
  20. package/src/engine/ai/movement/kinematic/wander.js +27 -27
  21. package/src/engine/animation/sprite.js +101 -0
  22. package/src/engine/animation/ticker.js +38 -0
  23. package/src/engine/behaviors/camera.js +68 -0
  24. package/src/engine/behaviors/controls/dynamic/modern.js +76 -0
  25. package/src/engine/behaviors/controls/dynamic/shooter.js +84 -0
  26. package/src/engine/behaviors/controls/dynamic/tank.js +69 -0
  27. package/src/engine/behaviors/controls/event-handlers.js +17 -0
  28. package/src/engine/behaviors/controls/kinematic/modern.js +76 -0
  29. package/src/engine/behaviors/controls/kinematic/shooter.js +82 -0
  30. package/src/engine/behaviors/controls/kinematic/tank.js +67 -0
  31. package/src/engine/behaviors/debug/collision.js +35 -0
  32. package/src/engine/behaviors/fps.js +29 -0
  33. package/src/engine/behaviors/fsm.js +33 -0
  34. package/src/{game/decorators → engine/behaviors}/fsm.test.js +49 -56
  35. package/src/engine/behaviors/game.js +15 -0
  36. package/src/engine/behaviors/input/controls.js +37 -0
  37. package/src/engine/behaviors/input/gamepad.js +114 -0
  38. package/src/engine/behaviors/input/input.js +48 -0
  39. package/src/engine/behaviors/input/keyboard.js +64 -0
  40. package/src/engine/behaviors/input/mouse.js +91 -0
  41. package/src/engine/behaviors/physics/bouncy.js +25 -0
  42. package/src/engine/behaviors/physics/clamped.js +36 -0
  43. package/src/{game/decorators/collisions.js → engine/behaviors/physics/collidable.js} +20 -24
  44. package/src/engine/behaviors/physics/jumpable.js +145 -0
  45. package/src/engine/behaviors/ui/button.js +17 -0
  46. package/src/engine/collision/detection.js +110 -115
  47. package/src/engine/core/api.js +34 -0
  48. package/src/engine/core/dev-tools.js +135 -0
  49. package/src/engine/core/engine.js +119 -0
  50. package/src/engine/core/loop.js +15 -0
  51. package/src/engine/{loop → core/loops}/animation-frame.js +25 -26
  52. package/src/engine/{loop → core/loops}/elapsed.js +22 -23
  53. package/src/engine/{loop → core/loops}/fixed.js +27 -28
  54. package/src/engine/{loop → core/loops}/flash.js +13 -14
  55. package/src/engine/{loop → core/loops}/lag.js +26 -27
  56. package/src/engine/core/select.js +26 -0
  57. package/src/engine/core/store.js +178 -0
  58. package/src/engine/core/store.test.js +110 -0
  59. package/src/engine/movement/dynamic/modern.js +21 -24
  60. package/src/engine/movement/dynamic/tank.js +43 -43
  61. package/src/engine/movement/kinematic/modern.js +16 -16
  62. package/src/engine/movement/kinematic/modern.test.js +27 -27
  63. package/src/engine/movement/kinematic/tank.js +27 -27
  64. package/src/engine/physics/bounds.js +138 -0
  65. package/src/engine/physics/position.js +43 -0
  66. package/src/engine/physics/position.test.js +80 -0
  67. package/src/engine/systems/sprite-animation.js +27 -0
  68. package/src/main.js +10 -5
  69. package/src/renderers/canvas/absolute-position.js +18 -0
  70. package/src/renderers/canvas/camera.js +13 -0
  71. package/src/renderers/canvas/canvas-renderer.js +68 -0
  72. package/src/{ui → renderers}/canvas/character.js +38 -35
  73. package/src/{ui → renderers}/canvas/form/button.js +25 -25
  74. package/src/{ui → renderers}/canvas/fps.js +18 -18
  75. package/src/renderers/canvas/image/hitmask.js +51 -0
  76. package/src/{ui → renderers}/canvas/image/image.js +34 -37
  77. package/src/{ui → renderers}/canvas/image/sprite.js +49 -49
  78. package/src/{ui → renderers}/canvas/image/tilemap.js +66 -64
  79. package/src/{ui → renderers}/canvas/mouse.js +37 -37
  80. package/src/renderers/canvas/rendering-system.js +79 -0
  81. package/src/{ui → renderers}/canvas/shapes/circle.js +29 -31
  82. package/src/{ui → renderers}/canvas/shapes/rectangle.js +27 -31
  83. package/src/renderers/react/game/character/index.jsx +20 -0
  84. package/src/{ui → renderers}/react/game/cursor/index.jsx +20 -20
  85. package/src/{ui → renderers}/react/game/form/fields/field/index.jsx +56 -56
  86. package/src/{ui → renderers}/react/game/form/fields/index.jsx +12 -12
  87. package/src/{ui → renderers}/react/game/form/index.jsx +22 -22
  88. package/src/{ui → renderers}/react/game/fps/index.jsx +16 -16
  89. package/src/{ui → renderers}/react/game/game.jsx +72 -71
  90. package/src/{ui → renderers}/react/game/index.jsx +29 -29
  91. package/src/{ui → renderers}/react/game/platform/index.jsx +30 -30
  92. package/src/{ui → renderers}/react/game/scene/index.jsx +27 -25
  93. package/src/{ui → renderers}/react/game/sprite/index.jsx +60 -58
  94. package/src/{ui → renderers}/react/game/stats/index.jsx +22 -22
  95. package/src/{ui → renderers}/react/hocs/with-absolute-position/index.jsx +20 -20
  96. package/src/{ui → renderers}/react/index.jsx +9 -9
  97. package/src/utils/algorithms/decision-tree.js +24 -24
  98. package/src/utils/algorithms/decision-tree.test.js +153 -102
  99. package/src/utils/algorithms/path-finding.js +155 -155
  100. package/src/utils/algorithms/path-finding.test.js +151 -151
  101. package/src/utils/data-structures/array.js +83 -83
  102. package/src/utils/data-structures/array.test.js +173 -173
  103. package/src/utils/data-structures/board.js +159 -159
  104. package/src/utils/data-structures/board.test.js +242 -242
  105. package/src/utils/data-structures/boolean.js +9 -9
  106. package/src/utils/data-structures/heap.js +164 -164
  107. package/src/utils/data-structures/heap.test.js +103 -103
  108. package/src/utils/data-structures/object.js +138 -102
  109. package/src/utils/data-structures/object.test.js +218 -121
  110. package/src/utils/data-structures/objects.js +66 -48
  111. package/src/utils/data-structures/objects.test.js +99 -99
  112. package/src/utils/data-structures/tree.js +36 -36
  113. package/src/utils/data-structures/tree.test.js +33 -33
  114. package/src/utils/functions/functions.js +19 -19
  115. package/src/utils/functions/functions.test.js +23 -23
  116. package/src/utils/math/geometry/circle.js +70 -117
  117. package/src/utils/math/geometry/circle.test.js +97 -97
  118. package/src/utils/math/geometry/hitmask.js +70 -39
  119. package/src/utils/math/geometry/hitmask.test.js +155 -84
  120. package/src/utils/math/geometry/line.js +35 -35
  121. package/src/utils/math/geometry/line.test.js +49 -49
  122. package/src/utils/math/geometry/point.js +78 -71
  123. package/src/utils/math/geometry/point.test.js +81 -81
  124. package/src/utils/math/geometry/rectangle.js +76 -45
  125. package/src/utils/math/geometry/rectangle.test.js +42 -42
  126. package/src/utils/math/geometry/segment.js +80 -80
  127. package/src/utils/math/geometry/segment.test.js +183 -183
  128. package/src/utils/math/geometry/triangle.js +15 -15
  129. package/src/utils/math/geometry/triangle.test.js +11 -11
  130. package/src/utils/math/linear-algebra/2d.js +28 -28
  131. package/src/utils/math/linear-algebra/2d.test.js +17 -17
  132. package/src/utils/math/linear-algebra/quaternion.js +22 -22
  133. package/src/utils/math/linear-algebra/quaternion.test.js +25 -25
  134. package/src/utils/math/linear-algebra/quaternions.js +20 -20
  135. package/src/utils/math/linear-algebra/quaternions.test.js +29 -29
  136. package/src/utils/math/linear-algebra/vector.js +327 -302
  137. package/src/utils/math/linear-algebra/vector.test.js +265 -257
  138. package/src/utils/math/linear-algebra/vectors.js +122 -122
  139. package/src/utils/math/linear-algebra/vectors.test.js +65 -65
  140. package/src/utils/math/linear-interpolation.js +9 -0
  141. package/src/utils/math/numbers.js +90 -90
  142. package/src/utils/math/numbers.test.js +137 -137
  143. package/src/utils/math/rng.js +44 -44
  144. package/src/utils/math/rng.test.js +39 -39
  145. package/src/utils/math/statistics.js +43 -43
  146. package/src/utils/math/statistics.test.js +47 -47
  147. package/src/utils/math/trigonometry.js +89 -89
  148. package/src/utils/math/trigonometry.test.js +52 -52
  149. package/src/utils/physics/acceleration.js +61 -63
  150. package/src/utils/physics/friction.js +28 -30
  151. package/src/utils/physics/friction.test.js +42 -44
  152. package/src/utils/physics/gravity.js +69 -71
  153. package/src/utils/physics/gravity.test.js +77 -80
  154. package/src/utils/physics/jump.js +31 -41
  155. package/src/utils/physics/velocity.js +36 -38
  156. package/src/docs/ai/movement/dynamic/align.js +0 -131
  157. package/src/docs/ai/movement/dynamic/arrive.js +0 -88
  158. package/src/docs/ai/movement/dynamic/dynamic.mdx +0 -99
  159. package/src/docs/ai/movement/dynamic/dynamic.stories.js +0 -58
  160. package/src/docs/ai/movement/dynamic/evade.js +0 -72
  161. package/src/docs/ai/movement/dynamic/face.js +0 -90
  162. package/src/docs/ai/movement/dynamic/flee.js +0 -38
  163. package/src/docs/ai/movement/dynamic/look-where-youre-going.js +0 -114
  164. package/src/docs/ai/movement/dynamic/match-velocity.js +0 -92
  165. package/src/docs/ai/movement/dynamic/pursue.js +0 -72
  166. package/src/docs/ai/movement/dynamic/seek.js +0 -37
  167. package/src/docs/ai/movement/dynamic/wander.js +0 -71
  168. package/src/docs/ai/movement/kinematic/align.js +0 -122
  169. package/src/docs/ai/movement/kinematic/arrive.js +0 -78
  170. package/src/docs/ai/movement/kinematic/face.js +0 -82
  171. package/src/docs/ai/movement/kinematic/flee.js +0 -36
  172. package/src/docs/ai/movement/kinematic/kinematic.mdx +0 -67
  173. package/src/docs/ai/movement/kinematic/kinematic.stories.js +0 -42
  174. package/src/docs/ai/movement/kinematic/seek.js +0 -34
  175. package/src/docs/ai/movement/kinematic/wander-as-seek.js +0 -62
  176. package/src/docs/ai/movement/kinematic/wander.js +0 -28
  177. package/src/docs/bounds.js +0 -7
  178. package/src/docs/code-reuse.js +0 -35
  179. package/src/docs/collision/circles.js +0 -58
  180. package/src/docs/collision/collision.mdx +0 -27
  181. package/src/docs/collision/collision.stories.js +0 -22
  182. package/src/docs/collision/platform.js +0 -76
  183. package/src/docs/collision/tilemap.js +0 -181
  184. package/src/docs/empty.js +0 -1
  185. package/src/docs/engine.mdx +0 -81
  186. package/src/docs/engine.stories.js +0 -37
  187. package/src/docs/event-handlers.js +0 -68
  188. package/src/docs/framerate.js +0 -37
  189. package/src/docs/game.jsx +0 -15
  190. package/src/docs/image/image.js +0 -19
  191. package/src/docs/image/image.stories.js +0 -22
  192. package/src/docs/image/sprite.js +0 -39
  193. package/src/docs/image/tilemap.js +0 -84
  194. package/src/docs/input/controls.js +0 -67
  195. package/src/docs/input/gamepad.js +0 -67
  196. package/src/docs/input/input.mdx +0 -55
  197. package/src/docs/input/input.stories.js +0 -27
  198. package/src/docs/input/keyboard.js +0 -58
  199. package/src/docs/input/mouse.js +0 -32
  200. package/src/docs/instances.js +0 -49
  201. package/src/docs/player/dynamic/double-jump.js +0 -90
  202. package/src/docs/player/dynamic/dynamic.stories.js +0 -32
  203. package/src/docs/player/dynamic/jump.js +0 -83
  204. package/src/docs/player/dynamic/modern-controls.js +0 -57
  205. package/src/docs/player/dynamic/shooter-controls.js +0 -51
  206. package/src/docs/player/dynamic/tank-controls.js +0 -44
  207. package/src/docs/player/kinematic/double-jump.js +0 -90
  208. package/src/docs/player/kinematic/jump.js +0 -82
  209. package/src/docs/player/kinematic/kinematic.stories.js +0 -32
  210. package/src/docs/player/kinematic/modern-controls.js +0 -56
  211. package/src/docs/player/kinematic/shooter-controls.js +0 -48
  212. package/src/docs/player/kinematic/tank-controls.js +0 -42
  213. package/src/docs/quick-start/first-game.js +0 -49
  214. package/src/docs/quick-start/hello-world.js +0 -1
  215. package/src/docs/quick-start.mdx +0 -127
  216. package/src/docs/quick-start.stories.js +0 -17
  217. package/src/docs/recipes/add-and-remove.js +0 -71
  218. package/src/docs/recipes/add-instance.js +0 -42
  219. package/src/docs/recipes/decision-tree.js +0 -169
  220. package/src/docs/recipes/random-instances.js +0 -25
  221. package/src/docs/recipes/recipes.mdx +0 -81
  222. package/src/docs/recipes/recipes.stories.js +0 -37
  223. package/src/docs/recipes/remove-instance.js +0 -52
  224. package/src/docs/recipes/states.js +0 -64
  225. package/src/docs/ui/button.js +0 -28
  226. package/src/docs/ui/form.stories.js +0 -55
  227. package/src/docs/ui-chooser.jsx +0 -6
  228. package/src/docs/utils/data-structures/object.mdx +0 -47
  229. package/src/docs/utils/data-structures/objects.mdx +0 -30
  230. package/src/docs/utils/functions/functions.mdx +0 -34
  231. package/src/docs/utils/math/geometry/circle.mdx +0 -55
  232. package/src/docs/utils/math/geometry/point.mdx +0 -38
  233. package/src/docs/utils/math/geometry/rectangle.mdx +0 -24
  234. package/src/docs/utils/math/geometry/segment.mdx +0 -55
  235. package/src/docs/utils/math/geometry/triangle.mdx +0 -22
  236. package/src/docs/utils/math/linear-algebra/2d.mdx +0 -22
  237. package/src/docs/utils/math/linear-algebra/quaternion.mdx +0 -21
  238. package/src/docs/utils/math/linear-algebra/quaternions.mdx +0 -22
  239. package/src/docs/utils/math/linear-algebra/vector.mdx +0 -177
  240. package/src/docs/utils/math/linear-algebra/vectors.mdx +0 -58
  241. package/src/docs/utils/math/numbers.mdx +0 -76
  242. package/src/docs/utils/math/random.mdx +0 -35
  243. package/src/docs/utils/math/statistics.mdx +0 -38
  244. package/src/docs/utils/math/trigonometry.mdx +0 -85
  245. package/src/docs/utils/physics/friction.mdx +0 -20
  246. package/src/docs/utils/physics/gravity.mdx +0 -28
  247. package/src/engine/loop.js +0 -15
  248. package/src/engine/store.js +0 -174
  249. package/src/engine/store.test.js +0 -256
  250. package/src/engine.js +0 -74
  251. package/src/game/animation.js +0 -26
  252. package/src/game/bounds.js +0 -66
  253. package/src/game/decorators/character.js +0 -5
  254. package/src/game/decorators/clamp-to-bounds.js +0 -15
  255. package/src/game/decorators/controls/dynamic/modern.js +0 -48
  256. package/src/game/decorators/controls/dynamic/shooter.js +0 -47
  257. package/src/game/decorators/controls/dynamic/tank.js +0 -55
  258. package/src/game/decorators/controls/kinematic/modern.js +0 -49
  259. package/src/game/decorators/controls/kinematic/shooter.js +0 -45
  260. package/src/game/decorators/controls/kinematic/tank.js +0 -52
  261. package/src/game/decorators/debug/collisions.js +0 -32
  262. package/src/game/decorators/double-jump.js +0 -70
  263. package/src/game/decorators/fps.js +0 -30
  264. package/src/game/decorators/fsm.js +0 -27
  265. package/src/game/decorators/game.js +0 -11
  266. package/src/game/decorators/image/image.js +0 -5
  267. package/src/game/decorators/image/sprite.js +0 -5
  268. package/src/game/decorators/image/tilemap.js +0 -5
  269. package/src/game/decorators/input/controls.js +0 -27
  270. package/src/game/decorators/input/gamepad.js +0 -74
  271. package/src/game/decorators/input/input.js +0 -41
  272. package/src/game/decorators/input/keyboard.js +0 -49
  273. package/src/game/decorators/input/mouse.js +0 -65
  274. package/src/game/decorators/jump.js +0 -72
  275. package/src/game/decorators/platform.js +0 -5
  276. package/src/game/decorators/ui/button.js +0 -21
  277. package/src/game/sprite.js +0 -119
  278. package/src/ui/canvas/absolute-position.js +0 -17
  279. package/src/ui/canvas/image/hitmask.js +0 -37
  280. package/src/ui/canvas.js +0 -81
  281. package/src/ui/react/game/character/index.jsx +0 -30
  282. package/src/utils/math/geometry/platform.js +0 -42
  283. package/src/utils/math/geometry/platform.test.js +0 -133
  284. /package/src/{ui → renderers}/react/game/character/character.module.scss +0 -0
  285. /package/src/{ui → renderers}/react/game/cursor/cursor.module.scss +0 -0
  286. /package/src/{ui → renderers}/react/game/form/fields/field/field.module.scss +0 -0
  287. /package/src/{ui → renderers}/react/game/form/fields/fields.module.scss +0 -0
  288. /package/src/{ui → renderers}/react/game/form/form.module.scss +0 -0
  289. /package/src/{ui → renderers}/react/game/platform/platform.module.scss +0 -0
  290. /package/src/{ui → renderers}/react/game/scene/scene.module.scss +0 -0
  291. /package/src/{ui → renderers}/react/game/sprite/sprite.module.css +0 -0
  292. /package/src/{ui → renderers}/react/hocs/with-absolute-position/with-absolute-position.module.scss +0 -0
@@ -1,71 +0,0 @@
1
- import { enableCharacter } from "@inglorious/game/decorators/character.js"
2
- import { enableMouse } from "@inglorious/game/decorators/input/mouse.js"
3
- import { filter } from "@inglorious/utils/data-structures/object.js"
4
- import { random } from "@inglorious/utils/math/rng.js"
5
- import { pi } from "@inglorious/utils/math/trigonometry.js"
6
-
7
- export default {
8
- types: {
9
- mouse: [
10
- enableMouse(),
11
- {
12
- "scene:click"(instance, event, options) {
13
- const { instances, notify } = options
14
- const characters = filter(
15
- instances,
16
- (_, { type }) => type === "character",
17
- )
18
- const ids = Object.keys(characters)
19
-
20
- const maxId = ids.length
21
- ? Number(ids[ids.length - 1].replace("character", ""))
22
- : 0
23
-
24
- notify({
25
- id: "instance:add",
26
- payload: {
27
- id: `character${maxId + 1}`,
28
- type: "character",
29
- position: event.payload,
30
- orientation: random(0, 2 * pi(), 0.01),
31
- collisions: {
32
- hitbox: {
33
- shape: "circle",
34
- radius: 12,
35
- },
36
- },
37
- },
38
- })
39
- },
40
-
41
- // this event handler is needed for React
42
- "instance:click"(instance, event, { notify }) {
43
- notify({ id: "instance:remove", payload: event.payload })
44
- },
45
- },
46
- ],
47
-
48
- character: [
49
- enableCharacter(),
50
- {
51
- // this event handler is needed in React
52
- "instance:click"(instance, event, { notify }) {
53
- notify({ id: "instance:remove", payload: event.payload })
54
- },
55
- },
56
- ],
57
- },
58
-
59
- instances: {
60
- mouse: {
61
- type: "mouse",
62
- position: [400, 0, 300],
63
- collisions: {
64
- hitbox: {
65
- shape: "circle",
66
- radius: 1,
67
- },
68
- },
69
- },
70
- },
71
- }
@@ -1,42 +0,0 @@
1
- import { enableCharacter } from "@inglorious/game/decorators/character.js"
2
- import { enableMouse } from "@inglorious/game/decorators/input/mouse.js"
3
- import { filter } from "@inglorious/utils/data-structures/object.js"
4
- import { random } from "@inglorious/utils/math/rng.js"
5
- import { pi } from "@inglorious/utils/math/trigonometry.js"
6
-
7
- export default {
8
- types: {
9
- mouse: [
10
- enableMouse(),
11
- {
12
- "mouse:click"(instance, event, options) {
13
- const { instances, notify } = options
14
- const characters = filter(
15
- instances,
16
- (_, { type }) => type === "character",
17
- )
18
- const ids = Object.keys(characters)
19
-
20
- notify({
21
- id: "instance:add",
22
- payload: {
23
- id: `character${ids.length + 1}`,
24
- type: "character",
25
- position: event.payload,
26
- orientation: random(0, 2 * pi(), 0.01),
27
- },
28
- })
29
- },
30
- },
31
- ],
32
-
33
- character: [enableCharacter()],
34
- },
35
-
36
- instances: {
37
- mouse: {
38
- type: "mouse",
39
- position: [400, 0, 300],
40
- },
41
- },
42
- }
@@ -1,169 +0,0 @@
1
- import arrive from "@inglorious/engine/ai/movement/kinematic/arrive.js"
2
- import { enableFsm } from "@inglorious/game/decorators/fsm.js"
3
- import { enableSprite } from "@inglorious/game/decorators/image/sprite.js"
4
- import { enableMouse } from "@inglorious/game/decorators/input/mouse.js"
5
- import { Sprite } from "@inglorious/game/sprite.js"
6
- import { decide } from "@inglorious/utils/algorithms/decision-tree.js"
7
- import { merge } from "@inglorious/utils/data-structures/objects.js"
8
- import { length } from "@inglorious/utils/math/linear-algebra/vector.js"
9
- import { subtract } from "@inglorious/utils/math/linear-algebra/vectors.js"
10
-
11
- // A reusable decision tree node
12
- const wakeUp = () => ({
13
- test: ({ instance, target }) => {
14
- const distance = length(subtract(target.position, instance.position))
15
- return distance >= 10
16
- },
17
- true: () => "aware",
18
- false: ({ instance }) => instance.state,
19
- })
20
-
21
- // Our decision tree
22
- const nextState = {
23
- test: ({ instance }) => instance.state,
24
- idle: () => ({
25
- test: ({ instance, target }) => {
26
- const distance = length(subtract(target.position, instance.position))
27
- return distance < 250
28
- },
29
- true: () => "aware",
30
- false: ({ instance }) => instance.state,
31
- }),
32
- chasing: () => ({
33
- test: ({ instance, target }) => {
34
- const distance = length(subtract(target.position, instance.position))
35
- return distance >= 250
36
- },
37
- true: () => "idle",
38
- false: () => ({
39
- test: ({ instance, target }) => {
40
- const distance = length(subtract(target.position, instance.position))
41
- return distance < 10
42
- },
43
- true: () => "sleepy",
44
- false: ({ instance }) => instance.state,
45
- }),
46
- }),
47
- sleepy: wakeUp,
48
- sleeping: wakeUp,
49
- }
50
-
51
- export default {
52
- types: {
53
- mouse: [enableMouse()],
54
-
55
- cat: [
56
- enableSprite(),
57
- enableFsm({
58
- idle: {
59
- "game:update"(instance, event, options) {
60
- const { mouse } = options.instances
61
-
62
- Sprite.play("idle", instance, options)
63
-
64
- instance.state = decide(nextState, { instance, target: mouse })
65
- },
66
- },
67
-
68
- aware: {
69
- "game:update"(instance, event, options) {
70
- Sprite.play("aware", instance, options)
71
- },
72
-
73
- "sprite:animationEnd"(instance, event) {
74
- const { id, spriteState } = event.payload
75
-
76
- // always check who originated the event and which sprite is running!
77
- if (id === instance.id && spriteState === "aware") {
78
- instance.state = "chasing"
79
- }
80
- },
81
- },
82
-
83
- chasing: {
84
- "game:update"(instance, event, options) {
85
- const { mouse } = options.instances
86
-
87
- merge(instance, arrive(instance, mouse, options))
88
-
89
- const spriteState = Sprite.move8(instance)
90
- Sprite.play(spriteState, instance, options)
91
-
92
- instance.state = decide(nextState, { instance, target: mouse })
93
- },
94
- },
95
-
96
- sleepy: {
97
- "game:update"(instance, event, options) {
98
- const { mouse } = options.instances
99
-
100
- Sprite.play("sleepy", instance, options)
101
-
102
- instance.state = decide(nextState, { instance, target: mouse })
103
- },
104
-
105
- "sprite:animationEnd"(instance, event) {
106
- const { id, spriteState } = event.payload
107
-
108
- // always check who originated the event and which sprite is running!
109
- if (id === instance.id && spriteState === "sleepy") {
110
- instance.state = "sleeping"
111
- }
112
- },
113
- },
114
-
115
- sleeping: {
116
- "game:update"(instance, event, options) {
117
- const { mouse } = options.instances
118
-
119
- Sprite.play("sleeping", instance, options)
120
-
121
- instance.state = decide(nextState, { instance, target: mouse })
122
- },
123
- },
124
- }),
125
- ],
126
- },
127
-
128
- instances: {
129
- game: {
130
- pixelated: true,
131
- },
132
-
133
- mouse: {
134
- type: "mouse",
135
- position: [0, 0, 0],
136
- },
137
-
138
- neko: {
139
- type: "cat",
140
- state: "idle",
141
- maxSpeed: 250,
142
- position: [400, 0, 300],
143
- sprite: {
144
- image: {
145
- id: "neko",
146
- src: "/sprites/neko.png",
147
- imageSize: [192, 192],
148
- tileSize: [32, 32],
149
- scale: 2,
150
- },
151
- speed: 0.2,
152
- frames: {
153
- idle: [4],
154
- aware: [0, 4],
155
- leftUp: [0x80000000 + 23, 0x80000000 + 29],
156
- up: [28, 30, 28, 31],
157
- rightUp: [23, 29],
158
- right: [16, 22],
159
- rightDown: [13, 14],
160
- down: [1, 2, 1, 7],
161
- leftDown: [0x80000000 + 13, 0x80000000 + 14],
162
- left: [0x80000000 + 16, 0x80000000 + 22],
163
- sleepy: [4, 10, 10, 3, 9, 15, 9, 15, 15],
164
- sleeping: [26, 26, 27, 27],
165
- },
166
- },
167
- },
168
- },
169
- }
@@ -1,25 +0,0 @@
1
- import { enableCharacter } from "@inglorious/game/decorators/character.js"
2
- import { random } from "@inglorious/utils/math/rng.js"
3
- import { pi } from "@inglorious/utils/math/trigonometry.js"
4
-
5
- export default {
6
- types: {
7
- character: [enableCharacter()],
8
- },
9
-
10
- instances: {
11
- ...Object.fromEntries(
12
- Array(100)
13
- .fill(null)
14
- .map((_, index) => [
15
- `character${index + 1}`,
16
- {
17
- id: `character${index + 1}`,
18
- type: "character",
19
- position: [random(0, 800), 0, random(0, 600)],
20
- orientation: random(0, 2 * pi(), 0.01),
21
- },
22
- ]),
23
- ),
24
- },
25
- }
@@ -1,81 +0,0 @@
1
- import { Canvas, Meta, Source } from '@storybook/blocks'
2
- import * as Recipes from './recipes.stories'
3
-
4
- import addAndRemove from './add-and-remove?raw'
5
- import addInstance from './add-instance?raw'
6
- import decisionTree from './decision-tree?raw'
7
- import randomInstances from './random-instances?raw'
8
- import removeInstance from './remove-instance?raw'
9
- import states from './states?raw'
10
-
11
- <Meta of={Recipes} />
12
-
13
- # Recipes
14
-
15
- Some common patterns that can be used in your game. Check the source code of each recipe and take inspiration!
16
-
17
- ## Spawning Random Instances
18
-
19
- The game configuration is pure JavaScript, so there's not much difference between defining the instances one by one or writing some code that generates them automatically!
20
-
21
- Here we are using a couple of utility functions from the [rng](/?path=/docs/utils-math-rng--docs) and [trigonometry](/?path=/docs/utils-math-trigonometry--docs) packages.
22
-
23
- <Source dark code={randomInstances} />
24
-
25
- <Canvas of={Recipes.RandomInstances} />
26
-
27
- ## Adding An Instance
28
-
29
- Programmatically adding a new instance to the scene is as easy as sending an event to the game engine asking nicely.
30
-
31
- Try clicking anywhere on the scene to create a new instance with random orientation.
32
-
33
- Here, apart from some math functions, we are also using a predefined mouse [input](/?path=/docs/games-input--docs) package that allows us to position a cursor and handle its events.
34
-
35
- <Source dark code={addInstance} />
36
-
37
- <Canvas of={Recipes.AddInstance} />
38
-
39
- ## Removing An Instance
40
-
41
- Removing an instance is pretty much the same: we notify the engine of our intention and we enjoy the result.
42
-
43
- Try clicking on any instance do remove it from the scene.
44
-
45
- <Source dark code={removeInstance} />
46
-
47
- <Canvas of={Recipes.RemoveInstance} />
48
-
49
- ## Add And Remove
50
-
51
- Clicking on a character will prevent the event from bubbling on the scene, so it's safe to add and remove characters by using the same strategy: click as if there's no tomorrow!
52
-
53
- The most complex part of this code is generating new incremental ids, which is not even mandatory. You know best what's good for your game!
54
-
55
- <Source dark code={addAndRemove} />
56
-
57
- <Canvas of={Recipes.AddAndRemove} />
58
-
59
- ## States
60
-
61
- Adding a states to our character is very easy, in fact every game so far was secretely using states already!
62
-
63
- We just need to wrap our event handlers into a `states` object, set an initial `state` on our instance, and transition from one state to another inside our event handlers.
64
-
65
- Try moving the cursor near the character to make it hunt you, and then moving it away to see the character wander around aimlessly.
66
-
67
- This code may seem a lot, but it's just a merge of the *Arrive* and *Wander* [Kinematic AI Movement](/?path=/docs/games-ai-movement-kinematic--docs) examples so feel free to take a look at them before approaching this one.
68
-
69
- <Source dark code={states} />
70
-
71
- <Canvas of={Recipes.States} />
72
-
73
- ## Decision Trees
74
-
75
- Decision trees are another sweet feature built-in inside of Inglorious Engine. The following example doesn't really need one, but why not?
76
-
77
- This game also showcases sprites!
78
-
79
- <Source dark code={decisionTree} />
80
-
81
- <Canvas of={Recipes.DecisionTree} />
@@ -1,37 +0,0 @@
1
- import UiChooser from "@inglorious/docs/ui-chooser.jsx"
2
-
3
- import addAndRemove from "./add-and-remove.js"
4
- import addInstance from "./add-instance.js"
5
- import decisionTree from "./decision-tree.js"
6
- import randomInstances from "./random-instances.js"
7
- import removeInstance from "./remove-instance.js"
8
- import states from "./states.js"
9
-
10
- export default {
11
- title: "Engine/Recipes",
12
- component: UiChooser,
13
- }
14
-
15
- export const RandomInstances = {
16
- args: { config: randomInstances },
17
- }
18
-
19
- export const AddInstance = {
20
- args: { config: addInstance },
21
- }
22
-
23
- export const RemoveInstance = {
24
- args: { config: removeInstance },
25
- }
26
-
27
- export const AddAndRemove = {
28
- args: { config: addAndRemove },
29
- }
30
-
31
- export const States = {
32
- args: { config: states },
33
- }
34
-
35
- export const DecisionTree = {
36
- args: { config: decisionTree },
37
- }
@@ -1,52 +0,0 @@
1
- import { enableCharacter } from "@inglorious/game/decorators/character.js"
2
- import { enableMouse } from "@inglorious/game/decorators/input/mouse.js"
3
- import { random } from "@inglorious/utils/math/rng.js"
4
- import { pi } from "@inglorious/utils/math/trigonometry.js"
5
-
6
- export default {
7
- types: {
8
- mouse: [enableMouse()],
9
-
10
- character: [
11
- enableCharacter(),
12
- {
13
- // this event handler is needed in React
14
- "instance:click"(instance, event, { notify }) {
15
- notify({ id: "instance:remove", payload: event.payload })
16
- },
17
- },
18
- ],
19
- },
20
-
21
- instances: {
22
- mouse: {
23
- type: "mouse",
24
- position: [400, 0, 300],
25
- collisions: {
26
- hitbox: {
27
- shape: "circle",
28
- radius: 1,
29
- },
30
- },
31
- },
32
-
33
- ...Object.fromEntries(
34
- Array(5)
35
- .fill(null)
36
- .map((_, index) => [
37
- `character${index + 1}`,
38
- {
39
- type: "character",
40
- position: [random(0, 800), 0, random(0, 600)],
41
- orientation: random(0, 2 * pi(), 0.01),
42
- collisions: {
43
- hitbox: {
44
- shape: "circle",
45
- radius: 12,
46
- },
47
- },
48
- },
49
- ]),
50
- ),
51
- },
52
- }
@@ -1,64 +0,0 @@
1
- import arrive from "@inglorious/engine/ai/movement/kinematic/arrive.js"
2
- import wander from "@inglorious/engine/ai/movement/kinematic/wander.js"
3
- import { clampToBounds, flip } from "@inglorious/game/bounds.js"
4
- import { enableCharacter } from "@inglorious/game/decorators/character.js"
5
- import { enableFsm } from "@inglorious/game/decorators/fsm.js"
6
- import { enableMouse } from "@inglorious/game/decorators/input/mouse.js"
7
- import { merge } from "@inglorious/utils/data-structures/objects.js"
8
- import { length } from "@inglorious/utils/math/linear-algebra/vector.js"
9
- import { subtract } from "@inglorious/utils/math/linear-algebra/vectors.js"
10
- import { pi } from "@inglorious/utils/math/trigonometry.js"
11
-
12
- export default {
13
- types: {
14
- mouse: [enableMouse()],
15
-
16
- character: [
17
- enableCharacter(),
18
- enableFsm({
19
- meandering: {
20
- "game:update"(instance, event, options) {
21
- const { instances } = options
22
- const target = instances.mouse
23
-
24
- merge(instance, wander(instance, options))
25
- flip(instance, instances.game.bounds)
26
-
27
- if (length(subtract(instance.position, target.position)) < 200) {
28
- instance.state = "hunting"
29
- }
30
- },
31
- },
32
-
33
- hunting: {
34
- "game:update"(instance, event, options) {
35
- const { instances } = options
36
- const target = instances.mouse
37
-
38
- merge(instance, arrive(instance, target, options))
39
- clampToBounds(instance, instances.game.bounds)
40
-
41
- if (length(subtract(instance.position, target.position)) >= 200) {
42
- instance.state = "meandering"
43
- }
44
- },
45
- },
46
- }),
47
- ],
48
- },
49
-
50
- instances: {
51
- mouse: {
52
- type: "mouse",
53
- position: [0, 0, 0],
54
- },
55
-
56
- character: {
57
- type: "character",
58
- state: "meandering",
59
- maxSpeed: 250,
60
- maxAngularSpeed: pi() / 4,
61
- position: [400, 0, 300],
62
- },
63
- },
64
- }
@@ -1,28 +0,0 @@
1
- import { enableMouse } from "@inglorious/game/decorators/input/mouse.js"
2
- import { enableButton } from "@inglorious/game/decorators/ui/button.js"
3
-
4
- export default {
5
- types: {
6
- mouse: [enableMouse()],
7
-
8
- button: [
9
- enableButton(),
10
- {
11
- size: [100, 50, 0],
12
- color: "black",
13
- backgroundColor: "darkgrey",
14
- },
15
- ],
16
- },
17
-
18
- instances: {
19
- mouse: {
20
- type: "mouse",
21
- },
22
-
23
- rect1: {
24
- type: "button",
25
- position: [400, 0, 300],
26
- },
27
- },
28
- }
@@ -1,55 +0,0 @@
1
- import UiChooser from "@inglorious/docs/ui-chooser.jsx"
2
-
3
- export default {
4
- title: "UI/Form",
5
- component: UiChooser,
6
- args: { ui: "react" },
7
- }
8
-
9
- export const Default = {
10
- args: {
11
- config: {
12
- types: {
13
- form: {},
14
- },
15
-
16
- instances: {
17
- login: {
18
- type: "form",
19
- position: [150, 0, 600 - 160],
20
- fields: {
21
- username: {
22
- label: "Username",
23
- },
24
- password: {
25
- label: "Password",
26
- inputType: "password",
27
- },
28
- },
29
- groups: {
30
- extraInfo: {
31
- title: "Extra Info",
32
- fields: {
33
- kids: {
34
- label: "How many kids do you have?",
35
- inputType: "number",
36
- defaultValue: 0,
37
- },
38
- dogs: {
39
- label: "Do you like dogs?",
40
- inputType: "checkbox",
41
- defaultValue: true,
42
- },
43
- cats: {
44
- label: "Do you like cats?",
45
- inputType: "checkbox",
46
- defaultValue: true,
47
- },
48
- },
49
- },
50
- },
51
- },
52
- },
53
- },
54
- },
55
- }
@@ -1,6 +0,0 @@
1
- import CanvasGame from "@inglorious/docs/game.jsx"
2
- import ReactGame from "@inglorious/ui/react/game/index.jsx"
3
-
4
- export default function UiChooser({ ui = "canvas", ...props }) {
5
- return ui === "canvas" ? <CanvasGame {...props} /> : <ReactGame {...props} />
6
- }
@@ -1,47 +0,0 @@
1
- import { Meta } from '@storybook/blocks'
2
-
3
- import { filter, find, map, toString } from '@inglorious/utils/data-structures/object'
4
-
5
- <Meta title="Utils/Data Structures/Object" />
6
-
7
- # Object
8
-
9
- Functions that deal with a single object.
10
-
11
- ## Filter
12
-
13
- Behaves similar to `Array.prototype.filter`, returning an object that contains only properties that satisfy the given predicate.
14
-
15
- <code>const predicate = (key) =&gt; ['key2', 'key3'].includes(key)</code>&nbsp;
16
-
17
- <code>const obj = \{ key1: 'value1', key2: 'value2', key3: 'value3' \}</code>&nbsp;
18
-
19
- <code>filter(obj, predicate) == {toString(filter({ key1: 'value1', key2: 'value2', key3: 'value3' }, (key) => ['key2', 'key3'].includes(key)))}</code>&nbsp;
20
-
21
- ## Find
22
-
23
- Behaves similar to `Array.prototype.find`, returning an object that contains only the first property that satisfies the given predicate.
24
-
25
- <code>const predicate = (key) =&gt; ['key2', 'key3'].includes(key)</code>&nbsp;
26
-
27
- <code>const obj = \{ key1: 'value1', key2: 'value2', key3: 'value3' \}</code>&nbsp;
28
-
29
- <code>find(obj, predicate) == {toString(find({ key1: 'value1', key2: 'value2', key3: 'value3' }, (key) => ['key2', 'key3'].includes(key)))}</code>&nbsp;
30
-
31
- ## Map
32
-
33
- Behaves similar to `Array.prototype.map`, returning an object that has values transformed according to the given callback.
34
-
35
- <code>const callback = (key, value) =&gt; value.toUpperCase()</code>&nbsp;
36
-
37
- <code>const obj = \{ key1: 'value1', key2: 'value2', key3: 'value3' \}</code>&nbsp;
38
-
39
- <code>map(obj, callback) == {toString(map({ key1: 'value1', key2: 'value2', key3: 'value3' }, (key, value) => value.toUpperCase()))}</code>&nbsp;
40
-
41
- ## To String
42
-
43
- Returns a string representation of a given object.
44
-
45
- <code>const obj = \{ key1: 'value1', key2: 'value2', key3: 'value3' \}</code>&nbsp;
46
-
47
- <code>toString(obj) == "{toString({ key1: 'value1', key2: 'value2', key3: 'value3' })}"</code>&nbsp;