Files
esengine/extensions/cocos/cocos-ecs/assets/scripts/ecs/components/Health.ts

66 lines
1.6 KiB
TypeScript
Raw Normal View History

import { Component } from '@esengine/ecs-framework';
/**
*
*
*/
export class Health extends Component {
/** 当前生命值 */
public currentHealth: number = 100;
/** 最大生命值 */
public maxHealth: number = 100;
/** 是否死亡 */
public isDead: boolean = false;
/** 生命值回复速度 (每秒) */
public regenRate: number = 0;
constructor(maxHealth: number = 100) {
super();
this.maxHealth = maxHealth;
this.currentHealth = maxHealth;
}
/**
*
*/
public takeDamage(damage: number): void {
this.currentHealth = Math.max(0, this.currentHealth - damage);
this.isDead = this.currentHealth <= 0;
}
/**
*
*/
public heal(amount: number): void {
if (!this.isDead) {
this.currentHealth = Math.min(this.maxHealth, this.currentHealth + amount);
}
}
/**
*
*/
public revive(healthPercent: number = 1.0): void {
this.isDead = false;
this.currentHealth = Math.floor(this.maxHealth * Math.max(0, Math.min(1, healthPercent)));
}
/**
*
*/
public getHealthPercent(): number {
return this.maxHealth > 0 ? this.currentHealth / this.maxHealth : 0;
}
/**
*
*/
public reset(): void {
this.currentHealth = this.maxHealth;
this.isDead = false;
this.regenRate = 0;
}
}