Best algorithm for recursive adjacent tiles?
Posted
by
OhMrBigshot
on Game Development
See other posts from Game Development
or by OhMrBigshot
Published on 2012-07-08T23:00:53Z
Indexed on
2012/07/09
3:23 UTC
Read the original article
Hit count: 365
In my game I have a set of tiles placed in a 2D array marked by their Xs and Zs ([1,1],[1,2], etc).
Now, I want a sort of "Paint Bucket" mechanism: Selecting a tile will destroy all adjacent tiles until a condition stops it, let's say, if it hits an object with hasFlag
.
Here's what I have so far, I'm sure it's pretty bad, it also freezes everything sometimes:
void destroyAdjacentTiles(int x, int z) {
int GridSize = Cubes.GetLength(0);
int minX = x == 0 ? x : x-1;
int maxX = x == GridSize - 1 ? x : x+1;
int minZ = z == 0 ? z : z-1;
int maxZ = z == GridSize - 1 ? z : z+1;
Debug.Log(string.Format("Cube: {0}, {1}; X {2}-{3}; Z {4}-{5}", x, z, minX, maxX, minZ, maxZ));
for (int curX = minX; curX <= maxX; curX++) {
for (int curZ = minZ; curZ <= maxZ; curZ++) {
if (Cubes[curX, curZ] != Cubes[x, z]) {
Debug.Log(string.Format(" Checking: {0}, {1}", curX, curZ));
if (Cubes[curX,curZ] && Cubes[curX,curZ].GetComponent<CubeBehavior>().hasFlag) {
Destroy(Cubes[curX,curZ]);
destroyAdjacentTiles(curX, curZ);
}
}
}
}
}
© Game Development or respective owner