Class Car

java.lang.Object
  |
  +--Car
All Implemented Interfaces:
uwcse.animation.Prop

public class Car
extends java.lang.Object
implements uwcse.animation.Prop

This class models a car in our scene.


Constructor Summary
Car(City city, Road road, char direction, int width, int height, int speed, java.awt.Color color)
          Construct all the components of the Car given the Road it is on and the direction of travel.
 
Method Summary
 void addTo(uwcse.graphics.GWindow g)
          Add the elements of this display object to the graphics window.
 uwcse.graphics.Shape getBody()
          Return the Shape that is the Car's body
 char getCurrentDirection()
          Return the direction of travel of the Car
 Road getCurrentRoad()
          Return the Road that the Car is currently traveling along.
 int getHeight()
          Return the height of the Car's body
 int getSpeed()
          Return the speed of the Car in pixels per clock tick
 int getWidth()
          Return the width of the Car's body
 int getX()
          Return the upper-left x-coordinate of the Car's body
 int getY()
          Return the upper-left y-coordinate of the Car's body
 boolean hasCrashedInto(Car car)
          Determine whether the Car's avatar has crashed into another Car.
 boolean isOnRoad(Road road)
          Determine whether the Car's avatar is on the Road in question.
 boolean isTotaled()
          Check whether this Car has been totaled.
 void removeFromWindow()
          Remove the elements of this display object from the graphics window.
 void tick()
          Perform all Car actions necessary per clock tick, assuming the Car hasn't been totaled.
 void totalCar(Car otherCar)
          Total the car after an accident with another Car! Turn the body red, send it spinning out of control according to the direction and speed of the other Car, and set totaled to true.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Car

public Car(City city,
           Road road,
           char direction,
           int width,
           int height,
           int speed,
           java.awt.Color color)
Construct all the components of the Car given the Road it is on and the direction of travel. Inform the Road object that a new Car is traveling on it by increasing its Car count.

Parameters:
city - the City object that will need to be queried by the Car
road - the Road on which the car is to be traveling initially
direction - the initial cardinal direction of the car's travel ('N','E','S','W')
width - width of the car's Rectangle
height - height of the car's Rectangle
speed - number of pixels to move the Car per frame
color - color of the Car
Method Detail

addTo

public void addTo(uwcse.graphics.GWindow g)
Add the elements of this display object to the graphics window.

Specified by:
addTo in interface uwcse.animation.Prop
Parameters:
g - the graphics window to use

removeFromWindow

public void removeFromWindow()
Remove the elements of this display object from the graphics window.

Specified by:
removeFromWindow in interface uwcse.animation.Prop

tick

public void tick()
Perform all Car actions necessary per clock tick, assuming the Car hasn't been totaled. First, check if stopped at a TrafficLight. If so, and the lamp has turned green, get back up to speed and forget the light. If not stopped at a TrafficLight, check if the Car is nearing an intersection (i.e. whether the Car will enter the intersection during the next tick). If so, check the appropriate lamp on that TrafficLight (if there is a TrafficLight at the intersection), and if it's red, stop by setting the speed to 0. Finally, move the Car by 'speed' pixels.


getCurrentRoad

public Road getCurrentRoad()
Return the Road that the Car is currently traveling along.

Returns:
the Road object that the Car is currently traveling along

getSpeed

public int getSpeed()
Return the speed of the Car in pixels per clock tick

Returns:
the integer speed of the Car

getCurrentDirection

public char getCurrentDirection()
Return the direction of travel of the Car

Returns:
the character current direction of the Car

getX

public int getX()
Return the upper-left x-coordinate of the Car's body

Returns:
the integer x-coordinate of the avatar's upper-left corner

getY

public int getY()
Return the upper-left y-coordinate of the Car's body

Returns:
the integer y-coordinate of the avatar's upper-left corner

getWidth

public int getWidth()
Return the width of the Car's body

Returns:
the integer width of the avatar

getHeight

public int getHeight()
Return the height of the Car's body

Returns:
the integer height of the avatar

getBody

public uwcse.graphics.Shape getBody()
Return the Shape that is the Car's body

Returns:
the Shape constituting the bounding box of the Car's avatar

isOnRoad

public boolean isOnRoad(Road road)
Determine whether the Car's avatar is on the Road in question.

Parameters:
road - the Road being tested
Returns:
true if the Car is touching the surface of the road in question, false otherwise

hasCrashedInto

public boolean hasCrashedInto(Car car)
Determine whether the Car's avatar has crashed into another Car.

Parameters:
car - the Car object being tested for intersection
Returns:
true if this Car is touching the other Car, false otherwise

totalCar

public void totalCar(Car otherCar)
Total the car after an accident with another Car! Turn the body red, send it spinning out of control according to the direction and speed of the other Car, and set totaled to true. Leave any additional components of the Car (such as passengers, lights, etc.) where they were as a lesson to the programmer! Finally, notify the Car's currentRoad that one less Car is on it.

Parameters:
otherCar - Car with which this Car had its accident

isTotaled

public boolean isTotaled()
Check whether this Car has been totaled.

Returns:
true if the Car has been totaled, false otherwise