phaser3-rex-plugins
Version:
1,674 lines (1,580 loc) • 93 kB
JavaScript
(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.rexbejeweled = 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$1 = {
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$b = 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$b(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$1);
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);
var EventEmitter = /*#__PURE__*/function (_EE) {
_inherits(EventEmitter, _EE);
var _super = _createSuper(EventEmitter);
function EventEmitter() {
_classCallCheck(this, EventEmitter);
return _super.apply(this, arguments);
}
_createClass(EventEmitter, [{
key: "shutdown",
value: function shutdown() {
this.removeAllListeners();
}
}, {
key: "destroy",
value: function destroy() {
this.removeAllListeners();
}
}]);
return EventEmitter;
}(EE);
var EventEmitterMethods = {
setEventEmitter: function setEventEmitter(eventEmitter, EventEmitterClass) {
if (EventEmitterClass === undefined) {
EventEmitterClass = EventEmitter;
}
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 [];
}
};
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2019 Photon Storm Ltd.
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
// Source object
// The key as a string, or an array of keys, i.e. 'banner', or 'banner.hideBanner'
// The default value to use if the key doesn't exist
/**
* Retrieves a value from an object.
*
* @function Phaser.Utils.Objects.GetValue
* @since 3.0.0
*
* @param {object} source - The object to retrieve the value from.
* @param {string} key - The name of the property to retrieve from the object. If a property is nested, the names of its preceding properties should be separated by a dot (`.`) - `banner.hideBanner` would return the value of the `hideBanner` property from the object stored in the `banner` property of the `source` object.
* @param {*} defaultValue - The value to return if the `key` isn't found in the `source` object.
*
* @return {*} The value of the requested key.
*/
var GetValue$a = function GetValue(source, key, defaultValue) {
if (!source || typeof source === 'number') {
return defaultValue;
} else if (source.hasOwnProperty(key)) {
return source[key];
} else if (key.indexOf('.') !== -1) {
var keys = key.split('.');
var parent = source;
var value = defaultValue;
// Use for loop here so we can break early
for (var i = 0; i < keys.length; i++) {
if (parent.hasOwnProperty(keys[i])) {
// Yes it has a key property, let's carry on down
value = parent[keys[i]];
parent = parent[keys[i]];
} else {
// Can't go any further, so reset to default
value = defaultValue;
break;
}
}
return value;
} else {
return defaultValue;
}
};
var StateProperties$1 = ['next', 'exit', 'enter'];
var FSM$1 = /*#__PURE__*/function () {
/*
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,
};
*/
function FSM(config) {
_classCallCheck(this, FSM);
// Attach get-next-state function
var states = GetValue$a(config, 'states', undefined);
if (states) {
this.addStates(states);
}
// Attach extend members
var extend = GetValue$a(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$a(config, 'eventEmitter', undefined);
var EventEmitterClass = GetValue$a(config, 'EventEmitterClass', undefined);
this.setEventEmitter(eventEmitter, EventEmitterClass);
this._stateLock = false;
this.resetFromJSON(config);
}
_createClass(FSM, [{
key: "shutdown",
value: function shutdown() {
this.destroyEventEmitter();
}
}, {
key: "destroy",
value: function destroy() {
this.shutdown();
}
}, {
key: "resetFromJSON",
value: function resetFromJSON(o) {
this.setEnable(GetValue$a(o, 'enable', true));
this.start(GetValue$a(o, 'start', undefined));
var init = GetValue$a(o, 'init', undefined);
if (init) {
init.call(this);
}
return this;
}
}, {
key: "toJSON",
value: function toJSON() {
return {
curState: this.state,
prevState: this.prevState,
enable: this.enable,
start: this._start
};
}
}, {
key: "setEnable",
value: function setEnable(e) {
if (e === undefined) {
e = true;
}
this.enable = e;
return this;
}
}, {
key: "toggleEnable",
value: function toggleEnable() {
this.setEnable(!this.enable);
return this;
}
}, {
key: "state",
get: function get() {
return this._state;
},
set: function set(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);
}
}
}, {
key: "prevState",
get: function get() {
return this._prevState;
}
}, {
key: "start",
value: function start(state) {
this._start = state;
this._prevState = undefined;
this._state = state; // Won't fire statechange events
return this;
}
}, {
key: "goto",
value: function goto(nextState) {
if (nextState != null) {
this.state = nextState;
}
return this;
}
}, {
key: "next",
value: function 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;
}
}, {
key: "stateProperties",
get: function get() {
return StateProperties$1;
}
}, {
key: "addState",
value: function 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["".concat(propertyName, "_").concat(name)] = propertyValue;
}
}
return this;
}
}, {
key: "addStates",
value: function 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;
}
}, {
key: "runMethod",
value: function 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);
}
}]);
return FSM;
}();
Object.assign(FSM$1.prototype, EventEmitterMethods);
var HasListener = function HasListener(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;
};
var StateProperties = ['next', 'exit', 'enter', 'update', 'preupdate', 'postupdate'];
var FSM = /*#__PURE__*/function (_FSMBase) {
_inherits(FSM, _FSMBase);
var _super = _createSuper(FSM);
function FSM() {
_classCallCheck(this, FSM);
return _super.apply(this, arguments);
}
_createClass(FSM, [{
key: "shutdown",
value:
/*
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) {},
postupdate: function(time, delta) {},
},
// ...
},
extend: {
i: 0,
name: 'abc'
// ...
},
init: function() {},
enable: true,
scene: undefined,
eventEmitter: true,
};
*/
function shutdown() {
this.stopUpdate();
this.stopPreUpdate();
this.stopPostUpdate();
this._scene = undefined;
_get(_getPrototypeOf(FSM.prototype), "shutdown", this).call(this);
}
}, {
key: "resetFromJSON",
value: function resetFromJSON(o) {
_get(_getPrototypeOf(FSM.prototype), "resetFromJSON", this).call(this, o);
this._scene = GetValue$a(o, 'scene', undefined);
return this;
}
}, {
key: "stateProperties",
get: function get() {
return StateProperties;
}
}, {
key: "update",
value: function update(time, delta) {
this.runMethod('update', time, delta);
}
}, {
key: "preupdate",
value: function preupdate(time, delta) {
this.runMethod('preupdate', time, delta);
}
}, {
key: "postupdate",
value: function postupdate(time, delta) {
this.runMethod('postupdate', time, delta);
}
}, {
key: "startUpdate",
value: function startUpdate(scene) {
if (!scene) {
scene = this._scene;
}
var eventEmitter = scene.sys.events;
if (HasListener(eventEmitter, 'update', this.update, this)) {
return this;
}
this._scene = scene;
eventEmitter.on('update', this.update, this);
return this;
}
}, {
key: "stopUpdate",
value: function stopUpdate() {
if (!this._scene) {
return this;
}
this._scene.sys.events.off('update', this.update, this);
return this;
}
}, {
key: "startPreUpdate",
value: function startPreUpdate(scene) {
if (!scene) {
scene = this._scene;
}
var eventEmitter = scene.sys.events;
if (HasListener(eventEmitter, 'preupdate', this.preupdate, this)) {
return this;
}
this._scene = scene;
eventEmitter.on('preupdate', this.preupdate, this);
return this;
}
}, {
key: "stopPreUpdate",
value: function stopPreUpdate() {
if (!this._scene) {
return this;
}
this._scene.sys.events.off('preupdate', this.preupdate, this);
return this;
}
}, {
key: "startPostUpdate",
value: function startPostUpdate(scene) {
if (!scene) {
scene = this._scene;
}
var eventEmitter = scene.sys.events;
if (HasListener(eventEmitter, 'postupdate', this.postupdate, this)) {
return this;
}
this._scene = scene;
eventEmitter.on('postupdate', this.postupdate, this);
return this;
}
}, {
key: "stopPostUpdate",
value: function stopPostUpdate() {
if (!this._scene) {
return this;
}
this._scene.sys.events.off('postupdate', this.postupdate, this);
return this;
}
}]);
return FSM;
}(FSM$1);
var BaseState = /*#__PURE__*/function (_FSM) {
_inherits(BaseState, _FSM);
var _super = _createSuper(BaseState);
function BaseState(bejeweled, config) {
var _this;
_classCallCheck(this, BaseState);
_this = _super.call(this, config);
_this.bejeweled = bejeweled; // Bejeweled
_this.board = bejeweled.board; // Bejeweled.board
_this.waitEvents = bejeweled.waitEvents; // Bejeweled.waitEvents
return _this;
}
_createClass(BaseState, [{
key: "shutdown",
value: function shutdown() {
_get(_getPrototypeOf(BaseState.prototype), "shutdown", this).call(this);
this.bejeweled = undefined;
this.board = undefined;
this.waitEvents = undefined;
}
}, {
key: "destroy",
value: function destroy() {
this.shutdown();
return this;
}
}, {
key: "next",
value: function next() {
// Wait until all events are completed
if (this.waitEvents.noWaitEvent) {
// Go to next state
_get(_getPrototypeOf(BaseState.prototype), "next", this).call(this);
} else {
// Try again later
this.waitEvents.setCompleteCallback(this.next, this);
}
}
}]);
return BaseState;
}(FSM);
var GetValue$9 = 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$9(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$8 = 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$8(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 GetValue$7 = Phaser.Utils.Objects.GetValue;
var Clamp = Phaser.Math.Clamp;
var Timer = /*#__PURE__*/function () {
function Timer(config) {
_classCallCheck(this, Timer);
this.resetFromJSON(config);
}
_createClass(Timer, [{
key: "resetFromJSON",
value: function resetFromJSON(o) {
this.state = GetValue$7(o, 'state', IDLE);
this.timeScale = GetValue$7(o, 'timeScale', 1);
this.delay = GetValue$7(o, 'delay', 0);
this.repeat = GetValue$7(o, 'repeat', 0);
this.repeatCounter = GetValue$7(o, 'repeatCounter', 0);
this.repeatDelay = GetValue$7(o, 'repeatDelay', 0);
this.duration = GetValue$7(o, 'duration', 0);
this.nowTime = GetValue$7(o, 'nowTime', 0);
this.justRestart = GetValue$7(o, 'justRestart', false);
}
}, {
key: "toJSON",
value: function toJSON() {
return {
state: this.state,
timeScale: this.timeScale,
delay: this.delay,
repeat: this.repeat,
repeatCounter: this.repeatCounter,
repeatDelay: this.repeatDelay,
duration: this.duration,
nowTime: this.nowTime,
justRestart: this.justRestart
};
}
}, {
key: "destroy",
value: function destroy() {}
}, {
key: "setTimeScale",
value: function setTimeScale(timeScale) {
this.timeScale = timeScale;
return this;
}
}, {
key: "setDelay",
value: function setDelay(delay) {
if (delay === undefined) {
delay = 0;
}
this.delay = delay;
return this;
}
}, {
key: "setDuration",
value: function setDuration(duration) {
this.duration = duration;
return this;
}
}, {
key: "setRepeat",
value: function setRepeat(repeat) {
this.repeat = repeat;
return this;
}
}, {
key: "setRepeatInfinity",
value: function setRepeatInfinity() {
this.repeat = -1;
return this;
}
}, {
key: "setRepeatDelay",
value: function setRepeatDelay(repeatDelay) {
this.repeatDelay = repeatDelay;
return this;
}
}, {
key: "start",
value: function start() {
this.nowTime = this.delay > 0 ? -this.delay : 0;
this.state = this.nowTime >= 0 ? COUNTDOWN : DELAY;
this.repeatCounter = 0;
return this;
}
}, {
key: "stop",
value: function stop() {
this.state = IDLE;
return this;
}
}, {
key: "update",
value: function update(time, delta) {
if (this.state === IDLE || this.state === DONE || delta === 0 || this.timeScale === 0) {
return;
}
this.nowTime += delta * this.timeScale;
this.justRestart = false;
if (this.nowTime >= this.duration) {
if (this.repeat === -1 || this.repeatCounter < this.repeat) {
this.repeatCounter++;
this.justRestart = true;
this.nowTime -= this.duration;
if (this.repeatDelay > 0) {
this.nowTime -= this.repeatDelay;
this.state = REPEATDELAY;
}
} else {
this.nowTime = this.duration;
this.state = DONE;
}
} else if (this.nowTime >= 0) {
this.state = COUNTDOWN;
}
}
}, {
key: "t",
get: function get() {
var t;
switch (this.state) {
case IDLE:
case DELAY:
case REPEATDELAY:
t = 0;
break;
case COUNTDOWN:
t = this.nowTime / this.duration;
break;
case DONE:
t = 1;
break;
}
return Clamp(t, 0, 1);
},
set: function set(value) {
value = Clamp(value, -1, 1);
if (value < 0) {
this.state = DELAY;
this.nowTime = -this.delay * value;
} else {
this.state = COUNTDOWN;
this.nowTime = this.duration * value;
if (value === 1 && this.repeat !== 0) {
this.repeatCounter++;
}
}
}
}, {
key: "setT",
value: function setT(t) {
this.t = t;
return this;
}
}, {
key: "isIdle",
get: function get() {
return this.state === IDLE;
}
}, {
key: "isDelay",
get: function get() {
return this.state === DELAY;
}
}, {
key: "isCountDown",
get: function get() {
return this.state === COUNTDOWN;
}
}, {
key: "isRunning",
get: function get() {
return this.state === DELAY || this.state === COUNTDOWN;
}
}, {
key: "isDone",
get: function get() {
return this.state === DONE;
}
}, {
key: "isOddIteration",
get: function get() {
return (this.repeatCounter & 1) === 1;
}
}, {
key: "isEvenIteration",
get: function get() {
return (this.repeatCounter & 1) === 0;
}
}]);
return Timer;
}();
var IDLE = 0;
var DELAY = 1;
var COUNTDOWN = 2;
var REPEATDELAY = 3;
var DONE = -1;
var TimerTickTask = /*#__PURE__*/function (_TickTask) {
_inherits(TimerTickTask, _TickTask);
var _super = _createSuper(TimerTickTask);
function TimerTickTask(parent, config) {
var _this;
_classCallCheck(this, TimerTickTask);
_this = _super.call(this, parent, config);
_this.timer = new Timer();
// boot() l