COSC 3P91

Assignment 5

Instructor: Michael Winter, Office J323, email: mwinter@brocku.ca


RoboRace (Part 5)

In this assignment we are going to add some sounds and music to the program, and our game will go online.

Music

At the beginning of the run() method of the Player class use the midiManager to play the music. Use the methods getSequence(fileName) and play(sequence,true) so that the music loops indefinitely. You should use the midi file animaniacs.mid. Make sure that the midi path is set by a call of setMidiPath("./Sounds&Midi/") for the midiManager before loading the file.

RoboRaceSoundManager

Implement the class RoboRaceSoundManager (package roborace.client) as follows:
  1. Make the class RoboRaceSoundManager a subclass of SoundManager (already done).
  2. In the constructor first call the super constructor with the following audio format new AudioFormat(8000,8,1,false,false) and 4 as the number of pooled threads. Then call super.setSoundPath("./Sounds&Midi/") in order to set the sound path (already done).
  3. In the constructor load all 7 game sounds (bump.wav, explosion.wav, fanfare.wav, drivingShort.wav, drivingLong.wav, pusher.wav, crusher.wav) into local variables so that they are loaded only once.
  4. Use the singleton design pattern to ensure that there is only one, globally accessible, instance of the sound manager.
  5. For each of the 7 sounds implement a method that plays the sound. In this method make sure that the sound is only played if it was not already started less than 400ms earlier, e.g., if there are two calls to play the bump sound within 400ms, then the second call is ignored and the sound is only played once. If more than 400ms have elapsed, then the bump sound is played twice.

Sounds

Use the RoboRaceSoundManager to play sound as follows:
  1. Play the crusher sound in the start() method of the class DecorationCrusherAnimation.
  2. Play the pusher sound in the start() method of the class DecorationPusherAnimation.
  3. Play the fanfare sound in the execute() method of the class EventVictory.
  4. Play the driving (short) sound in the step() and turn90() methods of the class RobotSprite.
  5. Play the driving (long) in the turn180() method of the class RobotSprite.
  6. Play the explosion sound in the destroyed() method of the class RobotSprite.
  7. Play the bump sound in the bump() method of the class RobotSprite.

NetworkPort

Implement the class NetworkPort (package roborace.common) as follows:
  1. The two streams of the socket that are passed in the constructor should be decorated by an InpuStreamReader and BufferedReader resp. a PrintWriter in order to allow reading and writing of whole lines of text.
  2. In sending a message use the character '\0' to indicate the end of a message. Print this character in its own line so that detecting the end of a message it becomes easier.
  3. In the method receive() read the message line by line until the line containing only the character '\0' is detected. The character '\0' should not be part the result.

The server (RoboRaceServer)

In the main class of the server (RoboRaceServer) do the following:
  1. Asks for the number nHuman of players using a game dialog (already implemented).
  2. Set up a server socket using PORT and accept nHuman clients. For each client create a NetworkPort, receive the player's name from the client, and send the playerID to the client.
  3. Create a GameMaster using the network ports and the names of the player.
  4. Run the GameMaster, i.e., call the run() method. There is no need to create a thread.

The client (RoboRaceClient)

In the main class of the client application (RoboRaceClient) do the following:
  1. Ask the player for a name using a game dialog (already implemented).
  2. Connect to the server using HOST and PORT.
  3. Create a NetworkPort.
  4. Send the name to the server and receive the playerID from the server.
  5. Create a Player using the network port and the playerID.
  6. Run the Player, i.e., call the run() method. There is no need to create a thread.
  7. Last but not least, uncomment the Line 26 so that the RoboRaceSoundManager gets instantiated early.

Remarks

  1. Use the static variables HOST and PORT to establish a connection.
  2. The GameMaster and the Player class do not need to be changed except adding the music as outlined above.
  3. You find a project folder to start here.

COSC Home Page
COSC 3P91 Home Page
© M. Winter, 2023