2022-11-16 13:04:23 +00:00
|
|
|
import { ISignal } from "../../Services/EventSystem/ISignal";
|
|
|
|
import { Signal } from "../../Services/EventSystem/Signal";
|
|
|
|
|
|
|
|
export class UnitLevel {
|
|
|
|
private xp = 0;
|
2022-12-13 14:56:13 +00:00
|
|
|
|
2022-11-16 13:04:23 +00:00
|
|
|
private currentLevel = 0;
|
|
|
|
private levelUpEvent: Signal<number> = new Signal<number>();
|
|
|
|
private xpAddedEvent: Signal<number> = new Signal<number>();
|
|
|
|
|
2022-12-13 14:56:13 +00:00
|
|
|
public constructor(private requiredXPs: number[], private xpMultiplier: number) {}
|
2022-11-16 13:04:23 +00:00
|
|
|
|
|
|
|
public addXp(points: number): void {
|
2022-12-13 14:56:13 +00:00
|
|
|
this.xp += points * this.xpMultiplier;
|
2022-11-16 13:04:23 +00:00
|
|
|
this.xpAddedEvent.trigger(this.xp);
|
|
|
|
this.tryLevelUp();
|
|
|
|
}
|
|
|
|
|
|
|
|
public get XP(): number {
|
|
|
|
return this.xp;
|
|
|
|
}
|
|
|
|
|
|
|
|
public get RequiredXP(): number {
|
|
|
|
return this.requiredXPs[this.currentLevel];
|
|
|
|
}
|
|
|
|
|
|
|
|
public get LevelUpEvent(): ISignal<number> {
|
|
|
|
return this.levelUpEvent;
|
|
|
|
}
|
|
|
|
|
|
|
|
public get XpAddedEvent(): ISignal<number> {
|
|
|
|
return this.xpAddedEvent;
|
|
|
|
}
|
|
|
|
|
|
|
|
private tryLevelUp(): void {
|
|
|
|
if (this.requiredXPs.length <= this.currentLevel) return;
|
|
|
|
if (this.xp < this.requiredXPs[this.currentLevel]) return;
|
|
|
|
|
|
|
|
this.xp -= this.requiredXPs[this.currentLevel];
|
|
|
|
this.currentLevel++;
|
|
|
|
|
|
|
|
this.levelUpEvent.trigger(this.currentLevel);
|
2022-12-19 11:00:55 +00:00
|
|
|
|
|
|
|
this.tryLevelUp();
|
2022-11-16 13:04:23 +00:00
|
|
|
}
|
|
|
|
}
|