/** * AbstractVehicle.java * * @author Zuo Yan * @version 1.0 */ package vehicle; import java.awt.Graphics2D; import world.World; /** * An AbstractVehicle has the current World and a MovementTracker.
* * Any subclass must implement the createShapes(), update(), and draw() methods.
* The createShapes() method is called from this constructor. It should instantiate
* all necessary geometrical shapes associated with this vehicle and register it with the
* MovementTracker of this object.
* The draw() method is called whenever it's time to paint/repaint the screen. This can happen at any time.
* State changes should not occur in this method. Presumably, this method should just set the color of the different
* shapes created in createShapes() and call the necessary methods in the graphics module.
* The update() method is called by the world whenever it's time to update the state of the world.
* This occurs onces World.FPS times per second. Any changes to the state of this vehicle such as moving
* should occur in this method.
*/
public abstract class AbstractVehicle {
protected World theWorld; // The world we are in
protected MovementTracker mvTrack; // Movement tracker
/**
* Initialize variables for the vehicle.
*
* @param world the World this vehicle is in
*/
public AbstractVehicle(World ourWorld) {
this.theWorld = ourWorld;
mvTrack = new MovementTracker();
//Some advice: in each subclass, write a createShapes method
//call it in the subclass constructor:
//createShapes();
//such a method would create all the shapes needed by the vehicle.
}
/**
* Returns the current x position of the center point
* of all the shapes contained in this tracker.
*
* @return x-coordinate of the shapes' center
*/
public double getX(){
return mvTrack.getX();
}
/**
* Returns the current y position of the center point
* of all the shapes contained in this tracker.
*
* @return y-coordinate of the shapes' center
*/
public double getY(){
return mvTrack.getY();
}
/**
* Gets the orientation changed from the inital drawing
*
* @return the angle in radians
*/
public double getOrientation() {
return mvTrack.getOrientation();
}
/**
* Set the current orientation with postive sign meaning clockwise.
*
* @param theta the angle in radians
*/
public void setOrientation(double theta) {
mvTrack.setRotation(theta);
}
/**
* Rotate shapes by theta with postive sign meaning clockwise
*
* @param theta the angle in radians
*/
public void rotateBy(double theta) {
mvTrack.rotateBy(theta);
}
/**
* Move to (x, y) in the world.
*
* @param x the x-coordinate to move to
* @param y the y-coordinate to move to
*/
public void moveTo(double x, double y) {
mvTrack.moveTo(x, y);
}
/**
* Move by xoffset and yoffset pixels.
*
* @param xoffset the number of pixels to move in the x-axis direction
* @param yoffset the number of pixelse to move in the y-axis direction
*/
public void moveBy(double xoffset, double yoffset) {
mvTrack.moveBy(xoffset, yoffset);
}
/**
* Retrieves the MovementTracker.
*
* @return the MovementTracker associated with this vehicle
*/
public MovementTracker getTracker(){
return mvTrack;
}
/**
* Handles a collision with another vehicle.
* The default is to do nothing.
* Note: The method is not required.
*
* @param av the other vehicle
*/
public void handleCollision(AbstractVehicle av) {
//System.out.println("Collision occured at around: (" + getX() + ", " + getY() + ")");
}
/**
* Called by World when it's time to update.
* Any state changes to this vehicle should occur here.
*/
public abstract void update();
/**
* Use the graphics system to actually paint the shapes
* we have created.
*
* @param g the graphics system to use
*/
public abstract void draw(Graphics2D g);
}