Slash-The-Hordes/assets/Scripts/Game/Unit/UnitLevel.ts
2022-11-28 12:19:04 +01:00

47 lines
1.2 KiB
TypeScript

import { ISignal } from "../../Services/EventSystem/ISignal";
import { Signal } from "../../Services/EventSystem/Signal";
export class UnitLevel {
private xp = 0;
private requiredXPs: number[];
private currentLevel = 0;
private levelUpEvent: Signal<number> = new Signal<number>();
private xpAddedEvent: Signal<number> = new Signal<number>();
public constructor(requiredXPs: number[]) {
this.requiredXPs = requiredXPs;
}
public addXp(points: number): void {
this.xp += points;
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);
}
}