57 lines
1.2 KiB
C#
57 lines
1.2 KiB
C#
using UnityEngine;
|
|
using UnityEngine.Events;
|
|
using TMPro;
|
|
|
|
public class HealthComponent : MonoBehaviour, ISquezeDamageReceiver
|
|
{
|
|
[SerializeField] int maxHealth = 100;
|
|
public int currentHealth { get; private set; }
|
|
|
|
public UnityEvent OnHealthZero;
|
|
public UnityEvent<int, int> OnHealthChange;
|
|
|
|
[Header("Squeze Damage")]
|
|
[SerializeField]
|
|
float minThreshold = 1f;
|
|
|
|
[SerializeField]
|
|
float squezeDamageScalor = 1f;
|
|
|
|
void Awake()
|
|
{
|
|
currentHealth = maxHealth;
|
|
}
|
|
|
|
public int getMaxHealth() {
|
|
return maxHealth;
|
|
}
|
|
|
|
public void setMaxHealth(int amount, bool heal = false) {
|
|
maxHealth = amount;
|
|
|
|
if (heal)
|
|
currentHealth = amount;
|
|
}
|
|
|
|
public void TakeDamage(int damage)
|
|
{
|
|
currentHealth -= damage;
|
|
OnHealthChange?.Invoke(currentHealth + damage, currentHealth);
|
|
|
|
if (currentHealth <= 0)
|
|
OnHealthZero?.Invoke();
|
|
}
|
|
|
|
public void TakeSquezeDamage(float squezeDamage)
|
|
{
|
|
if (squezeDamage < minThreshold) return;
|
|
|
|
TakeDamage((int) Mathf.Round(squezeDamage * squezeDamageScalor));
|
|
}
|
|
|
|
public void SimpleKill()
|
|
{
|
|
Destroy(gameObject);
|
|
}
|
|
}
|