3DTD/Assets/Scripts/Utilities/HealthComponent.cs

52 lines
1.3 KiB
C#

using UnityEngine;
using UnityEngine.Events;
using System;
using Cinemachine;
public class HealthComponent : MonoBehaviour
{
[SerializeField] float startHealth = 100;
public float currentHealth { get; private set; }
public static event Action<Vector3, float> OnHealthChangeAtPos;
public float StartHealth => startHealth;
public UnityEvent OnHealthZero;
public UnityEvent<float, float> OnHealthChange;
public UnityEvent<float> OnHealthChange2;
public UnityEvent OnHealthChange3;
void Awake()
{
currentHealth = startHealth;
}
public void TakeDamage(float damage)
{
OnHealthChange?.Invoke(currentHealth, currentHealth - damage);
OnHealthChange2?.Invoke(currentHealth - damage);
OnHealthChange3.Invoke();
OnHealthChangeAtPos?.Invoke(transform.position, currentHealth - damage);
currentHealth -= damage;
currentHealth = (int)Mathf.Clamp(currentHealth, 0f, startHealth);
if (currentHealth == 0)
OnHealthZero?.Invoke();
}
public void EnemyKill()
{
Destroy(gameObject);
}
public void SimpleKill()
{
CameraShake.instance.ShakeCamera(0.5f, 0.2f);
TowerCam.instance.ChangeToMainCamera();
Destroy(gameObject);
}
}