phaser4-rex-plugins
Version:
2 lines (1 loc) • 5.49 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 s(e,t,n){this.fn=e,this.context=t,this.once=n||!1}function i(e,t,r,i,o){if("function"!=typeof r)throw new TypeError("The listener must be a function");var l=new s(r,i||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,s=[];if(0===this._eventsCount)return s;for(r in e=this._events)t.call(e,r)&&s.push(n?r.slice(1):r);return Object.getOwnPropertySymbols?s.concat(Object.getOwnPropertySymbols(e)):s},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 s=0,i=r.length,o=new Array(i);s<i;s++)o[s]=r[s].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,s,i,o){var l=n?n+e:e;if(!this._events[l])return!1;var h,a,v=this._events[l],p=arguments.length;if(v.fn){switch(v.once&&this.removeListener(e,v.fn,void 0,!0),p){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,s),!0;case 5:return v.fn.call(v.context,t,r,s,i),!0;case 6:return v.fn.call(v.context,t,r,s,i,o),!0}for(a=1,h=new Array(p-1);a<p;a++)h[a-1]=arguments[a];v.fn.apply(v.context,h)}else{var f,u=v.length;for(a=0;a<u;a++)switch(v[a].once&&this.removeListener(e,v[a].fn,void 0,!0),p){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,s);break;default:if(!h)for(f=1,h=new Array(p-1);f<p;f++)h[f-1]=arguments[f];v[a].fn.apply(v[a].context,h)}}return!0},l.prototype.on=function(e,t,n){return i(this,e,t,n,!1)},l.prototype.once=function(e,t,n){return i(this,e,t,n,!0)},l.prototype.removeListener=function(e,t,r,s){var i=n?n+e:e;if(!this._events[i])return this;if(!t)return o(this,i),this;var l=this._events[i];if(l.fn)l.fn!==t||s&&!l.once||r&&l.context!==r||o(this,i);else{for(var h=0,a=[],v=l.length;h<v;h++)(l[h].fn!==t||s&&!l[h].once||r&&l[h].context!==r)&&a.push(l[h]);a.length?this._events[i]=1===a.length?a[0]:a:o(this,i)}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 s=function(e,t,n){if(!e||"number"==typeof e)return n;if("string"==typeof t){if(e.hasOwnProperty(t))return e[t];if(-1===t.indexOf("."))return n;t=t.split(".")}for(var r=t,s=e,i=n,o=0;o<r.length;o++){if(t=r[o],!s.hasOwnProperty(t)){i=n;break}s=i=s[t]}return i};return class extends r{constructor(e){super(),this.setTable(s(e,"table")),this.setMaxLevel(s(e,"maxLevel"));var t=s(e,"exp",0),n=s(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,s=this._exp;;){var i=this.getExp(r),o=this.getExp(r+1);if(t=Math.min(o,e),this.emit("levelup",r,s,t,i,o),r===n&&t===e)break;r++,s=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();