Slash-The-Hordes/assets/Scripts/Game/Unit/UnitHealth.ts
2022-12-20 13:43:44 +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(points);
}
public damage(points: number): void {
this.healthPoints -= points;
this.healthPointsChangeEvent.trigger(-points);
}
public setMaxHealth(maxHealth: number): void {
this.maxHealthPoints = maxHealth;
}
}