Beispiel #1
0
    private IEnumerator FindPathCoroutine()
    // private List<Node> FindPathCoroutine()
    {
        // Debug
        ClearMap();

        float xCheck = 0;
        float yCheck = 0;
        int   fCost;
        int   gCost;
        int   hCost;
        Node  neighbour;

        parent = map.grid[(int)start.x, (int)start.y];
        open.Add(parent);         // Initial starting point

        // Loop until end found
        while (open.Count > 0)
        {
            parent = FindLowestFCost();
            // HACK TODO DEBUG
//			parent.debugGO.GetComponentInChildren<Renderer>().material.color = Color.green;

            // Node is closed
            open.Remove(parent);

            // TODO: Check shouldn't need the contains check
            if (!closed.Contains(parent))
            {
                closed.Add(parent);
            }


            if (CheckReachedTarget())
            {
                yield return(new WaitForSeconds(1f));

                OnFoundPath?.Invoke();

                yield break;
                // return finalPath;
            }


            // Neighbours recalc
            for (int x = -1; x < 2; x++)
            {
                for (int y = -1; y < 2; y++)
                {
                    // Same as current so bail
                    if (x == 0 && y == 0)
                    {
                        continue;
                    }

                    xCheck = parent.position.x + x;
                    yCheck = parent.position.y + y;

                    // Bail if out of bounds or the current node, or in the closed list
                    if (xCheck < 0 || yCheck < 0 || xCheck >= map.size.x || yCheck >= map.size.y)
                    {
                        continue;
                    }

                    neighbour = map.grid[(int)xCheck, (int)yCheck];
                    // Bail if node used or blocked
                    if (closed.Contains(neighbour) || neighbour.isBlocked)
                    {
                        continue;
                    }

                    // Note: Multiply by ten to maintain ints for distances
                    hCost = (int)(10 * Vector2.Distance(neighbour.position, target));
                    gCost = parent.gCost + (int)(10f * Vector2.Distance(parent.position, neighbour.position));

                    // fCost
                    fCost = hCost + gCost;

                    // Bail if the existing fCost is lower
                    // HACK, don't check for 0, at least do -1 ffs
                    if (neighbour.fCost != 0 && fCost > neighbour.fCost)
                    {
                        continue;
                    }

                    // All good, so record new values (don't do it WHILE you're calculating the f,g,h costs because they rely on previous results)
                    neighbour.hCost = hCost;
                    neighbour.gCost = gCost;
                    neighbour.fCost = fCost;

                    // Debug
                    var textDebug = neighbour.debugGO.GetComponentInChildren <TextMesh>();
                    if (textDebug != null)
                    {
                        textDebug.text = "g=" + neighbour.gCost + "\nh=" + neighbour.hCost + "\nf=" + neighbour.fCost;
                    }

                    neighbour.parent = parent;

//					Debug.DrawLine(new Vector3(neighbour.position.x, 0, neighbour.position.y),
//								   new Vector3(neighbour.position.x, 10f, neighbour.position.y), Color.magenta,
//								   0.1f, false);

                    // TODO: Shouldn't need the contains check
                    if (!open.Contains(neighbour))
                    {
                        open.Add(neighbour);
                    }

                    // Debug
                    cameraDebugPosition = neighbour.position;
                    if (visualiseSpeed > 0 && showNeigbourSearch)
                    {
                        yield return(new WaitForSeconds(visualiseSpeed));
                    }
                }
            }

            // HACK TODO DEBUG
//			parent.debugGO.GetComponentInChildren<Renderer>().material.color = Color.blue;

            if (visualiseSpeed > 0 && showNodeSearch)
            {
                yield return(new WaitForSeconds(visualiseSpeed));
            }
        }

        yield return(new WaitForSeconds(2f));

        OnBlockedPath?.Invoke();
        // return null;
    }
Beispiel #2
0
//	private IEnumerator FindPathCoroutine()
    private List <Node> FindPathCoroutine()
    {
        // Debug
        ClearMap();

        float xCheck = 0;
        float yCheck = 0;
        int   fCost;
        int   gCost;
        int   hCost;
        Node  nodeToCheck;

        current = map.grid[(int)start.x, (int)start.y];
        open.Add(current);         // Initial starting point

        // Loop until end found
        while (open.Count > 0)
        {
            current = FindLowestFCost();
            // HACK TODO DEBUG
            current.debugGO.GetComponentInChildren <Renderer>().material.color = Color.green;

            // Node is closed
            open.Remove(current);

            // TODO: Check shouldn't need the contains check
            if (!closed.Contains(current))
            {
                closed.Add(current);
            }


            if (CheckReachedTarget())
            {
//                yield return new WaitForSeconds(2f);
                OnFoundPath?.Invoke();

                //                yield break;
                return(finalPath);
            }


            // Neighbours recalc
            for (int x = -1; x < 2; x++)
            {
                for (int y = -1; y < 2; y++)
                {
                    // Same as current so bail
                    if (x == 0 && y == 0)
                    {
                        continue;
                    }

                    xCheck = current.position.x + x;
                    yCheck = current.position.y + y;

                    // Bail if out of bounds or the current node, or in the closed list
                    if (xCheck < 0 || yCheck < 0 || xCheck >= map.size.x || yCheck >= map.size.y)
                    {
                        continue;
                    }

                    nodeToCheck = map.grid[(int)xCheck, (int)yCheck];
                    // Bail if node used or blocked
                    if (closed.Contains(nodeToCheck) || nodeToCheck.isBlocked)
                    {
                        continue;
                    }

                    // Note: Multiply by ten to maintain ints for distances
                    hCost = (int)(10 * Vector2.Distance(
                                      nodeToCheck.position,
                                      target));
                    gCost = current.gCost + (int)(10f * Vector2.Distance(
                                                      current.position,
                                                      nodeToCheck.position));

                    // fCost
                    fCost = hCost + gCost;

                    // Bail if the existing fCost is lower
                    if (nodeToCheck.fCost != 0 && fCost > nodeToCheck.fCost)
                    {
                        continue;
                    }

                    // All good, so record new values (don't do it WHILE you're calculating the f,g,h costs because they rely on previous results)
                    nodeToCheck.hCost = hCost;
                    nodeToCheck.gCost = gCost;
                    nodeToCheck.fCost = fCost;

                    // Debug
                    if (nodeToCheck.debugGO.GetComponentInChildren <TextMesh>() != null)
                    {
                        nodeToCheck.debugGO.GetComponentInChildren <TextMesh>().text =
                            nodeToCheck.gCost + ":" + nodeToCheck.hCost + "\n" + nodeToCheck.fCost;
                    }

                    nodeToCheck.parent = current;

                    Debug.DrawLine(new Vector3(nodeToCheck.position.x, 0, nodeToCheck.position.y),
                                   new Vector3(nodeToCheck.position.x, 10f, nodeToCheck.position.y), Color.magenta,
                                   0.1f, false);

                    // TODO: Shouldn't need the contains check
                    if (!open.Contains(nodeToCheck))
                    {
                        open.Add(nodeToCheck);
                    }

//                    if (visualiseSpeed > 0) yield return new WaitForSeconds(visualiseSpeed / 10f);
                }
            }

            // HACK TODO DEBUG
            current.debugGO.GetComponentInChildren <Renderer>().material.color = Color.blue;

//            if (visualiseSpeed > 0) yield return new WaitForSeconds(visualiseSpeed);
        }

//        yield return new WaitForSeconds(2f);
        OnBlockedPath?.Invoke();
        return(null);
    }