I am developing a 2d game where the world is made up of tiles.
I have an overworld and a “current world”:
- the current world represent the current tiles on screen at any given time, currently this is in a 2d array (14×10 elements).
- the overworld is the entire world, currently 28×20 elements, which means there are four screens of tiles. Again these are planned to be stored in a 2d array
I had originally thought to take the entire group of tiles and break them in to ‘current world’ sized 2d arrays that get passed to the current world object. This would mean however, having to work with a 4d array in the overworld level, which seems to be pretty ugly and more confusing to set up and play with later on.
It would have looked something like:
tiles[overworldX][overworldY][currentWorldX][currentWorldY]
where “tiles” is the 4d array and:
tiles[overworldX][overworldY]
would get you a 2d array of the group of tiles that fit on a screen/current world. The only idea I can come up with to make this slightly better is to create a helper object that just wraps around a 2d array, breaking up a 4d array in to two 2d arrays, but in essence it is still a 4d array.
Any suggestions on a better way to do this or perhaps a different data structure I can use? Or maybe keeping only a large group of 2d tiles for the overworld, then coming up with some fancy indexing/slicing algorithm that can pull out ‘current world’ sized parts?
4
You’re overcomplicating it. Just have a single 2d tiles
array, and index it like so:
tiles[overworldX + currentWorldX][overworldY + currentWorldY]
1