One Game Per Day – Day7

For today’s game, I was looking to make some kind of 2D MOBA and I started to browse OpenGameArt.org to find some assets to base my game on. Instead, I found a really nice set of really low-pixel Hexagon tileset by Pixel32. I started to think of a couple ways I could use the sprites and decide to make a game with it. Here is the result of the first day.

Game 7 – God of HexCity (Day1)

God of HexCity

Procedural world generation with basic city placement and land growth.

Play God of HexCity!! – WebPlayer

(* World generation can take a couple seconds to fully generate. City growth is limited to 7 cells for this version)

Idea :

The idea for this game is to make a twist on the classics like Simcity, Civilization or Colonization(one of my all-time favorite). The objective is to have an automated simulation between different cities that would fight to prosper and grow bigger while fighting for resources and land. Everything should be as scientifically accurate as possible. I would like to generate map from temperature,wind,humidity model that would yield different type of land for different position on the map. Depending on the position of the player, different source of energy would be available such as wind or solar.

The player is a god that look at the evolution happening. He can make change happens on the world, remove land from a player or grow one city’s land. He can change water to land or desert to moutain. He can add wheat close to a city to save it from famine. He could also put cloud above a city that heavily rely on solar power. However, each modification has a cost. Player will need to  manage his mana and perhaps, research new skills before being able to use them.

I see different type of games. A sandbox where player could look at the world evolve and have total, free control over it. A campaign where the player would have to make sure that its city take over the whole map by helping it grow faster than its enemy. Another mode could be some kind of skirmish, where you have to research new technology and use mana, but without having to make a city win, for the pure pleasure of modifying their behavior.

Design :

Scripts

The principal scripts of my project are the following :

–  GameManger.cs : Responsible for the initialization of all system
–  GridManager.cs : Generate the grid, setup cells on creation and deal with the interaction with the cell and the mouse.
WorldGenerator.cs : Algorith that calculate the type of tile for each cell. This is where the Perlin algorithm is evaluated.
Cell.cs : Each cell on the screen have a cell. It contains the tile definition aswell as cell’s owner and eventually temperature,wind,humity…
Tile.cs : A tile is a type of land(i.e Desert,Deep Water) and is associated with a sprite for rendering. It also contains some info such as if walking on the cell is possible.
TimeSystem.cs : This was a script developped in Game3 – DigDeepInc and I imported it. It divide a day in 1440 minutes and allow to step more than one minute per frame for proper simulation.
Simulation.cs : This is the script that evaluate the action of each city at each minute.
City.cs : Information about the city of each player. It’s size, the number of villager, its current energy…

To make it short :

At start, GameManager Initialize the game. GridManager call WorldGenerator to obtain the map to draw. It then build the grid by instantiating each Cell and assigning it a Tile. Grid generation is made over many frames to show the player the advancement of generation.

Each frame, TimeSystem step over X minutes and call Simulation.TimeTick() every time. At the end of the frame, it call Simulation.FrameTick() to render the result of the simulation only once.

Procedural Generation

During this project, I learned about the wonders of Mathf.PerlinNoiseI didn’t know Unity had a built-in PerlinNoise that allowed such easy generation. It doesn’t give total control over the generation but it allow for a quick implementation which was perfect for me. I used this implementation from Answer Unity3d :

</pre>
int mapheight = 15:
int mapwidth = 30;

Vector2 shift = new Vector2(0,0); // play with this to shift map around
float zoom = 0.1; // play with this to zoom into the noise field

for(int x = 0; x < mapwidth; x++)
for(int y = 0; y < mapheight; y++)
{
Vector2 pos = zoom * (new Vector2(x,y)) + shift;
float noise = Mathf.PerlinNoise(pos.x, pos.y);
if (noise<0.5f) map[x,y] = 0; // water
else if (noise<0.6f) map[x,y] = 2; // sand
else if (noise<0.9f) map[x,y] = 1; // land
else map[x,y] = 3; // mountain
}
<pre>

By using random values for shift you move around and get different part of the perlin noise everytime. By using random values for zoom, you end up more or less bumpiness in the map and allow to change from let’s say, many small islands or a big land with some water around it. Here are some results that are ordered by increasing the zoom from left to right.

3 Different procedurally generated map. The zoom is increased from left to right.

3 Different procedurally generated map. The zoom is increased from left to right.

Next day of work:

Here is what I’d be interested in working on for the next day on this game :

  • Make a menu before map generation to allow customization of map from the player(Size, number of player on the map, bumpiness vs mountains…)
  • Now that procedural generation is working and the data structures are in place, I am ready to implement the simulation and make the city look for wood, foods, water. Make them grow in population/size, ect…
  • Give basic power to the player’s god like changing a tile, adding certain item on a tile
  • Refine procedural generation and optimize the grid generation for bigger map. Instead of using 1 cell = 1 sprite, I could create a chunk sprite with different tile mapped on it.
  • Start working on the save/load system. I will probably introduce a seed for regeneration of the world at load and only update change to the world after loading.

Conclusion :

Another really interesting project! I love procedural generation. It’s like AI, you can play your game and surprise yourself by the results! I can see this game going somewhere. The low-pixel tileset is really nice and give a particular look to the game. Thanks to Pixel32 for the amazing free arts!

Advertisements

3 thoughts on “One Game Per Day – Day7

  1. Wow, looks great! Any chance of you creating a more detailed tutorial or releasing the scripts as paid or free on the unity asset store? I’d pay good money for a nice map generator like this 🙂

    • Hi!

      A lot of the work is done by the amazing Grids plugin from Gamelogic. This include the tile generation and the hexagonal data structure for the cells type.
      The rest is mainly Math.PerlinNoise to generate a map that is used to draw the right sprite on each cell.

      I put the script and the tileset on my Github. It doesn’t include the Grids/NGUI script so I don’t think it is worth to give the whole project file. Without the grids plugin the code won’t work but you can have a look at the code-source and modify it as you want. If you buy Grids or use the 30 day trial and want the whole project file, let me know and I’ll try to give you a package that you could easily integrate with your plugin.

      Also, remember that it’s a 1-day rapid prototyping project. Code could be greatly improved and have better commenting but it give decent result. I spent a bit of time cleaning/commenting WorldGenerator.cs + GridManager.cs where you should look for the map generation. The post should give enough information to understand it.

      Let me know if you have any questions, I’ll be happy to help you!

      • Thanks for replying and for putting the files on GitHub. I’ll grab the 30 day trial of Grids, so I’d be grateful if you could package your project and send it to me, I’d love to take a closer look at it when it’s integrated into Unity. You can send it via e-mail to nikola.ffzg@gmail.com 🙂

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s