aboutsummaryrefslogtreecommitdiff
path: root/Week14 Route 66/src/OO14route66/Car.java
diff options
context:
space:
mode:
Diffstat (limited to 'Week14 Route 66/src/OO14route66/Car.java')
-rw-r--r--Week14 Route 66/src/OO14route66/Car.java77
1 files changed, 49 insertions, 28 deletions
diff --git a/Week14 Route 66/src/OO14route66/Car.java b/Week14 Route 66/src/OO14route66/Car.java
index 6ebb863..0058d8d 100644
--- a/Week14 Route 66/src/OO14route66/Car.java
+++ b/Week14 Route 66/src/OO14route66/Car.java
@@ -1,38 +1,43 @@
package OO14route66;
import com.camilstaps.route66.Driver;
+import com.camilstaps.route66.Overviewer;
import java.awt.Color;
import java.awt.Graphics;
import java.util.Random;
/**
- * OO1route66 initial class
- * @author Pieter Koopman
- *
* Class for an individual car
+ *
+ * @author Pieter Koopman, Camil Staps
*/
-public class Car
-{
+public class Car {
public static final int
- CARWIDTH = 20,
- CARLENGTH = 40,
- MINCARSPACE = 5,
- MINCARSPEED = 2,
- MAXCARSPEED = 4;
- public static final Color[] carColors={Color.red, Color.blue, Color.black, Color.gray, Color.green,
+ CARWIDTH = 20,
+ CARLENGTH = 40,
+ MINCARSPACE = 5,
+ MINCARSPEED = 2,
+ MAXCARSPEED = 4;
+
+ public static final Color[] carColours = {Color.red, Color.blue, Color.black, Color.gray, Color.green,
Color.pink, Color.orange, Color.magenta, Color.lightGray};
private final int speed, number; // speed and car-number
private final Direction direction; // driving direction, used by paint
private int location; // current location on the road
- private final Color color; // color of this car
+ private final Color colour; // colour of this car
private final Random random; // here to ensure that every car gets a new
- private final Driver driver;
- private final Model model;
+ private final Driver driver; // this car's driver
+ private final Model model; // the model the car is in
/**
- * The constructor
+ * The constructor:
+ * - choose a random speed
+ * - set right location, direction and colour
+ * - create new Driver object
+ *
* @param number of the car
+ * @param model the model the car is in
*/
public Car(int number, Model model) {
this.number = number;
@@ -40,38 +45,62 @@ public class Car
speed = MINCARSPEED + random.nextInt(MAXCARSPEED - MINCARSPEED + 1);
direction = Direction.intToDirection(number);
location = RoadView.WINDOWSIZE - 2 - (number/Model.DIRECTIONS) * (CARLENGTH + MINCARSPACE);
- color = carColors[number % carColors.length];
+ colour = carColours[number % carColours.length];
driver = new Driver(this);
this.model = model;
}
+ /**
+ * Get the driver
+ * @return
+ */
public Driver getDriver() {
return driver;
}
+ /**
+ * Get the direction
+ * @return
+ */
public Direction getDirection() {
return direction;
}
+ /**
+ * Get the number of this car
+ * @return
+ */
public int getNumber() {
return number;
}
+
+ /**
+ * Get the current location of this car
+ * @return
+ */
+ public int getLocation() {
+ return location;
+ }
+ /**
+ * Get the location the car would move to if it would move
+ * @return
+ */
public int getNewLocation() {
return (location + speed) % RoadView.WINDOWSIZE;
}
/**
- * move this car one step
+ * Move the car one step
*/
public void step() {
location = getNewLocation();
- Regelaar.getInstance().setLocation(this, location);
+ Overviewer.getInstance().setLocation(this, location);
model.update();
}
/**
- * paint this car
+ * Paint this car
* @param g
*/
public void paint(Graphics g) {
@@ -104,17 +133,9 @@ public class Car
default:
x = y = w = h = 10;
}
- g.setColor(color);
+ g.setColor(colour);
g.fillRect(x, y, w, h);
g.setColor(Color.WHITE);
g.drawString(String.valueOf(number), x + w / 6, y + h / 2 + 4);
}
-
- /**
- * yield the current location of this car
- * @return
- */
- public int getLocation() {
- return location;
- }
}