- Home /
How to remove/tag tiles left by procedural generation?
I'm stumped. Decided to make a hex tile map using procedural generation; starting first by generating a flat, blank mesh with a boolean 'isSea' set to true and remains white, then it'll run a few loops to generate the landmass and set their 'isSea' boolean to false. But the process would sometimes leave clusters/groups of 'isSea' tiles inland, which I want to either remove or just set their 'isLake' boolean to true. Problem is I can't quite figure out how to do so; loop through all 'isSea' tiles would affect/include tiles that I dont need change.
How do I go about removing the inland 'isSea' tiles?
First of all: If you are using one GameObject per tile, that's bad. Performance will suffer once the map reaches a certain (not that big) size. Unity can't handle the resulting number of GOs.
As for your problem: Store the tile information in a collection of sorts so you can access each one by it's coordinates. That way, you can compare a tile's sea status with that of it's neightbors and find out which ones are inland.
the tiles are all in a list, regardless of type, so they're easier to loop through.
What condition am I supposed to look for? The loop goes through each individual tile and I can only narrow it down to which tiles has 'isSea' set to true or false. I can loop through and find all the neighbors of the inland 'isSea' tile but the code would also end up looping through the neighbors of the other 'isSea' tiles surrounding the coast. :(
That's why you are better off storing the tiles in a collection that lets you access them by their coordinates. For example, take a struct like this:
public struct Coordinate2 {
public int x;
public int y;
}
Then you can store your tiles in a Generic Dictionary where the coordinate is the key and the value is the tile.
public Dictionary<Coordinate2,Tile> tiles;
You can still loop through them in various ways, for example:
foreach($$anonymous$$eyValuePair<Coordinate2,Tile> pair in tiles) {
Tiletile = pair.Value;
}
or
for(int x = 0; x < 10; x++) {
for(int y = 0; y < 10; y++) {
Tile tile = null;
if(tiles.TryGetValue(new Coordinate2() {x = x, y = y}, out tile) == false) {
continue;
}
}
}
You could also store the tiles in a two-dimensional array
Tile[,] tiles = new Tile[10,10];
for(int x = 0; x < tiles.GetLength(0); x++) {
for(int y = 0; y < tiles.GetLength(1); y++) {
Tile tile = tiles[x,y];
}
}