using RPG.Core;
using RPG.Movement;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace RPG.Combat
{
public class Fighter : MonoBehaviour, IAction
{
[SerializeField] float weaponRange = 2f;
[SerializeField] float timeBetweenAttacks = 1f;
[SerializeField] float weaponDamage = 5f;
float timeSinceLastAttack = 0;
Transform target;
private void Update()
{
timeSinceLastAttack += Time.deltaTime;
if (target == null) return;
if (!GetIsInRange())
{
GetComponent<Mover>().MoveTo(target.position);
}
else
{
GetComponent<Mover>().Cancel();
AttackBehavior();
}
}
private void AttackBehavior()
{
if (timeSinceLastAttack > timeBetweenAttacks)
{
//This will trigger the Hit() event
GetComponent<Animator>().SetTrigger("attack");
timeSinceLastAttack = 0;
}
}
//Animation Event
void Hit()
{
target.GetComponent<Health>().TakeDamage(weaponDamage);
}
private bool GetIsInRange()
{
return Vector3.Distance(transform.position, target.position) < weaponRange;
}
public void Attack(CombatTarget combatTarget)
{
GetComponent<ActionScheduler>().StartAction(this);
target = combatTarget.transform;
}
public void Cancel()
{
target = null;
}
}
}