-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathvectorWrapper.ts
235 lines (210 loc) · 5.79 KB
/
vectorWrapper.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
import type { Vector2, Vector3 } from '@minecraft/server';
import { Vector2Utils, Vector3Utils } from './coreHelpers.js';
/**
* Vector3 wrapper class which can be used as a Vector3 for APIs on \@minecraft/server which require a Vector,
* but also contain additional helper methods. This is an alternative to using the core Vector 3 utility
* methods directly, for those who prefer a more object-oriented approach. This version of the class is mutable
* and changes state inline.
*
* For an immutable version of the build, use ImmutableVector3Builder.
*
* @public
*/
export class Vector3Builder implements Vector3 {
x: number;
y: number;
z: number;
constructor(vec: Vector3, arg?: never, arg2?: never);
constructor(x: number, y: number, z: number);
constructor(first: number | Vector3, y?: number, z?: number) {
if (typeof first === 'object') {
this.x = first.x;
this.y = first.y;
this.z = first.z;
} else {
this.x = first;
this.y = y ?? 0;
this.z = z ?? 0;
}
}
/**
* Assigns the values of the passed in vector to this vector. Returns itself.
*/
assign(vec: Vector3): this {
this.x = vec.x;
this.y = vec.y;
this.z = vec.z;
return this;
}
/**
* equals
*
* Check the equality of two vectors
*/
equals(v: Vector3): boolean {
return Vector3Utils.equals(this, v);
}
/**
* add
*
* Adds the vector v to this, returning itself.
*/
add(v: Vector3): this {
return this.assign(Vector3Utils.add(this, v));
}
/**
* subtract
*
* Subtracts the vector v from this, returning itself.
*/
subtract(v: Vector3): this {
return this.assign(Vector3Utils.subtract(this, v));
}
/** scale
*
* Scales this by the passed in value, returning itself.
*/
scale(val: number): this {
return this.assign(Vector3Utils.scale(this, val));
}
/**
* dot
*
* Computes the dot product of this and the passed in vector.
*/
dot(vec: Vector3): number {
return Vector3Utils.dot(this, vec);
}
/**
* cross
*
* Computes the cross product of this and the passed in vector, returning itself.
*/
cross(vec: Vector3): this {
return this.assign(Vector3Utils.cross(this, vec));
}
/**
* magnitude
*
* The magnitude of the vector
*/
magnitude(): number {
return Vector3Utils.magnitude(this);
}
/**
* distance
*
* Calculate the distance between two vectors
*/
distance(vec: Vector3): number {
return Vector3Utils.distance(this, vec);
}
/**
* normalize
*
* Normalizes this vector, returning itself.
*/
normalize(): this {
return this.assign(Vector3Utils.normalize(this));
}
/**
* floor
*
* Floor the components of a vector to produce a new vector
*/
floor(): this {
return this.assign(Vector3Utils.floor(this));
}
/**
* toString
*
* Create a string representation of a vector
*/
toString(options?: { decimals?: number; delimiter?: string }): string {
return Vector3Utils.toString(this, options);
}
/**
* clamp
*
* Clamps the components of a vector to limits to produce a new vector
*/
clamp(limits: { min?: Partial<Vector3>; max?: Partial<Vector3> }): this {
return this.assign(Vector3Utils.clamp(this, limits));
}
/**
* lerp
*
* Constructs a new vector using linear interpolation on each component from two vectors.
*/
lerp(vec: Vector3, t: number): this {
return this.assign(Vector3Utils.lerp(this, vec, t));
}
/**
* slerp
*
* Constructs a new vector using spherical linear interpolation on each component from two vectors.
*/
slerp(vec: Vector3, t: number): this {
return this.assign(Vector3Utils.slerp(this, vec, t));
}
/**
* multiply
*
* Element-wise multiplication of two vectors together.
* Not to be confused with {@link Vector3Builder.dot} product or {@link Vector3Builder.cross} product
*/
multiply(vec: Vector3): this {
return this.assign(Vector3Utils.multiply(this, vec));
}
/**
* rotateX
*
* Rotates the vector around the x axis counterclockwise (left hand rule)
* @param a - Angle in radians
*/
rotateX(a: number): this {
return this.assign(Vector3Utils.rotateX(this, a));
}
/**
* rotateY
*
* Rotates the vector around the y axis counterclockwise (left hand rule)
* @param a - Angle in radians
*/
rotateY(a: number): this {
return this.assign(Vector3Utils.rotateY(this, a));
}
/**
* rotateZ
*
* Rotates the vector around the z axis counterclockwise (left hand rule)
* @param a - Angle in radians
*/
rotateZ(a: number): this {
return this.assign(Vector3Utils.rotateZ(this, a));
}
}
/**
* Vector2 wrapper class which can be used as a Vector2 for APIs on \@minecraft/server which require a Vector2.
* @public
*/
export class Vector2Builder implements Vector2 {
x: number;
y: number;
constructor(vec: Vector2, arg?: never);
constructor(x: number, y: number);
constructor(first: number | Vector2, y?: number) {
if (typeof first === 'object') {
this.x = first.x;
this.y = first.y;
} else {
this.x = first;
this.y = y ?? 0;
}
}
toString(options?: { decimals?: number; delimiter?: string }): string {
return Vector2Utils.toString(this, options);
}
}