public Tile(char typeChar) { gScore = 0; hScore = 0; fScore = 0; parentTile = null; graphics = new GraphicsDevice(); if (typeChar == '0') { type = "grass"; texture = Texture2DFromFile(graphics, "Content/grass.png"); isWalkable = true; } else if (typeChar == '1') { type = "water"; texture = Texture2DFromFile(graphics, "Content/water.png"); isWalkable = false; } else if (typeChar == '2') { type = "bridge"; texture = Texture2DFromFile(graphics, "Content/bridge.png"); isWalkable = true; } else if (typeChar == '3') { type = "rock"; texture = Texture2DFromFile(graphics, "Content/rock.png"); isWalkable = false; } else if (typeChar == '4') { type = "select"; texture = Texture2DFromFile(graphics, "Content/grass2.png"); isWalkable = true; } height = texture.Height; width = texture.Width; }
public Map(String fileName) { name = fileName; openList = new List<Tile>(); closedList = new List<Tile>(); isDrawn = false; string[] tiles = File.ReadAllLines(fileName); width = tiles[0].Length; height = tiles.Length; mapTiles = new Tile[height, width]; for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { mapTiles[y, x] = new Tile(tiles[y][x]); mapTiles[y, x].xPosition = x; mapTiles[y, x].yPosition = y; mapTiles[y, x].fScore = 0; } } //pathList = new List<Tile>(); }
////////////////////////////////////////////////// // PATHFINDING:////////////////////////////////// ///////////////////////////////////////////////// public List<Tile> GetPath(Vector2 startPoint, Vector2 endPoint, List<Tile> newCollisionList) { // Clear tile scores foreach (Tile myTile in mapTiles) { myTile.parentTile = null; myTile.fScore = 0; myTile.gScore = 0; myTile.hScore = 0; } // Set lists: openList.Clear(); closedList.Clear(); collisionList = newCollisionList; List<Tile> pathList = new List<Tile>(); // Get starting and ending nodes: startTile = GetTile(startPoint); endTile = GetTile(endPoint); if (endTile == null|| endTile.isWalkable == false || closedList.Contains(endTile) == true) { return pathList; } openList.Add(startTile); while(closedList.Contains(endTile)==false && openList.Count != 0) { //System.Console.WriteLine(i++); currentTile = FindLowestScore(); if (currentTile == endTile) { break; } openList.Remove(currentTile); closedList.Add(currentTile); int x = currentTile.xPosition; int y = currentTile.yPosition; // Check all adjacent tiles to see if open: CheckNode(x - 1, y, x, y); CheckNode(x, y - 1, x, y); CheckNode(x + 1, y, x, y); CheckNode(x, y + 1, x, y); CheckNode(x + 1, y + 1, x, y); CheckNode(x + 1, y - 1, x, y); CheckNode(x - 1, y + 1, x, y); CheckNode(x - 1, y - 1, x, y); } while (currentTile.parentTile != null) { //System.Console.WriteLine(currentTile.xPosition + ", " + currentTile.yPosition); pathList.Add(currentTile); //Tile temp = currentTile.parentTile; //currentTile.parentTile = null; currentTile = currentTile.parentTile; if(pathList.Count > 50) { System.Console.WriteLine("break"); break; } } pathList.Reverse(); return pathList; }