I have a grid where objects can be placed down by the player. Each object that gets placed down is added to a dictionary where the key is the grid position.
Each object that gets placed down either gets added to a new zone, or added to an existing zone if any of the neighbors are in an existing zone. If there are multiple zones, then it just picks the first one it finds and adds it.
The issue I am struggling with is how to handle splitting up zones if an object is removed from the grid.
In the first image I have 2 zones.
In the second image, I add a new object to the grid, because that tile contains 2 neighbor tiles that are in a zone, it merges the 2 zones into one.
In the third image, I remove an object from the grid, which removes it from the zone, and in this case it creates 2 new zones (but could be more depending on neighbors). This is where I am struggling to work out the best way to efficiently do this.
- I have a grid array that holds all objects placed on the grid.
- A zone contains a list of tiles that are in that zone.
- Each tile in a zone also gets a zone id.
What would be a fast efficient way of splitting up a zone into multiple zones?
I thought about checking all the tiles in the zone by checking its neighbors, but thought that it could be quite slow, as a zone can grow to be very big over time (zones can auto merge as well). Also wouldn't there be a chance of an endless loop?