Game Development Reference
In-Depth Information
void Update() {
if (GameState != prevGameState)
{
ChangeState(gameState);
}
prevGameState = GameState;
}
10. The ChangeState() method checks the current game state (guaranteed
to have just changed this frame) as shown in the following code. We use a
switch statement to handle selection of conditional logic based on the value
of the new, current game state. Switch is a variation on the compound if/else
structure you may have seen before; when there are multiple options to se-
lect from, switch is regarded by many as being easier to read and maintain:
GameState = gs;
switch(gameState)
11. We don't need to do anything at this point for the case where we switch states
to MainMenu (since we only allow this state on start by default). Recall that
PopupMainMenu switches to LEVEL1 GameState on click. We handle this
by calling Application.LoadLevelAdditive("LEVEL1") . This line of
code loads the scene file by name (so long as it has been added to the build)
and adds all of the game objects from that scene to the current scene. The
net result of this will be a scene file with two game objects at the top level;
_global (and all of its children) and _level1 (and all of its children). Since
we never duplicate objects between scene files, we now have a complete
playable scene as shown in the following code:
case(eGameState.eGS_Level1):
{
Application.LoadLevelAdditive("LEVEL1");
break;
};
Search WWH ::




Custom Search