phaser4-rex-plugins
Version:
2 lines (1 loc) • 5.64 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 s(){}function r(e,t,n){this.fn=e,this.context=t,this.once=n||!1}function i(e,t,s,i,o){if("function"!=typeof s)throw new TypeError("The listener must be a function");var l=new r(s,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 s:delete e._events[t]}function l(){this._events=new s,this._eventsCount=0}Object.create&&(s.prototype=Object.create(null),(new s).__proto__||(n=!1)),l.prototype.eventNames=function(){var e,s,r=[];if(0===this._eventsCount)return r;for(s in e=this._events)t.call(e,s)&&r.push(n?s.slice(1):s);return Object.getOwnPropertySymbols?r.concat(Object.getOwnPropertySymbols(e)):r},l.prototype.listeners=function(e){var t=n?n+e:e,s=this._events[t];if(!s)return[];if(s.fn)return[s.fn];for(var r=0,i=s.length,o=new Array(i);r<i;r++)o[r]=s[r].fn;return o},l.prototype.listenerCount=function(e){var t=n?n+e:e,s=this._events[t];return s?s.fn?1:s.length:0},l.prototype.emit=function(e,t,s,r,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,s),!0;case 4:return v.fn.call(v.context,t,s,r),!0;case 5:return v.fn.call(v.context,t,s,r,i),!0;case 6:return v.fn.call(v.context,t,s,r,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 u,f=v.length;for(a=0;a<f;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,s);break;case 4:v[a].fn.call(v[a].context,t,s,r);break;default:if(!h)for(u=1,h=new Array(p-1);u<p;u++)h[u-1]=arguments[u];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,s,r){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||r&&!l.once||s&&l.context!==s||o(this,i);else{for(var h=0,a=[],v=l.length;h<v;h++)(l[h].fn!==t||r&&!l[h].once||s&&l[h].context!==s)&&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 s,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 s extends n{shutdown(){this.removeAllListeners()}destroy(){this.removeAllListeners()}}var r=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 s=t,r=e,i=n,o=0;o<s.length;o++){if(t=s[o],!r.hasOwnProperty(t)){i=n;break}r=i=r[t]}return i};class i extends s{constructor(e){super(),this.setTable(r(e,"table")),this.setMaxLevel(r(e,"maxLevel"));var t=r(e,"exp",0),n=r(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),s=this._level,r=this._exp;;){var i=this.getExp(s),o=this.getExp(s+1);if(t=Math.min(o,e),this.emit("levelup",s,r,t,i,o),s===n&&t===e)break;s++,r=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}}class o extends Phaser.Plugins.BasePlugin{constructor(e){super(e)}start(){this.game.events.on("destroy",this.destroy,this)}add(e){return new i(e)}}return o},"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self).rexlevelcounterplugin=t();