Need help implementing robust tile table
Posted: Fri Feb 05, 2010 3:56 am
I need a little help. In brief, I need a table that will contain up to about 2100 immobile tiles. This table needs to be in some format to facilitate the following:
- Object lookup by x and y coordinates (with little to no overhead)
- Ability to randomly select an object
Or, to be more specific, this table must firstly give me an efficient way to know if a tile is on the screen or not (so love.draw() can know whether or not to bother drawing it), secondly to tell me if a tile is at a given coordinate, and finally, offer a way to choose a tile at random.
So far I've been using tiles[x][y] = tile. This lets me easily know if a tile exists at a coordinate by saying "if tiles[x][y] then". I can also tell if a tile is on the screen or not in the draw function by doing "for x, row in pairs(tiles) do if x > camera.x and x < camera.x+screen.x then". However, what I CAN'T do this way is choose a tile at random.
So, does anyone have any ideas?
- Object lookup by x and y coordinates (with little to no overhead)
- Ability to randomly select an object
Or, to be more specific, this table must firstly give me an efficient way to know if a tile is on the screen or not (so love.draw() can know whether or not to bother drawing it), secondly to tell me if a tile is at a given coordinate, and finally, offer a way to choose a tile at random.
So far I've been using tiles[x][y] = tile. This lets me easily know if a tile exists at a coordinate by saying "if tiles[x][y] then". I can also tell if a tile is on the screen or not in the draw function by doing "for x, row in pairs(tiles) do if x > camera.x and x < camera.x+screen.x then". However, what I CAN'T do this way is choose a tile at random.
So, does anyone have any ideas?