@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
|
@@ -1,102 +0,0 @@
|
|
|
1
|
-
const INITIAL_LEVEL = 0
|
|
2
|
-
const NEXT_LEVEL = 2
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Creates a deep clone of the given object.
|
|
6
|
-
* @param {Object} obj - The object to clone.
|
|
7
|
-
* @returns {Object} A deep clone of the input object.
|
|
8
|
-
*/
|
|
9
|
-
export function clone(obj) {
|
|
10
|
-
return JSON.parse(JSON.stringify(obj))
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* Filters the properties of an object based on a callback function.
|
|
15
|
-
* @param {Object} obj - The object to filter.
|
|
16
|
-
* @param {Function} callback - A function that determines whether a property should be included.
|
|
17
|
-
* Receives (key, value, obj) as arguments.
|
|
18
|
-
* @returns {Object} A new object with the filtered properties.
|
|
19
|
-
*/
|
|
20
|
-
export function filter(obj, callback) {
|
|
21
|
-
return Object.fromEntries(
|
|
22
|
-
Object.entries(obj).filter(([key, value], obj) =>
|
|
23
|
-
callback(key, value, obj),
|
|
24
|
-
),
|
|
25
|
-
)
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* Finds the first property in an object that satisfies the callback function.
|
|
30
|
-
* @param {Object} obj - The object to search.
|
|
31
|
-
* @param {Function} callback - A function that determines whether a property matches.
|
|
32
|
-
* Receives (key, value, obj) as arguments.
|
|
33
|
-
* @returns {Object} An object containing the first matching property, or an empty object if none match.
|
|
34
|
-
*/
|
|
35
|
-
export function find(obj, callback) {
|
|
36
|
-
return Object.fromEntries([
|
|
37
|
-
Object.entries(obj).find(([key, value], obj) => callback(key, value, obj)),
|
|
38
|
-
])
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* Checks if a value is a plain object.
|
|
43
|
-
* @param {*} obj - The value to check.
|
|
44
|
-
* @returns {boolean} True if the value is a plain object, false otherwise.
|
|
45
|
-
*/
|
|
46
|
-
export function isObject(obj) {
|
|
47
|
-
return obj != null && obj.constructor === Object
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
/**
|
|
51
|
-
* Maps the properties of an object using a callback function.
|
|
52
|
-
* @param {Object} obj - The object to map.
|
|
53
|
-
* @param {Function} callback - A function that transforms each property.
|
|
54
|
-
* Receives (key, value, obj) as arguments.
|
|
55
|
-
* @returns {Object} A new object with the mapped properties.
|
|
56
|
-
*/
|
|
57
|
-
export function map(obj, callback) {
|
|
58
|
-
return Object.entries(obj).reduce((acc, [key, value]) => {
|
|
59
|
-
acc[key] = callback(key, value, obj)
|
|
60
|
-
return acc
|
|
61
|
-
}, {})
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
/**
|
|
65
|
-
* Converts an object or array to a formatted string representation.
|
|
66
|
-
* @param {*} obj - The object or array to convert.
|
|
67
|
-
* @param {number} [indentationLevel=INITIAL_LEVEL] - The current indentation level (used for nested structures).
|
|
68
|
-
* @returns {string} A string representation of the input object or array.
|
|
69
|
-
*/
|
|
70
|
-
export function toString(obj, indentationLevel = INITIAL_LEVEL) {
|
|
71
|
-
if (Array.isArray(obj)) {
|
|
72
|
-
return `[
|
|
73
|
-
${obj
|
|
74
|
-
.map(
|
|
75
|
-
(item) =>
|
|
76
|
-
" ".repeat(indentationLevel + NEXT_LEVEL) +
|
|
77
|
-
toString(item, indentationLevel + NEXT_LEVEL),
|
|
78
|
-
)
|
|
79
|
-
.join(",\n")}
|
|
80
|
-
${" ".repeat(indentationLevel)}]`
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
if (typeof obj === "object" && obj != null) {
|
|
84
|
-
return `{
|
|
85
|
-
${Object.entries(obj)
|
|
86
|
-
.map(
|
|
87
|
-
([key, value]) =>
|
|
88
|
-
`${" ".repeat(indentationLevel + NEXT_LEVEL)}${key}: ${toString(
|
|
89
|
-
value,
|
|
90
|
-
indentationLevel + NEXT_LEVEL,
|
|
91
|
-
)}`,
|
|
92
|
-
)
|
|
93
|
-
.join(",\n")}
|
|
94
|
-
${" ".repeat(indentationLevel)}}`
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
if (typeof obj === "string") {
|
|
98
|
-
return `"${obj}"`
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
return obj
|
|
102
|
-
}
|
|
@@ -1,121 +0,0 @@
|
|
|
1
|
-
import { expect, test } from "vitest"
|
|
2
|
-
|
|
3
|
-
import { clone, filter, find, isObject, map, toString } from "./object.js"
|
|
4
|
-
|
|
5
|
-
test("it should deep clone an object", () => {
|
|
6
|
-
const obj = {
|
|
7
|
-
primitive: 1,
|
|
8
|
-
array: [2, 3],
|
|
9
|
-
object: { a: 1, b: 2 },
|
|
10
|
-
}
|
|
11
|
-
const expectedResult = {
|
|
12
|
-
primitive: 1,
|
|
13
|
-
array: [2, 3],
|
|
14
|
-
object: { a: 1, b: 2 },
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
const result = clone(obj)
|
|
18
|
-
|
|
19
|
-
expect(result).toStrictEqual(expectedResult)
|
|
20
|
-
expect(result).not.toBe(expectedResult)
|
|
21
|
-
expect(result.primitive).toBe(expectedResult.primitive)
|
|
22
|
-
expect(result.array).not.toBe(expectedResult.array)
|
|
23
|
-
expect(result.object).not.toBe(expectedResult.object)
|
|
24
|
-
})
|
|
25
|
-
|
|
26
|
-
test("it should behave like Array.prototype.filter, but on an object", () => {
|
|
27
|
-
const obj = {
|
|
28
|
-
key1: "value1",
|
|
29
|
-
key2: "value2",
|
|
30
|
-
key3: "value3",
|
|
31
|
-
}
|
|
32
|
-
const callback = (key) => ["key2", "key3"].includes(key)
|
|
33
|
-
const expectedResult = {
|
|
34
|
-
key2: "value2",
|
|
35
|
-
key3: "value3",
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
expect(filter(obj, callback)).toStrictEqual(expectedResult)
|
|
39
|
-
})
|
|
40
|
-
|
|
41
|
-
test("it should behave like Array.prototype.find, but on an object", () => {
|
|
42
|
-
const obj = {
|
|
43
|
-
key1: "value1",
|
|
44
|
-
key2: "value2",
|
|
45
|
-
key3: "value3",
|
|
46
|
-
}
|
|
47
|
-
const callback = (key) => ["key2", "key3"].includes(key)
|
|
48
|
-
const expectedResult = {
|
|
49
|
-
key2: "value2",
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
expect(find(obj, callback)).toStrictEqual(expectedResult)
|
|
53
|
-
})
|
|
54
|
-
|
|
55
|
-
test("it correctly should check if a value is an object", () => {
|
|
56
|
-
expect(isObject(1)).toBe(false)
|
|
57
|
-
expect(isObject("a")).toBe(false)
|
|
58
|
-
expect(isObject([])).toBe(false)
|
|
59
|
-
expect(isObject(null)).toBe(false)
|
|
60
|
-
expect(isObject(new Date())).toBe(false)
|
|
61
|
-
expect(isObject({})).toBe(true)
|
|
62
|
-
})
|
|
63
|
-
|
|
64
|
-
test("it should behave like Array.prototype.map, but on an object", () => {
|
|
65
|
-
const obj = {
|
|
66
|
-
key1: "value1",
|
|
67
|
-
key2: "value2",
|
|
68
|
-
key3: "value3",
|
|
69
|
-
}
|
|
70
|
-
const callback = (key, value) => value.toUpperCase()
|
|
71
|
-
const expectedResult = {
|
|
72
|
-
key1: "VALUE1",
|
|
73
|
-
key2: "VALUE2",
|
|
74
|
-
key3: "VALUE3",
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
expect(map(obj, callback)).toStrictEqual(expectedResult)
|
|
78
|
-
})
|
|
79
|
-
|
|
80
|
-
test("it should return a string representation of a shallow object", () => {
|
|
81
|
-
const obj = {
|
|
82
|
-
key1: "value1",
|
|
83
|
-
key2: "value2",
|
|
84
|
-
key3: "value3",
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
expect(toString(obj)).toBe(`{
|
|
88
|
-
key1: "value1",
|
|
89
|
-
key2: "value2",
|
|
90
|
-
key3: "value3"
|
|
91
|
-
}`)
|
|
92
|
-
})
|
|
93
|
-
|
|
94
|
-
test("it should return a string representation of a nested object", () => {
|
|
95
|
-
const obj = {
|
|
96
|
-
a: 1,
|
|
97
|
-
b: [7, 3],
|
|
98
|
-
c: { d: 4, h: 8 },
|
|
99
|
-
e: [{ f: 5, i: 9 }],
|
|
100
|
-
g: 6,
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
expect(toString(obj)).toBe(`{
|
|
104
|
-
a: 1,
|
|
105
|
-
b: [
|
|
106
|
-
7,
|
|
107
|
-
3
|
|
108
|
-
],
|
|
109
|
-
c: {
|
|
110
|
-
d: 4,
|
|
111
|
-
h: 8
|
|
112
|
-
},
|
|
113
|
-
e: [
|
|
114
|
-
{
|
|
115
|
-
f: 5,
|
|
116
|
-
i: 9
|
|
117
|
-
}
|
|
118
|
-
],
|
|
119
|
-
g: 6
|
|
120
|
-
}`)
|
|
121
|
-
})
|
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
import { isObject } from "./object.js"
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Extends a destination object by merging it with one or more source objects.
|
|
5
|
-
* Performs a deep merge for nested objects and arrays.
|
|
6
|
-
*
|
|
7
|
-
* @param {Object} dest - The destination object to extend.
|
|
8
|
-
* @param {...Object} sources - The source objects to merge into the destination object.
|
|
9
|
-
* @returns {Object} - The extended destination object.
|
|
10
|
-
*/
|
|
11
|
-
export function extend(dest, ...sources) {
|
|
12
|
-
return merge({}, dest, ...sources)
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* Merges multiple source objects into a destination object.
|
|
17
|
-
* Performs a deep merge for nested objects and arrays.
|
|
18
|
-
*
|
|
19
|
-
* @param {Object} dest - The destination object to merge into.
|
|
20
|
-
* @param {...Object} sources - The source objects to merge from.
|
|
21
|
-
* @returns {Object} - The merged destination object.
|
|
22
|
-
*/
|
|
23
|
-
export function merge(dest, ...sources) {
|
|
24
|
-
return sources
|
|
25
|
-
.filter((source) => source != null)
|
|
26
|
-
.reduce((acc, source) => deepMerge(acc, source), dest)
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* Recursively merges the properties of the source object into the destination object.
|
|
31
|
-
*
|
|
32
|
-
* @param {Object} dest - The destination object to merge into.
|
|
33
|
-
* @param {Object} source - The source object to merge from.
|
|
34
|
-
* @returns {Object} - The merged destination object.
|
|
35
|
-
*/
|
|
36
|
-
function deepMerge(dest, source) {
|
|
37
|
-
for (const [key, value] of Object.entries(source)) {
|
|
38
|
-
if (Array.isArray(value) || isObject(value)) {
|
|
39
|
-
if (dest[key] === undefined) {
|
|
40
|
-
dest[key] = new value.__proto__.constructor()
|
|
41
|
-
}
|
|
42
|
-
dest[key] = deepMerge(dest[key], value)
|
|
43
|
-
} else {
|
|
44
|
-
dest[key] = value
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
return dest
|
|
48
|
-
}
|
|
@@ -1,99 +0,0 @@
|
|
|
1
|
-
import { expect, test } from "vitest"
|
|
2
|
-
|
|
3
|
-
import { extend, merge } from "./objects.js"
|
|
4
|
-
|
|
5
|
-
test("it should extend an object with another, producing a new object as a result", () => {
|
|
6
|
-
const obj1 = {
|
|
7
|
-
primitiveKept: 1,
|
|
8
|
-
primitiveMerged: 2,
|
|
9
|
-
primitiveArrayKept: [1, 2],
|
|
10
|
-
primitiveArrayMerged: [3, 4],
|
|
11
|
-
primitiveObjectKept: { a: 1 },
|
|
12
|
-
primitiveObjectMerged: { b: 2, c: 3 },
|
|
13
|
-
nestedArrayKept: [{ a: 1 }],
|
|
14
|
-
nestedArrayMerged: [{ b: 2, c: 3 }],
|
|
15
|
-
nestedObjectKept: { a: { b: 2 } },
|
|
16
|
-
nestedObjectMerged: { c: { d: 4 } },
|
|
17
|
-
}
|
|
18
|
-
const obj2 = {
|
|
19
|
-
primitiveMerged: 3,
|
|
20
|
-
primitiveAdded: 4,
|
|
21
|
-
primitiveArrayMerged: [5],
|
|
22
|
-
primitiveArrayAdded: [6, 7],
|
|
23
|
-
primitiveObjectMerged: { c: 4 },
|
|
24
|
-
primitiveObjectAdded: { d: 4 },
|
|
25
|
-
nestedArrayMerged: [{ d: 4 }],
|
|
26
|
-
nestedArrayAdded: [{ e: 5 }],
|
|
27
|
-
nestedObjectMerged: { c: { e: 5 } },
|
|
28
|
-
nestedObjectAdded: { f: { g: 7 } },
|
|
29
|
-
}
|
|
30
|
-
const expectedResult = {
|
|
31
|
-
primitiveKept: 1,
|
|
32
|
-
primitiveMerged: 3,
|
|
33
|
-
primitiveAdded: 4,
|
|
34
|
-
primitiveArrayKept: [1, 2],
|
|
35
|
-
primitiveArrayMerged: [5, 4],
|
|
36
|
-
primitiveArrayAdded: [6, 7],
|
|
37
|
-
primitiveObjectKept: { a: 1 },
|
|
38
|
-
primitiveObjectMerged: { b: 2, c: 4 },
|
|
39
|
-
primitiveObjectAdded: { d: 4 },
|
|
40
|
-
nestedArrayKept: [{ a: 1 }],
|
|
41
|
-
nestedArrayMerged: [{ b: 2, c: 3, d: 4 }],
|
|
42
|
-
nestedArrayAdded: [{ e: 5 }],
|
|
43
|
-
nestedObjectKept: { a: { b: 2 } },
|
|
44
|
-
nestedObjectMerged: { c: { d: 4, e: 5 } },
|
|
45
|
-
nestedObjectAdded: { f: { g: 7 } },
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
const result = extend(obj1, obj2)
|
|
49
|
-
expect(result).toStrictEqual(expectedResult)
|
|
50
|
-
expect(result).not.toBe(obj1)
|
|
51
|
-
})
|
|
52
|
-
|
|
53
|
-
test("it should deep merge an two objects, changing the first object in place", () => {
|
|
54
|
-
const obj1 = {
|
|
55
|
-
primitiveKept: 1,
|
|
56
|
-
primitiveMerged: 2,
|
|
57
|
-
primitiveArrayKept: [1, 2],
|
|
58
|
-
primitiveArrayMerged: [3, 4],
|
|
59
|
-
primitiveObjectKept: { a: 1 },
|
|
60
|
-
primitiveObjectMerged: { b: 2, c: 3 },
|
|
61
|
-
nestedArrayKept: [{ a: 1 }],
|
|
62
|
-
nestedArrayMerged: [{ b: 2, c: 3 }],
|
|
63
|
-
nestedObjectKept: { a: { b: 2 } },
|
|
64
|
-
nestedObjectMerged: { c: { d: 4 } },
|
|
65
|
-
}
|
|
66
|
-
const obj2 = {
|
|
67
|
-
primitiveMerged: 3,
|
|
68
|
-
primitiveAdded: 4,
|
|
69
|
-
primitiveArrayMerged: [5],
|
|
70
|
-
primitiveArrayAdded: [6, 7],
|
|
71
|
-
primitiveObjectMerged: { c: 4 },
|
|
72
|
-
primitiveObjectAdded: { d: 4 },
|
|
73
|
-
nestedArrayMerged: [{ d: 4 }],
|
|
74
|
-
nestedArrayAdded: [{ e: 5 }],
|
|
75
|
-
nestedObjectMerged: { c: { e: 5 } },
|
|
76
|
-
nestedObjectAdded: { f: { g: 7 } },
|
|
77
|
-
}
|
|
78
|
-
const expectedResult = {
|
|
79
|
-
primitiveKept: 1,
|
|
80
|
-
primitiveMerged: 3,
|
|
81
|
-
primitiveAdded: 4,
|
|
82
|
-
primitiveArrayKept: [1, 2],
|
|
83
|
-
primitiveArrayMerged: [5, 4],
|
|
84
|
-
primitiveArrayAdded: [6, 7],
|
|
85
|
-
primitiveObjectKept: { a: 1 },
|
|
86
|
-
primitiveObjectMerged: { b: 2, c: 4 },
|
|
87
|
-
primitiveObjectAdded: { d: 4 },
|
|
88
|
-
nestedArrayKept: [{ a: 1 }],
|
|
89
|
-
nestedArrayMerged: [{ b: 2, c: 3, d: 4 }],
|
|
90
|
-
nestedArrayAdded: [{ e: 5 }],
|
|
91
|
-
nestedObjectKept: { a: { b: 2 } },
|
|
92
|
-
nestedObjectMerged: { c: { d: 4, e: 5 } },
|
|
93
|
-
nestedObjectAdded: { f: { g: 7 } },
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
const result = merge(obj1, obj2)
|
|
97
|
-
expect(result).toStrictEqual(expectedResult)
|
|
98
|
-
expect(result).toBe(obj1)
|
|
99
|
-
})
|
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
/** @typedef {import('./types').Tree} Tree */
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Performs a breadth-first search (BFS) traversal on a tree structure.
|
|
5
|
-
* @param {Tree} tree - The root node of the tree.
|
|
6
|
-
* @returns {Array} An array of values in BFS order.
|
|
7
|
-
*/
|
|
8
|
-
export function bfs(tree) {
|
|
9
|
-
const result = []
|
|
10
|
-
const queue = [tree]
|
|
11
|
-
|
|
12
|
-
while (queue.length) {
|
|
13
|
-
const node = queue.shift()
|
|
14
|
-
result.push(node.value)
|
|
15
|
-
if (node.children) {
|
|
16
|
-
queue.push(...node.children)
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
return result
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* Performs a depth-first search (DFS) traversal on a tree structure.
|
|
25
|
-
* @param {Tree} tree - The root node of the tree.
|
|
26
|
-
* @returns {Array} An array of values in DFS order.
|
|
27
|
-
*/
|
|
28
|
-
export function dfs(tree) {
|
|
29
|
-
const result = [tree.value]
|
|
30
|
-
|
|
31
|
-
if (tree.children) {
|
|
32
|
-
result.push(...tree.children.flatMap(dfs))
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
return result
|
|
36
|
-
}
|
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
import { expect, test } from "vitest"
|
|
2
|
-
|
|
3
|
-
import { bfs, dfs } from "./tree.js"
|
|
4
|
-
|
|
5
|
-
test("it should perform Breadth-First Search on a tree", () => {
|
|
6
|
-
const tree = {
|
|
7
|
-
value: 1,
|
|
8
|
-
children: [
|
|
9
|
-
{ value: 2, children: [{ value: 4 }, { value: 5 }] },
|
|
10
|
-
{ value: 3, children: [{ value: 6 }, { value: 7 }] },
|
|
11
|
-
],
|
|
12
|
-
}
|
|
13
|
-
const expectedResult = [1, 2, 3, 4, 5, 6, 7]
|
|
14
|
-
|
|
15
|
-
const result = bfs(tree)
|
|
16
|
-
|
|
17
|
-
expect(result).toStrictEqual(expectedResult)
|
|
18
|
-
})
|
|
19
|
-
|
|
20
|
-
test("it should perform Depth-First Search on a tree", () => {
|
|
21
|
-
const tree = {
|
|
22
|
-
value: 1,
|
|
23
|
-
children: [
|
|
24
|
-
{ value: 2, children: [{ value: 3 }, { value: 4 }] },
|
|
25
|
-
{ value: 5, children: [{ value: 6 }, { value: 7 }] },
|
|
26
|
-
],
|
|
27
|
-
}
|
|
28
|
-
const expectedResult = [1, 2, 3, 4, 5, 6, 7]
|
|
29
|
-
|
|
30
|
-
const result = dfs(tree)
|
|
31
|
-
|
|
32
|
-
expect(result).toStrictEqual(expectedResult)
|
|
33
|
-
})
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Composes multiple functions from right to left, as if every function wraps the next one.
|
|
3
|
-
*
|
|
4
|
-
* @param {...Function} fns - Functions to compose.
|
|
5
|
-
* @returns {Function} A function that takes an initial value and applies the composed functions.
|
|
6
|
-
*/
|
|
7
|
-
export function compose(...fns) {
|
|
8
|
-
return (x) => fns.reduceRight((acc, fn) => fn(acc), x)
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* Pipes multiple functions from left to right, as if the functions are applied one by one.
|
|
13
|
-
*
|
|
14
|
-
* @param {...Function} fns - Functions to pipe.
|
|
15
|
-
* @returns {Function} A function that takes an initial value and applies the piped functions.
|
|
16
|
-
*/
|
|
17
|
-
export function pipe(...fns) {
|
|
18
|
-
return (x) => fns.reduce((acc, fn) => fn(acc), x)
|
|
19
|
-
}
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import { expect, test } from "vitest"
|
|
2
|
-
|
|
3
|
-
import { compose, pipe } from "./functions.js"
|
|
4
|
-
|
|
5
|
-
test("it should compose functions", () => {
|
|
6
|
-
const shout = (x) => x.toUpperCase()
|
|
7
|
-
const punctuate = (mark) => (x) => `${x}${mark}`
|
|
8
|
-
const html = (tag) => (x) => `<${tag}>${x}</${tag}>`
|
|
9
|
-
|
|
10
|
-
const fn = compose(html("p"), punctuate("!"), shout)
|
|
11
|
-
|
|
12
|
-
expect(fn("Hello world")).toBe("<p>HELLO WORLD!</p>")
|
|
13
|
-
})
|
|
14
|
-
|
|
15
|
-
test("it should pipe functions", () => {
|
|
16
|
-
const shout = (x) => x.toUpperCase()
|
|
17
|
-
const punctuate = (mark) => (x) => `${x}${mark}`
|
|
18
|
-
const html = (tag) => (x) => `<${tag}>${x}</${tag}>`
|
|
19
|
-
|
|
20
|
-
const fn = pipe(shout, punctuate("!"), html("p"))
|
|
21
|
-
|
|
22
|
-
expect(fn("Hello world")).toBe("<p>HELLO WORLD!</p>")
|
|
23
|
-
})
|
|
@@ -1,117 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @typedef {import("./types").Point} Point
|
|
3
|
-
* @typedef {import("./types").Circle} Circle
|
|
4
|
-
* @typedef {import("./types").Rectangle} Rectangle
|
|
5
|
-
* @typedef {import("./types").Platform} Platform
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
import { subtract } from "@inglorious/utils/math/linear-algebra/vectors.js"
|
|
9
|
-
|
|
10
|
-
import { clamp } from "../numbers.js"
|
|
11
|
-
import { intersectsCircle as platformIntersectsCircle } from "./platform.js"
|
|
12
|
-
import {
|
|
13
|
-
intersectsCircle as pointIntersectsCircle,
|
|
14
|
-
intersectsRectangle as pointIntersectsRectangle,
|
|
15
|
-
} from "./point.js"
|
|
16
|
-
import { intersectsCircle as segmentIntersectsCircle } from "./segment.js"
|
|
17
|
-
import { hypothenuse } from "./triangle.js"
|
|
18
|
-
|
|
19
|
-
const SQUARED = 2
|
|
20
|
-
const INITIAL_SUM = 0
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* Checks if a circle intersects with a point.
|
|
24
|
-
* @param {Circle} circle - The circle to check.
|
|
25
|
-
* @param {Point} point - The point to check.
|
|
26
|
-
* @returns {boolean} True if the point intersects the circle, false otherwise.
|
|
27
|
-
*/
|
|
28
|
-
export function intersectsPoint(circle, point) {
|
|
29
|
-
return pointIntersectsCircle(point, circle)
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* Checks if two circles intersect.
|
|
34
|
-
* @param {Circle} circle1 - The first circle.
|
|
35
|
-
* @param {Circle} circle2 - The second circle.
|
|
36
|
-
* @returns {boolean} True if the circles intersect, false otherwise.
|
|
37
|
-
*/
|
|
38
|
-
export function intersectsCircle(circle1, circle2) {
|
|
39
|
-
return (
|
|
40
|
-
hypothenuse(...subtract(circle1.position, circle2.position)) <=
|
|
41
|
-
circle1.radius + circle2.radius
|
|
42
|
-
)
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
* Checks if a circle intersects with a rectangle.
|
|
47
|
-
* @param {Circle} circle - The circle to check.
|
|
48
|
-
* @param {Rectangle} rectangle - The rectangle to check.
|
|
49
|
-
* @returns {boolean} True if the circle intersects the rectangle, false otherwise.
|
|
50
|
-
*/
|
|
51
|
-
export function intersectsRectangle(circle, rectangle) {
|
|
52
|
-
const [left, top, front] = rectangle.position
|
|
53
|
-
const [width, height, depth] = rectangle.size
|
|
54
|
-
|
|
55
|
-
const ltf = [left, top, front]
|
|
56
|
-
const rtf = [left + width, top, front]
|
|
57
|
-
const lbf = [left, top, front + depth]
|
|
58
|
-
const rbf = [left + width, top, front + depth]
|
|
59
|
-
|
|
60
|
-
const ltb = [left, top + height, front]
|
|
61
|
-
const rtb = [left + width, top + height, front]
|
|
62
|
-
const lbb = [left, top + height, front + depth]
|
|
63
|
-
const rbb = [left + width, top + height, front + depth]
|
|
64
|
-
|
|
65
|
-
return (
|
|
66
|
-
// Center
|
|
67
|
-
pointIntersectsRectangle(circle.position, rectangle) ||
|
|
68
|
-
// Front face
|
|
69
|
-
segmentIntersectsCircle({ from: ltf, to: rtf }, circle) ||
|
|
70
|
-
segmentIntersectsCircle({ from: rtf, to: rbf }, circle) ||
|
|
71
|
-
segmentIntersectsCircle({ from: rbf, to: lbf }, circle) ||
|
|
72
|
-
segmentIntersectsCircle({ from: lbf, to: ltf }, circle) ||
|
|
73
|
-
// Back face
|
|
74
|
-
segmentIntersectsCircle({ from: ltb, to: rtb }, circle) ||
|
|
75
|
-
segmentIntersectsCircle({ from: rtb, to: rbb }, circle) ||
|
|
76
|
-
segmentIntersectsCircle({ from: rbb, to: lbb }, circle) ||
|
|
77
|
-
segmentIntersectsCircle({ from: lbb, to: ltb }, circle) ||
|
|
78
|
-
// Connecting edges
|
|
79
|
-
segmentIntersectsCircle({ from: ltf, to: ltb }, circle) ||
|
|
80
|
-
segmentIntersectsCircle({ from: rtf, to: rtb }, circle) ||
|
|
81
|
-
segmentIntersectsCircle({ from: lbf, to: lbb }, circle) ||
|
|
82
|
-
segmentIntersectsCircle({ from: rbf, to: rbb }, circle) ||
|
|
83
|
-
// Corners
|
|
84
|
-
isCircleWithinRectangleRadius(circle, rectangle)
|
|
85
|
-
)
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
/**
|
|
89
|
-
* Checks if a circle intersects with a platform.
|
|
90
|
-
* @param {Circle} circle - The circle to check.
|
|
91
|
-
* @param {Platform} platform - The platform to check.
|
|
92
|
-
* @returns {boolean} True if the circle intersects the platform, false otherwise.
|
|
93
|
-
*/
|
|
94
|
-
export function intersectsPlatform(circle, platform) {
|
|
95
|
-
return platformIntersectsCircle(platform, circle)
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
function isCircleWithinRectangleRadius(circle, rectangle) {
|
|
99
|
-
const [left, top, front] = rectangle.position
|
|
100
|
-
const [width, height, depth] = rectangle.size
|
|
101
|
-
|
|
102
|
-
// Find the closest point on the rectangle to the circle's center
|
|
103
|
-
const [x, y, z] = circle.position
|
|
104
|
-
const closestPoint = [
|
|
105
|
-
clamp(x, left, left + width),
|
|
106
|
-
clamp(y, top, top + height),
|
|
107
|
-
clamp(z, front, front + depth),
|
|
108
|
-
]
|
|
109
|
-
|
|
110
|
-
// Calculate the distance from the circle's center to the closest point
|
|
111
|
-
const distanceSquared = subtract(circle.position, closestPoint).reduce(
|
|
112
|
-
(sum, value) => sum + value ** SQUARED,
|
|
113
|
-
INITIAL_SUM,
|
|
114
|
-
)
|
|
115
|
-
|
|
116
|
-
return distanceSquared <= circle.radius ** SQUARED
|
|
117
|
-
}
|