Пример #1
0
    void FindPath(Vector3 startPos, Vector3 targetPos)
    {
        Node_nn startNode  = grid.NodeFromWorldPoint(startPos);
        Node_nn targetNode = grid.NodeFromWorldPoint(targetPos);

        if (startNode == targetNode)
        {
            return;
        }

        List <Node_nn>    openSet   = new List <Node_nn>();
        HashSet <Node_nn> closedSet = new HashSet <Node_nn>();

        openSet.Add(startNode);

        while (openSet.Count > 0)
        {
            Node_nn node = openSet[0];
            for (int i = 1; i < openSet.Count; i++)
            {
                if (openSet[i].fCost < node.fCost || openSet[i].fCost == node.fCost)
                {
                    if (openSet[i].hCost < node.hCost)
                    {
                        node = openSet[i];
                    }
                }
            }

            openSet.Remove(node);
            closedSet.Add(node);

            if (node == targetNode)
            {
                RetracePath(startNode, targetNode);
                return;
            }

            foreach (Node_nn neighbour in grid.GetNeighbours(node))
            {
                if (!neighbour.walkable || closedSet.Contains(neighbour))
                {
                    continue;
                }

                int newCostToNeighbour = node.gCost + GetDistance(node, neighbour);
                if (newCostToNeighbour < neighbour.gCost || !openSet.Contains(neighbour))
                {
                    neighbour.gCost  = newCostToNeighbour;
                    neighbour.hCost  = GetDistance(neighbour, targetNode);
                    neighbour.parent = node;

                    if (!openSet.Contains(neighbour))
                    {
                        openSet.Add(neighbour);
                    }
                }
            }
        }
    }
Пример #2
0
    public void UpdateTiles()
    {
        TempNodeCache.Clear();
        RemoveNodeCache.Clear();

        for (float x = -lengthRadius; x < lengthRadius; x += 1)
        {
            for (float y = -heightRadius; y < heightRadius; y += 1)
            {
                getWorldTilesPos = gameObject.transform.position + new Vector3(x + 0.8f, y + 0.59f, 0); // offset values from testing and trying to get it right in the inspector

                // Debug.Log("getWorldTilesPos "+getWorldTilesPos);
                nodeCache = GridRef.NodeFromWorldPoint(getWorldTilesPos);
                TempNodeCache.Add(nodeCache);
                NodeCollisionCache.Add(nodeCache);
                nodeCache.walkable = false;
            }
        }

        // compare the tiles that are no longer present and cache them to be removed
        foreach (Node_nn node in NodeCollisionCache)
        {
            if (!TempNodeCache.Contains(node)) // if the moving platform is no longer over a node/tile
            {
                RemoveNodeCache.Add(node);
                if (!node.isStatic) // incase of static/unwalkable background, dont make it walkable
                {
                    node.walkable = true;
                }
            }
        }

        // remove tiles that the platform is not over
        foreach (Node_nn node in RemoveNodeCache)
        {
            NodeCollisionCache.Remove(node);
        }
    }