@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.
- package/LICENSE +9 -0
- package/README.md +72 -0
- package/package.json +76 -0
- package/src/docs/ai/movement/dynamic/align.js +131 -0
- package/src/docs/ai/movement/dynamic/arrive.js +88 -0
- package/src/docs/ai/movement/dynamic/dynamic.mdx +99 -0
- package/src/docs/ai/movement/dynamic/dynamic.stories.js +58 -0
- package/src/docs/ai/movement/dynamic/evade.js +72 -0
- package/src/docs/ai/movement/dynamic/face.js +90 -0
- package/src/docs/ai/movement/dynamic/flee.js +38 -0
- package/src/docs/ai/movement/dynamic/look-where-youre-going.js +114 -0
- package/src/docs/ai/movement/dynamic/match-velocity.js +92 -0
- package/src/docs/ai/movement/dynamic/pursue.js +72 -0
- package/src/docs/ai/movement/dynamic/seek.js +37 -0
- package/src/docs/ai/movement/dynamic/wander.js +71 -0
- package/src/docs/ai/movement/kinematic/align.js +122 -0
- package/src/docs/ai/movement/kinematic/arrive.js +78 -0
- package/src/docs/ai/movement/kinematic/face.js +82 -0
- package/src/docs/ai/movement/kinematic/flee.js +36 -0
- package/src/docs/ai/movement/kinematic/kinematic.mdx +67 -0
- package/src/docs/ai/movement/kinematic/kinematic.stories.js +42 -0
- package/src/docs/ai/movement/kinematic/seek.js +34 -0
- package/src/docs/ai/movement/kinematic/wander-as-seek.js +62 -0
- package/src/docs/ai/movement/kinematic/wander.js +28 -0
- package/src/docs/bounds.js +7 -0
- package/src/docs/code-reuse.js +35 -0
- package/src/docs/collision/circles.js +58 -0
- package/src/docs/collision/collision.mdx +27 -0
- package/src/docs/collision/collision.stories.js +22 -0
- package/src/docs/collision/platform.js +76 -0
- package/src/docs/collision/tilemap.js +181 -0
- package/src/docs/empty.js +1 -0
- package/src/docs/engine.mdx +81 -0
- package/src/docs/engine.stories.js +37 -0
- package/src/docs/event-handlers.js +68 -0
- package/src/docs/framerate.js +37 -0
- package/src/docs/game.jsx +15 -0
- package/src/docs/image/image.js +19 -0
- package/src/docs/image/image.stories.js +22 -0
- package/src/docs/image/sprite.js +39 -0
- package/src/docs/image/tilemap.js +84 -0
- package/src/docs/input/controls.js +67 -0
- package/src/docs/input/gamepad.js +67 -0
- package/src/docs/input/input.mdx +55 -0
- package/src/docs/input/input.stories.js +27 -0
- package/src/docs/input/keyboard.js +58 -0
- package/src/docs/input/mouse.js +32 -0
- package/src/docs/instances.js +49 -0
- package/src/docs/player/dynamic/double-jump.js +90 -0
- package/src/docs/player/dynamic/dynamic.stories.js +32 -0
- package/src/docs/player/dynamic/jump.js +83 -0
- package/src/docs/player/dynamic/modern-controls.js +57 -0
- package/src/docs/player/dynamic/shooter-controls.js +51 -0
- package/src/docs/player/dynamic/tank-controls.js +44 -0
- package/src/docs/player/kinematic/double-jump.js +90 -0
- package/src/docs/player/kinematic/jump.js +82 -0
- package/src/docs/player/kinematic/kinematic.stories.js +32 -0
- package/src/docs/player/kinematic/modern-controls.js +56 -0
- package/src/docs/player/kinematic/shooter-controls.js +48 -0
- package/src/docs/player/kinematic/tank-controls.js +42 -0
- package/src/docs/quick-start/first-game.js +49 -0
- package/src/docs/quick-start/hello-world.js +1 -0
- package/src/docs/quick-start.mdx +127 -0
- package/src/docs/quick-start.stories.js +17 -0
- package/src/docs/recipes/add-and-remove.js +71 -0
- package/src/docs/recipes/add-instance.js +42 -0
- package/src/docs/recipes/decision-tree.js +169 -0
- package/src/docs/recipes/random-instances.js +25 -0
- package/src/docs/recipes/recipes.mdx +81 -0
- package/src/docs/recipes/recipes.stories.js +37 -0
- package/src/docs/recipes/remove-instance.js +52 -0
- package/src/docs/recipes/states.js +64 -0
- package/src/docs/ui/button.js +28 -0
- package/src/docs/ui/form.stories.js +55 -0
- package/src/docs/ui-chooser.jsx +6 -0
- package/src/docs/utils/data-structures/object.mdx +47 -0
- package/src/docs/utils/data-structures/objects.mdx +30 -0
- package/src/docs/utils/functions/functions.mdx +34 -0
- package/src/docs/utils/math/geometry/circle.mdx +55 -0
- package/src/docs/utils/math/geometry/point.mdx +38 -0
- package/src/docs/utils/math/geometry/rectangle.mdx +24 -0
- package/src/docs/utils/math/geometry/segment.mdx +55 -0
- package/src/docs/utils/math/geometry/triangle.mdx +22 -0
- package/src/docs/utils/math/linear-algebra/2d.mdx +22 -0
- package/src/docs/utils/math/linear-algebra/quaternion.mdx +21 -0
- package/src/docs/utils/math/linear-algebra/quaternions.mdx +22 -0
- package/src/docs/utils/math/linear-algebra/vector.mdx +177 -0
- package/src/docs/utils/math/linear-algebra/vectors.mdx +58 -0
- package/src/docs/utils/math/numbers.mdx +76 -0
- package/src/docs/utils/math/random.mdx +35 -0
- package/src/docs/utils/math/statistics.mdx +38 -0
- package/src/docs/utils/math/trigonometry.mdx +85 -0
- package/src/docs/utils/physics/friction.mdx +20 -0
- package/src/docs/utils/physics/gravity.mdx +28 -0
- package/src/engine/ai/movement/dynamic/align.js +63 -0
- package/src/engine/ai/movement/dynamic/arrive.js +43 -0
- package/src/engine/ai/movement/dynamic/evade.js +38 -0
- package/src/engine/ai/movement/dynamic/face.js +20 -0
- package/src/engine/ai/movement/dynamic/flee.js +45 -0
- package/src/engine/ai/movement/dynamic/look-where-youre-going.js +17 -0
- package/src/engine/ai/movement/dynamic/match-velocity.js +50 -0
- package/src/engine/ai/movement/dynamic/pursue.js +38 -0
- package/src/engine/ai/movement/dynamic/seek.js +44 -0
- package/src/engine/ai/movement/dynamic/wander.js +32 -0
- package/src/engine/ai/movement/kinematic/align.js +37 -0
- package/src/engine/ai/movement/kinematic/arrive.js +42 -0
- package/src/engine/ai/movement/kinematic/face.js +20 -0
- package/src/engine/ai/movement/kinematic/flee.js +26 -0
- package/src/engine/ai/movement/kinematic/seek.js +26 -0
- package/src/engine/ai/movement/kinematic/seek.test.js +42 -0
- package/src/engine/ai/movement/kinematic/wander-as-seek.js +31 -0
- package/src/engine/ai/movement/kinematic/wander.js +27 -0
- package/src/engine/collision/detection.js +115 -0
- package/src/engine/loop/animation-frame.js +26 -0
- package/src/engine/loop/elapsed.js +23 -0
- package/src/engine/loop/fixed.js +28 -0
- package/src/engine/loop/flash.js +14 -0
- package/src/engine/loop/lag.js +27 -0
- package/src/engine/loop.js +15 -0
- package/src/engine/movement/dynamic/modern.js +24 -0
- package/src/engine/movement/dynamic/tank.js +43 -0
- package/src/engine/movement/kinematic/modern.js +16 -0
- package/src/engine/movement/kinematic/modern.test.js +27 -0
- package/src/engine/movement/kinematic/tank.js +27 -0
- package/src/engine/store.js +174 -0
- package/src/engine/store.test.js +256 -0
- package/src/engine.js +74 -0
- package/src/game/animation.js +26 -0
- package/src/game/bounds.js +66 -0
- package/src/game/decorators/character.js +5 -0
- package/src/game/decorators/clamp-to-bounds.js +15 -0
- package/src/game/decorators/collisions.js +24 -0
- package/src/game/decorators/controls/dynamic/modern.js +48 -0
- package/src/game/decorators/controls/dynamic/shooter.js +47 -0
- package/src/game/decorators/controls/dynamic/tank.js +55 -0
- package/src/game/decorators/controls/kinematic/modern.js +49 -0
- package/src/game/decorators/controls/kinematic/shooter.js +45 -0
- package/src/game/decorators/controls/kinematic/tank.js +52 -0
- package/src/game/decorators/debug/collisions.js +32 -0
- package/src/game/decorators/double-jump.js +70 -0
- package/src/game/decorators/fps.js +30 -0
- package/src/game/decorators/fsm.js +27 -0
- package/src/game/decorators/fsm.test.js +56 -0
- package/src/game/decorators/game.js +11 -0
- package/src/game/decorators/image/image.js +5 -0
- package/src/game/decorators/image/sprite.js +5 -0
- package/src/game/decorators/image/tilemap.js +5 -0
- package/src/game/decorators/input/controls.js +27 -0
- package/src/game/decorators/input/gamepad.js +74 -0
- package/src/game/decorators/input/input.js +41 -0
- package/src/game/decorators/input/keyboard.js +49 -0
- package/src/game/decorators/input/mouse.js +65 -0
- package/src/game/decorators/jump.js +72 -0
- package/src/game/decorators/platform.js +5 -0
- package/src/game/decorators/ui/button.js +21 -0
- package/src/game/sprite.js +119 -0
- package/src/main.js +5 -0
- package/src/ui/canvas/absolute-position.js +17 -0
- package/src/ui/canvas/character.js +35 -0
- package/src/ui/canvas/form/button.js +25 -0
- package/src/ui/canvas/fps.js +18 -0
- package/src/ui/canvas/image/hitmask.js +37 -0
- package/src/ui/canvas/image/image.js +37 -0
- package/src/ui/canvas/image/sprite.js +49 -0
- package/src/ui/canvas/image/tilemap.js +64 -0
- package/src/ui/canvas/mouse.js +37 -0
- package/src/ui/canvas/shapes/circle.js +31 -0
- package/src/ui/canvas/shapes/rectangle.js +31 -0
- package/src/ui/canvas.js +81 -0
- package/src/ui/react/game/character/character.module.scss +17 -0
- package/src/ui/react/game/character/index.jsx +30 -0
- package/src/ui/react/game/cursor/cursor.module.scss +47 -0
- package/src/ui/react/game/cursor/index.jsx +20 -0
- package/src/ui/react/game/form/fields/field/field.module.scss +5 -0
- package/src/ui/react/game/form/fields/field/index.jsx +56 -0
- package/src/ui/react/game/form/fields/fields.module.scss +48 -0
- package/src/ui/react/game/form/fields/index.jsx +12 -0
- package/src/ui/react/game/form/form.module.scss +18 -0
- package/src/ui/react/game/form/index.jsx +22 -0
- package/src/ui/react/game/fps/index.jsx +16 -0
- package/src/ui/react/game/game.jsx +71 -0
- package/src/ui/react/game/index.jsx +29 -0
- package/src/ui/react/game/platform/index.jsx +30 -0
- package/src/ui/react/game/platform/platform.module.scss +7 -0
- package/src/ui/react/game/scene/index.jsx +25 -0
- package/src/ui/react/game/scene/scene.module.scss +9 -0
- package/src/ui/react/game/sprite/index.jsx +58 -0
- package/src/ui/react/game/sprite/sprite.module.css +3 -0
- package/src/ui/react/game/stats/index.jsx +22 -0
- package/src/ui/react/hocs/with-absolute-position/index.jsx +20 -0
- package/src/ui/react/hocs/with-absolute-position/with-absolute-position.module.scss +5 -0
- package/src/ui/react/index.jsx +9 -0
- package/src/utils/algorithms/decision-tree.js +24 -0
- package/src/utils/algorithms/decision-tree.test.js +102 -0
- package/src/utils/algorithms/path-finding.js +155 -0
- package/src/utils/algorithms/path-finding.test.js +151 -0
- package/src/utils/algorithms/types.d.ts +28 -0
- package/src/utils/data-structures/array.js +83 -0
- package/src/utils/data-structures/array.test.js +173 -0
- package/src/utils/data-structures/board.js +159 -0
- package/src/utils/data-structures/board.test.js +242 -0
- package/src/utils/data-structures/boolean.js +9 -0
- package/src/utils/data-structures/heap.js +164 -0
- package/src/utils/data-structures/heap.test.js +103 -0
- package/src/utils/data-structures/object.js +102 -0
- package/src/utils/data-structures/object.test.js +121 -0
- package/src/utils/data-structures/objects.js +48 -0
- package/src/utils/data-structures/objects.test.js +99 -0
- package/src/utils/data-structures/tree.js +36 -0
- package/src/utils/data-structures/tree.test.js +33 -0
- package/src/utils/data-structures/types.d.ts +4 -0
- package/src/utils/functions/functions.js +19 -0
- package/src/utils/functions/functions.test.js +23 -0
- package/src/utils/math/geometry/circle.js +117 -0
- package/src/utils/math/geometry/circle.test.js +97 -0
- package/src/utils/math/geometry/hitmask.js +39 -0
- package/src/utils/math/geometry/hitmask.test.js +84 -0
- package/src/utils/math/geometry/line.js +35 -0
- package/src/utils/math/geometry/line.test.js +49 -0
- package/src/utils/math/geometry/platform.js +42 -0
- package/src/utils/math/geometry/platform.test.js +133 -0
- package/src/utils/math/geometry/point.js +71 -0
- package/src/utils/math/geometry/point.test.js +81 -0
- package/src/utils/math/geometry/rectangle.js +45 -0
- package/src/utils/math/geometry/rectangle.test.js +42 -0
- package/src/utils/math/geometry/segment.js +80 -0
- package/src/utils/math/geometry/segment.test.js +183 -0
- package/src/utils/math/geometry/triangle.js +15 -0
- package/src/utils/math/geometry/triangle.test.js +11 -0
- package/src/utils/math/geometry/types.d.ts +23 -0
- package/src/utils/math/linear-algebra/2d.js +28 -0
- package/src/utils/math/linear-algebra/2d.test.js +17 -0
- package/src/utils/math/linear-algebra/quaternion.js +22 -0
- package/src/utils/math/linear-algebra/quaternion.test.js +25 -0
- package/src/utils/math/linear-algebra/quaternions.js +20 -0
- package/src/utils/math/linear-algebra/quaternions.test.js +29 -0
- package/src/utils/math/linear-algebra/types.d.ts +4 -0
- package/src/utils/math/linear-algebra/vector.js +302 -0
- package/src/utils/math/linear-algebra/vector.test.js +257 -0
- package/src/utils/math/linear-algebra/vectors.js +122 -0
- package/src/utils/math/linear-algebra/vectors.test.js +65 -0
- package/src/utils/math/numbers.js +90 -0
- package/src/utils/math/numbers.test.js +137 -0
- package/src/utils/math/rng.js +44 -0
- package/src/utils/math/rng.test.js +39 -0
- package/src/utils/math/statistics.js +43 -0
- package/src/utils/math/statistics.test.js +47 -0
- package/src/utils/math/trigonometry.js +89 -0
- package/src/utils/math/trigonometry.test.js +52 -0
- package/src/utils/physics/acceleration.js +63 -0
- package/src/utils/physics/friction.js +30 -0
- package/src/utils/physics/friction.test.js +44 -0
- package/src/utils/physics/gravity.js +71 -0
- package/src/utils/physics/gravity.test.js +80 -0
- package/src/utils/physics/jump.js +41 -0
- package/src/utils/physics/velocity.js +38 -0
|
@@ -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,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,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
|
+
}
|