35 lines
711 B
JavaScript
35 lines
711 B
JavaScript
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);
|
||
}
|
||
}
|
||
|
||
/** 0–1 progress toward next level. */
|
||
get progress() {
|
||
return this.xp / this.xpToNext;
|
||
}
|
||
|
||
reset() {
|
||
this.xp = 0;
|
||
this.level = 1;
|
||
this.xpToNext = this._threshold(1);
|
||
}
|
||
}
|