• No results found

Changes to UI Controller

...

}

The preceding code is for the customization of the body type of the player character. The first thing it tries to do is to parse and convert the value passed to the function by the UI component. Next, it sets the selectedBodyType variable in the PC object. If for some reason, the value passed does not exist in the enumeration, we will assign the default value to the selectedBodyType variable. There is also debug statements to give you feedback about the current value.

Changes to UI Controller

The UI Controller will also need to be updated now to make the necessary changes to the GameMaster object. We would need to update the LoadLevel() function to the following:

public void LoadLevel()

This will make sure that GameMaster is updated with the proper player character data.

Let's go ahead and test the code.

Testing

Starting from the Main Menu scene, make sure that you have the following GameObjects in the scene: uiController and _GameMaster. The uiController game object should have UIController.cs attached and _GameMaster should have the following components attached: GameMaster.cs and an AudioSource component that will be used for the background music.

Have the _GameMaster GameObject selected in the Hierarchy Window; run the game.

Select the Start Game button. This will load the character customization scene. The _GameMaster GameObject should still be selected, if not, go ahead and select it from the Hierarchy Window, do some of the character customization and click on the Save button.

The first level should have been loaded with your character and the customization you have made to your character in the previous step. So visually, your character has retained all of the customization you have done, and from a data point of view, when you look at the _GameMaster GameObject in the Inspector Window, you will notice that the data has been saved properly as shown in the preceding figure.

Summary

Chapter 5 was mostly code. We enhanced the GameMaster Class to handle the game settings and scene management. We began the chapter by making the GameMaster handle the user interface, the player character data, and the game settings, which currently is just the volume for the background music.

We added a new UI element that displays the settings panel for the game. At the moment, it only contains the main volume control. Next, we added the necessary code in the

UIController class and the GameMaster class to handle the display of the settings window and also the slider value passed from the UI component to the UIController to the GameMaster class.

We also made the GameMaster class into a singleton. A singleton in software engineering is a design pattern that restricts the instantiation of a class to one object. This pattern fits perfectly for the GameMaster as we only need to have one instance of it active at any given time throughout the lifespan of the game.

We also looked how to perform scene management. We defined a static class named SceneName that contains constant string variables identifying the scene references in our game.

We then took the next step to improve our GameMaster and the internal structure for our code. We created a new class called LevelController.cs that handles the scene

management who in turn is driven by the GameMaster. We practically took the logic for level handling from within the GameMaster class and reworked and improved it in the LevelController class.

Next, we developed an AudioController class that basically manages the audio for our game. This class also is driven by the GameMaster. By this time, our GameMaster is a lean script that manages all of the other components.

The next big challenge was how to handle the player character data. Specifically speaking, how to save the character customization data for the player character internally after the player had customized the character. In order to save the data, we had to modify the PC.cs class.

We created several enumerations representing each part of the character that could be customized, such as the shoulder pad, the body type, the weapon type, the helmet type and so on. We used enumeration to make it easier to reference them within the code.

This approached forced us to make some modifications to the existing character

customization setup that we have had implemented previously. So we had to update the UI components, to reflect the enumeration defined for each customizable type and we also had to modify the CharacterCustomization.cs class to handle the new changes.

The CharacterCustomization class implemented a PC type variable to keep track of the customizations and finally pass the data along to the GameMaster. During the process, we also improved the case handling of the CharacterCustomization class for default values and so on.

Finally, we had a test run of the game to double check that everything worked as designed and implemented.

We created a lot of code in this chapter. In the next chapter, we are going to start building our inventory system, and yes, that is going to involve more code!