void Awake() { XMax = 50; YMax = 50; HeightMax = 8; Selected = Tiles.Single(t => t.X == 0 && t.Y == 0); }
public void SetTile(int type) { Controls c = GameObject.FindObjectOfType<Controls>(); Tile[] buildableTiles = new Tile[] { Tile.Nothing, Tile.Street, Tile.Blockade, Tile.Checkpoint }; int x = (int)Math.Round(c.Cursor.x); int y = (int)Math.Round(c.Cursor.y); if (tiles[x, y] != (Tile)type && buildableTiles.Contains(tiles[x, y])) { //if(ressources[tiles[x][y]] > 0 { ressources--; tiles[x, y] = (Tile)type; UpdatePaths(Distance); UpdatePaths(InfectedDistance, true); } }
public static List<Point> Pathfind(Tile[,] tileArray, int x, int y, int x2, int y2) { int Width = tileArray.GetLength(1); int Height = tileArray.GetLength(0); int[,] cost = new int[Width, Height]; cost[x, y] = 1; //floor type List<Point> active = new List<Point>(); active.Add(new Point(x, y)); // pathfind while (true) { // get lowest cost point in active list Point point = active[0]; for (int i = 1; i < active.Count; i++) { Point p = active[i]; if (cost[p.x, p.y] < cost[point.x, point.y]) point = p; } // if end point if (point.x == x2 && point.y == y2) break; // move in directions int currentCost = cost[point.x, point.y]; if (point.x - 1 >= 0 && cost[point.x - 1, point.y] == 0) { active.Add(new Point(point.x - 1, point.y)); cost[point.x - 1, point.y] = currentCost + getCost(tileArray[point.x - 1, point.y]); } if (point.x + 1 < Width && cost[point.x + 1, point.y] == 0) { active.Add(new Point(point.x + 1, point.y)); cost[point.x + 1, point.y] = currentCost + getCost(tileArray[point.x + 1, point.y]); } if (point.y - 1 >= 0 && cost[point.x, point.y - 1] == 0) { active.Add(new Point(point.x, point.y - 1)); cost[point.x, point.y - 1] = currentCost + getCost(tileArray[point.x, point.y - 1]); } if (point.y + 1 < Height && cost[point.x, point.y + 1] == 0) { active.Add(new Point(point.x, point.y + 1)); cost[point.x, point.y + 1] = currentCost + getCost(tileArray[point.x, point.y + 1]); } active.Remove(point); } // work backwards and find path List<Point> points = new List<Point>(); Point current = new Point(x2, y2); points.Add(current); while (current.x != x || current.y != y) { int highest = cost[current.x, current.y]; int left = highest, right = highest, up = highest, down = highest; // get cost of each direction if (current.x - 1 >= 0 && cost[current.x - 1, current.y] != 0) { left = cost[current.x - 1, current.y]; } if (current.x + 1 < Width && cost[current.x + 1, current.y] != 0) { right = cost[current.x + 1, current.y]; } if (current.y - 1 >= 0 && cost[current.x, current.y - 1] != 0) { up = cost[current.x, current.y - 1]; } if (current.y + 1 < Height && cost[current.x, current.y + 1] != 0) { down = cost[current.x, current.y + 1]; } // move in the lowest direction if (left <= GetMin(up, down, right)) { points.Add(current = new Point(current.x - 1, current.y)); } else if (right <= GetMin(left, down, up)) { points.Add(current = new Point(current.x + 1, current.y)); } else if (up <= GetMin(left, right, down)) { points.Add(current = new Point(current.x, current.y - 1)); } else { points.Add(current = new Point(current.x, current.y + 1)); } } points.Reverse(); return points; }
private static int getCost(Tile t) { if (!t.empty) { return 99; } else { return 1; } }
public void SetTile(int x, int y) { var tile = Tiles.SingleOrDefault(t => t.X == x && t.Y == y); if (tile != null) Selected = tile; }
public void PlusY() { Selected = Tiles.Single(t => t.Y == Selected.Y + 1); }
public void PlusX() { Selected = Tiles.Single(t => t.X == Selected.X + 1); }
//Calculate the 2D array of tiles, given the tile prefab private void loadTileArray(GameObject tileMapGameObject) { string strTileArray = ""; Bounds mapBounds = tileMapGameObject.GetComponentInChildren<Renderer>().bounds; int tileWidth = (int)Math.Ceiling(mapBounds.size.x / Tile.TILE_SIZE); int tileHeight = (int)Math.Ceiling(mapBounds.size.y / Tile.TILE_SIZE); tileArray = new Tile[tileWidth, tileHeight]; for (int y = 0; y < tileHeight; y++) { for (int x = 0; x < tileWidth; x++) { Vector3 center = new Vector3(x * Tile.TILE_SIZE + (Tile.TILE_SIZE / 2), -y * Tile.TILE_SIZE + (Tile.TILE_SIZE / 2), 0); Vector3 size = new Vector3(Tile.TILE_SIZE, Tile.TILE_SIZE); Bounds tileBounds = new Bounds(center, size); bool empty = !checkCollision(tileBounds); tileArray[x, y] = new Tile(x, y, empty); strTileArray += empty ? "." : "#"; } strTileArray += System.Environment.NewLine; } int i = 1; }