0

I'm trying to update the surrounding grid of tiles if one of them is disabled (not removed). For example, imagine we have a grid of tiles each with x representing one texture:

x, x, x, x, x, x
x, x, x, x, x, x
x, x, x, x, x, x
x, x, x, x, x, x
x, x, x, x, x, x
x, x, x, x, x, x

Now if some of these tiles are disabled, I need to update the surrounding tile textures to a different texture:

x, x, x, x, x, x
x, y, y, y, x, x
x, y,  , y, y, y
x, y,  , y,  , y
x, y, y, y,  , y
x, x, x, y, y, y

I'm trying to find the most efficient way of doing this. At present all of the tile data is stored in a dictionary with the key as a vector2.

I'm using C#/Unity 3D, however, I'm not necessarily looking for code but more or less logic on how to do this efficiently so I can program it in.

Any suggestions, examples, or what not would be helpful.

Thanks.

Euthyphro
  • 237
  • 3
  • 13
  • Possible duplicate: http://gamedev.stackexchange.com/questions/29524/choose-tile-based-on-adjacent-tiles – House Dec 10 '13 at 14:21
  • Without diving down into an actual answer, I can say the data structure used to store the map will be important. This situation brings to my mind the Half-Edge data structure for mesh representation. I suppose something similar could be used in this case. – kevintodisco Dec 10 '13 at 16:19

1 Answers1

0

The only way seems to look up the direct neighbors of the tile you disabled.

void DisableTile(Vector2 tileLocation)
   {
     tiles[tileLocation].Disable();
     if(tileLocation.X > 0) //if the neighbor on the left exists
     {
        tiles[new Vector2(tileLocation.X-1, tileLocation.Y)].RespondToNeighborDisabled(tileLocation); //update it
     }
     //repeat this process for all possible neighbors. (there should be 10 of those)
   }

A good way would be to implement a function like this on your tile entity, and to call it for all neighboring tiles when you disable or enable a tile :

void RespondToNeighborDisabled(Vector2 neighbor)
{
  if(this.Disabled) return;

  if(this.X < neighbor.X)
  {
     //the updated neighbor was to my right.
  }
  else if (this.X == neighbor.X)
  {
     //the neighbor was above or below me
  } 
  else
  {
     //the updated neighbor was to my left
  }
  //etc.
}
Timothy Groote
  • 374
  • 1
  • 11
  • 1
    though it's probably easier to stop using a Dictionary and just use a 2D array – Timothy Groote Dec 10 '13 at 14:34
  • 2
    I second the suggestion to move to a 2D array, if you can. Particularly if your maps are mostly contiguous blocks like your example. I recently TA'd an algorithms course, and the #1 cause of slow performance causing students grief was use of dictionaries where an array would suffice. They're both O(1) lookups, but that hides the extra hashing/probing/chaining work dictionaries do, not to mention their memory overhead. – DMGregory Dec 10 '13 at 14:56