UNPKG

phaser3-rex-plugins

Version:
900 lines (863 loc) 29 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.rexboundsplugin = 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 EventEmitterMethods = { setEventEmitter: function setEventEmitter(eventEmitter, EventEmitterClass) { if (EventEmitterClass === undefined) { EventEmitterClass = Phaser.Events.EventEmitter; // Use built-in EventEmitter class by default } this._privateEE = eventEmitter === true || eventEmitter === undefined; this._eventEmitter = this._privateEE ? new EventEmitterClass() : eventEmitter; return this; }, destroyEventEmitter: function destroyEventEmitter() { if (this._eventEmitter && this._privateEE) { this._eventEmitter.shutdown(); } return this; }, getEventEmitter: function getEventEmitter() { return this._eventEmitter; }, on: function on() { if (this._eventEmitter) { this._eventEmitter.on.apply(this._eventEmitter, arguments); } return this; }, once: function once() { if (this._eventEmitter) { this._eventEmitter.once.apply(this._eventEmitter, arguments); } return this; }, off: function off() { if (this._eventEmitter) { this._eventEmitter.off.apply(this._eventEmitter, arguments); } return this; }, emit: function emit(event) { if (this._eventEmitter && event) { this._eventEmitter.emit.apply(this._eventEmitter, arguments); } return this; }, addListener: function addListener() { if (this._eventEmitter) { this._eventEmitter.addListener.apply(this._eventEmitter, arguments); } return this; }, removeListener: function removeListener() { if (this._eventEmitter) { this._eventEmitter.removeListener.apply(this._eventEmitter, arguments); } return this; }, removeAllListeners: function removeAllListeners() { if (this._eventEmitter) { this._eventEmitter.removeAllListeners.apply(this._eventEmitter, arguments); } return this; }, listenerCount: function listenerCount() { if (this._eventEmitter) { return this._eventEmitter.listenerCount.apply(this._eventEmitter, arguments); } return 0; }, listeners: function listeners() { if (this._eventEmitter) { return this._eventEmitter.listeners.apply(this._eventEmitter, arguments); } return []; }, eventNames: function eventNames() { if (this._eventEmitter) { return this._eventEmitter.eventNames.apply(this._eventEmitter, arguments); } return []; } }; var SceneClass = Phaser.Scene; var IsSceneObject = function IsSceneObject(object) { return object instanceof SceneClass; }; var GetSceneObject = function GetSceneObject(object) { if (object == null || _typeof(object) !== 'object') { return null; } else if (IsSceneObject(object)) { // object = scene return object; } else if (object.scene && IsSceneObject(object.scene)) { // object = game object return object.scene; } else if (object.parent && object.parent.scene && IsSceneObject(object.parent.scene)) { // parent = bob object return object.parent.scene; } else { return null; } }; var GameClass = Phaser.Game; var IsGame = function IsGame(object) { return object instanceof GameClass; }; var GetGame = function GetGame(object) { if (object == null || _typeof(object) !== 'object') { return null; } else if (IsGame(object)) { return object; } else if (IsGame(object.game)) { return object.game; } else if (IsSceneObject(object)) { // object = scene object return object.sys.game; } else if (IsSceneObject(object.scene)) { // object = game object return object.scene.sys.game; } }; var GetValue$3 = Phaser.Utils.Objects.GetValue; var ComponentBase = /*#__PURE__*/function () { function ComponentBase(parent, config) { _classCallCheck(this, ComponentBase); this.setParent(parent); // gameObject, scene, or game this.isShutdown = false; // Event emitter, default is private event emitter this.setEventEmitter(GetValue$3(config, 'eventEmitter', true)); // Register callback of parent destroy event, also see `shutdown` method if (this.parent) { if (this.parent === this.scene) { // parent is a scene this.scene.sys.events.once('shutdown', this.onEnvDestroy, this); } else if (this.parent === this.game) { // parent is game this.game.events.once('shutdown', this.onEnvDestroy, this); } else if (this.parent.once) { // parent is game object or something else this.parent.once('destroy', this.onParentDestroy, this); } // bob object does not have event emitter } } _createClass(ComponentBase, [{ key: "shutdown", value: function shutdown(fromScene) { // Already shutdown if (this.isShutdown) { return; } // parent might not be shutdown yet if (this.parent) { if (this.parent === this.scene) { // parent is a scene this.scene.sys.events.off('shutdown', this.onEnvDestroy, this); } else if (this.parent === this.game) { // parent is game this.game.events.off('shutdown', this.onEnvDestroy, this); } else if (this.parent.once) { // parent is game object or something else this.parent.off('destroy', this.onParentDestroy, this); } // bob object does not have event emitter } this.destroyEventEmitter(); this.parent = undefined; this.scene = undefined; this.game = undefined; this.isShutdown = true; } }, { key: "destroy", value: function destroy(fromScene) { this.shutdown(fromScene); } }, { key: "onEnvDestroy", value: function onEnvDestroy() { this.destroy(true); } }, { key: "onParentDestroy", value: function onParentDestroy(parent, fromScene) { this.destroy(fromScene); } }, { key: "setParent", value: function setParent(parent) { this.parent = parent; // gameObject, scene, or game this.scene = GetSceneObject(parent); this.game = GetGame(parent); return this; } }]); return ComponentBase; }(); Object.assign(ComponentBase.prototype, EventEmitterMethods); var GetValue$2 = Phaser.Utils.Objects.GetValue; var TickTask = /*#__PURE__*/function (_ComponentBase) { _inherits(TickTask, _ComponentBase); var _super = _createSuper(TickTask); function TickTask(parent, config) { var _this; _classCallCheck(this, TickTask); _this = _super.call(this, parent, config); _this._isRunning = false; _this.isPaused = false; _this.tickingState = false; _this.setTickingMode(GetValue$2(config, 'tickingMode', 1)); // boot() later return _this; } // override _createClass(TickTask, [{ key: "boot", value: function boot() { if (this.tickingMode === 2 && !this.tickingState) { this.startTicking(); } } // override }, { key: "shutdown", value: function shutdown(fromScene) { // Already shutdown if (this.isShutdown) { return; } this.stop(); if (this.tickingState) { this.stopTicking(); } _get(_getPrototypeOf(TickTask.prototype), "shutdown", this).call(this, fromScene); } }, { key: "setTickingMode", value: function setTickingMode(mode) { if (typeof mode === 'string') { mode = TICKINGMODE[mode]; } this.tickingMode = mode; } // override }, { key: "startTicking", value: function startTicking() { this.tickingState = true; } // override }, { key: "stopTicking", value: function stopTicking() { this.tickingState = false; } }, { key: "isRunning", get: function get() { return this._isRunning; }, set: function set(value) { if (this._isRunning === value) { return; } this._isRunning = value; if (this.tickingMode === 1 && value != this.tickingState) { if (value) { this.startTicking(); } else { this.stopTicking(); } } } }, { key: "start", value: function start() { this.isPaused = false; this.isRunning = true; return this; } }, { key: "pause", value: function pause() { // Only can ba paused in running state if (this.isRunning) { this.isPaused = true; this.isRunning = false; } return this; } }, { key: "resume", value: function resume() { // Only can ba resumed in paused state (paused from running state) if (this.isPaused) { this.isRunning = true; } return this; } }, { key: "stop", value: function stop() { this.isPaused = false; this.isRunning = false; return this; } }, { key: "complete", value: function complete() { this.isPaused = false; this.isRunning = false; this.emit('complete', this.parent, this); } }]); return TickTask; }(ComponentBase); var TICKINGMODE = { 'no': 0, 'lazy': 1, 'always': 2 }; var GetValue$1 = Phaser.Utils.Objects.GetValue; var SceneUpdateTickTask = /*#__PURE__*/function (_TickTask) { _inherits(SceneUpdateTickTask, _TickTask); var _super = _createSuper(SceneUpdateTickTask); function SceneUpdateTickTask(parent, config) { var _this; _classCallCheck(this, SceneUpdateTickTask); _this = _super.call(this, parent, config); // scene update : update, preupdate, postupdate, prerender, render // game update : step, poststep, // If this.scene is not available, use game's 'step' event var defaultEventName = _this.scene ? 'update' : 'step'; _this.tickEventName = GetValue$1(config, 'tickEventName', defaultEventName); _this.isSceneTicker = !IsGameUpdateEvent(_this.tickEventName); return _this; } _createClass(SceneUpdateTickTask, [{ key: "startTicking", value: function startTicking() { _get(_getPrototypeOf(SceneUpdateTickTask.prototype), "startTicking", this).call(this); if (this.isSceneTicker) { this.scene.sys.events.on(this.tickEventName, this.update, this); } else { this.game.events.on(this.tickEventName, this.update, this); } } }, { key: "stopTicking", value: function stopTicking() { _get(_getPrototypeOf(SceneUpdateTickTask.prototype), "stopTicking", this).call(this); if (this.isSceneTicker && this.scene) { // Scene might be destoryed this.scene.sys.events.off(this.tickEventName, this.update, this); } else if (this.game) { this.game.events.off(this.tickEventName, this.update, this); } } // update(time, delta) { // // } }]); return SceneUpdateTickTask; }(TickTask); var IsGameUpdateEvent = function IsGameUpdateEvent(eventName) { return eventName === 'step' || eventName === 'poststep'; }; var GetDisplayWidth = function GetDisplayWidth(gameObject) { if (gameObject.displayWidth !== undefined) { return gameObject.displayWidth; } else { return gameObject.width; } }; var GetDisplayHeight = function GetDisplayHeight(gameObject) { if (gameObject.displayHeight !== undefined) { return gameObject.displayHeight; } else { return gameObject.height; } }; var Rectangle$1 = Phaser.Geom.Rectangle; var Vector2 = Phaser.Math.Vector2; var RotateAround = Phaser.Math.RotateAround; var GetBounds = function GetBounds(gameObject, output) { if (output === undefined) { output = new Rectangle$1(); } else if (output === true) { if (GlobRect === undefined) { GlobRect = new Rectangle$1(); } output = GlobRect; } if (gameObject.getBounds) { return gameObject.getBounds(output); } // We can use the output object to temporarily store the x/y coords in: var TLx, TLy, TRx, TRy, BLx, BLy, BRx, BRy; // Instead of doing a check if parent container is // defined per corner we only do it once. if (gameObject.parentContainer) { var parentMatrix = gameObject.parentContainer.getBoundsTransformMatrix(); GetTopLeft(gameObject, output); parentMatrix.transformPoint(output.x, output.y, output); TLx = output.x; TLy = output.y; GetTopRight(gameObject, output); parentMatrix.transformPoint(output.x, output.y, output); TRx = output.x; TRy = output.y; GetBottomLeft(gameObject, output); parentMatrix.transformPoint(output.x, output.y, output); BLx = output.x; BLy = output.y; GetBottomRight(gameObject, output); parentMatrix.transformPoint(output.x, output.y, output); BRx = output.x; BRy = output.y; } else { GetTopLeft(gameObject, output); TLx = output.x; TLy = output.y; GetTopRight(gameObject, output); TRx = output.x; TRy = output.y; GetBottomLeft(gameObject, output); BLx = output.x; BLy = output.y; GetBottomRight(gameObject, output); BRx = output.x; BRy = output.y; } output.x = Math.min(TLx, TRx, BLx, BRx); output.y = Math.min(TLy, TRy, BLy, BRy); output.width = Math.max(TLx, TRx, BLx, BRx) - output.x; output.height = Math.max(TLy, TRy, BLy, BRy) - output.y; return output; }; var GlobRect = undefined; var GetTopLeft = function GetTopLeft(gameObject, output, includeParent) { if (output === undefined) { output = new Vector2(); } else if (output === true) { if (GlobVector === undefined) { GlobVector = new Vector2(); } output = GlobVector; } if (gameObject.getTopLeft) { return gameObject.getTopLeft(output); } output.x = gameObject.x - GetDisplayWidth(gameObject) * gameObject.originX; output.y = gameObject.y - GetDisplayHeight(gameObject) * gameObject.originY; return PrepareBoundsOutput(gameObject, output, includeParent); }; var GetTopRight = function GetTopRight(gameObject, output, includeParent) { if (output === undefined) { output = new Vector2(); } else if (output === true) { if (GlobVector === undefined) { GlobVector = new Vector2(); } output = GlobVector; } if (gameObject.getTopRight) { return gameObject.getTopRight(output); } output.x = gameObject.x - GetDisplayWidth(gameObject) * gameObject.originX + GetDisplayWidth(gameObject); output.y = gameObject.y - GetDisplayHeight(gameObject) * gameObject.originY; return PrepareBoundsOutput(gameObject, output, includeParent); }; var GetBottomLeft = function GetBottomLeft(gameObject, output, includeParent) { if (output === undefined) { output = new Vector2(); } else if (output === true) { if (GlobVector === undefined) { GlobVector = new Vector2(); } output = GlobVector; } if (gameObject.getBottomLeft) { return gameObject.getBottomLeft(output); } output.x = gameObject.x - GetDisplayWidth(gameObject) * gameObject.originX; output.y = gameObject.y - GetDisplayHeight(gameObject) * gameObject.originY + GetDisplayHeight(gameObject); return PrepareBoundsOutput(gameObject, output, includeParent); }; var GetBottomRight = function GetBottomRight(gameObject, output, includeParent) { if (output === undefined) { output = new Vector2(); } else if (output === true) { if (GlobVector === undefined) { GlobVector = new Vector2(); } output = GlobVector; } if (gameObject.getBottomRight) { return gameObject.getBottomRight(output); } output.x = gameObject.x - GetDisplayWidth(gameObject) * gameObject.originX + GetDisplayWidth(gameObject); output.y = gameObject.y - GetDisplayHeight(gameObject) * gameObject.originY + GetDisplayHeight(gameObject); return PrepareBoundsOutput(gameObject, output, includeParent); }; var GlobVector = undefined; var PrepareBoundsOutput = function PrepareBoundsOutput(gameObject, output, includeParent) { if (includeParent === undefined) { includeParent = false; } if (gameObject.rotation !== 0) { RotateAround(output, gameObject.x, gameObject.y, gameObject.rotation); } if (includeParent && gameObject.parentContainer) { var parentMatrix = gameObject.parentContainer.getBoundsTransformMatrix(); parentMatrix.transformPoint(output.x, output.y, output); } return output; }; var Rectangle = Phaser.Geom.Rectangle; var GetValue = Phaser.Utils.Objects.GetValue; var Bounds = /*#__PURE__*/function (_TickTask) { _inherits(Bounds, _TickTask); var _super = _createSuper(Bounds); function Bounds(gameObject, config) { var _this; _classCallCheck(this, Bounds); if (config === undefined) { config = {}; } config.tickEventName = 'postupdate'; _this = _super.call(this, gameObject, config); // this.parent = gameObject; _this.bounds = new Rectangle(); _this.boundsTarget = undefined; _this.boundsEnable = {}; _this.clearHitResult(); _this.resetFromJSON(config); return _this; } _createClass(Bounds, [{ key: "resetFromJSON", value: function resetFromJSON(o) { var target = GetValue(o, 'target'); if (target) { this.setBoundsTarget(target); } else { this.setBoundsTarget(); this.setBounds(GetValue(o, 'bounds')); } this.setEnable(GetValue(o, 'enable', true)); this.setAlignMode(GetValue(o, 'alignMode', 0)); return this; } }, { key: "shutdown", value: function shutdown(fromScene) { // Already shutdown if (this.isShutdown) { return; } _get(_getPrototypeOf(Bounds.prototype), "shutdown", this).call(this, fromScene); } }, { key: "setBoundsTarget", value: function setBoundsTarget(gameObject) { this.boundsTarget = gameObject; return this; } }, { key: "setBounds", value: function setBounds(boundsConfig) { if (!boundsConfig) { return this; } var bounds = this.bounds; bounds.setSize(GetValue(boundsConfig, 'width', 0), GetValue(boundsConfig, 'height', 0)); if (boundsConfig.hasOwnProperty('centerX')) { bounds.centerX = boundsConfig.centerX; } else { bounds.x = GetValue(boundsConfig, 'x', 0); } if (boundsConfig.hasOwnProperty('centerY')) { bounds.centerY = boundsConfig.centerY; } else { bounds.y = GetValue(boundsConfig, 'y', 0); } return this; } }, { key: "setEnable", value: function setEnable(enable) { if (enable === undefined) { enable = true; } var boundsEnable = this.boundsEnable; if (typeof enable === 'boolean') { boundsEnable.left = enable; boundsEnable.right = enable; boundsEnable.top = enable; boundsEnable.bottom = enable; } else { boundsEnable.left = GetValue(enable, 'left', false); boundsEnable.right = GetValue(enable, 'right', false); boundsEnable.top = GetValue(enable, 'top', false); boundsEnable.bottom = GetValue(enable, 'bottom', false); } this.isRunning = this.enable; return this; } }, { key: "setAlignMode", value: function setAlignMode(mode) { if (typeof mode === 'string') { mode = AlignMode[mode]; } this.alignMode = mode; return this; } }, { key: "enable", get: function get() { var boundsEnable = this.boundsEnable; return boundsEnable.left || boundsEnable.right || boundsEnable.top || boundsEnable.bottom; }, set: function set(value) { this.setEnable(value); } }, { key: "update", value: function update(time, delta) { var gameObject = this.parent; this.clearHitResult(); if (!this.enable) { return this; } var target = this.boundsTarget; if (target) { GetBounds(target, this.bounds); } var bounds = this.bounds; var boundsEnable = this.boundsEnable; var alignToGOBound = this.alignMode === 0; var gameObjectBounds = alignToGOBound ? GetBounds(gameObject, true) : undefined; if (boundsEnable.left) { var left = alignToGOBound ? gameObjectBounds.left : gameObject.x; var dx = bounds.left - left; if (dx > 0) { gameObject.x += dx; this.isHitAny = true; this.isHitLeft = true; this.emit('hitleft', this.parent, this); } } if (boundsEnable.right) { var right = alignToGOBound ? gameObjectBounds.right : gameObject.x; var dx = bounds.right - right; if (dx < 0) { gameObject.x += dx; this.isHitAny = true; this.isHitRight = true; this.emit('hitright', this.parent, this); } } if (boundsEnable.top) { var top = alignToGOBound ? gameObjectBounds.top : gameObject.y; var dy = bounds.top - top; if (dy > 0) { gameObject.y += dy; this.isHitAny = true; this.isHitTop = true; this.emit('hittop', this.parent, this); } } if (boundsEnable.bottom) { var bottom = alignToGOBound ? gameObjectBounds.bottom : gameObject.y; var dy = bounds.bottom - bottom; if (dy < 0) { gameObject.y += dy; this.isHitAny = true; this.isHitBottom = true; this.emit('hitbottom', this.parent, this); } } if (this.isHitAny) { this.emit('hitany', this.parent, this); } } }, { key: "clearHitResult", value: function clearHitResult() { this.isHitAny = false; this.isHitLeft = false; this.isHitRight = false; this.isHitTop = false; this.isHitBottom = false; return this; } }]); return Bounds; }(SceneUpdateTickTask); var AlignMode = { bounds: 0, origin: 1 }; var BoundsPlugin = /*#__PURE__*/function (_Phaser$Plugins$BaseP) { _inherits(BoundsPlugin, _Phaser$Plugins$BaseP); var _super = _createSuper(BoundsPlugin); function BoundsPlugin(pluginManager) { _classCallCheck(this, BoundsPlugin); return _super.call(this, pluginManager); } _createClass(BoundsPlugin, [{ key: "start", value: function start() { var eventEmitter = this.game.events; eventEmitter.on('destroy', this.destroy, this); } }, { key: "add", value: function add(gameObject, config) { return new Bounds(gameObject, config); } }]); return BoundsPlugin; }(Phaser.Plugins.BasePlugin); return BoundsPlugin; }));