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

public class SoulBoom : MonoBehaviour
{
    [HideInInspector]
    public float time;
    public float boomTime;
    [HideInInspector]
    public bool isBoom;
    public AttackInfo attackInfo;
    public float destroyTime;
    public List<Character> characters = new List<Character>();
    private void Update()
    {
        time += Time.deltaTime;
        if (time >= boomTime)
        {
            if (!isBoom)
            {
                
                for(int i = 0; i < characters.Count; i++)
                {
                    if (characters[i].state == CharacterState.Die)
                    {
                        continue;
                    }

                    Vector3 target;
                    if (characters[i].transform.position.x > transform.position.x)
                    {
                        target = attackInfo.attackDir;
                    }
                    else
                    {
                        target = new Vector3(-attackInfo.attackDir.x, attackInfo.attackDir.y,
                            attackInfo.attackDir.z);
                    }
                    characters[i].BeHit
                        (attackInfo.damage, target * attackInfo.force,
                        attackInfo.changeHurt, attackInfo.repelValue);
                    isBoom = true;
                }

            }
            
        }
        else if(time >= boomTime - 0.01f)
        {
            GetComponent<Collider>().enabled = true;
        }
        if (time >= destroyTime)
        {
            gameObject.SetActive(false);
        }
    }
    private void OnTriggerEnter(Collider other)
    {
        if ((other.gameObject.layer == 6 || other.gameObject.layer == 7)
            && other.name == "BodyCollider")
        {
            Character character = other.GetComponentInParent<Character>();
            if (!characters.Exists(t => t == character))
            {
                characters.Add(character);
            }
        }
    }
    private void OnTriggerExit(Collider other)
    {
        if ((other.gameObject.layer == 6 || other.gameObject.layer == 7)
            && other.name == "BodyCollider")
        {
            Character character = other.GetComponentInParent<Character>();
            if (characters.Exists(t => t == character))
            {
                characters.Remove(character);
            }
        }
    }
}
