using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class AttackTrigger : MonoBehaviour
{
    public GameObject owner;
    public List<BeHitTrigger> trigedObjs;
    public int damage;
    public Vector3 force;

    private void Awake()
    {
        if (GetComponentInParent<PlayerController>())
        {
            owner = GetComponentInParent<PlayerController>().gameObject;
        }
        else if (GetComponentInParent<Demonic>())
        {
            owner = GetComponentInParent<Demonic>().gameObject;
        }
        else if (GetComponentInParent<Tower>())
        {
            owner = GetComponentInParent<Tower>().gameObject;
        }
        else if (GetComponentInParent<Enemy>())
        {
            owner = GetComponentInParent<Enemy>().gameObject;
        }
        else if (GetComponentInParent<EnemyTower>())
        {
            owner = GetComponentInParent<EnemyTower>().gameObject;
        }
    }

    private void OnTriggerEnter(Collider other)
    {
        BeHitTrigger hitTrigger = other.GetComponent<BeHitTrigger>();
        if (hitTrigger != null)
        {
            bool triged = false;
            for (int i = 0; i < trigedObjs.Count; i++)
            {
                if (trigedObjs[i] == hitTrigger)
                {
                    triged = true;
                    break;
                }
            }
            if (!triged)
            {
                trigedObjs.Add(hitTrigger);
                hitTrigger.BeHit(damage, force);
            }
        }
    }

    private void OnEnable()
    {
        trigedObjs.Clear();
    }
}
