1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.calcEasing = exports.collisionVelocity = exports.getParticleBaseVelocity = exports.getParticleDirectionAngle = exports.getDistance = exports.getDistances = exports.getValue = exports.setRangeValue = exports.getRangeMax = exports.getRangeMin = exports.getRangeValue = exports.randomInRange = exports.mix = exports.clamp = void 0;
|
4 | const Enums_1 = require("../Enums");
|
5 | const Vector_1 = require("../Core/Particle/Vector");
|
6 | function clamp(num, min, max) {
|
7 | return Math.min(Math.max(num, min), max);
|
8 | }
|
9 | exports.clamp = clamp;
|
10 | function mix(comp1, comp2, weight1, weight2) {
|
11 | return Math.floor((comp1 * weight1 + comp2 * weight2) / (weight1 + weight2));
|
12 | }
|
13 | exports.mix = mix;
|
14 | function randomInRange(r) {
|
15 | const max = getRangeMax(r);
|
16 | let min = getRangeMin(r);
|
17 | if (max === min) {
|
18 | min = 0;
|
19 | }
|
20 | return Math.random() * (max - min) + min;
|
21 | }
|
22 | exports.randomInRange = randomInRange;
|
23 | function getRangeValue(value) {
|
24 | return typeof value === "number" ? value : randomInRange(value);
|
25 | }
|
26 | exports.getRangeValue = getRangeValue;
|
27 | function getRangeMin(value) {
|
28 | return typeof value === "number" ? value : value.min;
|
29 | }
|
30 | exports.getRangeMin = getRangeMin;
|
31 | function getRangeMax(value) {
|
32 | return typeof value === "number" ? value : value.max;
|
33 | }
|
34 | exports.getRangeMax = getRangeMax;
|
35 | function setRangeValue(source, value) {
|
36 | if (source === value || (value === undefined && typeof source === "number")) {
|
37 | return source;
|
38 | }
|
39 | const min = getRangeMin(source), max = getRangeMax(source);
|
40 | return value !== undefined
|
41 | ? {
|
42 | min: Math.min(min, value),
|
43 | max: Math.max(max, value),
|
44 | }
|
45 | : setRangeValue(min, max);
|
46 | }
|
47 | exports.setRangeValue = setRangeValue;
|
48 | function getValue(options) {
|
49 | const random = options.random;
|
50 | const { enable, minimumValue } = typeof random === "boolean" ? { enable: random, minimumValue: 0 } : random;
|
51 | return enable ? getRangeValue(setRangeValue(options.value, minimumValue)) : getRangeValue(options.value);
|
52 | }
|
53 | exports.getValue = getValue;
|
54 | function getDistances(pointA, pointB) {
|
55 | const dx = pointA.x - pointB.x;
|
56 | const dy = pointA.y - pointB.y;
|
57 | return { dx: dx, dy: dy, distance: Math.sqrt(dx * dx + dy * dy) };
|
58 | }
|
59 | exports.getDistances = getDistances;
|
60 | function getDistance(pointA, pointB) {
|
61 | return getDistances(pointA, pointB).distance;
|
62 | }
|
63 | exports.getDistance = getDistance;
|
64 | function getParticleDirectionAngle(direction) {
|
65 | if (typeof direction === "number") {
|
66 | return (direction * Math.PI) / 180;
|
67 | }
|
68 | else {
|
69 | switch (direction) {
|
70 | case Enums_1.MoveDirection.top:
|
71 | return -Math.PI / 2;
|
72 | case Enums_1.MoveDirection.topRight:
|
73 | return -Math.PI / 4;
|
74 | case Enums_1.MoveDirection.right:
|
75 | return 0;
|
76 | case Enums_1.MoveDirection.bottomRight:
|
77 | return Math.PI / 4;
|
78 | case Enums_1.MoveDirection.bottom:
|
79 | return Math.PI / 2;
|
80 | case Enums_1.MoveDirection.bottomLeft:
|
81 | return (3 * Math.PI) / 4;
|
82 | case Enums_1.MoveDirection.left:
|
83 | return Math.PI;
|
84 | case Enums_1.MoveDirection.topLeft:
|
85 | return (-3 * Math.PI) / 4;
|
86 | case Enums_1.MoveDirection.none:
|
87 | default:
|
88 | return Math.random() * Math.PI * 2;
|
89 | }
|
90 | }
|
91 | }
|
92 | exports.getParticleDirectionAngle = getParticleDirectionAngle;
|
93 | function getParticleBaseVelocity(direction) {
|
94 | const baseVelocity = Vector_1.Vector.origin;
|
95 | baseVelocity.length = 1;
|
96 | baseVelocity.angle = direction;
|
97 | return baseVelocity;
|
98 | }
|
99 | exports.getParticleBaseVelocity = getParticleBaseVelocity;
|
100 | function collisionVelocity(v1, v2, m1, m2) {
|
101 | return Vector_1.Vector.create((v1.x * (m1 - m2)) / (m1 + m2) + (v2.x * 2 * m2) / (m1 + m2), v1.y);
|
102 | }
|
103 | exports.collisionVelocity = collisionVelocity;
|
104 | function calcEasing(value, type) {
|
105 | switch (type) {
|
106 | case Enums_1.EasingType.easeOutQuad:
|
107 | return 1 - (1 - value) ** 2;
|
108 | case Enums_1.EasingType.easeOutCubic:
|
109 | return 1 - (1 - value) ** 3;
|
110 | case Enums_1.EasingType.easeOutQuart:
|
111 | return 1 - (1 - value) ** 4;
|
112 | case Enums_1.EasingType.easeOutQuint:
|
113 | return 1 - (1 - value) ** 5;
|
114 | case Enums_1.EasingType.easeOutExpo:
|
115 | return value === 1 ? 1 : 1 - Math.pow(2, -10 * value);
|
116 | case Enums_1.EasingType.easeOutSine:
|
117 | return Math.sin((value * Math.PI) / 2);
|
118 | case Enums_1.EasingType.easeOutBack: {
|
119 | const c1 = 1.70158;
|
120 | const c3 = c1 + 1;
|
121 | return 1 + c3 * Math.pow(value - 1, 3) + c1 * Math.pow(value - 1, 2);
|
122 | }
|
123 | case Enums_1.EasingType.easeOutCirc:
|
124 | return Math.sqrt(1 - Math.pow(value - 1, 2));
|
125 | default:
|
126 | return value;
|
127 | }
|
128 | }
|
129 | exports.calcEasing = calcEasing;
|