I have a large 2D area which is to be represented by graphical tiles.
Each tile needs a few bits of information stored against it - enough that I ended up creating an actual object for each tile - but it doesn’t seem that efficient to me for the actual level size required.
Here’s the sort of information I need to keep an eye on for each tile:
- X & Y position (current a short, since its a grid reference and will never go over 5000 in either direction)
- Power level (a byte, as its minimum is 0 and maximum is 16)
- Requires power (a byte, with a minimum of 0 and maximum of 16)
- Static texture ID (a short, because there will never more than 32,767 textures!)
- Animated texture ID (again, a short, used like the above but either/or)
- Animation frame (a byte, used the control its texture animation)
- Frame time in milliseconds (a short, used the control its texture animation)
So my class looks a bit like this:
package com.ak.game;
public class Tile
{
private short x, y;
private byte power;
private byte needsPower;
private short texID, animTexID;
private byte frame;
private short frameTime;
public Tile(byte x, byte y)
{
this.x = x;
this.y = y;
}
}
My question really is how to best approach the definition of thousands of these tiles.
If I were to want to have a level comprising of, say, a 320 x 3000 tiles, surely an array of Tile[][] objects is really inefficient?
I only ever plan on rendering whatever is on the screen, which is fine (I can reference the leftX / rightX and TopY / BottomY tiles to create a small ‘viewport’ tile loop for this). But updating tiles that are offscreen that need to be tracked? Seems like a hugely inefficient route to have to cycle through all 320 x 3000 (960k) tiles to accomplish this!
I’d be really grateful to get some pointers on how other people may have solved similar situations before. Would I have to look into some kind of aggregating update over multiple frames? Perhaps updating only X number of tiles each frame, thus splitting up the wholesale update leg-work over the course of multiple frames?
I should add that I want to avoid using multi-threading where possible, since I want to try and keep the base code as optimised as possible without giving up and ‘cheating’ on another thread! (something I’m not confident with yet).
Thoughts would be welcome!
Thanks!