import { Vector } from "../Core/Utils/Vector"; let _random = Math.random; const easings = new Map(); export function addEasing(name, easing) { if (!easings.get(name)) { easings.set(name, easing); } } export function getEasing(name) { return easings.get(name) || ((value) => value); } export function setRandom(rnd = Math.random) { _random = rnd; } export function getRandom() { return clamp(_random(), 0, 1 - 1e-16); } export function clamp(num, min, max) { return Math.min(Math.max(num, min), max); } export function mix(comp1, comp2, weight1, weight2) { return Math.floor((comp1 * weight1 + comp2 * weight2) / (weight1 + weight2)); } export function randomInRange(r) { const max = getRangeMax(r); let min = getRangeMin(r); if (max === min) { min = 0; } return getRandom() * (max - min) + min; } export function getRangeValue(value) { return typeof value === "number" ? value : randomInRange(value); } export function getRangeMin(value) { return typeof value === "number" ? value : value.min; } export function getRangeMax(value) { return typeof value === "number" ? value : value.max; } export function setRangeValue(source, value) { if (source === value || (value === undefined && typeof source === "number")) { return source; } const min = getRangeMin(source), max = getRangeMax(source); return value !== undefined ? { min: Math.min(min, value), max: Math.max(max, value), } : setRangeValue(min, max); } export function getValue(options) { const random = options.random, { enable, minimumValue } = typeof random === "boolean" ? { enable: random, minimumValue: 0, } : random; return enable ? getRangeValue(setRangeValue(options.value, minimumValue)) : getRangeValue(options.value); } export function getDistances(pointA, pointB) { const dx = pointA.x - pointB.x, dy = pointA.y - pointB.y; return { dx: dx, dy: dy, distance: Math.sqrt(dx ** 2 + dy ** 2) }; } export function getDistance(pointA, pointB) { return getDistances(pointA, pointB).distance; } export function getParticleDirectionAngle(direction, position, center) { if (typeof direction === "number") { return (direction * Math.PI) / 180; } else { switch (direction) { case "top": return -Math.PI / 2; case "top-right": return -Math.PI / 4; case "right": return 0; case "bottom-right": return Math.PI / 4; case "bottom": return Math.PI / 2; case "bottom-left": return (3 * Math.PI) / 4; case "left": return Math.PI; case "top-left": return (-3 * Math.PI) / 4; case "inside": return Math.atan2(center.y - position.y, center.x - position.x); case "outside": return Math.atan2(position.y - center.y, position.x - center.x); default: return getRandom() * Math.PI * 2; } } } export function getParticleBaseVelocity(direction) { const baseVelocity = Vector.origin; baseVelocity.length = 1; baseVelocity.angle = direction; return baseVelocity; } export function collisionVelocity(v1, v2, m1, m2) { return Vector.create((v1.x * (m1 - m2)) / (m1 + m2) + (v2.x * 2 * m2) / (m1 + m2), v1.y); } export function calcPositionFromSize(data) { return data.position && data.position.x !== undefined && data.position.y !== undefined ? { x: (data.position.x * data.size.width) / 100, y: (data.position.y * data.size.height) / 100, } : undefined; } export function calcPositionOrRandomFromSize(data) { var _a, _b, _c, _d; return { x: (((_b = (_a = data.position) === null || _a === void 0 ? void 0 : _a.x) !== null && _b !== void 0 ? _b : getRandom() * 100) * data.size.width) / 100, y: (((_d = (_c = data.position) === null || _c === void 0 ? void 0 : _c.y) !== null && _d !== void 0 ? _d : getRandom() * 100) * data.size.height) / 100, }; } export function calcPositionOrRandomFromSizeRanged(data) { var _a, _b; const position = { x: ((_a = data.position) === null || _a === void 0 ? void 0 : _a.x) !== undefined ? getRangeValue(data.position.x) : undefined, y: ((_b = data.position) === null || _b === void 0 ? void 0 : _b.y) !== undefined ? getRangeValue(data.position.y) : undefined, }; return calcPositionOrRandomFromSize({ size: data.size, position }); } export function calcExactPositionOrRandomFromSize(data) { var _a, _b, _c, _d; return { x: (_b = (_a = data.position) === null || _a === void 0 ? void 0 : _a.x) !== null && _b !== void 0 ? _b : getRandom() * data.size.width, y: (_d = (_c = data.position) === null || _c === void 0 ? void 0 : _c.y) !== null && _d !== void 0 ? _d : getRandom() * data.size.height, }; } export function calcExactPositionOrRandomFromSizeRanged(data) { var _a, _b; const position = { x: ((_a = data.position) === null || _a === void 0 ? void 0 : _a.x) !== undefined ? getRangeValue(data.position.x) : undefined, y: ((_b = data.position) === null || _b === void 0 ? void 0 : _b.y) !== undefined ? getRangeValue(data.position.y) : undefined, }; return calcExactPositionOrRandomFromSize({ size: data.size, position }); } export function parseAlpha(input) { return input ? (input.endsWith("%") ? parseFloat(input) / 100 : parseFloat(input)) : 1; }