123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227 |
- using UnityEngine;
- namespace Pathfinding {
-
-
-
-
-
-
-
-
-
-
- public class XPath : ABPath {
-
-
-
-
-
-
-
-
-
-
- public PathEndingCondition endingCondition;
- public XPath () {}
- public new static XPath Construct (Vector3 start, Vector3 end, OnPathDelegate callback = null) {
- var p = PathPool.GetPath<XPath>();
- p.Setup(start, end, callback);
- p.endingCondition = new ABPathEndingCondition(p);
- return p;
- }
- protected override void Reset () {
- base.Reset();
- endingCondition = null;
- }
- #if !ASTAR_NO_GRID_GRAPH
- protected override bool EndPointGridGraphSpecialCase (GraphNode endNode) {
-
- return false;
- }
- #endif
-
- protected override void CompletePathIfStartIsValidTarget () {
- var pNode = pathHandler.GetPathNode(startNode);
- if (endingCondition.TargetFound(pNode)) {
- ChangeEndNode(startNode);
- Trace(pNode);
- CompleteState = PathCompleteState.Complete;
- }
- }
-
-
-
-
- void ChangeEndNode (GraphNode target) {
-
-
- if (endNode != null && endNode != startNode) {
- var pathNode = pathHandler.GetPathNode(endNode);
- pathNode.flag1 = pathNode.flag2 = false;
- }
- endNode = target;
- endPoint = (Vector3)target.position;
- }
- protected override void CalculateStep (long targetTick) {
- int counter = 0;
-
- while (CompleteState == PathCompleteState.NotCalculated) {
- searchedNodes++;
-
- if (endingCondition.TargetFound(currentR)) {
- CompleteState = PathCompleteState.Complete;
- break;
- }
-
- currentR.node.Open(this, currentR, pathHandler);
-
- if (pathHandler.heap.isEmpty) {
- FailWithError("Searched whole area but could not find target");
- return;
- }
-
- currentR = pathHandler.heap.Remove();
-
- if (counter > 500) {
-
- if (System.DateTime.UtcNow.Ticks >= targetTick) {
-
- return;
- }
- counter = 0;
- if (searchedNodes > 1000000) {
- throw new System.Exception("Probable infinite loop. Over 1,000,000 nodes searched");
- }
- }
- counter++;
- }
- if (CompleteState == PathCompleteState.Complete) {
- ChangeEndNode(currentR.node);
- Trace(currentR);
- }
- }
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public abstract class PathEndingCondition {
-
- protected Path path;
- protected PathEndingCondition () {}
- public PathEndingCondition (Path p) {
- if (p == null) throw new System.ArgumentNullException("p");
- this.path = p;
- }
-
-
- public abstract bool TargetFound(PathNode node);
- }
-
- public class ABPathEndingCondition : PathEndingCondition {
-
-
-
-
- protected ABPath abPath;
- public ABPathEndingCondition (ABPath p) {
- if (p == null) throw new System.ArgumentNullException("p");
- abPath = p;
- path = p;
- }
-
-
-
- public override bool TargetFound (PathNode node) {
- return node.node == abPath.endNode;
- }
- }
-
- public class EndingConditionProximity : ABPathEndingCondition {
-
- public float maxDistance = 10;
- public EndingConditionProximity (ABPath p, float maxDistance) : base(p) {
- this.maxDistance = maxDistance;
- }
- public override bool TargetFound (PathNode node) {
- return ((Vector3)node.node.position - abPath.originalEndPoint).sqrMagnitude <= maxDistance*maxDistance;
- }
- }
- }
|