Guest User

Untitled

a guest
Oct 7th, 2013
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.00 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. namespace Pathfinding {
  5. #if FALSE
  6.     /** Extended Path.
  7.      * \ingroup paths
  8.      * This is the same as a standard path but with a lot more customizations possible.
  9.      * \note With more customizations does make it slower to calculate but not by very much.
  10.      * \astarpro
  11.      */
  12.     public class XPath : ABPath {
  13.        
  14.         /** Ending Condition for the path.
  15.          * The ending condition determines when the path has been completed.
  16.          */
  17.         public PathEndingCondition endingCondition;
  18.        
  19.         public XPath () {}
  20.        
  21.         public XPath (Vector3 start, Vector3 end, OnPathDelegate callbackDelegate) {
  22.             Setup (start, end, callbackDelegate);
  23.         }
  24.        
  25.         public new static XPath Construct (Vector3 start, Vector3 end, OnPathDelegate callback = null) {
  26.             XPath p = PathPool<XPath>.GetPath ();
  27.             p.Setup (start,end,callback);
  28.             p.endingCondition = new ABPathEndingCondition (p);
  29.             return p;
  30.         }
  31.        
  32.         protected override void Recycle () {
  33.             PathPool<XPath>.Recycle (this);
  34.         }
  35.        
  36.         public override void Reset () {
  37.             base.Reset ();
  38.             endingCondition = null;
  39.         }
  40.        
  41.         public override void Initialize () {
  42.             base.Initialize ();
  43.            
  44.             if (currentR != null && endingCondition.TargetFound (currentR)) {
  45.                 CompleteState = PathCompleteState.Complete;
  46.                 endNode = currentR.node;
  47.                 Trace (currentR);
  48.             }
  49.         }
  50.        
  51.         public override void CalculateStep (long targetTick) {
  52.            
  53.             int counter = 0;
  54.            
  55.             //Continue to search while there hasn't ocurred an error and the end hasn't been found
  56.             while (!IsDone()) {
  57.                
  58.                 //@Performance Just for debug info
  59.                 searchedNodes++;
  60.                
  61.                 //Close the current node, if the current node satisfies the ending condition, terminate the path
  62.                 if (endingCondition.TargetFound (currentR)) {
  63.                     CompleteState = PathCompleteState.Complete;
  64.                     endNode = currentR.node;
  65.                     break;
  66.                 }
  67.                
  68.                 //Loop through all walkable neighbours of the node and add them to the open list.
  69.                 currentR.node.Open (runData,currentR, hTarget,this);
  70.                
  71.                 //any nodes left to search?
  72.                 if (runData.open.numberOfItems <= 1) {
  73.                     Error ();
  74.                     LogError ("No open points, whole area searched");
  75.                     return;
  76.                 }
  77.                
  78.                 //Select the node with the lowest F score and remove it from the open list
  79.                 currentR = runData.open.Remove ();
  80.                
  81.                 //Check for time every 500 nodes, roughly every 0.5 ms usually
  82.                 if (counter > 500) {
  83.                    
  84.                     //Have we exceded the maxFrameTime, if so we should wait one frame before continuing the search since we don't want the game to lag
  85.                     if (System.DateTime.UtcNow.Ticks >= targetTick) {
  86.                        
  87.                         //Return instead of yield'ing, a separate function handles the yield (CalculatePaths)
  88.                         return;
  89.                     }
  90.                    
  91.                     counter = 0;
  92.                 }
  93.                
  94.                 counter++;
  95.            
  96.             }
  97.            
  98.             if (CompleteState == PathCompleteState.Complete) {
  99.                 Trace (currentR);
  100.             }
  101.         }
  102.     }
  103.  
  104.     #endif
  105.    
  106.     /** Customized ending condition for a path.
  107. This class can be used to implement a custom ending condition for e.g an Pathfinding.XPath.\n
  108. Inherit from this class and override the #TargetFound function to implement you own ending condition logic.\n
  109. \n
  110. For example, you might want to create an Ending Condition which stops when a node is close enough to a given point.\n
  111. Then what you do is that you create your own class, let's call it EndingConditionProximity and override the function TargetFound to specify our own logic.
  112.  
  113. \code
  114. public class EndingConditionProximity : Pathfinding.PathEndingCondition {
  115.    
  116.     //The maximum distance to the target node before terminating the path
  117.     public float maxDistance = 10;
  118.    
  119.     public override bool TargetFound (Path p, Node node) {
  120.         return ((Vector3)node.position - p.originalEndPoint).sqrMagnitude <= maxDistance*maxDistance;
  121.     }
  122. }
  123. \endcode
  124.  
  125. One part at a time. We need to cast the node's position to a Vector3 since internally, it is stored as an integer coordinate (Int3).
  126. Then we subtract the Pathfinding.Path.originalEndPoint from it to get their difference. The original end point is always the exact point specified when calling the path.
  127. As a last step we check the squared magnitude (squared distance, it is much faster than the non-squared distance) and check if it is lower or equal to our maxDistance squared.\n
  128. There you have it, it is as simple as that.
  129. Then you simply assign it to the \a endingCondition variable on, for example an XPath which uses the EndingCondition.
  130.  
  131. \code
  132. EndingConditionProximity ec = new EndingConditionProximity ();
  133. ec.maxDistance = 100; //Or some other value
  134. myXPath.endingCondition = ec;
  135.  
  136. //Call the path!
  137. mySeeker.StartPath (ec);
  138. \endcode
  139.  
  140. Where \a mySeeker is a Seeker component, and \a myXPath is an Pathfinding.XPath.\n
  141.  
  142. \note The above was written without testing. I hope I haven't made any mistakes, if you try it out, and it doesn't seem to work. Please post a comment below.
  143.  
  144. \note Written for 3.0.8.3
  145.  
  146. \version Method structure changed in 3.2
  147.  
  148. \see Pathfinding.XPath
  149. \see Pathfinding.ConstantPath
  150.  
  151. */
  152.     public class PathEndingCondition {
  153.        
  154.         protected Path p;
  155.        
  156.         protected PathEndingCondition () {}
  157.        
  158.         public PathEndingCondition (Path p) {
  159.             if (p == null) throw new System.ArgumentNullException ("Please supply a non-null path");
  160.             this.p = p;
  161.         }
  162.        
  163.         /** Has the ending condition been fulfilled.
  164.          * \param node The current node.
  165.          * This is per default the same as asking if \a node == \a p.endNode */
  166.         public virtual bool TargetFound (PathNode node) {
  167.             return true;//node.node == p.endNode;
  168.         }
  169.     }
  170.    
  171.     public class ABPathEndingCondition : PathEndingCondition {
  172.        
  173.         protected ABPath abPath;
  174.        
  175.         public ABPathEndingCondition (ABPath p) {
  176.             if (p == null) throw new System.ArgumentNullException ("Please supply a non-null path");
  177.             abPath = p;
  178.         }
  179.         /** Has the ending condition been fulfilled.
  180.          * \param node The current node.
  181.          * This is per default the same as asking if \a node == \a p.endNode */
  182.         public override bool TargetFound (PathNode node) {
  183.             return node.node == abPath.endNode;
  184.         }
  185.     }
  186.  
  187. }
Advertisement
Add Comment
Please, Sign In to add comment