Game Development Reference
In-Depth Information
//Check enemy health
//Are we dead?
if(Health <= 0)
{
//Then perform die behaviour
Die();
return;
}
//Check for health low
if(health <= 20)
{
//Health is low, so find first-aid
RunAndFindHealthRestore();
return;
}
//Check ammo
//Have we run out of ammo?
if(Ammo <= 0)
{
//Then find more
SearchMore();
return;
}
//Health and ammo are fine. Can we see player? If so, shoot
if(HaveLineOfSight)
{
FireAtPlayer();
}
}
The preceding code sample 4-1 shows a heavy Update function filled with lots of
condition checking and responses. In essence, the Update function attempts to merge
event handling and response behaviors into one and the results in an unnecessarily
expensive process. If we think about the event connections between these different
processes (the health and ammo check), we see how the code could be refactored
more neatly. For example, ammo only changes on two occasions: when a weapon
is fired or when new ammo is collected. Similarly, health only changes on two
occasions: when an enemy is successfully attacked by the player or when an enemy
collects a first-aid kit. In the first case, there is a reduction, and in the latter case,
an increase.
 
Search WWH ::




Custom Search