@ -1,6 +1,7 @@
package seng302.Model ;
import javafx.animation.AnimationTimer ;
import javafx.application.Platform ;
import javafx.beans.property.StringProperty ;
import javafx.collections.FXCollections ;
@ -24,7 +25,7 @@ public abstract class Race implements Runnable {
protected long totalTimeElapsed ;
private int SLEEP_TIME = 50 ; //time in milliseconds to pause in a paced loop
private int SLEEP_TIME = 2 5; //time in milliseconds to pause in a paced loop
private int PRERACE_TIME = 10000 ; //Integer.MAX_VALUE; //time in milliseconds to pause during pre-race
private boolean timerEnabled = true ;
@ -59,6 +60,9 @@ public abstract class Race implements Runnable {
simulateRace ( ) ;
}
/ * *
* Disable the timer
* /
public void disableTimer ( ) {
timerEnabled = false ;
}
@ -72,14 +76,16 @@ public abstract class Race implements Runnable {
System . out . println ( "====================" ) ;
for ( int i = 0 ; i < startingBoats . size ( ) ; i + + ) {
if ( startingBoats . get ( i ) ! = null ) {
System . out . println ( i + 1 + ". " + startingBoats . get ( i ) . getName ( ) + ", Speed: "
System . out . println ( i + 1 + ". " + startingBoats . get ( i ) . toStrin g( ) + ", Speed: "
+ Math . round ( startingBoats . get ( i ) . getVelocity ( ) * 1.94384 ) + "kn" ) ;
startingBoats . get ( i ) . setCurrentLeg ( legs . get ( 0 ) ) ;
}
}
}
/ * *
* Countdown timer until race starts . Use PRERACE_TIME to set countdown duration .
* /
private void countdownTimer ( ) {
long currentTime = System . currentTimeMillis ( ) ;
long startTime = currentTime + PRERACE_TIME ;
@ -108,6 +114,10 @@ public abstract class Race implements Runnable {
}
}
/ * *
* Takes total time elapsed and format to hour : minute : second
* @return Formatted time as string
* /
private String calcTimer ( ) {
long minutes ;
long currentTimeInSeconds ;
@ -122,28 +132,43 @@ public abstract class Race implements Runnable {
return String . format ( "Race clock: %02d:%02d:%02d" , hours , minutes , remainingSeconds ) ;
}
/ * *
* Updates the calculated time to the timer label
* @param time The calculated time from calcTimer ( ) method
* /
private void updateTime ( String time ) {
Platform . runLater ( ( ) - > { controller . setTimer ( time ) ; } ) ;
}
private void updateFPS ( int fps ) {
Platform . runLater ( ( ) - > { controller . setFrames ( "FPS: " + fps ) ; } ) ;
}
/ * *
* Starts the Race Simulation , playing the race start to finish with the timescale .
* This prints the boats participating , the order that the events occur in time order , and the respective information of the events .
* /
private void simulateRace ( ) {
System . setProperty ( "javafx.animation.fullspeed" , "true" ) ;
new AnimationTimer ( ) {
long timeRaceStarted = System . currentTimeMillis ( ) ;
int fps = 0 ;
long timeCurrent = System . currentTimeMillis ( ) ;
long timeLoopStarted ;
long timeLoopEnded ;
@Override
public void handle ( long arg0 ) {
/ * long timeLoopStarted ;
long timeLoopEnded ;
int fps = 0 ; * /
while ( boatsFinished < startingBoats . size ( ) ) {
timeLoopStarted = System . currentTimeMillis ( ) ;
if ( boatsFinished < startingBoats . size ( ) ) {
//timeLoopStarted = System.currentTimeMillis();
totalTimeElapsed = System . currentTimeMillis ( ) - timeRaceStarted ;
for ( BoatInRace boat : startingBoats ) {
if ( boat ! = null & & ! boat . isFinished ( ) ) {
updatePosition ( boat , SLEEP_TIME ) ;
@ -151,15 +176,29 @@ public abstract class Race implements Runnable {
}
}
if ( controller ! = null ) controller . updateMap ( startingBoats ) ;
if ( timerEnabled ) updateTime ( calcTimer ( ) ) ;
if ( controller ! = null ) controller . updateMap ( startingBoats ) ;
if ( timerEnabled )
updateTime ( calcTimer ( ) ) ;
}
fps + + ;
if ( ( System . currentTimeMillis ( ) - timeCurrent ) > 1000 ) {
updateFPS ( fps ) ;
fps = 0 ;
timeCurrent = System . currentTimeMillis ( ) ;
}
;
/ * fps + + ;
try {
timeLoopEnded = System . currentTimeMillis ( ) ;
Thread . sleep ( SLEEP_TIME - ( timeLoopEnded - timeLoopStarted ) ) ;
} catch ( InterruptedException e ) {
return ;
}
}
} * /
} ;
//System.out.println("Avg fps:" + fps/(totalTimeElapsed/1000));
} . start ( ) ;
}
/ * *