mc-beta-server/server/Vec3.ts

75 lines
2.2 KiB
TypeScript
Raw Permalink Normal View History

2023-11-08 15:45:25 +00:00
export default class Vec3 {
2023-04-10 14:42:14 +01:00
public x:number;
public y:number;
public z:number;
2023-11-02 08:31:43 +00:00
public constructor(x?:Vec3 | number, y?:number, z?:number) {
2023-04-10 14:42:14 +01:00
if (typeof(x) === "number" && typeof(y) === "number" && typeof(z) === "number") {
this.x = x;
this.y = y;
this.z = z;
} else if (typeof(x) === "number" && typeof(y) !== "number" && typeof(z) !== "number") {
this.x = x;
this.y = x;
this.z = x;
2023-11-02 08:31:43 +00:00
} else if (x instanceof Vec3) {
this.x = x.x;
this.y = x.y;
this.z = x.z;
2023-04-10 14:42:14 +01:00
} else {
this.x = this.y = this.z = 0;
}
}
2023-04-10 21:52:30 +01:00
2023-11-05 10:56:28 +00:00
public get isZero() {
return this.x === 0 && this.y === 0 && this.z === 0;
}
2023-11-02 08:31:43 +00:00
public set(x?:Vec3 | number, y?:number, z?:number) {
if (x instanceof Vec3) {
this.x = x.x;
this.y = x.y;
this.z = x.z;
} else if (typeof(x) === "number" && typeof(y) === "number" && typeof(z) === "number") {
this.x = x;
this.y = y;
this.z = z;
} else {
throw new Error(`Invalid arguments for Vec3.set : ${typeof(x)}, ${typeof(y)}, ${typeof(z)}`);
}
}
add(x:Vec3 | number, y?:number, z?:number) {
if (x instanceof Vec3) {
this.set(this.x + x.x, this.y + x.y, this.z + x.z);
} else if (typeof(x) === "number" && typeof(y) === "number" && typeof(z) === "number") {
this.set(this.x + x, this.y + y, this.z + z);
} else {
throw new Error(`Invalid arguments for Vec3.add : ${typeof(x)}, ${typeof(y)}, ${typeof(z)}`);
}
}
sub(x:Vec3 | number, y?:number, z?:number) {
if (x instanceof Vec3) {
this.set(this.x - x.x, this.y - x.y, this.z - x.z);
} else if (typeof(x) === "number" && typeof(y) === "number" && typeof(z) === "number") {
this.set(this.x - x, this.y - y, this.z - z);
} else {
throw new Error(`Invalid arguments for Vec3.add : ${typeof(x)}, ${typeof(y)}, ${typeof(z)}`);
}
}
mult(x:Vec3 | number, y?:number, z?:number) {
if (x instanceof Vec3) {
this.set(this.x * x.x, this.y * x.y, this.z * x.z);
} else if (typeof(x) === "number" && typeof(y) === "number" && typeof(z) === "number") {
this.set(this.x * x, this.y * y, this.z * z);
} else {
throw new Error(`Invalid arguments for Vec3.mult : ${typeof(x)}, ${typeof(y)}, ${typeof(z)}`);
2023-11-02 08:31:43 +00:00
}
2023-04-10 21:52:30 +01:00
}
toAbs() {
return new Vec3(Math.round(this.x * 32), Math.round(this.y * 32), Math.round(this.z * 32));
}
2023-04-10 14:42:14 +01:00
}