144 lines
3.8 KiB
C#
144 lines
3.8 KiB
C#
using System;
|
|
using DDD.Players.Tycoons;
|
|
using Pathfinding;
|
|
using UnityEngine;
|
|
using Random = UnityEngine.Random;
|
|
|
|
namespace DDD.Enemies
|
|
{
|
|
public class AiMovement : MonoBehaviour
|
|
{
|
|
// Variables
|
|
#region Variables
|
|
|
|
private IAstarAI _iAstarAi;
|
|
|
|
#endregion
|
|
|
|
// Unity events
|
|
#region Unity events
|
|
|
|
private void Awake()
|
|
{
|
|
InitializeComponents();
|
|
StopMove();
|
|
}
|
|
|
|
#endregion
|
|
|
|
// Initialize methods
|
|
#region Initialize methods
|
|
|
|
private void InitializeComponents()
|
|
{
|
|
_iAstarAi = GetComponent<IAstarAI>();
|
|
}
|
|
|
|
#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 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);
|
|
}
|
|
|
|
public void Teleport(Vector3 position)
|
|
{
|
|
_iAstarAi?.Teleport(position);
|
|
}
|
|
|
|
public bool HasReachedDestination()
|
|
{
|
|
if (_iAstarAi == null) return false;
|
|
if (_iAstarAi.pathPending) return false;
|
|
|
|
var distanceToDestination = Vector3.Distance(_iAstarAi.position, _iAstarAi.destination);
|
|
return distanceToDestination < 0.5f && _iAstarAi.reachedEndOfPath;
|
|
}
|
|
|
|
public void SetMoveSpeed(float speed)
|
|
{
|
|
if (_iAstarAi == null) return;
|
|
|
|
_iAstarAi.maxSpeed = speed;
|
|
}
|
|
|
|
public bool IsPositionMovable(Vector3 endPosition)
|
|
{
|
|
var nearestNode = AstarPath.active.GetNearest(endPosition).node;
|
|
return nearestNode != null && nearestNode.Walkable;
|
|
|
|
// var startNode = AstarPath.active.GetNearest(startPosition).node;
|
|
// var endNode = AstarPath.active.GetNearest(endPosition).node;
|
|
//
|
|
// return PathUtilities.IsPathPossible(startNode, endNode);
|
|
}
|
|
|
|
public void MoveToRandomPositionInRange(float range, int graphIndex = 0)
|
|
{
|
|
Vector3 randomPosition;
|
|
var isMovable = false;
|
|
var graphBounds = AstarPath.active.graphs[0].bounds;
|
|
|
|
do
|
|
{
|
|
var randomDirection = Random.insideUnitCircle.normalized;
|
|
var randomOffset = new Vector3(randomDirection.x, 0, randomDirection.y) * Random.Range(0, range);
|
|
|
|
randomPosition = _iAstarAi.position + randomOffset;
|
|
if (!graphBounds.Contains(randomPosition))
|
|
{
|
|
continue;
|
|
}
|
|
|
|
isMovable = IsPositionMovable(randomPosition);
|
|
|
|
} while (!isMovable);
|
|
|
|
Move(randomPosition);
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
} |