Game Development Reference
In-Depth Information
Listing 10-32. Updating the GamePlay Controller
void UpdateController(long CurrentTime)
{
UpdateArenaObjects(CurrentTime);
UpdateTanks(CurrentTime);
}
Saving and Loading the Game State
In order to save the state of the game and restore this state, new code has to be added to the
MainActivity class and the MyGLRenderer class.
Modifying the MainActivity Class
For the MainActivity class, new code is added to the
onPause()
function that calls the
SaveGameState()
function in the MyGLRenderer class when the Android game is paused. (See Listing 10-33.)
Listing 10-33. Modifying the
onPause()
Function
@Override
protected void onPause()
{
super.onPause();
m_GLView.onPause();
// Save State
m_GLView.CustomGLRenderer.SaveGameState(SAVE_GAME_HANDLE);
}
Modifying the MyGLRenderer Class
The MyGLRenderer class has to be modified by adding functions that save and load in the game state.
The
SaveGameState()
function saves the state by saving key game variables if the game play is
currently active (
GameState.ActiveGamePlay
). (See Listing 10-34.)
The key elements of the game that are saved are the
1.
Player's score
2.
Player's health
The
m_CanContinue
variable, which is true if there is a previously saved game
to load and then to continue from
3.
4.
Camera's state
5.
Arena objects
6.
Tank objects