UNPKG

phaser3-rex-plugins

Version:
2 lines (1 loc) 10.6 kB
!function(t,e){"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();}(undefined,(function(){function t(e){return t="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},t(e)}function e(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function i(t,e){for(var i=0;i<e.length;i++){var n=e[i];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(t,(s=n.key,r=void 0,"symbol"==typeof(r=function(t,e){if("object"!=typeof t||null===t)return t;var i=t[Symbol.toPrimitive];if(void 0!==i){var n=i.call(t,e||"default");if("object"!=typeof n)return n;throw new TypeError("@@toPrimitive must return a primitive value.")}return ("string"===e?String:Number)(t)}(s,"string"))?r:String(r)),n);}var s,r;}function n(t,e,n){return e&&i(t.prototype,e),n&&i(t,n),Object.defineProperty(t,"prototype",{writable:!1}),t}function s(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Super expression must either be null or a function");t.prototype=Object.create(e&&e.prototype,{constructor:{value:t,writable:!0,configurable:!0}}),Object.defineProperty(t,"prototype",{writable:!1}),e&&o(t,e);}function r(t){return r=Object.setPrototypeOf?Object.getPrototypeOf.bind():function(t){return t.__proto__||Object.getPrototypeOf(t)},r(t)}function o(t,e){return o=Object.setPrototypeOf?Object.setPrototypeOf.bind():function(t,e){return t.__proto__=e,t},o(t,e)}function u(t,e){if(e&&("object"==typeof e||"function"==typeof e))return e;if(void 0!==e)throw new TypeError("Derived constructors may only return object or undefined");return function(t){if(void 0===t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return t}(t)}function h(t){var e=function(){if("undefined"==typeof Reflect||!Reflect.construct)return !1;if(Reflect.construct.sham)return !1;if("function"==typeof Proxy)return !0;try{return Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],(function(){}))),!0}catch(t){return !1}}();return function(){var i,n=r(t);if(e){var s=r(this).constructor;i=Reflect.construct(n,arguments,s);}else i=n.apply(this,arguments);return u(this,i)}}function c(){return c="undefined"!=typeof Reflect&&Reflect.get?Reflect.get.bind():function(t,e,i){var n=function(t,e){for(;!Object.prototype.hasOwnProperty.call(t,e)&&null!==(t=r(t)););return t}(t,e);if(n){var s=Object.getOwnPropertyDescriptor(n,e);return s.get?s.get.call(arguments.length<3?t:i):s.value}},c.apply(this,arguments)}var a={setEventEmitter:function(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:function(){return this._eventEmitter&&this._privateEE&&this._eventEmitter.shutdown(),this},getEventEmitter:function(){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):[]}},f=Phaser.Scene,l=function(t){return t instanceof f},p=Phaser.Game,v=function(t){return t instanceof p},y=Phaser.Utils.Objects.GetValue,d=function(){function i(t,n){e(this,i),this.setParent(t),this.isShutdown=!1,this.setEventEmitter(y(n,"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));}return n(i,[{key:"shutdown",value:function(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);}},{key:"destroy",value:function(t){this.shutdown(t);}},{key:"onEnvDestroy",value:function(){this.destroy(!0);}},{key:"onParentDestroy",value:function(t,e){this.destroy(e);}},{key:"setParent",value:function(e){var i;return this.parent=e,this.scene=null==(i=e)||"object"!==t(i)?null:l(i)?i:i.scene&&l(i.scene)?i.scene:i.parent&&i.parent.scene&&l(i.parent.scene)?i.parent.scene:null,this.game=function(e){return null==e||"object"!==t(e)?null:v(e)?e:v(e.game)?e.game:l(e)?e.sys.game:l(e.scene)?e.scene.sys.game:void 0}(e),this}}]),i}();Object.assign(d.prototype,a);var m=Phaser.Utils.Objects.GetValue,g=function(t){s(o,t);var i=h(o);function o(t,n){var s;return e(this,o),(s=i.call(this,t,n))._isRunning=!1,s.isPaused=!1,s.tickingState=!1,s.setTickingMode(m(n,"tickingMode",1)),s}return n(o,[{key:"boot",value:function(){2!==this.tickingMode||this.tickingState||this.startTicking();}},{key:"shutdown",value:function(t){this.isShutdown||(this.stop(),this.tickingState&&this.stopTicking(),c(r(o.prototype),"shutdown",this).call(this,t));}},{key:"setTickingMode",value:function(t){"string"==typeof t&&(t=E[t]),this.tickingMode=t;}},{key:"startTicking",value:function(){this.tickingState=!0;}},{key:"stopTicking",value:function(){this.tickingState=!1;}},{key:"isRunning",get:function(){return this._isRunning},set:function(t){this._isRunning!==t&&(this._isRunning=t,1===this.tickingMode&&t!=this.tickingState&&(t?this.startTicking():this.stopTicking()));}},{key:"start",value:function(){return this.isPaused=!1,this.isRunning=!0,this}},{key:"pause",value:function(){return this.isRunning&&(this.isPaused=!0,this.isRunning=!1),this}},{key:"resume",value:function(){return this.isPaused&&(this.isRunning=!0),this}},{key:"stop",value:function(){return this.isPaused=!1,this.isRunning=!1,this}},{key:"complete",value:function(){this.isPaused=!1,this.isRunning=!1,this.emit("complete",this.parent,this);}}]),o}(d),E={no:0,lazy:1,always:2},k=Phaser.Utils.Objects.GetValue,b=function(t){s(o,t);var i=h(o);function o(t,n){var s;e(this,o);var r=(s=i.call(this,t,n)).scene?"update":"step";return s.tickEventName=k(n,"tickEventName",r),s.isSceneTicker=!_(s.tickEventName),s}return n(o,[{key:"startTicking",value:function(){c(r(o.prototype),"startTicking",this).call(this),this.isSceneTicker?this.scene.sys.events.on(this.tickEventName,this.update,this):this.game.events.on(this.tickEventName,this.update,this);}},{key:"stopTicking",value:function(){c(r(o.prototype),"stopTicking",this).call(this),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);}}]),o}(g),_=function(t){return "step"===t||"poststep"===t},w=function(t,e,i,n){var s=t.body,r=s.velocity.x,o=s.velocity.y;e===r&&i===o||(s.setVelocity(e,i),n&&n(e,i,r,o));},P=Math.PI/180,S=function(t){return t*P},O=Phaser.Utils.Objects.GetValue,R=function(t){s(r,t);var i=h(r);function r(t,n){var s;return e(this,r),(s=i.call(this,t,n)).resetFromJSON(n),s.boot(),s}return n(r,[{key:"resetFromJSON",value:function(t){return this.parent.body||this.scene.physics.add.existing(this.parent,!1),this.setEnable(O(t,"enable",!0)),this.setDirMode(O(t,"dir","8dir")),this.setSpeed(O(t,"speed",200)),this.setRotateToDirection(O(t,"rotateToDirection",!1)),this.setWrapMode(O(t,"wrap",!1),O(t,"padding",0)),this.setCursorKeys(O(t,"cursorKeys",void 0)),this}},{key:"enable",get:function(){return this.isRunning},set:function(t){this.isRunning=t,t||w(this,0,0);}},{key:"setEnable",value:function(t){return null==t&&(t=!0),this.enable=t,t&&void 0===this.body&&this.scene.physics.add.existing(this.parent,!1),this}},{key:"setDirMode",value:function(t){return "string"==typeof t&&(t=T[t]),this.dirMode=t,this}},{key:"setSpeed",value:function(t){return this.speed=t,this}},{key:"setRotateToDirection",value:function(t){return this.rotateToDirection=t,this}},{key:"setWrapMode",value:function(t,e){return void 0===t&&(t=!0),this.wrap=t,this.padding=e,this}},{key:"setCursorKeys",value:function(t){return void 0===t&&(t=this.scene.input.keyboard.createCursorKeys()),this.cursorKeys=t,this}},{key:"isLeft",get:function(){return !!this.enable&&this.cursorKeys.left.isDown}},{key:"isRight",get:function(){return !!this.enable&&this.cursorKeys.right.isDown}},{key:"isUp",get:function(){return !!this.enable&&this.cursorKeys.up.isDown}},{key:"isDown",get:function(){return !!this.enable&&this.cursorKeys.down.isDown}},{key:"update",value:function(t,e){var i=this.parent;if(!this.enable)return w(i,0,0),this;if(!i.active)return this;var n,s,r,o=(this.isUp?-1:0)+(this.isDown?1:0),u=(this.isLeft?-1:0)+(this.isRight?1:0);if(0===u&&0===o)return w(i,0,0),this;switch(this.dirMode){case 0:u=0;break;case 1:o=0;break;case 2:0!==o&&(u=0);}return 0===o?(s=this.speed*u,r=0,n=1===u?j:M):0===u?(s=0,r=this.speed*o,n=1===o?D:N):(n=Math.atan2(o,u),s=this.speed*Math.cos(n),r=this.speed*Math.sin(n)),w(i,s,r),this.rotateToDirection&&void 0!==n&&(i.rotation=n),this.wrap&&i.body.world.wrap(i,this.padding),this}}]),r}(b),T={"up&down":0,"left&right":1,"4dir":2,"8dir":3},j=S(0),D=S(90),M=S(180),N=S(270);return function(t){s(r,Phaser.Plugins.BasePlugin);var i=h(r);function r(t){return e(this,r),i.call(this,t)}return n(r,[{key:"start",value:function(){this.game.events.on("destroy",this.destroy,this);}},{key:"add",value:function(t,e){return new R(t,e)}}]),r}()}));