@inglorious/engine 0.1.1 → 0.3.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/README.md +75 -72
- package/package.json +15 -37
- package/src/{engine/ai → ai}/movement/dynamic/align.js +9 -9
- package/src/{engine/ai → ai}/movement/dynamic/arrive.js +9 -10
- package/src/{engine/ai → ai}/movement/dynamic/evade.js +9 -9
- package/src/{engine/ai → ai}/movement/dynamic/face.js +5 -6
- package/src/{engine/ai/movement/dynamic/seek.js → ai/movement/dynamic/flee.js} +8 -7
- package/src/ai/movement/dynamic/look-where-youre-going.js +16 -0
- package/src/{engine/ai → ai}/movement/dynamic/match-velocity.js +9 -8
- package/src/{engine/ai → ai}/movement/dynamic/pursue.js +9 -9
- package/src/{engine/ai/movement/dynamic/flee.js → ai/movement/dynamic/seek.js} +7 -8
- package/src/{engine/ai → ai}/movement/dynamic/wander.js +9 -10
- package/src/{engine/ai → ai}/movement/kinematic/align.js +7 -7
- package/src/{engine/ai → ai}/movement/kinematic/arrive.js +8 -8
- package/src/{engine/ai → ai}/movement/kinematic/face.js +5 -6
- package/src/{engine/ai → ai}/movement/kinematic/flee.js +5 -5
- package/src/{engine/ai → ai}/movement/kinematic/seek.js +5 -5
- package/src/{engine/ai → ai}/movement/kinematic/seek.test.js +10 -10
- package/src/{engine/ai → ai}/movement/kinematic/wander-as-seek.js +9 -9
- package/src/{engine/ai → ai}/movement/kinematic/wander.js +5 -5
- package/src/animation/sprite.js +101 -0
- package/src/animation/ticker.js +38 -0
- package/src/behaviors/camera.js +68 -0
- package/src/behaviors/controls/dynamic/modern.js +76 -0
- package/src/behaviors/controls/dynamic/shooter.js +84 -0
- package/src/behaviors/controls/dynamic/tank.js +69 -0
- package/src/behaviors/controls/event-handlers.js +17 -0
- package/src/behaviors/controls/kinematic/modern.js +76 -0
- package/src/behaviors/controls/kinematic/shooter.js +82 -0
- package/src/behaviors/controls/kinematic/tank.js +67 -0
- package/src/behaviors/debug/collision.js +29 -0
- package/src/behaviors/fps.js +29 -0
- package/src/behaviors/fsm.js +33 -0
- package/src/{game/decorators → behaviors}/fsm.test.js +15 -22
- package/src/behaviors/game.js +15 -0
- package/src/behaviors/input/controls.js +37 -0
- package/src/behaviors/input/gamepad.js +114 -0
- package/src/behaviors/input/input.js +48 -0
- package/src/behaviors/input/keyboard.js +64 -0
- package/src/behaviors/input/mouse.js +91 -0
- package/src/behaviors/physics/bouncy.js +25 -0
- package/src/behaviors/physics/clamped.js +36 -0
- package/src/{game/decorators/collisions.js → behaviors/physics/collidable.js} +3 -7
- package/src/behaviors/physics/jumpable.js +145 -0
- package/src/behaviors/ui/button.js +17 -0
- package/src/collision/detection.js +110 -0
- package/src/core/api.js +34 -0
- package/src/core/dev-tools.js +135 -0
- package/src/core/engine.js +119 -0
- package/src/core/loop.js +15 -0
- package/src/{engine/loop → core/loops}/animation-frame.js +1 -2
- package/src/{engine/loop → core/loops}/elapsed.js +1 -2
- package/src/{engine/loop → core/loops}/fixed.js +1 -2
- package/src/{engine/loop → core/loops}/flash.js +1 -2
- package/src/{engine/loop → core/loops}/lag.js +1 -2
- package/src/core/select.js +26 -0
- package/src/core/store.js +178 -0
- package/src/core/store.test.js +110 -0
- package/src/main.js +7 -2
- package/src/{engine/movement → movement}/dynamic/modern.js +3 -6
- package/src/{engine/movement → movement}/dynamic/tank.js +9 -9
- package/src/{engine/movement → movement}/kinematic/modern.js +3 -3
- package/src/movement/kinematic/modern.test.js +27 -0
- package/src/{engine/movement → movement}/kinematic/tank.js +5 -5
- package/src/physics/bounds.js +138 -0
- package/src/physics/position.js +43 -0
- package/src/physics/position.test.js +80 -0
- package/src/systems/sprite-animation.js +27 -0
- package/src/engine/ai/movement/dynamic/look-where-youre-going.js +0 -17
- package/src/engine/collision/detection.js +0 -115
- package/src/engine/loop.js +0 -15
- package/src/engine/movement/kinematic/modern.test.js +0 -27
- package/src/engine/store.js +0 -174
- package/src/engine/store.test.js +0 -256
- package/src/engine.js +0 -74
- package/src/game/animation.js +0 -26
- package/src/game/bounds.js +0 -66
- package/src/game/decorators/character.js +0 -5
- package/src/game/decorators/clamp-to-bounds.js +0 -15
- package/src/game/decorators/controls/dynamic/modern.js +0 -48
- package/src/game/decorators/controls/dynamic/shooter.js +0 -47
- package/src/game/decorators/controls/dynamic/tank.js +0 -55
- package/src/game/decorators/controls/kinematic/modern.js +0 -49
- package/src/game/decorators/controls/kinematic/shooter.js +0 -45
- package/src/game/decorators/controls/kinematic/tank.js +0 -52
- package/src/game/decorators/debug/collisions.js +0 -32
- package/src/game/decorators/double-jump.js +0 -70
- package/src/game/decorators/fps.js +0 -30
- package/src/game/decorators/fsm.js +0 -27
- package/src/game/decorators/game.js +0 -11
- package/src/game/decorators/image/image.js +0 -5
- package/src/game/decorators/image/sprite.js +0 -5
- package/src/game/decorators/image/tilemap.js +0 -5
- package/src/game/decorators/input/controls.js +0 -27
- package/src/game/decorators/input/gamepad.js +0 -74
- package/src/game/decorators/input/input.js +0 -41
- package/src/game/decorators/input/keyboard.js +0 -49
- package/src/game/decorators/input/mouse.js +0 -65
- package/src/game/decorators/jump.js +0 -72
- package/src/game/decorators/platform.js +0 -5
- package/src/game/decorators/ui/button.js +0 -21
- package/src/game/sprite.js +0 -119
- package/src/ui/canvas/absolute-position.js +0 -17
- package/src/ui/canvas/character.js +0 -35
- package/src/ui/canvas/form/button.js +0 -25
- package/src/ui/canvas/fps.js +0 -18
- package/src/ui/canvas/image/hitmask.js +0 -37
- package/src/ui/canvas/image/image.js +0 -37
- package/src/ui/canvas/image/sprite.js +0 -49
- package/src/ui/canvas/image/tilemap.js +0 -64
- package/src/ui/canvas/mouse.js +0 -37
- package/src/ui/canvas/shapes/circle.js +0 -31
- package/src/ui/canvas/shapes/rectangle.js +0 -31
- package/src/ui/canvas.js +0 -81
- package/src/ui/react/game/character/character.module.scss +0 -17
- package/src/ui/react/game/character/index.jsx +0 -30
- package/src/ui/react/game/cursor/cursor.module.scss +0 -47
- package/src/ui/react/game/cursor/index.jsx +0 -20
- package/src/ui/react/game/form/fields/field/field.module.scss +0 -5
- package/src/ui/react/game/form/fields/field/index.jsx +0 -56
- package/src/ui/react/game/form/fields/fields.module.scss +0 -48
- package/src/ui/react/game/form/fields/index.jsx +0 -12
- package/src/ui/react/game/form/form.module.scss +0 -18
- package/src/ui/react/game/form/index.jsx +0 -22
- package/src/ui/react/game/fps/index.jsx +0 -16
- package/src/ui/react/game/game.jsx +0 -71
- package/src/ui/react/game/index.jsx +0 -29
- package/src/ui/react/game/platform/index.jsx +0 -30
- package/src/ui/react/game/platform/platform.module.scss +0 -7
- package/src/ui/react/game/scene/index.jsx +0 -25
- package/src/ui/react/game/scene/scene.module.scss +0 -9
- package/src/ui/react/game/sprite/index.jsx +0 -58
- package/src/ui/react/game/sprite/sprite.module.css +0 -3
- package/src/ui/react/game/stats/index.jsx +0 -22
- package/src/ui/react/hocs/with-absolute-position/index.jsx +0 -20
- package/src/ui/react/hocs/with-absolute-position/with-absolute-position.module.scss +0 -5
- package/src/ui/react/index.jsx +0 -9
- package/src/utils/algorithms/decision-tree.js +0 -24
- package/src/utils/algorithms/decision-tree.test.js +0 -102
- package/src/utils/algorithms/path-finding.js +0 -155
- package/src/utils/algorithms/path-finding.test.js +0 -151
- package/src/utils/algorithms/types.d.ts +0 -28
- package/src/utils/data-structures/array.js +0 -83
- package/src/utils/data-structures/array.test.js +0 -173
- package/src/utils/data-structures/board.js +0 -159
- package/src/utils/data-structures/board.test.js +0 -242
- package/src/utils/data-structures/boolean.js +0 -9
- package/src/utils/data-structures/heap.js +0 -164
- package/src/utils/data-structures/heap.test.js +0 -103
- package/src/utils/data-structures/object.js +0 -102
- package/src/utils/data-structures/object.test.js +0 -121
- package/src/utils/data-structures/objects.js +0 -48
- package/src/utils/data-structures/objects.test.js +0 -99
- package/src/utils/data-structures/tree.js +0 -36
- package/src/utils/data-structures/tree.test.js +0 -33
- package/src/utils/data-structures/types.d.ts +0 -4
- package/src/utils/functions/functions.js +0 -19
- package/src/utils/functions/functions.test.js +0 -23
- package/src/utils/math/geometry/circle.js +0 -117
- package/src/utils/math/geometry/circle.test.js +0 -97
- package/src/utils/math/geometry/hitmask.js +0 -39
- package/src/utils/math/geometry/hitmask.test.js +0 -84
- package/src/utils/math/geometry/line.js +0 -35
- package/src/utils/math/geometry/line.test.js +0 -49
- package/src/utils/math/geometry/platform.js +0 -42
- package/src/utils/math/geometry/platform.test.js +0 -133
- package/src/utils/math/geometry/point.js +0 -71
- package/src/utils/math/geometry/point.test.js +0 -81
- package/src/utils/math/geometry/rectangle.js +0 -45
- package/src/utils/math/geometry/rectangle.test.js +0 -42
- package/src/utils/math/geometry/segment.js +0 -80
- package/src/utils/math/geometry/segment.test.js +0 -183
- package/src/utils/math/geometry/triangle.js +0 -15
- package/src/utils/math/geometry/triangle.test.js +0 -11
- package/src/utils/math/geometry/types.d.ts +0 -23
- package/src/utils/math/linear-algebra/2d.js +0 -28
- package/src/utils/math/linear-algebra/2d.test.js +0 -17
- package/src/utils/math/linear-algebra/quaternion.js +0 -22
- package/src/utils/math/linear-algebra/quaternion.test.js +0 -25
- package/src/utils/math/linear-algebra/quaternions.js +0 -20
- package/src/utils/math/linear-algebra/quaternions.test.js +0 -29
- package/src/utils/math/linear-algebra/types.d.ts +0 -4
- package/src/utils/math/linear-algebra/vector.js +0 -302
- package/src/utils/math/linear-algebra/vector.test.js +0 -257
- package/src/utils/math/linear-algebra/vectors.js +0 -122
- package/src/utils/math/linear-algebra/vectors.test.js +0 -65
- package/src/utils/math/numbers.js +0 -90
- package/src/utils/math/numbers.test.js +0 -137
- package/src/utils/math/rng.js +0 -44
- package/src/utils/math/rng.test.js +0 -39
- package/src/utils/math/statistics.js +0 -43
- package/src/utils/math/statistics.test.js +0 -47
- package/src/utils/math/trigonometry.js +0 -89
- package/src/utils/math/trigonometry.test.js +0 -52
- package/src/utils/physics/acceleration.js +0 -63
- package/src/utils/physics/friction.js +0 -30
- package/src/utils/physics/friction.test.js +0 -44
- package/src/utils/physics/gravity.js +0 -71
- package/src/utils/physics/gravity.test.js +0 -80
- package/src/utils/physics/jump.js +0 -41
- package/src/utils/physics/velocity.js +0 -38
package/src/game/sprite.js
DELETED
|
@@ -1,119 +0,0 @@
|
|
|
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
|
-
}
|
|
@@ -1,17 +0,0 @@
|
|
|
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
|
-
}
|
|
@@ -1,35 +0,0 @@
|
|
|
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
|
-
}
|
|
@@ -1,25 +0,0 @@
|
|
|
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
|
-
}
|
package/src/ui/canvas/fps.js
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
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
|
-
}
|
|
@@ -1,37 +0,0 @@
|
|
|
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
|
-
}
|
|
@@ -1,37 +0,0 @@
|
|
|
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
|
-
}
|
|
@@ -1,49 +0,0 @@
|
|
|
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
|
-
}
|
|
@@ -1,64 +0,0 @@
|
|
|
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
|
-
}
|
package/src/ui/canvas/mouse.js
DELETED
|
@@ -1,37 +0,0 @@
|
|
|
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
|
-
}
|
|
@@ -1,31 +0,0 @@
|
|
|
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
|
-
}
|
|
@@ -1,31 +0,0 @@
|
|
|
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
|
-
}
|
package/src/ui/canvas.js
DELETED
|
@@ -1,81 +0,0 @@
|
|
|
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
|
-
}
|
|
@@ -1,17 +0,0 @@
|
|
|
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
|
-
}
|
|
@@ -1,30 +0,0 @@
|
|
|
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
|
-
}
|
|
@@ -1,47 +0,0 @@
|
|
|
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
|
-
}
|