Files
GoblinRaid/Assets/Scripts/Control/AIController.cs
2025-10-30 16:50:35 +00:00

80 lines
2.1 KiB
C#

using System;
using RPG.Combat;
using RPG.Core;
using RPG.Movement;
using UnityEngine;
using UnityEngine.AI;
namespace RPG.Control
{
public class AIController : MonoBehaviour,IAction
{
[SerializeField] float chaseDistance = 5f;
[SerializeField] float suspicionTime = 3f;
Fighter fighter;
Health health;
Mover mover;
GameObject player;
Vector3 guardPosition;
float timeSinceLastSawPlayer = Mathf.Infinity;
private void Start()
{
fighter = GetComponent<Fighter>();
health = GetComponent<Health>();
player = GameObject.FindWithTag("Player");
mover = GetComponent<Mover>();
guardPosition = transform.position;
}
private void Update()
{
if (health.IsDead()) return;
if (InAttackRangeOfPlayer() && fighter.CanAttack(player))
{
timeSinceLastSawPlayer = 0;
AttackBehaviour();
}
else if (timeSinceLastSawPlayer < suspicionTime)
{
SuspicionBehaviour();
}
else
{
//fighter.Cancel();
GuardBehaviour();
}
timeSinceLastSawPlayer += Time.deltaTime;
}
private void GuardBehaviour()
{
mover.StartMoveAction(guardPosition);
}
private void SuspicionBehaviour()
{
GetComponent<ActionScheduler>().CancelCurrentAction();
}
private void AttackBehaviour()
{
fighter.Attack(player);
}
private bool InAttackRangeOfPlayer()
{
float distanceToPlayer = Vector3.Distance(player.transform.position, transform.position);
return distanceToPlayer < chaseDistance;
}
public void Cancel()
{
}
//Called bt Unity
private void OnDrawGizmosSelected()
{
Gizmos.color = Color.blue;
Gizmos.DrawWireSphere(transform.position, chaseDistance);
}
}
}