aboutsummaryrefslogtreecommitdiff
path: root/Week14 Route 66/src/OO14route66/Car.java
diff options
context:
space:
mode:
authorCamil Staps2015-05-29 13:17:53 +0200
committerCamil Staps2015-05-29 13:17:53 +0200
commit3ccaa674fdbd3408d4cbea3ed9abb02c14433b9c (patch)
treec51dbe974566b4c437500fbf6894b626b203d265 /Week14 Route 66/src/OO14route66/Car.java
parentHack to make it seem to be working (diff)
licensing; reorganisation; javadoc; cleanup
Diffstat (limited to 'Week14 Route 66/src/OO14route66/Car.java')
-rw-r--r--Week14 Route 66/src/OO14route66/Car.java149
1 files changed, 0 insertions, 149 deletions
diff --git a/Week14 Route 66/src/OO14route66/Car.java b/Week14 Route 66/src/OO14route66/Car.java
deleted file mode 100644
index c12291a..0000000
--- a/Week14 Route 66/src/OO14route66/Car.java
+++ /dev/null
@@ -1,149 +0,0 @@
-package OO14route66;
-
-import com.camilstaps.route66.Driver;
-import java.awt.Color;
-import java.awt.Graphics;
-import java.util.Random;
-
-/**
- * Class for an individual car
- *
- * @author Pieter Koopman, Camil Staps
- */
-public class Car {
- public static final int
- 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 colour; // colour of this car
- private final Random random; // here to ensure that every car gets a new
- private final Driver driver; // this car's driver
- private final Model model; // the model the car is in
-
- /**
- * The constructor:
- * - choose a random speed
- * - set right location, direction and colour
- * - create new Driver object
- * - say hello to the Overviewer
- *
- * @param number of the car
- * @param model the model the car is in
- */
- public Car(int number, Model model) {
- this.number = number;
- random = new Random();
- speed = MINCARSPEED + random.nextInt(MAXCARSPEED - MINCARSPEED + 1);
- direction = Direction.intToDirection(number);
- location = RoadView.WINDOWSIZE - 2 - (number/Model.DIRECTIONS) * (CARLENGTH + MINCARSPACE);
- colour = carColours[number % carColours.length];
- driver = new Driver(this, model);
- 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 the car one step
- */
- public void step() {
- location = getNewLocation();
- //System.err.println("Car ");
- model.update();
- }
-
- public boolean isInFrontOfCrossing() {
- return location < RoadView.getStartCrossing() && getNewLocation() >= RoadView.getStartCrossing();
- }
-
- public boolean isOnCrossing() {
- return location > RoadView.getStartCrossing() && location - CARLENGTH < RoadView.getEndCrossing();
- }
-
- /**
- * Paint this car
- * @param g
- */
- public void paint(Graphics g) {
- int x, y, w, h;
- switch (direction) {
- case North:
- x = RoadView.WINDOWSIZE / 2 + 1;
- y = RoadView.WINDOWSIZE - location;
- w = Car.CARWIDTH;
- h = Car.CARLENGTH;
- break;
- case East:
- x = location - Car.CARLENGTH;
- y = RoadView.WINDOWSIZE / 2 + 1;
- w = Car.CARLENGTH;
- h = Car.CARWIDTH;
- break;
- case South:
- x = RoadView.WINDOWSIZE / 2 - Car.CARWIDTH - 1;
- y = location - Car.CARLENGTH;
- w = Car.CARWIDTH;
- h = Car.CARLENGTH;
- break;
- case West:
- x = RoadView.WINDOWSIZE - location - 1;
- y = RoadView.WINDOWSIZE / 2 - Car.CARWIDTH - 1;
- w = Car.CARLENGTH;
- h = Car.CARWIDTH;
- break;
- default:
- x = y = w = h = 10;
- }
- 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);
- }
-}