102 lines
2.4 KiB
C#
102 lines
2.4 KiB
C#
using UnityEngine;
|
|
using Invector;
|
|
|
|
public class SummonDamageReceiver : MonoBehaviour, vIHealthController
|
|
{
|
|
[Header("Health")]
|
|
[SerializeField] private int maxHealth = 40;
|
|
[SerializeField] private float _currentHealth = 40f;
|
|
|
|
[Header("Death")]
|
|
[SerializeField] private bool destroyOnDeath = true;
|
|
[SerializeField] private float destroyDelay = 0.1f;
|
|
[SerializeField] private GameObject deathEffect;
|
|
|
|
[Header("Debug")]
|
|
[SerializeField] private bool enableDebug = false;
|
|
|
|
[SerializeField] private OnReceiveDamage _onStartReceiveDamage = new OnReceiveDamage();
|
|
[SerializeField] private OnReceiveDamage _onReceiveDamage = new OnReceiveDamage();
|
|
[SerializeField] private OnDead _onDead = new OnDead();
|
|
|
|
public OnReceiveDamage onStartReceiveDamage => _onStartReceiveDamage;
|
|
public OnReceiveDamage onReceiveDamage => _onReceiveDamage;
|
|
public OnDead onDead => _onDead;
|
|
|
|
public float currentHealth => this._currentHealth;
|
|
public int MaxHealth => maxHealth;
|
|
public bool isDead { get; set; }
|
|
|
|
private void Awake()
|
|
{
|
|
ResetHealth();
|
|
}
|
|
|
|
public void TakeDamage(vDamage damage)
|
|
{
|
|
if (isDead) return;
|
|
_onStartReceiveDamage.Invoke(damage);
|
|
|
|
int dmg = Mathf.RoundToInt(damage.damageValue);
|
|
if (dmg <= 0) return;
|
|
|
|
ChangeHealth(-dmg);
|
|
_onReceiveDamage.Invoke(damage);
|
|
|
|
if (enableDebug)
|
|
Debug.Log($"[SummonDamageReceiver] {gameObject.name} took {dmg}, HP {currentHealth}/{maxHealth}");
|
|
}
|
|
|
|
public void AddHealth(int value)
|
|
{
|
|
if (isDead) return;
|
|
_currentHealth = Mathf.Min(maxHealth, currentHealth + value);
|
|
}
|
|
|
|
public void ChangeHealth(int value)
|
|
{
|
|
if (isDead) return;
|
|
_currentHealth = Mathf.Clamp(currentHealth + value, 0f, maxHealth);
|
|
|
|
if (currentHealth <= 0f)
|
|
Die();
|
|
}
|
|
|
|
public void ChangeMaxHealth(int value)
|
|
{
|
|
maxHealth = Mathf.Max(1, maxHealth + value);
|
|
_currentHealth = Mathf.Min(currentHealth, maxHealth);
|
|
}
|
|
|
|
public void ResetHealth(float health)
|
|
{
|
|
maxHealth = Mathf.Max(1, maxHealth);
|
|
_currentHealth = Mathf.Clamp(health, 0f, maxHealth);
|
|
isDead = false;
|
|
}
|
|
|
|
public void ResetHealth()
|
|
{
|
|
maxHealth = Mathf.Max(1, maxHealth);
|
|
_currentHealth = maxHealth;
|
|
isDead = false;
|
|
}
|
|
|
|
private void Die()
|
|
{
|
|
if (isDead) return;
|
|
isDead = true;
|
|
|
|
_onDead.Invoke(gameObject);
|
|
|
|
if (deathEffect != null)
|
|
{
|
|
GameObject fx = Instantiate(deathEffect, transform.position, transform.rotation);
|
|
Destroy(fx, 5f);
|
|
}
|
|
|
|
if (destroyOnDeath)
|
|
Destroy(gameObject, destroyDelay);
|
|
}
|
|
}
|