Dana Vrajitoru
B583/C490/I400 Game Programming and Design

B583/C490/I400 Homework 5

Due Date: Wednesday, September 28, 2022.

In this homework we will start writing a 2D scroller game. Here is an example (http://www.cs.iusb.edu/~danav/teach/b583/moon_shot.swf) of the intended result.

Ex. 1. Complete Lab 5.

Ex. 2

Continue the implementation of this game by adding the following features:

a. Add a feature to the PlayerControl class that also restricts the movement of the ship horizontally. Specifically, make the horizontal space circular such that if the ship exits from the left, it re-enters from the right and the other way around.

b. In the function Start in the class CritterControl , randomize the scale of the object in the range [0.75, 1.25] and apply it both to the horizontal and to the vertical scale at the same time so that the object doesn't look deformed. Hint: The scale attribute can be accessed from the code as transform.localScale. In lab 4 we've seen a real range function from the class Random.

Add a function Respawn and move all the code randomizing the position and the scale of the object from Start into it. Reset its velocity to 0 in this function. Then in the function Start, call this function. This will allow us to reuse the same object.

c. Scoring

Start the score at 50.

Add a collision detection callback function to the class CritterControl that starts this way:

// collision detection function
void OnCollisionEnter2D(Collision2D colInfo) 
{
    if (colInfo.collider.tag == "Player") 
        Debug.Log("collision");
}

In the collider function, when a critter collides with a Player object, play the sound attached to it. Remove that function call (playing the sound) from the function Start. We only want it to be played in case of collision.

Then check if the critter is a friend or not. If it is a friend, then add its score value to PlayerControl.score. If it isn't, then subtract the score value from the same.

After updating the score, check if the critter is set to respawn. If it is, then call the function Respawn. If it isn't, then deactivate it.

Add a score displaying object in a font of your choice and update its content every time the score is changed.

d. Game Master

Add an empty object called GM and attach a new script to it called GameMaster. This object will create and manage a number of critters. These will be created dynamically in the code.

Add two public attributes of type GameObject called friendRef and foeRef. Drop the parachute prefab on the friendRef and the helicopter on the foeRef. Then create two arrays of game objects with a size of 5 each containing friends and foes. Allocate the arrays and instantiate all the objects in the function Start. For that, you need a loop, let's say with a control variable i, where you can create a friend object with

friends[i] = Instantiate (friendRef) as GameObject;

and then similarly for the foes. This expression is used in place of a constructor.

Then adjust the vertical range in the function Respawn in the class CritterControl to avoid having the objects all fall from the sky at the same time.

Add an empty object and add a box collider 2D component to it. Resize it so that it's wider than the camera and place it somewhere below the camera area. Set its tag to "Ground". Then add another case in the collision detection of critters where in case they collide with the ground they respawn.

Add an object containing the moon sprite somewhere on the screen. Set its size to be fairly big. Deactivate it at the beginning of the game. Then in the function Update in the class GameMaster, check for the score being 0, and if it is, then deactivate all the friends and foes and activate the moon object. This will signify that the game is won.

Homework Submission

Turn in: the source files .cs and a Windows build.