@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,36 @@
|
|
|
1
|
+
import flee from "@inglorious/engine/ai/movement/kinematic/flee.js"
|
|
2
|
+
import { clampToBounds } from "@inglorious/game/bounds.js"
|
|
3
|
+
import { enableCharacter } from "@inglorious/game/decorators/character.js"
|
|
4
|
+
import { enableMouse } from "@inglorious/game/decorators/input/mouse.js"
|
|
5
|
+
import { merge } from "@inglorious/utils/data-structures/objects.js"
|
|
6
|
+
|
|
7
|
+
export default {
|
|
8
|
+
types: {
|
|
9
|
+
mouse: [enableMouse()],
|
|
10
|
+
|
|
11
|
+
character: [
|
|
12
|
+
enableCharacter(),
|
|
13
|
+
{
|
|
14
|
+
"game:update"(instance, event, { dt, instances }) {
|
|
15
|
+
const target = instances.mouse
|
|
16
|
+
|
|
17
|
+
merge(instance, flee(instance, target, { dt }))
|
|
18
|
+
clampToBounds(instance, instances.game.bounds)
|
|
19
|
+
},
|
|
20
|
+
},
|
|
21
|
+
],
|
|
22
|
+
},
|
|
23
|
+
|
|
24
|
+
instances: {
|
|
25
|
+
mouse: {
|
|
26
|
+
type: "mouse",
|
|
27
|
+
position: [400, 0, 300],
|
|
28
|
+
},
|
|
29
|
+
|
|
30
|
+
character: {
|
|
31
|
+
type: "character",
|
|
32
|
+
maxSpeed: 250,
|
|
33
|
+
position: [400, 0, 300],
|
|
34
|
+
},
|
|
35
|
+
},
|
|
36
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { Canvas, Meta, Source } from '@storybook/blocks'
|
|
2
|
+
import * as Kinematic from './kinematic.stories'
|
|
3
|
+
|
|
4
|
+
import align from './align?raw'
|
|
5
|
+
import arrive from './arrive?raw'
|
|
6
|
+
import flee from './flee?raw'
|
|
7
|
+
import seek from './seek?raw'
|
|
8
|
+
import wander from './wander?raw'
|
|
9
|
+
import wanderAsSeek from './wander-as-seek?raw'
|
|
10
|
+
|
|
11
|
+
<Meta of={Kinematic} />
|
|
12
|
+
|
|
13
|
+
# Kinematic Movement
|
|
14
|
+
|
|
15
|
+
The algorithms described here make an NPC instance move by changing its velocity. The movement is a little snappy but it does the trick for simple games.
|
|
16
|
+
|
|
17
|
+
Every algorithm returns some parameters, such as velocity, position, and orientation, which can be merged to the instance.
|
|
18
|
+
|
|
19
|
+
Most examples here make use of user [input](/?path=/docs/games-input--docs), so check those out if you haven't already.
|
|
20
|
+
|
|
21
|
+
## Seek
|
|
22
|
+
|
|
23
|
+
The seek algorithm makes the instance chase a target. Speed is constant and it makes the character flick around the target once reached.
|
|
24
|
+
|
|
25
|
+
<Source dark code={seek} />
|
|
26
|
+
|
|
27
|
+
<Canvas of={Kinematic.Seek} />
|
|
28
|
+
|
|
29
|
+
## Flee
|
|
30
|
+
|
|
31
|
+
The flee algorithm is the exact opposite of seek: the instance will run away from the target.
|
|
32
|
+
|
|
33
|
+
<Source dark code={flee} />
|
|
34
|
+
|
|
35
|
+
<Canvas of={Kinematic.Flee} />
|
|
36
|
+
|
|
37
|
+
## Arrive
|
|
38
|
+
|
|
39
|
+
The arrive algorithm is a more refined version of seek: the instance will slow down as it approaches the target.
|
|
40
|
+
|
|
41
|
+
<Source dark code={arrive} />
|
|
42
|
+
|
|
43
|
+
<Canvas of={Kinematic.Arrive} />
|
|
44
|
+
|
|
45
|
+
## Align
|
|
46
|
+
|
|
47
|
+
The align algorithm makes the instance rotate to match the target's orientation.
|
|
48
|
+
|
|
49
|
+
<Source dark code={align} />
|
|
50
|
+
|
|
51
|
+
<Canvas of={Kinematic.Align} />
|
|
52
|
+
|
|
53
|
+
## Wander
|
|
54
|
+
|
|
55
|
+
The wander algorithm makes the instance move around randomly. It does so by changing its orientation by a small random amount at each frame.
|
|
56
|
+
|
|
57
|
+
<Source dark code={wander} />
|
|
58
|
+
|
|
59
|
+
<Canvas of={Kinematic.Wander} />
|
|
60
|
+
|
|
61
|
+
## Wander as Seek
|
|
62
|
+
|
|
63
|
+
This is a simpler version of the wander algorithm that does not require trigonometry. It just seeks a random point in the scene, and once it's reached, it seeks another one.
|
|
64
|
+
|
|
65
|
+
<Source dark code={wanderAsSeek} />
|
|
66
|
+
|
|
67
|
+
<Canvas of={Kinematic.WanderAsSeek} />
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import UiChooser from "@inglorious/docs/ui-chooser.jsx"
|
|
2
|
+
|
|
3
|
+
import align from "./align.js"
|
|
4
|
+
import arrive from "./arrive.js"
|
|
5
|
+
import face from "./face.js"
|
|
6
|
+
import flee from "./flee.js"
|
|
7
|
+
import seek from "./seek.js"
|
|
8
|
+
import wander from "./wander.js"
|
|
9
|
+
import wanderAsSeek from "./wander-as-seek.js"
|
|
10
|
+
|
|
11
|
+
export default {
|
|
12
|
+
title: "Engine/AI/Movement/Kinematic",
|
|
13
|
+
component: UiChooser,
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export const Seek = {
|
|
17
|
+
args: { config: seek },
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export const Flee = {
|
|
21
|
+
args: { config: flee },
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export const Arrive = {
|
|
25
|
+
args: { config: arrive },
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export const Align = {
|
|
29
|
+
args: { config: align },
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export const Face = {
|
|
33
|
+
args: { config: face },
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export const Wander = {
|
|
37
|
+
args: { config: wander },
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export const WanderAsSeek = {
|
|
41
|
+
args: { config: wanderAsSeek },
|
|
42
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import seek from "@inglorious/engine/ai/movement/kinematic/seek.js"
|
|
2
|
+
import { enableCharacter } from "@inglorious/game/decorators/character.js"
|
|
3
|
+
import { enableMouse } from "@inglorious/game/decorators/input/mouse.js"
|
|
4
|
+
import { merge } from "@inglorious/utils/data-structures/objects.js"
|
|
5
|
+
|
|
6
|
+
export default {
|
|
7
|
+
types: {
|
|
8
|
+
mouse: [enableMouse()],
|
|
9
|
+
|
|
10
|
+
character: [
|
|
11
|
+
enableCharacter(),
|
|
12
|
+
{
|
|
13
|
+
"game:update"(instance, event, { dt, instances }) {
|
|
14
|
+
const target = instances.mouse
|
|
15
|
+
|
|
16
|
+
merge(instance, seek(instance, target, { dt }))
|
|
17
|
+
},
|
|
18
|
+
},
|
|
19
|
+
],
|
|
20
|
+
},
|
|
21
|
+
|
|
22
|
+
instances: {
|
|
23
|
+
mouse: {
|
|
24
|
+
type: "mouse",
|
|
25
|
+
position: [400, 0, 300],
|
|
26
|
+
},
|
|
27
|
+
|
|
28
|
+
character: {
|
|
29
|
+
type: "character",
|
|
30
|
+
maxSpeed: 250,
|
|
31
|
+
position: [400, 0, 300],
|
|
32
|
+
},
|
|
33
|
+
},
|
|
34
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import wanderAsSeek, {
|
|
2
|
+
DEFAULT_WANDER_RADIUS,
|
|
3
|
+
} from "@inglorious/engine/ai/movement/kinematic/wander-as-seek.js"
|
|
4
|
+
import { flip } from "@inglorious/game/bounds.js"
|
|
5
|
+
import { enableCharacter } from "@inglorious/game/decorators/character.js"
|
|
6
|
+
import { merge } from "@inglorious/utils/data-structures/objects.js"
|
|
7
|
+
import { pi } from "@inglorious/utils/math/trigonometry.js"
|
|
8
|
+
|
|
9
|
+
export default {
|
|
10
|
+
types: {
|
|
11
|
+
character: [
|
|
12
|
+
enableCharacter(),
|
|
13
|
+
{
|
|
14
|
+
"game:update"(instance, event, { dt, instances }) {
|
|
15
|
+
const { fields } = instances.parameters.groups.wanderAsSeek
|
|
16
|
+
|
|
17
|
+
merge(
|
|
18
|
+
instance,
|
|
19
|
+
wanderAsSeek(instance, {
|
|
20
|
+
dt,
|
|
21
|
+
wanderRadius: fields.wanderRadius.value,
|
|
22
|
+
}),
|
|
23
|
+
)
|
|
24
|
+
flip(instance, instances.game.bounds)
|
|
25
|
+
},
|
|
26
|
+
},
|
|
27
|
+
],
|
|
28
|
+
|
|
29
|
+
form: {
|
|
30
|
+
"field:change"(instance, event) {
|
|
31
|
+
const { id, value } = event.payload
|
|
32
|
+
instance.groups.wanderAsSeek.fields[id].value = value
|
|
33
|
+
},
|
|
34
|
+
},
|
|
35
|
+
},
|
|
36
|
+
|
|
37
|
+
instances: {
|
|
38
|
+
character: {
|
|
39
|
+
type: "character",
|
|
40
|
+
maxSpeed: 250,
|
|
41
|
+
maxAngularSpeed: pi() / 4,
|
|
42
|
+
position: [400, 0, 300],
|
|
43
|
+
},
|
|
44
|
+
|
|
45
|
+
parameters: {
|
|
46
|
+
type: "form",
|
|
47
|
+
position: [800 - 352, 0, 600],
|
|
48
|
+
groups: {
|
|
49
|
+
wanderAsSeek: {
|
|
50
|
+
title: "Wander As Seek",
|
|
51
|
+
fields: {
|
|
52
|
+
wanderRadius: {
|
|
53
|
+
label: "Wander Radius",
|
|
54
|
+
inputType: "number",
|
|
55
|
+
defaultValue: DEFAULT_WANDER_RADIUS,
|
|
56
|
+
},
|
|
57
|
+
},
|
|
58
|
+
},
|
|
59
|
+
},
|
|
60
|
+
},
|
|
61
|
+
},
|
|
62
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import wander from "@inglorious/engine/ai/movement/kinematic/wander.js"
|
|
2
|
+
import { flip } from "@inglorious/game/bounds.js"
|
|
3
|
+
import { enableCharacter } from "@inglorious/game/decorators/character.js"
|
|
4
|
+
import { merge } from "@inglorious/utils/data-structures/objects.js"
|
|
5
|
+
import { pi } from "@inglorious/utils/math/trigonometry.js"
|
|
6
|
+
|
|
7
|
+
export default {
|
|
8
|
+
types: {
|
|
9
|
+
character: [
|
|
10
|
+
enableCharacter(),
|
|
11
|
+
{
|
|
12
|
+
"game:update"(instance, event, { dt, instances }) {
|
|
13
|
+
merge(instance, wander(instance, { dt }))
|
|
14
|
+
flip(instance, instances.game.bounds)
|
|
15
|
+
},
|
|
16
|
+
},
|
|
17
|
+
],
|
|
18
|
+
},
|
|
19
|
+
|
|
20
|
+
instances: {
|
|
21
|
+
character: {
|
|
22
|
+
type: "character",
|
|
23
|
+
maxSpeed: 250,
|
|
24
|
+
maxAngularSpeed: pi() / 4,
|
|
25
|
+
position: [400, 0, 300],
|
|
26
|
+
},
|
|
27
|
+
},
|
|
28
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { bounce } from "@inglorious/game/bounds.js"
|
|
2
|
+
import { enableCharacter } from "@inglorious/game/decorators/character.js"
|
|
3
|
+
import { enableFps } from "@inglorious/game/decorators/fps.js"
|
|
4
|
+
import { merge } from "@inglorious/utils/data-structures/objects.js"
|
|
5
|
+
import { zero } from "@inglorious/utils/math/linear-algebra/vector.js"
|
|
6
|
+
import { pi } from "@inglorious/utils/math/trigonometry.js"
|
|
7
|
+
|
|
8
|
+
export default {
|
|
9
|
+
types: {
|
|
10
|
+
character: [
|
|
11
|
+
enableCharacter(),
|
|
12
|
+
{
|
|
13
|
+
"game:update"(instance, event, options) {
|
|
14
|
+
merge(instance, bounce(instance, options))
|
|
15
|
+
},
|
|
16
|
+
},
|
|
17
|
+
],
|
|
18
|
+
|
|
19
|
+
fps: [enableFps()],
|
|
20
|
+
},
|
|
21
|
+
|
|
22
|
+
instances: {
|
|
23
|
+
character: {
|
|
24
|
+
type: "character",
|
|
25
|
+
maxSpeed: 250,
|
|
26
|
+
orientation: pi() / 6,
|
|
27
|
+
position: zero(),
|
|
28
|
+
},
|
|
29
|
+
|
|
30
|
+
fps: {
|
|
31
|
+
type: "fps",
|
|
32
|
+
position: [0, 0, 600],
|
|
33
|
+
},
|
|
34
|
+
},
|
|
35
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { collidesWith } from "@inglorious/engine/collision/detection.js"
|
|
2
|
+
import { bounce } from "@inglorious/game/bounds.js"
|
|
3
|
+
import { enableCharacter } from "@inglorious/game/decorators/character.js"
|
|
4
|
+
import { merge } from "@inglorious/utils/data-structures/objects.js"
|
|
5
|
+
import { mod } from "@inglorious/utils/math/numbers.js"
|
|
6
|
+
import { pi } from "@inglorious/utils/math/trigonometry.js"
|
|
7
|
+
|
|
8
|
+
export default {
|
|
9
|
+
types: {
|
|
10
|
+
character: [
|
|
11
|
+
enableCharacter(),
|
|
12
|
+
{
|
|
13
|
+
"game:update"(instance, event, options) {
|
|
14
|
+
const characters = Object.values(options.instances).filter(
|
|
15
|
+
({ type }) => type === "character",
|
|
16
|
+
)
|
|
17
|
+
const target = characters.find(({ id }) => id !== instance.id)
|
|
18
|
+
|
|
19
|
+
if (collidesWith(instance, target)) {
|
|
20
|
+
instance.orientation += pi()
|
|
21
|
+
instance.orientation = mod(instance.orientation, 2 * pi())
|
|
22
|
+
}
|
|
23
|
+
merge(instance, bounce(instance, options))
|
|
24
|
+
},
|
|
25
|
+
},
|
|
26
|
+
],
|
|
27
|
+
},
|
|
28
|
+
|
|
29
|
+
instances: {
|
|
30
|
+
left: {
|
|
31
|
+
type: "character",
|
|
32
|
+
position: [200, 0, 300],
|
|
33
|
+
orientation: 0,
|
|
34
|
+
maxSpeed: 250,
|
|
35
|
+
collisions: {
|
|
36
|
+
hitbox: {
|
|
37
|
+
shape: "circle",
|
|
38
|
+
offset: [-6, 0, -6],
|
|
39
|
+
radius: 12,
|
|
40
|
+
},
|
|
41
|
+
},
|
|
42
|
+
},
|
|
43
|
+
|
|
44
|
+
right: {
|
|
45
|
+
type: "character",
|
|
46
|
+
position: [600, 0, 300],
|
|
47
|
+
orientation: pi(),
|
|
48
|
+
maxSpeed: 250,
|
|
49
|
+
collisions: {
|
|
50
|
+
hitbox: {
|
|
51
|
+
shape: "circle",
|
|
52
|
+
offset: [-6, 0, -6],
|
|
53
|
+
radius: 12,
|
|
54
|
+
},
|
|
55
|
+
},
|
|
56
|
+
},
|
|
57
|
+
},
|
|
58
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { Canvas, Meta, Source } from '@storybook/blocks'
|
|
2
|
+
import * as Collision from './collision.stories'
|
|
3
|
+
|
|
4
|
+
import circles from './circles?raw'
|
|
5
|
+
import platform from './platform?raw'
|
|
6
|
+
|
|
7
|
+
<Meta of={Collision} />
|
|
8
|
+
|
|
9
|
+
# Collision Detection
|
|
10
|
+
|
|
11
|
+
Examples on how to use the built-in collision detection functions.
|
|
12
|
+
|
|
13
|
+
## Circles
|
|
14
|
+
|
|
15
|
+
Two shapes with a circular hitbox bounce against each other when colliding.
|
|
16
|
+
|
|
17
|
+
<Source dark code={circles} />
|
|
18
|
+
|
|
19
|
+
<Canvas of={Collision.Circles} />
|
|
20
|
+
|
|
21
|
+
## Platform
|
|
22
|
+
|
|
23
|
+
A character is able to jump on top of a platform and keep its `y` position.
|
|
24
|
+
|
|
25
|
+
<Source dark code={platform} />
|
|
26
|
+
|
|
27
|
+
<Canvas of={Collision.Platform} />
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import UiChooser from "@inglorious/docs/ui-chooser.jsx"
|
|
2
|
+
|
|
3
|
+
import circles from "./circles.js"
|
|
4
|
+
import platform from "./platform.js"
|
|
5
|
+
import tilemap from "./tilemap.js"
|
|
6
|
+
|
|
7
|
+
export default {
|
|
8
|
+
title: "Engine/Collision Detection",
|
|
9
|
+
component: UiChooser,
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export const Circles = {
|
|
13
|
+
args: { config: circles },
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export const Platform = {
|
|
17
|
+
args: { config: platform },
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export const Tilemap = {
|
|
21
|
+
args: { config: tilemap },
|
|
22
|
+
}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { enableCharacter } from "@inglorious/game/decorators/character.js"
|
|
2
|
+
import { enableClampToBounds } from "@inglorious/game/decorators/clamp-to-bounds.js"
|
|
3
|
+
import { enableModernControls } from "@inglorious/game/decorators/controls/kinematic/modern.js"
|
|
4
|
+
import {
|
|
5
|
+
createControls,
|
|
6
|
+
enableControls,
|
|
7
|
+
} from "@inglorious/game/decorators/input/controls.js"
|
|
8
|
+
import { enableJump } from "@inglorious/game/decorators/jump.js"
|
|
9
|
+
import { enablePlatform } from "@inglorious/game/decorators/platform.js"
|
|
10
|
+
|
|
11
|
+
export default {
|
|
12
|
+
types: {
|
|
13
|
+
...enableControls(),
|
|
14
|
+
|
|
15
|
+
platform: [enablePlatform()],
|
|
16
|
+
|
|
17
|
+
character: [
|
|
18
|
+
enableCharacter(),
|
|
19
|
+
enableModernControls(),
|
|
20
|
+
enableClampToBounds(),
|
|
21
|
+
enableJump(),
|
|
22
|
+
],
|
|
23
|
+
},
|
|
24
|
+
|
|
25
|
+
instances: {
|
|
26
|
+
...createControls("input0", {
|
|
27
|
+
ArrowLeft: "left",
|
|
28
|
+
ArrowRight: "right",
|
|
29
|
+
Space: "jump",
|
|
30
|
+
KeyA: "left",
|
|
31
|
+
KeyD: "right",
|
|
32
|
+
Btn0: "jump",
|
|
33
|
+
Btn14: "left",
|
|
34
|
+
Btn15: "right",
|
|
35
|
+
Axis0: "leftRight",
|
|
36
|
+
}),
|
|
37
|
+
|
|
38
|
+
ground: {
|
|
39
|
+
type: "platform",
|
|
40
|
+
position: [0, 50, 0],
|
|
41
|
+
size: [800, 50, 0],
|
|
42
|
+
backgroundColor: "green",
|
|
43
|
+
collisions: {
|
|
44
|
+
platform: {
|
|
45
|
+
shape: "platform",
|
|
46
|
+
},
|
|
47
|
+
},
|
|
48
|
+
},
|
|
49
|
+
|
|
50
|
+
platform: {
|
|
51
|
+
type: "platform",
|
|
52
|
+
position: [600, 100, 0],
|
|
53
|
+
size: [80, 20, 0],
|
|
54
|
+
backgroundColor: "grey",
|
|
55
|
+
collisions: {
|
|
56
|
+
platform: {
|
|
57
|
+
shape: "platform",
|
|
58
|
+
},
|
|
59
|
+
},
|
|
60
|
+
},
|
|
61
|
+
|
|
62
|
+
character: {
|
|
63
|
+
type: "character",
|
|
64
|
+
layer: 1,
|
|
65
|
+
position: [200, 62, 0],
|
|
66
|
+
collisions: {
|
|
67
|
+
platform: {
|
|
68
|
+
// shape: "rectangle",
|
|
69
|
+
// size: [24, 24, 24],
|
|
70
|
+
shape: "circle",
|
|
71
|
+
radius: 12,
|
|
72
|
+
},
|
|
73
|
+
},
|
|
74
|
+
},
|
|
75
|
+
},
|
|
76
|
+
}
|
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
import { findCollisions } from "@inglorious/engine/collision/detection.js"
|
|
2
|
+
import { enableCollisionsDebug } from "@inglorious/game/decorators/debug/collisions.js"
|
|
3
|
+
import { enableSprite } from "@inglorious/game/decorators/image/sprite.js"
|
|
4
|
+
import { enableTilemap } from "@inglorious/game/decorators/image/tilemap.js"
|
|
5
|
+
import {
|
|
6
|
+
createControls,
|
|
7
|
+
enableControls,
|
|
8
|
+
} from "@inglorious/game/decorators/input/controls.js"
|
|
9
|
+
import { Sprite } from "@inglorious/game/sprite.js"
|
|
10
|
+
import { extend } from "@inglorious/utils/data-structures/objects.js"
|
|
11
|
+
import { zero } from "@inglorious/utils/math/linear-algebra/vector.js"
|
|
12
|
+
|
|
13
|
+
const X = 0
|
|
14
|
+
const Z = 2
|
|
15
|
+
|
|
16
|
+
export default {
|
|
17
|
+
types: {
|
|
18
|
+
...enableControls(),
|
|
19
|
+
|
|
20
|
+
tilemap: [enableTilemap(), enableCollisionsDebug()],
|
|
21
|
+
|
|
22
|
+
player: [
|
|
23
|
+
enableSprite(),
|
|
24
|
+
enableCollisionsDebug(),
|
|
25
|
+
// enableModernControls(),
|
|
26
|
+
(type) =>
|
|
27
|
+
extend(type, {
|
|
28
|
+
"game:update"(instance, event, options) {
|
|
29
|
+
type["game:update"]?.(instance, event, options)
|
|
30
|
+
|
|
31
|
+
const { maxSpeed } = instance
|
|
32
|
+
const { dungeon } = options.instances
|
|
33
|
+
|
|
34
|
+
const spriteState = Sprite.move2(instance)
|
|
35
|
+
Sprite.play(spriteState, instance, options)
|
|
36
|
+
|
|
37
|
+
const { input0 } = options.instances
|
|
38
|
+
instance.velocity = zero()
|
|
39
|
+
|
|
40
|
+
if (input0.left) {
|
|
41
|
+
instance.velocity[X] = -maxSpeed
|
|
42
|
+
}
|
|
43
|
+
if (input0.right) {
|
|
44
|
+
instance.velocity[X] = maxSpeed
|
|
45
|
+
}
|
|
46
|
+
if (input0.leftRight != null) {
|
|
47
|
+
instance.velocity[X] += input0.leftRight * maxSpeed
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const oldX = instance.position[X]
|
|
51
|
+
instance.position[X] += instance.velocity[X] * options.dt
|
|
52
|
+
if (findCollisions(dungeon, instance)) {
|
|
53
|
+
instance.position[X] = oldX
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
if (input0.down) {
|
|
57
|
+
instance.velocity[Z] = -maxSpeed
|
|
58
|
+
}
|
|
59
|
+
if (input0.up) {
|
|
60
|
+
instance.velocity[Z] = maxSpeed
|
|
61
|
+
}
|
|
62
|
+
if (input0.upDown != null) {
|
|
63
|
+
instance.velocity[Z] += -input0.upDown * maxSpeed
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const oldZ = instance.position[Z]
|
|
67
|
+
instance.position[Z] += instance.velocity[Z] * options.dt
|
|
68
|
+
if (findCollisions(dungeon, instance)) {
|
|
69
|
+
instance.position[Z] = oldZ
|
|
70
|
+
}
|
|
71
|
+
},
|
|
72
|
+
}),
|
|
73
|
+
],
|
|
74
|
+
},
|
|
75
|
+
|
|
76
|
+
instances: {
|
|
77
|
+
game: {
|
|
78
|
+
pixelated: true,
|
|
79
|
+
},
|
|
80
|
+
|
|
81
|
+
...createControls("input0", {
|
|
82
|
+
ArrowLeft: "left",
|
|
83
|
+
ArrowRight: "right",
|
|
84
|
+
ArrowDown: "down",
|
|
85
|
+
ArrowUp: "up",
|
|
86
|
+
}),
|
|
87
|
+
|
|
88
|
+
dungeon: {
|
|
89
|
+
type: "tilemap",
|
|
90
|
+
position: [400 - (16 * 6 * 3) / 2, 0, 300 - (16 * 5 * 3) / 2],
|
|
91
|
+
tilemap: {
|
|
92
|
+
image: {
|
|
93
|
+
id: "dungeon",
|
|
94
|
+
src: "/tilemaps/dungeon.png",
|
|
95
|
+
imageSize: [160, 160],
|
|
96
|
+
tileSize: [16, 16],
|
|
97
|
+
columns: 6,
|
|
98
|
+
scale: 3,
|
|
99
|
+
},
|
|
100
|
+
layers: [
|
|
101
|
+
{
|
|
102
|
+
tiles: [
|
|
103
|
+
// first row
|
|
104
|
+
0, 1, 2, 3, 4, 5,
|
|
105
|
+
// second row
|
|
106
|
+
10, 11, 12, 13, 14, 15,
|
|
107
|
+
// third row
|
|
108
|
+
20, 21, 22, 23, 24, 25,
|
|
109
|
+
// fourth row
|
|
110
|
+
30, 31, 32, 33, 34, 35,
|
|
111
|
+
// fifth row
|
|
112
|
+
40, 41, 42, 43, 44, 45,
|
|
113
|
+
],
|
|
114
|
+
},
|
|
115
|
+
{
|
|
116
|
+
tiles: [
|
|
117
|
+
// first row
|
|
118
|
+
-1, -1, -1, -1, -1, -1,
|
|
119
|
+
// second row
|
|
120
|
+
-1, -1, 83, -1, -1, -1,
|
|
121
|
+
// third row
|
|
122
|
+
-1, -1, -1, 97, -1, -1,
|
|
123
|
+
// fourth row
|
|
124
|
+
-1, -1, -1, -1, -1, -1,
|
|
125
|
+
// fifth row
|
|
126
|
+
-1, -1, 66, 67, -1, -1,
|
|
127
|
+
],
|
|
128
|
+
},
|
|
129
|
+
],
|
|
130
|
+
},
|
|
131
|
+
collisions: {
|
|
132
|
+
hitbox: {
|
|
133
|
+
shape: "hitmask",
|
|
134
|
+
tileSize: [48, 48],
|
|
135
|
+
columns: 6,
|
|
136
|
+
heights: [
|
|
137
|
+
// first row
|
|
138
|
+
2, 2, 2, 2, 2, 2,
|
|
139
|
+
// second row
|
|
140
|
+
2, 0, 1, 0, 0, 2,
|
|
141
|
+
// third row
|
|
142
|
+
2, 0, 0, 1, 0, 2,
|
|
143
|
+
// fourth row
|
|
144
|
+
2, 0, 0, 0, 0, 2,
|
|
145
|
+
// fifth row
|
|
146
|
+
2, 2, 2, 2, 2, 2,
|
|
147
|
+
],
|
|
148
|
+
},
|
|
149
|
+
},
|
|
150
|
+
},
|
|
151
|
+
|
|
152
|
+
player: {
|
|
153
|
+
type: "player",
|
|
154
|
+
position: [400 - 16 * 3, 1, 300 - 16],
|
|
155
|
+
maxSpeed: 250,
|
|
156
|
+
sprite: {
|
|
157
|
+
image: {
|
|
158
|
+
id: "dungeon_character",
|
|
159
|
+
src: "/sprites/dungeon_character.png",
|
|
160
|
+
imageSize: [112, 64],
|
|
161
|
+
tileSize: [16, 16],
|
|
162
|
+
scale: 3,
|
|
163
|
+
},
|
|
164
|
+
speed: 0.2,
|
|
165
|
+
frames: {
|
|
166
|
+
right: [17],
|
|
167
|
+
left: [0x80000000 + 17],
|
|
168
|
+
},
|
|
169
|
+
},
|
|
170
|
+
collisions: {
|
|
171
|
+
hitbox: {
|
|
172
|
+
shape: "rectangle",
|
|
173
|
+
size: [48, 0, 48],
|
|
174
|
+
offset: [-48 / 2, 0, -48 / 2],
|
|
175
|
+
// size: [24, 0, 24],
|
|
176
|
+
// offset: [-8, 0, -8],
|
|
177
|
+
},
|
|
178
|
+
},
|
|
179
|
+
},
|
|
180
|
+
},
|
|
181
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default {}
|