@inglorious/engine 0.1.0 → 0.2.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 +39 -36
- package/package.json +20 -33
- package/src/engine/ai/movement/dynamic/align.js +63 -63
- package/src/engine/ai/movement/dynamic/arrive.js +42 -43
- package/src/engine/ai/movement/dynamic/evade.js +38 -38
- package/src/engine/ai/movement/dynamic/face.js +19 -20
- package/src/engine/ai/movement/dynamic/flee.js +45 -45
- package/src/engine/ai/movement/dynamic/look-where-youre-going.js +16 -17
- package/src/engine/ai/movement/dynamic/match-velocity.js +51 -50
- package/src/engine/ai/movement/dynamic/pursue.js +38 -38
- package/src/engine/ai/movement/dynamic/seek.js +44 -44
- package/src/engine/ai/movement/dynamic/wander.js +31 -32
- package/src/engine/ai/movement/kinematic/align.js +37 -37
- package/src/engine/ai/movement/kinematic/arrive.js +42 -42
- package/src/engine/ai/movement/kinematic/face.js +19 -20
- package/src/engine/ai/movement/kinematic/flee.js +26 -26
- package/src/engine/ai/movement/kinematic/seek.js +26 -26
- package/src/engine/ai/movement/kinematic/seek.test.js +42 -42
- package/src/engine/ai/movement/kinematic/wander-as-seek.js +31 -31
- package/src/engine/ai/movement/kinematic/wander.js +27 -27
- package/src/engine/animation/sprite.js +101 -0
- package/src/engine/animation/ticker.js +38 -0
- package/src/engine/behaviors/camera.js +68 -0
- package/src/engine/behaviors/controls/dynamic/modern.js +76 -0
- package/src/engine/behaviors/controls/dynamic/shooter.js +84 -0
- package/src/engine/behaviors/controls/dynamic/tank.js +69 -0
- package/src/engine/behaviors/controls/event-handlers.js +17 -0
- package/src/engine/behaviors/controls/kinematic/modern.js +76 -0
- package/src/engine/behaviors/controls/kinematic/shooter.js +82 -0
- package/src/engine/behaviors/controls/kinematic/tank.js +67 -0
- package/src/engine/behaviors/debug/collision.js +35 -0
- package/src/engine/behaviors/fps.js +29 -0
- package/src/engine/behaviors/fsm.js +33 -0
- package/src/{game/decorators → engine/behaviors}/fsm.test.js +49 -56
- package/src/engine/behaviors/game.js +15 -0
- package/src/engine/behaviors/input/controls.js +37 -0
- package/src/engine/behaviors/input/gamepad.js +114 -0
- package/src/engine/behaviors/input/input.js +48 -0
- package/src/engine/behaviors/input/keyboard.js +64 -0
- package/src/engine/behaviors/input/mouse.js +91 -0
- package/src/engine/behaviors/physics/bouncy.js +25 -0
- package/src/engine/behaviors/physics/clamped.js +36 -0
- package/src/{game/decorators/collisions.js → engine/behaviors/physics/collidable.js} +20 -24
- package/src/engine/behaviors/physics/jumpable.js +145 -0
- package/src/engine/behaviors/ui/button.js +17 -0
- package/src/engine/collision/detection.js +110 -115
- package/src/engine/core/api.js +34 -0
- package/src/engine/core/dev-tools.js +135 -0
- package/src/engine/core/engine.js +119 -0
- package/src/engine/core/loop.js +15 -0
- package/src/engine/{loop → core/loops}/animation-frame.js +25 -26
- package/src/engine/{loop → core/loops}/elapsed.js +22 -23
- package/src/engine/{loop → core/loops}/fixed.js +27 -28
- package/src/engine/{loop → core/loops}/flash.js +13 -14
- package/src/engine/{loop → core/loops}/lag.js +26 -27
- package/src/engine/core/select.js +26 -0
- package/src/engine/core/store.js +178 -0
- package/src/engine/core/store.test.js +110 -0
- package/src/engine/movement/dynamic/modern.js +21 -24
- package/src/engine/movement/dynamic/tank.js +43 -43
- package/src/engine/movement/kinematic/modern.js +16 -16
- package/src/engine/movement/kinematic/modern.test.js +27 -27
- package/src/engine/movement/kinematic/tank.js +27 -27
- package/src/engine/physics/bounds.js +138 -0
- package/src/engine/physics/position.js +43 -0
- package/src/engine/physics/position.test.js +80 -0
- package/src/engine/systems/sprite-animation.js +27 -0
- package/src/main.js +10 -5
- package/src/renderers/canvas/absolute-position.js +18 -0
- package/src/renderers/canvas/camera.js +13 -0
- package/src/renderers/canvas/canvas-renderer.js +68 -0
- package/src/{ui → renderers}/canvas/character.js +38 -35
- package/src/{ui → renderers}/canvas/form/button.js +25 -25
- package/src/{ui → renderers}/canvas/fps.js +18 -18
- package/src/renderers/canvas/image/hitmask.js +51 -0
- package/src/{ui → renderers}/canvas/image/image.js +34 -37
- package/src/{ui → renderers}/canvas/image/sprite.js +49 -49
- package/src/{ui → renderers}/canvas/image/tilemap.js +66 -64
- package/src/{ui → renderers}/canvas/mouse.js +37 -37
- package/src/renderers/canvas/rendering-system.js +79 -0
- package/src/{ui → renderers}/canvas/shapes/circle.js +29 -31
- package/src/{ui → renderers}/canvas/shapes/rectangle.js +27 -31
- package/src/renderers/react/game/character/index.jsx +20 -0
- package/src/{ui → renderers}/react/game/cursor/index.jsx +20 -20
- package/src/{ui → renderers}/react/game/form/fields/field/index.jsx +56 -56
- package/src/{ui → renderers}/react/game/form/fields/index.jsx +12 -12
- package/src/{ui → renderers}/react/game/form/index.jsx +22 -22
- package/src/{ui → renderers}/react/game/fps/index.jsx +16 -16
- package/src/{ui → renderers}/react/game/game.jsx +72 -71
- package/src/{ui → renderers}/react/game/index.jsx +29 -29
- package/src/{ui → renderers}/react/game/platform/index.jsx +30 -30
- package/src/{ui → renderers}/react/game/scene/index.jsx +27 -25
- package/src/{ui → renderers}/react/game/sprite/index.jsx +60 -58
- package/src/{ui → renderers}/react/game/stats/index.jsx +22 -22
- package/src/{ui → renderers}/react/hocs/with-absolute-position/index.jsx +20 -20
- package/src/{ui → renderers}/react/index.jsx +9 -9
- package/src/utils/algorithms/decision-tree.js +24 -24
- package/src/utils/algorithms/decision-tree.test.js +153 -102
- package/src/utils/algorithms/path-finding.js +155 -155
- package/src/utils/algorithms/path-finding.test.js +151 -151
- package/src/utils/data-structures/array.js +83 -83
- package/src/utils/data-structures/array.test.js +173 -173
- package/src/utils/data-structures/board.js +159 -159
- package/src/utils/data-structures/board.test.js +242 -242
- package/src/utils/data-structures/boolean.js +9 -9
- package/src/utils/data-structures/heap.js +164 -164
- package/src/utils/data-structures/heap.test.js +103 -103
- package/src/utils/data-structures/object.js +138 -102
- package/src/utils/data-structures/object.test.js +218 -121
- package/src/utils/data-structures/objects.js +66 -48
- package/src/utils/data-structures/objects.test.js +99 -99
- package/src/utils/data-structures/tree.js +36 -36
- package/src/utils/data-structures/tree.test.js +33 -33
- package/src/utils/functions/functions.js +19 -19
- package/src/utils/functions/functions.test.js +23 -23
- package/src/utils/math/geometry/circle.js +70 -117
- package/src/utils/math/geometry/circle.test.js +97 -97
- package/src/utils/math/geometry/hitmask.js +70 -39
- package/src/utils/math/geometry/hitmask.test.js +155 -84
- package/src/utils/math/geometry/line.js +35 -35
- package/src/utils/math/geometry/line.test.js +49 -49
- package/src/utils/math/geometry/point.js +78 -71
- package/src/utils/math/geometry/point.test.js +81 -81
- package/src/utils/math/geometry/rectangle.js +76 -45
- package/src/utils/math/geometry/rectangle.test.js +42 -42
- package/src/utils/math/geometry/segment.js +80 -80
- package/src/utils/math/geometry/segment.test.js +183 -183
- package/src/utils/math/geometry/triangle.js +15 -15
- package/src/utils/math/geometry/triangle.test.js +11 -11
- package/src/utils/math/linear-algebra/2d.js +28 -28
- package/src/utils/math/linear-algebra/2d.test.js +17 -17
- package/src/utils/math/linear-algebra/quaternion.js +22 -22
- package/src/utils/math/linear-algebra/quaternion.test.js +25 -25
- package/src/utils/math/linear-algebra/quaternions.js +20 -20
- package/src/utils/math/linear-algebra/quaternions.test.js +29 -29
- package/src/utils/math/linear-algebra/vector.js +327 -302
- package/src/utils/math/linear-algebra/vector.test.js +265 -257
- package/src/utils/math/linear-algebra/vectors.js +122 -122
- package/src/utils/math/linear-algebra/vectors.test.js +65 -65
- package/src/utils/math/linear-interpolation.js +9 -0
- package/src/utils/math/numbers.js +90 -90
- package/src/utils/math/numbers.test.js +137 -137
- package/src/utils/math/rng.js +44 -44
- package/src/utils/math/rng.test.js +39 -39
- package/src/utils/math/statistics.js +43 -43
- package/src/utils/math/statistics.test.js +47 -47
- package/src/utils/math/trigonometry.js +89 -89
- package/src/utils/math/trigonometry.test.js +52 -52
- package/src/utils/physics/acceleration.js +61 -63
- package/src/utils/physics/friction.js +28 -30
- package/src/utils/physics/friction.test.js +42 -44
- package/src/utils/physics/gravity.js +69 -71
- package/src/utils/physics/gravity.test.js +77 -80
- package/src/utils/physics/jump.js +31 -41
- package/src/utils/physics/velocity.js +36 -38
- package/src/docs/ai/movement/dynamic/align.js +0 -131
- package/src/docs/ai/movement/dynamic/arrive.js +0 -88
- package/src/docs/ai/movement/dynamic/dynamic.mdx +0 -99
- package/src/docs/ai/movement/dynamic/dynamic.stories.js +0 -58
- package/src/docs/ai/movement/dynamic/evade.js +0 -72
- package/src/docs/ai/movement/dynamic/face.js +0 -90
- package/src/docs/ai/movement/dynamic/flee.js +0 -38
- package/src/docs/ai/movement/dynamic/look-where-youre-going.js +0 -114
- package/src/docs/ai/movement/dynamic/match-velocity.js +0 -92
- package/src/docs/ai/movement/dynamic/pursue.js +0 -72
- package/src/docs/ai/movement/dynamic/seek.js +0 -37
- package/src/docs/ai/movement/dynamic/wander.js +0 -71
- package/src/docs/ai/movement/kinematic/align.js +0 -122
- package/src/docs/ai/movement/kinematic/arrive.js +0 -78
- package/src/docs/ai/movement/kinematic/face.js +0 -82
- package/src/docs/ai/movement/kinematic/flee.js +0 -36
- package/src/docs/ai/movement/kinematic/kinematic.mdx +0 -67
- package/src/docs/ai/movement/kinematic/kinematic.stories.js +0 -42
- package/src/docs/ai/movement/kinematic/seek.js +0 -34
- package/src/docs/ai/movement/kinematic/wander-as-seek.js +0 -62
- package/src/docs/ai/movement/kinematic/wander.js +0 -28
- package/src/docs/bounds.js +0 -7
- package/src/docs/code-reuse.js +0 -35
- package/src/docs/collision/circles.js +0 -58
- package/src/docs/collision/collision.mdx +0 -27
- package/src/docs/collision/collision.stories.js +0 -22
- package/src/docs/collision/platform.js +0 -76
- package/src/docs/collision/tilemap.js +0 -181
- package/src/docs/empty.js +0 -1
- package/src/docs/engine.mdx +0 -81
- package/src/docs/engine.stories.js +0 -37
- package/src/docs/event-handlers.js +0 -68
- package/src/docs/framerate.js +0 -37
- package/src/docs/game.jsx +0 -15
- package/src/docs/image/image.js +0 -19
- package/src/docs/image/image.stories.js +0 -22
- package/src/docs/image/sprite.js +0 -39
- package/src/docs/image/tilemap.js +0 -84
- package/src/docs/input/controls.js +0 -67
- package/src/docs/input/gamepad.js +0 -67
- package/src/docs/input/input.mdx +0 -55
- package/src/docs/input/input.stories.js +0 -27
- package/src/docs/input/keyboard.js +0 -58
- package/src/docs/input/mouse.js +0 -32
- package/src/docs/instances.js +0 -49
- package/src/docs/player/dynamic/double-jump.js +0 -90
- package/src/docs/player/dynamic/dynamic.stories.js +0 -32
- package/src/docs/player/dynamic/jump.js +0 -83
- package/src/docs/player/dynamic/modern-controls.js +0 -57
- package/src/docs/player/dynamic/shooter-controls.js +0 -51
- package/src/docs/player/dynamic/tank-controls.js +0 -44
- package/src/docs/player/kinematic/double-jump.js +0 -90
- package/src/docs/player/kinematic/jump.js +0 -82
- package/src/docs/player/kinematic/kinematic.stories.js +0 -32
- package/src/docs/player/kinematic/modern-controls.js +0 -56
- package/src/docs/player/kinematic/shooter-controls.js +0 -48
- package/src/docs/player/kinematic/tank-controls.js +0 -42
- package/src/docs/quick-start/first-game.js +0 -49
- package/src/docs/quick-start/hello-world.js +0 -1
- package/src/docs/quick-start.mdx +0 -127
- package/src/docs/quick-start.stories.js +0 -17
- package/src/docs/recipes/add-and-remove.js +0 -71
- package/src/docs/recipes/add-instance.js +0 -42
- package/src/docs/recipes/decision-tree.js +0 -169
- package/src/docs/recipes/random-instances.js +0 -25
- package/src/docs/recipes/recipes.mdx +0 -81
- package/src/docs/recipes/recipes.stories.js +0 -37
- package/src/docs/recipes/remove-instance.js +0 -52
- package/src/docs/recipes/states.js +0 -64
- package/src/docs/ui/button.js +0 -28
- package/src/docs/ui/form.stories.js +0 -55
- package/src/docs/ui-chooser.jsx +0 -6
- package/src/docs/utils/data-structures/object.mdx +0 -47
- package/src/docs/utils/data-structures/objects.mdx +0 -30
- package/src/docs/utils/functions/functions.mdx +0 -34
- package/src/docs/utils/math/geometry/circle.mdx +0 -55
- package/src/docs/utils/math/geometry/point.mdx +0 -38
- package/src/docs/utils/math/geometry/rectangle.mdx +0 -24
- package/src/docs/utils/math/geometry/segment.mdx +0 -55
- package/src/docs/utils/math/geometry/triangle.mdx +0 -22
- package/src/docs/utils/math/linear-algebra/2d.mdx +0 -22
- package/src/docs/utils/math/linear-algebra/quaternion.mdx +0 -21
- package/src/docs/utils/math/linear-algebra/quaternions.mdx +0 -22
- package/src/docs/utils/math/linear-algebra/vector.mdx +0 -177
- package/src/docs/utils/math/linear-algebra/vectors.mdx +0 -58
- package/src/docs/utils/math/numbers.mdx +0 -76
- package/src/docs/utils/math/random.mdx +0 -35
- package/src/docs/utils/math/statistics.mdx +0 -38
- package/src/docs/utils/math/trigonometry.mdx +0 -85
- package/src/docs/utils/physics/friction.mdx +0 -20
- package/src/docs/utils/physics/gravity.mdx +0 -28
- package/src/engine/loop.js +0 -15
- 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/image/hitmask.js +0 -37
- package/src/ui/canvas.js +0 -81
- package/src/ui/react/game/character/index.jsx +0 -30
- package/src/utils/math/geometry/platform.js +0 -42
- package/src/utils/math/geometry/platform.test.js +0 -133
- /package/src/{ui → renderers}/react/game/character/character.module.scss +0 -0
- /package/src/{ui → renderers}/react/game/cursor/cursor.module.scss +0 -0
- /package/src/{ui → renderers}/react/game/form/fields/field/field.module.scss +0 -0
- /package/src/{ui → renderers}/react/game/form/fields/fields.module.scss +0 -0
- /package/src/{ui → renderers}/react/game/form/form.module.scss +0 -0
- /package/src/{ui → renderers}/react/game/platform/platform.module.scss +0 -0
- /package/src/{ui → renderers}/react/game/scene/scene.module.scss +0 -0
- /package/src/{ui → renderers}/react/game/sprite/sprite.module.css +0 -0
- /package/src/{ui → renderers}/react/hocs/with-absolute-position/with-absolute-position.module.scss +0 -0
|
@@ -1,122 +1,122 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @typedef {import('./types').Vector2} Vector2
|
|
3
|
-
* @typedef {import('./types').Vector3} Vector3
|
|
4
|
-
* @typedef {import('./types').Vector7} Vector7
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
import { magnitude, shift } from "./vector.js"
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* Alias for the sum function.
|
|
11
|
-
* @type {typeof sum}
|
|
12
|
-
*/
|
|
13
|
-
export const add = sum
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* Computes the cross product of multiple vectors.
|
|
17
|
-
* @param {...Vector3 | Vector7} vectors - The vectors to compute the cross product for.
|
|
18
|
-
* @returns {Vector3 | Vector7} The resulting vector after the cross product.
|
|
19
|
-
*/
|
|
20
|
-
export function cross(...vectors) {
|
|
21
|
-
return vectors.reduce(crossMultiplyCoordinates)
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* Computes the distance between multiple vectors.
|
|
26
|
-
* @param {...Vector2 | Vector3} vectors - The vectors to compute the distance between.
|
|
27
|
-
* @returns {number} The distance between the vectors.
|
|
28
|
-
*/
|
|
29
|
-
export function distance(...vectors) {
|
|
30
|
-
return magnitude(subtract(...vectors))
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* Computes the dot product of multiple vectors.
|
|
35
|
-
* @param {...Vector2 | Vector3} vectors - The vectors to compute the dot product for.
|
|
36
|
-
* @returns {number} The resulting scalar value of the dot product.
|
|
37
|
-
*/
|
|
38
|
-
export function dot(...vectors) {
|
|
39
|
-
return vectors
|
|
40
|
-
.reduce(dotMultiplyCoordinates)
|
|
41
|
-
.reduce((coord1, coord2) => coord1 + coord2)
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* Alias for the dot product function.
|
|
46
|
-
* @type {typeof dot}
|
|
47
|
-
*/
|
|
48
|
-
export const scalarProduct = dot
|
|
49
|
-
|
|
50
|
-
/**
|
|
51
|
-
* Subtracts multiple vectors.
|
|
52
|
-
* @param {...Vector2 | Vector3} vectors - The vectors to subtract.
|
|
53
|
-
* @returns {Vector2 | Vector3} The resulting vector after subtraction.
|
|
54
|
-
*/
|
|
55
|
-
export function subtract(...vectors) {
|
|
56
|
-
return vectors.reduce(subtractCoordinates)
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
/**
|
|
60
|
-
* Sums multiple vectors.
|
|
61
|
-
* @param {...Vector2 | Vector3} vectors - The vectors to sum.
|
|
62
|
-
* @returns {Vector2 | Vector3} The resulting vector after summation.
|
|
63
|
-
*/
|
|
64
|
-
export function sum(...vectors) {
|
|
65
|
-
return vectors.reduce(sumCoordinates)
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
/**
|
|
69
|
-
* Alias for the cross product function.
|
|
70
|
-
* @type {typeof cross}
|
|
71
|
-
*/
|
|
72
|
-
export const vectorProduct = cross
|
|
73
|
-
|
|
74
|
-
/**
|
|
75
|
-
* Adds the coordinates of two vectors.
|
|
76
|
-
* @param {Vector2 | Vector3} vector1 - The first vector.
|
|
77
|
-
* @param {Vector2 | Vector3} vector2 - The second vector.
|
|
78
|
-
* @returns {Vector2 | Vector3} The resulting vector after addition.
|
|
79
|
-
*/
|
|
80
|
-
function sumCoordinates(vector1, vector2) {
|
|
81
|
-
return vector1.map((coordinate, index) => coordinate + vector2[index])
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
/**
|
|
85
|
-
* Subtracts the coordinates of two vectors.
|
|
86
|
-
* @param {Vector2 | Vector3} vector1 - The first vector.
|
|
87
|
-
* @param {Vector2 | Vector3} vector2 - The second vector.
|
|
88
|
-
* @returns {Vector2 | Vector3} The resulting vector after subtraction.
|
|
89
|
-
*/
|
|
90
|
-
function subtractCoordinates(vector1, vector2) {
|
|
91
|
-
return vector1.map((coordinate, index) => coordinate - vector2[index])
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
/**
|
|
95
|
-
* Computes the cross product of two vectors' coordinates.
|
|
96
|
-
* @param {Vector3} vector1 - The first vector.
|
|
97
|
-
* @param {Vector3} vector2 - The second vector.
|
|
98
|
-
* @returns {Vector3} The resulting vector after the cross product.
|
|
99
|
-
*/
|
|
100
|
-
function crossMultiplyCoordinates(vector1, vector2) {
|
|
101
|
-
const indexes = Array(vector1.length)
|
|
102
|
-
.fill(null)
|
|
103
|
-
.map((_, index) => index)
|
|
104
|
-
|
|
105
|
-
return indexes.map((_, index) => {
|
|
106
|
-
const [index1, index2] = shift(
|
|
107
|
-
indexes.filter((_, i) => i !== index),
|
|
108
|
-
index,
|
|
109
|
-
)
|
|
110
|
-
return vector1[index1] * vector2[index2] - vector1[index2] * vector2[index1]
|
|
111
|
-
})
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
/**
|
|
115
|
-
* Multiplies the coordinates of two vectors.
|
|
116
|
-
* @param {Vector2 | Vector3} vector1 - The first vector.
|
|
117
|
-
* @param {Vector2 | Vector3} vector2 - The second vector.
|
|
118
|
-
* @returns {Vector2 | Vector3} The resulting vector containing multiplied coordinates.
|
|
119
|
-
*/
|
|
120
|
-
function dotMultiplyCoordinates(vector1, vector2) {
|
|
121
|
-
return vector1.map((coordinate, index) => coordinate * vector2[index])
|
|
122
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* @typedef {import('./types').Vector2} Vector2
|
|
3
|
+
* @typedef {import('./types').Vector3} Vector3
|
|
4
|
+
* @typedef {import('./types').Vector7} Vector7
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { magnitude, shift } from "./vector.js"
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Alias for the sum function.
|
|
11
|
+
* @type {typeof sum}
|
|
12
|
+
*/
|
|
13
|
+
export const add = sum
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Computes the cross product of multiple vectors.
|
|
17
|
+
* @param {...Vector3 | Vector7} vectors - The vectors to compute the cross product for.
|
|
18
|
+
* @returns {Vector3 | Vector7} The resulting vector after the cross product.
|
|
19
|
+
*/
|
|
20
|
+
export function cross(...vectors) {
|
|
21
|
+
return vectors.reduce(crossMultiplyCoordinates)
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Computes the distance between multiple vectors.
|
|
26
|
+
* @param {...Vector2 | Vector3} vectors - The vectors to compute the distance between.
|
|
27
|
+
* @returns {number} The distance between the vectors.
|
|
28
|
+
*/
|
|
29
|
+
export function distance(...vectors) {
|
|
30
|
+
return magnitude(subtract(...vectors))
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Computes the dot product of multiple vectors.
|
|
35
|
+
* @param {...Vector2 | Vector3} vectors - The vectors to compute the dot product for.
|
|
36
|
+
* @returns {number} The resulting scalar value of the dot product.
|
|
37
|
+
*/
|
|
38
|
+
export function dot(...vectors) {
|
|
39
|
+
return vectors
|
|
40
|
+
.reduce(dotMultiplyCoordinates)
|
|
41
|
+
.reduce((coord1, coord2) => coord1 + coord2)
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Alias for the dot product function.
|
|
46
|
+
* @type {typeof dot}
|
|
47
|
+
*/
|
|
48
|
+
export const scalarProduct = dot
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Subtracts multiple vectors.
|
|
52
|
+
* @param {...Vector2 | Vector3} vectors - The vectors to subtract.
|
|
53
|
+
* @returns {Vector2 | Vector3} The resulting vector after subtraction.
|
|
54
|
+
*/
|
|
55
|
+
export function subtract(...vectors) {
|
|
56
|
+
return vectors.reduce(subtractCoordinates)
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Sums multiple vectors.
|
|
61
|
+
* @param {...Vector2 | Vector3} vectors - The vectors to sum.
|
|
62
|
+
* @returns {Vector2 | Vector3} The resulting vector after summation.
|
|
63
|
+
*/
|
|
64
|
+
export function sum(...vectors) {
|
|
65
|
+
return vectors.reduce(sumCoordinates)
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Alias for the cross product function.
|
|
70
|
+
* @type {typeof cross}
|
|
71
|
+
*/
|
|
72
|
+
export const vectorProduct = cross
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Adds the coordinates of two vectors.
|
|
76
|
+
* @param {Vector2 | Vector3} vector1 - The first vector.
|
|
77
|
+
* @param {Vector2 | Vector3} vector2 - The second vector.
|
|
78
|
+
* @returns {Vector2 | Vector3} The resulting vector after addition.
|
|
79
|
+
*/
|
|
80
|
+
function sumCoordinates(vector1, vector2) {
|
|
81
|
+
return vector1.map((coordinate, index) => coordinate + vector2[index])
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Subtracts the coordinates of two vectors.
|
|
86
|
+
* @param {Vector2 | Vector3} vector1 - The first vector.
|
|
87
|
+
* @param {Vector2 | Vector3} vector2 - The second vector.
|
|
88
|
+
* @returns {Vector2 | Vector3} The resulting vector after subtraction.
|
|
89
|
+
*/
|
|
90
|
+
function subtractCoordinates(vector1, vector2) {
|
|
91
|
+
return vector1.map((coordinate, index) => coordinate - vector2[index])
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Computes the cross product of two vectors' coordinates.
|
|
96
|
+
* @param {Vector3} vector1 - The first vector.
|
|
97
|
+
* @param {Vector3} vector2 - The second vector.
|
|
98
|
+
* @returns {Vector3} The resulting vector after the cross product.
|
|
99
|
+
*/
|
|
100
|
+
function crossMultiplyCoordinates(vector1, vector2) {
|
|
101
|
+
const indexes = Array(vector1.length)
|
|
102
|
+
.fill(null)
|
|
103
|
+
.map((_, index) => index)
|
|
104
|
+
|
|
105
|
+
return indexes.map((_, index) => {
|
|
106
|
+
const [index1, index2] = shift(
|
|
107
|
+
indexes.filter((_, i) => i !== index),
|
|
108
|
+
index,
|
|
109
|
+
)
|
|
110
|
+
return vector1[index1] * vector2[index2] - vector1[index2] * vector2[index1]
|
|
111
|
+
})
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Multiplies the coordinates of two vectors.
|
|
116
|
+
* @param {Vector2 | Vector3} vector1 - The first vector.
|
|
117
|
+
* @param {Vector2 | Vector3} vector2 - The second vector.
|
|
118
|
+
* @returns {Vector2 | Vector3} The resulting vector containing multiplied coordinates.
|
|
119
|
+
*/
|
|
120
|
+
function dotMultiplyCoordinates(vector1, vector2) {
|
|
121
|
+
return vector1.map((coordinate, index) => coordinate * vector2[index])
|
|
122
|
+
}
|
|
@@ -1,65 +1,65 @@
|
|
|
1
|
-
import { expect, test } from "vitest"
|
|
2
|
-
|
|
3
|
-
import { cross, distance, dot, subtract, sum } from "./vectors.js"
|
|
4
|
-
|
|
5
|
-
test("it should compute the cross product between two vectors (aka vectorProduct)", () => {
|
|
6
|
-
const vector1 = [1, 2, 3]
|
|
7
|
-
const vector2 = [4, 5, 6]
|
|
8
|
-
const expectedResult = [-3, 6, -3]
|
|
9
|
-
|
|
10
|
-
expect(cross(vector1, vector2)).toStrictEqual(expectedResult)
|
|
11
|
-
})
|
|
12
|
-
|
|
13
|
-
test("it should compute the cross product of multiple vectors (aka vectorProduct)", () => {
|
|
14
|
-
const vectors = [
|
|
15
|
-
[1, 2, 3],
|
|
16
|
-
[4, 5, 6],
|
|
17
|
-
[7, 8, 9],
|
|
18
|
-
]
|
|
19
|
-
const expectedResult = [78, 6, -66]
|
|
20
|
-
|
|
21
|
-
expect(cross(...vectors)).toStrictEqual(expectedResult)
|
|
22
|
-
})
|
|
23
|
-
|
|
24
|
-
test("it should compute the distance between multiple vectors", () => {
|
|
25
|
-
const vectors = [
|
|
26
|
-
[0, 0, 0],
|
|
27
|
-
[4, 0, 3],
|
|
28
|
-
]
|
|
29
|
-
const expectedResult = 5
|
|
30
|
-
|
|
31
|
-
expect(distance(...vectors)).toStrictEqual(expectedResult)
|
|
32
|
-
})
|
|
33
|
-
|
|
34
|
-
test("it should compute the dot product of multiple vectors (aka scalarProduct)", () => {
|
|
35
|
-
const vectors = [
|
|
36
|
-
[1, 2, 3],
|
|
37
|
-
[4, 5, 6],
|
|
38
|
-
[7, 8, 9],
|
|
39
|
-
]
|
|
40
|
-
const expectedResult = 270
|
|
41
|
-
|
|
42
|
-
expect(dot(...vectors)).toStrictEqual(expectedResult)
|
|
43
|
-
})
|
|
44
|
-
|
|
45
|
-
test("it should subtract multiple vectors", () => {
|
|
46
|
-
const vectors = [
|
|
47
|
-
[1, 2, 3],
|
|
48
|
-
[4, 5, 6],
|
|
49
|
-
[7, 8, 9],
|
|
50
|
-
]
|
|
51
|
-
const expectedResult = [-10, -11, -12]
|
|
52
|
-
|
|
53
|
-
expect(subtract(...vectors)).toStrictEqual(expectedResult)
|
|
54
|
-
})
|
|
55
|
-
|
|
56
|
-
test("it should sum multiple vectors (aka add)", () => {
|
|
57
|
-
const vectors = [
|
|
58
|
-
[1, 2, 3],
|
|
59
|
-
[4, 5, 6],
|
|
60
|
-
[7, 8, 9],
|
|
61
|
-
]
|
|
62
|
-
const expectedResult = [12, 15, 18]
|
|
63
|
-
|
|
64
|
-
expect(sum(...vectors)).toStrictEqual(expectedResult)
|
|
65
|
-
})
|
|
1
|
+
import { expect, test } from "vitest"
|
|
2
|
+
|
|
3
|
+
import { cross, distance, dot, subtract, sum } from "./vectors.js"
|
|
4
|
+
|
|
5
|
+
test("it should compute the cross product between two vectors (aka vectorProduct)", () => {
|
|
6
|
+
const vector1 = [1, 2, 3]
|
|
7
|
+
const vector2 = [4, 5, 6]
|
|
8
|
+
const expectedResult = [-3, 6, -3]
|
|
9
|
+
|
|
10
|
+
expect(cross(vector1, vector2)).toStrictEqual(expectedResult)
|
|
11
|
+
})
|
|
12
|
+
|
|
13
|
+
test("it should compute the cross product of multiple vectors (aka vectorProduct)", () => {
|
|
14
|
+
const vectors = [
|
|
15
|
+
[1, 2, 3],
|
|
16
|
+
[4, 5, 6],
|
|
17
|
+
[7, 8, 9],
|
|
18
|
+
]
|
|
19
|
+
const expectedResult = [78, 6, -66]
|
|
20
|
+
|
|
21
|
+
expect(cross(...vectors)).toStrictEqual(expectedResult)
|
|
22
|
+
})
|
|
23
|
+
|
|
24
|
+
test("it should compute the distance between multiple vectors", () => {
|
|
25
|
+
const vectors = [
|
|
26
|
+
[0, 0, 0],
|
|
27
|
+
[4, 0, 3],
|
|
28
|
+
]
|
|
29
|
+
const expectedResult = 5
|
|
30
|
+
|
|
31
|
+
expect(distance(...vectors)).toStrictEqual(expectedResult)
|
|
32
|
+
})
|
|
33
|
+
|
|
34
|
+
test("it should compute the dot product of multiple vectors (aka scalarProduct)", () => {
|
|
35
|
+
const vectors = [
|
|
36
|
+
[1, 2, 3],
|
|
37
|
+
[4, 5, 6],
|
|
38
|
+
[7, 8, 9],
|
|
39
|
+
]
|
|
40
|
+
const expectedResult = 270
|
|
41
|
+
|
|
42
|
+
expect(dot(...vectors)).toStrictEqual(expectedResult)
|
|
43
|
+
})
|
|
44
|
+
|
|
45
|
+
test("it should subtract multiple vectors", () => {
|
|
46
|
+
const vectors = [
|
|
47
|
+
[1, 2, 3],
|
|
48
|
+
[4, 5, 6],
|
|
49
|
+
[7, 8, 9],
|
|
50
|
+
]
|
|
51
|
+
const expectedResult = [-10, -11, -12]
|
|
52
|
+
|
|
53
|
+
expect(subtract(...vectors)).toStrictEqual(expectedResult)
|
|
54
|
+
})
|
|
55
|
+
|
|
56
|
+
test("it should sum multiple vectors (aka add)", () => {
|
|
57
|
+
const vectors = [
|
|
58
|
+
[1, 2, 3],
|
|
59
|
+
[4, 5, 6],
|
|
60
|
+
[7, 8, 9],
|
|
61
|
+
]
|
|
62
|
+
const expectedResult = [12, 15, 18]
|
|
63
|
+
|
|
64
|
+
expect(sum(...vectors)).toStrictEqual(expectedResult)
|
|
65
|
+
})
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Performs linear interpolation between two numbers.
|
|
3
|
+
*
|
|
4
|
+
* @param {number} a - The start value.
|
|
5
|
+
* @param {number} b - The end value.
|
|
6
|
+
* @param {number} t - The interpolation factor, typically in the range [0, 1].
|
|
7
|
+
* @returns {number} The interpolated value.
|
|
8
|
+
*/
|
|
9
|
+
export const lerp = (a, b, t) => a + (b - a) * t
|
|
@@ -1,90 +1,90 @@
|
|
|
1
|
-
const DEFAULT_PRECISION = 1 // Default precision used for snapping numbers.
|
|
2
|
-
const DEFAULT_TOLERANCE = 0.1 // Default tolerance used for comparing numbers.
|
|
3
|
-
const SQUARE_ROOT = 0.5 // Exponent used for calculating square roots.
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Returns the absolute value of a number.
|
|
7
|
-
* @param {number} num - The number to get the absolute value of.
|
|
8
|
-
* @returns {number} The absolute value of the input number.
|
|
9
|
-
*/
|
|
10
|
-
export function abs(num) {
|
|
11
|
-
return Math.abs(num)
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* Clamps a number within the inclusive range specified by min and max.
|
|
16
|
-
* @param {number} num - The number to clamp.
|
|
17
|
-
* @param {number} min - The minimum value.
|
|
18
|
-
* @param {number} max - The maximum value.
|
|
19
|
-
* @returns {number} The clamped value.
|
|
20
|
-
*/
|
|
21
|
-
export function clamp(num, min, max) {
|
|
22
|
-
if (num < min) {
|
|
23
|
-
return min
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
if (num > max) {
|
|
27
|
-
return max
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
return num
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* Checks if two numbers are close to each other within a given tolerance.
|
|
35
|
-
* @param {number} num1 - The first number.
|
|
36
|
-
* @param {number} num2 - The second number.
|
|
37
|
-
* @param {number} [tolerance=DEFAULT_TOLERANCE] - The tolerance for comparison.
|
|
38
|
-
* @returns {boolean} True if the numbers are close, false otherwise.
|
|
39
|
-
*/
|
|
40
|
-
export function isClose(num1, num2, tolerance = DEFAULT_TOLERANCE) {
|
|
41
|
-
return abs(num1 - num2) <= tolerance
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* Computes the modulus of two numbers, ensuring a positive result.
|
|
46
|
-
* @param {number} dividend - The number to be divided.
|
|
47
|
-
* @param {number} divisor - The number to divide by.
|
|
48
|
-
* @returns {number} The modulus result.
|
|
49
|
-
*/
|
|
50
|
-
export function mod(dividend, divisor) {
|
|
51
|
-
return ((dividend % divisor) + divisor) % divisor
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* Snaps a number to the nearest multiple of a given precision.
|
|
56
|
-
* @param {number} num - The number to snap.
|
|
57
|
-
* @param {number} [precision=DEFAULT_PRECISION] - The precision to snap to.
|
|
58
|
-
* @returns {number} The snapped value.
|
|
59
|
-
*/
|
|
60
|
-
export function snap(num, precision = DEFAULT_PRECISION) {
|
|
61
|
-
return Math.round(num / precision) * precision
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
/**
|
|
65
|
-
* Alias for the `mod` function.
|
|
66
|
-
* @type {typeof mod}
|
|
67
|
-
*/
|
|
68
|
-
export const remainder = mod
|
|
69
|
-
|
|
70
|
-
/**
|
|
71
|
-
* Returns the sign of a number: 1 for positive, -1 for negative, and 0 for zero.
|
|
72
|
-
* @param {number} num - The number to get the sign of.
|
|
73
|
-
* @returns {number} The sign of the number.
|
|
74
|
-
*/
|
|
75
|
-
export function sign(num) {
|
|
76
|
-
if (!num) {
|
|
77
|
-
return num
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
return num / abs(num)
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
/**
|
|
84
|
-
* Computes the square root of a number.
|
|
85
|
-
* @param {number} num - The number to compute the square root of.
|
|
86
|
-
* @returns {number} The square root of the number.
|
|
87
|
-
*/
|
|
88
|
-
export function sqrt(num) {
|
|
89
|
-
return num ** SQUARE_ROOT
|
|
90
|
-
}
|
|
1
|
+
const DEFAULT_PRECISION = 1 // Default precision used for snapping numbers.
|
|
2
|
+
const DEFAULT_TOLERANCE = 0.1 // Default tolerance used for comparing numbers.
|
|
3
|
+
const SQUARE_ROOT = 0.5 // Exponent used for calculating square roots.
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Returns the absolute value of a number.
|
|
7
|
+
* @param {number} num - The number to get the absolute value of.
|
|
8
|
+
* @returns {number} The absolute value of the input number.
|
|
9
|
+
*/
|
|
10
|
+
export function abs(num) {
|
|
11
|
+
return Math.abs(num)
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Clamps a number within the inclusive range specified by min and max.
|
|
16
|
+
* @param {number} num - The number to clamp.
|
|
17
|
+
* @param {number} min - The minimum value.
|
|
18
|
+
* @param {number} max - The maximum value.
|
|
19
|
+
* @returns {number} The clamped value.
|
|
20
|
+
*/
|
|
21
|
+
export function clamp(num, min, max) {
|
|
22
|
+
if (num < min) {
|
|
23
|
+
return min
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
if (num > max) {
|
|
27
|
+
return max
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
return num
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Checks if two numbers are close to each other within a given tolerance.
|
|
35
|
+
* @param {number} num1 - The first number.
|
|
36
|
+
* @param {number} num2 - The second number.
|
|
37
|
+
* @param {number} [tolerance=DEFAULT_TOLERANCE] - The tolerance for comparison.
|
|
38
|
+
* @returns {boolean} True if the numbers are close, false otherwise.
|
|
39
|
+
*/
|
|
40
|
+
export function isClose(num1, num2, tolerance = DEFAULT_TOLERANCE) {
|
|
41
|
+
return abs(num1 - num2) <= tolerance
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Computes the modulus of two numbers, ensuring a positive result.
|
|
46
|
+
* @param {number} dividend - The number to be divided.
|
|
47
|
+
* @param {number} divisor - The number to divide by.
|
|
48
|
+
* @returns {number} The modulus result.
|
|
49
|
+
*/
|
|
50
|
+
export function mod(dividend, divisor) {
|
|
51
|
+
return ((dividend % divisor) + divisor) % divisor
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Snaps a number to the nearest multiple of a given precision.
|
|
56
|
+
* @param {number} num - The number to snap.
|
|
57
|
+
* @param {number} [precision=DEFAULT_PRECISION] - The precision to snap to.
|
|
58
|
+
* @returns {number} The snapped value.
|
|
59
|
+
*/
|
|
60
|
+
export function snap(num, precision = DEFAULT_PRECISION) {
|
|
61
|
+
return Math.round(num / precision) * precision
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Alias for the `mod` function.
|
|
66
|
+
* @type {typeof mod}
|
|
67
|
+
*/
|
|
68
|
+
export const remainder = mod
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Returns the sign of a number: 1 for positive, -1 for negative, and 0 for zero.
|
|
72
|
+
* @param {number} num - The number to get the sign of.
|
|
73
|
+
* @returns {number} The sign of the number.
|
|
74
|
+
*/
|
|
75
|
+
export function sign(num) {
|
|
76
|
+
if (!num) {
|
|
77
|
+
return num
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
return num / abs(num)
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Computes the square root of a number.
|
|
85
|
+
* @param {number} num - The number to compute the square root of.
|
|
86
|
+
* @returns {number} The square root of the number.
|
|
87
|
+
*/
|
|
88
|
+
export function sqrt(num) {
|
|
89
|
+
return num ** SQUARE_ROOT
|
|
90
|
+
}
|