phaser3-rex-plugins
Version:
2 lines (1 loc) • 5.78 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,n="~";function r(){}function i(e,t,n){this.fn=e,this.context=t,this.once=n||!1}function s(e,t,r,s,o){if("function"!=typeof r)throw new TypeError("The listener must be a function");var l=new i(r,s||e,o),h=n?n+t:t;return e._events[h]?e._events[h].fn?e._events[h]=[e._events[h],l]:e._events[h].push(l):(e._events[h]=l,e._eventsCount++),e}function o(e,t){0==--e._eventsCount?e._events=new r:delete e._events[t]}function l(){this._events=new r,this._eventsCount=0}Object.create&&(r.prototype=Object.create(null),(new r).__proto__||(n=!1)),l.prototype.eventNames=function(){var e,r,i=[];if(0===this._eventsCount)return i;for(r in e=this._events)t.call(e,r)&&i.push(n?r.slice(1):r);return Object.getOwnPropertySymbols?i.concat(Object.getOwnPropertySymbols(e)):i},l.prototype.listeners=function(e){var t=n?n+e:e,r=this._events[t];if(!r)return[];if(r.fn)return[r.fn];for(var i=0,s=r.length,o=new Array(s);i<s;i++)o[i]=r[i].fn;return o},l.prototype.listenerCount=function(e){var t=n?n+e:e,r=this._events[t];return r?r.fn?1:r.length:0},l.prototype.emit=function(e,t,r,i,s,o){var l=n?n+e:e;if(!this._events[l])return!1;var h,a,v=this._events[l],f=arguments.length;if(v.fn){switch(v.once&&this.removeListener(e,v.fn,void 0,!0),f){case 1:return v.fn.call(v.context),!0;case 2:return v.fn.call(v.context,t),!0;case 3:return v.fn.call(v.context,t,r),!0;case 4:return v.fn.call(v.context,t,r,i),!0;case 5:return v.fn.call(v.context,t,r,i,s),!0;case 6:return v.fn.call(v.context,t,r,i,s,o),!0}for(a=1,h=new Array(f-1);a<f;a++)h[a-1]=arguments[a];v.fn.apply(v.context,h)}else{var u,p=v.length;for(a=0;a<p;a++)switch(v[a].once&&this.removeListener(e,v[a].fn,void 0,!0),f){case 1:v[a].fn.call(v[a].context);break;case 2:v[a].fn.call(v[a].context,t);break;case 3:v[a].fn.call(v[a].context,t,r);break;case 4:v[a].fn.call(v[a].context,t,r,i);break;default:if(!h)for(u=1,h=new Array(f-1);u<f;u++)h[u-1]=arguments[u];v[a].fn.apply(v[a].context,h)}}return!0},l.prototype.on=function(e,t,n){return s(this,e,t,n,!1)},l.prototype.once=function(e,t,n){return s(this,e,t,n,!0)},l.prototype.removeListener=function(e,t,r,i){var s=n?n+e:e;if(!this._events[s])return this;if(!t)return o(this,s),this;var l=this._events[s];if(l.fn)l.fn!==t||i&&!l.once||r&&l.context!==r||o(this,s);else{for(var h=0,a=[],v=l.length;h<v;h++)(l[h].fn!==t||i&&!l[h].once||r&&l[h].context!==r)&&a.push(l[h]);a.length?this._events[s]=1===a.length?a[0]:a:o(this,s)}return this},l.prototype.removeAllListeners=function(e){var t;return e?(t=n?n+e:e,this._events[t]&&o(this,t)):(this._events=new r,this._eventsCount=0),this},l.prototype.off=l.prototype.removeListener,l.prototype.addListener=l.prototype.on,l.prefixed=n,l.EventEmitter=l,e.exports=l}(t);var n=e(t.exports);class r extends n{shutdown(){this.removeAllListeners()}destroy(){this.removeAllListeners()}}var i=function(e,t,n,r){var i=e&&("object"==typeof e||"function"==typeof e),o=r&&("object"==typeof r||"function"==typeof r);if(!i&&!o)return n;var l=String(t);if(i&&l in e)return e[l];if(o&&l in r)return r[l];if(-1===l.indexOf("."))return n;var h=l.split(".");if(i){var a=s(e,h,n);if(a.found)return a.value}if(o){var v=s(r,h,n);if(v.found)return v.value}return n},s=function(e,t,n){for(var r=e,i=n,s=0,l=t.length;s<l;s++){var h=t[s];if(!r||"object"!=typeof r&&"function"!=typeof r||!(h in r))return o.found=!1,o;r=i=r[h]}return o.found=!0,o.value=i,o},o={};return class extends r{constructor(e){super(),this.setTable(i(e,"table")),this.setMaxLevel(i(e,"maxLevel"));var t=i(e,"exp",0),n=i(e,"level",void 0);void 0===n||this.checkLevel(n,t)||(console.error(`Level ${n} and Exp ${t} are mismatch`),n=void 0),this.resetExp(t,n)}setTable(e){var t;return this.levelTable=e,this.isLevelMapFunction=(t=e)&&"function"==typeof t,this}setMaxLevel(e){var t;return void 0===e&&(e=Array.isArray(this.levelTable)?this.levelTable.length-1:-1),t=-1!==e?this.getExp(e):-1,this.hasMaxLevel=-1!==e,this.maxLevel=e,this.maxExp=t,this}resetExp(e,t){return this.hasMaxLevel&&e>this.maxExp&&(e=this.maxExp),void 0===t&&(t=this.getLevel(e)),this._exp=e,this._level=t,this._requiredExp=this.getRequiredExpToNextLevel(t,e),this}get exp(){return this._exp}set exp(e){if(this.hasMaxLevel&&e>this.maxExp&&(e=this.maxExp),e<this._exp)this.resetExp(e);else if(e!==this._exp){for(var t,n=this.getLevel(e,this._level),r=this._level,i=this._exp;;){var s=this.getExp(r),o=this.getExp(r+1);if(t=Math.min(o,e),this.emit("levelup",r,i,t,s,o),r===n&&t===e)break;r++,i=o}this.resetExp(e,n)}}get level(){return this._level}set level(e){this.hasMaxLevel&&e>this.maxLevel?this.exp=this.maxExp:this.exp=this.getExp(e)}get requiredExp(){return this._requiredExp}getExp(e){return void 0===e?this._exp:this.isLevelMapFunction?this.levelTable(e):(this.hasMaxLevel&&e>this.maxLevel&&(e=this.maxLevel),this.levelTable[e])}getLevel(e,t){if(void 0===e)return this._level;for(void 0===t&&(t=0);;){var n=this.getExp(t+1);if(n>e)break;if(t++,this.hasMaxLevel&&n===this.maxExp)break}return t}getRequiredExpToNextLevel(e,t){return void 0===e&&(e=this.level),void 0===t&&(t=this.exp),this.getExp(e+1)-t}checkLevel(e,t){return t>=this.getExp(e)&&t<this.getExp(e+1)}gainExp(e,t,n){return t&&this.on("levelup",t,n),this.exp+=e,t&&this.off("levelup",t,n),this}setExp(e,t,n){return t&&this.on("levelup",t,n),this.exp=e,t&&this.off("levelup",t,n),this}setLevel(e,t,n){return t&&this.on("levelup",t,n),this.level=e,t&&this.off("levelup",t,n),this}}},"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self).rexlevelcounter=t();