You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
335 lines
12 KiB
335 lines
12 KiB
package seng302.Model;
|
|
|
|
|
|
import javafx.animation.AnimationTimer;
|
|
import javafx.application.Platform;
|
|
import javafx.beans.property.StringProperty;
|
|
import javafx.collections.FXCollections;
|
|
import javafx.collections.ObservableArray;
|
|
import javafx.collections.ObservableList;
|
|
import org.geotools.referencing.GeodeticCalculator;
|
|
import seng302.Controllers.RaceController;
|
|
import seng302.GPSCoordinate;
|
|
|
|
import java.awt.geom.Point2D;
|
|
import java.util.ArrayList;
|
|
import java.util.Random;
|
|
|
|
/**
|
|
* Parent class for races
|
|
* Created by fwy13 on 3/03/17.
|
|
*/
|
|
public abstract class Race implements Runnable {
|
|
//protected BoatInRace[] startingBoats;
|
|
protected ObservableList<BoatInRace> startingBoats;
|
|
protected ArrayList<Leg> legs;
|
|
protected RaceController controller;
|
|
protected int boatsFinished = 0;
|
|
protected long totalTimeElapsed;
|
|
|
|
private int lastFPS = 20;
|
|
|
|
|
|
protected int scaleFactor;
|
|
|
|
private int SLEEP_TIME = 100; //time in milliseconds to pause in a paced loop
|
|
protected int PRERACE_TIME = 5000; //time in milliseconds to pause during pre-race
|
|
private boolean timerEnabled = true; //boolean to determine if timer is ran
|
|
|
|
/**
|
|
* Initailiser for Race
|
|
* @param boats Takes in an array of boats that are participating in the race.
|
|
* @param legs Number of marks in order that the boats pass in order to complete the race.
|
|
*/
|
|
public Race(BoatInRace[] boats, ArrayList<Leg> legs, RaceController controller, int scaleFactor) {
|
|
|
|
this.startingBoats = FXCollections.observableArrayList(boats);
|
|
this.legs = legs;
|
|
this.legs.add(new Leg("Finish", this.legs.size()));
|
|
this.controller = controller;
|
|
this.scaleFactor = scaleFactor;
|
|
if (startingBoats != null && startingBoats.size() > 0){
|
|
initialiseBoats();
|
|
}
|
|
}
|
|
|
|
// /**
|
|
// * Constructor for Race class
|
|
// * @param boats boats participating in the race.
|
|
// * @param legs legs that there are in the race.
|
|
// */
|
|
// public Race(BoatInRace[] boats, ArrayList<Leg> legs, int scaleFactor) {
|
|
// if (boats.length > 0) {
|
|
// for (BoatInRace boat : boats) {
|
|
// if (boat != null) {
|
|
// boat.setScaledVelocity(boat.getVelocity() * scaleFactor);
|
|
// }
|
|
// }
|
|
// }
|
|
// this.startingBoats = FXCollections.observableArrayList(boats);
|
|
// this.legs = legs;
|
|
// this.legs.add(new Leg("Finish", this.legs.size()));
|
|
// this.scaleFactor = scaleFactor;
|
|
// }
|
|
|
|
|
|
protected void initialiseBoats() {
|
|
|
|
// Leg startLeg = legs.get(0);
|
|
// Leg copyLeg = startLeg.createCopy();
|
|
|
|
//ArrayList<GPSCoordinate> startPositions = getSpreadStartingPositions();
|
|
for (int i = 0; i < startingBoats.size(); i++) {
|
|
BoatInRace boat = startingBoats.get(i);
|
|
if (boat != null) {
|
|
boat.setScaledVelocity(boat.getVelocity() * scaleFactor);
|
|
boat.setCurrentLeg(legs.get(0));
|
|
// copyLeg.setStartGPSCoordinate(startPositions.get(i));
|
|
// boat.setCurrentLeg(startLeg);
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Runnable for the thread.
|
|
*/
|
|
public void run() {
|
|
setControllerListeners();
|
|
initialiseBoats();
|
|
if(timerEnabled) countdownTimer();
|
|
simulateRace();
|
|
}
|
|
|
|
/**
|
|
* Disable the timer
|
|
*/
|
|
public void disableTimer() {
|
|
timerEnabled = false;
|
|
}
|
|
|
|
|
|
/**
|
|
* Countdown timer until race starts. Use PRERACE_TIME to set countdown duration.
|
|
*/
|
|
protected 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;
|
|
if (timeLeft == 0 && controller != null) {
|
|
updateTime("Race is starting...");
|
|
} else {
|
|
currentTimeInSeconds = timeLeft / 1000;
|
|
minutes = currentTimeInSeconds / 60;
|
|
remainingSeconds = currentTimeInSeconds % 60;
|
|
hours = minutes / 60;
|
|
minutes = minutes % 60;
|
|
if (controller != null) {
|
|
updateTime(String.format("Race clock: -%02d:%02d:%02d", hours, minutes, remainingSeconds));
|
|
}
|
|
}
|
|
try {
|
|
timeLoopEnded = System.currentTimeMillis();
|
|
Thread.sleep(SLEEP_TIME - (timeLoopEnded - currentTime));
|
|
} catch (InterruptedException e) {
|
|
e.printStackTrace();
|
|
}
|
|
currentTime = System.currentTimeMillis();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Takes total time elapsed and format to hour:minute:second
|
|
* @return Formatted time as string
|
|
*/
|
|
protected String calcTimer() {
|
|
long minutes;
|
|
long currentTimeInSeconds;
|
|
long remainingSeconds;
|
|
long hours;
|
|
|
|
currentTimeInSeconds = (totalTimeElapsed * scaleFactor)/ 1000;
|
|
minutes = currentTimeInSeconds / 60;
|
|
remainingSeconds = currentTimeInSeconds % 60;
|
|
hours = minutes / 60;
|
|
minutes = minutes % 60;
|
|
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
|
|
*/
|
|
protected void updateTime(String time){
|
|
Platform.runLater(() -> {controller.setTimer(time);});
|
|
}
|
|
|
|
/**
|
|
* Update the calculated fps to the fps label
|
|
* @param fps The new calculated fps value
|
|
*/
|
|
private void updateFPS(int fps) {
|
|
Platform.runLater(() -> {controller.setFrames("FPS: " + fps);});
|
|
}
|
|
|
|
private boolean doNotFinish() {
|
|
Random rand = new Random();
|
|
return rand.nextInt(100) < 1;
|
|
}
|
|
|
|
/**
|
|
* 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");
|
|
|
|
for (BoatInRace boat: startingBoats){
|
|
boat.setStarted(true);
|
|
}
|
|
|
|
new AnimationTimer() {
|
|
|
|
long timeRaceStarted = System.currentTimeMillis(); //start time of loop
|
|
int fps = 0; //init fps value
|
|
long timeCurrent = System.currentTimeMillis(); //current time
|
|
|
|
@Override
|
|
public void handle(long arg0) {
|
|
|
|
/*long timeLoopStarted;
|
|
long timeLoopEnded;
|
|
int fps = 0;*/
|
|
if (boatsFinished < startingBoats.size()) {
|
|
//timeLoopStarted = System.currentTimeMillis();
|
|
totalTimeElapsed = System.currentTimeMillis() - timeRaceStarted;
|
|
|
|
for (BoatInRace boat : startingBoats) {
|
|
if (boat != null && !boat.isFinished()) {
|
|
updatePosition(boat, Math.round(1000/lastFPS));
|
|
checkPosition(boat, totalTimeElapsed);
|
|
}
|
|
}
|
|
|
|
if (controller != null) controller.updateMap(startingBoats);
|
|
if (timerEnabled)
|
|
updateTime(calcTimer());
|
|
} else {
|
|
//Exit animation timer
|
|
updateTime(calcTimer());
|
|
updateFPS(0); //race ended so fps = 0
|
|
stop(); //exit animation timer
|
|
}
|
|
fps++;
|
|
if ((System.currentTimeMillis()-timeCurrent) > 1000){
|
|
updateFPS(fps);
|
|
lastFPS = fps;
|
|
fps = 0;
|
|
timeCurrent = System.currentTimeMillis();
|
|
}
|
|
|
|
}
|
|
|
|
}.start();
|
|
}
|
|
|
|
/**
|
|
* Checks the position of the boat, this updates the boats current position.
|
|
* @param boat Boat that the postion is to be updated for.
|
|
* @param timeElapsed Time that has elapse since the start of the the race.
|
|
* @see BoatInRace
|
|
*/
|
|
protected void checkPosition(BoatInRace boat, long timeElapsed) {
|
|
if (boat.getDistanceTravelledInLeg() > boat.getCurrentLeg().getDistance()){
|
|
//boat has passed onto new leg
|
|
if (boat.getCurrentLeg().getName().equals("Finish")) {
|
|
//boat has finished
|
|
boatsFinished++;
|
|
boat.setFinished(true);
|
|
boat.setTimeFinished(timeElapsed);
|
|
} else if(doNotFinish()) {
|
|
boatsFinished++;
|
|
boat.setFinished(true);
|
|
boat.setCurrentLeg(new Leg("DNF",-1));
|
|
boat.setVelocity(0);
|
|
boat.setScaledVelocity(0);
|
|
} else {
|
|
//Calculate how much the boat overshot the marker by
|
|
boat.setDistanceTravelledInLeg(boat.getDistanceTravelledInLeg() - boat.getCurrentLeg().getDistance());
|
|
//Move boat on to next leg
|
|
Leg nextLeg = legs.get(boat.getCurrentLeg().getLegNumber() + 1);
|
|
|
|
boat.setCurrentLeg(nextLeg);
|
|
//Add overshoot distance into the distance travelled for the next leg
|
|
boat.setDistanceTravelledInLeg(boat.getDistanceTravelledInLeg());
|
|
}
|
|
//Update the boat display table in the GUI to reflect the leg change
|
|
FXCollections.sort(startingBoats, (a, b) -> b.getCurrentLeg().getLegNumber() - a.getCurrentLeg().getLegNumber());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Update call for the controller.
|
|
*/
|
|
protected void setControllerListeners() {
|
|
if(controller != null) controller.setInfoTable(this);
|
|
}
|
|
|
|
/**
|
|
* Returns the boats that have started the race.
|
|
* @return ObservableList of BoatInRace class that participated in the race.
|
|
* @see ObservableList
|
|
* @see BoatInRace
|
|
*/
|
|
public ObservableList<BoatInRace> getStartingBoats() {
|
|
return startingBoats;
|
|
}
|
|
|
|
/**
|
|
* Updates the boat's gps coordinates depending on time elapsed
|
|
* @param boat
|
|
* @param millisecondsElapsed
|
|
*/
|
|
protected abstract void updatePosition(BoatInRace boat, int millisecondsElapsed);
|
|
|
|
/**
|
|
* Creates a list of starting positions for the different boats, so they do not appear cramped at the start line
|
|
* @return
|
|
*/
|
|
// public ArrayList<GPSCoordinate> getSpreadStartingPositions() {
|
|
//
|
|
// int nBoats = startingBoats.size();
|
|
// GPSCoordinate marker1 = legs.get(0).getStartMarker1();
|
|
// GPSCoordinate marker2 = legs.get(0).getStartMarker2();
|
|
// GeodeticCalculator initialCalc = new GeodeticCalculator();
|
|
// initialCalc.setStartingGeographicPoint(marker1.getLongitude(), marker1.getLatitude());
|
|
// initialCalc.setDestinationGeographicPoint(marker2.getLongitude(), marker2.getLatitude());
|
|
//
|
|
// double azimuth = initialCalc.getAzimuth();
|
|
// double distanceBetweenMarkers = initialCalc.getOrthodromicDistance();
|
|
// double distanceBetweenBoats = distanceBetweenMarkers / (nBoats + 1);
|
|
//
|
|
// GeodeticCalculator positionCalc = new GeodeticCalculator();
|
|
// positionCalc.setStartingGeographicPoint(marker1.getLongitude(), marker1.getLatitude());
|
|
// ArrayList<GPSCoordinate> positions = new ArrayList<>();
|
|
//
|
|
// for (int i = 0; i < nBoats; i++) {
|
|
// positionCalc.setDirection(azimuth, distanceBetweenBoats);
|
|
// Point2D position = positionCalc.getDestinationGeographicPoint();
|
|
// positions.add(new GPSCoordinate(position.getY(), position.getX()));
|
|
//
|
|
// positionCalc = new GeodeticCalculator();
|
|
// positionCalc.setStartingGeographicPoint(position);
|
|
// }
|
|
// return positions;
|
|
// }
|
|
}
|