phaser3-rex-plugins
Version:
2 lines (1 loc) • 10.8 kB
JavaScript
!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).rextouchcursorplugin=e();}(undefined,(function(){function t(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function e(t,e){for(var n=0;n<e.length;n++){var i=e[n];i.enumerable=i.enumerable||!1,i.configurable=!0,"value"in i&&(i.writable=!0),Object.defineProperty(t,(r=i.key,o=void 0,"symbol"==typeof(o=function(t,e){if("object"!=typeof t||null===t)return t;var n=t[Symbol.toPrimitive];if(void 0!==n){var i=n.call(t,e||"default");if("object"!=typeof i)return i;throw new TypeError("@@toPrimitive must return a primitive value.")}return ("string"===e?String:Number)(t)}(r,"string"))?o:String(o)),i);}var r,o;}function n(t,n,i){return n&&e(t.prototype,n),i&&e(t,i),Object.defineProperty(t,"prototype",{writable:!1}),t}function i(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 s(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 u(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 n,i=r(t);if(e){var o=r(this).constructor;n=Reflect.construct(i,arguments,o);}else n=i.apply(this,arguments);return s(this,n)}}function h(){return h="undefined"!=typeof Reflect&&Reflect.get?Reflect.get.bind():function(t,e,n){var i=function(t,e){for(;!Object.prototype.hasOwnProperty.call(t,e)&&null!==(t=r(t)););return t}(t,e);if(i){var o=Object.getOwnPropertyDescriptor(i,e);return o.get?o.get.call(arguments.length<3?t:n):o.value}},h.apply(this,arguments)}var a=Phaser.Input.Keyboard.Key,c=Phaser.Input.Keyboard.KeyCodes,l=function(){function e(n){t(this,e),this.cursorKeys={up:new a(n,c.UP),down:new a(n,c.DOWN),left:new a(n,c.LEFT),right:new a(n,c.RIGHT)},this.noKeyDown=!0;}return n(e,[{key:"shutdown",value:function(t){for(var e in this.cursorKeys)this.cursorKeys[e].destroy();this.cursorKeys=void 0;}},{key:"destroy",value:function(t){shutdown(t);}},{key:"createCursorKeys",value:function(){return this.cursorKeys}},{key:"setKeyState",value:function(t,e){var n=this.cursorKeys[t];return n.enabled?(e&&(this.noKeyDown=!1),n.isDown!==e&&(y.timeStamp=Date.now(),y.keyCode=n.keyCode,e?n.onDown(y):n.onUp(y)),this):this}},{key:"clearAllKeysState",value:function(){for(var t in this.noKeyDown=!0,this.cursorKeys)this.setKeyState(t,!1);return this}},{key:"getKeyState",value:function(t){return this.cursorKeys[t]}},{key:"upKeyDown",get:function(){return this.cursorKeys.up.isDown}},{key:"downKeyDown",get:function(){return this.cursorKeys.down.isDown}},{key:"leftKeyDown",get:function(){return this.cursorKeys.left.isDown}},{key:"rightKeyDown",get:function(){return this.cursorKeys.right.isDown}},{key:"anyKeyDown",get:function(){return !this.noKeyDown}}]),e}(),y={timeStamp:0,keyCode:0,altKey:!1,ctrlKey:!1,shiftKey:!1,metaKey:!1,location:0},f=180/Math.PI,v={"up&down":0,"left&right":1,"4dir":2,"8dir":3},d={},p=Phaser.Utils.Objects.GetValue,m=Phaser.Math.Distance.Between,w=Phaser.Math.Angle.Between,b=function(e){i(o,e);var r=u(o);function o(e,n){var i;return t(this,o),(i=r.call(this,e)).resetFromJSON(n),i}return n(o,[{key:"resetFromJSON",value:function(t){null==this.start&&(this.start={x:0,y:0}),null==this.end&&(this.end={x:0,y:0}),this._enable=void 0,this.setEnable(p(t,"enable",!0)),this.setMode(p(t,"dir","8dir")),this.setDistanceThreshold(p(t,"forceMin",16));var e=p(t,"start.x",null),n=p(t,"start.y",null),i=p(t,"end.x",null),r=p(t,"end.y",null);return this.setVector(e,n,i,r),this}},{key:"toJSON",value:function(){return {enable:this.enable,dir:this.dirMode,forceMin:this.forceMin,start:{x:this.start.x,y:this.start.y},end:{x:this.end.x,y:this.end.y}}}},{key:"setMode",value:function(t){return "string"==typeof t&&(t=v[t]),this.dirMode=t,this}},{key:"enable",get:function(){return this._enable},set:function(t){if(this._enable!==t)return t||this.clearVector(),this._enable=t,this}},{key:"setEnable",value:function(t){return void 0===t&&(t=!0),this.enable=t,this}},{key:"toggleEnable",value:function(){return this.setEnable(!this.enable),this}},{key:"setDistanceThreshold",value:function(t){return t<0&&(t=0),this.forceMin=t,this}},{key:"clearVector",value:function(){return this.start.x=0,this.start.y=0,this.end.x=0,this.end.y=0,this.clearAllKeysState(),this}},{key:"setVector",value:function(t,e,n,i){if(!this.enable)return this;if(null===t)return this.clearVector(),this;if(void 0===n&&(n=t,t=0,i=e,e=0),this.start.x=t,this.start.y=e,this.end.x=n,this.end.y=i,this.forceMin>0&&this.force<this.forceMin)return this.clearVector(),this;this.noKeyDown=!0;var r=function(t,e,n){switch(void 0===n?n={}:!0===n&&(n=d),n.left=!1,n.right=!1,n.up=!1,n.down=!1,t=(t+360)%360,e){case 0:t<180?n.down=!0:n.up=!0;break;case 1:t>90&&t<=270?n.left=!0:n.right=!0;break;case 2:t>45&&t<=135?n.down=!0:t>135&&t<=225?n.left=!0:t>225&&t<=315?n.up=!0:n.right=!0;break;case 3:t>22.5&&t<=67.5?(n.down=!0,n.right=!0):t>67.5&&t<=112.5?n.down=!0:t>112.5&&t<=157.5?(n.down=!0,n.left=!0):t>157.5&&t<=202.5?n.left=!0:t>202.5&&t<=247.5?(n.left=!0,n.up=!0):t>247.5&&t<=292.5?n.up=!0:t>292.5&&t<=337.5?(n.up=!0,n.right=!0):n.right=!0;}return n}(this.angle,this.dirMode,!0);for(var o in r)this.setKeyState(o,r[o]);return this}},{key:"forceX",get:function(){return this.end.x-this.start.x}},{key:"forceY",get:function(){return this.end.y-this.start.y}},{key:"force",get:function(){return m(this.start.x,this.start.y,this.end.x,this.end.y)}},{key:"rotation",get:function(){return w(this.start.x,this.start.y,this.end.x,this.end.y)}},{key:"angle",get:function(){return this.rotation*f}},{key:"octant",get:function(){var t=0;return this.rightKeyDown?t=this.downKeyDown?45:0:this.downKeyDown?t=this.leftKeyDown?135:90:this.leftKeyDown?t=this.upKeyDown?225:180:this.upKeyDown&&(t=this.rightKeyDown?315:270),t}}]),o}(l),E={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):[]}},g={},K=Phaser.Utils.Objects.GetValue,_=Phaser.Geom.Circle,k=Phaser.Geom.Circle.Contains,D=function(e){i(s,e);var o=u(s);function s(e,n){var i;t(this,s);var r=e.scene;i=o.call(this,r,n);var u=K(n,"eventEmitter",void 0),h=K(n,"EventEmitterClass",void 0);return i.setEventEmitter(u,h),i.scene=r,i.mainCamera=r.sys.cameras.main,i.pointer=void 0,i.gameObject=e,i.radius=K(n,"radius",100),e.setInteractive(new _(e.displayOriginX,e.displayOriginY,i.radius),k),i.boot(),i}return n(s,[{key:"resetFromJSON",value:function(t){return h(r(s.prototype),"resetFromJSON",this).call(this,t),this.pointer=void 0,this}},{key:"toJSON",value:function(){var t=h(r(s.prototype),"toJSON",this).call(this);return t.radius=this.radius,t}},{key:"boot",value:function(){this.gameObject.on("pointerdown",this.onKeyDownStart,this),this.gameObject.on("pointerover",this.onKeyDownStart,this),this.scene.input.on("pointermove",this.onKeyDown,this),this.scene.input.on("pointerup",this.onKeyUp,this),this.gameObject.once("destroy",this.onParentDestroy,this);}},{key:"shutdown",value:function(t){this.scene&&(this.scene.input.off("pointermove",this.onKeyDown,this),this.scene.input.off("pointerup",this.onKeyUp,this),this.destroyEventEmitter(),this.scene=void 0,this.mainCamera=void 0,this.pointer=void 0,this.gameObject=void 0,h(r(s.prototype),"shutdown",this).call(this));}},{key:"destroy",value:function(t){this.shutdown(t);}},{key:"onParentDestroy",value:function(t,e){this.destroy(e);}},{key:"onKeyDownStart",value:function(t){t.isDown&&void 0===this.pointer&&(this.pointer=t,this.onKeyDown(t),this.emit("pointerdown",t));}},{key:"onKeyDown",value:function(t){if(this.pointer===t){var e=t.camera;if(e){var n=this.gameObject,i=this.end;e!==this.mainCamera?i=function(t,e,n,i){return void 0===i?i={}:!0===i&&(i=g),n.getWorldPoint(t,e,i),i}(t.x,t.y,e,i):(i.x=t.worldX,i.y=t.worldY);var r=n.x,o=n.y;0===n.scrollFactorX&&(r+=e.scrollX),0===n.scrollFactorY&&(o+=e.scrollY),this.setVector(r,o,i.x,i.y),this.emit("update");}}}},{key:"onKeyUp",value:function(t){this.pointer===t&&(this.pointer=void 0,this.clearVector(),this.emit("update"),this.emit("pointerup",t));}},{key:"forceUpdate",value:function(){var t=this.pointer;return t&&t.isDown?(this.onKeyDown(t),this):this}}]),s}(b);return Object.assign(D.prototype,E),function(e){i(o,Phaser.Plugins.BasePlugin);var r=u(o);function o(e){return t(this,o),r.call(this,e)}return n(o,[{key:"start",value:function(){this.game.events.on("destroy",this.destroy,this);}},{key:"add",value:function(t,e){return new D(t,e)}}]),o}()}));