2020-09-23 00:54:30 +01:00
|
|
|
|
using System;
|
|
|
|
|
using UnityEngine.Events;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
|
|
namespace Pink.Mechanics
|
|
|
|
|
{
|
|
|
|
|
public class Health : MonoBehaviour
|
|
|
|
|
{
|
2020-10-03 09:40:51 +01:00
|
|
|
|
[Min(0)]
|
2020-09-23 00:54:30 +01:00
|
|
|
|
public float MaxHealth = 10f;
|
|
|
|
|
|
2020-09-30 13:23:01 +01:00
|
|
|
|
[Header("Events")]
|
|
|
|
|
[Space]
|
|
|
|
|
|
2020-09-23 00:54:30 +01:00
|
|
|
|
public UnityEvent WasHurt;
|
|
|
|
|
public UnityEvent WasHealed;
|
2020-09-30 13:23:01 +01:00
|
|
|
|
public UnityEvent WasKilled;
|
2020-09-23 00:54:30 +01:00
|
|
|
|
|
|
|
|
|
private float currentHealth;
|
2020-10-03 09:40:51 +01:00
|
|
|
|
public float CurrentHealth {
|
2020-09-23 00:54:30 +01:00
|
|
|
|
get => currentHealth;
|
|
|
|
|
set => currentHealth = Math.Min(
|
|
|
|
|
Math.Max(0f, value),
|
|
|
|
|
MaxHealth);
|
|
|
|
|
}
|
2020-09-30 13:23:01 +01:00
|
|
|
|
public bool IsDead => CurrentHealth == 0;
|
|
|
|
|
public bool IsAlive => CurrentHealth > 0;
|
2020-09-23 00:54:30 +01:00
|
|
|
|
|
|
|
|
|
// Start is called before the first frame update
|
|
|
|
|
void Start()
|
|
|
|
|
{
|
|
|
|
|
CurrentHealth = MaxHealth;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool Hurt(float decrement)
|
|
|
|
|
{
|
2020-09-30 13:23:01 +01:00
|
|
|
|
if (IsAlive)
|
|
|
|
|
{
|
|
|
|
|
CurrentHealth -= decrement;
|
|
|
|
|
WasHurt.Invoke();
|
|
|
|
|
if (IsDead)
|
|
|
|
|
WasKilled.Invoke();
|
|
|
|
|
}
|
|
|
|
|
return IsAlive;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool Hurt()
|
|
|
|
|
{
|
|
|
|
|
return Hurt(MaxHealth / 5);
|
2020-09-23 00:54:30 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-10-03 09:40:51 +01:00
|
|
|
|
public bool Heal(float increment = 1)
|
2020-09-23 00:54:30 +01:00
|
|
|
|
{
|
|
|
|
|
CurrentHealth += increment;
|
|
|
|
|
WasHealed.Invoke();
|
|
|
|
|
return CurrentHealth == MaxHealth;
|
|
|
|
|
}
|
2020-09-30 13:23:01 +01:00
|
|
|
|
|
2020-10-01 18:11:43 +01:00
|
|
|
|
public void Kill()
|
2020-09-30 13:23:01 +01:00
|
|
|
|
{
|
|
|
|
|
if (IsAlive)
|
|
|
|
|
{
|
|
|
|
|
Hurt(MaxHealth);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Reset()
|
|
|
|
|
{
|
|
|
|
|
currentHealth = MaxHealth;
|
|
|
|
|
}
|
2020-09-23 00:54:30 +01:00
|
|
|
|
}
|
|
|
|
|
}
|