UNPKG

@spibo-studio/spibo-studio

Version:

Spibo Studio is an HTML Canvas Library for creating adventure games

111 lines 4.68 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { if (typeof b !== "function" && b !== null) throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); var CanvasImage_1 = __importDefault(require("./Canvas/CanvasImage")); var BackgroundPositionFactory_1 = __importDefault(require("./Background/BackgroundPositionFactory")); var Background = /** @class */ (function (_super) { __extends(Background, _super); function Background(imageSrc, width, height, scrollable, position) { var _this = _super.call(this, imageSrc, width, height) || this; _this.scrollable = false; _this.restrictedZones = []; _this.gateways = []; if (scrollable != null) _this.scrollable = scrollable; if (position != null) _this.position = position; return _this; } Object.defineProperty(Background.prototype, "height", { get: function () { return this.image.height; }, enumerable: false, configurable: true }); Object.defineProperty(Background.prototype, "width", { get: function () { return this.image.width; }, enumerable: false, configurable: true }); Object.defineProperty(Background.prototype, "pos", { get: function () { return this.position; }, enumerable: false, configurable: true }); Background.prototype.isRestricted = function (position) { var restricted = false; var charBgPos = BackgroundPositionFactory_1.default.getBackgroundPosFromCanvasPos(position, this); if (charBgPos) { var conflictualZone = this.restrictedZones.find(function (zone) { return zone.isInside(charBgPos); }); if (conflictualZone) restricted = true; } return restricted; }; Background.prototype.checkGateways = function (position) { var charBgPos = BackgroundPositionFactory_1.default.getBackgroundPosFromCanvasPos(position, this); if (charBgPos) { var gatewayEntered = this.gateways.find(function (gateway) { return gateway.isInside(charBgPos); }); if (gatewayEntered && gatewayEntered.action) { gatewayEntered.action(); } } }; Background.prototype.moveUp = function (speed, framerate) { var _a = this, scrollable = _a.scrollable, pos = _a.pos; if (scrollable && pos && pos.y > 0) { var newYpos = pos.y - speed / framerate; if (newYpos < 0) newYpos = 0; pos.y = newYpos; } }; Background.prototype.moveDown = function (speed, framerate, canvasHeight) { var _a = this, height = _a.height, pos = _a.pos, scrollable = _a.scrollable; if (scrollable && pos && pos.y < height - canvasHeight) { var newYpos = pos.y + speed / framerate; pos.y = newYpos > height - canvasHeight ? height - canvasHeight : newYpos; } }; Background.prototype.moveLeft = function (speed, framerate) { var _a = this, scrollable = _a.scrollable, pos = _a.pos; if (scrollable && pos && pos.x > 0) { var newXpos = pos.x - speed / framerate; if (newXpos < 0) newXpos = 0; pos.x = newXpos; } }; Background.prototype.moveRight = function (speed, framerate, canvasWidth) { var _a = this, pos = _a.pos, scrollable = _a.scrollable, width = _a.width; if (scrollable && pos && pos.x < width - canvasWidth) { var newXpos = pos.x + speed / framerate; pos.x = newXpos > width - canvasWidth ? width - canvasWidth : newXpos; } }; return Background; }(CanvasImage_1.default)); exports.default = Background; //# sourceMappingURL=Background.js.map