|
|
|
|
@ -31,7 +31,7 @@ public abstract class Race implements Runnable {
|
|
|
|
|
protected int scaleFactor;
|
|
|
|
|
|
|
|
|
|
private int SLEEP_TIME = 100; //time in milliseconds to pause in a paced loop
|
|
|
|
|
protected int PRERACE_TIME = 5000; //time in milliseconds to pause during pre-race
|
|
|
|
|
protected int PRERACE_TIME = 15000; //time in milliseconds to pause during pre-race
|
|
|
|
|
private boolean timerEnabled = true; //boolean to determine if timer is ran
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
@ -85,7 +85,7 @@ public abstract class Race implements Runnable {
|
|
|
|
|
setControllerListeners();
|
|
|
|
|
initialiseBoats();
|
|
|
|
|
if (timerEnabled) countdownTimer();
|
|
|
|
|
simulateRace();
|
|
|
|
|
//simulateRace();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
@ -100,37 +100,35 @@ public abstract class Race implements Runnable {
|
|
|
|
|
* Countdown timer until race starts. Use PRERACE_TIME to set countdown duration.
|
|
|
|
|
*/
|
|
|
|
|
protected void countdownTimer() {
|
|
|
|
|
long currentTime = System.currentTimeMillis();
|
|
|
|
|
long startTime = currentTime + PRERACE_TIME;
|
|
|
|
|
long minutes;
|
|
|
|
|
long currentTimeInSeconds;
|
|
|
|
|
long remainingSeconds;
|
|
|
|
|
long hours;
|
|
|
|
|
long timeLeft;
|
|
|
|
|
long timeLoopEnded;
|
|
|
|
|
new AnimationTimer() {
|
|
|
|
|
long currentTime = System.currentTimeMillis();
|
|
|
|
|
long startTime = currentTime + (PRERACE_TIME/scaleFactor);
|
|
|
|
|
long minutes;
|
|
|
|
|
long currentTimeInSeconds;
|
|
|
|
|
long remainingSeconds;
|
|
|
|
|
long hours;
|
|
|
|
|
long timeLeft;
|
|
|
|
|
|
|
|
|
|
while (currentTime <= startTime) {
|
|
|
|
|
timeLeft = startTime - currentTime;
|
|
|
|
|
if (timeLeft == 0 && controller != null) {
|
|
|
|
|
updateTime("Race is starting...");
|
|
|
|
|
} else {
|
|
|
|
|
currentTimeInSeconds = timeLeft / 1000;
|
|
|
|
|
minutes = currentTimeInSeconds / 60;
|
|
|
|
|
remainingSeconds = currentTimeInSeconds % 60;
|
|
|
|
|
hours = minutes / 60;
|
|
|
|
|
minutes = minutes % 60;
|
|
|
|
|
if (controller != null) {
|
|
|
|
|
updateTime(String.format("Race clock: -%02d:%02d:%02d", hours, minutes, remainingSeconds));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
timeLoopEnded = System.currentTimeMillis();
|
|
|
|
|
Thread.sleep(SLEEP_TIME - (timeLoopEnded - currentTime));
|
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
@Override
|
|
|
|
|
public void handle(long arg0) {
|
|
|
|
|
timeLeft = startTime - currentTime;
|
|
|
|
|
if (timeLeft <= 0 && controller != null) {
|
|
|
|
|
updateTime("Race is starting...");
|
|
|
|
|
stop();
|
|
|
|
|
simulateRace();
|
|
|
|
|
} else {
|
|
|
|
|
currentTimeInSeconds = (timeLeft*scaleFactor) / 1000;
|
|
|
|
|
minutes = currentTimeInSeconds / 60;
|
|
|
|
|
remainingSeconds = currentTimeInSeconds % 60;
|
|
|
|
|
hours = minutes / 60;
|
|
|
|
|
minutes = minutes % 60;
|
|
|
|
|
if (controller != null) {
|
|
|
|
|
updateTime(String.format("Race clock: -%02d:%02d:%02d", hours, minutes, remainingSeconds));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
currentTime = System.currentTimeMillis();
|
|
|
|
|
}
|
|
|
|
|
currentTime = System.currentTimeMillis();
|
|
|
|
|
}
|
|
|
|
|
}.start();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
@ -176,7 +174,7 @@ public abstract class Race implements Runnable {
|
|
|
|
|
|
|
|
|
|
private boolean doNotFinish() {
|
|
|
|
|
Random rand = new Random();
|
|
|
|
|
return rand.nextInt(100) < 1;
|
|
|
|
|
return rand.nextInt(4) < 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
@ -264,8 +262,21 @@ public abstract class Race implements Runnable {
|
|
|
|
|
boat.setDistanceTravelledInLeg(boat.getDistanceTravelledInLeg());
|
|
|
|
|
}
|
|
|
|
|
//Update the boat display table in the GUI to reflect the leg change
|
|
|
|
|
FXCollections.sort(startingBoats, (a, b) -> b.getCurrentLeg().getLegNumber() - a.getCurrentLeg().getLegNumber());
|
|
|
|
|
boat.setPosition(startingBoats.indexOf(boat));
|
|
|
|
|
updatePositions();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Update position of boats in race, no position if on starting leg or DNF.
|
|
|
|
|
*/
|
|
|
|
|
private void updatePositions() {
|
|
|
|
|
FXCollections.sort(startingBoats, (a, b) -> b.getCurrentLeg().getLegNumber() - a.getCurrentLeg().getLegNumber());
|
|
|
|
|
for(BoatInRace boat: startingBoats) {
|
|
|
|
|
if(boat != null) {
|
|
|
|
|
boat.setPosition(Integer.toString(startingBoats.indexOf(boat) + 1));
|
|
|
|
|
if (boat.getCurrentLeg().getName().equals("DNF") || boat.getCurrentLeg().getLegNumber() == 0)
|
|
|
|
|
boat.setPosition("-");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|