ts-ritofile
Version:
TypeScript library for reading and writing League of Legends game file formats
50 lines (42 loc) • 1.33 kB
text/typescript
import { JsonSerializable } from '../core/json-encoder';
export class Vector implements JsonSerializable {
public x: number;
public y: number;
public z?: number;
public w?: number;
constructor(x: number, y: number, z?: number, w?: number) {
this.x = x;
this.y = y;
this.z = z;
this.w = w;
}
static lerp(vec1: Vector, vec2: Vector, weight: number): Vector {
if (weight < 0.005) return vec1;
if (weight > 0.995) return vec2;
// Python version only handles 3D vectors for lerp
return new Vector(
vec1.x + (vec2.x - vec1.x) * weight,
vec1.y + (vec2.y - vec1.y) * weight,
vec1.z! + (vec2.z! - vec1.z!) * weight
);
}
toString(): string {
let result = `${this.x.toFixed(4)} ${this.y.toFixed(4)}`;
if (this.z !== undefined) result += ` ${this.z.toFixed(4)}`;
if (this.w !== undefined) result += ` ${this.w.toFixed(4)}`;
return result;
}
toArray(): number[] {
const result = [this.x, this.y];
if (this.z !== undefined) result.push(this.z);
if (this.w !== undefined) result.push(this.w);
return result;
}
__json__(): number[] {
return this.toArray();
}
// Keep toJSON for standard JavaScript compatibility
toJSON(): number[] {
return this.__json__();
}
}