UNPKG

phaser4-rex-plugins

Version:
2 lines (1 loc) 13.7 kB
var t,e;t=void 0,e=function(){var t={setEventEmitter(t,e){return void 0===e&&(e=Phaser.Events.EventEmitter),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(){return this._eventEmitter&&this._eventEmitter.on.apply(this._eventEmitter,arguments),this},once(){return this._eventEmitter&&this._eventEmitter.once.apply(this._eventEmitter,arguments),this},off(){return this._eventEmitter&&this._eventEmitter.off.apply(this._eventEmitter,arguments),this},emit(t){return this._eventEmitter&&t&&this._eventEmitter.emit.apply(this._eventEmitter,arguments),this},addListener(){return this._eventEmitter&&this._eventEmitter.addListener.apply(this._eventEmitter,arguments),this},removeListener(){return this._eventEmitter&&this._eventEmitter.removeListener.apply(this._eventEmitter,arguments),this},removeAllListeners(){return this._eventEmitter&&this._eventEmitter.removeAllListeners.apply(this._eventEmitter,arguments),this},listenerCount(){return this._eventEmitter?this._eventEmitter.listenerCount.apply(this._eventEmitter,arguments):0},listeners(){return this._eventEmitter?this._eventEmitter.listeners.apply(this._eventEmitter,arguments):[]},eventNames(){return this._eventEmitter?this._eventEmitter.eventNames.apply(this._eventEmitter,arguments):[]}};const e=Phaser.Scene;var s=function(t){return t instanceof e};const n=Phaser.Game;var i=function(t){return t instanceof n};const r=Phaser.Utils.Objects.GetValue;class o{constructor(t,e){this.setParent(t),this.isShutdown=!1,this.setEventEmitter(r(e,"eventEmitter",!0)),this.parent&&(this.parent===this.scene?this.scene.sys.events.once("shutdown",this.onEnvDestroy,this):this.parent===this.game?this.game.events.once("shutdown",this.onEnvDestroy,this):this.parent.once&&this.parent.once("destroy",this.onParentDestroy,this))}shutdown(t){this.isShutdown||(this.parent&&(this.parent===this.scene?this.scene.sys.events.off("shutdown",this.onEnvDestroy,this):this.parent===this.game?this.game.events.off("shutdown",this.onEnvDestroy,this):this.parent.once&&this.parent.off("destroy",this.onParentDestroy,this)),this.destroyEventEmitter(),this.parent=void 0,this.scene=void 0,this.game=void 0,this.isShutdown=!0)}destroy(t){this.shutdown(t)}onEnvDestroy(){this.destroy(!0)}onParentDestroy(t,e){this.destroy(e)}setParent(t){var e;return this.parent=t,this.scene=null==(e=t)||"object"!=typeof e?null:s(e)?e:e.scene&&s(e.scene)?e.scene:e.parent&&e.parent.scene&&s(e.parent.scene)?e.parent.scene:null,this.game=function(t){return null==t||"object"!=typeof t?null:i(t)?t:i(t.game)?t.game:s(t)?t.sys.game:s(t.scene)?t.scene.sys.game:void 0}(t),this}}function h(t){return t&&t.__esModule&&Object.prototype.hasOwnProperty.call(t,"default")?t.default:t}Object.assign(o.prototype,t);var a={exports:{}};!function(t){var e=Object.prototype.hasOwnProperty,s="~";function n(){}function i(t,e,s){this.fn=t,this.context=e,this.once=s||!1}function r(t,e,n,r,o){if("function"!=typeof n)throw new TypeError("The listener must be a function");var h=new i(n,r||t,o),a=s?s+e:e;return t._events[a]?t._events[a].fn?t._events[a]=[t._events[a],h]:t._events[a].push(h):(t._events[a]=h,t._eventsCount++),t}function o(t,e){0==--t._eventsCount?t._events=new n:delete t._events[e]}function h(){this._events=new n,this._eventsCount=0}Object.create&&(n.prototype=Object.create(null),(new n).__proto__||(s=!1)),h.prototype.eventNames=function(){var t,n,i=[];if(0===this._eventsCount)return i;for(n in t=this._events)e.call(t,n)&&i.push(s?n.slice(1):n);return Object.getOwnPropertySymbols?i.concat(Object.getOwnPropertySymbols(t)):i},h.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 i=0,r=n.length,o=new Array(r);i<r;i++)o[i]=n[i].fn;return o},h.prototype.listenerCount=function(t){var e=s?s+t:t,n=this._events[e];return n?n.fn?1:n.length:0},h.prototype.emit=function(t,e,n,i,r,o){var h=s?s+t:t;if(!this._events[h])return!1;var a,u,p=this._events[h],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,i),!0;case 5:return p.fn.call(p.context,e,n,i,r),!0;case 6:return p.fn.call(p.context,e,n,i,r,o),!0}for(u=1,a=new Array(v-1);u<v;u++)a[u-1]=arguments[u];p.fn.apply(p.context,a)}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,i);break;default:if(!a)for(c=1,a=new Array(v-1);c<v;c++)a[c-1]=arguments[c];p[u].fn.apply(p[u].context,a)}}return!0},h.prototype.on=function(t,e,s){return r(this,t,e,s,!1)},h.prototype.once=function(t,e,s){return r(this,t,e,s,!0)},h.prototype.removeListener=function(t,e,n,i){var r=s?s+t:t;if(!this._events[r])return this;if(!e)return o(this,r),this;var h=this._events[r];if(h.fn)h.fn!==e||i&&!h.once||n&&h.context!==n||o(this,r);else{for(var a=0,u=[],p=h.length;a<p;a++)(h[a].fn!==e||i&&!h[a].once||n&&h[a].context!==n)&&u.push(h[a]);u.length?this._events[r]=1===u.length?u[0]:u:o(this,r)}return this},h.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},h.prototype.off=h.prototype.removeListener,h.prototype.addListener=h.prototype.on,h.prefixed=s,h.EventEmitter=h,t.exports=h}(a);var u=h(a.exports);class p extends u{shutdown(){this.removeAllListeners()}destroy(){this.removeAllListeners()}}var v={setEventEmitter(t,e){return void 0===e&&(e=p),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):[]}},c=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,i=t,r=s,o=0;o<n.length;o++){if(e=n[o],!i.hasOwnProperty(e)){r=s;break}i=r=i[e]}return r};const l=["next","exit","enter"];let d=class{constructor(t){var e=c(t,"states",void 0);e&&this.addStates(e);var s=c(t,"extend",void 0);if(s)for(var n in s)this.hasOwnProperty(n)&&void 0!==this[n]||(this[n]=s[n]);var i=c(t,"eventEmitter",void 0),r=c(t,"EventEmitterClass",void 0);this.setEventEmitter(i,r),this._stateLock=!1,this.resetFromJSON(t)}shutdown(){this.destroyEventEmitter()}destroy(){this.shutdown()}resetFromJSON(t){this.setEnable(c(t,"enable",!0)),this.start(c(t,"start",void 0));var e=c(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,i=this[n];i&&i.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 l}addState(t,e){"string"!=typeof t&&(t=(e=t).name);for(var s=this.stateProperties,n=0,i=s.length;n<i;n++){var r=s[n],o=e[r];o&&(this[`${r}_${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,i,r){var o=this[t+"_"+this.state];if(o){var h=arguments.length;switch(h){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,i);case 6:return o.call(this,e,s,n,i,r)}for(var a=new Array(h-1),u=1;u<h;u++)a[u-1]=arguments[u];return o.apply(this,a)}}};Object.assign(d.prototype,v);var m=function(t,e,s,n,i){void 0===i&&(i=!1);var r=t._events[e];if(!r)return!1;for(var o=0,h=r.length;o<h;o++){var a=r[o];if(a.fn===s&&a.context===n&&a.once===i)return!0}return!1};const _=["next","exit","enter","update","preupdate","postupdate"];class f extends d{shutdown(){this.stopUpdate(),this.stopPreUpdate(),this.stopPostUpdate(),this._scene=void 0,super.shutdown()}resetFromJSON(t){return super.resetFromJSON(t),this._scene=c(t,"scene",void 0),this}get stateProperties(){return _}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 m(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 m(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 m(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}}class E extends f{constructor(){super({eventEmitter:!1}),this.goto("IDLE")}setCooldownTime(t){return this.cooldownTime=t,this.cooldownMode=void 0!==t,this}request(){return this.runMethod("request")}update_IDLE(){this.compensationTime=0}request_IDLE(){return this.next(),!0}next_IDLE(){if(this.cooldownMode)return"COOLDOWN"}enter_COOLDOWN(){this.remainderTime=this.cooldownTime+this.compensationTime}update_COOLDOWN(t,e){this.remainderTime-=e,this.remainderTime<0&&(this.compensationTime=this.cooldownTime>e?-this.remainderTime:0,this.goto("IDLE"))}request_COOLDOWN(){return!1}}const y=Phaser.Utils.Objects.GetValue;class w extends o{constructor(t,e){super(t,e),this._enable=void 0,this.cooldown=new E,this.parent.setInteractive(y(e,"inputConfig",void 0)),this.resetFromJSON(e),this.boot()}resetFromJSON(t){return this.pointer=void 0,this.prevIsInTouch=!1,this.isInTouching=!1,this.setEnable(y(t,"enable",!0)),this.setCooldown(y(t,"cooldown",void 0)),this}boot(){var t=this.parent;t.on("pointerdown",this.onPointIn,this),t.on("pointerover",this.onPointIn,this),t.on("pointerup",this.onPointOut,this),t.on("pointerout",this.onPointOut,this),this.scene.sys.events.on("preupdate",this.preupdate,this)}shutdown(t){this.isShutdown||(this.scene.sys.events.off("preupdate",this.preupdate,this),this.pointer=void 0,super.shutdown(t))}get enable(){return this._enable}set enable(t){if(this._enable!==t)return t||(this.prevIsInTouch=!1,this.isInTouching=!1,this.pointer=void 0),this._enable=t,this}setEnable(t){return void 0===t&&(t=!0),this.enable=t,this}get cooldownTime(){return this.cooldown.cooldownTime}set cooldownTime(t){this.cooldown.setCooldownTime(t)}setCooldown(t){return this.cooldownTime=t,this}toggleEnable(){return this.setEnable(!this.enable),this}onPointIn(t,e,s){this.enable&&t.isDown&&void 0===this.pointer&&(this.pointer=t,this.isInTouching=!0)}onPointOut(t){this.enable&&this.pointer===t&&(this.pointer=void 0,this.isInTouching=!1)}preupdate(t,e){this.cooldown.update(t,e),!this.prevIsInTouch&&this.isInTouching&&this.emit("touchstart",this,this.parent),this.isInTouching&&this.cooldown.request()&&this.emit("intouch",this,this.parent,this.pointer),this.prevIsInTouch&&!this.isInTouching&&this.emit("touchend",this,this.parent),this.prevIsInTouch=this.isInTouching}}class g extends Phaser.Plugins.BasePlugin{constructor(t){super(t)}start(){this.game.events.on("destroy",this.destroy,this)}add(t,e){return new w(t,e)}}return g},"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t="undefined"!=typeof globalThis?globalThis:t||self).rexintouchingplugin=e();