UNPKG

phaser3-rex-plugins

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