@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,49 @@
1
+ import imageDraw from "./image.js"
2
+
3
+ const DEFAULT_SCALE = 1
4
+
5
+ const FLIP = -1
6
+ const NO_FLIP = 1
7
+
8
+ const CENTER_WIDTH = 2
9
+ const CENTER_HEIGHT = 2
10
+
11
+ const FLIPPED_HORIZONTALLY_FLAG = 0x80000000
12
+ const FLIPPED_VERTICALLY_FLAG = 0x40000000
13
+ // const FLIPPED_DIAGONALLY_FLAG = 0x20000000
14
+ // const ROTATED_HEXAGONAL_120_FLAG = 0x10000000
15
+
16
+ export default function draw(ctx, instance, options) {
17
+ const { image, frames, state, value } = instance.sprite
18
+ const { imageSize, tileSize, scale = DEFAULT_SCALE } = image
19
+
20
+ const [imageWidth] = imageSize
21
+ const [tileWidth, tileHeight] = tileSize
22
+ const cols = imageWidth / tileWidth
23
+
24
+ const flaggedTile = frames[state][value]
25
+
26
+ const isFlippedHorizontally = !!(flaggedTile & FLIPPED_HORIZONTALLY_FLAG)
27
+ const isFlippedVertically = !!(flaggedTile & FLIPPED_VERTICALLY_FLAG)
28
+
29
+ let tile = flaggedTile
30
+ tile &= ~FLIPPED_HORIZONTALLY_FLAG
31
+ tile &= ~FLIPPED_VERTICALLY_FLAG
32
+
33
+ const sx = tile % cols
34
+ const sy = Math.floor(tile / cols)
35
+
36
+ ctx.save()
37
+
38
+ ctx.scale(scale, scale)
39
+
40
+ ctx.scale(
41
+ isFlippedHorizontally ? FLIP : NO_FLIP,
42
+ isFlippedVertically ? FLIP : NO_FLIP,
43
+ )
44
+ ctx.translate(-tileWidth / CENTER_WIDTH, -tileHeight / CENTER_HEIGHT)
45
+
46
+ imageDraw(ctx, { image, sx, sy }, options)
47
+
48
+ ctx.restore()
49
+ }
@@ -0,0 +1,64 @@
1
+ import imageDraw from "./image.js"
2
+
3
+ const DEFAULT_SCALE = 1
4
+
5
+ const FLIP = -1
6
+ const NO_FLIP = 1
7
+
8
+ const CENTER_WIDTH = 2
9
+ const CENTER_HEIGHT = 2
10
+
11
+ const FLIPPED_HORIZONTALLY_FLAG = 0x80000000
12
+ const FLIPPED_VERTICALLY_FLAG = 0x40000000
13
+ // const FLIPPED_DIAGONALLY_FLAG = 0x20000000
14
+ // const ROTATED_HEXAGONAL_120_FLAG = 0x10000000
15
+
16
+ export default function draw(ctx, instance, options) {
17
+ const { tilemap } = instance
18
+ const { image, layers } = tilemap
19
+ const { imageSize, tileSize, columns, scale = DEFAULT_SCALE } = image
20
+
21
+ const [imageWidth] = imageSize
22
+ const [tileWidth, tileHeight] = tileSize
23
+ const sCols = imageWidth / tileWidth
24
+
25
+ ctx.save()
26
+
27
+ layers.forEach(({ tiles }) => {
28
+ const dRows = Math.ceil(tiles.length / columns)
29
+ const height = dRows * tileHeight
30
+
31
+ tiles.forEach((flaggedTile, index) => {
32
+ const isFlippedHorizontally = !!(flaggedTile & FLIPPED_HORIZONTALLY_FLAG)
33
+ const isFlippedVertically = !!(flaggedTile & FLIPPED_VERTICALLY_FLAG)
34
+
35
+ let tile = flaggedTile
36
+ tile &= ~FLIPPED_HORIZONTALLY_FLAG
37
+ tile &= ~FLIPPED_VERTICALLY_FLAG
38
+
39
+ const sx = tile % sCols
40
+ const sy = Math.floor(tile / sCols)
41
+
42
+ const dx = (index % columns) * tileWidth
43
+ const dy = Math.floor(index / columns) * tileHeight - height
44
+
45
+ ctx.save()
46
+
47
+ ctx.scale(scale, scale)
48
+ ctx.translate(dx, dy)
49
+
50
+ ctx.translate(tileWidth / CENTER_WIDTH, tileHeight / CENTER_HEIGHT)
51
+ ctx.scale(
52
+ isFlippedHorizontally ? FLIP : NO_FLIP,
53
+ isFlippedVertically ? FLIP : NO_FLIP,
54
+ )
55
+ ctx.translate(-tileWidth / CENTER_WIDTH, -tileHeight / CENTER_HEIGHT)
56
+
57
+ imageDraw(ctx, { image, sx, sy }, options)
58
+
59
+ ctx.restore()
60
+ })
61
+ })
62
+
63
+ ctx.restore()
64
+ }
@@ -0,0 +1,37 @@
1
+ /* eslint-disable no-magic-numbers */
2
+
3
+ export default function draw(ctx, instance) {
4
+ const { color = "black", thickness = 1, orientation = 0 } = instance
5
+
6
+ ctx.save()
7
+
8
+ ctx.strokeStyle = color
9
+ ctx.fillStyle = color
10
+ ctx.lineWidth = thickness
11
+
12
+ ctx.rotate(-orientation)
13
+
14
+ ctx.beginPath()
15
+ ctx.moveTo(-6, 0)
16
+ ctx.lineTo(-3, 0)
17
+ ctx.stroke()
18
+
19
+ ctx.moveTo(4, 0)
20
+ ctx.lineTo(7, 0)
21
+ ctx.stroke()
22
+
23
+ ctx.moveTo(0, -6)
24
+ ctx.lineTo(0, -3)
25
+ ctx.stroke()
26
+
27
+ ctx.moveTo(0, 4)
28
+ ctx.lineTo(0, 7)
29
+ ctx.stroke()
30
+ ctx.closePath()
31
+
32
+ ctx.beginPath()
33
+ ctx.fillRect(0, 0, 1, 1)
34
+ ctx.closePath()
35
+
36
+ ctx.restore()
37
+ }
@@ -0,0 +1,31 @@
1
+ /* eslint-disable no-magic-numbers */
2
+
3
+ import { zero } from "@inglorious/utils/math/linear-algebra/vector.js"
4
+ import { pi } from "@inglorious/utils/math/trigonometry.js"
5
+
6
+ export default function draw(ctx, instance) {
7
+ const {
8
+ position = zero(),
9
+ radius = 24,
10
+ color = "black",
11
+ backgroundColor = "transparent",
12
+ thickness = 1,
13
+ } = instance
14
+ const [x, , z] = position
15
+
16
+ ctx.save()
17
+
18
+ ctx.lineWidth = thickness
19
+ ctx.strokeStyle = color
20
+ ctx.fillStyle = backgroundColor
21
+
22
+ ctx.translate(x, z)
23
+
24
+ ctx.beginPath()
25
+ ctx.arc(0, 0, radius, 0, 2 * pi())
26
+ ctx.fill()
27
+ ctx.stroke()
28
+ ctx.closePath()
29
+
30
+ ctx.restore()
31
+ }
@@ -0,0 +1,31 @@
1
+ /* eslint-disable no-magic-numbers */
2
+
3
+ import { zero } from "@inglorious/utils/math/linear-algebra/vector.js"
4
+
5
+ export default function draw(ctx, instance) {
6
+ const {
7
+ offset = zero(),
8
+ size,
9
+ color = "black",
10
+ backgroundColor = "transparent",
11
+ thickness = 1,
12
+ } = instance
13
+ const [x, , z] = offset
14
+ const [width = 100, height = 50, depth = 0] = size
15
+
16
+ ctx.save()
17
+
18
+ ctx.lineWidth = thickness
19
+ ctx.strokeStyle = color
20
+ ctx.fillStyle = backgroundColor
21
+
22
+ ctx.translate(x, z)
23
+
24
+ ctx.beginPath()
25
+ ctx.fillRect(0, 0, width, height + depth)
26
+ ctx.strokeRect(0, 0, width, height + depth)
27
+ ctx.stroke()
28
+ ctx.closePath()
29
+
30
+ ctx.restore()
31
+ }
@@ -0,0 +1,81 @@
1
+ import Engine from "@inglorious/engine.js"
2
+ import { track } from "@inglorious/game/decorators/input/mouse.js"
3
+
4
+ import { absolutePosition } from "./canvas/absolute-position.js"
5
+
6
+ const Y = 1
7
+ const Z = 2
8
+
9
+ export function start(config, canvas) {
10
+ const ctx = canvas.getContext("2d")
11
+ const engine = new Engine(config, { render: render(ctx) })
12
+
13
+ const { game } = engine._store.getState().instances
14
+ const [, , width, height] = game.bounds
15
+
16
+ canvas.style.width = `${width}px`
17
+ canvas.style.height = `${height}px`
18
+ const dpi = window.devicePixelRatio
19
+ canvas.width = Math.floor(width * dpi)
20
+ canvas.height = Math.floor(height * dpi)
21
+ ctx.scale(dpi, dpi)
22
+
23
+ if (game.pixelated) {
24
+ canvas.style.imageRendering = "pixelated"
25
+ ctx.textRendering = "geometricPrecision"
26
+ }
27
+
28
+ document.addEventListener("keypress", (event) => {
29
+ if (event.key === "p") {
30
+ engine.isRunning ? engine.stop() : engine.start()
31
+ }
32
+ })
33
+
34
+ const { onMouseMove, onClick } = track(canvas, {
35
+ notify: engine.notify,
36
+ })
37
+
38
+ canvas.addEventListener("mousemove", onMouseMove)
39
+ canvas.addEventListener("click", onClick)
40
+
41
+ engine.start()
42
+ }
43
+
44
+ function render(ctx) {
45
+ return (options) => {
46
+ const { types, instances } = options
47
+
48
+ const { game, mouse, ...rest } = instances
49
+
50
+ const [x, y, width, height] = game.bounds
51
+ ctx.fillStyle = "lightgrey"
52
+ ctx.fillRect(x, y, width, height)
53
+ if (game.pixelated) {
54
+ ctx.imageSmoothingEnabled = false
55
+ }
56
+
57
+ Object.values(rest)
58
+ .filter(({ position }) => position)
59
+ .toSorted(
60
+ (a, b) =>
61
+ a.layer - b.layer ||
62
+ a.position[Y] - b.position[Y] ||
63
+ b.position[Z] - a.position[Z],
64
+ )
65
+ .forEach((instance) =>
66
+ draw(ctx, instance, { ...options, type: types[instance.type] }),
67
+ )
68
+
69
+ if (mouse) {
70
+ draw(ctx, mouse, { ...options, type: types[mouse.type] })
71
+ }
72
+ }
73
+ }
74
+
75
+ function draw(ctx, instance, options) {
76
+ const draw = options.type.draw
77
+
78
+ if (draw) {
79
+ absolutePosition(draw)(ctx, instance, options)
80
+ }
81
+ }
@@ -0,0 +1,17 @@
1
+ .character {
2
+ width: var(--size);
3
+ height: var(--size);
4
+ box-sizing: border-box;
5
+ background-color: lightgrey;
6
+ border: 1px solid grey;
7
+ border-radius: 50%;
8
+ transform: translate(-50%, -50%) rotate(var(--angle));
9
+
10
+ &::after {
11
+ content: '🞁';
12
+ position: absolute;
13
+ top: 50%;
14
+ right: 0;
15
+ transform: translate(50%, -50%) rotate(90deg);
16
+ }
17
+ }
@@ -0,0 +1,30 @@
1
+ import { useDispatch } from "react-redux"
2
+
3
+ import classes from "./character.module.scss"
4
+
5
+ const DEFAULT_SIZE = 24
6
+ const DEFAULT_ORIENTATION = 0
7
+
8
+ export default function Character({ id, type, instance, className, style }) {
9
+ const notify = useDispatch()
10
+
11
+ const size = type.size ?? DEFAULT_SIZE
12
+ const { orientation = DEFAULT_ORIENTATION } = instance
13
+
14
+ const handleClick = (event) => {
15
+ event.stopPropagation()
16
+ notify({ id: "instance:click", payload: id })
17
+ }
18
+
19
+ return (
20
+ <div
21
+ className={`${classes.character} ${className}`}
22
+ style={{
23
+ ...style,
24
+ "--size": `${size}px`,
25
+ "--angle": `${-orientation}rad`,
26
+ }}
27
+ onClick={handleClick}
28
+ />
29
+ )
30
+ }
@@ -0,0 +1,47 @@
1
+ .cursor {
2
+ width: 1px;
3
+ height: 1px;
4
+ background-color: black;
5
+ transform: rotate(var(--angle));
6
+ pointer-events: none;
7
+
8
+ .top,
9
+ .bottom,
10
+ .left,
11
+ .right {
12
+ background-color: black;
13
+ position: absolute;
14
+ }
15
+
16
+ .top,
17
+ .bottom {
18
+ width: 1px;
19
+ height: 4px;
20
+ }
21
+
22
+ .top {
23
+ bottom: 5px;
24
+ left: 0;
25
+ }
26
+
27
+ .bottom {
28
+ top: 5px;
29
+ left: 0;
30
+ }
31
+
32
+ .left,
33
+ .right {
34
+ width: 4px;
35
+ height: 1px;
36
+ }
37
+
38
+ .left {
39
+ top: 0;
40
+ right: 5px;
41
+ }
42
+
43
+ .right {
44
+ top: 0;
45
+ left: 5px;
46
+ }
47
+ }
@@ -0,0 +1,20 @@
1
+ import classes from "./cursor.module.scss"
2
+
3
+ export default function Cursor({ instance, className, style }) {
4
+ const { orientation } = instance
5
+
6
+ return (
7
+ <div
8
+ className={`${classes.cursor} ${className}`}
9
+ style={{
10
+ ...style,
11
+ "--angle": `${-orientation}rad`,
12
+ }}
13
+ >
14
+ <div className={classes.top} />
15
+ <div className={classes.bottom} />
16
+ <div className={classes.left} />
17
+ <div className={classes.right} />
18
+ </div>
19
+ )
20
+ }
@@ -0,0 +1,5 @@
1
+ .field {
2
+ display: grid;
3
+ grid-template-columns: 1fr 1fr auto;
4
+ column-gap: 0.5rem;
5
+ }
@@ -0,0 +1,56 @@
1
+ import { useEffect, useState } from "react"
2
+ import { useDispatch } from "react-redux"
3
+
4
+ import classes from "./field.module.scss"
5
+
6
+ export default function Field({ id, instance }) {
7
+ const { label, inputType, defaultValue = "", ...rest } = instance
8
+
9
+ const [value, setValue] = useState(defaultValue)
10
+ useEffect(() => {
11
+ setValue(defaultValue)
12
+ }, [defaultValue, inputType])
13
+
14
+ const notify = useDispatch()
15
+
16
+ const handleChange = (event) => {
17
+ const newValue = parse(event.target, inputType)
18
+ setValue(newValue)
19
+ notify({ id: "field:change", payload: { id, value: newValue } })
20
+ }
21
+
22
+ const handleClick = () => {
23
+ const newValue = defaultValue
24
+ setValue(newValue)
25
+ notify({ id: "field:change", payload: { id, value: newValue } })
26
+ }
27
+
28
+ return (
29
+ <div className={classes.field}>
30
+ <label htmlFor={id}>{label}</label>
31
+ <input
32
+ {...rest}
33
+ id={id}
34
+ type={inputType}
35
+ value={value}
36
+ checked={value}
37
+ onChange={handleChange}
38
+ />
39
+ <button onClick={handleClick}>&#8634;</button>
40
+ </div>
41
+ )
42
+ }
43
+
44
+ function parse(target, inputType) {
45
+ switch (inputType) {
46
+ case "checkbox":
47
+ case "radio":
48
+ return target.checked
49
+
50
+ case "number":
51
+ return Number(target.value)
52
+
53
+ default:
54
+ return target.value
55
+ }
56
+ }
@@ -0,0 +1,48 @@
1
+ $black: #282c34;
2
+ $dark-grey: #32363e;
3
+ $middle-grey: #5c6370;
4
+ $light-grey: #abb2bf;
5
+ $highlight: #c678dd;
6
+
7
+ .fields {
8
+ padding: 1rem;
9
+ display: grid;
10
+ row-gap: 0.5rem;
11
+ border-bottom: 1px solid $black;
12
+
13
+ label,
14
+ input,
15
+ button {
16
+ color: $light-grey;
17
+ }
18
+
19
+ input {
20
+ background-color: $dark-grey;
21
+ border-top: 1px solid $black;
22
+ border-left: 1px solid $black;
23
+ border-bottom: 1px solid $light-grey;
24
+ border-right: 1px solid $light-grey;
25
+ outline: none;
26
+
27
+ &[type='checkbox'],
28
+ &[type='radio'] {
29
+ accent-color: $dark-grey;
30
+ }
31
+
32
+ &[type='number'] {
33
+ text-align: right;
34
+ }
35
+
36
+ &:focus {
37
+ outline: 1px solid $highlight;
38
+ }
39
+ }
40
+
41
+ button {
42
+ background-color: $dark-grey;
43
+ border-top: 1px solid $light-grey;
44
+ border-left: 1px solid $light-grey;
45
+ border-bottom: 1px solid $black;
46
+ border-right: 1px solid $black;
47
+ }
48
+ }
@@ -0,0 +1,12 @@
1
+ import Field from "./field/index.jsx"
2
+ import classes from "./fields.module.scss"
3
+
4
+ export default function Fields({ fields }) {
5
+ return (
6
+ <div className={classes.fields}>
7
+ {Object.entries(fields).map(([id, field]) => (
8
+ <Field key={id} id={id} instance={field} />
9
+ ))}
10
+ </div>
11
+ )
12
+ }
@@ -0,0 +1,18 @@
1
+ $black: #282c34;
2
+ $dark-grey: #32363e;
3
+ $middle-grey: #5c6370;
4
+ $light-grey: #abb2bf;
5
+ $highlight: #c678dd;
6
+
7
+ .form {
8
+ font-family: system-ui;
9
+ background-color: $dark-grey;
10
+ cursor: initial;
11
+
12
+ > .group {
13
+ color: $light-grey;
14
+ background-color: $middle-grey;
15
+ padding: 0.5rem;
16
+ border-top: 1px solid $light-grey;
17
+ }
18
+ }
@@ -0,0 +1,22 @@
1
+ import { Fragment } from "react"
2
+
3
+ import Fields from "./fields/index.jsx"
4
+ import classes from "./form.module.scss"
5
+
6
+ export default function Form({ instance, className, style }) {
7
+ const { fields, groups } = instance
8
+
9
+ return (
10
+ <div className={`${classes.form} ${className}`} style={style}>
11
+ {fields && <Fields fields={fields} />}
12
+
13
+ {groups &&
14
+ Object.entries(groups).map(([id, { title, fields }]) => (
15
+ <Fragment key={id}>
16
+ <div className={classes.group}>{title}</div>
17
+ {fields && <Fields fields={fields} />}
18
+ </Fragment>
19
+ ))}
20
+ </div>
21
+ )
22
+ }
@@ -0,0 +1,16 @@
1
+ export const DEFAULT_ACCURACY = 0
2
+
3
+ const ONE_SECOND = 1
4
+
5
+ export default function Fps({ type, instance, className, style }) {
6
+ const { accuracy = DEFAULT_ACCURACY } = type
7
+ const { value } = instance.dt
8
+
9
+ const fps = ONE_SECOND / value
10
+
11
+ return (
12
+ <div className={className} style={style}>
13
+ FPS: {fps.toFixed(accuracy)}
14
+ </div>
15
+ )
16
+ }
@@ -0,0 +1,71 @@
1
+ import { withAbsolutePosition } from "@inglorious/ui/react/hocs/with-absolute-position"
2
+ import { useSelector } from "react-redux"
3
+
4
+ import Character from "./character/index.jsx"
5
+ import Cursor from "./cursor/index.jsx"
6
+ import Form from "./form/index.jsx"
7
+ import Fps from "./fps/index.jsx"
8
+ import Platform from "./platform/index.jsx"
9
+ import Scene from "./scene/index.jsx"
10
+ import Sprite from "./sprite/index.jsx"
11
+ import Stats from "./stats/index.jsx"
12
+
13
+ const Components = {
14
+ character: withAbsolutePosition(Character),
15
+ mouse: withAbsolutePosition(Cursor),
16
+ form: withAbsolutePosition(Form),
17
+ fps: withAbsolutePosition(Fps),
18
+ platform: withAbsolutePosition(Platform),
19
+ sprite: withAbsolutePosition(Sprite),
20
+ stats: withAbsolutePosition(Stats),
21
+ }
22
+
23
+ const Y = 1
24
+ const Z = 2
25
+
26
+ export default function Game({ engine }) {
27
+ // NOTE: don't use simply engine.instances here: need to subscribe to animate scene!
28
+ const instances = useSelector((state) => state.instances)
29
+
30
+ const types = engine._store.getTypes()
31
+ const { mouse, ...rest } = instances
32
+ const options = { types, instances }
33
+
34
+ const draw = createDraw(options)
35
+
36
+ return (
37
+ <Scene instances={instances}>
38
+ {Object.values(rest)
39
+ .filter(({ position }) => position)
40
+ .toSorted(
41
+ (a, b) =>
42
+ a.layer - b.layer ||
43
+ a.position[Y] - b.position[Y] ||
44
+ b.position[Z] - a.position[Z],
45
+ )
46
+ .map(draw)}
47
+ {mouse && draw(mouse)}
48
+ </Scene>
49
+ )
50
+ }
51
+
52
+ function createDraw(options) {
53
+ return function Draw(instance) {
54
+ const { types, instances } = options
55
+ const type = types[instance.type]
56
+
57
+ const Component = instance.sprite
58
+ ? Components.sprite
59
+ : Components[instance.type]
60
+
61
+ return (
62
+ <Component
63
+ key={instance.id}
64
+ id={instance.id}
65
+ type={type}
66
+ instance={instance}
67
+ instances={instances}
68
+ />
69
+ )
70
+ }
71
+ }