COSC 3P91

Assignment 4

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


RoboRace (Part 4)

In this assignment you are going to implement parts of the animated GUI for RoboRace.

CardPane

The lower left part of the application's JFrame is occupied by a CardPane (package roborace.client), a subclass of JPanel. This panel relies on the paint-repaint mechanism. Implement the class as follows:
  1. Implement the method paintComponent(Graphics g) so that the card list cards is displayed properly in any possible situation, i.e., cards has up to 7 cards and some of them selected. Each image of a card is 92 pixels wide. The cards that have been selected already are stored in the list selectedCards. Use the method indexOf(card) to obtain the index when the card was selected (returns -1 if the card was not selected). Use this index to draw the roman numeral on top of the card. The images are stored in the array selectImages. The images of the roman numerals should be offset by 30,50 on top of the card.
  2. The method selectCards(CardList list) is called by the player thread to initialize the selection process. In this method do the following:
    1. Store the list parameter in the variable cards.
    2. Modify the boolean flag selecting to indicate that the player can now select cards.
    3. Enable the ok button.
    4. Let the player thread wait until the selection process is completed.
    5. Once the player thread is notified return the list cards.
  3. The method stopSelection() is called by the AWT thread when the ok button is pressed. Here do the following:
    1. Check that 5 cards have been selected. If not, use a game dialog to make the player aware that (s)he has to select 5 cards.
    2. Otherwise, the cards become the selectedCards, disable the ok button, modify the boolean variable selecting and notfy the player thread.
  4. The following diagram visualizes the synchronization of the player and the AWT thread:
  5. The method mousePressed(MouseEvent e) has to be implemented. It is called when the player clicks on the panel. Compute which card was clicked and then add or remove the card from the selectedCards depending whether it was already selected or not. Make sure that the player cannot select more than 5 cards.
Remark: Make sure that you call repaint() whenever the content has changed.

BoardCanvas

The upper left part of the JFrame is occupied by a threaded BoardCanvas (package roborace.client). Implement the animation loop from class in the run() of this class. Use the methods update(long elapsedTime) and draw(Graphics graphics) of the animated board board.

RobotSprite

The class RobotSprite (package roborace.client) represents the robot sprite moving around the factory hall. It is implemented as a finite state machine using the states Idle, Turning, Moving, Explode, Dead. Implement the following methods:
  1. The methods destroyed(), turn90(boolean clockwise), turn180(), step(Direction direction), bump(Direction direction) are called by the player thread in order to initiate the corresponding action in the robot sprite. All methods work similar as follows:
    1. Stop the currentAnimation
    2. Select and start the appropriate new animation (use the private methods getExplodeAnimation(direction), getTurn90Animation(direction,clockwise), getTurn180Animation(direction), getMoveAnimation(this.direction).
    3. In stepping or bumping add way points to the variable wayPoints (use the private methods Point getMoveWayPoint(Direction direction), List getBumpWayPoints(Direction direction)).
    4. Change the direction of the robot in the 2 turn methods.
    5. Set the variable state appropriately, i.e., set the state of the finite state machine correctly.
  2. The method update(long elapsedTime) will update the sprite depending on its state. In all cases it will first update the currentAnimation:
    1. state==Explode: If the animation is not running any more, the animation is reset and the currentAnimation becomes the dead animation (use the private method getDeadAnimation(direction)). The robot is now dead and the player thread is notified.
    2. state==Turning: Do the same as above except that the robot is idle afterwards (use the private method getIdleAnimation(direction)).
    3. state==Moving: Compute the distance of the current position (variables x, y) to the first way point. If this distance is less or equal to SPEED*elapsedTime, then move the robot to the first way point and remove it from the list of way points. If there are no more way points left, then go to the idle state as above. If the distance is not less or equal to SPEED*elapsedTime, then move the robot towards the way point with an overall speed of SPEED.

Sources

  1. You find a project folder to start here.

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