2024-03-19 17:42:38 +03:00
|
|
|
using Koptilnya.StateMachine;
|
|
|
|
using Mirror;
|
|
|
|
using UnityEngine;
|
|
|
|
using UnityEngine.AI;
|
|
|
|
|
|
|
|
namespace Characters.Enemy.States
|
|
|
|
{
|
|
|
|
public class PatrolState : State<EnemyState>
|
|
|
|
{
|
|
|
|
private readonly Enemy _enemy;
|
|
|
|
private readonly NavMeshTriangulation _navMeshData;
|
|
|
|
|
|
|
|
private CustomNetworkManager _networkManager;
|
|
|
|
|
|
|
|
public PatrolState(Enemy enemy) : base(EnemyState.Patrol)
|
|
|
|
{
|
|
|
|
_enemy = enemy;
|
|
|
|
_navMeshData = NavMeshTriangulator.Data;
|
|
|
|
_networkManager = NetworkManager.singleton.GetComponent<CustomNetworkManager>();
|
|
|
|
}
|
|
|
|
|
|
|
|
public override void Enter()
|
|
|
|
{
|
2024-03-19 17:56:12 +03:00
|
|
|
if (!_enemy.isServer) return;
|
|
|
|
|
2024-03-19 20:18:01 +03:00
|
|
|
_enemy.SetSpeedMul(1f);
|
2024-03-19 17:42:38 +03:00
|
|
|
_enemy.SetAimRigWeight(0f);
|
|
|
|
}
|
|
|
|
|
|
|
|
public override void Update()
|
|
|
|
{
|
2024-03-19 17:56:12 +03:00
|
|
|
if (!_enemy.isServer) return;
|
|
|
|
|
2024-03-19 17:42:38 +03:00
|
|
|
float sortDistance = float.MaxValue;
|
|
|
|
foreach (var player in _networkManager.alive)
|
|
|
|
{
|
|
|
|
float distance = Vector3.Distance(player.transform.position, _enemy.transform.position);
|
|
|
|
|
|
|
|
if (distance <= _enemy.aggressionDistance && distance < sortDistance && _enemy.agent.SetDestination(player.transform.position))
|
|
|
|
{
|
|
|
|
sortDistance = distance;
|
|
|
|
_enemy.target = player.transform;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_enemy.target != null)
|
|
|
|
{
|
2024-03-19 17:56:12 +03:00
|
|
|
_enemy.state = EnemyState.Chase;
|
2024-03-19 17:42:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var remainingDistance = _enemy.agent.GetPathRemainingDistance();
|
|
|
|
var invalidPath = _enemy.agent.pathStatus != NavMeshPathStatus.PathComplete;
|
|
|
|
|
|
|
|
if (remainingDistance <= 1.5f || invalidPath)
|
|
|
|
{
|
|
|
|
SetRandomDestination();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetRandomDestination()
|
|
|
|
{
|
|
|
|
_enemy.agent.destination = GetRandomLocation();
|
|
|
|
}
|
|
|
|
|
|
|
|
Vector3 GetRandomLocation()
|
|
|
|
{
|
|
|
|
return _navMeshData.vertices[_navMeshData.indices[Random.Range(0, _navMeshData.indices.Length - 3)]];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|