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;
|
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
|
* Initailiser for Race
|
||||||
@@ -53,6 +54,7 @@ public abstract class Race implements Runnable {
|
|||||||
public void run() {
|
public void run() {
|
||||||
updateController();
|
updateController();
|
||||||
preRace();
|
preRace();
|
||||||
|
countdownTimer();
|
||||||
simulateRace();
|
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() {
|
private String calcTimer() {
|
||||||
long minutes;
|
long minutes;
|
||||||
long currentTimeInSeconds;
|
long currentTimeInSeconds;
|
||||||
@@ -81,7 +111,8 @@ public abstract class Race implements Runnable {
|
|||||||
currentTimeInSeconds = totalTimeElapsed / 1000;
|
currentTimeInSeconds = totalTimeElapsed / 1000;
|
||||||
minutes = currentTimeInSeconds / 60;
|
minutes = currentTimeInSeconds / 60;
|
||||||
remainingSeconds = currentTimeInSeconds % 60;
|
remainingSeconds = currentTimeInSeconds % 60;
|
||||||
hours = minutes/60;
|
hours = minutes / 60;
|
||||||
|
minutes = minutes % 60;
|
||||||
return String.format("%02d:%02d:%02d", hours, minutes, remainingSeconds);
|
return String.format("%02d:%02d:%02d", hours, minutes, remainingSeconds);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user