RaceLogic no longer uses AnimationTimer for its main loop (since that ran in javafx thread).

main
fjc40 9 years ago
parent 175fb11178
commit c9875f3987

@ -40,20 +40,23 @@ public class RaceLogic implements RunnableWithFramePeriod {
@Override @Override
public void run() { public void run() {
race.initialiseBoats(); race.initialiseBoats();
this.countdownTimer.start();
countdown();
raceLoop();
} }
/** /**
* Countdown timer until race starts. * Countdown timer until race starts.
*/ */
protected AnimationTimer countdownTimer = new AnimationTimer() { private void countdown() {
long previousFrameTime = System.currentTimeMillis();
long currentTime = System.currentTimeMillis(); while (race.getRaceStatusEnum() != RaceStatusEnum.STARTED) {
@Override long currentTime = System.currentTimeMillis();
public void handle(long arg0) {
//Update race time. //Update race time.
race.updateRaceTime(currentTime); race.updateRaceTime(currentTime);
@ -72,47 +75,28 @@ public class RaceLogic implements RunnableWithFramePeriod {
race.changeWindDirection(); race.changeWindDirection();
if (race.getRaceStatusEnum() == RaceStatusEnum.STARTED) { if (race.getRaceStatusEnum() == RaceStatusEnum.STARTED) {
race.setBoatsStatusToRacing(); race.setBoatsStatusToRacing();
raceTimer.start();
this.stop();
} }
//Update the animations timer's time. waitForFramePeriod(previousFrameTime, currentTime, 50);
currentTime = System.currentTimeMillis(); previousFrameTime = currentTime;
} }
}; }
/** /**
* Timer that runs for the duration of the race, until all boats finish. * Timer that runs for the duration of the race, until all boats finish.
*/ */
private AnimationTimer raceTimer = new AnimationTimer() { private void raceLoop() {
/** long previousFrameTime = System.currentTimeMillis();
* Start time of loop, in milliseconds.
*/
long timeRaceStarted = System.currentTimeMillis();
/** while (race.getRaceStatusEnum() != RaceStatusEnum.FINISHED) {
* Current time during a loop iteration.
*/
long currentTime = System.currentTimeMillis();
/**
* The time of the previous frame, in milliseconds.
*/
long lastFrameTime = timeRaceStarted;
long framePeriod = currentTime - lastFrameTime;
@Override
public void handle(long arg0) {
//Get the current time. //Get the current time.
currentTime = System.currentTimeMillis(); long currentTime = System.currentTimeMillis();
//Execute commands from clients. //Execute commands from clients.
commands.execute(); commands.execute();
@ -124,7 +108,7 @@ public class RaceLogic implements RunnableWithFramePeriod {
if (race.getNumberOfActiveBoats() != 0) { if (race.getNumberOfActiveBoats() != 0) {
//Get the time period of this frame. //Get the time period of this frame.
framePeriod = currentTime - lastFrameTime; long framePeriod = currentTime - previousFrameTime;
//For each boat, we update its position, and generate a BoatLocationMessage. //For each boat, we update its position, and generate a BoatLocationMessage.
for (MockBoat boat : race.getBoats()) { for (MockBoat boat : race.getBoats()) {
@ -141,7 +125,6 @@ public class RaceLogic implements RunnableWithFramePeriod {
//Otherwise, the race is over! //Otherwise, the race is over!
raceFinished.start(); raceFinished.start();
race.setRaceStatusEnum(RaceStatusEnum.FINISHED); race.setRaceStatusEnum(RaceStatusEnum.FINISHED);
this.stop();
} }
if (race.getNumberOfActiveBoats() != 0) { if (race.getNumberOfActiveBoats() != 0) {
@ -152,10 +135,13 @@ public class RaceLogic implements RunnableWithFramePeriod {
server.parseSnapshot(); server.parseSnapshot();
//Update the last frame time. //Update the last frame time.
this.lastFrameTime = currentTime; previousFrameTime = currentTime;
} }
waitForFramePeriod(previousFrameTime, currentTime, 50);
previousFrameTime = currentTime;
} }
}; }
/** /**
* Broadcast that the race has finished. * Broadcast that the race has finished.

@ -174,7 +174,7 @@ public class VisualiserBoat extends Boat {
*/ */
public String getTimeToNextMarkFormatted(ZonedDateTime currentTime) { public String getTimeToNextMarkFormatted(ZonedDateTime currentTime) {
if (getTimeAtLastMark() != null) { if ((getTimeAtLastMark() != null) && (currentTime != null)) {
//Calculate time delta. //Calculate time delta.
Duration timeUntil = Duration.between(currentTime, getEstimatedTimeAtNextMark()); Duration timeUntil = Duration.between(currentTime, getEstimatedTimeAtNextMark());
@ -213,7 +213,7 @@ public class VisualiserBoat extends Boat {
*/ */
public String getTimeSinceLastMarkFormatted(ZonedDateTime currentTime) { public String getTimeSinceLastMarkFormatted(ZonedDateTime currentTime) {
if (getTimeAtLastMark() != null) { if ((getTimeAtLastMark() != null) && (currentTime != null)) {
//Calculate time delta. //Calculate time delta.
Duration timeSince = Duration.between(getTimeAtLastMark(), currentTime); Duration timeSince = Duration.between(getTimeAtLastMark(), currentTime);

Loading…
Cancel
Save