@vyr/engine 0.0.1

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.
Files changed (95) hide show
  1. package/package.json +19 -0
  2. package/src/ArrayUtils.ts +65 -0
  3. package/src/AsyncTask.ts +72 -0
  4. package/src/Category.ts +119 -0
  5. package/src/Color.ts +111 -0
  6. package/src/Engine.ts +101 -0
  7. package/src/Generate.ts +40 -0
  8. package/src/InputSystem.ts +108 -0
  9. package/src/Listener.ts +59 -0
  10. package/src/ObjectPool.ts +84 -0
  11. package/src/ObjectUtils.ts +49 -0
  12. package/src/Scriptable.ts +27 -0
  13. package/src/Serialization.ts +49 -0
  14. package/src/Traverser.ts +39 -0
  15. package/src/actor/Actor.ts +28 -0
  16. package/src/actor/AnimationUnitActor.ts +289 -0
  17. package/src/actor/DivActor.ts +70 -0
  18. package/src/actor/FragmentActor.ts +56 -0
  19. package/src/actor/HTMActor.ts +166 -0
  20. package/src/actor/HTMServiceActor.ts +57 -0
  21. package/src/actor/HTMTransformControllerActor.ts +404 -0
  22. package/src/actor/StyleActor.ts +96 -0
  23. package/src/actor/index.ts +8 -0
  24. package/src/asset/Asset.ts +271 -0
  25. package/src/asset/AssetGraph.ts +246 -0
  26. package/src/asset/index.ts +2 -0
  27. package/src/descriptor/AnimationUnitDescriptor.ts +65 -0
  28. package/src/descriptor/CameraDescriptor.ts +12 -0
  29. package/src/descriptor/ControllerDescriptor.ts +16 -0
  30. package/src/descriptor/DatasetDescriptor.ts +92 -0
  31. package/src/descriptor/Descriptor.ts +415 -0
  32. package/src/descriptor/DivDescriptor.ts +18 -0
  33. package/src/descriptor/DynamicDescriptor.ts +27 -0
  34. package/src/descriptor/HTMLDescriptor.ts +87 -0
  35. package/src/descriptor/HTMLServiceDescriptor.ts +19 -0
  36. package/src/descriptor/HTMLTransformControllerDescriptor.ts +34 -0
  37. package/src/descriptor/NodeDescriptor.ts +32 -0
  38. package/src/descriptor/PrefabDescriptor.ts +53 -0
  39. package/src/descriptor/PrefabInstanceDescriptor.ts +32 -0
  40. package/src/descriptor/RoutineDescriptor.ts +54 -0
  41. package/src/descriptor/ServiceDescriptor.ts +32 -0
  42. package/src/descriptor/ServiceSchedulerDescriptor.ts +32 -0
  43. package/src/descriptor/StyleDescriptor.ts +213 -0
  44. package/src/descriptor/index.ts +17 -0
  45. package/src/graphics/Collection.ts +25 -0
  46. package/src/graphics/Compilation.ts +82 -0
  47. package/src/graphics/Graphics.ts +475 -0
  48. package/src/graphics/Observer.ts +36 -0
  49. package/src/graphics/Unit.ts +83 -0
  50. package/src/graphics/VariableProxy.ts +92 -0
  51. package/src/graphics/index.ts +5 -0
  52. package/src/index.ts +26 -0
  53. package/src/interpreter/AnimationUnitInterpreter.ts +53 -0
  54. package/src/interpreter/DatasetInterpreter.ts +11 -0
  55. package/src/interpreter/DivInterpreter.ts +44 -0
  56. package/src/interpreter/DynamicInterpreter.ts +207 -0
  57. package/src/interpreter/FragmentInterpreter.ts +34 -0
  58. package/src/interpreter/HTMLServiceInterpreter.ts +47 -0
  59. package/src/interpreter/HTMLTransformControllerInterpreter.ts +40 -0
  60. package/src/interpreter/Interpreter.ts +69 -0
  61. package/src/interpreter/PrefaInterpreter.ts +11 -0
  62. package/src/interpreter/PrefabInstanceInterpreter.ts +12 -0
  63. package/src/interpreter/RoutineInterpreter.ts +88 -0
  64. package/src/interpreter/ServiceInterpreter.ts +24 -0
  65. package/src/interpreter/ServiceSchedulerInterpreter.ts +42 -0
  66. package/src/interpreter/StyleInterpreter.ts +66 -0
  67. package/src/interpreter/index.ts +14 -0
  68. package/src/locale/Language.ts +10 -0
  69. package/src/locale/LanguageProvider.ts +48 -0
  70. package/src/locale/index.ts +2 -0
  71. package/src/math/Euler.ts +303 -0
  72. package/src/math/Matrix4.ts +1123 -0
  73. package/src/math/Quaternion.ts +737 -0
  74. package/src/math/Vector2.ts +680 -0
  75. package/src/math/Vector3.ts +1062 -0
  76. package/src/math/index.ts +5 -0
  77. package/src/math/utils.ts +17 -0
  78. package/src/preset/execute/dataset/index.ts +1 -0
  79. package/src/preset/execute/dataset/update.ts +52 -0
  80. package/src/preset/execute/graphics/index.ts +1 -0
  81. package/src/preset/execute/graphics/invoke.ts +49 -0
  82. package/src/preset/execute/index.ts +4 -0
  83. package/src/preset/execute/net/index.ts +1 -0
  84. package/src/preset/execute/net/request.ts +103 -0
  85. package/src/preset/execute/scheduler/index.ts +1 -0
  86. package/src/preset/execute/scheduler/switch.ts +46 -0
  87. package/src/preset/index.ts +7 -0
  88. package/src/preset/routine/graphics/index.ts +1 -0
  89. package/src/preset/routine/graphics/invoke.ts +27 -0
  90. package/src/preset/routine/index.ts +2 -0
  91. package/src/preset/routine/scheduler/index.ts +1 -0
  92. package/src/preset/routine/scheduler/switch.ts +27 -0
  93. package/src/setup/index.ts +17 -0
  94. package/src/utils/AssetProvider.ts +72 -0
  95. package/src/utils/index.ts +1 -0
@@ -0,0 +1,680 @@
1
+ import { DeserializationObject } from "../Serialization"
2
+ import { clamp } from './utils';
3
+
4
+ /**
5
+ * Class representing a 2D vector. A 2D vector is an ordered pair of numbers
6
+ * (labeled x and y), which can be used to represent a number of things, such as:
7
+ *
8
+ * - A point in 2D space (i.e. a position on a plane).
9
+ * - A direction and length across a plane. In three.js the length will
10
+ * always be the Euclidean distance(straight-line distance) from `(0, 0)` to `(x, y)`
11
+ * and the direction is also measured from `(0, 0)` towards `(x, y)`.
12
+ * - Any arbitrary ordered pair of numbers.
13
+ *
14
+ * There are other things a 2D vector can be used to represent, such as
15
+ * momentum vectors, complex numbers and so on, however these are the most
16
+ * common uses in three.js.
17
+ *
18
+ * Iterating through a vector instance will yield its components `(x, y)` in
19
+ * the corresponding order.
20
+ * ```js
21
+ * const a = new Vector2( 0, 1 );
22
+ *
23
+ * //no arguments; will be initialised to (0, 0)
24
+ * const b = new Vector2( );
25
+ *
26
+ * const d = a.distanceTo( b );
27
+ * ```
28
+ */
29
+ class Vector2 {
30
+ static create(v2?: number | DeserializationObject<Vector2>, y?: number) {
31
+ if (v2 === undefined) return new Vector2()
32
+
33
+ if (typeof v2 === 'number') {
34
+ return new Vector2(v2 as number, y)
35
+ } else {
36
+ return new Vector2(v2.x, v2.y)
37
+ }
38
+ }
39
+
40
+ /**
41
+ * The x value of this vector.
42
+ */
43
+ x: number;
44
+
45
+ /**
46
+ * The y value of this vector.
47
+ */
48
+ y: number;
49
+
50
+ /**
51
+ * Constructs a new 2D vector.
52
+ *
53
+ * @param x - The x value of this vector.
54
+ * @param y - The y value of this vector.
55
+ */
56
+ constructor(x: number = 0, y: number = 0) {
57
+ this.x = x;
58
+ this.y = y;
59
+ }
60
+
61
+ /**
62
+ * Alias for {@link Vector2#x}.
63
+ */
64
+ get width(): number {
65
+ return this.x;
66
+ }
67
+
68
+ set width(value: number) {
69
+ this.x = value;
70
+ }
71
+
72
+ /**
73
+ * Alias for {@link Vector2#y}.
74
+ */
75
+ get height(): number {
76
+ return this.y;
77
+ }
78
+
79
+ set height(value: number) {
80
+ this.y = value;
81
+ }
82
+
83
+ /**
84
+ * Sets the vector components.
85
+ *
86
+ * @param x - The value of the x component.
87
+ * @param y - The value of the y component.
88
+ * @return A reference to this vector.
89
+ */
90
+ set(x: number, y: number): Vector2 {
91
+ this.x = x;
92
+ this.y = y;
93
+ return this;
94
+ }
95
+
96
+ /**
97
+ * Sets the vector components to the same value.
98
+ *
99
+ * @param scalar - The value to set for all vector components.
100
+ * @return A reference to this vector.
101
+ */
102
+ setScalar(scalar: number): Vector2 {
103
+ this.x = scalar;
104
+ this.y = scalar;
105
+ return this;
106
+ }
107
+
108
+ /**
109
+ * Sets the vector's x component to the given value
110
+ *
111
+ * @param x - The value to set.
112
+ * @return A reference to this vector.
113
+ */
114
+ setX(x: number): Vector2 {
115
+ this.x = x;
116
+ return this;
117
+ }
118
+
119
+ /**
120
+ * Sets the vector's y component to the given value
121
+ *
122
+ * @param y - The value to set.
123
+ * @return A reference to this vector.
124
+ */
125
+ setY(y: number): Vector2 {
126
+ this.y = y;
127
+ return this;
128
+ }
129
+
130
+ /**
131
+ * Allows to set a vector component with an index.
132
+ *
133
+ * @param index - The component index. `0` equals to x, `1` equals to y.
134
+ * @param value - The value to set.
135
+ * @return A reference to this vector.
136
+ */
137
+ setComponent(index: number, value: number): Vector2 {
138
+ switch (index) {
139
+ case 0: this.x = value; break;
140
+ case 1: this.y = value; break;
141
+ default: throw new Error('index is out of range: ' + index);
142
+ }
143
+ return this;
144
+ }
145
+
146
+ /**
147
+ * Returns the value of the vector component which matches the given index.
148
+ *
149
+ * @param index - The component index. `0` equals to x, `1` equals to y.
150
+ * @return A vector component value.
151
+ */
152
+ getComponent(index: number): number {
153
+ switch (index) {
154
+ case 0: return this.x;
155
+ case 1: return this.y;
156
+ default: throw new Error('index is out of range: ' + index);
157
+ }
158
+ }
159
+
160
+ /**
161
+ * Returns a new vector with copied values from this instance.
162
+ *
163
+ * @return A clone of this instance.
164
+ */
165
+ clone(): Vector2 {
166
+ return new (this.constructor as typeof Vector2)(this.x, this.y);
167
+ }
168
+
169
+ /**
170
+ * Copies the values of the given vector to this instance.
171
+ *
172
+ * @param v - The vector to copy.
173
+ * @return A reference to this vector.
174
+ */
175
+ copy(v: Vector2): Vector2 {
176
+ this.x = v.x;
177
+ this.y = v.y;
178
+ return this;
179
+ }
180
+
181
+ /**
182
+ * Adds the given vector to this instance.
183
+ *
184
+ * @param v - The vector to add.
185
+ * @return A reference to this vector.
186
+ */
187
+ add(v: Vector2): Vector2 {
188
+ this.x += v.x;
189
+ this.y += v.y;
190
+ return this;
191
+ }
192
+
193
+ /**
194
+ * Adds the given scalar value to all components of this instance.
195
+ *
196
+ * @param s - The scalar to add.
197
+ * @return A reference to this vector.
198
+ */
199
+ addScalar(s: number): Vector2 {
200
+ this.x += s;
201
+ this.y += s;
202
+ return this;
203
+ }
204
+
205
+ /**
206
+ * Adds the given vectors and stores the result in this instance.
207
+ *
208
+ * @param a - The first vector.
209
+ * @param b - The second vector.
210
+ * @return A reference to this vector.
211
+ */
212
+ addVectors(a: Vector2, b: Vector2): Vector2 {
213
+ this.x = a.x + b.x;
214
+ this.y = a.y + b.y;
215
+ return this;
216
+ }
217
+
218
+ /**
219
+ * Adds the given vector scaled by the given factor to this instance.
220
+ *
221
+ * @param v - The vector.
222
+ * @param s - The factor that scales `v`.
223
+ * @return A reference to this vector.
224
+ */
225
+ addScaledVector(v: Vector2, s: number): Vector2 {
226
+ this.x += v.x * s;
227
+ this.y += v.y * s;
228
+ return this;
229
+ }
230
+
231
+ /**
232
+ * Subtracts the given vector from this instance.
233
+ *
234
+ * @param v - The vector to subtract.
235
+ * @return A reference to this vector.
236
+ */
237
+ sub(v: Vector2): Vector2 {
238
+ this.x -= v.x;
239
+ this.y -= v.y;
240
+ return this;
241
+ }
242
+
243
+ /**
244
+ * Subtracts the given scalar value from all components of this instance.
245
+ *
246
+ * @param s - The scalar to subtract.
247
+ * @return A reference to this vector.
248
+ */
249
+ subScalar(s: number): Vector2 {
250
+ this.x -= s;
251
+ this.y -= s;
252
+ return this;
253
+ }
254
+
255
+ /**
256
+ * Subtracts the given vectors and stores the result in this instance.
257
+ *
258
+ * @param a - The first vector.
259
+ * @param b - The second vector.
260
+ * @return A reference to this vector.
261
+ */
262
+ subVectors(a: Vector2, b: Vector2): Vector2 {
263
+ this.x = a.x - b.x;
264
+ this.y = a.y - b.y;
265
+ return this;
266
+ }
267
+
268
+ /**
269
+ * Multiplies the given vector with this instance.
270
+ *
271
+ * @param v - The vector to multiply.
272
+ * @return A reference to this vector.
273
+ */
274
+ multiply(v: Vector2): Vector2 {
275
+ this.x *= v.x;
276
+ this.y *= v.y;
277
+ return this;
278
+ }
279
+
280
+ /**
281
+ * Multiplies the given scalar value with all components of this instance.
282
+ *
283
+ * @param scalar - The scalar to multiply.
284
+ * @return A reference to this vector.
285
+ */
286
+ multiplyScalar(scalar: number): Vector2 {
287
+ this.x *= scalar;
288
+ this.y *= scalar;
289
+ return this;
290
+ }
291
+
292
+ /**
293
+ * Divides this instance by the given vector.
294
+ *
295
+ * @param v - The vector to divide.
296
+ * @return A reference to this vector.
297
+ */
298
+ divide(v: Vector2): Vector2 {
299
+ this.x /= v.x;
300
+ this.y /= v.y;
301
+ return this;
302
+ }
303
+
304
+ /**
305
+ * Divides this vector by the given scalar.
306
+ *
307
+ * @param scalar - The scalar to divide.
308
+ * @return A reference to this vector.
309
+ */
310
+ divideScalar(scalar: number): Vector2 {
311
+ return this.multiplyScalar(1 / scalar);
312
+ }
313
+
314
+ /**
315
+ * If this vector's x or y value is greater than the given vector's x or y
316
+ * value, replace that value with the corresponding min value.
317
+ *
318
+ * @param v - The vector.
319
+ * @return A reference to this vector.
320
+ */
321
+ min(v: Vector2): Vector2 {
322
+ this.x = Math.min(this.x, v.x);
323
+ this.y = Math.min(this.y, v.y);
324
+ return this;
325
+ }
326
+
327
+ /**
328
+ * If this vector's x or y value is less than the given vector's x or y
329
+ * value, replace that value with the corresponding max value.
330
+ *
331
+ * @param v - The vector.
332
+ * @return A reference to this vector.
333
+ */
334
+ max(v: Vector2): Vector2 {
335
+ this.x = Math.max(this.x, v.x);
336
+ this.y = Math.max(this.y, v.y);
337
+ return this;
338
+ }
339
+
340
+ /**
341
+ * If this vector's x or y value is greater than the max vector's x or y
342
+ * value, it is replaced by the corresponding value.
343
+ * If this vector's x or y value is less than the min vector's x or y value,
344
+ * it is replaced by the corresponding value.
345
+ *
346
+ * @param min - The minimum x and y values.
347
+ * @param max - The maximum x and y values in the desired range.
348
+ * @return A reference to this vector.
349
+ */
350
+ clamp(min: Vector2, max: Vector2): Vector2 {
351
+ // assumes min < max, componentwise
352
+ this.x = clamp(this.x, min.x, max.x);
353
+ this.y = clamp(this.y, min.y, max.y);
354
+ return this;
355
+ }
356
+
357
+ /**
358
+ * If this vector's x or y values are greater than the max value, they are
359
+ * replaced by the max value.
360
+ * If this vector's x or y values are less than the min value, they are
361
+ * replaced by the min value.
362
+ *
363
+ * @param minVal - The minimum value the components will be clamped to.
364
+ * @param maxVal - The maximum value the components will be clamped to.
365
+ * @return A reference to this vector.
366
+ */
367
+ clampScalar(minVal: number, maxVal: number): Vector2 {
368
+ this.x = clamp(this.x, minVal, maxVal);
369
+ this.y = clamp(this.y, minVal, maxVal);
370
+ return this;
371
+ }
372
+
373
+ /**
374
+ * If this vector's length is greater than the max value, it is replaced by
375
+ * the max value.
376
+ * If this vector's length is less than the min value, it is replaced by the
377
+ * min value.
378
+ *
379
+ * @param min - The minimum value the vector length will be clamped to.
380
+ * @param max - The maximum value the vector length will be clamped to.
381
+ * @return A reference to this vector.
382
+ */
383
+ clampLength(min: number, max: number): Vector2 {
384
+ const length = this.length();
385
+ return this.divideScalar(length || 1).multiplyScalar(clamp(length, min, max));
386
+ }
387
+
388
+ /**
389
+ * The components of this vector are rounded down to the nearest integer value.
390
+ *
391
+ * @return A reference to this vector.
392
+ */
393
+ floor(): Vector2 {
394
+ this.x = Math.floor(this.x);
395
+ this.y = Math.floor(this.y);
396
+ return this;
397
+ }
398
+
399
+ /**
400
+ * The components of this vector are rounded up to the nearest integer value.
401
+ *
402
+ * @return A reference to this vector.
403
+ */
404
+ ceil(): Vector2 {
405
+ this.x = Math.ceil(this.x);
406
+ this.y = Math.ceil(this.y);
407
+ return this;
408
+ }
409
+
410
+ /**
411
+ * The components of this vector are rounded to the nearest integer value
412
+ *
413
+ * @return A reference to this vector.
414
+ */
415
+ round(): Vector2 {
416
+ this.x = Math.round(this.x);
417
+ this.y = Math.round(this.y);
418
+ return this;
419
+ }
420
+
421
+ /**
422
+ * The components of this vector are rounded towards zero (up if negative,
423
+ * down if positive) to an integer value.
424
+ *
425
+ * @return A reference to this vector.
426
+ */
427
+ roundToZero(): Vector2 {
428
+ this.x = Math.trunc(this.x);
429
+ this.y = Math.trunc(this.y);
430
+ return this;
431
+ }
432
+
433
+ /**
434
+ * Inverts this vector - i.e. sets x = -x and y = -y.
435
+ *
436
+ * @return A reference to this vector.
437
+ */
438
+ negate(): Vector2 {
439
+ this.x = - this.x;
440
+ this.y = - this.y;
441
+ return this;
442
+ }
443
+
444
+ /**
445
+ * Calculates the dot product of the given vector with this instance.
446
+ *
447
+ * @param v - The vector to compute the dot product with.
448
+ * @return The result of the dot product.
449
+ */
450
+ dot(v: Vector2): number {
451
+ return this.x * v.x + this.y * v.y;
452
+ }
453
+
454
+ /**
455
+ * Calculates the cross product of the given vector with this instance.
456
+ *
457
+ * @param v - The vector to compute the cross product with.
458
+ * @return The result of the cross product.
459
+ */
460
+ cross(v: Vector2): number {
461
+ return this.x * v.y - this.y * v.x;
462
+ }
463
+
464
+ /**
465
+ * Computes the square of the Euclidean length (straight-line length) from
466
+ * (0, 0) to (x, y). If you are comparing the lengths of vectors, you should
467
+ * compare the length squared instead as it is slightly more efficient to calculate.
468
+ *
469
+ * @return The square length of this vector.
470
+ */
471
+ lengthSq(): number {
472
+ return this.x * this.x + this.y * this.y;
473
+ }
474
+
475
+ /**
476
+ * Computes the Euclidean length (straight-line length) from (0, 0) to (x, y).
477
+ *
478
+ * @return The length of this vector.
479
+ */
480
+ length(): number {
481
+ return Math.sqrt(this.x * this.x + this.y * this.y);
482
+ }
483
+
484
+ /**
485
+ * Computes the Manhattan length of this vector.
486
+ *
487
+ * @return The length of this vector.
488
+ */
489
+ manhattanLength(): number {
490
+ return Math.abs(this.x) + Math.abs(this.y);
491
+ }
492
+
493
+ /**
494
+ * Converts this vector to a unit vector - that is, sets it equal to a vector
495
+ * with the same direction as this one, but with a vector length of `1`.
496
+ *
497
+ * @return A reference to this vector.
498
+ */
499
+ normalize(): Vector2 {
500
+ return this.divideScalar(this.length() || 1);
501
+ }
502
+
503
+ /**
504
+ * Computes the angle in radians of this vector with respect to the positive x-axis.
505
+ *
506
+ * @return The angle in radians.
507
+ */
508
+ angle(): number {
509
+ const angle = Math.atan2(- this.y, - this.x) + Math.PI;
510
+ return angle;
511
+ }
512
+
513
+ /**
514
+ * Returns the angle between the given vector and this instance in radians.
515
+ *
516
+ * @param v - The vector to compute the angle with.
517
+ * @return The angle in radians.
518
+ */
519
+ angleTo(v: Vector2): number {
520
+ const denominator = Math.sqrt(this.lengthSq() * v.lengthSq());
521
+
522
+ if (denominator === 0) return Math.PI / 2;
523
+
524
+ const theta = this.dot(v) / denominator;
525
+
526
+ // clamp, to handle numerical problems
527
+ return Math.acos(clamp(theta, - 1, 1));
528
+ }
529
+
530
+ /**
531
+ * Computes the distance from the given vector to this instance.
532
+ *
533
+ * @param v - The vector to compute the distance to.
534
+ * @return The distance.
535
+ */
536
+ distanceTo(v: Vector2): number {
537
+ return Math.sqrt(this.distanceToSquared(v));
538
+ }
539
+
540
+ /**
541
+ * Computes the squared distance from the given vector to this instance.
542
+ * If you are just comparing the distance with another distance, you should compare
543
+ * the distance squared instead as it is slightly more efficient to calculate.
544
+ *
545
+ * @param v - The vector to compute the squared distance to.
546
+ * @return The squared distance.
547
+ */
548
+ distanceToSquared(v: Vector2): number {
549
+ const dx = this.x - v.x, dy = this.y - v.y;
550
+ return dx * dx + dy * dy;
551
+ }
552
+
553
+ /**
554
+ * Computes the Manhattan distance from the given vector to this instance.
555
+ *
556
+ * @param v - The vector to compute the Manhattan distance to.
557
+ * @return The Manhattan distance.
558
+ */
559
+ manhattanDistanceTo(v: Vector2): number {
560
+ return Math.abs(this.x - v.x) + Math.abs(this.y - v.y);
561
+ }
562
+
563
+ /**
564
+ * Sets this vector to a vector with the same direction as this one, but
565
+ * with the specified length.
566
+ *
567
+ * @param length - The new length of this vector.
568
+ * @return A reference to this vector.
569
+ */
570
+ setLength(length: number): Vector2 {
571
+ return this.normalize().multiplyScalar(length);
572
+ }
573
+
574
+ /**
575
+ * Linearly interpolates between the given vector and this instance, where
576
+ * alpha is the percent distance along the line - alpha = 0 will be this
577
+ * vector, and alpha = 1 will be the given one.
578
+ *
579
+ * @param v - The vector to interpolate towards.
580
+ * @param alpha - The interpolation factor, typically in the closed interval `[0, 1]`.
581
+ * @return A reference to this vector.
582
+ */
583
+ lerp(v: Vector2, alpha: number): Vector2 {
584
+ this.x += (v.x - this.x) * alpha;
585
+ this.y += (v.y - this.y) * alpha;
586
+ return this;
587
+ }
588
+
589
+ /**
590
+ * Linearly interpolates between the given vectors, where alpha is the percent
591
+ * distance along the line - alpha = 0 will be first vector, and alpha = 1 will
592
+ * be the second one. The result is stored in this instance.
593
+ *
594
+ * @param v1 - The first vector.
595
+ * @param v2 - The second vector.
596
+ * @param alpha - The interpolation factor, typically in the closed interval `[0, 1]`.
597
+ * @return A reference to this vector.
598
+ */
599
+ lerpVectors(v1: Vector2, v2: Vector2, alpha: number): Vector2 {
600
+ this.x = v1.x + (v2.x - v1.x) * alpha;
601
+ this.y = v1.y + (v2.y - v1.y) * alpha;
602
+ return this;
603
+ }
604
+
605
+ /**
606
+ * Returns `true` if this vector is equal with the given one.
607
+ *
608
+ * @param v - The vector to test for equality.
609
+ * @return Whether this vector is equal with the given one.
610
+ */
611
+ equals(v: Vector2): boolean {
612
+ return ((v.x === this.x) && (v.y === this.y));
613
+ }
614
+
615
+ /**
616
+ * Sets this vector's x value to be `array[ offset ]` and y
617
+ * value to be `array[ offset + 1 ]`.
618
+ *
619
+ * @param array - An array holding the vector component values.
620
+ * @param offset - The offset into the array.
621
+ * @return A reference to this vector.
622
+ */
623
+ fromArray(array: number[], offset: number = 0): Vector2 {
624
+ this.x = array[offset];
625
+ this.y = array[offset + 1];
626
+ return this;
627
+ }
628
+
629
+ /**
630
+ * Writes the components of this vector to the given array. If no array is provided,
631
+ * the method returns a new instance.
632
+ *
633
+ * @param array - The target array holding the vector components.
634
+ * @param offset - Index of the first element in the array.
635
+ * @return The vector components.
636
+ */
637
+ toArray(array: number[] = [], offset: number = 0): number[] {
638
+ array[offset] = this.x;
639
+ array[offset + 1] = this.y;
640
+ return array;
641
+ }
642
+
643
+ /**
644
+ * Rotates this vector around the given center by the given angle.
645
+ *
646
+ * @param center - The point around which to rotate.
647
+ * @param angle - The angle to rotate, in radians.
648
+ * @return A reference to this vector.
649
+ */
650
+ rotateAround(center: Vector2, angle: number): Vector2 {
651
+ const c = Math.cos(angle), s = Math.sin(angle);
652
+
653
+ const x = this.x - center.x;
654
+ const y = this.y - center.y;
655
+
656
+ this.x = x * c - y * s + center.x;
657
+ this.y = x * s + y * c + center.y;
658
+
659
+ return this;
660
+ }
661
+
662
+ /**
663
+ * Sets each component of this vector to a pseudo-random value between `0` and
664
+ * `1`, excluding `1`.
665
+ *
666
+ * @return A reference to this vector.
667
+ */
668
+ random(): Vector2 {
669
+ this.x = Math.random();
670
+ this.y = Math.random();
671
+ return this;
672
+ }
673
+
674
+ *[Symbol.iterator](): Generator<number> {
675
+ yield this.x;
676
+ yield this.y;
677
+ }
678
+ }
679
+
680
+ export { Vector2 };