UNPKG

phaser4-rex-plugins

Version:
2 lines (1 loc) 9.11 kB
var t,e;t=void 0,e=function(){function t(t){return t&&t.__esModule&&Object.prototype.hasOwnProperty.call(t,"default")?t.default:t}var e={exports:{}};!function(t){var e=Object.prototype.hasOwnProperty,s="~";function n(){}function r(t,e,s){this.fn=t,this.context=e,this.once=s||!1}function i(t,e,n,i,o){if("function"!=typeof n)throw new TypeError("The listener must be a function");var a=new r(n,i||t,o),h=s?s+e:e;return t._events[h]?t._events[h].fn?t._events[h]=[t._events[h],a]:t._events[h].push(a):(t._events[h]=a,t._eventsCount++),t}function o(t,e){0==--t._eventsCount?t._events=new n:delete t._events[e]}function a(){this._events=new n,this._eventsCount=0}Object.create&&(n.prototype=Object.create(null),(new n).__proto__||(s=!1)),a.prototype.eventNames=function(){var t,n,r=[];if(0===this._eventsCount)return r;for(n in t=this._events)e.call(t,n)&&r.push(s?n.slice(1):n);return Object.getOwnPropertySymbols?r.concat(Object.getOwnPropertySymbols(t)):r},a.prototype.listeners=function(t){var e=s?s+t:t,n=this._events[e];if(!n)return[];if(n.fn)return[n.fn];for(var r=0,i=n.length,o=new Array(i);r<i;r++)o[r]=n[r].fn;return o},a.prototype.listenerCount=function(t){var e=s?s+t:t,n=this._events[e];return n?n.fn?1:n.length:0},a.prototype.emit=function(t,e,n,r,i,o){var a=s?s+t:t;if(!this._events[a])return!1;var h,u,p=this._events[a],v=arguments.length;if(p.fn){switch(p.once&&this.removeListener(t,p.fn,void 0,!0),v){case 1:return p.fn.call(p.context),!0;case 2:return p.fn.call(p.context,e),!0;case 3:return p.fn.call(p.context,e,n),!0;case 4:return p.fn.call(p.context,e,n,r),!0;case 5:return p.fn.call(p.context,e,n,r,i),!0;case 6:return p.fn.call(p.context,e,n,r,i,o),!0}for(u=1,h=new Array(v-1);u<v;u++)h[u-1]=arguments[u];p.fn.apply(p.context,h)}else{var c,l=p.length;for(u=0;u<l;u++)switch(p[u].once&&this.removeListener(t,p[u].fn,void 0,!0),v){case 1:p[u].fn.call(p[u].context);break;case 2:p[u].fn.call(p[u].context,e);break;case 3:p[u].fn.call(p[u].context,e,n);break;case 4:p[u].fn.call(p[u].context,e,n,r);break;default:if(!h)for(c=1,h=new Array(v-1);c<v;c++)h[c-1]=arguments[c];p[u].fn.apply(p[u].context,h)}}return!0},a.prototype.on=function(t,e,s){return i(this,t,e,s,!1)},a.prototype.once=function(t,e,s){return i(this,t,e,s,!0)},a.prototype.removeListener=function(t,e,n,r){var i=s?s+t:t;if(!this._events[i])return this;if(!e)return o(this,i),this;var a=this._events[i];if(a.fn)a.fn!==e||r&&!a.once||n&&a.context!==n||o(this,i);else{for(var h=0,u=[],p=a.length;h<p;h++)(a[h].fn!==e||r&&!a[h].once||n&&a[h].context!==n)&&u.push(a[h]);u.length?this._events[i]=1===u.length?u[0]:u:o(this,i)}return this},a.prototype.removeAllListeners=function(t){var e;return t?(e=s?s+t:t,this._events[e]&&o(this,e)):(this._events=new n,this._eventsCount=0),this},a.prototype.off=a.prototype.removeListener,a.prototype.addListener=a.prototype.on,a.prefixed=s,a.EventEmitter=a,t.exports=a}(e);var s=t(e.exports);class n extends s{shutdown(){this.removeAllListeners()}destroy(){this.removeAllListeners()}}var r={setEventEmitter(t,e){return void 0===e&&(e=n),this._privateEE=!0===t||void 0===t,this._eventEmitter=this._privateEE?new e:t,this},destroyEventEmitter(){return this._eventEmitter&&this._privateEE&&this._eventEmitter.shutdown(),this},getEventEmitter(){return this._eventEmitter},on:function(){return this._eventEmitter&&this._eventEmitter.on.apply(this._eventEmitter,arguments),this},once:function(){return this._eventEmitter&&this._eventEmitter.once.apply(this._eventEmitter,arguments),this},off:function(){return this._eventEmitter&&this._eventEmitter.off.apply(this._eventEmitter,arguments),this},emit:function(t){return this._eventEmitter&&t&&this._eventEmitter.emit.apply(this._eventEmitter,arguments),this},addListener:function(){return this._eventEmitter&&this._eventEmitter.addListener.apply(this._eventEmitter,arguments),this},removeListener:function(){return this._eventEmitter&&this._eventEmitter.removeListener.apply(this._eventEmitter,arguments),this},removeAllListeners:function(){return this._eventEmitter&&this._eventEmitter.removeAllListeners.apply(this._eventEmitter,arguments),this},listenerCount:function(){return this._eventEmitter?this._eventEmitter.listenerCount.apply(this._eventEmitter,arguments):0},listeners:function(){return this._eventEmitter?this._eventEmitter.listeners.apply(this._eventEmitter,arguments):[]},eventNames:function(){return this._eventEmitter?this._eventEmitter.eventNames.apply(this._eventEmitter,arguments):[]}},i=function(t,e,s){if(!t||"number"==typeof t)return s;if("string"==typeof e){if(t.hasOwnProperty(e))return t[e];if(-1===e.indexOf("."))return s;e=e.split(".")}for(var n=e,r=t,i=s,o=0;o<n.length;o++){if(e=n[o],!r.hasOwnProperty(e)){i=s;break}r=i=r[e]}return i};const o=["next","exit","enter"];let a=class{constructor(t){var e=i(t,"states",void 0);e&&this.addStates(e);var s=i(t,"extend",void 0);if(s)for(var n in s)this.hasOwnProperty(n)&&void 0!==this[n]||(this[n]=s[n]);var r=i(t,"eventEmitter",void 0),o=i(t,"EventEmitterClass",void 0);this.setEventEmitter(r,o),this._stateLock=!1,this.resetFromJSON(t)}shutdown(){this.destroyEventEmitter()}destroy(){this.shutdown()}resetFromJSON(t){this.setEnable(i(t,"enable",!0)),this.start(i(t,"start",void 0));var e=i(t,"init",void 0);return e&&e.call(this),this}toJSON(){return{curState:this.state,prevState:this.prevState,enable:this.enable,start:this._start}}setEnable(t){return void 0===t&&(t=!0),this.enable=t,this}toggleEnable(){return this.setEnable(!this.enable),this}set state(t){if(this.enable&&!this._stateLock&&this._state!==t){if(this._prevState=this._state,this._state=t,this._stateLock=!0,this.emit("statechange",this),null!=this._prevState){var e="exit_"+this._prevState,s=this[e];s&&s.call(this),this.emit(e,this)}if(this._stateLock=!1,null!=this._state){var n="enter_"+this._state,r=this[n];r&&r.call(this),this.emit(n,this)}}}get state(){return this._state}get prevState(){return this._prevState}start(t){return this._start=t,this._prevState=void 0,this._state=t,this}goto(t){return null!=t&&(this.state=t),this}next(){var t,e=this["next_"+this.state];return e&&(t="string"==typeof e?e:e.call(this)),this.goto(t),this}get stateProperties(){return o}addState(t,e){"string"!=typeof t&&(t=(e=t).name);for(var s=this.stateProperties,n=0,r=s.length;n<r;n++){var i=s[n],o=e[i];o&&(this[`${i}_${t}`]=o)}return this}addStates(t){if(Array.isArray(t))for(var e=0,s=t.length;e<s;e++)this.addState(t[e]);else for(var n in t)this.addState(n,t[n]);return this}runMethod(t,e,s,n,r,i){var o=this[t+"_"+this.state];if(o){var a=arguments.length;switch(a){case 1:return o.call(this);case 2:return o.call(this,e);case 3:return o.call(this,e,s);case 4:return o.call(this,e,s,n);case 5:return o.call(this,e,s,n,r);case 6:return o.call(this,e,s,n,r,i)}for(var h=new Array(a-1),u=1;u<a;u++)h[u-1]=arguments[u];return o.apply(this,h)}}};Object.assign(a.prototype,r);var h=function(t,e,s,n,r){void 0===r&&(r=!1);var i=t._events[e];if(!i)return!1;for(var o=0,a=i.length;o<a;o++){var h=i[o];if(h.fn===s&&h.context===n&&h.once===r)return!0}return!1};const u=["next","exit","enter","update","preupdate","postupdate"];class p extends a{shutdown(){this.stopUpdate(),this.stopPreUpdate(),this.stopPostUpdate(),this._scene=void 0,super.shutdown()}resetFromJSON(t){return super.resetFromJSON(t),this._scene=i(t,"scene",void 0),this}get stateProperties(){return u}update(t,e){this.runMethod("update",t,e)}preupdate(t,e){this.runMethod("preupdate",t,e)}postupdate(t,e){this.runMethod("postupdate",t,e)}startUpdate(t){t||(t=this._scene);var e=t.sys.events;return h(e,"update",this.update,this)||(this._scene=t,e.on("update",this.update,this)),this}stopUpdate(){return this._scene?(this._scene.sys.events.off("update",this.update,this),this):this}startPreUpdate(t){t||(t=this._scene);var e=t.sys.events;return h(e,"preupdate",this.preupdate,this)||(this._scene=t,e.on("preupdate",this.preupdate,this)),this}stopPreUpdate(){return this._scene?(this._scene.sys.events.off("preupdate",this.preupdate,this),this):this}startPostUpdate(t){t||(t=this._scene);var e=t.sys.events;return h(e,"postupdate",this.postupdate,this)||(this._scene=t,e.on("postupdate",this.postupdate,this)),this}stopPostUpdate(){return this._scene?(this._scene.sys.events.off("postupdate",this.postupdate,this),this):this}}var v=function(t){return null==t||""===t||0===t.length};class c extends Phaser.Plugins.BasePlugin{constructor(t){super(t)}start(){this.game.events.on("destroy",this.destroy,this)}add(t){return new p(t)}}return function(t,e,s,n){if(void 0===n&&(n="."),"object"==typeof t)if(v(e)){if(null==s)return;"object"==typeof s&&(t=s)}else{"string"==typeof e&&(e=e.split(n));var r=e.pop(),i=function(t,e,s){var n=t;if(v(e));else{var r;"string"==typeof e&&(e=e.split("."));for(var i=0,o=e.length;i<o;i++){var a;null!=n[r=e[i]]&&"object"==typeof n[r]||(a=i===o-1?void 0===s?{}:s:{},n[r]=a),n=n[r]}}return n}(t,e);i[r]=s}}(window,"RexPlugins.FSM",p),c},"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t="undefined"!=typeof globalThis?globalThis:t||self).rexfsmplugin=e();