Exemplo n.º 1
0
        public Vector2[] FindPath(Vector2 from, Vector2 to, SAP2DPathfindingConfig config)
        {
            SAP_GridSource grid        = GetGrid(config.GridIndex);
            SAP_TileData   startTile   = grid.GetTileDataAtWorldPosition(from);
            SAP_TileData   targetTile  = grid.GetTileDataAtWorldPosition(to);
            SAP_TileData   currentTile = startTile;

            if (targetTile.isWalkable == false)
            {
                return(null);
            }

            List <SAP_TileData> openList   = new List <SAP_TileData>();
            List <SAP_TileData> closedList = new List <SAP_TileData>();

            while (closedList.Contains(targetTile) == false)
            {
                List <SAP_TileData> neighbors = grid.GetNeighborTiles(currentTile, config.CutCorners);

                foreach (SAP_TileData neighbor in neighbors)
                {
                    if (neighbor.isWalkable == false || closedList.Contains(neighbor) == true)
                    {
                        continue;
                    }
                    if (openList.Contains(neighbor) == true)
                    {
                        SAP_TileData oldParent = neighbor.ParentTile;
                        int          oldG      = neighbor.G;
                        neighbor.SetParentTile(currentTile, targetTile);
                        if (neighbor.G >= oldG)
                        {
                            neighbor.SetParentTile(oldParent, targetTile);
                        }
                    }
                    else
                    {
                        neighbor.SetParentTile(currentTile, targetTile);
                        openList.Add(neighbor);
                    }
                }
                openList.Remove(currentTile);
                closedList.Add(currentTile);

                currentTile = FindNextTile(openList);

                if (currentTile == null)
                {
                    Debug.LogError("Path not found");
                    return(null);
                }
            }
            return(PathRecovery(startTile, targetTile));
        }
Exemplo n.º 2
0
 private void Reset()
 {
     Config = Resources.Load("Main/Configs/Default") as SAP2DPathfindingConfig;
 }