示例#1
0
 public void SetupAroundNode(VectorPathNode node)
 {
     foreach (VectorNode neighbor in node.vecNode.neighbors)
     {
         if (!IsOverlaying(neighbor.GetRelativPosition()) && neighbor.active)
         {
             VectorPathNode newNode = ScriptableObject.CreateInstance("VectorNavigation.VectorPathNode") as VectorPathNode;
             newNode.Setup(neighbor, position, endPoint, node);
             open.Add(newNode);
             overlayList.Add(newNode.position);
         }
     }
 }
示例#2
0
        public IEnumerator FindPath(Vector3 lastPoint, Vector3 startPoint)
        {
            position = startPoint;

            pathFound = false;

            open.Clear();
            closed.Clear();
            path.Clear();
            overlayList.Clear();

            VectorPathNode current = CreateInstance("VectorNavigation.VectorPathNode") as VectorPathNode;

            current.Setup(GetClosestNode(position, true), position, lastPoint, null);
            current.isStart = true;
            start           = current;
            open.Add(start);
            overlayList.Add(start.position);

            endPoint = GetClosestNode(lastPoint).GetRelativPosition();

            int count = 0;

            while (!pathFound && open.Count > 0)
            {
                current = GetNextPathNode(open);
                if (current != null)
                {
                    if (current.position != endPoint)
                    {
                        SetupAroundNode(current);
                    }
                    else
                    {
                        path.Add(current);
                        pathFound = true;
                    }
                    closed.Add(current);
                    open.Remove(current);

                    count++;
                }

                if (count >= 1000 || current == null)
                {
                    Debug.Log("Failed to reach end");
                    break;
                }
            }

            count = 0;
            if (pathFound)
            {
                while (!path.Contains(start))
                {
                    if (!path.Contains(path[0].lastNode))
                    {
                        path.Insert(0, path[0].lastNode);
                    }

                    count++;
                    if (count >= 10000)
                    {
                        Debug.Log("Failed to find path");
                        break;
                    }
                }
            }

            yield return(null);
        }