UNPKG

phaser4-rex-plugins

Version:
2 lines (1 loc) 12.8 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 i=Phaser.Game;var r=function(t){return t instanceof i};const n=Phaser.Utils.Objects.GetValue;class h{constructor(t,e){this.setParent(t),this.isShutdown=!1,this.setEventEmitter(n(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:r(t)?t:r(t.game)?t.game:s(t)?t.sys.game:s(t.scene)?t.scene.sys.game:void 0}(t),this}}Object.assign(h.prototype,t);const a=Phaser.Utils.Objects.GetValue;class o extends h{constructor(t,e){super(t,e),this._isRunning=!1,this.isPaused=!1,this.tickingState=!1,this.setTickingMode(a(e,"tickingMode",1))}boot(){2!==this.tickingMode||this.tickingState||this.startTicking()}shutdown(t){this.isShutdown||(this.stop(),this.tickingState&&this.stopTicking(),super.shutdown(t))}setTickingMode(t){"string"==typeof t&&(t=u[t]),this.tickingMode=t}startTicking(){this.tickingState=!0}stopTicking(){this.tickingState=!1}get isRunning(){return this._isRunning}set isRunning(t){this._isRunning!==t&&(this._isRunning=t,1===this.tickingMode&&t!=this.tickingState&&(t?this.startTicking():this.stopTicking()))}start(){return this.isPaused=!1,this.isRunning=!0,this}pause(){return this.isRunning&&(this.isPaused=!0,this.isRunning=!1),this}resume(){return this.isPaused&&(this.isPaused=!1,this.isRunning=!0),this}stop(){return this.isPaused=!1,this.isRunning=!1,this}complete(){this.isPaused=!1,this.isRunning=!1,this.emit("complete",this.parent,this)}}const u={no:0,lazy:1,always:2},m=Phaser.Utils.Objects.GetValue;class v extends o{constructor(t,e){super(t,e);var s=this.scene?"update":"step";this.tickEventName=m(e,"tickEventName",s),this.isSceneTicker=!p(this.tickEventName)}startTicking(){super.startTicking(),this.isSceneTicker?this.scene.sys.events.on(this.tickEventName,this.update,this):this.game.events.on(this.tickEventName,this.update,this)}stopTicking(){super.stopTicking(),this.isSceneTicker&&this.scene?this.scene.sys.events.off(this.tickEventName,this.update,this):this.game&&this.game.events.off(this.tickEventName,this.update,this)}}var p=function(t){return"step"===t||"poststep"===t};const c=Phaser.Utils.Objects.GetValue,d=Phaser.Math.Clamp;class l{constructor(t){this.resetFromJSON(t)}resetFromJSON(t){this.state=c(t,"state",y),this.timeScale=c(t,"timeScale",1),this.delay=c(t,"delay",0),this.repeat=c(t,"repeat",0),this.repeatCounter=c(t,"repeatCounter",0),this.repeatDelay=c(t,"repeatDelay",0),this.duration=c(t,"duration",0),this.nowTime=c(t,"nowTime",0),this.justRestart=c(t,"justRestart",!1)}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}}destroy(){}setTimeScale(t){return this.timeScale=t,this}setDelay(t){return void 0===t&&(t=0),this.delay=t,this}setDuration(t){return this.duration=t,this}setRepeat(t){return this.repeat=t,this}setRepeatInfinity(){return this.repeat=-1,this}setRepeatDelay(t){return this.repeatDelay=t,this}start(){return this.nowTime=this.delay>0?-this.delay:0,this.state=this.nowTime>=0?E:g,this.repeatCounter=0,this}stop(){return this.state=y,this}update(t,e){this.state!==y&&this.state!==_&&0!==e&&0!==this.timeScale&&(this.nowTime+=e*this.timeScale,this.justRestart=!1,this.nowTime>=this.duration?-1===this.repeat||this.repeatCounter<this.repeat?(this.repeatCounter++,this.justRestart=!0,this.nowTime-=this.duration,this.repeatDelay>0&&(this.nowTime-=this.repeatDelay,this.state=f)):(this.nowTime=this.duration,this.state=_):this.nowTime>=0&&(this.state=E))}get t(){var t;switch(this.state){case y:case g:case f:t=0;break;case E:t=this.nowTime/this.duration;break;case _:t=1}return d(t,0,1)}set t(t){(t=d(t,-1,1))<0?(this.state=g,this.nowTime=-this.delay*t):(this.state=E,this.nowTime=this.duration*t,1===t&&0!==this.repeat&&this.repeatCounter++)}setT(t){return this.t=t,this}get isIdle(){return this.state===y}get isDelay(){return this.state===g}get isCountDown(){return this.state===E}get isRunning(){return this.state===g||this.state===E}get isDone(){return this.state===_}get isOddIteration(){return!(1&~this.repeatCounter)}get isEvenIteration(){return!(1&this.repeatCounter)}}const y=0,g=1,E=2,f=3,_=-1;class T extends v{constructor(t,e){super(t,e),this.timer=new l}shutdown(t){this.isShutdown||(super.shutdown(t),this.timer.destroy(),this.timer=void 0)}start(){return this.timer.start(),super.start(),this}stop(){return this.timer.stop(),super.stop(),this}complete(){return this.timer.stop(),super.complete(),this}}const w=Phaser.Utils.Objects.GetValue,M=Phaser.Utils.Objects.GetAdvancedValue,P=Phaser.Tweens.Builders.GetEaseFunction;class D extends T{resetFromJSON(t){return this.timer.resetFromJSON(w(t,"timer")),this.setEnable(w(t,"enable",!0)),this.setTarget(w(t,"target",this.parent)),this.setDelay(M(t,"delay",0)),this.setDuration(M(t,"duration",1e3)),this.setEase(w(t,"ease","Linear")),this.setRepeat(w(t,"repeat",0)),this}setEnable(t){return null==t&&(t=!0),this.enable=t,this}setTarget(t){return void 0===t&&(t=this.parent),this.target=t,this}setDelay(t){return this.delay=t,this}setDuration(t){return this.duration=t,this}setRepeat(t){return this.repeat=t,this}setRepeatDelay(t){return this.repeatDelay=t,this}setEase(t){return void 0===t&&(t="Linear"),this.ease=t,this.easeFn=P(t),this}start(){return this.timer.isRunning||super.start(),this}restart(){return this.timer.stop(),this.start.apply(this,arguments),this}stop(t){return void 0===t&&(t=!1),super.stop(),t&&(this.timer.setT(1),this.updateTarget(this.target,this.timer),this.complete()),this}update(t,e){if(!this.isRunning||!this.enable||this.parent.hasOwnProperty("active")&&!this.parent.active)return this;var s=this.target,i=this.timer;return i.update(t,e),i.isDelay||this.updateTarget(s,i),this.emit("update",s,this),i.isDone&&this.complete(),this}updateTarget(t,e){}}const R=Phaser.Utils.Objects.GetValue,k=Phaser.Utils.Objects.GetAdvancedValue,S=Phaser.Math.Linear;class b extends D{constructor(t,e){super(t,e),this.resetFromJSON(e),this.boot()}resetFromJSON(t){if(super.resetFromJSON(t),this.setMode(R(t,"mode",0)),t&&(t.hasOwnProperty("x")||t.hasOwnProperty("y"))){var e=k(t,"x",void 0),s=k(t,"y",void 0);this.setTargetPosition(e,s)}else this.setTargetPosition(t);return this}setMode(t){return"string"==typeof t&&(t=O[t]),this.mode=t,this}setTargetPosition(t,e){if("number"==typeof t||"number"==typeof e)this.startX=this.parent.x,this.startY=this.parent.y,this.endX=t,this.endY=e;else{var s=t;this.startX=k(s,"startX",void 0),this.startY=k(s,"startY",void 0),this.endX=k(s,"endX",void 0),this.endY=k(s,"endY",void 0)}return this.hasMoveX=void 0!==this.startX&&void 0!==this.endX,this.hasMoveY=void 0!==this.startY&&void 0!==this.endY,this}start(){if(this.timer.isRunning)return this;var t=this.parent;return this.hasMoveX&&(t.x=this.startX),this.hasMoveY&&(t.y=this.startY),this.timer.setDelay(this.delay).setDuration(this.duration).setRepeat(2===this.mode?-1:0),super.start(),this}updateTarget(t,e){var s=e.t;e.isOddIteration&&(s=1-s),s=this.easeFn(s),this.hasMoveX&&(t.x=S(this.startX,this.endX,s)),this.hasMoveY&&(t.y=S(this.startY,this.endY,s))}complete(){return super.complete(),1===this.mode&&this.parent.destroy(),this}}const O={stop:0,destroy:1,yoyo:2};var j=function(t,e){if("number"==typeof t)return t;var s=t[0],i=parseFloat(t.substr(2));switch(s){case"+":return e+i;case"-":return e-i;case"*":return e*i;case"/":return e/i}},N=function(t,e,s,i,r,n,h){n instanceof b&&(h=n,n=void 0),void 0===n&&(n=!1);var a={};return a.mode=n?1:0,void 0!==s&&(a.startX=t.x,a.endX=j(s,t.x)),void 0!==i&&(a.startY=t.y,a.endY=j(i,t.y)),a.duration=e,a.ease=void 0===r?"Linear":r,void 0===h?h=new b(t,a):h.resetFromJSON(a),h.restart(),h},F=function(t,e,s,i,r,n,h){n instanceof b&&(h=n,n=void 0),void 0===n&&(n=!1);var a={};return a.mode=n?1:0,void 0!==s&&(a.startX=j(s,t.x),a.endX=t.x),void 0!==i&&(a.startY=j(i,t.y),a.endY=t.y),a.duration=e,a.ease=void 0===r?"Linear":r,void 0===h?h=new b(t,a):h.resetFromJSON(a),h.restart(),h},x=function(t){return function(t,e){return new Promise((function(s,i){t.once(e,(function(){s()}))}))}(t,"complete")};const X=Phaser.Utils.Objects.IsPlainObject,Y=Phaser.Math.Distance.Between;var C={onInitEaseMove(){var t=this,e=this._easeMove;e.completeEventName=void 0,e.on("complete",(function(){e.completeEventName&&(t.emit(e.completeEventName,t),e.completeEventName=void 0)}))},moveFrom(t,e,s,i,r){if(X(t)){var n=t;e=n.x,s=n.y,t=n.hasOwnProperty("speed")?1e3*Y(e,s,this.x,this.y)/n.speed:n.duration,i=n.ease}var h=void 0===this._easeMove;return this._easeMove=F(this,t,e,s,i,r,this._easeMove),h&&this.onInitEaseMove(),this._easeMove.completeEventName="movefrom.complete",this},moveFromPromise(t,e,s,i,r){return this.moveFrom(t,e,s,i,r),x(this._easeMove)},moveFromDestroy(t,e,s,i){return this.moveFrom(t,e,s,i,!0),this},moveFromDestroyPromise(t,e,s,i){return this.moveFromDestroy(t,e,s,i),x(this._easeMove)},isRunningMoveFrom(){return this._easeMove&&(this._easeMove.completeEventName="movefrom.complete")},moveTo(t,e,s,i,r){if(X(t)){var n=t;e=n.x,s=n.y,t=n.hasOwnProperty("speed")?1e3*Y(e,s,this.x,this.y)/n.speed:n.duration,i=n.ease}var h=void 0===this._easeMove;return this._easeMove=N(this,t,e,s,i,r,this._easeMove),h&&this.onInitEaseMove(),this._easeMove.completeEventName,this},moveToPromise(t,e,s,i,r){return this.moveTo(t,e,s,i,r),x(this._easeMove)},moveToDestroy(t,e,s,i){return this.moveTo(t,e,s,i,!0),this},moveToDestroyPromise(t,e,s,i){return this.moveToDestroy(t,e,s,i,!0),x(this._easeMove)},isRunningMoveTo(){return this._easeMove&&"moveto.complete"===this._easeMove.completeEventName},isRunningEaseMove(){return this.isRunningMoveFrom()||this.isRunningMoveTo()},moveStop(t){return this._easeMove?(this._easeMove.stop(t),this):this}};class G extends Phaser.Plugins.BasePlugin{constructor(t){super(t)}start(){this.game.events.on("destroy",this.destroy,this)}add(t,e){return new b(t,e)}injectMethods(t){return Object.assign(t,C),t}injectMethodsToRootClass(){return this.injectMethods(Phaser.GameObjects.GameObject.prototype),this}}var L={moveTo:N,moveFrom:F,moveToDestroy:function(t,e,s,i,r,n){return N(t,e,s,i,r,!0,n)},moveFromDestroy:function(t,e,s,i,r,n){return F(t,e,s,i,r,!0,n)}};return Object.assign(G.prototype,L),G},"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t="undefined"!=typeof globalThis?globalThis:t||self).rexeasemoveplugin=e();