Mock race runs without an fx component

#story[778]
main
Erika Savell 9 years ago
parent 3326da4f5d
commit 34c0aec0b9

@ -1,6 +1,8 @@
package seng302; package seng302;
import javafx.application.Application;
import javafx.stage.Stage;
import org.xml.sax.SAXException; import org.xml.sax.SAXException;
import seng302.Model.ConstantVelocityRace; import seng302.Model.ConstantVelocityRace;
import seng302.Model.Race; import seng302.Model.Race;
@ -8,7 +10,7 @@ import seng302.Model.Race;
import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException; import java.io.IOException;
public class App{ public class App extends Application {
/** /**
* Entry point for running the programme * Entry point for running the programme
@ -16,6 +18,11 @@ public class App{
* @param args for starting the programme * @param args for starting the programme
*/ */
public static void main(String[] args) { public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
try { try {
RaceDataSource raceData = new RaceXMLReader("raceXML/bermuda_AC35.xml"); RaceDataSource raceData = new RaceXMLReader("raceXML/bermuda_AC35.xml");
ConstantVelocityRace newRace = new ConstantVelocityRace(raceData, 15); ConstantVelocityRace newRace = new ConstantVelocityRace(raceData, 15);
@ -28,7 +35,6 @@ public class App{
} catch (ParserConfigurationException e) { } catch (ParserConfigurationException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
} }

@ -269,22 +269,6 @@ public class BoatInRace extends Boat {
this.position.set(position); this.position.set(position);
} }
/**
* Adds a new point to boat's track.
* @param coordinate of point on track
* @return whether add is successful
* @see seng302.Model.TrackPoint
*/
public boolean addTrackPoint(GPSCoordinate coordinate) {
Boolean added = System.currentTimeMillis() >= nextValidTime;
long currentTime = System.currentTimeMillis();
if (added && this.started) {
nextValidTime = currentTime + (long) trackPointTimeInterval;
track.add(new TrackPoint(coordinate, currentTime, TRACK_POINT_LIMIT * (long) trackPointTimeInterval));
}
return added;
}
/** /**
* Returns the boat's sampled track between start of race and current time. * Returns the boat's sampled track between start of race and current time.
* @return queue of track points * @return queue of track points

@ -25,6 +25,7 @@ public abstract class Race implements Runnable {
protected int boatsFinished = 0; protected int boatsFinished = 0;
protected long totalTimeElapsed; protected long totalTimeElapsed;
private int lastFPS = 20; private int lastFPS = 20;
private int dnfChance = 0; //percentage chance a boat fails at each checkpoint
protected int scaleFactor; protected int scaleFactor;
@ -67,7 +68,7 @@ public abstract class Race implements Runnable {
/** /**
* Checks the position of the boat, this updates the boats current position. * Checks the position of the boat, this updates the boats current position.
* *
* @param boat Boat that the postion is to be updated for. * @param boat Boat that the position is to be updated for.
* @param timeElapsed Time that has elapse since the start of the the race. * @param timeElapsed Time that has elapse since the start of the the race.
* @see BoatInRace * @see BoatInRace
*/ */
@ -86,16 +87,14 @@ public abstract class Race implements Runnable {
*/ */
public void run() { public void run() {
initialiseBoats(); initialiseBoats();
System.out.println("Running race"); countdownTimer();
simulateRace();
} }
/** /**
* Countdown timer until race starts. Use PRERACE_TIME to set countdown duration. * Countdown timer until race starts. Use PRERACE_TIME to set countdown duration.
*/ */
protected void countdownTimer() { protected void countdownTimer() {
System.out.println("Running countdown timer"); AnimationTimer timer = new AnimationTimer() {
new AnimationTimer() {
long currentTime = System.currentTimeMillis(); long currentTime = System.currentTimeMillis();
long startTime = currentTime + (PRERACE_TIME/scaleFactor); long startTime = currentTime + (PRERACE_TIME/scaleFactor);
long minutes; long minutes;
@ -106,9 +105,7 @@ public abstract class Race implements Runnable {
@Override @Override
public void handle(long arg0) { public void handle(long arg0) {
System.out.println("Starting timer");
timeLeft = startTime - currentTime; timeLeft = startTime - currentTime;
System.out.println(timeLeft);
if (timeLeft <= 0) { if (timeLeft <= 0) {
stop(); stop();
simulateRace(); simulateRace();
@ -121,8 +118,8 @@ public abstract class Race implements Runnable {
} }
currentTime = System.currentTimeMillis(); currentTime = System.currentTimeMillis();
} }
}.start(); };
System.out.println("Running countdown timer"); timer.start();
} }
/** /**
@ -159,30 +156,19 @@ public abstract class Race implements Runnable {
new AnimationTimer() { new AnimationTimer() {
long timeRaceStarted = System.currentTimeMillis(); //start time of loop long timeRaceStarted = System.currentTimeMillis(); //start time of loop
int fps = 0; //init fps value
long timeCurrent = System.currentTimeMillis(); //current time
@Override @Override
public void handle(long arg0) { public void handle(long arg0) {
System.out.println();
if (boatsFinished < startingBoats.size()) { if (boatsFinished < startingBoats.size()) {
totalTimeElapsed = System.currentTimeMillis() - timeRaceStarted; totalTimeElapsed = System.currentTimeMillis() - timeRaceStarted;
for (BoatInRace boat : startingBoats) { for (BoatInRace boat : startingBoats) {
if (boat != null && !boat.isFinished()) { if (boat != null && !boat.isFinished()) {
boat.addTrackPoint(boat.getCurrentPosition());
updatePosition(boat, Math.round(1000 / lastFPS) > 20 ? 15 : Math.round(1000 / lastFPS)); updatePosition(boat, Math.round(1000 / lastFPS) > 20 ? 15 : Math.round(1000 / lastFPS));
checkPosition(boat, totalTimeElapsed); checkPosition(boat, totalTimeElapsed);
} }
} }
} }
fps++;
if ((System.currentTimeMillis() - timeCurrent) > 1000) {
lastFPS = fps;
fps = 0;
timeCurrent = System.currentTimeMillis();
}
} }
@ -197,6 +183,7 @@ public abstract class Race implements Runnable {
for(BoatInRace boat: startingBoats) { for(BoatInRace boat: startingBoats) {
if(boat != null) { if(boat != null) {
boat.setPosition(Integer.toString(startingBoats.indexOf(boat) + 1)); boat.setPosition(Integer.toString(startingBoats.indexOf(boat) + 1));
System.out.println(boat.getName() + boat.getPosition());
if (boat.getCurrentLeg().getName().equals("DNF") || boat.getCurrentLeg().getLegNumber() == 0) if (boat.getCurrentLeg().getName().equals("DNF") || boat.getCurrentLeg().getLegNumber() == 0)
boat.setPosition("-"); boat.setPosition("-");
} }

Loading…
Cancel
Save