Game Development Reference
In-Depth Information
Line 03. The SaveGamePrepare event is called on the PlayerController just before
the game state is serialized. This is the opportunity the PlayerController needs to
confirm its current status to the LoadSaveManager. It does this by filling in the
Player transformation data, after retrieving a reference to the serializable Player
data object in the LoadSaveManager.GameStateData object.
Line 87. The LoadGameComplete event is a little more intricate. The function is
called automatically by the NotificationsManager just after game data has been
loaded from an XML file, and so it represents an opportunity for the Player to
restore its data from the serialized class back into the PlayerController object.
It starts by restoring the Player cash and the collected weapon. In the case of
the weapon, it also ensures that the weapon power-up is removed from the
level if the weapon is collected (as opposed to having the fists/punch weapon),
preventing the Player from collecting the gun weapon and its ammo twice. And
then, finally, it restores Player health and Player transformation data.
Completing the Enemy Class
Just as we needed to update the PlayerController class to respond to load-and-save game events,
we also need to update the Enemy base class, allowing all Enemies to load and save their data. The
newly added functions to the Enemy.cs class are listed in Listing 9-8; these support load and save
functionality. The full class listing (minus loading and saving) is shown in Chapter 7, if you want to
see it. Remember, all source code for CMOD is included in the project files, and the completed
CMOD Unity project is also included in the files for this chapter.
Listing 9-8. Completing the Enemy Class
01 //------------------------------------------------
02 //Function called when saving game
03 public void SaveGamePrepare(Component Sender)
04 {
05 //Create enemy data for this enemy
06 LoadSaveManager.GameStateData.DataEnemy ThisEnemy =
new LoadSaveManager.GameStateData.DataEnemy();
07
08 //Fill in data for current enemy
09 ThisEnemy.EnemyID = EnemyID;
10 ThisEnemy.Health = Health;
11 ThisEnemy.PosRotScale.X = ThisTransform.position.x;
12 ThisEnemy.PosRotScale.Y = ThisTransform.position.y;
13 ThisEnemy.PosRotScale.Z = ThisTransform.position.z;
14 ThisEnemy.PosRotScale.RotX = ThisTransform.localEulerAngles.x;
15 ThisEnemy.PosRotScale.RotY = ThisTransform.localEulerAngles.y;
16 ThisEnemy.PosRotScale.RotZ = ThisTransform.localEulerAngles.z;
17 ThisEnemy.PosRotScale.ScaleX = ThisTransform.localScale.x;
18 ThisEnemy.PosRotScale.ScaleY = ThisTransform.localScale.y;
19 ThisEnemy.PosRotScale.ScaleZ = ThisTransform.localScale.z;
20
 
Search WWH ::




Custom Search