Dana Vrajitoru
B583/C490 Game Programming and Design

B583/C490 Homework 4

Due Date: Wednesday, September 21, 2022.

In this homework we'll continue Lab 4 and finish the game of Skittles.

Here is an example of the finished game (http://www.cs.iusb.edu/~danav/teach/b583/skitGL/).

Ex. 1. Lab. After you complete the lab, you should have an area delimited by walls, a spinner, and 9 pegs placed in the arena. The spinner is rolled in a randomized way when the game starts.

Ex. 2. Add the following elements to the game.

a. Peg Count.
Add three attributes in the class SpinnerManager, a non-static public int pegCount, initialized as 9 in the function Start, a non-static public int moveNumber, initialized as 0 in the function Start, and a public static int bestScore, initialized as 100 in the declaration (not in the function Start).

Remove the function call to RollSpin from the function Start and move it to the function Update, under a check for keyboard input from the user with the character being a space. Increment the moveNumber in RollSpin. This way, by hitting a space we roll the spinner again.

Make the displayed text show "Moves: ## Best: ##" with the current number of moves and the value of the best score. You will need to use the reference to the text object to do this.

b. Collisions.
Add the following function to the class SpinManager:

 
void OnCollisionEnter(Collision colInfo)
{
    if (colInfo.collider.tag == "peg" && colInfo.collider.GetComponent<PegManager>().standing)
    {
        pegCount--;
        colInfo.collider.GetComponent<PegManager>().standing = false;
    }
}

Now when you run the game, you should notice that when the spinner hits a peg the first time, it decreases its peg count.

Add a "Player" tag to the spinner. Add a similar collision function to the PegManager class that plays its sound when it collides with an object having the Player tag.

c. Winning the game.
Modify the collision function in the class SpinManager so that after a collision, if the peg number is down to 0, the game is won. In that case, compare the number of moves with the best score. If the number of moves is lower, replace the best score with the new one, and display a message saying that there is a new best score. If not, simply display a winning message.

d. New Game.
Add the following line to the top of the SpinnerManager file (before the class definition):

using UnityEngine.SceneManagement;

Add the following function to this class:

public void NewGame()
{
    SceneManager.LoadScene("skittles", LoadSceneMode.Single);
}
Make sure that the name of the scene is exactly what you gave it.

Create button in the scene and call it newGame. In the Hierarchy it will appear under a Canvas object. Set the colors of the button however you like. Then click the arrow next to the button in the hierarchy to show a Text child. Here you can change the label of the button to "New Game".

Back to the button object itself, in the Rectangle Transformation at the top in the inspector, set its coordinates to 0, 0, 0. Now the button should show in the middle of the scene. Change the position of the button in small increments until it shows where you want on the scene when played. If you find a better method for this, let me know. The values (-300, 140, 0) for the position with the width reduced to 120 work well for me.

Then below the Button (Script) component in the inspector, you will see a section called On Click() which should show empty. Click on the + and then under Runtime Only, click on the small circle and select the spinner object from the Scene tab. Then next to it, click on the No Function and choose the SpinnerManager component, and from there, the NewGame function.

Add an extra trigger for the new game on pressing the key N in case the button doesn't show up properly on the screen in the build (it happens sometime).

Extra credit. Up to 3 points for creative additions to the game functionality.

Turn in: all the source files .cs and a zipped build folder for Windows or WebGL.