phaser4-rex-plugins
Version:
2 lines (1 loc) • 1.46 kB
JavaScript
var t,e;t=void 0,e=function(){const t=Phaser.Utils.Objects.GetValue;class e{constructor(e){this.state=s,this.setKey(t(e,"key","away")),this.setPeriod(t(e,"period",1e3))}destroy(){this.stop()}get awayTime(){var t=localStorage.getItem(this.key);if(this.start(),null==t)return 0;t=parseInt(t);var e=this.curTime;return t<0||t>e?0:e-t}get curTime(){return(new Date).getTime()}start(){return this.stop(),this.updateTime(),this.timer=setInterval(this.updateTime.bind(this),this.period),this.state=i,this}stop(){return this.state===s||(clearTimeout(this.timer),this.timer=void 0,this.state=s),this}updateTime(){return localStorage.setItem(this.key,this.curTime),this}setKey(t){return this.key=t,this}setPeriod(t){return this.period=t,this}}const s=0,i=1;class r extends Phaser.Plugins.BasePlugin{constructor(t){super(t)}start(){this.game.events.on("destroy",this.destroy,this)}destroy(){this._defaultAwayTimer&&this._defaultAwayTimer.destroy(),super.destroy()}add(t){return new e(t)}get defaultAwayTimer(){return this._defaultAwayTimer||(this._defaultAwayTimer=this.add()),this._defaultAwayTimer}get awayTime(){return this.defaultAwayTimer.awayTime}setKey(t){return this.defaultAwayTimer.setKey(t),this}setPeriod(t){return this.defaultAwayTimer.setPeriod(t),this}}return r},"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t="undefined"!=typeof globalThis?globalThis:t||self).rexawaytimeplugin=e();