Hark-the-Moon-Men-Cometh / Assets / Scripts / StaticDamageSource.cs
StaticDamageSource.cs
Raw
using System.Collections.Generic;
using UnityEngine;

public class StaticDamageSource : MonoBehaviour, IDamageSource
{
    public List<GameObject> i_sources { get => m_sources; 
        set => m_sources = value; }
    public float i_damage { get => m_damage; set => m_damage = value; }

    [SerializeField]
    private List<GameObject> m_sources;
    [SerializeField]
    private float m_damage;

    private void Awake()
    {
        m_sources = new List<GameObject>();
        m_sources.Add(gameObject);
    }

    public void InflictDamage(IHarmable target)
    {
        target.TakeDamage(m_damage);
    }

    private void OnCollisionEnter(Collision collision)
    {
        IHarmable other = collision.gameObject.GetComponent<IHarmable>();
        if (other != null && !m_sources.Contains(collision.gameObject))
        {
            InflictDamage(other);
        }
    }

    private void OnTriggerEnter(Collider other)
    {
        IHarmable ouchie = other.gameObject.GetComponent<IHarmable>();
        if (ouchie != null && !m_sources.Contains(other.gameObject))
        {
            InflictDamage(ouchie);
        }
    }
}