Advertisement
Guest User

Waypoint

a guest
Jan 4th, 2015
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.56 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using Priority_Queue;
  4. using System.Collections.Generic;
  5.  
  6. public class Waypoint : PriorityQueueNode
  7. {
  8.     private Vector2 pt;
  9.     public float x;
  10.     public float y;
  11.     public Waypoint next;
  12.     public Waypoint prev;
  13.     public float cost; // movement cost of this waypoint
  14.     public float totalCost; // total movement cost to get to this waypoint from previous ones
  15.  
  16.     private List<Waypoint> Successors;
  17.  
  18.     // use this constructor if the waypoint has no previous or future (known) waypoints to go to yet
  19.     public Waypoint(Vector2 point)
  20.     {
  21.         pt = point;
  22.         x = point.x;
  23.         y = point.y;
  24.         next = null;
  25.         prev = null;
  26.         Successors = new List<Waypoint>();
  27.  
  28.         TileMath tm = TileMath.GetTileMath((int)x, (int)y);
  29.  
  30.         cost = tm.GetPathfindingHeuristic();
  31.         totalCost = cost;
  32.        
  33.  
  34.     }
  35.  
  36.     // use this constructor to record the waypoint as having a previous waypoint, next one, or both
  37.     public Waypoint(Vector2 point, Waypoint child, Waypoint parent)
  38.     {
  39.         pt = point;
  40.         x = point.x;
  41.         y = point.y;
  42.         next = child;
  43.         prev = parent;
  44.         Successors = new List<Waypoint>();
  45.  
  46.         TileMath tm = TileMath.GetTileMath((int)x, (int)y);
  47.  
  48.         cost = tm.GetPathfindingHeuristic();
  49.  
  50.         totalCost = cost;
  51.  
  52.         Waypoint temp = this.prev;
  53.  
  54.         while(temp != null)
  55.         {
  56.             TileMath currWP = TileMath.GetTileMath((int)temp.x, (int)temp.y);
  57.  
  58.             totalCost = totalCost + currWP.GetPathfindingHeuristic();
  59.  
  60.             temp = temp.prev;
  61.         }
  62.     }
  63.  
  64.     public Vector2 GetPoint()
  65.     {
  66.         return pt;
  67.     }
  68.  
  69.     public List<Waypoint> GetSuccessors()
  70.     {
  71.         Successors.Clear();
  72.  
  73.         TileMath tm = TileMath.GetTileMath((int)x,(int)y);
  74.  
  75.         // check LEFT traversability, if it can be walked on, add it to the successor list
  76.         TileMath left = tm.GetLeftNeighbor();
  77.         if(left.GetIsHorizontallyTraversable())
  78.         {
  79.             Waypoint l = new Waypoint(new Vector2(left.GetX(), left.GetY()), null, this);
  80.             Successors.Add(l);
  81.         }
  82.  
  83.         // check RIGHT traversability, if it can be walked on, add it to the successor list
  84.         TileMath right = tm.GetRightNeighbor();
  85.         if(right.GetIsHorizontallyTraversable())
  86.         {
  87.             Waypoint r = new Waypoint(new Vector2(right.GetX(), right.GetY()), null, this);
  88.             Successors.Add(r);
  89.         }
  90.        
  91.  
  92.         return Successors;
  93.     }
  94.  
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement