Advertisement
EdibleCookie

PathManager

Jul 25th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.88 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class PathManager : MonoBehaviour
  6. {
  7.     public float walkSpeed = 5.0f;
  8.  
  9.     private Stack<Vector3> currentPath;
  10.     private Vector3 currentWaypointPosition;
  11.     private float moveTimeTotal;
  12.     private float moveTimeCurrent;
  13.  
  14.     public void NavigateTo(Vector3 destination)
  15.     {
  16.         currentPath = new Stack<Vector3> ();
  17.         var currentNode = FindClosestWaypoint (transform.position);
  18.         var endNode = FindClosestWaypoint (destination);
  19.         if (currentNode == null || endNode == null || currentNode == endNode)
  20.             return;
  21.         var openList = new SortedList<float, Waypoint> ();
  22.         var closedList = new List<Waypoint> ();
  23.         openList.Add (0, currentNode);
  24.         currentNode.previous = null;
  25.         currentNode.distance = 0f;
  26.         while (openList.Count > 0)
  27.         {
  28.             currentNode = openList.Values[0];
  29.             openList.RemoveAt (0);
  30.             var dist = currentNode.distance;
  31.             closedList.Add (currentNode);
  32.             if (currentNode == endNode)
  33.             {
  34.                 break;
  35.             }
  36.             foreach (var neighbor in currentNode.neighbors)
  37.             {
  38.                 if (closedList.Contains (neighbor) || openList.ContainsValue (neighbor))
  39.                     continue;
  40.                 neighbor.previous = currentNode;
  41.                 neighbor.distance = dist + (neighbor.transform.position - currentNode.transform.position).magnitude;
  42.                 var distanceToTarget = (neighbor.transform.position - endNode.transform.position).magnitude;
  43.                 openList.Add (neighbor.distance + distanceToTarget, neighbor);
  44.             }
  45.         }
  46.         if (currentNode == endNode)
  47.         {
  48.             while (currentNode.previous != null)
  49.             {
  50.                 currentPath.Push (currentNode.transform.position);
  51.                 currentNode = currentNode.previous;
  52.             }
  53.             currentPath.Push (transform.position);
  54.         }
  55.     }
  56.  
  57.     public void Stop()
  58.     {
  59.         currentPath = null;
  60.         moveTimeTotal = 0;
  61.         moveTimeCurrent = 0;
  62.     }
  63.  
  64.     void Update()
  65.     {
  66.         if (currentPath != null && currentPath.Count > 0)
  67.         {
  68.             if (moveTimeCurrent < moveTimeTotal)
  69.             {
  70.                 moveTimeCurrent += Time.deltaTime;
  71.                 if (moveTimeCurrent > moveTimeTotal)
  72.                     moveTimeCurrent = moveTimeTotal;
  73.                 transform.position = Vector3.Lerp (currentWaypointPosition, currentPath.Peek (), moveTimeCurrent / moveTimeTotal);
  74.             } else
  75.             {
  76.                 currentWaypointPosition = currentPath.Pop ();
  77.                 if (currentPath.Count == 0)
  78.                     Stop ();
  79.                 else
  80.                 {
  81.                     moveTimeCurrent = 0;
  82.                     moveTimeTotal = (currentWaypointPosition - currentPath.Peek ()).magnitude / walkSpeed;
  83.                 }
  84.             }
  85.         }
  86.     }
  87.  
  88.     private Waypoint FindClosestWaypoint(Vector3 target)
  89.     {
  90.         GameObject closest = null;
  91.         float closestDist = Mathf.Infinity;
  92.         foreach (var waypoint in GameObject.FindGameObjectsWithTag("Waypoint"))
  93.         {
  94.             var dist = (waypoint.transform.position - target).magnitude;
  95.             if (dist < closestDist)
  96.             {
  97.                 closest = waypoint;
  98.                 closestDist = dist;
  99.             }
  100.         }
  101.         if (closest != null)
  102.         {
  103.             return closest.GetComponent<Waypoint> ();
  104.         }
  105.         return null;
  106.     }
  107.  
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement