0
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ Possible duplicate: gamedev.stackexchange.com/questions/29524/… \$\endgroup\$ – MichaelHouse Dec 10 '13 at 14:21
  • \$\begingroup\$ 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. \$\endgroup\$ – kevintodisco Dec 10 '13 at 16:19
0
\$\begingroup\$

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.
}
\$\endgroup\$
  • 1
    \$\begingroup\$ though it's probably easier to stop using a Dictionary<Vector2> and just use a 2D array \$\endgroup\$ – Timothy Groote Dec 10 '13 at 14:34
  • 2
    \$\begingroup\$ 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. \$\endgroup\$ – DMGregory Dec 10 '13 at 14:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.