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

public class Archer : Ally
{

    [SerializeField]
    private GameObject _arrow;

    private Rigidbody2D _rigidbodyAlly;
    private Rigidbody2D _rigidbodyOpponent;

    void Start() {
        _rigidbodyAlly = GetComponent<Rigidbody2D>();
    }

    void Update() {

        if(IsEnemyDetected) {
            _rigidbodyOpponent = Enemy.GetComponent<Rigidbody2D>();
            AttackEnemy();
        }

    }

    void AttackEnemy() {
        //Attack Cooldown
            if(AttackSpeed < AttackSpeedWait) {

                GameObject _newArrow = Instantiate(_arrow, _rigidbodyAlly.position, Quaternion.identity);

                //Warning : the Speed of the arrow is equal to the speed of this unit, if this unit need to move, use an other variable !
                _newArrow.GetComponent<Projectile>().Damage = AttackDamage;
                _newArrow.GetComponent<Projectile>().EnemySpeed = Enemy.Speed;
                _newArrow.GetComponent<Projectile>().VectorStart = _rigidbodyAlly.position;
                _newArrow.GetComponent<Projectile>().VectorEnd =  _rigidbodyOpponent.position;
                _newArrow.GetComponent<Projectile>().Target = Enemy;

                AttackSpeedWait = 0f;
                
            }

            AttackSpeedWait += Time.deltaTime;
            
    }

}
