phaser4-rex-plugins
Version:
2 lines (1 loc) • 6.18 kB
JavaScript
var e,t;e=void 0,t=function(){function e(e){return e&&e.__esModule&&Object.prototype.hasOwnProperty.call(e,"default")?e.default:e}var t={exports:{}};!function(e){var t=Object.prototype.hasOwnProperty,r="~";function n(){}function s(e,t,r){this.fn=e,this.context=t,this.once=r||!1}function i(e,t,n,i,o){if("function"!=typeof n)throw new TypeError("The listener must be a function");var a=new s(n,i||e,o),h=r?r+t:t;return e._events[h]?e._events[h].fn?e._events[h]=[e._events[h],a]:e._events[h].push(a):(e._events[h]=a,e._eventsCount++),e}function o(e,t){0==--e._eventsCount?e._events=new n:delete e._events[t]}function a(){this._events=new n,this._eventsCount=0}Object.create&&(n.prototype=Object.create(null),(new n).__proto__||(r=!1)),a.prototype.eventNames=function(){var e,n,s=[];if(0===this._eventsCount)return s;for(n in e=this._events)t.call(e,n)&&s.push(r?n.slice(1):n);return Object.getOwnPropertySymbols?s.concat(Object.getOwnPropertySymbols(e)):s},a.prototype.listeners=function(e){var t=r?r+e:e,n=this._events[t];if(!n)return[];if(n.fn)return[n.fn];for(var s=0,i=n.length,o=new Array(i);s<i;s++)o[s]=n[s].fn;return o},a.prototype.listenerCount=function(e){var t=r?r+e:e,n=this._events[t];return n?n.fn?1:n.length:0},a.prototype.emit=function(e,t,n,s,i,o){var a=r?r+e:e;if(!this._events[a])return!1;var h,u,f=this._events[a],m=arguments.length;if(f.fn){switch(f.once&&this.removeListener(e,f.fn,void 0,!0),m){case 1:return f.fn.call(f.context),!0;case 2:return f.fn.call(f.context,t),!0;case 3:return f.fn.call(f.context,t,n),!0;case 4:return f.fn.call(f.context,t,n,s),!0;case 5:return f.fn.call(f.context,t,n,s,i),!0;case 6:return f.fn.call(f.context,t,n,s,i,o),!0}for(u=1,h=new Array(m-1);u<m;u++)h[u-1]=arguments[u];f.fn.apply(f.context,h)}else{var c,l=f.length;for(u=0;u<l;u++)switch(f[u].once&&this.removeListener(e,f[u].fn,void 0,!0),m){case 1:f[u].fn.call(f[u].context);break;case 2:f[u].fn.call(f[u].context,t);break;case 3:f[u].fn.call(f[u].context,t,n);break;case 4:f[u].fn.call(f[u].context,t,n,s);break;default:if(!h)for(c=1,h=new Array(m-1);c<m;c++)h[c-1]=arguments[c];f[u].fn.apply(f[u].context,h)}}return!0},a.prototype.on=function(e,t,r){return i(this,e,t,r,!1)},a.prototype.once=function(e,t,r){return i(this,e,t,r,!0)},a.prototype.removeListener=function(e,t,n,s){var i=r?r+e:e;if(!this._events[i])return this;if(!t)return o(this,i),this;var a=this._events[i];if(a.fn)a.fn!==t||s&&!a.once||n&&a.context!==n||o(this,i);else{for(var h=0,u=[],f=a.length;h<f;h++)(a[h].fn!==t||s&&!a[h].once||n&&a[h].context!==n)&&u.push(a[h]);u.length?this._events[i]=1===u.length?u[0]:u:o(this,i)}return this},a.prototype.removeAllListeners=function(e){var t;return e?(t=r?r+e:e,this._events[t]&&o(this,t)):(this._events=new n,this._eventsCount=0),this},a.prototype.off=a.prototype.removeListener,a.prototype.addListener=a.prototype.on,a.prefixed=r,a.EventEmitter=a,e.exports=a}(t);var r=e(t.exports);class n extends r{shutdown(){this.removeAllListeners()}destroy(){this.removeAllListeners()}}var s=function(e,t,r){if(!e||"number"==typeof e)return r;if("string"==typeof t){if(e.hasOwnProperty(t))return e[t];if(-1===t.indexOf("."))return r;t=t.split(".")}for(var n=t,s=e,i=r,o=0;o<n.length;o++){if(t=n[o],!s.hasOwnProperty(t)){i=r;break}s=i=s[t]}return i},i=function(e){if("number"==typeof e)return e;var t=e,r=t.day||t.d||0,n=t.hour||t.h||0,s=t.minute||t.m||0,i=t.second||t.s||0;return e=1e3*(i+=60*(s+=60*(n+=24*r)))},o=function(e,t){if(!(t>=e.length)){for(var r=e.length-1,n=e[t],s=t;s<r;s++)e[s]=e[s+1];return e.length=r,n}};class a extends n{constructor(e){super();var t=s(e,"getTimestampCallback");t||(this.setStartTimestamp(s(e,"startTimestamp")),t=h.bind(this)),this.setGetTimestampCallback(t),this.resetFromJSON(e)}resetFromJSON(e){return this.timers=s(e,"timers",[]),this}toJSON(){return{timers:this.timers}}setStartTimestamp(e){return void 0===e&&(e=(new Date).getTime()),this.startTimestamp=e-window.performance.now(),this}setGetTimestampCallback(e){return this.getCurrentTimestampCallback=e,this}addTimer(e,t,r,n){void 0===n&&(n=this.getCurrentTimestampCallback());var s={name:e,start:n,period:t=i(t),data:r};return void 0!==r&&(s.data=r),this._add(s),this}incTimerPeriod(e,t){t=i(t);for(var r=0,n=this.timers.length;r<n;r++){var s=this.timers[r];s.name===e&&(s.period+=t)}return this.emitUpdateEvent(),this}getExpiredTimers(e){void 0===e&&(e=this.getCurrentTimestampCallback());for(var t=[],r=0,n=this.timers.length;r<n;r++){var s=this.timers[r];e>=s.start+s.period&&t.push(s)}return t}popExpiredTimers(e){var t=this.getExpiredTimers(e);return this._remove(t),t}getTimersProgress(e){void 0===e&&(e=this.getCurrentTimestampCallback());for(var t=[],r=0,n=this.timers.length;r<n;r++){var s=this.timers[r],i=e-s.start,o=s.period,a=(i=Math.min(i,o))/o;t.push({name:s.name,period:o,elapsed:i,progress:a,timer:s})}return t}getTimers(e){if(void 0===e)return this.timers.slice();for(var t=[],r=0,n=this.timers.length;r<n;r++){var s=this.timers[r];s.name===e&&t.push(s)}return t}removeTimers(e){return"object"!=typeof e&&(e=this.getTimers(e)),Array.isArray(e)||(e=[e]),this._remove(e),this}clearTimers(){var e=this.getTimers();return e.reverse(),this.removeTimers(e),this}get length(){return this.timers.length}get lastTimer(){return this.timers[this.timers.length-1]}emitUpdateEvent(){return this.emit("update",this.timers),this}_add(e){this.timers.push(e),this.emit("add",e,this.timers),this.emitUpdateEvent()}_remove(e){!function(e,t,r,n){var s;if(void 0===n&&(n=e),!Array.isArray(t))return-1!==(s=e.indexOf(t))?(o(e,s),r&&r.call(n,t),t):null;for(var i=t.length-1;i>=0;){var a=t[i];-1!==(s=e.indexOf(a))?(o(e,s),r&&r.call(n,a)):t.pop(),i--}}(this.timers,e,(function(e){this.emit("remove",e,this.timers)}),this),this.emitUpdateEvent()}}var h=function(){return this.startTimestamp+window.performance.now()};class u extends Phaser.Plugins.BasePlugin{constructor(e){super(e)}start(){this.game.events.on("destroy",this.destroy,this)}add(e){return new a(e)}}return u},"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self).rexrealtimetimersplugin=t();