Implement countdown clock
-Added method countdownTimer() that countdowns from the current time to an selected time -Method is called in run (in Race) before the simulatedRace -Private integer PRERACE_TIME is used to set time before the race begins #story [16]
This commit is contained in:
@@ -24,7 +24,8 @@ public abstract class Race implements Runnable {
|
||||
protected long totalTimeElapsed;
|
||||
|
||||
|
||||
private int SLEEP_TIME = 1000; //time in milliseconds to pause in a paced loop
|
||||
private int SLEEP_TIME = 25; //time in milliseconds to pause in a paced loop
|
||||
private int PRERACE_TIME = Integer.MAX_VALUE; //time in milliseconds to pause during pre-race
|
||||
|
||||
/**
|
||||
* Initailiser for Race
|
||||
@@ -53,6 +54,7 @@ public abstract class Race implements Runnable {
|
||||
public void run() {
|
||||
updateController();
|
||||
preRace();
|
||||
countdownTimer();
|
||||
simulateRace();
|
||||
}
|
||||
|
||||
@@ -72,6 +74,34 @@ public abstract class Race implements Runnable {
|
||||
}
|
||||
|
||||
|
||||
private void countdownTimer() {
|
||||
long currentTime = System.currentTimeMillis();
|
||||
long startTime = currentTime + PRERACE_TIME;
|
||||
long minutes;
|
||||
long currentTimeInSeconds;
|
||||
long remainingSeconds;
|
||||
long hours;
|
||||
long timeLeft;
|
||||
long timeLoopEnded;
|
||||
|
||||
while (currentTime <= startTime) {
|
||||
timeLeft = startTime - currentTime;
|
||||
currentTimeInSeconds = timeLeft / 1000;
|
||||
minutes = currentTimeInSeconds / 60;
|
||||
remainingSeconds = currentTimeInSeconds % 60;
|
||||
hours = minutes / 60;
|
||||
minutes = minutes % 60;
|
||||
updateTime(String.format("%02d:%02d:%02d", hours, minutes, remainingSeconds));
|
||||
try {
|
||||
timeLoopEnded = System.currentTimeMillis();
|
||||
Thread.sleep(SLEEP_TIME - (timeLoopEnded - currentTime));
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
currentTime = System.currentTimeMillis();
|
||||
}
|
||||
}
|
||||
|
||||
private String calcTimer() {
|
||||
long minutes;
|
||||
long currentTimeInSeconds;
|
||||
@@ -82,6 +112,7 @@ public abstract class Race implements Runnable {
|
||||
minutes = currentTimeInSeconds / 60;
|
||||
remainingSeconds = currentTimeInSeconds % 60;
|
||||
hours = minutes / 60;
|
||||
minutes = minutes % 60;
|
||||
return String.format("%02d:%02d:%02d", hours, minutes, remainingSeconds);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user