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

public class AoEAbility : Ability
{
    private float Radius = 3.0f;

    public override void Use(Player _owner)
    {
        if (Ready)
        {
            m_TimeLeft = Cooldown;
            Ready = false;

            List<Player> affectedPlayers = new List<Player>();
            Player[] allPlayers = GameObject.FindObjectsOfType<Player>();
            Rigidbody2D ownerRigidBody = _owner.GetComponent<Rigidbody2D>();

            for (int i = 0; i < allPlayers.Length; ++i)
            {
                // check distance between ower and player, add them to the 
                Rigidbody2D targetRigidbody = allPlayers[i].GetComponent<Rigidbody2D>();

                if (targetRigidbody != null && targetRigidbody != ownerRigidBody)
                {
                    float dist = Vector2.Distance(targetRigidbody.position, ownerRigidBody.position);

                    if (dist < Radius)
                    {
                        affectedPlayers.Add(allPlayers[i]);
                    }
                }
            }

            for (int i = 0; i < affectedPlayers.Count; ++i)
            {
                DoAction(affectedPlayers[i]);
            }
            //_owner.StatusIconObj.GetComponent<SpriteRenderer>().sprite = _owner.StatusStunned;
            //_owner.AbilityIconObj.GetComponent<SpriteRenderer>().sprite = _owner.AbilityStun;
            Debug.Log(Name + " used");
        }
    }
}
