private void UpdateVisual()
 {
     MeshUtils.CreateEmptyMeshArrays(m_grid.GetWidth() * m_grid.GetHeight(),
                                     out Vector3[] vertices, out Vector2[] uv, out int[] triangles);
Exemple #2
0
    public List <PathNode> FindPath(int startX, int startY, int endX, int endY)
    {
        PathNode startNode = m_grid.GetGridObject(startX, startY);
        PathNode endNode   = m_grid.GetGridObject(endX, endY);

        if (startNode == null || endNode == null)
        {
            return(null);    // Invalid path
        }
        m_openList = new List <PathNode> {
            startNode
        };
        m_closeList = new List <PathNode>();

        for (int x = 0; x < m_grid.GetWidth(); x++)
        {
            for (int y = 0; y < m_grid.GetHeight(); y++)
            {
                PathNode pathNode = m_grid.GetGridObject(x, y);

                pathNode.SetGCost(int.MaxValue);
                pathNode.CalculateFCost();
                pathNode.SetPreviousNode(null);
            }
        }

        startNode.SetGCost(0);
        startNode.SetHCost(CalculateDistanceCost(startNode, endNode));
        startNode.CalculateFCost();

        while (m_openList.Count > 0)
        {
            PathNode currentNode = GetLowestFCostNode(m_openList);

            // Reached final node
            if (currentNode == endNode)
            {
                return(CalculatePath(endNode));
            }

            m_openList.Remove(currentNode);
            m_closeList.Add(currentNode);

            foreach (PathNode neighbouringNode in GetNeighbourList(currentNode))
            {
                if (m_closeList.Contains(neighbouringNode))
                {
                    continue;
                }
                if (!neighbouringNode.isWalkable)
                {
                    m_closeList.Add(neighbouringNode);
                    continue;
                }

                int tentativeGCost = currentNode.GetGCost() + CalculateDistanceCost(currentNode, neighbouringNode);

                if (tentativeGCost < neighbouringNode.GetGCost())
                {
                    neighbouringNode.SetPreviousNode(currentNode);
                    neighbouringNode.SetGCost(tentativeGCost);
                    neighbouringNode.SetHCost(CalculateDistanceCost(neighbouringNode, endNode));
                    neighbouringNode.CalculateFCost();

                    if (!m_openList.Contains(neighbouringNode))
                    {
                        m_openList.Add(neighbouringNode);
                    }
                }
            }
        }

        // Ran out of nodes on the m_openList
        return(null);
    }