UNPKG

@fuwu-yuan/bgew

Version:

Baguette Game Engine Web is a french 2D Web game engine

160 lines (159 loc) 3.69 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Vector2D = void 0; /** * Vector class ported to typescript from winduptoy gist * https://gist.github.com/winduptoy/a1aa09c3499e09edbd33 * * Javascript class originaly hacked from evanw's lightgl.js * https://github.com/evanw/lightgl.js/blob/master/src/vector.js */ class Vector2D { constructor(x = 0, y = 0) { this._x = x; this._y = y; } /* GETTERS / SETTERS */ get x() { return this._x; } set x(value) { this._x = value; } get y() { return this._y; } set y(value) { this._y = value; } /* INSTANCE METHODS */ negative() { this.x = -this.x; this.y = -this.y; return this; } add(v) { if (v instanceof Vector2D) { this.x += v.x; this.y += v.y; } else { this.x += v; this.y += v; } return this; } subtract(v) { if (v instanceof Vector2D) { this.x -= v.x; this.y -= v.y; } else { this.x -= v; this.y -= v; } return this; } multiply(v) { if (v instanceof Vector2D) { this.x *= v.x; this.y *= v.y; } else { this.x *= v; this.y *= v; } return this; } divide(v) { if (v instanceof Vector2D) { if (v.x != 0) this.x /= v.x; if (v.y != 0) this.y /= v.y; } else { if (v != 0) { this.x /= v; this.y /= v; } } return this; } equals(v) { return this.x == v.x && this.y == v.y; } dot(v) { return this.x * v.x + this.y * v.y; } cross(v) { return this.x * v.y - this.y * v.x; } length() { return Math.sqrt(this.dot(this)); } normalize() { return this.divide(this.length()); } min() { return Math.min(this.x, this.y); } max() { return Math.max(this.x, this.y); } toAngles() { return -Math.atan2(-this.y, this.x); } angleTo(a) { return Math.acos(this.dot(a) / (this.length() * a.length())); } toArray(n) { return [this.x, this.y].slice(0, n || 2); } clone() { return new Vector2D(this.x, this.y); } set(x, y) { this.x = x; this.y = y; return this; } /* STATIC METHODS */ static negative(a) { return new Vector2D(-a.x, -a.y); } static add(a, b) { if (b instanceof Vector2D) return new Vector2D(a.x + b.x, a.y + b.y); else return new Vector2D(a.x + b, a.y + b); } static subtract(a, b) { if (b instanceof Vector2D) return new Vector2D(a.x - b.x, a.y - b.y); else return new Vector2D(a.x - b, a.y - b); } static multiply(a, b) { if (b instanceof Vector2D) return new Vector2D(a.x * b.x, a.y * b.y); else return new Vector2D(a.x * b, a.y * b); } static divide(a, b) { if (b instanceof Vector2D) return new Vector2D(a.x / b.x, a.y / b.y); else return new Vector2D(a.x / b, a.y / b); } static equals(a, b) { return a.x == b.x && a.y == b.y; } static dot(a, b) { return a.x * b.x + a.y * b.y; } static cross(a, b) { return a.x * b.y - a.y * b.x; } } exports.Vector2D = Vector2D;