71 lines
1.7 KiB
C#
71 lines
1.7 KiB
C#
|
using System.Collections;
|
||
|
using System.Collections.Generic;
|
||
|
using Polyart;
|
||
|
using UnityEngine;
|
||
|
|
||
|
public class HealthManager : MonoBehaviour
|
||
|
{
|
||
|
[Header("Health")]
|
||
|
public float maxHealth = 100f;
|
||
|
public float health = 100f;
|
||
|
|
||
|
[Header("Regen (optional)")]
|
||
|
public bool enableRegen = true;
|
||
|
public float regenPerSecond = 1.5f;
|
||
|
public float regenDelayAfterDamage = 4f;
|
||
|
|
||
|
bool isDead = false;
|
||
|
float regenResumeTime = 0f;
|
||
|
|
||
|
void Awake()
|
||
|
{
|
||
|
health = Mathf.Clamp(health, 0, maxHealth);
|
||
|
}
|
||
|
|
||
|
void Update()
|
||
|
{
|
||
|
if (!enableRegen || isDead) return;
|
||
|
if (Time.time < regenResumeTime) return;
|
||
|
if (health >= maxHealth) return;
|
||
|
|
||
|
health = Mathf.Min(maxHealth, health + regenPerSecond * Time.deltaTime);
|
||
|
}
|
||
|
|
||
|
public void TakeDamage(float damage)
|
||
|
{
|
||
|
if (isDead || damage <= 0f) return;
|
||
|
|
||
|
health = Mathf.Max(0f, health - damage);
|
||
|
regenResumeTime = Time.time + Mathf.Max(0f, regenDelayAfterDamage);
|
||
|
|
||
|
if (health <= 0f) Die();
|
||
|
}
|
||
|
|
||
|
public void Heal(float amount)
|
||
|
{
|
||
|
if (amount <= 0f || isDead) return;
|
||
|
health = Mathf.Min(maxHealth, health + amount);
|
||
|
}
|
||
|
|
||
|
public bool IsDead() => isDead;
|
||
|
|
||
|
void Die()
|
||
|
{
|
||
|
if (isDead) return;
|
||
|
isDead = true;
|
||
|
|
||
|
var anim = GetComponent<Animator>();
|
||
|
if (anim) anim.SetTrigger("die");
|
||
|
|
||
|
// If you have an ActionScheduler, uncomment:
|
||
|
// var scheduler = GetComponent<ActionScheduler>();
|
||
|
// if (scheduler) scheduler.CancelCurrentAction();
|
||
|
|
||
|
var cc = GetComponent<CharacterController>();
|
||
|
if (cc) cc.enabled = false;
|
||
|
|
||
|
var fps = GetComponent<FirstPersonController_Polyart>();
|
||
|
if (fps) fps.enabled = false;
|
||
|
}
|
||
|
}
|