overrun/js/systems/XPSystem.js

35 lines
711 B
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

export class XPSystem {
constructor(scene) {
this.scene = scene;
this.xp = 0;
this.level = 1;
this.xpToNext = this._threshold(1);
}
/** XP needed to go from level `lvl` to `lvl+1`. */
_threshold(lvl) {
return 100 + (lvl - 1) * 75;
}
addXP(amount) {
this.xp += amount;
while (this.xp >= this.xpToNext) {
this.xp -= this.xpToNext;
this.level++;
this.xpToNext = this._threshold(this.level);
this.scene.events.emit('level-up', this.level);
}
}
/** 01 progress toward next level. */
get progress() {
return this.xp / this.xpToNext;
}
reset() {
this.xp = 0;
this.level = 1;
this.xpToNext = this._threshold(1);
}
}