UNPKG

@moderrkowo/jsgl

Version:

Client-side JavaScript library for creating web 2D games. Focusing at objective game.

348 lines (347 loc) 10.6 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Vector2 = void 0; var MathUtils_1 = require("../utils/math/MathUtils"); /** @group Important Classes */ var Vector2 = /** @class */ (function () { /** * Constructs a new Vector2 with the given components. * @param x - X component * @param y - Y component * @constructor */ function Vector2(x, y) { if (x === void 0) { x = 0; } if (y === void 0) { y = 0; } if (typeof x !== 'number') throw new Error('X must be an number!'); if (typeof y !== 'number') throw new Error('Y must be an number!'); this.x = x; this.y = y; } // Static Methods /** * Performs Linear Interpolation on Vectors2 with given decimal midpoint * @method * @param a - The first Vector2 * @param b - The second Vector2 * @param c - The decimal midpoint * @returns Vector2 after Linear Interpolation. * @example * const a = new JSGL.Vector2(3, 9); * const b = new JSGL.Vector2(5, 2); * JSGL.Vector2.Lerp(a, b, 0); // (3, 9) * JSGL.Vector2.Lerp(a, b, 0.5); // (4, 5.5) * JSGL.Vector2.Lerp(a, b, 1); // (5, 2) */ Vector2.Lerp = function (a, b, c) { if (c === void 0) { c = 0; } return new Vector2((0, MathUtils_1.Lerp)(a.x, b.x, c), (0, MathUtils_1.Lerp)(a.y, b.y, c)); }; /** * Returns the new Vector2 with maximum X and Y coordinates from first and second Vector2 * @method * @param a - The first Vector2 * @param b - The second Vector2 * @returns The new Vector2 with maximum X and Y * @example * const a = new JSGL.Vector2(3, 9); * const b = new JSGL.Vector2(5, 2); * const max = JSGL.Vector2.Max(a, b); // (5, 9) */ Vector2.Max = function (a, b) { return new Vector2(Math.max(a.x, b.x), Math.max(a.y, b.y)); }; /** * Returns the new Vector2 with minimum X and Y coordinates from first and second Vector2 * @method * @param a - The first Vector2 * @param b - The second Vector2 * @returns The new Vector2 with minimum X and Y * @example * const a = new JSGL.Vector2(3, 9); * const b = new JSGL.Vector2(5, 2); * const min = JSGL.Vector2.Min(a, b); // (3, 2) */ Vector2.Min = function (a, b) { return new Vector2(Math.min(a.x, b.x), Math.min(a.y, b.y)); }; /** * Returns is `point` between given 2D range. * @method * @param min - The minimal range * @param max - The maximum range * @param point - The point * @returns is `point` between range. * @example * const min = new JSGL.Vector2(5, 5); * const max = new JSGL.Vector2(7, 7); * JSGL.Vector2.IsPointIn(min, max, new JSGL.Vector2(6, 5)); */ Vector2.IsPointIn = function (min, max, point) { return (0, MathUtils_1.IsInRange)(point.x, min.x, max.x) && (0, MathUtils_1.IsInRange)(point.y, min.y, max.y); }; /** * Returns true if two vectors are equal. * @method * @param v - The first Vector2 * @param v2 - The second Vector2 * @returns are vectors equal * @example * const vector1 = new JSGL.Vector2(2, 4); * const vector2 = new JSGL.Vector2(2, 4); * JSGL.Vector2.Equal(vector1, vector2); */ Vector2.Equal = function (v, v2) { if (v === undefined || v2 === undefined) return false; if (!(v instanceof Vector2) || !(v2 instanceof Vector2)) return false; return v.x === v2.x && v.y === v2.y; }; Object.defineProperty(Vector2, "zero", { // Accesors /** * @returns (0, 0) */ get: function () { return new Vector2(0, 0); }, enumerable: false, configurable: true }); Object.defineProperty(Vector2, "one", { /** * @returns (1, 1) */ get: function () { return new Vector2(1, 1); }, enumerable: false, configurable: true }); Object.defineProperty(Vector2, "up", { /** * @returns (0, 1) */ get: function () { return new Vector2(0, 1); }, enumerable: false, configurable: true }); Object.defineProperty(Vector2, "down", { /** * @returns (0, -1) */ get: function () { return new Vector2(0, -1); }, enumerable: false, configurable: true }); Object.defineProperty(Vector2, "right", { /** * @returns (1, 0) */ get: function () { return new Vector2(1, 0); }, enumerable: false, configurable: true }); Object.defineProperty(Vector2, "left", { /** * @returns (-1, 0) */ get: function () { return new Vector2(-1, 0); }, enumerable: false, configurable: true }); Object.defineProperty(Vector2.prototype, "magnitude", { get: function () { return Math.sqrt(Math.pow(this.x, 2) + Math.pow(this.y, 2)); }, enumerable: false, configurable: true }); // Instance Methods /** * Sets components to given Vector2 or X, Y. * @method * @param x - The Vector2 or X-coordinate * @param y - The Y-coordinate (if `x` isn't Vector2) * @returns This reference * @example * vector2.set(exampleVector); * vector2.set(x, y); */ Vector2.prototype.set = function (x, y) { if (x === undefined) return this; if (x instanceof Vector2) { this.x = x.x; this.y = x.y; } else if (typeof x === 'number') { this.x = x; if (y !== undefined && typeof y === 'number') { this.y = y; } else { this.y = 0; } } return this; }; /** * Adds given Vector2 or X, Y to this Vector2 components. * @method * @param x - The Vector2 or X-coordinate * @param y - The Y-coordinate (if `x` isn't Vector2) * @returns This reference * @example * vector2.add(exampleVector); * vector2.add(x, y); */ Vector2.prototype.add = function (x, y) { if (x instanceof Vector2) { this.x += x.x; this.y += x.y; } else if (typeof x === 'number' && y !== undefined && typeof y === 'number') { this.x += x; this.y += y; } return this; }; /** * Substracts given Vector2 or X, Y to this Vector2 components. * @method * @param x - The Vector2 or X-coordinate * @param y - The Y-coordinate (if `x` isn't Vector2) * @returns This reference * @example * vector2.subtract(exampleVector); * vector2.subtract(x, y); */ Vector2.prototype.subtract = function (x, y) { if (x instanceof Vector2) { this.x -= x.x; this.y -= x.y; } else if (typeof x === 'number' && y !== undefined && typeof y === 'number') { this.x -= x; this.y -= y; } return this; }; /** * Multiplies component by scalar or Vector2. * @method * @param x - The Vector2 or scalar * @returns This reference * @example * vector2.multiply(exampleVector); * vector2.multiply(scalar); */ Vector2.prototype.multiply = function (x) { if (x instanceof Vector2) { this.x *= x.x; this.y *= x.y; } else if (typeof x === 'number') { this.x *= x; this.y *= x; } return this; }; /** * Divides component by scalar or Vector2. * @method * @param x - The Vector2 or scalar * @returns This reference * @example * vector2.divide(exampleVector); * vector2.divide(scalar); */ Vector2.prototype.divide = function (x) { if (x instanceof Vector2) { this.x /= x.x; this.y /= x.y; } else if (typeof x === 'number') { this.x /= x; this.y /= x; } return this; }; /** * Returns distance between Vector2 or X, Y coordinate. * @method * @param x - The Vector2 or X-coordinate * @param y - The Y-coordinate (if `x` isn't Vector2) * @returns The distance between vectors * @example * vector2.distance(exampleVector); * vector2.distance(0, 0); */ Vector2.prototype.distance = function (x, y) { if (x instanceof Vector2) { return Math.sqrt(Math.pow(Math.abs(this.x - x.x), 2) + Math.pow(Math.abs(this.y - x.y), 2)); } else if (typeof x === 'number' && y !== undefined && typeof y === 'number') { return Math.sqrt(Math.pow(Math.abs(this.x - x), 2) + Math.pow(Math.abs(this.y - y), 2)); } throw new Error('Invalid params!'); }; /** * Floors the X, Y components. * @method * @returns The reference * @example * vector2.floor(); */ Vector2.prototype.floor = function () { this.x = (0, MathUtils_1.floor)(this.x); this.y = (0, MathUtils_1.floor)(this.y); return this; }; /** * Returns true if two vectors are equal. * @method * @param v - The second Vector2 * @returns are vectors equal * @example * const vector1 = new JSGL.Vector2(2, 4); * const vector2 = new JSGL.Vector2(2, 4); * vector1.equal(vector2); */ Vector2.prototype.equal = function (v) { if (v === undefined) return false; if (!(v instanceof Vector2)) return false; return this.x === v.x && this.y === v.y; }; /** * Clones the Vector2. * @method * @returns The clone */ Vector2.prototype.clone = function () { return new Vector2(this.x, this.y); }; return Vector2; }()); exports.Vector2 = Vector2;