@inglorious/engine 0.2.0 → 0.4.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 +76 -75
- package/package.json +14 -25
- package/src/{engine/ai → ai}/movement/dynamic/align.js +63 -63
- package/src/{engine/ai → ai}/movement/dynamic/arrive.js +42 -42
- package/src/{engine/ai → ai}/movement/dynamic/evade.js +38 -38
- package/src/{engine/ai → ai}/movement/dynamic/face.js +19 -19
- package/src/{engine/ai → ai}/movement/dynamic/flee.js +45 -45
- package/src/{engine/ai → ai}/movement/dynamic/look-where-youre-going.js +16 -16
- package/src/{engine/ai → ai}/movement/dynamic/match-velocity.js +51 -51
- package/src/{engine/ai → ai}/movement/dynamic/pursue.js +38 -38
- package/src/{engine/ai → ai}/movement/dynamic/seek.js +44 -44
- package/src/{engine/ai → ai}/movement/dynamic/wander.js +31 -31
- package/src/{engine/ai → ai}/movement/kinematic/align.js +37 -37
- package/src/{engine/ai → ai}/movement/kinematic/arrive.js +42 -42
- package/src/{engine/ai → ai}/movement/kinematic/face.js +19 -19
- package/src/{engine/ai → ai}/movement/kinematic/flee.js +26 -26
- package/src/{engine/ai → ai}/movement/kinematic/seek.js +26 -26
- package/src/{engine/ai → ai}/movement/kinematic/seek.test.js +42 -42
- package/src/{engine/ai → ai}/movement/kinematic/wander-as-seek.js +31 -31
- package/src/{engine/ai → ai}/movement/kinematic/wander.js +27 -27
- package/src/{engine/animation → animation}/sprite.js +101 -101
- package/src/{engine/animation → animation}/ticker.js +38 -38
- package/src/{engine/behaviors → behaviors}/camera.js +68 -68
- package/src/{engine/behaviors → behaviors}/controls/dynamic/modern.js +76 -76
- package/src/{engine/behaviors → behaviors}/controls/dynamic/shooter.js +84 -84
- package/src/{engine/behaviors → behaviors}/controls/dynamic/tank.js +69 -69
- package/src/{engine/behaviors → behaviors}/controls/event-handlers.js +17 -17
- package/src/{engine/behaviors → behaviors}/controls/kinematic/modern.js +76 -76
- package/src/{engine/behaviors → behaviors}/controls/kinematic/shooter.js +82 -82
- package/src/{engine/behaviors → behaviors}/controls/kinematic/tank.js +67 -67
- package/src/behaviors/debug/collision.js +29 -0
- package/src/{engine/behaviors → behaviors}/fps.js +29 -29
- package/src/{engine/behaviors → behaviors}/fsm.js +33 -33
- package/src/{engine/behaviors → behaviors}/fsm.test.js +49 -49
- package/src/{engine/behaviors → behaviors}/game.js +15 -15
- package/src/{engine/behaviors → behaviors}/input/controls.js +37 -37
- package/src/{engine/behaviors → behaviors}/input/gamepad.js +114 -114
- package/src/{engine/behaviors → behaviors}/input/input.js +48 -48
- package/src/{engine/behaviors → behaviors}/input/keyboard.js +64 -64
- package/src/{engine/behaviors → behaviors}/input/mouse.js +91 -91
- package/src/{engine/behaviors → behaviors}/physics/bouncy.js +25 -25
- package/src/{engine/behaviors → behaviors}/physics/clamped.js +36 -36
- package/src/{engine/behaviors → behaviors}/physics/collidable.js +20 -20
- package/src/{engine/behaviors → behaviors}/physics/jumpable.js +145 -145
- package/src/{engine/behaviors → behaviors}/ui/button.js +17 -17
- package/src/{engine/collision → collision}/detection.js +110 -110
- package/src/{engine/core → core}/dev-tools.js +135 -135
- package/src/{engine/core → core}/engine.js +119 -119
- package/src/{engine/core → core}/loop.js +15 -15
- package/src/{engine/core → core}/loops/animation-frame.js +25 -25
- package/src/{engine/core → core}/loops/elapsed.js +22 -22
- package/src/{engine/core → core}/loops/fixed.js +27 -27
- package/src/{engine/core → core}/loops/flash.js +13 -13
- package/src/{engine/core → core}/loops/lag.js +26 -26
- package/src/main.js +10 -10
- package/src/{engine/movement → movement}/dynamic/modern.js +21 -21
- package/src/{engine/movement → movement}/dynamic/tank.js +43 -43
- package/src/{engine/movement → movement}/kinematic/modern.js +16 -16
- package/src/{engine/movement → movement}/kinematic/modern.test.js +27 -27
- package/src/{engine/movement → movement}/kinematic/tank.js +27 -27
- package/src/{engine/physics → physics}/bounds.js +138 -138
- package/src/{engine/physics → physics}/position.js +43 -43
- package/src/{engine/physics → physics}/position.test.js +80 -80
- package/src/{engine/systems → systems}/sprite-animation.js +27 -27
- package/src/engine/behaviors/debug/collision.js +0 -35
- package/src/engine/core/api.js +0 -34
- package/src/engine/core/select.js +0 -26
- package/src/engine/core/store.js +0 -178
- package/src/engine/core/store.test.js +0 -110
- package/src/renderers/canvas/absolute-position.js +0 -18
- package/src/renderers/canvas/camera.js +0 -13
- package/src/renderers/canvas/canvas-renderer.js +0 -68
- package/src/renderers/canvas/character.js +0 -38
- package/src/renderers/canvas/form/button.js +0 -25
- package/src/renderers/canvas/fps.js +0 -18
- package/src/renderers/canvas/image/hitmask.js +0 -51
- package/src/renderers/canvas/image/image.js +0 -34
- package/src/renderers/canvas/image/sprite.js +0 -49
- package/src/renderers/canvas/image/tilemap.js +0 -66
- package/src/renderers/canvas/mouse.js +0 -37
- package/src/renderers/canvas/rendering-system.js +0 -79
- package/src/renderers/canvas/shapes/circle.js +0 -29
- package/src/renderers/canvas/shapes/rectangle.js +0 -27
- package/src/renderers/react/game/character/character.module.scss +0 -17
- package/src/renderers/react/game/character/index.jsx +0 -20
- package/src/renderers/react/game/cursor/cursor.module.scss +0 -47
- package/src/renderers/react/game/cursor/index.jsx +0 -20
- package/src/renderers/react/game/form/fields/field/field.module.scss +0 -5
- package/src/renderers/react/game/form/fields/field/index.jsx +0 -56
- package/src/renderers/react/game/form/fields/fields.module.scss +0 -48
- package/src/renderers/react/game/form/fields/index.jsx +0 -12
- package/src/renderers/react/game/form/form.module.scss +0 -18
- package/src/renderers/react/game/form/index.jsx +0 -22
- package/src/renderers/react/game/fps/index.jsx +0 -16
- package/src/renderers/react/game/game.jsx +0 -72
- package/src/renderers/react/game/index.jsx +0 -29
- package/src/renderers/react/game/platform/index.jsx +0 -30
- package/src/renderers/react/game/platform/platform.module.scss +0 -7
- package/src/renderers/react/game/scene/index.jsx +0 -27
- package/src/renderers/react/game/scene/scene.module.scss +0 -9
- package/src/renderers/react/game/sprite/index.jsx +0 -60
- package/src/renderers/react/game/sprite/sprite.module.css +0 -3
- package/src/renderers/react/game/stats/index.jsx +0 -22
- package/src/renderers/react/hocs/with-absolute-position/index.jsx +0 -20
- package/src/renderers/react/hocs/with-absolute-position/with-absolute-position.module.scss +0 -5
- package/src/renderers/react/index.jsx +0 -9
- package/src/utils/algorithms/decision-tree.js +0 -24
- package/src/utils/algorithms/decision-tree.test.js +0 -153
- 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 -138
- package/src/utils/data-structures/object.test.js +0 -218
- package/src/utils/data-structures/objects.js +0 -66
- 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 -70
- package/src/utils/math/geometry/circle.test.js +0 -97
- package/src/utils/math/geometry/hitmask.js +0 -70
- package/src/utils/math/geometry/hitmask.test.js +0 -155
- package/src/utils/math/geometry/line.js +0 -35
- package/src/utils/math/geometry/line.test.js +0 -49
- package/src/utils/math/geometry/point.js +0 -78
- package/src/utils/math/geometry/point.test.js +0 -81
- package/src/utils/math/geometry/rectangle.js +0 -76
- 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 -327
- package/src/utils/math/linear-algebra/vector.test.js +0 -265
- 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/linear-interpolation.js +0 -9
- 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 -61
- package/src/utils/physics/friction.js +0 -28
- package/src/utils/physics/friction.test.js +0 -42
- package/src/utils/physics/gravity.js +0 -69
- package/src/utils/physics/gravity.test.js +0 -77
- package/src/utils/physics/jump.js +0 -31
- package/src/utils/physics/velocity.js +0 -36
|
@@ -1,218 +0,0 @@
|
|
|
1
|
-
import { expect, test } from "vitest"
|
|
2
|
-
|
|
3
|
-
import {
|
|
4
|
-
clone,
|
|
5
|
-
filter,
|
|
6
|
-
find,
|
|
7
|
-
isObject,
|
|
8
|
-
map,
|
|
9
|
-
produce,
|
|
10
|
-
toString,
|
|
11
|
-
} from "./object.js"
|
|
12
|
-
|
|
13
|
-
test("it should deep clone an object", () => {
|
|
14
|
-
const obj = {
|
|
15
|
-
primitive: 1,
|
|
16
|
-
array: [2, 3],
|
|
17
|
-
object: { a: 1, b: 2 },
|
|
18
|
-
}
|
|
19
|
-
const expectedResult = {
|
|
20
|
-
primitive: 1,
|
|
21
|
-
array: [2, 3],
|
|
22
|
-
object: { a: 1, b: 2 },
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
const result = clone(obj)
|
|
26
|
-
|
|
27
|
-
expect(result).toStrictEqual(expectedResult)
|
|
28
|
-
expect(result).not.toBe(expectedResult)
|
|
29
|
-
expect(result.primitive).toBe(expectedResult.primitive)
|
|
30
|
-
expect(result.array).not.toBe(expectedResult.array)
|
|
31
|
-
expect(result.object).not.toBe(expectedResult.object)
|
|
32
|
-
})
|
|
33
|
-
|
|
34
|
-
test("it should behave like Array.prototype.filter, but on an object", () => {
|
|
35
|
-
const obj = {
|
|
36
|
-
key1: "value1",
|
|
37
|
-
key2: "value2",
|
|
38
|
-
key3: "value3",
|
|
39
|
-
}
|
|
40
|
-
const callback = (key) => ["key2", "key3"].includes(key)
|
|
41
|
-
const expectedResult = {
|
|
42
|
-
key2: "value2",
|
|
43
|
-
key3: "value3",
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
expect(filter(obj, callback)).toStrictEqual(expectedResult)
|
|
47
|
-
})
|
|
48
|
-
|
|
49
|
-
test("it should behave like Array.prototype.find, but on an object", () => {
|
|
50
|
-
const obj = {
|
|
51
|
-
key1: "value1",
|
|
52
|
-
key2: "value2",
|
|
53
|
-
key3: "value3",
|
|
54
|
-
}
|
|
55
|
-
const callback = (key) => ["key2", "key3"].includes(key)
|
|
56
|
-
const expectedResult = {
|
|
57
|
-
key2: "value2",
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
expect(find(obj, callback)).toStrictEqual(expectedResult)
|
|
61
|
-
})
|
|
62
|
-
|
|
63
|
-
test("it correctly should check if a value is an object", () => {
|
|
64
|
-
expect(isObject(1)).toBe(false)
|
|
65
|
-
expect(isObject("a")).toBe(false)
|
|
66
|
-
expect(isObject([])).toBe(false)
|
|
67
|
-
expect(isObject(null)).toBe(false)
|
|
68
|
-
expect(isObject(new Date())).toBe(false)
|
|
69
|
-
expect(isObject({})).toBe(true)
|
|
70
|
-
})
|
|
71
|
-
|
|
72
|
-
test("it should behave like Array.prototype.map, but on an object", () => {
|
|
73
|
-
const obj = {
|
|
74
|
-
key1: "value1",
|
|
75
|
-
key2: "value2",
|
|
76
|
-
key3: "value3",
|
|
77
|
-
}
|
|
78
|
-
const callback = (key, value) => value.toUpperCase()
|
|
79
|
-
const expectedResult = {
|
|
80
|
-
key1: "VALUE1",
|
|
81
|
-
key2: "VALUE2",
|
|
82
|
-
key3: "VALUE3",
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
expect(map(obj, callback)).toStrictEqual(expectedResult)
|
|
86
|
-
})
|
|
87
|
-
|
|
88
|
-
test("it should produce a new state without mutating the original", () => {
|
|
89
|
-
const baseState = {
|
|
90
|
-
a: 1,
|
|
91
|
-
b: {
|
|
92
|
-
c: [2, 3],
|
|
93
|
-
d: { e: 4 },
|
|
94
|
-
},
|
|
95
|
-
f: 5,
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
const recipe = (draft) => {
|
|
99
|
-
draft.a = 10
|
|
100
|
-
draft.b.c.push(4)
|
|
101
|
-
draft.b.d.e = 40
|
|
102
|
-
draft.g = 6
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
const expectedState = {
|
|
106
|
-
a: 10,
|
|
107
|
-
b: {
|
|
108
|
-
c: [2, 3, 4],
|
|
109
|
-
d: { e: 40 },
|
|
110
|
-
},
|
|
111
|
-
f: 5,
|
|
112
|
-
g: 6,
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
const originalBaseState = JSON.parse(JSON.stringify(baseState))
|
|
116
|
-
|
|
117
|
-
const nextState = produce(baseState, recipe)
|
|
118
|
-
|
|
119
|
-
expect(nextState).toStrictEqual(expectedState)
|
|
120
|
-
expect(nextState).not.toBe(baseState)
|
|
121
|
-
expect(baseState).toStrictEqual(originalBaseState)
|
|
122
|
-
})
|
|
123
|
-
|
|
124
|
-
test("it should support currying to produce a new state", () => {
|
|
125
|
-
const baseState = {
|
|
126
|
-
a: 1,
|
|
127
|
-
b: {
|
|
128
|
-
c: [2, 3],
|
|
129
|
-
d: { e: 4 },
|
|
130
|
-
},
|
|
131
|
-
f: 5,
|
|
132
|
-
}
|
|
133
|
-
const originalStateCopy = clone(baseState)
|
|
134
|
-
|
|
135
|
-
const recipe = (draft) => {
|
|
136
|
-
draft.a = 10
|
|
137
|
-
draft.b.c.push(4)
|
|
138
|
-
draft.g = 6
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
const expectedState = {
|
|
142
|
-
a: 10,
|
|
143
|
-
b: {
|
|
144
|
-
c: [2, 3, 4],
|
|
145
|
-
d: { e: 4 },
|
|
146
|
-
},
|
|
147
|
-
f: 5,
|
|
148
|
-
g: 6,
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
const producer = produce(recipe)
|
|
152
|
-
const nextState = producer(baseState)
|
|
153
|
-
|
|
154
|
-
expect(nextState).toStrictEqual(expectedState)
|
|
155
|
-
expect(nextState).not.toBe(baseState)
|
|
156
|
-
expect(baseState).toStrictEqual(originalStateCopy)
|
|
157
|
-
})
|
|
158
|
-
|
|
159
|
-
test("it should pass extra arguments to the recipe", () => {
|
|
160
|
-
const baseState = { value: 1 }
|
|
161
|
-
const recipe = (draft, increment, multiplier) => {
|
|
162
|
-
draft.value = (draft.value + increment) * multiplier
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
// Test non-curried version
|
|
166
|
-
const nextStateUncurried = produce(baseState, recipe, 2, 3) // (1 + 2) * 3 = 9
|
|
167
|
-
expect(nextStateUncurried.value).toBe(9)
|
|
168
|
-
|
|
169
|
-
// Test curried version
|
|
170
|
-
const producer = produce(recipe)
|
|
171
|
-
const nextStateCurried = producer(baseState, 4, 5) // (1 + 4) * 5 = 25
|
|
172
|
-
expect(nextStateCurried.value).toBe(25)
|
|
173
|
-
|
|
174
|
-
expect(baseState.value).toBe(1)
|
|
175
|
-
})
|
|
176
|
-
|
|
177
|
-
test("it should return a string representation of a shallow object", () => {
|
|
178
|
-
const obj = {
|
|
179
|
-
key1: "value1",
|
|
180
|
-
key2: "value2",
|
|
181
|
-
key3: "value3",
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
expect(toString(obj)).toBe(`{
|
|
185
|
-
key1: "value1",
|
|
186
|
-
key2: "value2",
|
|
187
|
-
key3: "value3"
|
|
188
|
-
}`)
|
|
189
|
-
})
|
|
190
|
-
|
|
191
|
-
test("it should return a string representation of a nested object", () => {
|
|
192
|
-
const obj = {
|
|
193
|
-
a: 1,
|
|
194
|
-
b: [7, 3],
|
|
195
|
-
c: { d: 4, h: 8 },
|
|
196
|
-
e: [{ f: 5, i: 9 }],
|
|
197
|
-
g: 6,
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
expect(toString(obj)).toBe(`{
|
|
201
|
-
a: 1,
|
|
202
|
-
b: [
|
|
203
|
-
7,
|
|
204
|
-
3
|
|
205
|
-
],
|
|
206
|
-
c: {
|
|
207
|
-
d: 4,
|
|
208
|
-
h: 8
|
|
209
|
-
},
|
|
210
|
-
e: [
|
|
211
|
-
{
|
|
212
|
-
f: 5,
|
|
213
|
-
i: 9
|
|
214
|
-
}
|
|
215
|
-
],
|
|
216
|
-
g: 6
|
|
217
|
-
}`)
|
|
218
|
-
})
|
|
@@ -1,66 +0,0 @@
|
|
|
1
|
-
import { isObject } from "./object.js"
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Extends a target object by merging it with one or more source objects.
|
|
5
|
-
* Performs a deep merge for nested objects and arrays.
|
|
6
|
-
*
|
|
7
|
-
* @param {Object} target - The target object to extend.
|
|
8
|
-
* @param {...Object} sources - The source objects to merge into the target object.
|
|
9
|
-
* @returns {Object} - The extended target object.
|
|
10
|
-
*/
|
|
11
|
-
export function extend(target, ...sources) {
|
|
12
|
-
return merge({}, target, ...sources)
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* Merges multiple source objects into a target object.
|
|
17
|
-
* Performs a deep merge for nested objects and arrays.
|
|
18
|
-
*
|
|
19
|
-
* @param {Object} target - The target object to merge into.
|
|
20
|
-
* @param {...Object} sources - The source objects to merge from.
|
|
21
|
-
* @returns {Object} - The merged target object.
|
|
22
|
-
*/
|
|
23
|
-
export function merge(target, ...sources) {
|
|
24
|
-
return sources
|
|
25
|
-
.filter((source) => source != null)
|
|
26
|
-
.reduce((acc, source) => deepMerge(acc, source), target)
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* Recursively merges the properties of the source object into the target object.
|
|
31
|
-
*
|
|
32
|
-
* @param {Object} target - The target object to merge into.
|
|
33
|
-
* @param {Object} source - The source object to merge from.
|
|
34
|
-
* @returns {Object} - The merged target object.
|
|
35
|
-
*/
|
|
36
|
-
function deepMerge(target, source) {
|
|
37
|
-
for (const [key, value] of Object.entries(source)) {
|
|
38
|
-
if (Array.isArray(value) || isObject(value)) {
|
|
39
|
-
if (target[key] === undefined) {
|
|
40
|
-
target[key] = new value.__proto__.constructor()
|
|
41
|
-
}
|
|
42
|
-
target[key] = deepMerge(target[key], value)
|
|
43
|
-
} else {
|
|
44
|
-
target[key] = value
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
return target
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
/**
|
|
51
|
-
* Assigns default properties to a target object from a source object.
|
|
52
|
-
* For each key in `defaultProps`, if `target[key]` is `null` or `undefined`,
|
|
53
|
-
* it is set to `defaultProps[key]`.
|
|
54
|
-
*
|
|
55
|
-
* This function modifies the target object in place.
|
|
56
|
-
*
|
|
57
|
-
* @param {Object} target The object to apply defaults to.
|
|
58
|
-
* @param {Object} defaultProps The object containing the default properties.
|
|
59
|
-
* @returns {Object} The modified target object.
|
|
60
|
-
*/
|
|
61
|
-
export function defaults(target, defaultProps) {
|
|
62
|
-
for (const key in defaultProps) {
|
|
63
|
-
target[key] ??= defaultProps[key]
|
|
64
|
-
}
|
|
65
|
-
return target
|
|
66
|
-
}
|
|
@@ -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,70 +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 { clamp } from "@inglorious/utils/math/linear-algebra/vector.js"
|
|
9
|
-
import { subtract } from "@inglorious/utils/math/linear-algebra/vectors.js"
|
|
10
|
-
|
|
11
|
-
import { intersectsCircle as pointIntersectsCircle } from "./point.js"
|
|
12
|
-
import { hypothenuse } from "./triangle.js"
|
|
13
|
-
|
|
14
|
-
const HALF = 2
|
|
15
|
-
const SQUARED = 2
|
|
16
|
-
const INITIAL_SUM = 0
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* Checks if a circle intersects with a point.
|
|
20
|
-
* @param {Circle} circle - The circle to check.
|
|
21
|
-
* @param {Point} point - The point to check.
|
|
22
|
-
* @returns {boolean} True if the point intersects the circle, false otherwise.
|
|
23
|
-
*/
|
|
24
|
-
export function intersectsPoint(circle, point) {
|
|
25
|
-
return pointIntersectsCircle(point, circle)
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* Checks if two circles intersect.
|
|
30
|
-
* @param {Circle} circle1 - The first circle.
|
|
31
|
-
* @param {Circle} circle2 - The second circle.
|
|
32
|
-
* @returns {boolean} True if the circles intersect, false otherwise.
|
|
33
|
-
*/
|
|
34
|
-
export function intersectsCircle(circle1, circle2) {
|
|
35
|
-
return (
|
|
36
|
-
hypothenuse(...subtract(circle1.position, circle2.position)) <=
|
|
37
|
-
circle1.radius + circle2.radius
|
|
38
|
-
)
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* Checks if a circle intersects with a rectangle.
|
|
43
|
-
* @param {Circle} circle - The circle to check.
|
|
44
|
-
* @param {Rectangle} rectangle - The rectangle to check.
|
|
45
|
-
* @returns {boolean} True if the circle intersects the rectangle, false otherwise.
|
|
46
|
-
*/
|
|
47
|
-
export function intersectsRectangle(circle, rectangle) {
|
|
48
|
-
const [rectX, rectY, rectZ] = rectangle.position
|
|
49
|
-
const [width, height, depth] = rectangle.size
|
|
50
|
-
|
|
51
|
-
const left = rectX - width / HALF
|
|
52
|
-
const right = rectX + width / HALF
|
|
53
|
-
const bottom = rectY - height / HALF
|
|
54
|
-
const top = rectY + height / HALF
|
|
55
|
-
const back = rectZ - depth / HALF
|
|
56
|
-
const front = rectZ + depth / HALF
|
|
57
|
-
|
|
58
|
-
const closestPoint = clamp(
|
|
59
|
-
circle.position,
|
|
60
|
-
[left, bottom, back],
|
|
61
|
-
[right, top, front],
|
|
62
|
-
)
|
|
63
|
-
|
|
64
|
-
const distanceSquared = subtract(circle.position, closestPoint).reduce(
|
|
65
|
-
(sum, value) => sum + value ** SQUARED,
|
|
66
|
-
INITIAL_SUM,
|
|
67
|
-
)
|
|
68
|
-
|
|
69
|
-
return distanceSquared <= circle.radius ** SQUARED
|
|
70
|
-
}
|
|
@@ -1,97 +0,0 @@
|
|
|
1
|
-
import { expect, test } from "vitest"
|
|
2
|
-
|
|
3
|
-
import {
|
|
4
|
-
intersectsCircle,
|
|
5
|
-
intersectsPoint,
|
|
6
|
-
intersectsRectangle,
|
|
7
|
-
} from "./circle.js"
|
|
8
|
-
|
|
9
|
-
test("it should prove that a circle around a point intersects with it", () => {
|
|
10
|
-
const circle = {
|
|
11
|
-
position: [1, 1, 0],
|
|
12
|
-
radius: 1,
|
|
13
|
-
}
|
|
14
|
-
const point = [1.5, 1.5, 0]
|
|
15
|
-
|
|
16
|
-
expect(intersectsPoint(circle, point)).toBe(true)
|
|
17
|
-
})
|
|
18
|
-
|
|
19
|
-
test("it should prove that two equal circles intersect", () => {
|
|
20
|
-
const circle1 = { position: [1, 1, 0], radius: 1 }
|
|
21
|
-
const circle2 = { position: [1, 1, 0], radius: 1 }
|
|
22
|
-
|
|
23
|
-
expect(intersectsCircle(circle1, circle2)).toBe(true)
|
|
24
|
-
})
|
|
25
|
-
|
|
26
|
-
test("it should prove that two circles shifted by a small x intersect", () => {
|
|
27
|
-
const circle1 = { position: [1, 1, 0], radius: 1 }
|
|
28
|
-
const circle2 = { position: [2, 1, 0], radius: 1 }
|
|
29
|
-
|
|
30
|
-
expect(intersectsCircle(circle1, circle2)).toBe(true)
|
|
31
|
-
})
|
|
32
|
-
|
|
33
|
-
test("it should prove that two touching circles intersect", () => {
|
|
34
|
-
const circle1 = { position: [1, 1, 0], radius: 1 }
|
|
35
|
-
const circle2 = { position: [3, 1, 0], radius: 1 }
|
|
36
|
-
|
|
37
|
-
expect(intersectsCircle(circle1, circle2)).toBe(true)
|
|
38
|
-
})
|
|
39
|
-
|
|
40
|
-
test("it should prove that two non-touching circles do not intersect", () => {
|
|
41
|
-
const circle1 = { position: [1, 1, 0], radius: 1 }
|
|
42
|
-
const circle2 = { position: [4, 1, 0], radius: 1 }
|
|
43
|
-
|
|
44
|
-
expect(intersectsCircle(circle1, circle2)).toBe(false)
|
|
45
|
-
})
|
|
46
|
-
|
|
47
|
-
test("it should prove that a circle inside of a rectangle intersects with it", () => {
|
|
48
|
-
const circle = {
|
|
49
|
-
position: [1, 1, 0],
|
|
50
|
-
radius: 1,
|
|
51
|
-
}
|
|
52
|
-
const rectangle = {
|
|
53
|
-
position: [-1, -1, 0],
|
|
54
|
-
size: [4, 4, 0],
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
expect(intersectsRectangle(circle, rectangle)).toBe(true)
|
|
58
|
-
})
|
|
59
|
-
|
|
60
|
-
test("it should prove that a circle touching the border of a rectangle intersects with it", () => {
|
|
61
|
-
const circle = {
|
|
62
|
-
position: [1, 1, 0],
|
|
63
|
-
radius: 1,
|
|
64
|
-
}
|
|
65
|
-
const rectangle = {
|
|
66
|
-
position: [-1, 1, 0], // Rectangle's right edge is at x=0, touching the circle's left edge.
|
|
67
|
-
size: [2, 2, 0],
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
expect(intersectsRectangle(circle, rectangle)).toBe(true)
|
|
71
|
-
})
|
|
72
|
-
|
|
73
|
-
test("it should prove that a circle crossing a rectangle intersects with it", () => {
|
|
74
|
-
const circle = {
|
|
75
|
-
position: [1, 1, 0],
|
|
76
|
-
radius: 1,
|
|
77
|
-
}
|
|
78
|
-
const rectangle = {
|
|
79
|
-
position: [0, 0, 0],
|
|
80
|
-
size: [2, 2, 0],
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
expect(intersectsRectangle(circle, rectangle)).toBe(true)
|
|
84
|
-
})
|
|
85
|
-
|
|
86
|
-
test("it should prove that a circle outside of a rectangle does not intersect with it", () => {
|
|
87
|
-
const circle = {
|
|
88
|
-
position: [1, 1, 0],
|
|
89
|
-
radius: 1,
|
|
90
|
-
}
|
|
91
|
-
const rectangle = {
|
|
92
|
-
position: [-3, 0, 0],
|
|
93
|
-
size: [2, 2, 0],
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
expect(intersectsRectangle(circle, rectangle)).toBe(false)
|
|
97
|
-
})
|