-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.ts
121 lines (99 loc) · 3.01 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
import { find, Node, v2, Vec2 } from 'cc';
import { CONSTANTS } from './constants';
import { SceneData } from './persistentData/sceneData';
export interface AnimationEvent {
frame: number;
func: string;
params: any[];
}
export function GetSceneData()
{
const sceneDataNode = find(CONSTANTS.SCENE_DATA_PATH) as Node;
return sceneDataNode.getComponent(SceneData) as SceneData;
}
/*
* @ClampVectorValue
*
* Restricts the sensitivity of swipe information
*
* @param Vector2 swipe Swipe information from EasyTouch plugin
* @param float sensitivity Sensitivity for either OnSwipe or OnSwipeEnd
*/
export function ClampVectorValue(rawVector: Vec2, maxMin: number = 1)
{
let clampedVector = v2(0, 0);
clampedVector.x = ClampValue(rawVector.x, maxMin, maxMin * -1);
clampedVector.y = ClampValue(rawVector.y, maxMin, maxMin * -1);
return clampedVector;
}
export function ClampValue(value: number, max: number, min: number) {
if(value > max) {
return max;
}
if(value < min) {
return min;
}
return value;
}
export function GetV2Sign(vector2: Vec2) {
const vectorValue = vector2.x + vector2.y;
if(vectorValue >= 0) {
return 1;
}
return -1;
}
export function InvertV2Values(vector2: Vec2, axesToInvert: string[]) {
for (let i = 0; i < axesToInvert.length; i++) {
if(axesToInvert[i] == "x") {
vector2.x *= -1;
} else {
vector2.y *= -1;
}
}
return vector2;
}
// Raises a Vector3 to a power while retaining
// its original positive or negative values
export function ExponentiateV2(rawVector: Vec2, power: number)
{
const xValue = ExponentiatePosNegValue(rawVector.x, power);
const yValue = ExponentiatePosNegValue(rawVector.y, power);
return v2(xValue, yValue);
}
// Raises a positive or negative value to a power while
// retaining its original positive or negative value
export function ExponentiatePosNegValue(rawValue: number, power: number)
{
const sign = rawValue >= 0 ? 1 : -1;
const distance = Math.abs(rawValue);
let exponentVal = distance > 0 ? Math.pow(distance, power) : 0;
return exponentVal *= sign;
}
export function GetVector2Direction(vectors: Vec2[], invertX: boolean = false, invertY: boolean = false)
{
const vectorTotals = GetVector2Totals(vectors);
if (Math.abs(vectorTotals.x) > Math.abs(vectorTotals.y)) {
if (invertX) {
vectorTotals.x *= -1;
}
return vectorTotals.x > 0 ? v2(1, 0) : v2(-1, 0);
}
if (invertY) {
vectorTotals.y *= -1;
}
return vectorTotals.y > 0 ? v2(0, 1) : v2(0, -1);
}
export function GetVector2Totals(vectors: Vec2[])
{
let totalXForce = 0;
let totalYForce = 0;
for (let z = 0; z < vectors.length; z++) {
totalXForce += vectors[z].x;
totalYForce += vectors[z].y;
}
return v2(totalXForce, totalYForce);
}
export function GetSquareMagnitude(vector: Vec2) {
const total = ((vector.x * vector.x) + (vector.x * vector.x) + (vector.z * vector.z));
return Math.pow(total, 2);
}