UNPKG

phaser4-rex-plugins

Version:
2 lines (1 loc) 4.06 kB
var e,t;e=void 0,t=function(){const e=Phaser.Input.Keyboard.Key,t=Phaser.Input.Keyboard.KeyCodes,s=["up","down","left","right"];class n{constructor(e){this.scene=e,this.keys={},this.cursorKeys={},this.noKeyDown=!0;for(var t=0,n=s.length;t<n;t++){var i=s[t];this.addKey(i),this.cursorKeys[i]=this.keys[i]}}shutdown(e){for(var t in this.scene=void 0,this.keys)this.keys[t].destroy();this.keys=void 0,this.cursorKeys=void 0}destroy(e){shutdown(e)}createCursorKeys(){return this.cursorKeys}setKeyState(e,t){var s=this.keys[e];return s.enabled?(t&&(this.noKeyDown=!1),s.isDown!==t&&(i.timeStamp=Date.now(),i.keyCode=s.keyCode,t?s.onDown(i):s.onUp(i)),this):this}clearAllKeysState(){for(var e in this.noKeyDown=!0,this.keys)this.setKeyState(e,!1);return this}getKeyState(e){return this.keys[e]}get upKeyDown(){return this.keys.up.isDown}get downKeyDown(){return this.keys.down.isDown}get leftKeyDown(){return this.keys.left.isDown}get rightKeyDown(){return this.keys.right.isDown}get anyKeyDown(){return!this.noKeyDown}addKey(s,n){return void 0===n&&(n=s),"string"==typeof n&&(n=n.toUpperCase(),t.hasOwnProperty(n)&&(n=t[n])),this.keys[s]=new e(this.scene,n),this}addKeys(e){for(var t=0,s=e.length;t<s;t++)this.addKey(e[t]);return this}}var i={timeStamp:0,keyCode:0,altKey:!1,ctrlKey:!1,shiftKey:!1,metaKey:!1,location:0};const o=Phaser.Cameras.Scene2D.BaseCamera,r=Phaser.Geom.Rectangle;var h=function(e,t,s){return t instanceof o||(s=t,t=void 0),void 0===s?s=new r:!0===s&&(s=a),t?e.scale.getViewPort(t,s):e.scale.getViewPort(s)},a=new r;const u=Phaser.Utils.Objects.GetValue;class d extends n{constructor(e,t){super(e),this.scene=e,this._enable=void 0,this.setEnable(u(t,"enable",!0)),this.setSensitiveDistance(u(t,"sensitiveDistance",20));var s=u(t,"bounds",void 0);void 0===s?(s=h(e),this.autoUpdateViewportBounds=!0):this.autoUpdateViewportBounds=!1,this.setBounds(s),this.pointerOutGameReleaseEnable=u(t,"pointerOutGameRelease",!0),this.pointerOutBoundsReleaseEnable=u(t,"pointerOutBoundsRelease",!1),this.boot()}boot(){this.scene.input.on("pointermove",this.onPointerMove,this),this.pointerOutGameReleaseEnable&&this.scene.input.on("gameout",this.clearAllKeysState,this),this.autoUpdateViewportBounds&&this.scene.scale.on("resize",this.updateBoundsByViewport,this),this.scene.sys.events.once("shutdown",this.destroy,this)}shutdown(){this.scene&&(this.scene.input.off("pointermove",this.onPointerMove,this),this.pointerOutGameReleaseEnable&&this.scene.input.off("gameout",this.clearAllKeysState,this),this.autoUpdateViewportBounds&&this.scene.scale.off("resize",this.updateBoundsByViewport,this),this.scene.sys.events.off("shutdown",this.destroy,this),this.scene=void 0,super.shutdown())}destroy(){this.shutdown()}updateBoundsByViewport(){h(this.scene,this.bounds)}get enable(){return this._enable}set enable(e){if(this._enable!==e)return e||this.clearAllKeysState(),this._enable=e,this}setEnable(e){return void 0===e&&(e=!0),this.enable=e,this}toggleEnable(){return this.setEnable(!this.enable),this}setBounds(e){return this.bounds=e,this}getBounds(){return this.bounds}setSensitiveDistance(e){return this.sensitiveDistance=e,this}onPointerMove(e){if(this.enable){var t=e.x,s=e.y,n=this.bounds.left,i=this.bounds.right,o=this.bounds.top,r=this.bounds.bottom,h=this.sensitiveDistance,a=t>=n&&t<=n+h,u=t<=i&&t>=i-h,d=s>=o&&s<=o+h,y=s<=r&&s>=r-h;this.pointerOutBoundsReleaseEnable||(a|=t<n,u|=t>i,d|=s<o,y|=s>r),this.setKeyState("left",a),this.setKeyState("right",u),this.setKeyState("up",d),this.setKeyState("down",y)}}get up(){return this.upKeyDown}get down(){return this.downKeyDown}get left(){return this.leftKeyDown}get right(){return this.rightKeyDown}get noKey(){return this.noKeyDown}}class y extends Phaser.Plugins.BasePlugin{constructor(e){super(e)}start(){this.game.events.on("destroy",this.destroy,this)}add(e,t){return new d(e,t)}}return y},"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self).rexcursoratboundsplugin=t();