Last time I talked about creating the mesh in Unity, and all the troubles that came with it. The next part was actually putting in into the game. Previously the game world was just a flat green plane with all the different cities and locations scattered around the players city. These weren’t spawned in or anything, they just existed in the scene already.
So what I needed to do was take the terrain generator, find a random suitable spot to place the player’s city, place random cities and locations around the maps in suitable locations, along with implementing the discovery system.
I had already started the discovery system in code, I stored the chunks that player had already discovered, at the beginning there is only one location you have, the players location. That spot is found by looking through all the chunks, and finding the most preferred location looking to make sure that chunk wasn’t an ocean or a mountain (No floating cities for now!) and that it’s neighbors had some good suitable locations surrounding it. Once that was done, I had the game discover about 3 – 4 locations surrounding the player, so that when you start you’ll at least have a slight idea about the terrain around you.
Now we had to fill the world with locations, yet the current locations were all already existing in the world, predetermined. So I had to change the way the system was actually working. I got rid of all the existing locations, and decided to try a Unity feature I hadn’t used before: ScriptableObjects.
In Unity a ScriptableObject can be used as a sort of template. I made a class, and gave it values that locations would need, such as potential jobs in that area, rewards, the name of the location, etc. Then I could essentially create instances of that class and have individual templates for different areas.
Now I could pass these into my world creation script, and it would choose between the types randomly. Do we need to spawn a city? Lets find a ScriptableObject that has city information randomly and spawn that in the world. Do we need a random location? Well, we look at the biome for that too, so if we’re in a forest, we’re much more likely to spawn a lumber mill, if we’re next to a mountain, we have a high chance to spawn a mine.
Right now these repeat fairly often, but as we add more and more, the world will feel more diverse with lots of different locations for different needs. Undiscovered locations just show up as spheres with question marks on them for now, if you send employees to that area then that area becomes discovered and you can see the location. In the future the plan is to replace that with some kinda of fog of war shader, similar to Civilization or NorthGuard.
1 comment on “TC – Terrain Generation The Game”Add yours →