I’m doing a bomberman demo, right now I have 1 tilemap that has the background sprites in it.
I have a gamemanager to create multiples destructibles bricks in the awake of the game, by doing so I can randomize the patterns of bricks and store information about the bricks and its drops (the super powers you can sometimes get by exploding the bricks).
For now all I’m doing is taking the size of the tilemap and creating an grid with the same size
public GameObject wall;
[SerializeField] private Tilemap tileMap;
[SerializeField] private Tile[] ground;
[SerializeField] private Tile[] indestructible;
private Grid<BackgroundTile> walls;
[SerializeField] private Vector3Int size;
[SerializeField] private Vector3Int origin;
private void Awake()
{
size = tileMap.size;
walls = new Grid<BackgroundTile>(size.x, size.y);
}
private void Start()
{
origin = tileMap.origin;
Debug.Log(origin);
CreateTiles();
CreateWalls();
}
In the CreateTile() I do a for loop through my grid and then take a position based on the grid and check if the position of the tilemap has an specific tile (In this case a block)
If so the backgroundTile in this position can not hold a brick by default.
-CLASS BACKGROUNDTILE to understand better
public class BackgroundTile
{
public bool IsUsable { get; set; }
public GameObject wall;
public BackgroundTile(bool isUsable, GameObject wall)
{
IsUsable = isUsable;
this.wall = wall;
}
}
After that, in the CreateWalls method I check if the background tile can have a brick, if so I instantiate a brick, and hold the information about this brick in the grid;
private void CreateWalls()
{
for (int i = 0; i < size.x - 1; i++) // really dont know why I need to subtract 1
for (int j = 0; j < size.y; j++)
{
Vector3Int _pos = new Vector3Int(i + origin.x, j + origin.y, origin.z);
if (walls.gridArray[i, j].IsUsable && ShouldCreate(_pos)) //Check if the position need to have a brick
{
GameObject _wall = Instantiate(wall, _pos, Quaternion.identity);
walls.gridArray[i, j].wall = _wall;
}
}
}
For now its working
Im instantiating the gameObjects with this pos
Vector3Int _pos = new Vector3Int(i + origin.x, j + origin.y, origin.z);
Where I and J belongs to the for loop
Origin is an vector3Int that I got in the Start method using the tileMaps property
origin = tileMap.origin;
If I change the tile Anchor of the tilemap or change its position in the scene all the bricks instantiated will be displaced wrong.
Whats the best way to make this more consistent independently of position changes?
Also, I’m having a problem with the property Tilemap.size, its seems that the Vector3Int size returns the x with one additional value, when doing a for loop I need to subtract one,or else the I’m gonna have another column of bricks and i dont know why.
for (int i = 0; i < size.x - 1; i++) // really dont know why I need to subtract 1
for (int j = 0; j < size.y; j++)
{
Vector3Int _pos = new Vector3Int(i + origin.x, j + origin.y, origin.z);
if (walls.gridArray[i, j].IsUsable && ShouldCreate(_pos))
{
GameObject _wall = Instantiate(wall, _pos, Quaternion.identity);
walls.gridArray[i, j].wall = _wall;
}
}