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);
         }
     }
 }
Exemple #2
0
        public void Setup(VectorNode node, Vector3 start, Vector3 end, VectorPathNode preNode)
        {
            vecNode = node;

            position = node.GetRelativPosition();
            normal   = node.normal;

            if (preNode == null)
            {
                startDistance = Vector3.Distance(position, start);
            }
            else
            {
                startDistance = Vector3.Distance(position, preNode.position) + preNode.startDistance;

                lastNode = preNode;
                name     = preNode.name + 1;
                lastName = name - 1;
            }
            endDistance = Vector3.Distance(position, end);
        }
        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);
        }