I’m creating a 2d game, with a huge tilebased world. The grid is made up of 16x16px tiles. The grid also has a layer dimension: A couple of background graphics layers, a couple of action layers (game logic, collision, more) and a foreground graphics layer.
With a 1024*1024 grid and say 5 layers thats 5242880 tiles. So for performance reasons, I can’t have the whole world active at once - I can’t even create a GameObject for each tile to activate/deactivate.
So I think I need a solution where the underlying, layered griddata is stored in a way, so I blazingly fast can pull e.g.:
GetTiles (int minX, int maxX, int minY, int maxY)
This method could be called by the camera, based on what’s visible. The return value is used to create gameobjects and place them correctly.
My first idea was to store each layer in a 2d array. But e.g. the foreground graphics layer is rarely used. So that 2d array would mostly hold nulls, which is quite a waste, right?
My second idea was having a Tile class, where each Tile holds an gridX-, gridY- and layer value. And then have an array of all tiles in the game. Problem is, that then I would have to iterate through the whole list with something like:
if (x > minX && x < maxX && y > minY && y < maxY)
…which would create a framerate hiccup.
So my question is: What’s the best way to store a lot of layered grid data, so I quickly can pull a 2d range?
I’m a quite experienced front-end programmer, but I barely know anything about storing larger chunks of data or other back-end’ish stuff.
I’m not expecting a ready-to-use-coded solution… but any starting points, explanation of key concepts or reads/tutorials on this subject is most wellcome! I really don’t know where to start… Thanks in advance