Reputation: 471
I'm working on a 2d top down game in pure Java and until now I've got a noise mapping system that works with chunks of tiles (and entities) just like Minecraft.
The way I store tiles is by creating a int[s * s]
array, where s
is the size (in tiles) that a side of the chunk has. Then, I have a static array of Tiles which contains each of the possible tiles in the game, indexed with an integer. Those are the integers stored in the int[s * s]
in each chunk. Now, I want to be able to overlay tiles, such as invisible spawn tiles, walls, trees and transition tiles (like when grass changes to sand: there should be a border of grass over the sand, which would be a new tile).
The problem is that only one integer can be stored in each coordinate of the chunk. Another way to do it would be to put actual instances of the tiles inside the chunks and make the overlaying tiles point to the ones directly below them (like a stack), but I think that's kind of a bad approach. Another approach which I tested was to make an array of Lists of tiles, that was also really bad.
Which would be a good way to accomplish this?
Upvotes: 1
Views: 919
Reputation: 106351
Typically you would separate the storage for tiles and objects.
int [w*h]
or similar for the tiles themselves,
which would only allow one tile per square.ArrayList [w*h]
which stores a list of
entities in each square. This array would contain null
for empty
squares, so the extra storage cost for the majority of squares (that
have no entities in them) is minimal.There are other approaches of course, but this has the advantage that it is both simple and pretty efficient.
Only real downside is that you will have to write separate code for handling tiles and entities - but since you usually handle tiles and entities differently anyway then this is unlikely to be a big problem.
Upvotes: 1