91 lines
2.1 KiB
C#
91 lines
2.1 KiB
C#
using Pathfinding;
|
|
using UnityEngine;
|
|
|
|
namespace BlueWater.Enemies
|
|
{
|
|
public class AiMovement : MonoBehaviour
|
|
{
|
|
// Variables
|
|
#region Variables
|
|
|
|
private IAstarAI _iAstarAi;
|
|
|
|
#endregion
|
|
|
|
// Initialize methods
|
|
#region Initialize methods
|
|
|
|
public void InitializeComponents(IAstarAI iAstartAi)
|
|
{
|
|
_iAstarAi = iAstartAi;
|
|
}
|
|
|
|
#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);
|
|
}
|
|
|
|
public void Teleport(Vector3 position)
|
|
{
|
|
_iAstarAi?.Teleport(position);
|
|
}
|
|
|
|
public bool HasReachedDestination()
|
|
{
|
|
return _iAstarAi is { pathPending: false, reachedEndOfPath: true };
|
|
}
|
|
|
|
public void SetMoveSpeed(float speed)
|
|
{
|
|
if (_iAstarAi == null) return;
|
|
|
|
_iAstarAi.maxSpeed = speed;
|
|
}
|
|
|
|
public bool IsPositionMovable(Vector3 startPosition, Vector3 endPosition)
|
|
{
|
|
var startNode = AstarPath.active.GetNearest(startPosition).node;
|
|
var endNode = AstarPath.active.GetNearest(endPosition).node;
|
|
|
|
return PathUtilities.IsPathPossible(startNode, endNode);
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
} |