phaser4-rex-plugins
Version:
2 lines (1 loc) • 4.86 kB
JavaScript
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,a=Phaser.Math.Vector2,v=Phaser.Math.DegToRad,p=Phaser.Math.Angle.Between,c=Phaser.Math.Linear;class m extends h{constructor(t,e){super(t,{eventEmitter:!1}),this._t=0,this.pathVector=new a,this.spacePoints=void 0,this.resetFromJSON(e)}resetFromJSON(t){this.setPath(o(t,"path",void 0));var e=o(t,"rotateToPath",!1),s=o(t,"rotationOffset",void 0);void 0===s&&(s=v(o(t,"angleOffset",0))),this.setRotateToPath(e,s);var i=o(t,"spacedPoints",!1);i?this.setSpacedPointsMode(o(i,"divisions",void 0),o(i,"stepRate",10)):this.setSpacedPointsMode(!1);var n=o(t,"t",void 0);return void 0!==n&&this.setT(n),this}toJSON(){return{path:this.path,t:this.t,rotateToPath:this.rotateToPath,rotationOffset:this.rotationOffset}}setPath(t){return this.path=t,this}setT(t){return this.t=t,this}get t(){return this._t}set t(t){this._t=t,this.update()}setRotateToPath(t,e){return this.rotateToPath=t,this.rotationOffset=e,this}setSpacedPointsMode(t,e){return t||e?(this.spacePoints=this.path.getSpacedPoints(t,e,this.spacePoints),this.spacePoints.push(this.path.getPoint(1))):this.spacePoints=void 0,this}getPoint(t){if(void 0===this.spacePoints)return this.path.getPoint(this.t,this.pathVector);var e=(this.spacePoints.length-1)*t,s=Math.floor(e),i=this.spacePoints[s],n=this.spacePoints[s+1];if(n){var r=e-s;this.pathVector.x=c(i.x,n.x,r),this.pathVector.y=c(i.y,n.y,r)}else this.pathVector.x=i.x,this.pathVector.y=i.y;return this.pathVector}update(){if(void 0!==this.path){var t=this.parent,e=t.x,s=t.y;this.pathVector=this.getPoint(this._t);var i=this.pathVector.x,n=this.pathVector.y;e===i&&s===n||(t.setPosition(i,n),this.rotateToPath&&(t.rotation=p(e,s,i,n)+this.rotationOffset))}}}class u extends Phaser.Plugins.BasePlugin{constructor(t){super(t)}start(){this.game.events.on("destroy",this.destroy,this)}add(t,e){return new m(t,e)}}return u},"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t="undefined"!=typeof globalThis?globalThis:t||self).rexpathfollowerplugin=e();