Question

In my tile-based top down game, somewhat like Terraria, I need to have tiles efficiently update in a certain circumstance. The circumstance I am currently working on is drawing connections between tiles.

By connections I mean drawing an overlay on top of each tile depending on its position, so if the tile is on a corner, it will have a corner overlay which connects to other overlays. Somewhat like connected textures in Minecraft rather connected textures in any tile games most likely.

The problem I am having right now is how to handle updating the overlay. For example, if I destroy a tile, the overlays of all the adjacent tiles must be updated because there is no longer a tile to connect to. This part is easy because I could just do something like this:

updateOverlay(x+1, y);
updateOverlay(x-1, y);
updateOverlay(x, y+1);
updateOverlay(x, y-1);

This is where the problem comes in. What happens if when I call the updateOverlay(x+1, y), the overlay of this changes such that the adjacent tiles to this tile require an overlay update, and what happens if one of those adjacent tiles causes the same thing? Then it's a huge string of updates.

I tried a recursive implementation somewhat like the following:

public boolean updateOverlay(int x, int y){
   changeOverlayAccordingly();

   if(overlayHasChanged) return true;

      updateOverlay(x+1, y);
      updateOverlay(x-1, y);
      updateOverlay(x, y+1);
      updateOverlay(x, y-1);

   return false;
}

the if(overlayHasChanged) statement was supposed to stop an infinite recursive loop from occuring, but it didn't really do anything because I still got stack overflow errors. Also a recursive solution to the problem probably isn't the best because it wouldn't be that stable.

Once again, I understand that I could just update all of the adjacent overlays and be done with it, but what if those updates lead to more updates being required?

   [2]   [6]
[1][x][3][5][8]
   [4]   [7]

To further illustrate my point, look to the above diagram.

if [x] is the original overlay being updated, [1] [2] [3] and [4] will be updated as well. But, what happens if [3] changes such that [5] requires and update too? and what happens if [5] then changes such that [6] [7] and [8] require updates?

Are there any other solutions to handle updating the tile overlays?

Any help would be greatly appreciated

Was it helpful?

Solution

What you need is to put things into perspective. Giving priority to certain updates, you would avoid the infinite loops. I'd try updating it top down, and then checking if everything is alright; then, just adjusting some points.

But, then, why would you even need to update things on chain? If it's a one block at a time operation, there's no way changes would occur on a radius greater than one block. (Please explain and I'll edit accordingly). If it involves destruction of adjacent tiles, try going from center to borders, ignoring the blocks that point to the center.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top