Keeping score during the game

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:

Keeping score during the game
Keeping score during the game

Initializing the scoring system

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:

  • The playerLives variable on line 16
  • The score variable on line 18

    We need two more variables to remember the values stored in score and playerLives:

    Line 12: private int playerLivesSelected = 2;

  • The playerLivesSelected variable stores the Player Lives selected in SetupState
  • This resets playerLives back to the value it was before a Scene is lost, or to reset playerLives before Scene2 is started

Line 13: private int sceneBeginningScore;

  • The sceneBeginningScore variable stores the Score before a Scene is started
  • This is used to reset score back to the value it was before a Scene GameObject was lost, or to remember the Score earned in Scene1 before starting Scene2

Step 2:

Line 22: playerLives = playerLivesSelected;

  • When the game is started, the default value of 2 is assigned to playerLives

In the SetupState file:

Step 3:

  • The user has the option to select more Player Lives
  • Dot Syntax is used to call the SetPlayer() method in GameData
  • The SetPlayer() method takes an argument of 5, 10, or 1000 to pass to GameData in line 31

In the GameData file:

Step 4:

Line 31: public void SetPlayerLives(int livesSelected)

  • The value received is assigned to the parameter variable livesSelected

Line 33: playerLivesSelected = livesSelected;

  • The value is assigned to playerLivesSelected
  • The value now in playerLivesSelected is remembered for the whole game

Line 34: playerLives = livesSelected;

  • The livesSelected variable is also assigned to playerLives to be displayed on screen

Keeping score in the Scene1 play State

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:

Keeping score in the Scene1 play State

An analysis of the code shown in the preceding screenshot is given in the following subsections:

Losing the game in Scene1

In PlayStateS cene1_1 file:

Step 5:

This step is executed if we lost in Scene1.

Line 32: if(manager.gameDataRef.playerLives <= 0)

  • This if statement checks to see if playerLives has decreased to zero

Line 34: manager.SwitchState(new LostStateScene1(manager));

  • Since we lost, the State Machine switches to LostStateScene1

Line 36: player.rigidbody.isKinematic = true;

  • The Is Kinematic option is checked to stop Player from being affected by physics

Line 37: player.transform.position = new Vector3(50, .5f, 40);

  • The Player GameObject is repositioned back to the beginning position for Scene1 to be replayed

Line 35: manager.gameDataRef.ResetPlayer();

  • To replay Scene1, the score and playerLives variables need to be reset to the values they were when first starting Scene1
  • The ResetPlayer() method in GameData is called in line 25

In GameData file:

Step 6:

Line 25: public void ResetPlayer()

  • This resets the score and playerLives values

Line 27: playerLives = playerLivesSelected;

  • playerLivesSelected is restoring playerLives

Live 28: score = sceneBeginningScore;

  • SceneBeginningScore had a value of 0 (zero) when Scene1 started
  • This is resetting score back to 0

The scoring system has been reset and Player repositioned, ready to replay Scene1

Winning the level in Scene1

In PlayStateScene1_1 file:

Step 7:

This step is executed if we win in Scene1.

Line 40: if(manager.gameDataRef.score >= 2)

  • This if statement checks to see if score has increased to 2

Line 43: player.rigidbody.isKinematic = true;

  • The Is Kinematic option is checked to stop Player from being affected by physics

Line 44: player.transform.position = new Vector3(50, .5f, 40);

  • The Player GameObject is positioned for Scene2

Line 42: manager.SwitchState(new WonStateScene1(manager));

  • Since we won, the State Machine switches to WonStateScene1:
    Winning the level in Scene1

In WonStateScene1 file:

Step 8:

Line 16: manager.gameDataRef.SetScore();

  • This is in the constructor, so as soon as the State Machine enters WonStateScene1, the SetScore() method in GameData is called

In GameData file:

Step 9:

Line 37: public void SetScore()

  • This method is called from WonStateScene1 line16

Line 39: sceneBeginningScore = score;

  • The score variable has the value earned in Scene1 and is the starting point for Scene2

Determining how to win or lose

We 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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.218.239.182