UNPKG

phaser4-rex-plugins

Version:
8 lines (7 loc) 7.17 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 n=function(t){return t instanceof i};const r=Phaser.Utils.Objects.GetValue;class h{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:n(t)?t:n(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 o=Phaser.Utils.Objects.GetValue;class a extends h{constructor(t,e){super(t,e),this._isRunning=!1,this.isPaused=!1,this.tickingState=!1,this.setTickingMode(o(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},c=Phaser.Utils.Objects.GetValue;class d extends a{constructor(t,e){super(t,e);var s=this.scene?"update":"step";this.tickEventName=c(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},v=function(t,e,s,i){var n=t.body,r=n.velocity.x,h=n.velocity.y;e===r&&s===h||(n.setVelocity(e,s),i&&i(e,s,r,h))}; /** * @author Richard Davey <rich@photonstorm.com> * @copyright 2018 Photon Storm Ltd. * @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License} */ const m=Math.PI/180;var g=function(t){return t*m};const l=Phaser.Utils.Objects.GetValue;class E extends d{constructor(t,e){super(t,e),this.resetFromJSON(e),this.boot()}resetFromJSON(t){return this.parent.body||this.scene.physics.add.existing(this.parent,!1),this.setEnable(l(t,"enable",!0)),this.setDirMode(l(t,"dir","8dir")),this.setSpeed(l(t,"speed",200)),this.setRotateToDirection(l(t,"rotateToDirection",!1)),this.setWrapMode(l(t,"wrap",!1),l(t,"padding",0)),this.setCursorKeys(l(t,"cursorKeys",void 0)),this}get enable(){return this.isRunning}set enable(t){this.isRunning=t,t||v(this,0,0)}setEnable(t){return null==t&&(t=!0),this.enable=t,t&&void 0===this.body&&this.scene.physics.add.existing(this.parent,!1),this}setDirMode(t){return"string"==typeof t&&(t=y[t]),this.dirMode=t,this}setSpeed(t){return this.speed=t,this}setRotateToDirection(t){return this.rotateToDirection=t,this}setWrapMode(t,e){return void 0===t&&(t=!0),this.wrap=t,this.padding=e,this}setCursorKeys(t){return void 0===t&&(t=this.scene.input.keyboard.createCursorKeys()),this.cursorKeys=t,this}get isLeft(){var t=this.cursorKeys.left;return!(!this.enable||!t)&&t.isDown}get isRight(){var t=this.cursorKeys.right;return!(!this.enable||!t)&&t.isDown}get isUp(){var t=this.cursorKeys.up;return!(!this.enable||!t)&&t.isDown}get isDown(){var t=this.cursorKeys.down;return!(!this.enable||!t)&&t.isDown}update(t,e){var s=this.parent;if(!this.enable)return v(s,0,0),this;if(!s.active)return this;var i,n,r,h=(this.isUp?-1:0)+(this.isDown?1:0),o=(this.isLeft?-1:0)+(this.isRight?1:0);if(0===o&&0===h)return v(s,0,0),this;switch(this.dirMode){case 0:o=0;break;case 1:h=0;break;case 2:0!==h&&(o=0)}return 0===h?(n=this.speed*o,r=0,i=1===o?f:k):0===o?(n=0,r=this.speed*h,i=1===h?_:b):(i=Math.atan2(h,o),n=this.speed*Math.cos(i),r=this.speed*Math.sin(i)),v(s,n,r),this.rotateToDirection&&void 0!==i&&(s.rotation=i),this.wrap&&s.body.world.wrap(s,this.padding),this}}const y={"up&down":0,"left&right":1,"4dir":2,"8dir":3},f=g(0),_=g(90),k=g(180),b=g(270);class w extends Phaser.Plugins.BasePlugin{constructor(t){super(t)}start(){this.game.events.on("destroy",this.destroy,this)}add(t,e){return new E(t,e)}}return w},"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t="undefined"!=typeof globalThis?globalThis:t||self).rexeightdirectionplugin=e();