Exemple #1
0
 public AStar_Node(TileScript _tileScript, float _gCost, float _hCost, AStar_Node _parent)
 {
     m_TileScript = _tileScript;
     g_cost       = _gCost;
     h_cost       = _hCost;
     m_Parent     = _parent;
 }
Exemple #2
0
    public List <TileScript> GetFoundPath(TileScript _startTile, TileScript _endTile, bool ignoreEndNull = true)
    {
        m_SearchedPath.Clear();
        AStar_Node           endNodePath       = null;
        List <AStar_Node>    openSet           = new List <AStar_Node>();
        List <TileScript>    openSetTileScript = new List <TileScript>();
        HashSet <TileScript> closedSet         = new HashSet <TileScript>();
        AStar_Node           startPathNode     = new AStar_Node(_startTile, 0, Vector3.Distance(_startTile.transform.position, _endTile.transform.position), null);

        openSet.Add(startPathNode);
        openSetTileScript.Add(_startTile);

        while (openSet.Count > 0)
        {
            // get cheapest path
            AStar_Node cheapestNode = null;
            foreach (AStar_Node node in openSet)
            {
                if (cheapestNode == null || cheapestNode.f_cost > node.f_cost)
                {
                    cheapestNode = node;
                }
            }
            if (cheapestNode.m_TileScript == _endTile)
            {
                endNodePath = cheapestNode;
                break;
            }

            // then begin the searching of the neighbours!
            List <TileScript> neighbourOfCheapNode = GetNeighbourTiles(cheapestNode.m_TileScript);
            foreach (TileScript neighbourTile in neighbourOfCheapNode)
            {
                // make sure it is not in the open set and it is walkable
                if (neighbourTile.TileType == TileScript.TILE_TYPE.WALKABLE && !openSetTileScript.Contains(neighbourTile) && !closedSet.Contains(neighbourTile))
                {
                    // this might not be the correct way to calculate GCost but it works for now
                    float      GCost    = Vector3.Distance(_startTile.transform.position, neighbourTile.transform.position) + cheapestNode.g_cost;
                    float      HCost    = Vector3.Distance(neighbourTile.transform.position, _endTile.transform.position);
                    AStar_Node nodePath = new AStar_Node(neighbourTile, GCost, HCost, cheapestNode);
                    // then we can add it in the open list
                    openSetTileScript.Add(neighbourTile);
                    openSet.Add(nodePath);
                }
                else if (!ignoreEndNull && neighbourTile == _endTile &&
                         !openSetTileScript.Contains(neighbourTile) && !closedSet.Contains(neighbourTile))
                {
                    // this might not be the correct way to calculate GCost but it works for now
                    float      GCost    = Vector3.Distance(_startTile.transform.position, neighbourTile.transform.position) + cheapestNode.g_cost;
                    float      HCost    = Vector3.Distance(neighbourTile.transform.position, _endTile.transform.position);
                    AStar_Node nodePath = new AStar_Node(neighbourTile, GCost, HCost, cheapestNode);
                    // then we can add it in the open list
                    openSetTileScript.Add(neighbourTile);
                    openSet.Add(nodePath);
                }
            }
            closedSet.Add(cheapestNode.m_TileScript);
            openSet.Remove(cheapestNode);
            openSetTileScript.Remove(cheapestNode.m_TileScript);
        }
        // then we can start retracing the path
        if (endNodePath != null)
        {
            AStar_Node startTracingNode = endNodePath;
            if (!ignoreEndNull)
            {
                startTracingNode = endNodePath.m_Parent;
            }

            while (startTracingNode != null)
            {
                m_SearchedPath.Add(startTracingNode.m_TileScript);
                startTracingNode = startTracingNode.m_Parent;
            }
            // reverse since it is from end to start!
            m_SearchedPath.Reverse();
        }
        return(m_SearchedPath);
    }