@nativewrappers/fivem 0.0.19 → 0.0.21
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/client/Camera.js +2 -2
- package/client/Events.js +3 -3
- package/common/utils/Vector.d.ts +249 -14
- package/common/utils/Vector.js +274 -21
- package/package.json +1 -1
- package/server/Events.js +3 -3
package/client/Camera.js
CHANGED
|
@@ -35,10 +35,10 @@ export class Camera {
|
|
|
35
35
|
return this.ForwardVector;
|
|
36
36
|
}
|
|
37
37
|
set Direction(direction) {
|
|
38
|
-
const dir = direction.normalize;
|
|
38
|
+
const dir = direction.normalize();
|
|
39
39
|
const vec1 = new Vector3(dir.x, dir.y, 0);
|
|
40
40
|
const vec2 = new Vector3(dir.z, vec1.distanceSquared(vec1), 0);
|
|
41
|
-
const vec3 = vec2.normalize;
|
|
41
|
+
const vec3 = vec2.normalize();
|
|
42
42
|
this.Rotation = new Vector3(Math.atan2(vec3.x, vec3.y) * 57.295779513082323, this.Rotation.y, Math.atan2(dir.x, dir.y) * -57.295779513082323);
|
|
43
43
|
}
|
|
44
44
|
// public get UpVector() : Vector3 {
|
package/client/Events.js
CHANGED
|
@@ -11,15 +11,15 @@ const getClassFromArguments = (...args) => {
|
|
|
11
11
|
for (const arg of args) {
|
|
12
12
|
switch (arg.type) {
|
|
13
13
|
case ClassTypes.Vector2: {
|
|
14
|
-
newArgs.push(Vector2.
|
|
14
|
+
newArgs.push(Vector2.fromObject(arg));
|
|
15
15
|
continue;
|
|
16
16
|
}
|
|
17
17
|
case ClassTypes.Vector3: {
|
|
18
|
-
newArgs.push(Vector3.
|
|
18
|
+
newArgs.push(Vector3.fromObject(arg));
|
|
19
19
|
continue;
|
|
20
20
|
}
|
|
21
21
|
case ClassTypes.Vector4: {
|
|
22
|
-
newArgs.push(Vector4.
|
|
22
|
+
newArgs.push(Vector4.fromObject(arg));
|
|
23
23
|
continue;
|
|
24
24
|
}
|
|
25
25
|
case ClassTypes.Ped: {
|
package/common/utils/Vector.d.ts
CHANGED
|
@@ -1,61 +1,212 @@
|
|
|
1
1
|
import type { MsgpackBuffer } from '../types';
|
|
2
|
+
/**
|
|
3
|
+
* Represents a 2-dimensional vector.
|
|
4
|
+
*/
|
|
2
5
|
export interface Vec2 {
|
|
3
6
|
x: number;
|
|
4
7
|
y: number;
|
|
5
8
|
}
|
|
9
|
+
/**
|
|
10
|
+
* Represents a 3-dimensional vector.
|
|
11
|
+
*/
|
|
6
12
|
export interface Vec3 {
|
|
7
13
|
x: number;
|
|
8
14
|
y: number;
|
|
9
15
|
z: number;
|
|
10
16
|
}
|
|
17
|
+
/**
|
|
18
|
+
* Represents a 4-dimensional vector.
|
|
19
|
+
*/
|
|
11
20
|
export interface Vec4 {
|
|
12
21
|
x: number;
|
|
13
22
|
y: number;
|
|
14
23
|
z: number;
|
|
15
24
|
w: number;
|
|
16
25
|
}
|
|
26
|
+
/**
|
|
27
|
+
* An object with vector components.
|
|
28
|
+
*/
|
|
17
29
|
interface VectorObject {
|
|
18
30
|
x: number;
|
|
19
31
|
y: number;
|
|
20
32
|
z?: number;
|
|
21
33
|
w?: number;
|
|
22
34
|
}
|
|
23
|
-
|
|
35
|
+
/**
|
|
36
|
+
* Represents a vector of variable dimensions.
|
|
37
|
+
*/
|
|
38
|
+
type VectorN<L extends number, T = number> = L extends 2 ? [T, T] : L extends 3 ? [T, T, T] : L extends 4 ? [T, T, T, T] : never;
|
|
39
|
+
/**
|
|
40
|
+
* An array that can be converted to a vector.
|
|
41
|
+
*/
|
|
42
|
+
type VectorArray<T> = T extends Vec4 ? VectorN<4> : T extends Vec3 ? VectorN<3> : T extends Vec2 ? VectorN<2> : number[];
|
|
43
|
+
/**
|
|
44
|
+
* The constructor type of the Vector class.
|
|
45
|
+
*/
|
|
24
46
|
type VectorType = typeof Vector;
|
|
47
|
+
/**
|
|
48
|
+
* Represents an object or class that can be treated as a vector.
|
|
49
|
+
*/
|
|
25
50
|
type VectorLike = VectorObject | Vector;
|
|
51
|
+
/**
|
|
52
|
+
* Utility type to get the vector type of an object based on its component.
|
|
53
|
+
*/
|
|
54
|
+
type InferVector<T> = T extends Vec4 | VectorN<4> ? Vector4 : T extends Vec3 | VectorN<3> ? Vector3 : T extends Vec2 | VectorN<2> ? Vector2 : any;
|
|
55
|
+
/**
|
|
56
|
+
* A base vector class inherited by all vector classes.
|
|
57
|
+
*/
|
|
26
58
|
export declare class Vector {
|
|
27
59
|
size: number;
|
|
28
60
|
x: number;
|
|
29
61
|
y: number;
|
|
30
62
|
z?: number | undefined;
|
|
31
63
|
w?: number | undefined;
|
|
64
|
+
/**
|
|
65
|
+
* The type identifier for vectors.
|
|
66
|
+
*/
|
|
32
67
|
type: string;
|
|
33
|
-
static create
|
|
34
|
-
static
|
|
35
|
-
static
|
|
68
|
+
protected static create(x: number, y?: number): Vector2;
|
|
69
|
+
protected static create(x: number, y?: number, z?: number): Vector3;
|
|
70
|
+
protected static create(x: number, y?: number, z?: number, w?: number): Vector4;
|
|
71
|
+
/**
|
|
72
|
+
* Creates a new vector based on the provided vector-like object.
|
|
73
|
+
* @param obj The object representing the vector.
|
|
74
|
+
* @returns A new vector instance.
|
|
75
|
+
*/
|
|
76
|
+
protected static create<T extends VectorType, U extends VectorLike>(this: T, obj: U): InferVector<U>;
|
|
77
|
+
/**
|
|
78
|
+
* Creates a vector from binary data in a MsgpackBuffer.
|
|
79
|
+
* @param msgpackBuffer The buffer containing binary data.
|
|
80
|
+
* @returns A new vector instance.
|
|
81
|
+
*/
|
|
82
|
+
static fromBuffer<T extends VectorType>(this: T, { buffer }: MsgpackBuffer): InstanceType<T>;
|
|
83
|
+
/**
|
|
84
|
+
* Creates a deep copy of the provided vector.
|
|
85
|
+
* @param obj The vector to clone.
|
|
86
|
+
* @returns A new vector instance that is a copy of the provided vector.
|
|
87
|
+
*/
|
|
88
|
+
static clone<T extends VectorType, U extends VectorLike>(this: T, obj: U): InferVector<U>;
|
|
89
|
+
/**
|
|
90
|
+
* Performs an operation between a vector and either another vector or scalar value.
|
|
91
|
+
* @param a - The first vector.
|
|
92
|
+
* @param b - The second vector or scalar value.
|
|
93
|
+
* @param operator - The function defining the operation to perform.
|
|
94
|
+
* @returns A new vector resulting from the operation.
|
|
95
|
+
*/
|
|
36
96
|
private static operate;
|
|
97
|
+
/**
|
|
98
|
+
* Adds two vectors or a scalar value to a vector.
|
|
99
|
+
* @param a - The first vector or scalar value.
|
|
100
|
+
* @param b - The second vector or scalar value.
|
|
101
|
+
* @returns A new vector with incremented components.
|
|
102
|
+
*/
|
|
37
103
|
static add<T extends VectorType, U extends VectorLike>(this: T, a: U, b: VectorLike | number): U;
|
|
104
|
+
/**
|
|
105
|
+
* Adds a scalar value to the x-component of a vector.
|
|
106
|
+
* @param obj - The vector.
|
|
107
|
+
* @param x - The value to add to the x-component.
|
|
108
|
+
* @returns A new vector with the x-component incremented.
|
|
109
|
+
*/
|
|
110
|
+
static addX<T extends VectorType, U extends VectorLike>(this: T, obj: U, x: number): InferVector<U>;
|
|
111
|
+
/**
|
|
112
|
+
* Adds a scalar value to the y-component of a vector.
|
|
113
|
+
* @param obj - The vector.
|
|
114
|
+
* @param y - The value to add to the y-component.
|
|
115
|
+
* @returns A new vector with the y-component incremented.
|
|
116
|
+
*/
|
|
117
|
+
static addY<T extends VectorType, U extends VectorLike>(this: T, obj: U, y: number): InferVector<U> | undefined;
|
|
118
|
+
/**
|
|
119
|
+
* Adds a scalar value to the z-component of a vector.
|
|
120
|
+
* @param obj - The vector.
|
|
121
|
+
* @param z - The value to add to the z-component.
|
|
122
|
+
* @returns A new vector with the z-component incremented.
|
|
123
|
+
*/
|
|
124
|
+
static addZ<T extends VectorType, U extends VectorLike>(this: T, obj: U, z: number): InferVector<U> | undefined;
|
|
125
|
+
/**
|
|
126
|
+
* Adds a scalar value to the w-component of a vector.
|
|
127
|
+
* @param obj - The vector.
|
|
128
|
+
* @param w - The value to add to the w-component.
|
|
129
|
+
* @returns A new vector with the w-component incremented.
|
|
130
|
+
*/
|
|
131
|
+
static addW<T extends VectorType, U extends VectorLike>(this: T, obj: U, w: number): InferVector<U> | undefined;
|
|
132
|
+
/**
|
|
133
|
+
* Subtracts one vector from another or subtracts a scalar value from a vector.
|
|
134
|
+
* @param a - The vector.
|
|
135
|
+
* @param b - The second vector or scalar value.
|
|
136
|
+
* @returns A new vector with subtracted components.
|
|
137
|
+
*/
|
|
38
138
|
static subtract<T extends VectorType, U extends VectorLike>(this: T, a: U, b: VectorLike | number): U;
|
|
139
|
+
/**
|
|
140
|
+
* Multiplies two vectors by their components, or multiplies a vector by a scalar value.
|
|
141
|
+
* @param a - The vector.
|
|
142
|
+
* @param b - The second vector or scalar value.
|
|
143
|
+
* @returns A new vector with multiplied components.
|
|
144
|
+
*/
|
|
39
145
|
static multiply<T extends VectorType, U extends VectorLike>(this: T, a: U, b: VectorLike | number): U;
|
|
146
|
+
/**
|
|
147
|
+
* Divides two vectors by their components, or divides a vector by a scalar value.
|
|
148
|
+
* @param a - The vector.
|
|
149
|
+
* @param b - The second vector or scalar vector.
|
|
150
|
+
* @returns A new vector with divided components.
|
|
151
|
+
*/
|
|
40
152
|
static divide<T extends VectorType, U extends VectorLike>(this: T, a: U, b: VectorLike | number): U;
|
|
153
|
+
/**
|
|
154
|
+
* Calculates the dot product of two vectors.
|
|
155
|
+
* @param a - The first vector.
|
|
156
|
+
* @param b - The second vector.
|
|
157
|
+
* @returns A scalar value representing the degree of alignment between the input vectors.
|
|
158
|
+
*/
|
|
41
159
|
static dotProduct<T extends VectorType, U extends VectorLike>(this: T, a: U, b: U): number;
|
|
160
|
+
/**
|
|
161
|
+
* Calculates the cross product of two vectors in three-dimensional space.
|
|
162
|
+
* @param a - The first vector.
|
|
163
|
+
* @param b - The second vector.
|
|
164
|
+
* @returns A new vector perpendicular to both input vectors.
|
|
165
|
+
*/
|
|
166
|
+
static crossProduct<T extends VectorType, U extends VectorObject>(this: T, a: U, b: U): U;
|
|
167
|
+
/**
|
|
168
|
+
* Normalizes a vector, producing a new vector with the same direction but with a magnitude of 1.
|
|
169
|
+
* @param vector - The vector to be normalized.
|
|
170
|
+
* @returns The new normalized vector.
|
|
171
|
+
*/
|
|
42
172
|
static normalize<T extends VectorType, U extends VectorLike>(this: T, a: U): U;
|
|
43
173
|
/**
|
|
44
174
|
* Creates a vector from an array of numbers.
|
|
45
175
|
* @param primitive An array of numbers (usually returned by a native).
|
|
46
176
|
*/
|
|
47
|
-
static fromArray<T extends VectorType
|
|
177
|
+
static fromArray<T extends VectorType, U extends VectorArray<T>>(this: T, primitive: U): InferVector<U>;
|
|
178
|
+
/**
|
|
179
|
+
* Creates a vector from an array or object containing vector components.
|
|
180
|
+
* @param primitive The object to use as a vector.
|
|
181
|
+
*/
|
|
182
|
+
static fromObject<T extends VectorType, U extends InferVector<T> | VectorArray<T>>(this: T, primitive: U): InstanceType<T>;
|
|
48
183
|
/**
|
|
49
184
|
* Creates an array of vectors from an array of number arrays
|
|
50
185
|
* @param primitives A multi-dimensional array of number arrays
|
|
51
186
|
*/
|
|
52
|
-
static fromArrays<T extends VectorType>(this: T, primitives:
|
|
187
|
+
static fromArrays<T extends VectorType, U extends VectorArray<T>[]>(this: T, primitives: U): InferVector<VectorArray<T>>[];
|
|
188
|
+
/**
|
|
189
|
+
* Calculates the length (magnitude) of a vector.
|
|
190
|
+
* @param obj - The vector for which to calculate the length.
|
|
191
|
+
* @returns The magnitude of the vector.
|
|
192
|
+
*/
|
|
53
193
|
static Length<T extends VectorType, U extends VectorLike>(this: T, obj: U): number;
|
|
194
|
+
/**
|
|
195
|
+
* Constructs a new vector.
|
|
196
|
+
* @param size The size of the vector (number of components).
|
|
197
|
+
* @param x The x-component of the vector.
|
|
198
|
+
* @param y The y-component of the vector (optional, defaults to x).
|
|
199
|
+
* @param z The z-component of the vector (optional).
|
|
200
|
+
* @param w The w-component of the vector (optional).
|
|
201
|
+
*/
|
|
54
202
|
constructor(size: number, x?: number, y?: number, z?: number | undefined, w?: number | undefined);
|
|
55
|
-
[Symbol.iterator](): Iterator<
|
|
203
|
+
[Symbol.iterator](): Iterator<number>;
|
|
56
204
|
toString(): string;
|
|
57
205
|
toJSON(): string;
|
|
58
|
-
|
|
206
|
+
/**
|
|
207
|
+
* @see Vector.clone
|
|
208
|
+
*/
|
|
209
|
+
clone(): InferVector<this>;
|
|
59
210
|
/**
|
|
60
211
|
* The product of the Euclidean magnitudes of this and another Vector.
|
|
61
212
|
*
|
|
@@ -70,32 +221,116 @@ export declare class Vector {
|
|
|
70
221
|
* @returns Distance between this and another vector.
|
|
71
222
|
*/
|
|
72
223
|
distance(v: VectorLike): number;
|
|
73
|
-
|
|
224
|
+
/**
|
|
225
|
+
* @see Vector.normalize
|
|
226
|
+
*/
|
|
227
|
+
normalize(): this;
|
|
228
|
+
/**
|
|
229
|
+
* @see Vector.dotProduct
|
|
230
|
+
*/
|
|
74
231
|
dotProduct(v: this): number;
|
|
232
|
+
/**
|
|
233
|
+
* @see Vector.add
|
|
234
|
+
*/
|
|
75
235
|
add(v: VectorLike | number): this;
|
|
236
|
+
/**
|
|
237
|
+
* @see Vector.addX
|
|
238
|
+
*/
|
|
239
|
+
addX(x: number): InferVector<this>;
|
|
240
|
+
/**
|
|
241
|
+
* @see Vector.addY
|
|
242
|
+
*/
|
|
243
|
+
addY(x: number): InferVector<this> | undefined;
|
|
244
|
+
/**
|
|
245
|
+
* @see Vector.subtract
|
|
246
|
+
*/
|
|
76
247
|
subtract(v: VectorLike): this;
|
|
248
|
+
/**
|
|
249
|
+
* @see Vector.multiply
|
|
250
|
+
*/
|
|
77
251
|
multiply(v: VectorLike | number): this;
|
|
252
|
+
/**
|
|
253
|
+
* @see Vector.divide
|
|
254
|
+
*/
|
|
78
255
|
divide(v: VectorLike | number): this;
|
|
79
|
-
|
|
80
|
-
|
|
256
|
+
/**
|
|
257
|
+
* Converts the vector to an array of its components.
|
|
258
|
+
*/
|
|
259
|
+
toArray(): number[];
|
|
260
|
+
/**
|
|
261
|
+
* Replaces the components of the vector with the components of another vector object.
|
|
262
|
+
* @param v - The object whose components will replace the current vector's components.
|
|
263
|
+
*/
|
|
264
|
+
replace<T extends VectorLike>(v: T): void;
|
|
265
|
+
/**
|
|
266
|
+
* Calculates the length (magnitude) of a vector.
|
|
267
|
+
* @returns The magnitude of the vector.
|
|
268
|
+
*/
|
|
81
269
|
get Length(): number;
|
|
82
270
|
}
|
|
271
|
+
/**
|
|
272
|
+
* Represents a 2-dimensional vector.
|
|
273
|
+
*/
|
|
83
274
|
export declare class Vector2 extends Vector {
|
|
84
275
|
type: string;
|
|
85
|
-
constructor(x: number, y?: number);
|
|
86
276
|
static readonly Zero: Vector2;
|
|
277
|
+
/**
|
|
278
|
+
* Constructs a new 2D vector.
|
|
279
|
+
* @param x The x-component of the vector.
|
|
280
|
+
* @param y The y-component of the vector (optional, defaults to x).
|
|
281
|
+
*/
|
|
282
|
+
constructor(x: number, y?: number);
|
|
87
283
|
}
|
|
284
|
+
/**
|
|
285
|
+
* Represents a 3-dimensional vector.
|
|
286
|
+
*/
|
|
88
287
|
export declare class Vector3 extends Vector implements Vec3 {
|
|
89
288
|
type: string;
|
|
90
289
|
z: number;
|
|
91
|
-
constructor(x: number, y?: number, z?: number);
|
|
92
290
|
static readonly Zero: Vector3;
|
|
291
|
+
/**
|
|
292
|
+
* Constructs a new 3D vector.
|
|
293
|
+
* @param x The x-component of the vector.
|
|
294
|
+
* @param y The y-component of the vector (optional, defaults to x).
|
|
295
|
+
* @param z The z-component of the vector (optional, defaults to y).
|
|
296
|
+
*/
|
|
297
|
+
constructor(x: number, y?: number, z?: number);
|
|
298
|
+
/**
|
|
299
|
+
* @see Vector.addZ
|
|
300
|
+
*/
|
|
301
|
+
addZ(z: number): InferVector<this> | undefined;
|
|
302
|
+
/**
|
|
303
|
+
* @see Vector.crossProduct
|
|
304
|
+
*/
|
|
305
|
+
crossProduct(v: VectorLike): VectorLike;
|
|
93
306
|
}
|
|
307
|
+
/**
|
|
308
|
+
* Represents a 4-dimensional vector.
|
|
309
|
+
*/
|
|
94
310
|
export declare class Vector4 extends Vector {
|
|
95
311
|
type: string;
|
|
96
312
|
z: number;
|
|
97
313
|
w: number;
|
|
98
|
-
constructor(x: number, y?: number, z?: number, w?: number);
|
|
99
314
|
static readonly Zero: Vector4;
|
|
315
|
+
/**
|
|
316
|
+
* Constructs a new 4D vector.
|
|
317
|
+
* @param x The x-component of the vector.
|
|
318
|
+
* @param y The y-component of the vector (optional, defaults to x).
|
|
319
|
+
* @param z The z-component of the vector (optional, defaults to y).
|
|
320
|
+
* @param w The w-component of the vector (optional, defaults to z).
|
|
321
|
+
*/
|
|
322
|
+
constructor(x: number, y?: number, z?: number, w?: number);
|
|
323
|
+
/**
|
|
324
|
+
* @see Vector.addZ
|
|
325
|
+
*/
|
|
326
|
+
addZ(z: number): InferVector<this> | undefined;
|
|
327
|
+
/**
|
|
328
|
+
* @see Vector.addW
|
|
329
|
+
*/
|
|
330
|
+
addW(w: number): InferVector<this> | undefined;
|
|
331
|
+
/**
|
|
332
|
+
* @see Vector.crossProduct
|
|
333
|
+
*/
|
|
334
|
+
crossProduct(v: VectorLike): VectorLike;
|
|
100
335
|
}
|
|
101
336
|
export {};
|
package/common/utils/Vector.js
CHANGED
|
@@ -1,5 +1,16 @@
|
|
|
1
1
|
// Adapted from https://raw.githubusercontent.com/you21979/typescript-vector/master/vector3.ts
|
|
2
|
+
/**
|
|
3
|
+
* A base vector class inherited by all vector classes.
|
|
4
|
+
*/
|
|
2
5
|
export class Vector {
|
|
6
|
+
/**
|
|
7
|
+
* Creates a new vector based on the provided parameters.
|
|
8
|
+
* @param x The x-component of the vector.
|
|
9
|
+
* @param y The y-component of the vector (optional, defaults to the value of x).
|
|
10
|
+
* @param z The z-component of the vector (optional, defaults to the value of y).
|
|
11
|
+
* @param w The w-component of the vector (optional, defaults to the value of z).
|
|
12
|
+
* @returns A new vector instance.
|
|
13
|
+
*/
|
|
3
14
|
static create(x, y, z, w) {
|
|
4
15
|
if (y === undefined && typeof x === 'number')
|
|
5
16
|
return new Vector2(x, x);
|
|
@@ -18,15 +29,32 @@ export class Vector {
|
|
|
18
29
|
return new Vector4(x, y, z, w);
|
|
19
30
|
}
|
|
20
31
|
}
|
|
32
|
+
/**
|
|
33
|
+
* Creates a vector from binary data in a MsgpackBuffer.
|
|
34
|
+
* @param msgpackBuffer The buffer containing binary data.
|
|
35
|
+
* @returns A new vector instance.
|
|
36
|
+
*/
|
|
21
37
|
static fromBuffer({ buffer }) {
|
|
22
38
|
const arr = [];
|
|
23
39
|
for (let offset = 0; offset < buffer.length; offset += 4)
|
|
24
40
|
arr.push(buffer.readFloatLE(offset));
|
|
25
41
|
return this.fromArray(arr);
|
|
26
42
|
}
|
|
43
|
+
/**
|
|
44
|
+
* Creates a deep copy of the provided vector.
|
|
45
|
+
* @param obj The vector to clone.
|
|
46
|
+
* @returns A new vector instance that is a copy of the provided vector.
|
|
47
|
+
*/
|
|
27
48
|
static clone(obj) {
|
|
28
49
|
return this.create(obj);
|
|
29
50
|
}
|
|
51
|
+
/**
|
|
52
|
+
* Performs an operation between a vector and either another vector or scalar value.
|
|
53
|
+
* @param a - The first vector.
|
|
54
|
+
* @param b - The second vector or scalar value.
|
|
55
|
+
* @param operator - The function defining the operation to perform.
|
|
56
|
+
* @returns A new vector resulting from the operation.
|
|
57
|
+
*/
|
|
30
58
|
static operate(a, b, operator) {
|
|
31
59
|
let { x, y, z, w } = a;
|
|
32
60
|
const isNumber = typeof b === 'number';
|
|
@@ -38,18 +66,98 @@ export class Vector {
|
|
|
38
66
|
w = operator(w, isNumber ? b : b.w ?? 0);
|
|
39
67
|
return this.create(x, y, z, w);
|
|
40
68
|
}
|
|
69
|
+
/**
|
|
70
|
+
* Adds two vectors or a scalar value to a vector.
|
|
71
|
+
* @param a - The first vector or scalar value.
|
|
72
|
+
* @param b - The second vector or scalar value.
|
|
73
|
+
* @returns A new vector with incremented components.
|
|
74
|
+
*/
|
|
41
75
|
static add(a, b) {
|
|
42
76
|
return this.operate(a, b, (x, y) => x + y);
|
|
43
77
|
}
|
|
78
|
+
/**
|
|
79
|
+
* Adds a scalar value to the x-component of a vector.
|
|
80
|
+
* @param obj - The vector.
|
|
81
|
+
* @param x - The value to add to the x-component.
|
|
82
|
+
* @returns A new vector with the x-component incremented.
|
|
83
|
+
*/
|
|
84
|
+
static addX(obj, x) {
|
|
85
|
+
const vec = this.clone(obj);
|
|
86
|
+
vec.x += x;
|
|
87
|
+
return vec;
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Adds a scalar value to the y-component of a vector.
|
|
91
|
+
* @param obj - The vector.
|
|
92
|
+
* @param y - The value to add to the y-component.
|
|
93
|
+
* @returns A new vector with the y-component incremented.
|
|
94
|
+
*/
|
|
95
|
+
static addY(obj, y) {
|
|
96
|
+
if (typeof obj.y !== 'number')
|
|
97
|
+
return;
|
|
98
|
+
const vec = this.clone(obj);
|
|
99
|
+
vec.y += y;
|
|
100
|
+
return vec;
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Adds a scalar value to the z-component of a vector.
|
|
104
|
+
* @param obj - The vector.
|
|
105
|
+
* @param z - The value to add to the z-component.
|
|
106
|
+
* @returns A new vector with the z-component incremented.
|
|
107
|
+
*/
|
|
108
|
+
static addZ(obj, z) {
|
|
109
|
+
if (typeof obj.z !== 'number')
|
|
110
|
+
return;
|
|
111
|
+
const vec = this.clone(obj);
|
|
112
|
+
vec.z += z;
|
|
113
|
+
return vec;
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Adds a scalar value to the w-component of a vector.
|
|
117
|
+
* @param obj - The vector.
|
|
118
|
+
* @param w - The value to add to the w-component.
|
|
119
|
+
* @returns A new vector with the w-component incremented.
|
|
120
|
+
*/
|
|
121
|
+
static addW(obj, w) {
|
|
122
|
+
if (typeof obj.w !== 'number')
|
|
123
|
+
return;
|
|
124
|
+
const vec = this.clone(obj);
|
|
125
|
+
vec.w += w;
|
|
126
|
+
return vec;
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Subtracts one vector from another or subtracts a scalar value from a vector.
|
|
130
|
+
* @param a - The vector.
|
|
131
|
+
* @param b - The second vector or scalar value.
|
|
132
|
+
* @returns A new vector with subtracted components.
|
|
133
|
+
*/
|
|
44
134
|
static subtract(a, b) {
|
|
45
135
|
return this.operate(a, b, (x, y) => x - y);
|
|
46
136
|
}
|
|
137
|
+
/**
|
|
138
|
+
* Multiplies two vectors by their components, or multiplies a vector by a scalar value.
|
|
139
|
+
* @param a - The vector.
|
|
140
|
+
* @param b - The second vector or scalar value.
|
|
141
|
+
* @returns A new vector with multiplied components.
|
|
142
|
+
*/
|
|
47
143
|
static multiply(a, b) {
|
|
48
144
|
return this.operate(a, b, (x, y) => x * y);
|
|
49
145
|
}
|
|
146
|
+
/**
|
|
147
|
+
* Divides two vectors by their components, or divides a vector by a scalar value.
|
|
148
|
+
* @param a - The vector.
|
|
149
|
+
* @param b - The second vector or scalar vector.
|
|
150
|
+
* @returns A new vector with divided components.
|
|
151
|
+
*/
|
|
50
152
|
static divide(a, b) {
|
|
51
153
|
return this.operate(a, b, (x, y) => x / y);
|
|
52
154
|
}
|
|
155
|
+
/**
|
|
156
|
+
* Calculates the dot product of two vectors.
|
|
157
|
+
* @param a - The first vector.
|
|
158
|
+
* @param b - The second vector.
|
|
159
|
+
* @returns A scalar value representing the degree of alignment between the input vectors.
|
|
160
|
+
*/
|
|
53
161
|
static dotProduct(a, b) {
|
|
54
162
|
let result = 0;
|
|
55
163
|
for (const key of ['x', 'y', 'z', 'w']) {
|
|
@@ -63,8 +171,31 @@ export class Vector {
|
|
|
63
171
|
}
|
|
64
172
|
return result;
|
|
65
173
|
}
|
|
174
|
+
/**
|
|
175
|
+
* Calculates the cross product of two vectors in three-dimensional space.
|
|
176
|
+
* @param a - The first vector.
|
|
177
|
+
* @param b - The second vector.
|
|
178
|
+
* @returns A new vector perpendicular to both input vectors.
|
|
179
|
+
*/
|
|
180
|
+
static crossProduct(a, b) {
|
|
181
|
+
const { x: ax, y: ay, z: az, w: aw } = a;
|
|
182
|
+
const { x: bx, y: by, z: bz } = b;
|
|
183
|
+
if (ax === undefined ||
|
|
184
|
+
ay === undefined ||
|
|
185
|
+
az === undefined ||
|
|
186
|
+
bx === undefined ||
|
|
187
|
+
by === undefined ||
|
|
188
|
+
bz === undefined)
|
|
189
|
+
throw new Error('Vector.crossProduct requires two three-dimensional vectors.');
|
|
190
|
+
return this.create(ay * bz - az * by, az * bx - ax * bz, ax * by - ay * bx, aw);
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* Normalizes a vector, producing a new vector with the same direction but with a magnitude of 1.
|
|
194
|
+
* @param vector - The vector to be normalized.
|
|
195
|
+
* @returns The new normalized vector.
|
|
196
|
+
*/
|
|
66
197
|
static normalize(a) {
|
|
67
|
-
const length = a.Length
|
|
198
|
+
const length = a instanceof Vector ? a.Length : this.Length(a);
|
|
68
199
|
return this.divide(a, length);
|
|
69
200
|
}
|
|
70
201
|
/**
|
|
@@ -73,15 +204,17 @@ export class Vector {
|
|
|
73
204
|
*/
|
|
74
205
|
static fromArray(primitive) {
|
|
75
206
|
const [x, y, z, w] = primitive;
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
207
|
+
return this.create(x, y, z, w);
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* Creates a vector from an array or object containing vector components.
|
|
211
|
+
* @param primitive The object to use as a vector.
|
|
212
|
+
*/
|
|
213
|
+
static fromObject(primitive) {
|
|
214
|
+
if (Array.isArray(primitive))
|
|
215
|
+
return this.fromArray(primitive);
|
|
216
|
+
const { x, y, z, w } = primitive;
|
|
217
|
+
return this.create(x, y, z, w);
|
|
85
218
|
}
|
|
86
219
|
/**
|
|
87
220
|
* Creates an array of vectors from an array of number arrays
|
|
@@ -90,30 +223,47 @@ export class Vector {
|
|
|
90
223
|
static fromArrays(primitives) {
|
|
91
224
|
return primitives.map(this.fromArray);
|
|
92
225
|
}
|
|
226
|
+
/**
|
|
227
|
+
* Calculates the length (magnitude) of a vector.
|
|
228
|
+
* @param obj - The vector for which to calculate the length.
|
|
229
|
+
* @returns The magnitude of the vector.
|
|
230
|
+
*/
|
|
93
231
|
static Length(obj) {
|
|
94
232
|
let sum = 0;
|
|
95
233
|
for (const key of ['x', 'y', 'z', 'w']) {
|
|
96
|
-
|
|
97
|
-
|
|
234
|
+
if (key in obj) {
|
|
235
|
+
const value = obj[key];
|
|
98
236
|
sum += value * value;
|
|
237
|
+
}
|
|
99
238
|
}
|
|
100
239
|
return Math.sqrt(sum);
|
|
101
240
|
}
|
|
241
|
+
/**
|
|
242
|
+
* Constructs a new vector.
|
|
243
|
+
* @param size The size of the vector (number of components).
|
|
244
|
+
* @param x The x-component of the vector.
|
|
245
|
+
* @param y The y-component of the vector (optional, defaults to x).
|
|
246
|
+
* @param z The z-component of the vector (optional).
|
|
247
|
+
* @param w The w-component of the vector (optional).
|
|
248
|
+
*/
|
|
102
249
|
constructor(size, x = 0, y = x, z, w) {
|
|
103
250
|
this.size = size;
|
|
104
251
|
this.x = x;
|
|
105
252
|
this.y = y;
|
|
106
253
|
this.z = z;
|
|
107
254
|
this.w = w;
|
|
255
|
+
/**
|
|
256
|
+
* The type identifier for vectors.
|
|
257
|
+
*/
|
|
108
258
|
this.type = 'vec';
|
|
109
259
|
}
|
|
110
260
|
*[Symbol.iterator]() {
|
|
111
|
-
yield
|
|
112
|
-
yield
|
|
261
|
+
yield this.x;
|
|
262
|
+
yield this.y;
|
|
113
263
|
if (this.z)
|
|
114
|
-
yield
|
|
264
|
+
yield this.z;
|
|
115
265
|
if (this.w)
|
|
116
|
-
yield
|
|
266
|
+
yield this.w;
|
|
117
267
|
}
|
|
118
268
|
toString() {
|
|
119
269
|
const { x, y, z, w } = this;
|
|
@@ -122,6 +272,9 @@ export class Vector {
|
|
|
122
272
|
toJSON() {
|
|
123
273
|
return this.toString();
|
|
124
274
|
}
|
|
275
|
+
/**
|
|
276
|
+
* @see Vector.clone
|
|
277
|
+
*/
|
|
125
278
|
clone() {
|
|
126
279
|
return Vector.clone(this);
|
|
127
280
|
}
|
|
@@ -144,59 +297,159 @@ export class Vector {
|
|
|
144
297
|
distance(v) {
|
|
145
298
|
return Math.sqrt(this.distanceSquared(v));
|
|
146
299
|
}
|
|
147
|
-
|
|
300
|
+
/**
|
|
301
|
+
* @see Vector.normalize
|
|
302
|
+
*/
|
|
303
|
+
normalize() {
|
|
148
304
|
return Vector.normalize(this);
|
|
149
305
|
}
|
|
306
|
+
/**
|
|
307
|
+
* @see Vector.dotProduct
|
|
308
|
+
*/
|
|
150
309
|
dotProduct(v) {
|
|
151
310
|
return Vector.dotProduct(this, v);
|
|
152
311
|
}
|
|
312
|
+
/**
|
|
313
|
+
* @see Vector.add
|
|
314
|
+
*/
|
|
153
315
|
add(v) {
|
|
154
316
|
return Vector.add(this, v);
|
|
155
317
|
}
|
|
318
|
+
/**
|
|
319
|
+
* @see Vector.addX
|
|
320
|
+
*/
|
|
321
|
+
addX(x) {
|
|
322
|
+
return Vector.addX(this, x);
|
|
323
|
+
}
|
|
324
|
+
/**
|
|
325
|
+
* @see Vector.addY
|
|
326
|
+
*/
|
|
327
|
+
addY(x) {
|
|
328
|
+
return Vector.addY(this, x);
|
|
329
|
+
}
|
|
330
|
+
/**
|
|
331
|
+
* @see Vector.subtract
|
|
332
|
+
*/
|
|
156
333
|
subtract(v) {
|
|
157
334
|
return Vector.subtract(this, v);
|
|
158
335
|
}
|
|
336
|
+
/**
|
|
337
|
+
* @see Vector.multiply
|
|
338
|
+
*/
|
|
159
339
|
multiply(v) {
|
|
160
340
|
return Vector.multiply(this, v);
|
|
161
341
|
}
|
|
342
|
+
/**
|
|
343
|
+
* @see Vector.divide
|
|
344
|
+
*/
|
|
162
345
|
divide(v) {
|
|
163
346
|
return Vector.divide(this, v);
|
|
164
347
|
}
|
|
348
|
+
/**
|
|
349
|
+
* Converts the vector to an array of its components.
|
|
350
|
+
*/
|
|
165
351
|
toArray() {
|
|
166
|
-
return [this
|
|
352
|
+
return [...this];
|
|
167
353
|
}
|
|
354
|
+
/**
|
|
355
|
+
* Replaces the components of the vector with the components of another vector object.
|
|
356
|
+
* @param v - The object whose components will replace the current vector's components.
|
|
357
|
+
*/
|
|
168
358
|
replace(v) {
|
|
169
|
-
|
|
170
|
-
|
|
359
|
+
for (const key of ['x', 'y', 'z', 'w']) {
|
|
360
|
+
if (key in this && key in v)
|
|
361
|
+
this[key] = v[key];
|
|
362
|
+
}
|
|
171
363
|
}
|
|
364
|
+
/**
|
|
365
|
+
* Calculates the length (magnitude) of a vector.
|
|
366
|
+
* @returns The magnitude of the vector.
|
|
367
|
+
*/
|
|
172
368
|
get Length() {
|
|
173
369
|
let sum = 0;
|
|
174
|
-
for (let
|
|
370
|
+
for (let value of this)
|
|
175
371
|
sum += value * value;
|
|
176
372
|
return Math.sqrt(sum);
|
|
177
373
|
}
|
|
178
374
|
}
|
|
375
|
+
/**
|
|
376
|
+
* Represents a 2-dimensional vector.
|
|
377
|
+
*/
|
|
179
378
|
export class Vector2 extends Vector {
|
|
379
|
+
/**
|
|
380
|
+
* Constructs a new 2D vector.
|
|
381
|
+
* @param x The x-component of the vector.
|
|
382
|
+
* @param y The y-component of the vector (optional, defaults to x).
|
|
383
|
+
*/
|
|
180
384
|
constructor(x, y = x) {
|
|
181
385
|
super(2, x, y);
|
|
182
386
|
this.type = 'vec2';
|
|
183
387
|
}
|
|
184
388
|
}
|
|
185
389
|
Vector2.Zero = new Vector2(0, 0);
|
|
390
|
+
/**
|
|
391
|
+
* Represents a 3-dimensional vector.
|
|
392
|
+
*/
|
|
186
393
|
export class Vector3 extends Vector {
|
|
394
|
+
/**
|
|
395
|
+
* Constructs a new 3D vector.
|
|
396
|
+
* @param x The x-component of the vector.
|
|
397
|
+
* @param y The y-component of the vector (optional, defaults to x).
|
|
398
|
+
* @param z The z-component of the vector (optional, defaults to y).
|
|
399
|
+
*/
|
|
187
400
|
constructor(x, y = x, z = y) {
|
|
188
401
|
super(3, x, y, z);
|
|
189
402
|
this.type = 'vec3';
|
|
190
403
|
this.z = z;
|
|
191
404
|
}
|
|
405
|
+
/**
|
|
406
|
+
* @see Vector.addZ
|
|
407
|
+
*/
|
|
408
|
+
addZ(z) {
|
|
409
|
+
return Vector.addZ(this, z);
|
|
410
|
+
}
|
|
411
|
+
/**
|
|
412
|
+
* @see Vector.crossProduct
|
|
413
|
+
*/
|
|
414
|
+
crossProduct(v) {
|
|
415
|
+
return Vector.crossProduct(this, v);
|
|
416
|
+
}
|
|
192
417
|
}
|
|
193
418
|
Vector3.Zero = new Vector3(0, 0, 0);
|
|
419
|
+
/**
|
|
420
|
+
* Represents a 4-dimensional vector.
|
|
421
|
+
*/
|
|
194
422
|
export class Vector4 extends Vector {
|
|
423
|
+
/**
|
|
424
|
+
* Constructs a new 4D vector.
|
|
425
|
+
* @param x The x-component of the vector.
|
|
426
|
+
* @param y The y-component of the vector (optional, defaults to x).
|
|
427
|
+
* @param z The z-component of the vector (optional, defaults to y).
|
|
428
|
+
* @param w The w-component of the vector (optional, defaults to z).
|
|
429
|
+
*/
|
|
195
430
|
constructor(x, y = x, z = y, w = z) {
|
|
196
431
|
super(4, x, y, z, w);
|
|
197
432
|
this.type = 'vec4';
|
|
198
433
|
this.z = z;
|
|
199
434
|
this.w = w;
|
|
200
435
|
}
|
|
436
|
+
/**
|
|
437
|
+
* @see Vector.addZ
|
|
438
|
+
*/
|
|
439
|
+
addZ(z) {
|
|
440
|
+
return Vector.addZ(this, z);
|
|
441
|
+
}
|
|
442
|
+
/**
|
|
443
|
+
* @see Vector.addW
|
|
444
|
+
*/
|
|
445
|
+
addW(w) {
|
|
446
|
+
return Vector.addW(this, w);
|
|
447
|
+
}
|
|
448
|
+
/**
|
|
449
|
+
* @see Vector.crossProduct
|
|
450
|
+
*/
|
|
451
|
+
crossProduct(v) {
|
|
452
|
+
return Vector.crossProduct(this, v);
|
|
453
|
+
}
|
|
201
454
|
}
|
|
202
455
|
Vector4.Zero = new Vector4(0, 0, 0, 0);
|
package/package.json
CHANGED
package/server/Events.js
CHANGED
|
@@ -8,15 +8,15 @@ const getClassFromArguments = (...args) => {
|
|
|
8
8
|
for (const arg of args) {
|
|
9
9
|
switch (arg.type) {
|
|
10
10
|
case ClassTypes.Vector2: {
|
|
11
|
-
newArgs.push(Vector2.
|
|
11
|
+
newArgs.push(Vector2.fromObject(arg));
|
|
12
12
|
continue;
|
|
13
13
|
}
|
|
14
14
|
case ClassTypes.Vector3: {
|
|
15
|
-
newArgs.push(Vector3.
|
|
15
|
+
newArgs.push(Vector3.fromObject(arg));
|
|
16
16
|
continue;
|
|
17
17
|
}
|
|
18
18
|
case ClassTypes.Vector4: {
|
|
19
|
-
newArgs.push(Vector4.
|
|
19
|
+
newArgs.push(Vector4.fromObject(arg));
|
|
20
20
|
continue;
|
|
21
21
|
}
|
|
22
22
|
case ClassTypes.Ped: {
|