Dana Vrajitoru
B583/C490 Game Programming and Design

B583/C490/I400 Lab 6

Due Date: Wednesday, October 5, 2022, as part of the homework.
Game Description

In this lab we will continue the 2D scroller game and we will implement a second level. Here is an example of the intended result (http://www.cs.iusb.edu/~danav/teach/b583/shooterGL/) or just level 2 (http://www.cs.iusb.edu/~danav/teach/b583/shooterL2/).

Preparation

Download the following new resources:
rock.png
bullet.png
rock.mp3
break.mp3
fire.mp3

Add an info text displaying some brief instructions at the beginning of the game. Make the GameMaster turn that text to an empty string in the function Update when a key was pressed and the game is not over yet. You can use Input.anyKey (not a function) for this.

Level

Add a public int level attribute to the class GameMaster and set its value in the scene as 1. Change the name of the scene to Level1.

Add the following function to GameMaster:

void StartLevel() 
{
    if (level == 1) 
    {
    }
}

Add a similar one called CheckEndLevel.

Move the code from the function Start that will work differently in the two levels into the block of the case level==1 in StartLevel. Specifically: initializing the arrays friends and foes, initializing the score and its display, and setting the info text. Call the function StartLevel in the function Start.

In the function Update, identify the end level test specific to level 1. That is probably checking that the score is 0. Move that into the function CheckEndLevel inside the case where level==1.

Level 2 description

You are in control of a rocket that you can move in four directions. You float in space encountering parachutes and rocks. You must catch 7 parachutes but avoid rocks altogether. To help with the navigation, you can shoot bullets at the rocks.

New scene

Save the scene Level1, then do Save Scene As and give it the name Level2. Open the new scene.

Set the level of the GameMaster in the new scene as 2. Add a rock sprite to the scene, resize it to a reasonable size. Add a Rigidbody2D to it with the gravity scale 0.1 and a polygon collider. Add an Audio Source component to it and attach the sound "rock" to its Audio Clip. Add the CritterControl script component to it, set it as not a friend with a score value of 10. Then convert it to a prefab and delete it from the play area. Then click on the GM object and drop the rock prefab onto its Foe Ref property.

Build Settings

Before we can load the second level from the first one in case of winning conditions, we need to add the scenes to the build. Open the scene Level1 again. The from the File menu click on Build Settings and click on Add Current (right side, below the top empty box). Then open the scene Level2 again and proceed the same way. You should see the two scenes listed in the build with Level1 at position 0 and Level2 at position 1.

Go back to the first scene. Then in the GameMaster class, let's assume that you have a bool attribute called gameOver, initialized as false. In the function Update, when the level is won because the player has achieved a score of 0, set gameOver to true and set the info text as "Level complete". Then add a test for gameOver being true (in Update but outside of the test for the score being 0) where you check if the game is over and the player has pressed any key, just like before. First, import the appropriate module:

using UnityEngine.SceneManagement;

Then if the user pressed a key after the game is over, do the following:

SceneManager.LoadScene("Level2");

This way when the player has won the level, we display the information about it and then wait for them to press any key to start the new one.

Now you can test this new functionality.

Resources Load

We want the player to be able to play two different sounds: the break sound when it collides with a rock and the fire sound when it fires the gun. In the Level2 scene, add an Audio Source component to the player and set its clip as "break". Then create a folder called Resources under Assets and move the fire clip into it.

Then add the following attribute to the PlayerControl class:

AudioSource playerSound;
AudioClip breakClip, fireClip;

Then in the function Start, add the following code:

playerSound = GetComponent<AudioSource>();
if (playerSound)
{
    breakClip = playerSound.clip;
    fireClip = Resources.Load("fire") as AudioClip;
}
Then add the func Fire with the following code:
void Fire()
{
    playerSound.clip = fireClip;
    playerSound.Play();
}

Finally, add another KeyCode attribute to the class for the fire action. Then in Unity set the value of this key as Space. Add a case of the conditional for it in the function Update and call the function Fire. Use the function GetKeyUp to trigger the Fire call instead of GetKey to avoid firing many bullets at once. Now you can test the firing sound.

Make sure that the added code has not broken the functionality of Level1.

This will be continued in the homework.