0
votes

I have a list of Tiles. Each tile contains a Vector2 and a Texture. Simple tile class.

List<Tile>tiles = new List<Tile>();

If I click in the centre of the group of tiles, say a grid that's 10x10, how can I remove the tiles in the certain 2x2 area. Or how can I manipulate tiles that are already added to the list, based on their position? Is there an easier way than iterating through them every cycle and matching the position to the tile?

Sorry if this is a confusing question. I don't know how to word it.

2

2 Answers

1
votes

You will have to loop to find neigbours of tile you selected, then when you found them set some flag like "RemoveThis=true". One solution is to use Vector2D.Distance, so you compare distance between every tile and location where you clicked.

There are a lot of different algorithms, but it looks you need Chebyshev distance method: http://en.wikipedia.org/wiki/Chebyshev_distance

Then you can remove all those tiles: tiles.removeAll(function(c) c.RemoveThis)

1
votes

What you are probably looking to do is have an easier way to manipulate your data. Using Lists here is not the better way to go.

I would suggest using an array instead. For example:

//Init 10x10 tile array
int Width = 10;
int Height = 10;
Tile[] Tiles = new Tile[Width, Height];

To add items to this collection, you would do:

for (int x = 0; x < Width; x++)
    for (int y = 0; y < Height; y++)
        Tiles[x, y] = new Tile();

Then to remove a set of for connecting tiles, it is as simple as:

void DeleteTiles(int x, int y)
{
    Tiles[x, y] = null;
    Tiles[(x+1) % Width, y] = null;
    Tiles[x, (y+1) % Height] = null;
    Tiles[(x+1) % Width, (y+1) % Height] = null;
}