diff options
author | Camil Staps | 2015-05-29 12:06:42 +0200 |
---|---|---|
committer | Camil Staps | 2015-05-29 12:06:42 +0200 |
commit | a68fa3c3c96c38b811755022089cb8aee2f5521c (patch) | |
tree | ef1463ba40bffedfb22aeca5547c7129c2dc3aca /Week14 Route 66/src/com/camilstaps/route66/Driver.java | |
parent | Overviewer without Map (diff) |
Intermediate commit: works partially, but sometimes cars go over each other (Crossing.isAllowed() is not synchronized)
Diffstat (limited to 'Week14 Route 66/src/com/camilstaps/route66/Driver.java')
-rw-r--r-- | Week14 Route 66/src/com/camilstaps/route66/Driver.java | 32 |
1 files changed, 27 insertions, 5 deletions
diff --git a/Week14 Route 66/src/com/camilstaps/route66/Driver.java b/Week14 Route 66/src/com/camilstaps/route66/Driver.java index e2e5a7d..4a323cd 100644 --- a/Week14 Route 66/src/com/camilstaps/route66/Driver.java +++ b/Week14 Route 66/src/com/camilstaps/route66/Driver.java @@ -8,27 +8,46 @@ import OO14route66.Model; import java.util.Random; /** - * - * @author camilstaps + * A Driver takes care its car doesn't collide + * @author Camil Staps */ public class Driver implements Runnable { private final Car car; + private final Model model; private final Random r; - private boolean running; + private boolean running, waitingToCross = false; private int delay; - public Driver(Car car) { + public Driver(Car car, Model model) { this.car = car; + this.model = model; r = new Random(); } + /** + * Set running to false to disable driving altogether + * @param value + */ public void setRunning(boolean value) { running = value; } + public void setWaitingToCross(boolean value) { + waitingToCross = value; + } + + public boolean getWaitingToCross() { + return waitingToCross; + } + + /** + * Set some approximate delay for {@link Driver#pause()}. The actual delay + * is still pseudorandom. + * @param value + */ public void setDelay(int value) { delay = value; } @@ -38,7 +57,7 @@ public class Driver implements Runnable { public void run() { running = true; while (true) { - while (!running || !Overviewer.getInstance().isSafeLocation(car, car.getNewLocation())); + while (!running || !model.isSafeLocation(car, car.getNewLocation())); car.step(); @@ -46,6 +65,9 @@ public class Driver implements Runnable { } } + /** + * Pause for some pseudorandom time + */ protected void pause() { try { int bonus = 0; |