2024-06-18 18:16:19 +00:00
|
|
|
using System;
|
2024-10-06 23:41:09 +00:00
|
|
|
using BlueWater.Players.Tycoons;
|
2024-06-13 16:46:37 +00:00
|
|
|
using Pathfinding;
|
2024-06-03 18:26:03 +00:00
|
|
|
using UnityEngine;
|
2024-10-06 23:41:09 +00:00
|
|
|
using Random = UnityEngine.Random;
|
2024-06-03 18:26:03 +00:00
|
|
|
|
|
|
|
namespace BlueWater.Enemies
|
|
|
|
{
|
|
|
|
public class AiMovement : MonoBehaviour
|
|
|
|
{
|
|
|
|
// Variables
|
|
|
|
#region Variables
|
|
|
|
|
|
|
|
private IAstarAI _iAstarAi;
|
|
|
|
|
2024-06-13 16:46:37 +00:00
|
|
|
#endregion
|
|
|
|
|
|
|
|
// Unity events
|
|
|
|
#region Unity events
|
|
|
|
|
|
|
|
private void Awake()
|
|
|
|
{
|
|
|
|
InitializeComponents();
|
2024-06-18 18:16:19 +00:00
|
|
|
StopMove();
|
2024-06-13 16:46:37 +00:00
|
|
|
}
|
2024-06-18 18:16:19 +00:00
|
|
|
|
2024-06-03 18:26:03 +00:00
|
|
|
#endregion
|
|
|
|
|
|
|
|
// Initialize methods
|
|
|
|
#region Initialize methods
|
|
|
|
|
2024-06-13 16:46:37 +00:00
|
|
|
private void InitializeComponents()
|
2024-06-03 18:26:03 +00:00
|
|
|
{
|
2024-06-13 16:46:37 +00:00
|
|
|
_iAstarAi = GetComponent<IAstarAI>();
|
2024-06-03 18:26:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
// Methods
|
|
|
|
#region Methods
|
|
|
|
|
|
|
|
public void EnableMove()
|
|
|
|
{
|
|
|
|
if (_iAstarAi == null) return;
|
|
|
|
|
|
|
|
_iAstarAi.isStopped = false;
|
|
|
|
_iAstarAi.canMove = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void StopMove()
|
|
|
|
{
|
|
|
|
if (_iAstarAi == null) return;
|
|
|
|
|
|
|
|
_iAstarAi.isStopped = true;
|
|
|
|
_iAstarAi.canMove = false;
|
|
|
|
_iAstarAi.SetPath(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Move(Vector3 position)
|
|
|
|
{
|
|
|
|
if (_iAstarAi == null) return;
|
|
|
|
|
|
|
|
_iAstarAi.destination = position;
|
|
|
|
EnableMove();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void MoveTarget(Collider target)
|
|
|
|
{
|
|
|
|
if (!target)
|
|
|
|
{
|
|
|
|
StopMove();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Move(target.transform.position);
|
|
|
|
}
|
|
|
|
|
2024-10-06 23:41:09 +00:00
|
|
|
public Vector3 SetRandomPoint()
|
|
|
|
{
|
|
|
|
if (_iAstarAi == null) throw new Exception("AstarAi 오류");
|
|
|
|
|
|
|
|
var randomFactor = Random.Range(0.2f, 0.8f);
|
|
|
|
return Vector3.Lerp(_iAstarAi.position, _iAstarAi.destination, randomFactor);
|
|
|
|
}
|
|
|
|
|
2024-06-03 18:26:03 +00:00
|
|
|
public void Teleport(Vector3 position)
|
|
|
|
{
|
|
|
|
_iAstarAi?.Teleport(position);
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool HasReachedDestination()
|
|
|
|
{
|
2024-06-18 18:16:19 +00:00
|
|
|
if (_iAstarAi == null) return false;
|
|
|
|
if (_iAstarAi.pathPending) return false;
|
|
|
|
|
|
|
|
var distanceToDestination = Vector3.Distance(_iAstarAi.position, _iAstarAi.destination);
|
|
|
|
return distanceToDestination < 0.5f && _iAstarAi.reachedEndOfPath;
|
2024-06-03 18:26:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void SetMoveSpeed(float speed)
|
|
|
|
{
|
|
|
|
if (_iAstarAi == null) return;
|
|
|
|
|
|
|
|
_iAstarAi.maxSpeed = speed;
|
|
|
|
}
|
|
|
|
|
2024-07-01 10:36:07 +00:00
|
|
|
public bool IsPositionMovable(Vector3 endPosition)
|
2024-06-03 18:26:03 +00:00
|
|
|
{
|
2024-07-01 10:36:07 +00:00
|
|
|
var nearestNode = AstarPath.active.GetNearest(endPosition).node;
|
|
|
|
return nearestNode != null && nearestNode.Walkable;
|
2024-06-03 18:26:03 +00:00
|
|
|
|
2024-07-01 10:36:07 +00:00
|
|
|
// var startNode = AstarPath.active.GetNearest(startPosition).node;
|
|
|
|
// var endNode = AstarPath.active.GetNearest(endPosition).node;
|
|
|
|
//
|
|
|
|
// return PathUtilities.IsPathPossible(startNode, endNode);
|
2024-06-03 18:26:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
}
|
|
|
|
}
|