mirror of
https://github.com/Bubberstation/Bubberstation.git
synced 2026-08-26 14:39:58 +01:00
## About The Pull Request This PR has no player-facing changes. Closes #83161 Replaces several `collection.ts` functions with functions from the `es-toolkit` package. ### Migration <table> <tr> <th width="100px">Function</th> <th>Notes</th> </tr> <tr> <td> `filter` </td> <td rowspan="3"> Replace `common/collections` import with `es-toolkit/compat` </td> </tr> <tr> <td> `map` </td> </tr> <tr> <td> `reduce` </td> </tr> <tr> <td> `uniqBy` </td> <td rowspan="5"> Replace `common/collections` import with `es-toolkit` </td> </tr> <tr> <td> `uniq` </td> </tr> <tr> <td> `zip` </td> </tr> <tr> <td> `range` </td> </tr> <tr> <td> `sortBy` </td> </tr> <tr> <td> `paginate` </td> <td> Replace with `chunk` from `es-toolkit` </td> </tr> <tr> <td> `deepMerge` </td> <td> Replace with `toMerged` from `es-toolkit` </td> </tr> <tr> <td> `sort` </td> <td> This function is broken because it leaves the input array unchanged. Replace with es-toolkit's `sortBy` or the native `Array.prototype.sort` depending on context </td> </tr> </table> ## Why It's Good For The Game Reduced maintenance costs --------- Co-authored-by: Arthri <41360489+a@users.noreply.github.com>
56 lines
1.6 KiB
TypeScript
56 lines
1.6 KiB
TypeScript
/**
|
|
* N-dimensional vector manipulation functions.
|
|
*
|
|
* Vectors are plain number arrays, i.e. [x, y, z].
|
|
*
|
|
* @file
|
|
* @copyright 2020 Aleksej Komarov
|
|
* @license MIT
|
|
*/
|
|
|
|
import { zip } from 'es-toolkit';
|
|
import { map, reduce } from 'es-toolkit/compat';
|
|
|
|
const ADD = (a: number, b: number): number => a + b;
|
|
const SUB = (a: number, b: number): number => a - b;
|
|
const MUL = (a: number, b: number): number => a * b;
|
|
const DIV = (a: number, b: number): number => a / b;
|
|
|
|
export type Vector = number[];
|
|
|
|
// It's really not ideal to bypass the type system and use `as Vector`
|
|
// however, there isn't a more eloquent way to type these
|
|
|
|
export const vecAdd = (...vecs: Vector[]): Vector => {
|
|
return map(zip<number>(...vecs) as Vector[], (x) => reduce(x, ADD)) as Vector;
|
|
};
|
|
|
|
export const vecSubtract = (...vecs: Vector[]): Vector => {
|
|
return map(zip<number>(...vecs) as Vector[], (x) => reduce(x, SUB)) as Vector;
|
|
};
|
|
|
|
export const vecMultiply = (...vecs: Vector[]): Vector => {
|
|
return map(zip<number>(...vecs) as Vector[], (x) => reduce(x, MUL)) as Vector;
|
|
};
|
|
|
|
export const vecDivide = (...vecs: Vector[]): Vector => {
|
|
return map(zip<number>(...vecs) as Vector[], (x) => reduce(x, DIV)) as Vector;
|
|
};
|
|
|
|
export const vecScale = (vec: Vector, n: number): Vector => {
|
|
return map(vec, (x) => x * n);
|
|
};
|
|
|
|
export const vecInverse = (vec: Vector): Vector => {
|
|
return map(vec, (x) => -x);
|
|
};
|
|
|
|
export const vecLength = (vec: Vector): number => {
|
|
return Math.sqrt(reduce(vecMultiply(vec, vec), ADD) as number);
|
|
};
|
|
|
|
export const vecNormalize = (vec: Vector): Vector => {
|
|
const length = vecLength(vec);
|
|
return map(vec, (c) => c / length);
|
|
};
|