We have a simple system that increases Score of Player as it touches Good Orbs and takes Lives from Player as it crashes into Bad Orbs. When the Score reaches a set number, we win. When Player Lives decreases to zero, we lose.
When we lose, we have the option to retry the Scene. To do this, the system needs to remember what the Score and Player Lives were when first entering the Scene so they can be reset back to these values.
The following two screenshots show the code for initializing the score:
The following steps occur while the game is in the setup State of the game.
An analysis of the code shown in the preceding two screenshots is as follows:
In the GameData
file:
Step 1:
From Chapter 9, Start Building a Game and Get the Basic Structure Running, we already have the variables for keeping track of the Score and Player Lives during gameplay:
playerLives
variable on line 16score
variable on line 18We need two more variables to remember the values stored in score
and playerLives
:
Line 12: private int playerLivesSelected = 2;
playerLivesSelected
variable stores the Player Lives selected in SetupState
playerLives
back to the value it was before a Scene is lost, or to reset playerLives
before Scene2 is startedLine 13: private int sceneBeginningScore;
sceneBeginningScore
variable stores the Score before a Scene is startedscore
back to the value it was before a Scene GameObject was lost, or to remember the Score earned in Scene1 before starting Scene2Step 2:
Line 22: playerLives = playerLivesSelected;
2
is assigned to playerLives
In the SetupState
file:
Step 3:
In the GameData
file:
Step 4:
Line 31: public void SetPlayerLives(int livesSelected)
livesSelected
Line 33: playerLivesSelected = livesSelected;
playerLivesSelected
playerLivesSelected
is remembered for the whole gameLine 34: playerLives = livesSelected;
The user has now switched from the setup State to playing the game in Scene1.
There are two possible play States in Scene1. For the scoring system, they both have the exact same code in their StateUpdate()
methods, so I will only explain the code in PlayStateScene1_1
.
The following screenshot shows the code for checking the Score to see if we win, and checking Player Lives to see if we lose:
An analysis of the code shown in the preceding screenshot is given in the following subsections:
In PlayStateS
cene1_1
file:
Step 5:
This step is executed if we lost in Scene1.
Line 32: if(manager.gameDataRef.playerLives <= 0)
Line 34: manager.SwitchState(new LostStateScene1(manager));
LostStateScene1
Line 36: player.rigidbody.isKinematic = true;
Line 37: player.transform.position = new Vector3(50, .5f, 40);
Line 35: manager.gameDataRef.ResetPlayer();
Step 6:
Line 25: public void ResetPlayer()
score
and playerLives
valuesLine 27: playerLives = playerLivesSelected;
playerLivesSelected
is restoring playerLives
Live 28: score = sceneBeginningScore;
SceneBeginningScore
had a value of 0 (zero) when Scene1 startedscore
back to 0The scoring system has been reset and Player repositioned, ready to replay Scene1
In PlayStateScene1_1
file:
Step 7:
This step is executed if we win in Scene1.
Line 40: if(manager.gameDataRef.score >= 2)
Line 43: player.rigidbody.isKinematic = true;
Line 44: player.transform.position = new Vector3(50, .5f, 40);
Line 42: manager.SwitchState(new WonStateScene1(manager));
In WonStateScene1
file:
Step 8:
Line 16: manager.gameDataRef.SetScore();
In GameData
file:
Step 9:
Line 37: public void SetScore()
WonStateScene1
line16Line 39: sceneBeginningScore = score;
score
variable has the value earned in Scene1 and is the starting point for Scene2We still need to discover the code that will increase the Score, or take Player Lives.
Player will be interacting with two simple GameObjects made using Unity's primitives, two flattened Cylinders and two Spheres.
Two versions are created, a bad GameObject with a red glowing material, and a good GameObject with a green glowing material.
18.218.239.182