UNPKG

@moderrkowo/jsgl

Version:

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

231 lines (230 loc) 7.12 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Transform = void 0; var Vector2_1 = require("./Vector2"); var MathUtils_1 = require("../utils/math/MathUtils"); var Rotation_1 = require("./Rotation"); /** @group Important Classes */ var Transform = /** @class */ (function () { /** * Constructs new Transform * @param posX The X-coordinate * @param posY The Y-coordinate * @param scaleX The width * @param scaleY The height * @param rotation The rotation in degrees */ function Transform(posX, posY, scaleX, scaleY, rotation) { if (rotation === void 0) { rotation = 0; } this.position = new Vector2_1.Vector2(posX, posY); this.scale = new Vector2_1.Vector2(scaleX, scaleY); this.rotation = new Rotation_1.Rotation({ type: Rotation_1.RotationType.DEGREES, value: rotation, }); } Object.defineProperty(Transform.prototype, "angles", { // Rotation /** * Gets rotation in radians. */ get: function () { return this.rotation.angles; }, /** * Sets rotation in radians. */ set: function (radians) { this.rotation.angles = radians; }, enumerable: false, configurable: true }); Object.defineProperty(Transform.prototype, "eulerAngles", { /** * Gets rotation in degrees. * @property */ get: function () { return this.rotation.eulerAngles; }, /** * Sets rotation in degrees. */ set: function (degrees) { this.rotation.eulerAngles = degrees; }, enumerable: false, configurable: true }); Object.defineProperty(Transform.prototype, "positionCenter", { /** * Gets the Vector2 of center position. * @returns The center position */ get: function () { var x = this.position.x + this.scale.x / 2; var y = this.position.y + this.scale.y / 2; return new Vector2_1.Vector2(x, y); }, enumerable: false, configurable: true }); Object.defineProperty(Transform.prototype, "forward", { /** * Gets forward Vector2 * @property */ get: function () { var radians = Rotation_1.Rotation.ToRadians(this.rotation.eulerAngles); return new Vector2_1.Vector2(Math.cos(radians), Math.sin(radians)); }, enumerable: false, configurable: true }); Object.defineProperty(Transform.prototype, "backward", { /** * Gets backward Vector2 * @property */ get: function () { return this.forward.multiply(-1); }, enumerable: false, configurable: true }); /** * @beta * @method */ Transform.prototype.bounce = function () { this.rotation.eulerAngles += 180; }; /** * @beta * @method * @param grid Game canvas grid setting */ Transform.prototype.ifOnEdgeBounce = function (grid) { var max = grid.clone().subtract(new Vector2_1.Vector2(this.scale.x, this.scale.y)); var isOnEdge = !Vector2_1.Vector2.IsPointIn(new Vector2_1.Vector2(), max, this.position); if (isOnEdge) { this.position.x = (0, MathUtils_1.Clamp)(this.position.x, 0, grid.x - this.scale.x); this.position.y = (0, MathUtils_1.Clamp)(this.position.y, 0, grid.y - this.scale.y); this.bounce(); } }; Transform.prototype.set = function (x, y) { if (x === undefined) return; if (x instanceof Vector2_1.Vector2) { // Vector2 set this.position.x = x.x; this.position.y = x.y; } else if (typeof x === 'number' && y !== undefined && typeof y === 'number') { // x, y set this.position.x = x; this.position.y = y; } }; Transform.prototype.setX = function (x) { if (x === undefined) return; if (x instanceof Vector2_1.Vector2) { this.position.x = x.x; } else if (typeof x === 'number') { this.position.x = x; } }; Transform.prototype.setY = function (y) { if (y === undefined) return; if (y instanceof Vector2_1.Vector2) { this.position.y = y.y; } else if (typeof y === 'number') { this.position.y = y; } }; /** * Repositions X-coordinate and Y-coordinate. * @method * @param x - Vector2 or X-coordinate * @param y - Optional Y-coordinate * @example * const vector2 = new JSGL.Vector2(5, 2); * transform.translate(vector2); * * transform.translate(5, 2); */ Transform.prototype.translate = function (x, y) { if (x === undefined) return; if (x instanceof Vector2_1.Vector2) { // Vector2 translate this.position.x += x.x; this.position.y += x.y; } else if (typeof x === 'number' && y !== undefined && typeof y === 'number') { // x, y translate this.position.x += x; this.position.y += y; } }; /** * Repositions X-coordinate. * @method * @param x - Vector2 or X-coordinate * @example * const vector2 = new JSGL.Vector2(5, 2); * transform.translateX(vector2); * * transform.translateX(5); */ Transform.prototype.translateX = function (x) { if (x === undefined) return; if (x instanceof Vector2_1.Vector2) { this.position.x += x.x; } else if (typeof x === 'number') { this.position.x += x; } }; /** * Repositions Y-coordinate. * @method * @param y - Vector2 or Y-coordinate * @example * const vector2 = new JSGL.Vector2(5, 2); * transform.translateY(vector2); * * transform.translateY(2); */ Transform.prototype.translateY = function (y) { if (y === undefined) return; if (y instanceof Vector2_1.Vector2) { this.position.y += y.y; } else if (typeof y === 'number') { this.position.y += y; } }; /** * Clones Transform and returns new object. * @method * @returns The cloned object */ Transform.prototype.clone = function () { return new Transform(this.position.x, this.position.y, this.scale.x, this.scale.y, this.rotation.eulerAngles); }; return Transform; }()); exports.Transform = Transform;