using UnityEngine;
public class RoutineManager : MonoBehaviour
{
[SerializeField] Way[] paths;
[SerializeField] Transform player;
private Way currentPath;
private Transform currentWayPoint;
private Transform nextWaypoint;
[SerializeField] float waypointReachedDistance = 3f;
[SerializeField] HouseManager houseManager;
public Transform CurrentTarget => nextWaypoint;
void Start()
{
if (paths.Length > 0)
{
Debug.Log(houseManager.currentHouseIndex);
SelectPath(houseManager.currentHouseIndex);
}
}
void Update()
{
if (currentPath == null || nextWaypoint == null) return;
if (currentWayPoint == null)
{
currentWayPoint = currentPath.GetNearestPoint(player.position);
nextWaypoint = currentPath.GetNextpoint(currentWayPoint);
}
UpdateNavigation();
}
void UpdateNavigation()
{
//float distanceToWaypoint = Vector3.Distance(player.position, currentWayPoint.position);
float distanceToWaypoint = Vector3.Distance(player.position, nextWaypoint.position);
if (distanceToWaypoint < waypointReachedDistance )
{
currentWayPoint = nextWaypoint;
Transform nextPoint = currentPath.GetNextpoint(currentWayPoint);
if ( nextPoint != null )
{
nextWaypoint = nextPoint;
}
else
{
nextWaypoint = currentWayPoint;
}
}
}
public void SelectPath(int pathIndex)
{
if (pathIndex < paths.Length)
{
currentPath = paths[pathIndex];
currentWayPoint = currentPath.GetNearestPoint(player.position);
if (currentWayPoint != null)
{
Transform nextPoint = currentPath.GetNextpoint(currentWayPoint);
if (nextWaypoint == null)
{
nextWaypoint = currentWayPoint;
}
else
{
nextWaypoint = nextPoint;
}
}
}
}
void OnDrawGizmos()
{
if (currentPath != null && currentPath.waypoints != null)
{
for (int i = 0; i < currentPath.waypoints.Count; i++)
{
if (currentPath.waypoints[i] != null)
{
Gizmos.color = (i == 0) ? Color.green : Color.white;
Gizmos.DrawWireSphere(currentPath.waypoints[i].position, 0.5f);
if (i < currentPath.waypoints.Count - 1 && currentPath.waypoints[i + 1] != null)
{
Gizmos.DrawLine(currentPath.waypoints[i].position,
currentPath.waypoints[i + 1].position);
}
}
}
}
if (currentWayPoint != null)
{
Gizmos.color = Color.yellow;
Gizmos.DrawWireSphere(currentWayPoint.position, 1f);
}
if (nextWaypoint != null)
{
Gizmos.color = Color.green;
Gizmos.DrawWireSphere(nextWaypoint.position, 1.5f);
}
}
}