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

public class Root : MonoBehaviour
{
    [SerializeField]
    private Entity _entity;
    [SerializeField]
    private GameObject _projectile;
    [SerializeField]
    private Transform _projectileSpawn;

    public void Attack() 
    {
        if (_entity == null || _entity.Enemy == null) return;

        _entity.Enemy.Hit(_entity.AttackDamage);
        if(_entity.Enemy.Hp <= 0) 
        {
            _entity.Enemy.Death();
            _entity.IsEnemyDetected = false;
        }
    }

    public void ShotProjectile()
    {
        Rigidbody2D _rigidbodyAlly;
        Rigidbody2D _rigidbodyOpponent;
        _rigidbodyAlly = _entity.GetComponent<Rigidbody2D>();

        if (!_entity.Enemy) return;
        _rigidbodyOpponent = _entity.Enemy.GetComponent<Rigidbody2D>();

        Vector3 spawnPos = (_projectileSpawn == null) ? _rigidbodyAlly.position : _projectileSpawn.position;
        GameObject _newArrow = Instantiate(_projectile, spawnPos, Quaternion.identity);
        
        var projectile = _newArrow.GetComponent<Projectile>();
        projectile.Damage = _entity.AttackDamage;  
        projectile.Origin = _entity;
    }

    public void PlaySound(string soundName)
    {
        _entity.PlaySound(soundName);
    }
}
