-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.ts
294 lines (277 loc) · 6.32 KB
/
utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
import { IPoint, IPoint3, MapThree } from "./interfaces";
/**
* Classic projection found in most games
* @date 3/14/2023 - 12:21:46 PM
*
* @export
* @type {number}
*/
export const CLASSIC = Math.atan(0.5);
/**
* Isometric projection
* @date 3/14/2023 - 12:23:04 PM
*
* @export
* @type {number}
*/
export const ISOMETRIC = Math.PI / 6;
/**
* Military projection
* @date 3/14/2023 - 12:23:06 PM
*
* @export
* @type {number}
*/
export const MILITARY = Math.PI / 4;
/**
* Point specifying top left placement factor
* @date 3/14/2023 - 12:23:06 PM
*
* @export
* @type {IPoint}
*/
export const TOP_LEFT: IPoint = { x: 0, y: 0 };
/**
* Point specifying middle placement factor
* @date 3/14/2023 - 12:23:06 PM
*
* @export
* @type {IPoint}
*/
export const MIDDLE: IPoint = { x: 0.5, y: 0.5 };
/**
* Point specifying a factor of 1 for both axes
* @date 3/14/2023 - 12:23:06 PM
*
* @export
* @type {IPoint}
*/
export const FULL: IPoint = { x: 1, y: 1 };
/**
* Enum specifying compass directions, including NONE
* @date 3/14/2023 - 12:23:06 PM
*
* @export
* @enum {DIRECTION}
*/
export enum DIRECTION {
NONE = "NONE",
N = "N",
NE = "NE",
E = "E",
SE = "SE",
S = "S",
SW = "SW",
W = "W",
NW = "NW",
}
/**
* Given a three-dimensional map, return a one dimensional array sorted by ascending z -> x -> y containing values at those paths
* @date 3/14/2023 - 12:23:06 PM
*
* @export
* @template T
* @param {MapThree<T>} grids
* @returns {T[]}
*
* @example
*
* const [furthest, middle, ...etc, closest] = collectRay(3dmap)
*/
export function collectRay<T>(grids: MapThree<T>): T[] {
const entries: T[] = [];
for (const z of Array.from(grids.keys()).sort()) {
const zPlane = grids.get(z);
for (const x of Array.from(zPlane.keys()).sort()) {
const xPlane = zPlane.get(x);
for (const y of Array.from(xPlane.keys()).sort()) {
entries.push(xPlane.get(y));
}
}
}
return entries;
}
/**
* Deeply set a value in object at given indices using maps for new path creation
* @date 3/14/2023 - 12:23:06 PM
*
* @export
* @template T
* @param {Map<number, any>} obj
* @param {number[]} indices
* @param {T} value
* @returns {T}
*
* @example
*
* const tile = set(new Map(), [0, 1, 2], sprite)
*/
export function set<T>(obj: Map<number, any>, indices: number[], value: T): T {
let o = obj;
while (indices.length - 1) {
var n = indices.shift();
let v = o.get(n);
if (v === undefined) {
const m = new Map();
o.set(n, m);
o = m;
} else {
o = v;
}
}
o.set(indices[0], value);
return value;
}
/**
* Deeply get a value from a map at given indicies path.
* If `setIfNull` is provided, new maps will be used for path creation and the value will be set.
* @date 3/14/2023 - 12:23:06 PM
*
* @export
* @template T
* @param {Map<number, any>} map
* @param {number[]} indices
* @param {?T} [setIfNull]
* @returns {T}
*
* @example
*
* const tile = get(new Map(), [0, 1, 2], sprite)
*/
export function get<T>(
map: Map<number, any>,
indices: number[],
setIfNull?: T
): T {
var o = map;
while (indices.length) {
var n = indices.shift();
let v = o.get(n);
if (v === undefined) {
if (setIfNull && !indices.length) {
o.set(n, setIfNull);
return setIfNull;
} else if (setIfNull) {
v = new Map();
o.set(n, v);
} else {
return;
}
}
o = v;
}
return o as T;
}
/**
* Get a value from a three-dimensional map with a point
* @date 3/14/2023 - 12:35:21 PM
*
* @export
* @template T
* @param {MapThree<T>} map
* @param {IPoint3} point
* @returns {T}
*
* @example
*
* const v = pointGet(mapThree, { x: 1, y: 1, z: 1 })
*/
export function pointGet<T>(map: MapThree<T>, point: IPoint3): T {
return get<T>(map, [point.z || 0, point.x, point.y]);
}
/**
* Deeply remove a value in map at given indices
* @date 3/14/2023 - 12:23:06 PM
*
* @export
* @param {Map<number, any>} obj
* @param {number[]} indices
* @returns {boolean}
*
* @example
*
* const removed = remove(new Map(), [0, 1, 2])
*/
export function remove(obj: Map<number, any>, indices: number[]): boolean {
let o = obj;
while (indices.length - 1) {
var n = indices.shift();
let v = o.get(n);
if (v === undefined) return false;
else o = v;
}
return o.delete(indices[0]);
}
/**
* Retrieves distance between two 2d points
* @date 3/14/2023 - 12:23:06 PM
*
* @export
* @param {IPoint} from
* @param {IPoint} to
* @returns {number}
*
* @example
*
* const dist = getDistance({ x: 5, y: 5 }, { x: 10, y: 10 })
*/
export function getDistance(from: IPoint, to: IPoint): number {
return Math.sqrt(
(to.x - from.x) * (to.x - from.x) + (to.y - from.y) * (to.y - from.y)
);
}
/**
* Returns the 2D center of a list of points. This value is not rounded and can be floating point.
* @date 4/12/2023 - 9:01:37 AM
*
* @export
* @param {IPoint[]} points
* @returns {IPoint}
*/
export function getCenter(points: IPoint[]): IPoint {
return {
x: sum(points.map(p => p.x)) / points.length,
y: sum(points.map(p => p.y)) / points.length
}
}
/**
* Sums an array of numbers, ignoring null and undefined
* @date 3/14/2023 - 12:28:23 PM
*
* @export
* @param {(number | null | undefined)[]} numbers
* @returns {number}
*
* @example
* const total = sum([ 1, 10, null, 5 ])
*/
export function sum(numbers: (number | null | undefined)[]): number {
return numbers.reduce((s, n) => (!!n ? s + n : s), 0);
}
const ZERO = 0;
/**
* Gets compass direction between two points
* @date 3/14/2023 - 12:23:06 PM
*
* @export
* @param {IPoint3} from
* @param {IPoint3} to
* @returns {DIRECTION}
*
* @example
*
* getDirection({ x: 1, y: 1 }, { x: 1: y: 0 }) === DIRECTION.N
*/
export function getDirection(from: IPoint3, to: IPoint3): DIRECTION {
let diffX = to.x - from.x,
diffY = to.y - from.y;
if (diffX === ZERO && diffY < ZERO) return DIRECTION.N;
if (diffX > ZERO && diffY < ZERO) return DIRECTION.NE;
if (diffX > ZERO && diffY === ZERO) return DIRECTION.E;
if (diffX > ZERO && diffY > ZERO) return DIRECTION.SE;
if (diffX === ZERO && diffY > ZERO) return DIRECTION.S;
if (diffX < ZERO && diffY > ZERO) return DIRECTION.SW;
if (diffX < ZERO && diffY === ZERO) return DIRECTION.W;
if (diffX < ZERO && diffY < ZERO) return DIRECTION.NW;
return DIRECTION.NONE;
}