Game Development Reference
In-Depth Information
class can therefore be written as a subclass of the
SimpleProjectile
class. The
Powerboat
class
declares two fields: one named
mode
that determines whether the boat is accelerating, cruising
at constant speed, or slowing down; and the other named
planingSpeed
that stores the speed at
which the powerboat begins to plane.
public class Powerboat extends SimpleProjectile
{
private String mode;
private double planingSpeed;
The
Powerboat
constructor only consists of three lines of code. The first thing done is to call
the
SimpleProjectile
constructor to initialize the location and velocity components of the boat.
The
mode
and
planingSpeed
fields are initialized according to the value passed to the constructor.
// The Powerboat constructor calls the
// SimpleProjectile constructor and initializes
// the value of the mode variable.
public Powerboat(double x, double y, double z, double vx,
double vy, double vz, double time) {
super(x, y, z, vx, vy, vz, time);
mode = "accelerating"; // Accelerating, cruising, or
// decelerating
this.planingSpeed = planingSpeed;
}
The
Powerboat
class declares methods to return or change the value of the
mode
and
planingSpeed
fields and implements the
getLocationAndVelocity
method to update the
location and velocity of the boat.
// These methods access or change the value of the
// mode and planingSpeed fields.
public String getMode() {
return mode;
}
public void setMode(String value) {
mode = value;
}
public double getPlaningSpeed() {
return planingSpeed;
}
public void setPlaningSpeed(double value) {
planingSpeed = value;
}