1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.NumberUtils = void 0;
|
4 | const Directions_1 = require("../Enums/Directions");
|
5 | const Vector_1 = require("../Core/Particle/Vector");
|
6 | class NumberUtils {
|
7 | static clamp(num, min, max) {
|
8 | return Math.min(Math.max(num, min), max);
|
9 | }
|
10 | static mix(comp1, comp2, weight1, weight2) {
|
11 | return Math.floor((comp1 * weight1 + comp2 * weight2) / (weight1 + weight2));
|
12 | }
|
13 | static randomInRange(r) {
|
14 | const max = NumberUtils.getRangeMax(r);
|
15 | let min = NumberUtils.getRangeMin(r);
|
16 | if (max === min) {
|
17 | min = 0;
|
18 | }
|
19 | return Math.random() * (max - min) + min;
|
20 | }
|
21 | static getRangeValue(value) {
|
22 | return typeof value === "number" ? value : NumberUtils.randomInRange(value);
|
23 | }
|
24 | static getRangeMin(value) {
|
25 | return typeof value === "number" ? value : value.min;
|
26 | }
|
27 | static getRangeMax(value) {
|
28 | return typeof value === "number" ? value : value.max;
|
29 | }
|
30 | static setRangeValue(source, value) {
|
31 | if (source === value || (value === undefined && typeof source === "number")) {
|
32 | return source;
|
33 | }
|
34 | const min = NumberUtils.getRangeMin(source), max = NumberUtils.getRangeMax(source);
|
35 | return value !== undefined
|
36 | ? {
|
37 | min: Math.min(min, value),
|
38 | max: Math.max(max, value),
|
39 | }
|
40 | : NumberUtils.setRangeValue(min, max);
|
41 | }
|
42 | static getValue(options) {
|
43 | const random = options.random;
|
44 | const { enable, minimumValue } = typeof random === "boolean" ? { enable: random, minimumValue: 0 } : random;
|
45 | return enable
|
46 | ? NumberUtils.getRangeValue(NumberUtils.setRangeValue(options.value, minimumValue))
|
47 | : NumberUtils.getRangeValue(options.value);
|
48 | }
|
49 | static getDistances(pointA, pointB) {
|
50 | const dx = pointA.x - pointB.x;
|
51 | const dy = pointA.y - pointB.y;
|
52 | return { dx: dx, dy: dy, distance: Math.sqrt(dx * dx + dy * dy) };
|
53 | }
|
54 | static getDistance(pointA, pointB) {
|
55 | return NumberUtils.getDistances(pointA, pointB).distance;
|
56 | }
|
57 | static getParticleBaseVelocity(direction) {
|
58 | const baseVelocity = Vector_1.Vector.origin;
|
59 | baseVelocity.length = 1;
|
60 | switch (direction) {
|
61 | case Directions_1.MoveDirection.top:
|
62 | baseVelocity.angle = -Math.PI / 2;
|
63 | break;
|
64 | case Directions_1.MoveDirection.topRight:
|
65 | baseVelocity.angle = -Math.PI / 4;
|
66 | break;
|
67 | case Directions_1.MoveDirection.right:
|
68 | baseVelocity.angle = 0;
|
69 | break;
|
70 | case Directions_1.MoveDirection.bottomRight:
|
71 | baseVelocity.angle = Math.PI / 4;
|
72 | break;
|
73 | case Directions_1.MoveDirection.bottom:
|
74 | baseVelocity.angle = Math.PI / 2;
|
75 | break;
|
76 | case Directions_1.MoveDirection.bottomLeft:
|
77 | baseVelocity.angle = (3 * Math.PI) / 4;
|
78 | break;
|
79 | case Directions_1.MoveDirection.left:
|
80 | baseVelocity.angle = Math.PI;
|
81 | break;
|
82 | case Directions_1.MoveDirection.topLeft:
|
83 | baseVelocity.angle = (-3 * Math.PI) / 4;
|
84 | break;
|
85 | case Directions_1.MoveDirection.none:
|
86 | default:
|
87 | baseVelocity.angle = Math.random() * Math.PI * 2;
|
88 | break;
|
89 | }
|
90 | return baseVelocity;
|
91 | }
|
92 | static rotateVelocity(velocity, angle) {
|
93 | return {
|
94 | horizontal: velocity.horizontal * Math.cos(angle) - velocity.vertical * Math.sin(angle),
|
95 | vertical: velocity.horizontal * Math.sin(angle) + velocity.vertical * Math.cos(angle),
|
96 | };
|
97 | }
|
98 | static collisionVelocity(v1, v2, m1, m2) {
|
99 | return Vector_1.Vector.create((v1.x * (m1 - m2)) / (m1 + m2) + (v2.x * 2 * m2) / (m1 + m2), v1.y);
|
100 | }
|
101 | }
|
102 | exports.NumberUtils = NumberUtils;
|