SanAndreasUnity/Assets/Scripts/Behaviours/Damageable.cs
2020-05-31 19:07:22 +02:00

45 lines
856 B
C#

using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
namespace SanAndreasUnity.Behaviours
{
public class DamageInfo
{
public float amount = 0f;
public object data = null;
}
public class Damageable : MonoBehaviour
{
[SerializeField] private float m_health = 0f;
public float Health { get { return m_health; } set { m_health = value; } }
[SerializeField] private UnityEvent m_onDamage = new UnityEvent ();
public DamageInfo LastDamageInfo { get; private set; }
public void Damage (DamageInfo info)
{
this.LastDamageInfo = info;
m_onDamage.Invoke ();
}
public void HandleDamageByDefault ()
{
DamageInfo info = this.LastDamageInfo;
this.Health -= info.amount;
if (this.Health <= 0f) {
Destroy (this.gameObject);
}
}
}
}