UNPKG

phaser3-rex-plugins

Version:
315 lines (302 loc) 10.6 kB
(function (global, factory) { typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() : typeof define === 'function' && define.amd ? define(factory) : (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.rexfullwindowrectangleplugin = factory()); })(this, (function () { 'use strict'; function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); } function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); } function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); } function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } } function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } function _possibleConstructorReturn(self, call) { if (call && (typeof call === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); } function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; } function _superPropBase(object, property) { while (!Object.prototype.hasOwnProperty.call(object, property)) { object = _getPrototypeOf(object); if (object === null) break; } return object; } function _get() { if (typeof Reflect !== "undefined" && Reflect.get) { _get = Reflect.get.bind(); } else { _get = function _get(target, property, receiver) { var base = _superPropBase(target, property); if (!base) return; var desc = Object.getOwnPropertyDescriptor(base, property); if (desc.get) { return desc.get.call(arguments.length < 3 ? target : receiver); } return desc.value; }; } return _get.apply(this, arguments); } function _toPrimitive(input, hint) { if (typeof input !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (typeof res !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); } function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return typeof key === "symbol" ? key : String(key); } var Rectangle = Phaser.GameObjects.Rectangle; var FullWindowRectangle = /*#__PURE__*/function (_Rectangle) { _inherits(FullWindowRectangle, _Rectangle); var _super = _createSuper(FullWindowRectangle); function FullWindowRectangle(scene, color, alpha) { var _this; _classCallCheck(this, FullWindowRectangle); _this = _super.call(this, scene, 0, 0, 2, 2, color, 1); _this.setAlpha(alpha); _this.setScrollFactor(0); _this.boot(); return _this; } _createClass(FullWindowRectangle, [{ key: "boot", value: function boot() { var scene = this.scene; scene.sys.events.on('prerender', this.resize, this); } }, { key: "destroy", value: function destroy(fromScene) { // preDestroy method does not have fromScene parameter // This Game Object has already been destroyed if (!this.scene || this.ignoreDestroy) { return; } this.scene.sys.events.off('prerender', this.resize, this); _get(_getPrototypeOf(FullWindowRectangle.prototype), "destroy", this).call(this, fromScene); } }, { key: "tint", get: function get() { return this.fillColor; }, set: function set(value) { this.setFillStyle(value, this.fillAlpha); } }, { key: "resize", value: function resize() { var scene = this.scene; var gameSize = scene.sys.scale.gameSize; var camera = scene.sys.cameras.main; var gameWidth = gameSize.width, gameHeight = gameSize.height, scale = 1 / camera.zoom; var x = gameWidth / 2, y = gameHeight / 2, width = gameWidth * scale, height = gameHeight * scale; if (this.x !== x || this.y !== y) { this.setPosition(x, y); } if (this.width !== width || this.height !== height) { this.setSize(width, height).setOrigin(0.5); } } }]); return FullWindowRectangle; }(Rectangle); function Factory (fillColor, fillAlpha) { var gameObject = new FullWindowRectangle(this.scene, fillColor, fillAlpha); this.scene.add.existing(gameObject); return gameObject; } var GetAdvancedValue = Phaser.Utils.Objects.GetAdvancedValue; var BuildGameObject = Phaser.GameObjects.BuildGameObject; function Creator (config, addToScene) { if (config === undefined) { config = {}; } if (addToScene !== undefined) { config.add = addToScene; } var fillColor = GetAdvancedValue(config, 'color', undefined); var fillAlpha = GetAdvancedValue(config, 'alpha', undefined); var gameObject = new FullWindowRectangle(this.scene, fillColor, fillAlpha); BuildGameObject(this.scene, gameObject, config); return gameObject; } var IsInValidKey = function IsInValidKey(keys) { return keys == null || keys === '' || keys.length === 0; }; var GetEntry = function GetEntry(target, keys, defaultEntry) { var entry = target; if (IsInValidKey(keys)) ; else { if (typeof keys === 'string') { keys = keys.split('.'); } var key; for (var i = 0, cnt = keys.length; i < cnt; i++) { key = keys[i]; if (entry[key] == null || _typeof(entry[key]) !== 'object') { var newEntry; if (i === cnt - 1) { if (defaultEntry === undefined) { newEntry = {}; } else { newEntry = defaultEntry; } } else { newEntry = {}; } entry[key] = newEntry; } entry = entry[key]; } } return entry; }; var SetValue = function SetValue(target, keys, value, delimiter) { if (delimiter === undefined) { delimiter = '.'; } // no object if (_typeof(target) !== 'object') { return; } // invalid key else if (IsInValidKey(keys)) { // don't erase target if (value == null) { return; } // set target to another object else if (_typeof(value) === 'object') { target = value; } } else { if (typeof keys === 'string') { keys = keys.split(delimiter); } var lastKey = keys.pop(); var entry = GetEntry(target, keys); entry[lastKey] = value; } return target; }; var FullWindowRectanglePlugin = /*#__PURE__*/function (_Phaser$Plugins$BaseP) { _inherits(FullWindowRectanglePlugin, _Phaser$Plugins$BaseP); var _super = _createSuper(FullWindowRectanglePlugin); function FullWindowRectanglePlugin(pluginManager) { var _this; _classCallCheck(this, FullWindowRectanglePlugin); _this = _super.call(this, pluginManager); // Register our new Game Object type pluginManager.registerGameObject('rexFullWindowRectangle', Factory, Creator); return _this; } _createClass(FullWindowRectanglePlugin, [{ key: "start", value: function start() { var eventEmitter = this.game.events; eventEmitter.on('destroy', this.destroy, this); } }]); return FullWindowRectanglePlugin; }(Phaser.Plugins.BasePlugin); SetValue(window, 'RexPlugins.GameObjects.FullWindowRectangle', FullWindowRectangle); return FullWindowRectanglePlugin; }));