The Game – Forest Terrain

Summary

In my last blog post on the game I demonstrated how to setup blocked terrain cells (aka mountains). Now I’m going to introduce a forest cell that will block tanks but not infantry.

The Forest Terrain Type

I wanted to introduce a forest terrain type so that I could prevent tanks from penetrating (assume this is a forest of very large trees), but allow infantry to penetrate. First, I used the same Google Maps screen shot technique to create a forest hex cell:

Then I setup a test map to test my shortest path algorithm:

Modifying the Code

In my previous code, I created a property to handle the blocked terrain. This was located inside the GameMap object. In order to test both the terrain and unit combinations I had to change this into a method that uses the unit type integer as a parameter. The forest terrain number is 7 and a tank is unit type 2. So I setup the Blocked() method to check for this combination and return true (as in, terrain will block this unit type). Eventually, I’ll need to extend this simplistic method to include an array of terrain types verses unit types. Before I do that, I’ll probably introduce a technique of slowing down the progress of a unit. For this instance I’ll make the unit travel slower through the forest. For now though, I’m going to just provide a flag for blocked or not blocked.

Now that the Blocked property has been converted to a method, any calling methods need to be re-factored to pass the unit type. Then I tested to see if the tank went around the wall of forest and the unit went through the forest.

Adding Unit Tests

I ended up adding the unit tests after I made this work. Technically, I could have added them first and used the unit tests in order to build the code. The purpose of adding unit tests after the fact is that I want to make sure that future changes don’t break this feature. So you’ll see two new unit tests that will test an infantry unit and a tank unit path calculation for the sample map above.

Getting the Code

You can go to my GitHub account and download the zipped up version of this project (Click Here).

Leave a Reply