UNPKG

phaser4-rex-plugins

Version:
1,637 lines (1,346 loc) 78.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.rexrotate = factory()); })(this, (function () { 'use strict'; var EventEmitterMethods$1 = { 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() { if (this._eventEmitter && this._privateEE) { this._eventEmitter.shutdown(); } return this; }, getEventEmitter() { return this._eventEmitter; }, on() { if (this._eventEmitter) { this._eventEmitter.on.apply(this._eventEmitter, arguments); } return this; }, once() { if (this._eventEmitter) { this._eventEmitter.once.apply(this._eventEmitter, arguments); } return this; }, off() { if (this._eventEmitter) { this._eventEmitter.off.apply(this._eventEmitter, arguments); } return this; }, emit(event) { if (this._eventEmitter && event) { this._eventEmitter.emit.apply(this._eventEmitter, arguments); } return this; }, addListener() { if (this._eventEmitter) { this._eventEmitter.addListener.apply(this._eventEmitter, arguments); } return this; }, removeListener() { if (this._eventEmitter) { this._eventEmitter.removeListener.apply(this._eventEmitter, arguments); } return this; }, removeAllListeners() { if (this._eventEmitter) { this._eventEmitter.removeAllListeners.apply(this._eventEmitter, arguments); } return this; }, listenerCount() { if (this._eventEmitter) { return this._eventEmitter.listenerCount.apply(this._eventEmitter, arguments); } return 0; }, listeners() { if (this._eventEmitter) { return this._eventEmitter.listeners.apply(this._eventEmitter, arguments); } return []; }, eventNames() { if (this._eventEmitter) { return this._eventEmitter.eventNames.apply(this._eventEmitter, arguments); } return []; }, }; const SceneClass = Phaser.Scene; var IsSceneObject = function (object) { return (object instanceof SceneClass); }; var GetSceneObject = function (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; } }; const GameClass = Phaser.Game; var IsGame = function (object) { return (object instanceof GameClass); }; var GetGame = function (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; } }; const GetValue$6 = Phaser.Utils.Objects.GetValue; class ComponentBase { constructor(parent, config) { this.setParent(parent); // gameObject, scene, or game this.isShutdown = false; // Event emitter, default is private event emitter this.setEventEmitter(GetValue$6(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 } } 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; } destroy(fromScene) { this.shutdown(fromScene); } onEnvDestroy() { this.destroy(true); } onParentDestroy(parent, fromScene) { this.destroy(fromScene); } setParent(parent) { this.parent = parent; // gameObject, scene, or game this.scene = GetSceneObject(parent); this.game = GetGame(parent); return this; } } Object.assign( ComponentBase.prototype, EventEmitterMethods$1 ); const GetValue$5 = Phaser.Utils.Objects.GetValue; class TickTask extends ComponentBase { constructor(parent, config) { super(parent, config); this._isRunning = false; this.isPaused = false; this.tickingState = false; this.setTickingMode(GetValue$5(config, 'tickingMode', 1)); // boot() later } // override boot() { if ((this.tickingMode === 2) && (!this.tickingState)) { this.startTicking(); } } // override shutdown(fromScene) { // Already shutdown if (this.isShutdown) { return; } this.stop(); if (this.tickingState) { this.stopTicking(); } super.shutdown(fromScene); } setTickingMode(mode) { if (typeof (mode) === 'string') { mode = TICKINGMODE[mode]; } this.tickingMode = mode; } // override startTicking() { this.tickingState = true; } // override stopTicking() { this.tickingState = false; } get isRunning() { return this._isRunning; } set isRunning(value) { if (this._isRunning === value) { return; } this._isRunning = value; if ((this.tickingMode === 1) && (value != this.tickingState)) { if (value) { this.startTicking(); } else { this.stopTicking(); } } } start() { this.isPaused = false; this.isRunning = true; return this; } pause() { // Only can ba paused in running state if (this.isRunning) { this.isPaused = true; this.isRunning = false; } return this; } resume() { // Only can ba resumed in paused state (paused from running state) if (this.isPaused) { this.isPaused = false; this.isRunning = true; } return this; } stop() { this.isPaused = false; this.isRunning = false; return this; } complete() { this.isPaused = false; this.isRunning = false; this.emit('complete', this.parent, this); } } const TICKINGMODE = { 'no': 0, 'lazy': 1, 'always': 2 }; var GetDisplayWidth = function (gameObject) { if (gameObject.displayWidth !== undefined) { return gameObject.displayWidth; } else { return gameObject.width; } }; var GetDisplayHeight = function (gameObject) { if (gameObject.displayHeight !== undefined) { return gameObject.displayHeight; } else { return gameObject.height; } }; const Rectangle = Phaser.Geom.Rectangle; const Vector2 = Phaser.Math.Vector2; const RotateAround$1 = Phaser.Math.RotateAround; const P3Container = Phaser.GameObjects.Container; var GetBounds = function (gameObject, output) { if (output === undefined) { output = new Rectangle(); } else if (output === true) { if (GlobRect === undefined) { GlobRect = new Rectangle(); } output = GlobRect; } if (gameObject.getBounds && !(gameObject instanceof P3Container)) { 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 (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, includeParent); } output.x = gameObject.x - (GetDisplayWidth(gameObject) * gameObject.originX); output.y = gameObject.y - (GetDisplayHeight(gameObject) * gameObject.originY); return PrepareBoundsOutput(gameObject, output, includeParent); }; var GetTopRight = function (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, includeParent); } 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 (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, includeParent); } 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 (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, includeParent); } 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 (gameObject, output, includeParent) { if (includeParent === undefined) { includeParent = false; } if (gameObject.rotation !== 0) { RotateAround$1(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 IsPointInBounds = function (gameObject, x, y, preTest, postTest) { // Can't get bounds if (!gameObject) { return false; } if (preTest && !preTest(gameObject, x, y)) { return false; } var boundsRect = GetBounds(gameObject, true); if (!boundsRect.contains(x, y)) { return false; } if (postTest && !postTest(gameObject, x, y)) { return false; } return true; }; var GetPointerWorldXY = function (pointer, targetCamera, out) { var camera = pointer.camera; if (!camera) { return null; } if (out === undefined) { out = {}; } else if (out === true) { out = globalOut$1; } if (camera === targetCamera) { out.x = pointer.worldX; out.y = pointer.worldY; } else { camera.getWorldPoint(pointer.x, pointer.y, out); } return out; }; var globalOut$1 = {}; var PointerTest = function (gameObject, pointer, mainTest, preTest, postTest) { var mainCamera = gameObject.scene.sys.cameras.main, worldXY; var useScreenXY = (gameObject.scrollFactorX === 0) && (gameObject.scrollFactorY === 0); if (pointer) { if (useScreenXY) { return mainTest(gameObject, pointer.x, pointer.y, preTest, postTest); } else { worldXY = GetPointerWorldXY(pointer, mainCamera, true); if (!worldXY) { return false; } return mainTest(gameObject, worldXY.x, worldXY.y, preTest, postTest); } } else { var inputManager = gameObject.scene.input.manager; var pointersTotal = inputManager.pointersTotal; var pointers = inputManager.pointers; for (var i = 0; i < pointersTotal; i++) { pointer = pointers[i]; if (useScreenXY) { if (mainTest(gameObject, pointer.x, pointer.y, preTest, postTest)) { return true; } } else { worldXY = GetPointerWorldXY(pointer, mainCamera, true); if (!worldXY) { continue; } if (mainTest(gameObject, worldXY.x, worldXY.y, preTest, postTest)) { return true; } } } return false; }}; var IsPointerInBounds = function (gameObject, pointer, preTest, postTest) { return PointerTest(gameObject, pointer, IsPointInBounds, preTest, postTest) }; const GetValue$4 = Phaser.Utils.Objects.GetValue; class OnePointerTracer extends TickTask { constructor(gameObject, config) { var scene = GetSceneObject(gameObject); if (scene === gameObject) { gameObject = undefined; } super(scene, config); this.gameObject = gameObject; if (gameObject) { gameObject.setInteractive(GetValue$4(config, 'inputConfig', undefined)); } this._enable = undefined; this.resetFromJSON(config); this.boot(); } resetFromJSON(o) { this.setEnable(GetValue$4(o, 'enable', true)); this.setDetectBounds(); if (this.gameObject === undefined) { this.setDetectBounds(GetValue$4(o, 'bounds', undefined)); } else { this.setDetectBounds(); } this.tracerState = TOUCH0$1; // this.recongizedState = new stateClass(this); this.pointer = undefined; this.lastPointer = undefined; // Last catched pointer this.movedState = false; this.isTouchingAnyObject = false; return this; } boot() { super.boot(); if (this.gameObject) { this.gameObject.on('pointerdown', this.onPointerDown, this); } else { this.scene.input.on('pointerdown', this.onPointerDown, this); } this.scene.input.on('pointerup', this.onPointerUp, this); this.scene.input.on('gameout', this.dragCancel, this); this.scene.input.on('pointermove', this.onPointerMove, this); this.scene.sys.events.once('shutdown', this.destroy, this); } shutdown(fromScene) { if (!this.scene) { return } if (this.gameObject) ; else { this.scene.input.off('pointerdown', this.onPointerDown, this); } this.scene.input.off('pointerup', this.onPointerUp, this); this.scene.input.off('gameout', this.dragCancel, this); this.scene.input.off('pointermove', this.onPointerMove, this); this.scene.sys.events.off('shutdown', this.destroy, this); this.gameObject = undefined; this.bounds = undefined; this.pointer = undefined; this.lastPointer = undefined; // Last catched pointer this.movedState = false; super.shutdown(fromScene); } get enable() { return this._enable; } set enable(e) { if (this._enable === e) { return; } if (!e) { this.dragCancel(); } this._enable = e; return this; } setEnable(e) { if (e === undefined) { e = true; } this.enable = e; return this; } setDetectBounds(bounds) { this.bounds = bounds; return this; } toggleEnable() { this.setEnable(!this.enable); return this; } onPointerDown(pointer, gameObjects) { if (!this.enable) { return; } if (this.pointer !== undefined) { return; } var isInsideBounds = (this.bounds) ? this.bounds.contains(pointer.x, pointer.y) : true; if (!isInsideBounds) { return; } if (this.pointer === pointer) { return; } this.pointer = pointer; this.pointerCamera = pointer.camera; this.lastPointer = pointer; this.movedState = false; this.tracerState = TOUCH1$1; if (this.gameObject === undefined) { this.isTouchingAnyObject = (gameObjects.length > 0); } this.onDragStart(); } onPointerUp(pointer) { if (!this.enable) { return; } var isInsideBounds = (this.bounds) ? this.bounds.contains(pointer.x, pointer.y) : true; if (!isInsideBounds) { return; } if (this.pointer !== pointer) { return; } this.pointer = undefined; this.pointerCamera = undefined; this.movedState = false; this.tracerState = TOUCH0$1; this.onDragEnd(); } onPointerMove(pointer) { if (!this.enable) { return; } if (pointer.isDown) { var isInsideBounds = (this.bounds) ? this.bounds.contains(pointer.x, pointer.y) : true; var isCatchedPointer = (this.pointer === pointer); if (!isCatchedPointer && isInsideBounds) ; else if (isCatchedPointer && !isInsideBounds) { // Pointer moves out of bounds this.onPointerUp(pointer); } else { // Pointer drags in bounds if (!this.movedState) { this.movedState = (pointer.x !== pointer.downX) || (pointer.y !== pointer.downY); } if (this.movedState) { this.onDrag(); } } } } dragCancel() { if (this.tracerState === TOUCH1$1) { this.onDragEnd(); } this.pointer = undefined; this.tracerState = TOUCH0$1; return this; } onDragStart() { this.emit('dragstart', this); } onDragEnd() { this.emit('dragend', this); } onDrag() { this.emit('drag', this); } // onLastPointerMove() { } preUpdate(time, delta) { } postUpdate(time, delta) { } startTicking() { super.startTicking(); this.scene.sys.events.on('preupdate', this.preUpdate, this); this.scene.sys.events.on('postupdate', this.postUpdate, this); } stopTicking() { super.stopTicking(); if (this.scene) { // Scene might be destoryed this.scene.sys.events.off('preupdate', this.preUpdate, this); this.scene.sys.events.off('postupdate', this.postUpdate, this); } } setRecongizedStateObject(stateObject) { this.recongizedState = stateObject; return this; } get state() { return this.recongizedState.state; } set state(newState) { this.recongizedState.state = newState; } cancel() { this.state = IDLE$3; return this; } isPointerInGameObject(gameObject, preTest, postTest) { var pointer = this.lastPointer; if (!pointer) { return false; } return IsPointerInBounds(gameObject, pointer, preTest, postTest); } } const TOUCH0$1 = 0; const TOUCH1$1 = 1; const IDLE$3 = 'IDLE'; function getDefaultExportFromCjs (x) { return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x; } var eventemitter3 = {exports: {}}; (function (module) { var has = Object.prototype.hasOwnProperty , prefix = '~'; /** * Constructor to create a storage for our `EE` objects. * An `Events` instance is a plain object whose properties are event names. * * @constructor * @private */ function Events() {} // // We try to not inherit from `Object.prototype`. In some engines creating an // instance in this way is faster than calling `Object.create(null)` directly. // If `Object.create(null)` is not supported we prefix the event names with a // character to make sure that the built-in object properties are not // overridden or used as an attack vector. // if (Object.create) { Events.prototype = Object.create(null); // // This hack is needed because the `__proto__` property is still inherited in // some old browsers like Android 4, iPhone 5.1, Opera 11 and Safari 5. // if (!new Events().__proto__) prefix = false; } /** * Representation of a single event listener. * * @param {Function} fn The listener function. * @param {*} context The context to invoke the listener with. * @param {Boolean} [once=false] Specify if the listener is a one-time listener. * @constructor * @private */ function EE(fn, context, once) { this.fn = fn; this.context = context; this.once = once || false; } /** * Add a listener for a given event. * * @param {EventEmitter} emitter Reference to the `EventEmitter` instance. * @param {(String|Symbol)} event The event name. * @param {Function} fn The listener function. * @param {*} context The context to invoke the listener with. * @param {Boolean} once Specify if the listener is a one-time listener. * @returns {EventEmitter} * @private */ function addListener(emitter, event, fn, context, once) { if (typeof fn !== 'function') { throw new TypeError('The listener must be a function'); } var listener = new EE(fn, context || emitter, once) , evt = prefix ? prefix + event : event; if (!emitter._events[evt]) emitter._events[evt] = listener, emitter._eventsCount++; else if (!emitter._events[evt].fn) emitter._events[evt].push(listener); else emitter._events[evt] = [emitter._events[evt], listener]; return emitter; } /** * Clear event by name. * * @param {EventEmitter} emitter Reference to the `EventEmitter` instance. * @param {(String|Symbol)} evt The Event name. * @private */ function clearEvent(emitter, evt) { if (--emitter._eventsCount === 0) emitter._events = new Events(); else delete emitter._events[evt]; } /** * Minimal `EventEmitter` interface that is molded against the Node.js * `EventEmitter` interface. * * @constructor * @public */ function EventEmitter() { this._events = new Events(); this._eventsCount = 0; } /** * Return an array listing the events for which the emitter has registered * listeners. * * @returns {Array} * @public */ EventEmitter.prototype.eventNames = function eventNames() { var names = [] , events , name; if (this._eventsCount === 0) return names; for (name in (events = this._events)) { if (has.call(events, name)) names.push(prefix ? name.slice(1) : name); } if (Object.getOwnPropertySymbols) { return names.concat(Object.getOwnPropertySymbols(events)); } return names; }; /** * Return the listeners registered for a given event. * * @param {(String|Symbol)} event The event name. * @returns {Array} The registered listeners. * @public */ EventEmitter.prototype.listeners = function listeners(event) { var evt = prefix ? prefix + event : event , handlers = this._events[evt]; if (!handlers) return []; if (handlers.fn) return [handlers.fn]; for (var i = 0, l = handlers.length, ee = new Array(l); i < l; i++) { ee[i] = handlers[i].fn; } return ee; }; /** * Return the number of listeners listening to a given event. * * @param {(String|Symbol)} event The event name. * @returns {Number} The number of listeners. * @public */ EventEmitter.prototype.listenerCount = function listenerCount(event) { var evt = prefix ? prefix + event : event , listeners = this._events[evt]; if (!listeners) return 0; if (listeners.fn) return 1; return listeners.length; }; /** * Calls each of the listeners registered for a given event. * * @param {(String|Symbol)} event The event name. * @returns {Boolean} `true` if the event had listeners, else `false`. * @public */ EventEmitter.prototype.emit = function emit(event, a1, a2, a3, a4, a5) { var evt = prefix ? prefix + event : event; if (!this._events[evt]) return false; var listeners = this._events[evt] , len = arguments.length , args , i; if (listeners.fn) { if (listeners.once) this.removeListener(event, listeners.fn, undefined, true); switch (len) { case 1: return listeners.fn.call(listeners.context), true; case 2: return listeners.fn.call(listeners.context, a1), true; case 3: return listeners.fn.call(listeners.context, a1, a2), true; case 4: return listeners.fn.call(listeners.context, a1, a2, a3), true; case 5: return listeners.fn.call(listeners.context, a1, a2, a3, a4), true; case 6: return listeners.fn.call(listeners.context, a1, a2, a3, a4, a5), true; } for (i = 1, args = new Array(len -1); i < len; i++) { args[i - 1] = arguments[i]; } listeners.fn.apply(listeners.context, args); } else { var length = listeners.length , j; for (i = 0; i < length; i++) { if (listeners[i].once) this.removeListener(event, listeners[i].fn, undefined, true); switch (len) { case 1: listeners[i].fn.call(listeners[i].context); break; case 2: listeners[i].fn.call(listeners[i].context, a1); break; case 3: listeners[i].fn.call(listeners[i].context, a1, a2); break; case 4: listeners[i].fn.call(listeners[i].context, a1, a2, a3); break; default: if (!args) for (j = 1, args = new Array(len -1); j < len; j++) { args[j - 1] = arguments[j]; } listeners[i].fn.apply(listeners[i].context, args); } } } return true; }; /** * Add a listener for a given event. * * @param {(String|Symbol)} event The event name. * @param {Function} fn The listener function. * @param {*} [context=this] The context to invoke the listener with. * @returns {EventEmitter} `this`. * @public */ EventEmitter.prototype.on = function on(event, fn, context) { return addListener(this, event, fn, context, false); }; /** * Add a one-time listener for a given event. * * @param {(String|Symbol)} event The event name. * @param {Function} fn The listener function. * @param {*} [context=this] The context to invoke the listener with. * @returns {EventEmitter} `this`. * @public */ EventEmitter.prototype.once = function once(event, fn, context) { return addListener(this, event, fn, context, true); }; /** * Remove the listeners of a given event. * * @param {(String|Symbol)} event The event name. * @param {Function} fn Only remove the listeners that match this function. * @param {*} context Only remove the listeners that have this context. * @param {Boolean} once Only remove one-time listeners. * @returns {EventEmitter} `this`. * @public */ EventEmitter.prototype.removeListener = function removeListener(event, fn, context, once) { var evt = prefix ? prefix + event : event; if (!this._events[evt]) return this; if (!fn) { clearEvent(this, evt); return this; } var listeners = this._events[evt]; if (listeners.fn) { if ( listeners.fn === fn && (!once || listeners.once) && (!context || listeners.context === context) ) { clearEvent(this, evt); } } else { for (var i = 0, events = [], length = listeners.length; i < length; i++) { if ( listeners[i].fn !== fn || (once && !listeners[i].once) || (context && listeners[i].context !== context) ) { events.push(listeners[i]); } } // // Reset the array, or remove it completely if we have no more listeners. // if (events.length) this._events[evt] = events.length === 1 ? events[0] : events; else clearEvent(this, evt); } return this; }; /** * Remove all listeners, or those of the specified event. * * @param {(String|Symbol)} [event] The event name. * @returns {EventEmitter} `this`. * @public */ EventEmitter.prototype.removeAllListeners = function removeAllListeners(event) { var evt; if (event) { evt = prefix ? prefix + event : event; if (this._events[evt]) clearEvent(this, evt); } else { this._events = new Events(); this._eventsCount = 0; } return this; }; // // Alias methods names because people roll like that. // EventEmitter.prototype.off = EventEmitter.prototype.removeListener; EventEmitter.prototype.addListener = EventEmitter.prototype.on; // // Expose the prefix. // EventEmitter.prefixed = prefix; // // Allow `EventEmitter` to be imported as module namespace. // EventEmitter.EventEmitter = EventEmitter; // // Expose the module. // { module.exports = EventEmitter; } } (eventemitter3)); var eventemitter3Exports = eventemitter3.exports; var EE = /*@__PURE__*/getDefaultExportFromCjs(eventemitter3Exports); class EventEmitter extends EE { shutdown() { this.removeAllListeners(); } destroy() { this.removeAllListeners(); } } var EventEmitterMethods = { setEventEmitter(eventEmitter, EventEmitterClass) { if (EventEmitterClass === undefined) { EventEmitterClass = EventEmitter; } this._privateEE = (eventEmitter === true) || (eventEmitter === undefined); this._eventEmitter = (this._privateEE) ? (new EventEmitterClass()) : eventEmitter; return this; }, destroyEventEmitter() { if (this._eventEmitter && this._privateEE) { this._eventEmitter.shutdown(); } return this; }, getEventEmitter() { return this._eventEmitter; }, on: function () { if (this._eventEmitter) { this._eventEmitter.on.apply(this._eventEmitter, arguments); } return this; }, once: function () { if (this._eventEmitter) { this._eventEmitter.once.apply(this._eventEmitter, arguments); } return this; }, off: function () { if (this._eventEmitter) { this._eventEmitter.off.apply(this._eventEmitter, arguments); } return this; }, emit: function (event) { if (this._eventEmitter && event) { this._eventEmitter.emit.apply(this._eventEmitter, arguments); } return this; }, addListener: function () { if (this._eventEmitter) { this._eventEmitter.addListener.apply(this._eventEmitter, arguments); } return this; }, removeListener: function () { if (this._eventEmitter) { this._eventEmitter.removeListener.apply(this._eventEmitter, arguments); } return this; }, removeAllListeners: function () { if (this._eventEmitter) { this._eventEmitter.removeAllListeners.apply(this._eventEmitter, arguments); } return this; }, listenerCount: function () { if (this._eventEmitter) { return this._eventEmitter.listenerCount.apply(this._eventEmitter, arguments); } return 0; }, listeners: function () { if (this._eventEmitter) { return this._eventEmitter.listeners.apply(this._eventEmitter, arguments); } return []; }, eventNames: function () { if (this._eventEmitter) { return this._eventEmitter.eventNames.apply(this._eventEmitter, arguments); } return []; }, }; var GetValue$3 = function (source, key, defaultValue) { if (!source || typeof source === 'number') { return defaultValue; } if (typeof (key) === 'string') { if (source.hasOwnProperty(key)) { return source[key]; } if (key.indexOf('.') !== -1) { key = key.split('.'); } else { return defaultValue; } } var keys = key; var parent = source; var value = defaultValue; // Use for loop here so we can break early for (var i = 0; i < keys.length; i++) { key = keys[i]; if (parent.hasOwnProperty(key)) { // Yes it has a key property, let's carry on down value = parent[key]; parent = value; } else { // Can't go any further, so reset to default value = defaultValue; break; } } return value; }; const StateProperties$1 = ['next', 'exit', 'enter']; let FSM$1 = class FSM { /* var config = { start: 'A', // default: undefined states: { A: { next: 'B', // function() { return 'B'; } enter: function() {}, exit: function() {}, }, // ... }, extend: { i: 0, name: 'abc' // ... }, init: function() {}, enable: true, eventEmitter: true, }; */ constructor(config) { // Attach get-next-state function var states = GetValue$3(config, 'states', undefined); if (states) { this.addStates(states); } // Attach extend members var extend = GetValue$3(config, 'extend', undefined); if (extend) { for (var name in extend) { if (!this.hasOwnProperty(name) || this[name] === undefined) { this[name] = extend[name]; } } } // Event emitter var eventEmitter = GetValue$3(config, 'eventEmitter', undefined); var EventEmitterClass = GetValue$3(config, 'EventEmitterClass', undefined); this.setEventEmitter(eventEmitter, EventEmitterClass); this._stateLock = false; this.resetFromJSON(config); } shutdown() { this.destroyEventEmitter(); } destroy() { this.shutdown(); } resetFromJSON(o) { this.setEnable(GetValue$3(o, 'enable', true)); this.start(GetValue$3(o, 'start', undefined)); var init = GetValue$3(o, 'init', undefined); if (init) { init.call(this); } return this; } toJSON() { return { curState: this.state, prevState: this.prevState, enable: this.enable, start: this._start }; } setEnable(e) { if (e === undefined) { e = true; } this.enable = e; return this; } toggleEnable() { this.setEnable(!this.enable); return this; } set state(newState) { if (!this.enable || this._stateLock) { return; } if (this._state === newState) { return; } this._prevState = this._state; this._state = newState; this._stateLock = true; // lock state this.emit('statechange', this); if (this._prevState != null) { var exitEventName = 'exit_' + this._prevState; var exitCallback = this[exitEventName]; if (exitCallback) { exitCallback.call(this); } this.emit(exitEventName, this); } this._stateLock = false; if (this._state != null) { var enterEventName = 'enter_' + this._state; var enterCallback = this[enterEventName]; if (enterCallback) { enterCallback.call(this); } this.emit(enterEventName, this); } } get state() { return this._state; } get prevState() { return this._prevState; } start(state) { this._start = state; this._prevState = undefined; this._state = state; // Won't fire statechange events return this; } goto(nextState) { if (nextState != null) { this.state = nextState; } return this; } next() { var nextState; var getNextState = this['next_' + this.state]; if (getNextState) { if (typeof (getNextState) === 'string') { nextState = getNextState; } else { nextState = getNextState.call(this); } } this.goto(nextState); return this; } get stateProperties() { return StateProperties$1; } addState(name, state) { if (typeof (name) !== 'string') { state = name; name = state.name; } var stateProperties = this.stateProperties; for (var i = 0, cnt = stateProperties.length; i < cnt; i++) { var propertyName = stateProperties[i]; var propertyValue = state[propertyName]; if (propertyValue) { this[`${propertyName}_${name}`] = propertyValue; } } return this; } addStates(states) { if (Array.isArray(states)) { for (var i = 0, cnt = states.length; i < cnt; i++) { this.addState(states[i]); } } else { for (var name in states) { this.addState(name, states[name]); } } return this; } runMethod(methodName, a1, a2, a3, a4, a5) { var fn = this[methodName + '_' + this.state]; if (!fn) { return undefined; } // Copy from eventemitter3 var len = arguments.length; switch (len) { case 1: return fn.call(this); case 2: return fn.call(this, a1); case 3: return fn.call(this, a1, a2); case 4: return fn.call(this, a1, a2, a3); case 5: return fn.call(this, a1, a2, a3, a4); case 6: return fn.call(this, a1, a2, a3, a4, a5); } var args = new Array(len - 1); for (var i = 1; i < len; i++) { args[i - 1] = arguments[i]; } return fn.apply(this, args); } }; Object.assign( FSM$1.prototype, EventEmitterMethods, ); var HasListener = function (eventEmitter, eventName, fn, context, once) { if (once === undefined) { once = false; } var listeners = eventEmitter._events[eventName]; if (!listeners) { return false; } for (var i = 0, cnt = listeners.length; i < cnt; i++) { var listener = listeners[i]; if ((listener.fn === fn) && (listener.context === context) && (listener.once === once) ) { return true; } } return false; }; const StateProperties = ['next', 'exit', 'enter', 'update', 'preupdate', 'postupdate']; class FSM extends FSM$1 { /* var config = { start: 'A', // default: undefined states: { A: { next: 'B', // function() { return 'B'; } enter: function() {}, exit: function() {}, update: function(time, delta) {}, preupdate: function(time, delta) {},