public static GridTile FindClosestGridTile(Vector3 point) { GridTile[,] world = TerrainController.thisTerrainController.world; int tileSize = TerrainController.thisTerrainController.tileSize; int x = (int)((point.x + tileSize / 2) / tileSize); int z = (int)((point.z + tileSize / 2) / tileSize); if (world == null) { return(null); } if (x >= world.GetLength(0) || x < 0) { return(null); } if (z >= world.GetLength(1) || z < 0) { return(null); } return(world[x, z]); }
private List <GridTile> GetNeighbourList(GridTile currentTile) { List <GridTile> neighbourList = new List <GridTile>(); if (currentTile.X - 1 >= 0) { // Left neighbourList.Add(GetGridTile(currentTile.X - 1, currentTile.Y)); if (currentTile.GridData.IsDiagonalPathEnabled) { // Left Down if (currentTile.Y - 1 >= 0) { neighbourList.Add(GetGridTile(currentTile.X - 1, currentTile.Y - 1)); } // Left Up if (currentTile.Y + 1 < grid.GetLength(1)) { neighbourList.Add(GetGridTile(currentTile.X - 1, currentTile.Y + 1)); } } } if (currentTile.X + 1 < grid.GetLength(0)) { // Right neighbourList.Add(GetGridTile(currentTile.X + 1, currentTile.Y)); if (currentTile.GridData.IsDiagonalPathEnabled) { // Right Down if (currentTile.Y - 1 >= 0) { neighbourList.Add(GetGridTile(currentTile.X + 1, currentTile.Y - 1)); } // Right Up if (currentTile.Y + 1 < grid.GetLength(1)) { neighbourList.Add(GetGridTile(currentTile.X + 1, currentTile.Y + 1)); } } } // Down if (currentTile.Y - 1 >= 0) { neighbourList.Add(GetGridTile(currentTile.X, currentTile.Y - 1)); } // Up if (currentTile.Y + 1 < grid.GetLength(1)) { neighbourList.Add(GetGridTile(currentTile.X, currentTile.Y + 1)); } return(neighbourList); }
private GridTile ItlGetTileFromPos(int x, int z) { if (x < 0 || x >= m_pGridTiles.GetLength(0) || z < 0 || z >= m_pGridTiles.GetLength(1)) { return(null); } else { return(m_pGridTiles[x, z]); } }
public string Render() { // Set up image size int width = (Grid.GetLength(0) * TileSize) + TileSize; int height = (Grid.GetLength(1) * TileSize) + TileSize; // Create bitmap and graphics Bitmap editedBitmap = new Bitmap(width, height); Graphics graphicImage = Graphics.FromImage(editedBitmap); graphicImage.SmoothingMode = SmoothingMode.AntiAlias; // Only render background if the file exists, not all games have background string backgroundPath = Path.Combine(AppContext.BaseDirectory, $"textures/background_{Name}.png"); if (File.Exists(backgroundPath)) { Bitmap backTexture = new Bitmap(backgroundPath); graphicImage.DrawImage(backTexture, 0, 0, width, height); } // Loop through grid, rendering tiles for (int x = 0; x < Grid.GetLength(0); x++) { for (int y = 0; y < Grid.GetLength(1); y++) { string tileTexturePath = Path.Combine(AppContext.BaseDirectory, $"textures/tile_{Name}_{Grid[x,y].State}.png"); // Some tiles dont have textues, such as tile_connect_free if (File.Exists(tileTexturePath)) { Bitmap tileTexture = new Bitmap(tileTexturePath); graphicImage.DrawImage(tileTexture, (x * TileSize) + (TileSize / 2), (y * TileSize) + (TileSize / 2), TileSize, TileSize); tileTexture.Dispose(); } } } // Save board string path = Path.Combine(AppContext.BaseDirectory, $"textures/games/grid_{Name}_{Id}.png"); editedBitmap.Save(path, System.Drawing.Imaging.ImageFormat.Png); // Dispose of what we no longer need graphicImage.Dispose(); editedBitmap.Dispose(); // Return path to board return(path); }
private void GenerateLevel() { //Generate new Level level = new GridTile[xSize, ySize]; for (int x = 0; x < xSize; x++) { for (int y = 0; y < ySize; y++) { GridTile _tile = Instantiate(tilePrefab, new Vector3(0 + x * xOffset, 0, 0 + y * yOffset), Quaternion.identity, this.transform); _tile.gameObject.name = "Tile: " + x + ", " + y; _tile.coordinates = new Vector2Int(x, y); level[x, y] = _tile; } } //Set up neighbours for (int x = 0; x < level.GetLength(0); x++) { for (int y = 0; y < level.GetLength(1); y++) { //north if (y < level.GetLength(1) - 1) { level[x, y].neighbourNorth = level[x, y + 1]; level[x, y].neighbours.Add(level[x, y + 1]); } //south if (y > 0) { level[x, y].neighbourSouth = level[x, y - 1]; level[x, y].neighbours.Add(level[x, y - 1]); } //east if (x < level.GetLength(0) - 1) { level[x, y].neighbourEast = level[x + 1, y]; level[x, y].neighbours.Add(level[x + 1, y]); } //west if (x > 0) { level[x, y].neighbourWest = level[x - 1, y]; level[x, y].neighbours.Add(level[x - 1, y]); } } } }
void CreateTile(int x, int z, int prefabChoice) { if (gridArray.GetLength(0) > x && gridArray.GetLength(1) > z) { if (gridArray[x, z] != null) { Debug.Log("Destroyed"); Destroy(gridArray[x, z].gameObject); } gridArray[x, z] = Instantiate(tilePrefabOptions[prefabChoice]); gridArray[x, z].transform.localPosition = new Vector3(x, 0, z); gridArray[x, z].transform.SetParent(transform); } }
public GridRender(ulong id, string name, int width, int height, string defaultState, int tileSize = 64) { Grid = new GridTile[width, height]; for (int x = 0; x < Grid.GetLength(0); x++) { for (int y = 0; y < Grid.GetLength(1); y++) { Grid[x, y] = new GridTile(defaultState); } } Id = id; Name = name; TileSize = tileSize; }
void CreateGridArray(int width, int depth) { gridArray = new GridTile[width, depth]; for (int x = 0; x < gridArray.GetLength(0); x++) { for (int z = 0; z < gridArray.GetLength(1); z++) { CreateTile(x, z, 0); } } CreateTile(5, 7, 3); CreateTile(6, 7, 3); CreateTile(7, 7, 3); CreateTile(5, 8, 3); //CombineMesh(gameObject); }
private void GenerateGrid(Texture2D map) { //Start with a clean array grid = new GridTile[levelToSpawn.width, levelToSpawn.height]; //Fill it with tile prefabs depending on color for (int x = 0; x < map.width; x++) { for (int y = 0; y < map.height; y++) { SpawnTile(x, y); } } //Set up neighbours for (int x = 0; x < grid.GetLength(0); x++) { for (int y = 0; y < grid.GetLength(1); y++) { //North if (y < grid.GetLength(1) - 1) { grid[x, y].neighbourN = grid[x, y + 1]; } //East if (x < grid.GetLength(0) - 1) { grid[x, y].neighbourE = grid[x + 1, y]; } //South if (y > 0) { grid[x, y].neighbourS = grid[x, y - 1]; } //West if (x > 0) { grid[x, y].neighbourW = grid[x - 1, y]; } } } }
public void SetTileColorsToDefault() { if (Input.GetMouseButtonUp(0)) { GridTile[,] gridTiles = GridManager.Instance.Grid; for (int x = 0; x < gridTiles.GetLength(0); x++) { for (int y = 0; y < gridTiles.GetLength(1); y++) { if (gridTiles[x, y].IsWalkable) { gridTiles[x, y].gameObject.GetComponent <MeshRenderer>().material.color = Color.white; } } } } }
public void SetTile(TileHolder tile) { if (IsInsideGrid(tile.x, tile.y) && tile.tileType != TileType.Normal) { grid[tile.x, tile.y] = new GridTile(tile.x, tile.y, tile.tileType); tiles[tile.x, tile.y] = tile; if (tile.y == grid.GetLength(1) - 1) { tile.SetSprite(TopSide); if (Random.Range(0, 101) < stickSpawnChance) { Transform stick = SpawnStick(true); stick.SetParent(tile.transform); stick.localPosition = new Vector3(-0.25f, 0.9f); } } else if (tile.y == 0) { tile.SetSprite(BottomSide); if (Random.Range(0, 101) < stickSpawnChance) { Transform stick = SpawnStick(false); stick.SetParent(tile.transform); stick.localPosition = new Vector3(-0.25f, -0.9f); } } if (fillWaitCoroutine != null) { StopCoroutine(fillWaitCoroutine); } fillWaitCoroutine = StartCoroutine(WaitForTiles()); } }
public void StartSearch(GridTile startPos, GridTile endPos) { todo = new List <GridTile>(); done = new List <GridTile>(); todo.Add(startPos); smallestDis = startPos; smallestDis.CalcuteDistance(endPos, startPos); while (todo.Count > 0) { smallestDis = todo[0]; for (int i = 0; i < todo.Count; i++) { if (smallestDis != null && todo[i].totalDistance <= smallestDis.totalDistance) { if (temp != todo[i]) { temp = todo[i]; smallestDis = temp; } } } done.Add(smallestDis); done.Add(temp); todo.Remove(temp); if (smallestDis == endPos) { endPos.CalcuteDistance(endPos, smallestDis.previousTile); done.Reverse(); foreach (GridTile tile in done) { if (tile == smallestDis.previousTile && !path.Contains(tile)) { path.Add(tile); smallestDis = smallestDis.previousTile; } } if (smallestDis == startPos) { path.Reverse(); break; } } GridTile[,] grid = GameObject.FindGameObjectWithTag("PathManager").GetComponent <PathManager>().grid; List <GridTile> neighbour = new List <GridTile>(); int k, j; j = smallestDis.x_pos; k = smallestDis.z_pos; //Left if (j - 1 >= 0 && j != 0) { if (!grid[j - 1, k].blockade) { neighbour.Add(grid[j - 1, k]); } } //Right if (j + 1 < grid.GetLength(0) && j != grid.GetLength(0) - 1) { if (!grid[j + 1, k].blockade) { neighbour.Add(grid[j + 1, k]); } } //Below if (k - 1 >= 0 && k != 0) { if (!grid[j, k - 1].blockade) { neighbour.Add(grid[j, k - 1]); } } //Above if (k + 1 < grid.GetLength(1) && k != grid.GetLength(1) - 1) { if (!grid[j, k + 1].blockade) { neighbour.Add(grid[j, k + 1]); } } foreach (GridTile neigbouringGrid in neighbour) { if (!done.Contains(neigbouringGrid) && !todo.Contains(neigbouringGrid)) { neigbouringGrid.CalcuteDistance(endPos, smallestDis); todo.Add(neigbouringGrid); } } } if (todo.Count <= 0) { Debug.LogError("No Target"); } }
public List <GridTile> FindPath(GridTile[,] _grid, GridTile startTile, GridTile endTile) { grid = _grid; if (grid == null || startTile == null || endTile == null) { return(null);// Invalid Path } openList = new List <GridTile> { startTile }; closedList = new List <GridTile>(); for (int x = 0; x < grid.GetLength(0); x++) { for (int y = 0; y < grid.GetLength(1); y++) { GridTile gridTile = grid[x, y]; gridTile.GCost = int.MaxValue; gridTile.CalculateFCost(); gridTile.CameFromTile = null; } } startTile.GCost = 0; startTile.HCost = CalculateDistanceCost(startTile, endTile); startTile.CalculateFCost(); while (openList.Count > 0) { GridTile currentTile = GetLowestFCostTile(openList); if (currentTile == endTile) { // Reached final node //visual return(CalculatePath(endTile)); } openList.Remove(currentTile); closedList.Add(currentTile); foreach (GridTile neighbourTile in GetNeighbourList(currentTile)) { if (closedList.Contains(neighbourTile)) { continue; } if (!neighbourTile.IsWalkable) { closedList.Add(neighbourTile); continue; } int tentativeGCost = currentTile.GCost + CalculateDistanceCost(currentTile, neighbourTile); if (tentativeGCost < neighbourTile.GCost) { neighbourTile.CameFromTile = currentTile; neighbourTile.GCost = tentativeGCost; neighbourTile.HCost = CalculateDistanceCost(neighbourTile, endTile); neighbourTile.CalculateFCost(); if (!openList.Contains(neighbourTile)) { openList.Add(neighbourTile); } } //visual //PathfindingDebugStepVisual.Instance.TakeSnapshot(grid, currentTile, openList, closedList); } } // Out of nodes on the openList return(null); }