I am trying to create a paint bucket-style flood fill for a tile system, in order to create an editor.
However, basing it on the four way flood fill algorithm is causing problems. x + 1 and y + 1 work just fine, however the moment they are paired with x - 1 and y - 1, it causes a stack overflow. In addition, my comparative point is the color of the tile. There are instances where it will just ignore if colors do match, and overwrite them anyway, when it ought to exit instead.
Following the example algorithms here, this code feels like it -ought- to work:
However, as described above, my own implementation in C# just isn't working properly in all directions:
public void FloodFill(int x, int y, Color fillColor, Color oldColor)
{
if (x < 0 || x >= boardSize) return;
if (y < 0 || y >= boardSize) return;
Tile tile = world.GetTileAt(x, y);
if (tile.currentColor.Equals(fillColor))
{
return;
}
if (tile.currentColor.Equals(oldColor))
{
UpdateTile(tile.currentTile, fillColor);
FloodFill(x - 1, y, fillColor, oldColor);
FloodFill(x + 1, y, fillColor, oldColor);
FloodFill(x, y - 1, fillColor, oldColor);
FloodFill(x, y + 1, fillColor, oldColor);
}
return;
}
My best guess is that metaphorically, it's tripping over itself, but that should not be occurring with the comparisons and returns to prevent execution. This conclusion comes from the log - it keeps hopping tiles when dealing with both positive and negative numbers.
Here's the log thus far:
MasterController.FloodFill (System.Int32 x, System.Int32 y, UnityEngine.Color fillColor, UnityEngine.Color oldColor) (at Assets/Game Assets/Scripts/Gameplay/MasterController.cs:134)
MasterController.FloodFill (System.Int32 x, System.Int32 y, UnityEngine.Color fillColor, UnityEngine.Color oldColor) (at Assets/Game Assets/Scripts/Gameplay/MasterController.cs:136)
MasterController.FloodFill (System.Int32 x, System.Int32 y, UnityEngine.Color fillColor, UnityEngine.Color oldColor) (at Assets/Game Assets/Scripts/Gameplay/MasterController.cs:138)
MasterController.FloodFill (System.Int32 x, System.Int32 y, UnityEngine.Color fillColor, UnityEngine.Color oldColor) (at Assets/Game Assets/Scripts/Gameplay/MasterController.cs:136)
MasterController.FloodFill (System.Int32 x, System.Int32 y, UnityEngine.Color fillColor, UnityEngine.Color oldColor) (at Assets/Game Assets/Scripts/Gameplay/MasterController.cs:138)
MasterController.FloodFill (System.Int32 x, System.Int32 y, UnityEngine.Color fillColor, UnityEngine.Color oldColor) (at Assets/Game Assets/Scripts/Gameplay/MasterController.cs:136)
MasterController.FloodFill (System.Int32 x, System.Int32 y, UnityEngine.Color fillColor, UnityEngine.Color oldColor) (at Assets/Game Assets/Scripts/Gameplay/MasterController.cs:138)
This is the code for UpdateTile and GetTileAt:
public void UpdateTile(Transform tile, Color color)
{
Mesh mesh = tile.GetComponent<MeshFilter>().mesh;
Color32[] nextColor = new Color32[mesh.vertices.Length];
for (int i = 0; i < nextColor.Length; i++)
{
nextColor[i] = color;
}
mesh.colors32 = nextColor;
}
public Tile GetTileAt(int x, int y)
{
if (x > size || x < 0 || y > size || y < 0)
{
return null;
}
return tiles[x, y];
}