using Base.Common;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class AttackTrigger : MonoBehaviour
{
    public Character owner;
    public List<BeHitTrigger> trigedObjs;
    public int damage;
    public Vector3 force;
    public bool changeHurt;
    public float repelValue;
    public GameObject spiritsAttackEffect;
    public int offsetY = 1;
    public float hitRate = 1;
    public GameObject Miss;
    private void Awake()
    {
        owner = GetComponentInParent<Character>();
    }

    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);
                if (Util.CheckCanHit(owner.tag, hitTrigger.owner.tag) && !hitTrigger.owner.isDie)
                {
                    if (Random.Range(0, 1) < hitRate)
                    {
                        hitTrigger.BeHit(damage, force, changeHurt, repelValue);
                        if (spiritsAttackEffect != null)
                        {
                            GameObject effect = Instantiate(spiritsAttackEffect);
                            effect.transform.position = new Vector3(hitTrigger.transform.position.x,
                                transform.position.y + offsetY, 0);
                        }
                    }



                }
            }
        }
    }

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