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

43 lines
1.2 KiB
TypeScript

import { ISignal } from "../../Services/EventSystem/ISignal";
import { Signal } from "../../Services/EventSystem/Signal";
export class UnitHealth {
private healthPoints: number;
private maxHealthPoints: number;
private healthPointsChangeEvent: Signal<number> = new Signal<number>();
public constructor(maxHealth: number) {
this.maxHealthPoints = maxHealth;
this.healthPoints = maxHealth;
}
public get IsAlive(): boolean {
return 0 < this.healthPoints;
}
public get HealthPoints(): number {
return this.healthPoints;
}
public get MaxHealthPoints(): number {
return this.maxHealthPoints;
}
public get HealthPointsChangeEvent(): ISignal<number> {
return this.healthPointsChangeEvent;
}
public heal(points: number): void {
this.healthPoints = Math.min(this.maxHealthPoints, this.healthPoints + points);
this.healthPointsChangeEvent.trigger(this.healthPoints);
}
public damage(points: number): void {
this.healthPoints -= points;
this.healthPointsChangeEvent.trigger(this.healthPoints);
}
public setMaxHealth(maxHealth: number): void {
this.maxHealthPoints = maxHealth;
}
}