@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,5 @@
1
+ import draw from "@inglorious/ui/canvas/image/tilemap.js"
2
+
3
+ export function enableTilemap() {
4
+ return { draw }
5
+ }
@@ -0,0 +1,27 @@
1
+ import { extend } from "@inglorious/utils/data-structures/objects.js"
2
+
3
+ import { createGamepad, enableGamepad } from "./gamepad.js"
4
+ import { createInput, enableInput } from "./input.js"
5
+ import { createKeyboard, enableKeyboard } from "./keyboard.js"
6
+
7
+ const DEFAULT_PARAMS = {
8
+ name: "input0",
9
+ }
10
+
11
+ export function enableControls(params) {
12
+ params = extend(DEFAULT_PARAMS, params)
13
+
14
+ return {
15
+ keyboard: [enableKeyboard(params)],
16
+ gamepad: [enableGamepad(params)],
17
+ input: [enableInput(params)],
18
+ }
19
+ }
20
+
21
+ export function createControls(name = DEFAULT_PARAMS.name, mapping = {}) {
22
+ return {
23
+ [`keyboard_${name}`]: createKeyboard(`keyboard_${name}`, mapping),
24
+ [`gamepad_${name}`]: createGamepad(`gamepad_${name}`, mapping),
25
+ [name]: createInput(name, mapping),
26
+ }
27
+ }
@@ -0,0 +1,74 @@
1
+ const DEFAULT_PARAMS = {
2
+ name: "gamepad0",
3
+ }
4
+
5
+ export function enableGamepad() {
6
+ return {
7
+ "game:update"(instance, event, { notify }) {
8
+ navigator.getGamepads().forEach((gamepad) => {
9
+ if (gamepad == null) {
10
+ return
11
+ }
12
+
13
+ gamepad.axes.forEach((axis, index) => {
14
+ notify({
15
+ id: "gamepad:axis",
16
+ payload: { id: gamepad.index, axis: `Axis${index}`, value: axis },
17
+ })
18
+ })
19
+
20
+ gamepad.buttons.forEach((button, index) => {
21
+ const id = button.pressed ? "gamepad:press" : "gamepad:release"
22
+ notify({
23
+ id,
24
+ payload: { id: gamepad.index, button: `Btn${index}` },
25
+ })
26
+ })
27
+ })
28
+ },
29
+
30
+ "gamepad:axis"(instance, event, { notify }) {
31
+ const { id, axis, value } = event.payload
32
+
33
+ if (instance.id !== `gamepad${id}`) {
34
+ return
35
+ }
36
+
37
+ const action = instance.mapping[axis]
38
+ instance[action] = value
39
+ notify({ id: "input:axis", payload: { id, action, value } })
40
+ },
41
+
42
+ "gamepad:press"(instance, event, { notify }) {
43
+ const { id, button } = event.payload
44
+
45
+ if (instance.id !== `gamepad${id}`) {
46
+ return
47
+ }
48
+
49
+ const action = instance.mapping[button]
50
+ if (!instance[action]) {
51
+ instance[action] = true
52
+ notify({ id: "input:press", payload: { id, action } })
53
+ }
54
+ },
55
+
56
+ "gamepad:release"(instance, event, { notify }) {
57
+ const { id, button } = event.payload
58
+
59
+ if (instance.id !== `gamepad${id}`) {
60
+ return
61
+ }
62
+
63
+ const action = instance.mapping[button]
64
+ if (instance[action]) {
65
+ instance[action] = false
66
+ notify({ id: "input:release", payload: { id, action } })
67
+ }
68
+ },
69
+ }
70
+ }
71
+
72
+ export function createGamepad(name = DEFAULT_PARAMS.name, mapping = {}) {
73
+ return { id: name, type: "gamepad", mapping }
74
+ }
@@ -0,0 +1,41 @@
1
+ const DEFAULT_PARAMS = {
2
+ name: "input0",
3
+ }
4
+
5
+ export function enableInput() {
6
+ return {
7
+ "input:axis"(instance, event) {
8
+ const { id, action, value } = event.payload
9
+
10
+ if (!id.endsWith(instance.id)) {
11
+ return
12
+ }
13
+
14
+ instance[action] = value
15
+ },
16
+
17
+ "input:press"(instance, event) {
18
+ const { id, action } = event.payload
19
+
20
+ if (!id.endsWith(instance.id)) {
21
+ return
22
+ }
23
+
24
+ instance[action] = true
25
+ },
26
+
27
+ "input:release"(instance, event) {
28
+ const { id, action } = event.payload
29
+
30
+ if (!id.endsWith(instance.id)) {
31
+ return
32
+ }
33
+
34
+ instance[action] = false
35
+ },
36
+ }
37
+ }
38
+
39
+ export function createInput(name = DEFAULT_PARAMS.name, mapping = {}) {
40
+ return { id: name, type: "input", mapping }
41
+ }
@@ -0,0 +1,49 @@
1
+ const DEFAULT_PARAMS = {
2
+ name: "keyboard0",
3
+ }
4
+
5
+ export function enableKeyboard() {
6
+ let handleKeyDown, handleKeyUp
7
+
8
+ return {
9
+ "game:start"(instance, event, options) {
10
+ handleKeyDown = createKeyboardHandler("keyboard:keyDown", options)
11
+ handleKeyUp = createKeyboardHandler("keyboard:keyUp", options)
12
+
13
+ document.addEventListener("keydown", handleKeyDown)
14
+ document.addEventListener("keyup", handleKeyUp)
15
+ },
16
+
17
+ "game:stop"() {
18
+ document.removeEventListener("keydown", handleKeyDown)
19
+ document.removeEventListener("keyup", handleKeyUp)
20
+ },
21
+
22
+ "keyboard:keyDown"(instance, event, { notify }) {
23
+ const action = instance.mapping[event.payload]
24
+ if (!instance[action]) {
25
+ instance[action] = true
26
+ notify({ id: "input:press", payload: { id: instance.id, action } })
27
+ }
28
+ },
29
+
30
+ "keyboard:keyUp"(instance, event, { notify }) {
31
+ const action = instance.mapping[event.payload]
32
+ if (instance[action]) {
33
+ instance[action] = false
34
+ notify({ id: "input:release", payload: { id: instance.id, action } })
35
+ }
36
+ },
37
+ }
38
+ }
39
+
40
+ export function createKeyboard(name = DEFAULT_PARAMS.name, mapping = {}) {
41
+ return { id: name, type: "keyboard", mapping }
42
+ }
43
+
44
+ function createKeyboardHandler(id, { notify }) {
45
+ return (event) => {
46
+ event.stopPropagation()
47
+ notify({ id, payload: event.code })
48
+ }
49
+ }
@@ -0,0 +1,65 @@
1
+ import { findCollision } from "@inglorious/engine/collision/detection.js"
2
+ import { clampToBounds } from "@inglorious/game/bounds.js"
3
+ import draw from "@inglorious/ui/canvas/mouse.js"
4
+
5
+ const NO_Y = 0
6
+
7
+ export function enableMouse() {
8
+ return {
9
+ draw,
10
+
11
+ "mouse:move"(instance, event, { instances }) {
12
+ instance.position = event.payload
13
+
14
+ clampToBounds(instance, instances.game.bounds)
15
+ },
16
+
17
+ "mouse:click"(instance, event, options) {
18
+ const { notify } = options
19
+
20
+ const clickedInstance = findCollision(instance, options)
21
+ if (clickedInstance) {
22
+ notify({ id: "instance:click", payload: clickedInstance.id })
23
+ } else {
24
+ notify({ id: "scene:click", payload: event.payload })
25
+ }
26
+ },
27
+ }
28
+ }
29
+
30
+ export function track(parent, options) {
31
+ const handleMouseMove = createHandler("mouse:move", parent, options)
32
+ const handleClick = createHandler("mouse:click", parent, options)
33
+
34
+ return { onMouseMove: handleMouseMove, onClick: handleClick }
35
+ }
36
+
37
+ function createHandler(id, parent, { notify }) {
38
+ return (event) => {
39
+ event.stopPropagation()
40
+
41
+ if (parent == null) {
42
+ return
43
+ }
44
+
45
+ const payload = calculatePosition({
46
+ clientX: event.clientX,
47
+ clientY: event.clientY,
48
+ parent,
49
+ })
50
+
51
+ notify({ id, payload })
52
+ }
53
+ }
54
+
55
+ function calculatePosition({ clientX, clientY, parent }) {
56
+ const bounds = parent.getBoundingClientRect()
57
+
58
+ const scaleX = (parent.width || parent.clientWidth) / bounds.width
59
+ const scaleY = (parent.height || parent.clientHeight) / bounds.height
60
+
61
+ const x = (clientX - bounds.left) * scaleX
62
+ const z = (bounds.bottom - clientY) * scaleY
63
+
64
+ return [x, NO_Y, z]
65
+ }
@@ -0,0 +1,72 @@
1
+ import { collidesWith } from "@inglorious/engine/collision/detection.js"
2
+ import { extend, merge } from "@inglorious/utils/data-structures/objects.js"
3
+ import { applyGravity } from "@inglorious/utils/physics/gravity.js"
4
+ import { jump } from "@inglorious/utils/physics/jump.js"
5
+
6
+ import { enableFsm } from "./fsm.js"
7
+
8
+ const DEFAULT_PARAMS = {
9
+ onState: "default",
10
+ onInput: "input0",
11
+ maxSpeed: 250,
12
+ maxJump: 100,
13
+ maxLeap: 100,
14
+ }
15
+ const FALLING = 0
16
+
17
+ export function enableJump(params) {
18
+ params = extend(DEFAULT_PARAMS, params)
19
+
20
+ const freeFall = createFreeFall(params)
21
+
22
+ return enableFsm({
23
+ [params.onState]: {
24
+ "game:update"(instance, event, options) {
25
+ freeFall(instance, event, options)
26
+ },
27
+
28
+ "input:press"(instance, event, options) {
29
+ instance.onInput = instance.onInput ?? params.onInput
30
+ instance.maxJump = instance.maxJump ?? params.maxJump
31
+ instance.maxLeap = instance.maxLeap ?? params.maxLeap
32
+ instance.maxSpeed = instance.maxSpeed ?? params.maxSpeed
33
+
34
+ const { id, action } = event.payload
35
+ if (id.endsWith(instance.onInput) && action === "jump") {
36
+ instance.state = "jumping"
37
+ merge(instance, jump(instance, options))
38
+ }
39
+ },
40
+ },
41
+
42
+ jumping: {
43
+ "game:update"(instance, event, options) {
44
+ freeFall(instance, event, options)
45
+ },
46
+ },
47
+ })
48
+ }
49
+
50
+ function createFreeFall(params) {
51
+ return (instance, event, options) => {
52
+ instance.maxLeap = instance.maxLeap ?? params.maxLeap
53
+
54
+ merge(instance, applyGravity(instance, options))
55
+
56
+ const targets = Object.values(options.instances).filter(
57
+ ({ type }) => type === "platform",
58
+ )
59
+
60
+ targets.forEach((target) => {
61
+ if (instance.vy < FALLING && collidesWith(instance, target, "platform")) {
62
+ instance.vy = 0
63
+ const [x, , z] = instance.position
64
+ const { radius } = instance.collisions.platform
65
+ const [, targetY] = target.position
66
+ const py = targetY + radius
67
+ instance.position = [x, py, z]
68
+ instance.state = params.onState
69
+ }
70
+ })
71
+ }
72
+ }
@@ -0,0 +1,5 @@
1
+ import draw from "@inglorious/ui/canvas/shapes/rectangle.js"
2
+
3
+ export function enablePlatform() {
4
+ return { draw }
5
+ }
@@ -0,0 +1,21 @@
1
+ import draw from "@inglorious/ui/canvas/form/button.js"
2
+
3
+ export function enableButton() {
4
+ return {
5
+ draw,
6
+
7
+ states: {
8
+ default: {
9
+ "instance:click"(instance) {
10
+ instance.state = "pressed"
11
+ },
12
+ },
13
+
14
+ pressed: {
15
+ "instance:release"(instance) {
16
+ instance.state = "default"
17
+ },
18
+ },
19
+ },
20
+ }
21
+ }
@@ -0,0 +1,119 @@
1
+ /* eslint-disable no-magic-numbers */
2
+ import { Animation } from "@inglorious/game/animation.js"
3
+ import { mod } from "@inglorious/utils/math/numbers.js"
4
+ import { pi, toRange } from "@inglorious/utils/math/trigonometry.js"
5
+
6
+ const BEFORE = -1
7
+ const AFTER = 1
8
+
9
+ export const Sprite = {
10
+ move2,
11
+ move4,
12
+ move6,
13
+ move8,
14
+ play,
15
+ }
16
+
17
+ function move2(instance) {
18
+ const directions = 2
19
+ const multiple = pi() / directions
20
+ const theta = toRange(instance.orientation) / multiple
21
+
22
+ if (theta > 0 + BEFORE && theta < 0 + AFTER) {
23
+ return "right"
24
+ } else if (theta < 0 + BEFORE || theta > 0 + AFTER) {
25
+ return "left"
26
+ }
27
+
28
+ return instance.sprite.state ?? "right"
29
+ }
30
+
31
+ function move4(instance) {
32
+ const directions = 4
33
+ const multiple = pi() / directions
34
+ const theta = toRange(instance.orientation) / multiple
35
+
36
+ if (theta >= -2 + BEFORE && theta < -2 + AFTER) {
37
+ return "down"
38
+ } else if (theta >= 0 + BEFORE && theta < 0 + AFTER) {
39
+ return "right"
40
+ } else if (theta >= 2 + BEFORE && theta < 2 + AFTER) {
41
+ return "up"
42
+ } else if (theta < 0 + BEFORE || theta > 0 + AFTER) {
43
+ return "left"
44
+ }
45
+
46
+ return instance.sprite.state ?? "down"
47
+ }
48
+
49
+ function move6(instance) {
50
+ const directions = 6
51
+ const multiple = pi() / directions
52
+ const theta = toRange(instance.orientation) / multiple
53
+
54
+ if (theta >= -6 + BEFORE && theta < -6 + AFTER) {
55
+ return "leftDown"
56
+ } else if (theta >= -4 + BEFORE && theta < -4 + AFTER) {
57
+ return "down"
58
+ } else if (theta >= -2 + BEFORE && theta < -2 + AFTER) {
59
+ return "rightDown"
60
+ } else if (theta >= 0 + BEFORE && theta < 0 + AFTER) {
61
+ return "right"
62
+ } else if (theta >= 2 + BEFORE && theta < 2 + AFTER) {
63
+ return "rightUp"
64
+ } else if (theta >= 4 + BEFORE && theta < 4 + AFTER) {
65
+ return "up"
66
+ } else if (theta >= 6 + BEFORE && theta < 6 + AFTER) {
67
+ return "leftUp"
68
+ } else if (theta < 0 + BEFORE || theta > 0 + AFTER) {
69
+ return "left"
70
+ }
71
+
72
+ return instance.sprite.state ?? "down"
73
+ }
74
+
75
+ function move8(instance) {
76
+ const directions = 8
77
+ const multiple = pi() / directions
78
+ const theta = toRange(instance.orientation) / multiple
79
+
80
+ if (theta >= -6 + BEFORE && theta < -6 + AFTER) {
81
+ return "leftDown"
82
+ } else if (theta >= -4 + BEFORE && theta < -4 + AFTER) {
83
+ return "down"
84
+ } else if (theta >= -2 + BEFORE && theta < -2 + AFTER) {
85
+ return "rightDown"
86
+ } else if (theta >= 0 + BEFORE && theta < 0 + AFTER) {
87
+ return "right"
88
+ } else if (theta >= 2 + BEFORE && theta < 2 + AFTER) {
89
+ return "rightUp"
90
+ } else if (theta >= 4 + BEFORE && theta < 4 + AFTER) {
91
+ return "up"
92
+ } else if (theta >= 6 + BEFORE && theta < 6 + AFTER) {
93
+ return "leftUp"
94
+ } else if (theta < 0 + BEFORE || theta > 0 + AFTER) {
95
+ return "left"
96
+ }
97
+
98
+ return instance.sprite.state ?? "down"
99
+ }
100
+
101
+ function play(spriteState, instance, options) {
102
+ Animation.play("sprite", spriteState, instance, { ...options, onTick })
103
+ }
104
+
105
+ function onTick(instance, options) {
106
+ const { notify } = options
107
+
108
+ const { frames, state } = instance.sprite
109
+
110
+ const framesLength = frames[state].length
111
+
112
+ instance.sprite.value = mod(instance.sprite.value + 1, framesLength)
113
+ if (instance.sprite.value === framesLength - 1) {
114
+ notify({
115
+ id: `sprite:animationEnd`,
116
+ payload: { id: instance.id, spriteState: state },
117
+ })
118
+ }
119
+ }
package/src/main.js ADDED
@@ -0,0 +1,5 @@
1
+ import { start } from "@inglorious/ui/canvas.js"
2
+ import game from "game"
3
+
4
+ const canvas = document.getElementById("canvas")
5
+ window.addEventListener("load", () => start(game, canvas))
@@ -0,0 +1,17 @@
1
+ import { snap, zero } from "@inglorious/utils/math/linear-algebra/vector.js"
2
+
3
+ export function absolutePosition(draw) {
4
+ return (ctx, instance, options = {}) => {
5
+ const { position = zero() } = instance
6
+ const { instances } = options
7
+ const [, , , screenHeight] = instances.game.bounds
8
+ const [x, y, z] = snap(position)
9
+
10
+ ctx.save()
11
+
12
+ ctx.translate(x, screenHeight - y - z)
13
+ draw(ctx, instance, options)
14
+
15
+ ctx.restore()
16
+ }
17
+ }
@@ -0,0 +1,35 @@
1
+ /* eslint-disable no-magic-numbers */
2
+
3
+ import circle from "./shapes/circle.js"
4
+
5
+ export default function draw(ctx, instance) {
6
+ const { size = 24, orientation = 0 } = instance
7
+
8
+ const radius = size * 0.5
9
+
10
+ ctx.save()
11
+
12
+ ctx.rotate(-orientation)
13
+ ctx.translate(radius - 1, 0)
14
+
15
+ ctx.fillStyle = "black"
16
+
17
+ ctx.beginPath()
18
+ ctx.moveTo(0, 6)
19
+ ctx.lineTo(0, -6)
20
+ ctx.lineTo(6, 0)
21
+ ctx.fill()
22
+ ctx.closePath()
23
+ ctx.restore()
24
+
25
+ ctx.save()
26
+
27
+ circle(ctx, {
28
+ ...instance,
29
+ radius,
30
+ position: undefined,
31
+ backgroundColor: "lightgrey",
32
+ })
33
+
34
+ ctx.restore()
35
+ }
@@ -0,0 +1,25 @@
1
+ /* eslint-disable no-magic-numbers */
2
+
3
+ export default function draw(ctx, instance) {
4
+ const { size, color = "black", thickness = 1 } = instance
5
+ const [width = 100, height = 50] = size
6
+
7
+ ctx.save()
8
+
9
+ ctx.lineWidth = thickness
10
+ ctx.strokeStyle = color
11
+
12
+ if (instance.state === "pressed") {
13
+ ctx.fillStyle = "white"
14
+ } else {
15
+ ctx.fillStyle = "black"
16
+ }
17
+
18
+ ctx.beginPath()
19
+ ctx.fillRect(0, 0, width, height)
20
+ ctx.strokeRect(0, 0, width, height)
21
+ ctx.stroke()
22
+ ctx.closePath()
23
+
24
+ ctx.restore()
25
+ }
@@ -0,0 +1,18 @@
1
+ const DEFAULT_PADDING = 10
2
+ const ONE_SECOND = 1
3
+
4
+ export default function draw(ctx, instance) {
5
+ const { accuracy, size, value } = instance.dt
6
+
7
+ ctx.save()
8
+
9
+ ctx.font = `${size}px sans serif`
10
+ ctx.fillStyle = "black"
11
+ ctx.fillText(
12
+ (ONE_SECOND / value).toFixed(accuracy),
13
+ DEFAULT_PADDING,
14
+ DEFAULT_PADDING + size,
15
+ )
16
+
17
+ ctx.restore()
18
+ }
@@ -0,0 +1,37 @@
1
+ import drawRectangle from "@inglorious/ui/canvas/shapes/rectangle.js"
2
+ import { max } from "@inglorious/utils/data-structures/array.js"
3
+
4
+ const NO_Y = 0
5
+ const MAX_HUE = 255
6
+
7
+ export default function draw(ctx, instance, options) {
8
+ const { tileSize, columns, heights } = instance
9
+
10
+ const [tileWidth, tileHeight] = tileSize
11
+ const rows = Math.ceil(heights.length / columns)
12
+ const height = rows * tileHeight
13
+
14
+ const minH = 0
15
+ const maxH = max(heights)
16
+
17
+ heights.forEach((h, index) => {
18
+ const x = (index % columns) * tileWidth
19
+ const z = Math.floor(index / columns) * tileHeight - height
20
+
21
+ ctx.save()
22
+
23
+ const normalizedH = (h - minH) / (maxH - minH)
24
+ const hue = MAX_HUE - normalizedH * MAX_HUE
25
+
26
+ const instance = {
27
+ offset: [x, NO_Y, z],
28
+ size: [tileWidth, NO_Y, tileHeight],
29
+ color: "transparent",
30
+ backgroundColor: `hsla(${hue}, 100%, 50%, 0.2)`,
31
+ }
32
+
33
+ drawRectangle(ctx, instance, options)
34
+
35
+ ctx.restore()
36
+ })
37
+ }
@@ -0,0 +1,37 @@
1
+ const DEFAULT_POSITION = 0
2
+
3
+ export default function draw(ctx, instance) {
4
+ const { image, sx = DEFAULT_POSITION, sy = DEFAULT_POSITION } = instance
5
+ const { id, src, imageSize, tileSize = imageSize } = image
6
+
7
+ const [tileWidth, tileHeight] = tileSize
8
+
9
+ const imgParams = [
10
+ sx * tileWidth,
11
+ sy * tileHeight,
12
+ tileWidth,
13
+ tileHeight,
14
+ DEFAULT_POSITION,
15
+ DEFAULT_POSITION,
16
+ tileWidth,
17
+ tileHeight,
18
+ ]
19
+
20
+ ctx.save()
21
+
22
+ const img = document.getElementById(id)
23
+ if (img) {
24
+ ctx.drawImage(img, ...imgParams)
25
+ } else {
26
+ const img = new Image()
27
+ img.id = id
28
+ img.style.display = "none"
29
+ img.onload = () => {
30
+ ctx.drawImage(img, ...imgParams)
31
+ }
32
+ img.src = src
33
+ document.body.appendChild(img)
34
+ }
35
+
36
+ ctx.restore()
37
+ }