Within Hex World the terrain is made up of thousands of hexagon tiles that can dynamically shift. In order to manage these tiles a system had to be devised that could provide an interface that can allow other objects to interact with the terrain, as well as provide the individual hexagons with a means of operating as a single terrain object.
This is where the world manager comes into play. The world manager is system that is 4 levels deep. With a world manager that contains world chunks, which contains world stacks, which contain the world tiles.
World Manager – The world manager acts as the the front door to the terrain system, allowing access the chunks and the stacks of the system. It also manages the creation of the chunks, setting there neighbours and initiating there own creation of their own world stacks that they contain.
World Chunk – The world chunks contain a 16 x 16 resolution of world stacks each. These series of world stacks makes it easier to manage the terrain as a whole. Making it possible to cull and load in large portions of the terrain. In the image below we can see the different chunk boundaries of the terrain.
World Stacks – World stacks hold references to world tiles that exist in its coordinate space on the board. The world stacks directly communicate with the tile factory, requesting tiles of certain heights and types to be created based on the composition it has stored. The world stack also controls the terrain movement. The reason the world stacks are given control instead of the tiles is because if the tiles were shifting independently I would have to make additional checks to ensure one tile does not pass through another. Below are a series of images demonstrating stacks made of different compositions of tile types to create tunnels, caves, interesting structures.
World Tiles – World tiles are the physical component of the terrain. They are created by the tile factory and maintained primarily by the world stacks. World tiles can be given unique behaviours and methods of interaction. There is a link below to a post I made about the development of world tiles.
Optimisation – When developing the world manager and the structure of the terrain many issues were encountered. The initial problem was dealing with the high amount of game objects with rigid bodies. In order to combat this I modified the tile factory and the world manger system to create tiles of varying heights so parts of the composition defined in a world stack can be combined into a single game object. An example of this would be if I had a stacks composition array that consisted of the first 6 elements being stone, then 3 being grass, and then the rest being air. Initially I would define 9 tile game objects, 6 stone tile game objects and 3 grass tile game objects. What I now do instead is combine the 6 stone tiles into a single tile game object that is 6 times taller than normal, and the 3 grass tiles into a single tile game object that is 3 times taller than normal. This approach drastically improved performance, and allowed for levels that were up to 20 times larger.
Future work – Further development on the world manager still needs to be performed, as the terrain size is still too low. I currently have plans on a new world manager that dynamically build chunks from a loaded world file. The composition of tiles are stored as bytes, there are 32 bytes in a stack, and 256 stacks in a chunk. What this means is that if I had a world with a resolution of 128 x 128 chunks I would need 134217728 Bytes, or 134 mega Bytes, of memory to load in the entire composition of the terrain space. Once I have this composition loaded in I can start using it to dynamically construct world chunks in real time and dump world chunks that are out of player sight.
The code for the world manager system can be downloaded from the code listing page under the Hex World project files. These files are located within Assets >> Scripts >> World Tile Manager.