fgm24/Assets/Scripts/Player/HealthComponent.cs

59 lines
1.3 KiB
C#
Raw Normal View History

2024-02-03 01:14:34 +01:00
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;
}
2024-02-03 18:07:52 +01:00
public int getMaxHealth() {
return maxHealth;
}
public void setMaxHealth(int amount, bool heal = false) {
maxHealth = amount;
if (heal)
currentHealth = amount;
}
2024-02-03 01:14:34 +01:00
public void TakeDamage(int damage)
{
currentHealth -= damage;
OnHealthChange?.Invoke(currentHealth + damage, currentHealth);
if (currentHealth <= 0) {
2024-02-03 01:14:34 +01:00
OnHealthZero?.Invoke();
BloodComputeShader.Instance.createBlood(transform.position, maxHealth*10, maxHealth / 25.0f);
}
2024-02-03 01:14:34 +01:00
}
public void TakeSquezeDamage(float squezeDamage)
{
if (squezeDamage < minThreshold) return;
TakeDamage((int) Mathf.Round(squezeDamage * squezeDamageScalor));
}
2024-02-04 00:08:25 +01:00
public void EnemyKill()
2024-02-03 01:14:34 +01:00
{
Destroy(gameObject);
}
}