Game Development Reference
In-Depth Information
Level
The level data is all processed in Level.cs. Right now, the level layout is hard-
coded in the TileData array that's in the LoadLevel function. This is defin-
itely not ideal, but it would have been an unnecessary complexity for this game to
actually load in the data from a file. The rest of the LoadLevel function basic-
ally loops through the array and places the tiles in the correct spots. Because the
tiles are hexagons, the calculations for the center position are a bit more complex
than they would be for square tiles. There is one other function here, called In-
tersects , that will be discussed when we cover the physics of the game.
Timer
The Timer class (in Utils/Timer.cs) is a utility class that allows for registration
of functions that get called after a period of time. Because each game object has
an instance of a Timer class, it becomes a handy way to have delayed behavior.
Rather than needing to add many floating point variables to track how much time
is remaining to call a particular function, you can simply add a timer, like so:
Click here to view code image
m_Timer.AddTimer("HideError", duration, ClearEr-
rorMessage, false );
Thefirstparameterforthe AddTimer functionisjustauniquenameforthetimer,
the second is how long until the timer fires, the third is the name of the function to
call, and the last parameter is whether or not the timer is looping. For example, it's
possible to set up a function to be called once per second using the Timer class.
Pathfinding
Pathfinding (in the appropriately named Pathfinder.cs) is implemented using the
A* algorithm (which is discussed in Chapter 9 ). Because enemies always spawn
from the same location, the pathfinding code does not recalculate the path for
every enemy. Instead, the Pathfinder has a global path that it saves as the op-
timal path for new enemies to take. This optimal path is shown in game as the
white line segments.
Intowerdefense,it'simportanttonotallowtheplayertofullyblockoffthepathof
theenemies.Iftherewerenopath,theenemieswouldjustgetstuckassoonasthey
spawned, and the game would not be challenging in the slightest. To solve this
problem, before a new tower is built, GameState checks with the Pathfind-
Search WWH ::




Custom Search