2024-04-19 23:32:37 +02:00
|
|
|
using UnityEngine;
|
|
|
|
using UnityEngine.Events;
|
|
|
|
using System;
|
|
|
|
|
|
|
|
public class HealthComponent : MonoBehaviour
|
|
|
|
{
|
|
|
|
[SerializeField] float startHealth = 100;
|
|
|
|
|
|
|
|
public float currentHealth { get; private set; }
|
|
|
|
|
|
|
|
public static event Action<Vector3, float> OnHealthChangeAtPos;
|
|
|
|
|
2024-04-21 01:12:48 +02:00
|
|
|
public float StartHealth => startHealth;
|
2024-04-19 23:32:37 +02:00
|
|
|
public UnityEvent OnHealthZero;
|
|
|
|
public UnityEvent<float, float> OnHealthChange;
|
2024-04-21 02:34:45 +02:00
|
|
|
public UnityEvent<float> OnHealthChange2;
|
2024-04-21 02:47:54 +02:00
|
|
|
public UnityEvent OnHealthChange3;
|
2024-04-19 23:32:37 +02:00
|
|
|
|
|
|
|
void Awake()
|
|
|
|
{
|
|
|
|
currentHealth = startHealth;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void TakeDamage(float damage)
|
|
|
|
{
|
|
|
|
OnHealthChange?.Invoke(currentHealth, currentHealth - damage);
|
2024-04-21 02:34:45 +02:00
|
|
|
OnHealthChange2?.Invoke(currentHealth - damage);
|
2024-04-21 02:47:54 +02:00
|
|
|
OnHealthChange3.Invoke();
|
2024-04-19 23:32:37 +02:00
|
|
|
OnHealthChangeAtPos?.Invoke(transform.position, currentHealth - damage);
|
|
|
|
currentHealth -= damage;
|
|
|
|
|
|
|
|
currentHealth = (int) Mathf.Clamp(currentHealth, 0f, Mathf.Infinity);
|
|
|
|
if (currentHealth == 0)
|
|
|
|
OnHealthZero?.Invoke();
|
|
|
|
}
|
2024-04-20 20:48:38 +02:00
|
|
|
|
|
|
|
public void SimpleKill()
|
|
|
|
{
|
2024-04-21 10:11:44 +02:00
|
|
|
CameraController.instance.ShakeCamera(5,1);
|
|
|
|
|
2024-04-20 20:48:38 +02:00
|
|
|
Destroy(gameObject);
|
|
|
|
}
|
2024-04-19 23:32:37 +02:00
|
|
|
}
|
|
|
|
|