|
|
|
@ -3,14 +3,11 @@ package seng302.Model;
|
|
|
|
|
|
|
|
|
|
|
|
import javafx.animation.AnimationTimer;
|
|
|
|
import javafx.animation.AnimationTimer;
|
|
|
|
import javafx.application.Platform;
|
|
|
|
import javafx.application.Platform;
|
|
|
|
import javafx.beans.property.StringProperty;
|
|
|
|
|
|
|
|
import javafx.collections.FXCollections;
|
|
|
|
import javafx.collections.FXCollections;
|
|
|
|
import javafx.collections.ObservableArray;
|
|
|
|
|
|
|
|
import javafx.collections.ObservableList;
|
|
|
|
import javafx.collections.ObservableList;
|
|
|
|
import seng302.Controllers.RaceController;
|
|
|
|
import seng302.Controllers.RaceController;
|
|
|
|
import seng302.GPSCoordinate;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import java.util.*;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Parent class for races
|
|
|
|
* Parent class for races
|
|
|
|
@ -24,32 +21,55 @@ public abstract class Race implements Runnable {
|
|
|
|
protected int boatsFinished = 0;
|
|
|
|
protected int boatsFinished = 0;
|
|
|
|
protected long totalTimeElapsed;
|
|
|
|
protected long totalTimeElapsed;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protected int scaleFactor;
|
|
|
|
|
|
|
|
|
|
|
|
private int SLEEP_TIME = 100; //time in milliseconds to pause in a paced loop
|
|
|
|
private int SLEEP_TIME = 100; //time in milliseconds to pause in a paced loop
|
|
|
|
protected int PRERACE_TIME = 10000;//Integer.MAX_VALUE; //time in milliseconds to pause during pre-race
|
|
|
|
protected int PRERACE_TIME = 100;//Integer.MAX_VALUE; //time in milliseconds to pause during pre-race
|
|
|
|
private boolean timerEnabled = true;
|
|
|
|
private boolean timerEnabled = true;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Initailiser for Race
|
|
|
|
* Initailiser for Race
|
|
|
|
|
|
|
|
*
|
|
|
|
* @param boats Takes in an array of boats that are participating in the 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.
|
|
|
|
* @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) {
|
|
|
|
public Race(BoatInRace[] boats, ArrayList<Leg> legs, RaceController controller, int scaleFactor) {
|
|
|
|
|
|
|
|
if (boats.length > 0) {
|
|
|
|
|
|
|
|
for (BoatInRace boat : boats) {
|
|
|
|
|
|
|
|
if (boat != null) {
|
|
|
|
|
|
|
|
boat.setScaledVelocity(boat.getVelocity() * scaleFactor);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
this.startingBoats = FXCollections.observableArrayList(boats);
|
|
|
|
this.startingBoats = FXCollections.observableArrayList(boats);
|
|
|
|
this.legs = legs;
|
|
|
|
this.legs = legs;
|
|
|
|
this.legs.add(new Leg("Finish", this.legs.size()));
|
|
|
|
this.legs.add(new Leg("Finish", this.legs.size()));
|
|
|
|
this.controller = controller;
|
|
|
|
this.controller = controller;
|
|
|
|
|
|
|
|
this.scaleFactor = scaleFactor;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Constructor for Race class
|
|
|
|
* Constructor for Race class
|
|
|
|
|
|
|
|
*
|
|
|
|
* @param boats boats participating in the race.
|
|
|
|
* @param boats boats participating in the race.
|
|
|
|
* @param marks legs that there are in the race.
|
|
|
|
* @param legs legs that there are in the race.
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public Race(BoatInRace[] boats, ArrayList<Leg> marks) {
|
|
|
|
public Race(BoatInRace[] boats, ArrayList<Leg> legs, int scaleFactor) {
|
|
|
|
this(boats, marks, null);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Runnable for the thread.
|
|
|
|
* Runnable for the thread.
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
@ -68,16 +88,13 @@ public abstract class Race implements Runnable {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Set up the state in waiting for the race starts.
|
|
|
|
* Initialises the boats,
|
|
|
|
|
|
|
|
* Sets the boats' current to the first leg in the race
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
private void preRace() {
|
|
|
|
private void preRace() {
|
|
|
|
//show the boats participating.
|
|
|
|
//show the boats participating.
|
|
|
|
System.out.println("Boats Participating:");
|
|
|
|
|
|
|
|
System.out.println("====================");
|
|
|
|
|
|
|
|
for (int i = 0; i < startingBoats.size(); i++) {
|
|
|
|
for (int i = 0; i < startingBoats.size(); i++) {
|
|
|
|
if (startingBoats.get(i) != null) {
|
|
|
|
if (startingBoats.get(i) != null) {
|
|
|
|
System.out.println(i + 1 + ". " + startingBoats.get(i).toString() + ", Speed: "
|
|
|
|
|
|
|
|
+ Math.round(startingBoats.get(i).getVelocity() * 1.94384) + "kn");
|
|
|
|
|
|
|
|
startingBoats.get(i).setCurrentLeg(legs.get(0));
|
|
|
|
startingBoats.get(i).setCurrentLeg(legs.get(0));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@ -98,13 +115,17 @@ public abstract class Race implements Runnable {
|
|
|
|
|
|
|
|
|
|
|
|
while (currentTime <= startTime) {
|
|
|
|
while (currentTime <= startTime) {
|
|
|
|
timeLeft = startTime - currentTime;
|
|
|
|
timeLeft = startTime - currentTime;
|
|
|
|
|
|
|
|
if (timeLeft == 0 && controller != null) {
|
|
|
|
|
|
|
|
updateTime("Race is starting...");
|
|
|
|
|
|
|
|
} else {
|
|
|
|
currentTimeInSeconds = timeLeft / 1000;
|
|
|
|
currentTimeInSeconds = timeLeft / 1000;
|
|
|
|
minutes = currentTimeInSeconds / 60;
|
|
|
|
minutes = currentTimeInSeconds / 60;
|
|
|
|
remainingSeconds = currentTimeInSeconds % 60;
|
|
|
|
remainingSeconds = currentTimeInSeconds % 60;
|
|
|
|
hours = minutes / 60;
|
|
|
|
hours = minutes / 60;
|
|
|
|
minutes = minutes % 60;
|
|
|
|
minutes = minutes % 60;
|
|
|
|
if (controller != null) {
|
|
|
|
if (controller != null) {
|
|
|
|
updateTime(String.format("Time until race starts: %02d:%02d:%02d", hours, minutes, remainingSeconds));
|
|
|
|
updateTime(String.format("Race clock: -%02d:%02d:%02d", hours, minutes, remainingSeconds));
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
timeLoopEnded = System.currentTimeMillis();
|
|
|
|
timeLoopEnded = System.currentTimeMillis();
|
|
|
|
@ -127,8 +148,9 @@ public abstract class Race implements Runnable {
|
|
|
|
long hours;
|
|
|
|
long hours;
|
|
|
|
|
|
|
|
|
|
|
|
currentTimeInSeconds = totalTimeElapsed / 1000;
|
|
|
|
currentTimeInSeconds = totalTimeElapsed / 1000;
|
|
|
|
minutes = currentTimeInSeconds / 60;
|
|
|
|
long scaledTimeInSeconds = currentTimeInSeconds * scaleFactor;
|
|
|
|
remainingSeconds = currentTimeInSeconds % 60;
|
|
|
|
minutes = scaledTimeInSeconds / 60;
|
|
|
|
|
|
|
|
remainingSeconds = scaledTimeInSeconds % 60;
|
|
|
|
hours = minutes / 60;
|
|
|
|
hours = minutes / 60;
|
|
|
|
minutes = minutes % 60;
|
|
|
|
minutes = minutes % 60;
|
|
|
|
return String.format("Race clock: %02d:%02d:%02d", hours, minutes, remainingSeconds);
|
|
|
|
return String.format("Race clock: %02d:%02d:%02d", hours, minutes, remainingSeconds);
|
|
|
|
@ -205,13 +227,13 @@ 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 postion 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
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
protected void checkPosition(BoatInRace boat, long timeElapsed) {
|
|
|
|
protected void checkPosition(BoatInRace boat, long timeElapsed) {
|
|
|
|
if (boat.getDistanceTravelledInLeg() > boat.getCurrentLeg().getDistance()) {
|
|
|
|
if (boat.getDistanceTravelledInLeg() > boat.getCurrentLeg().getDistance()) {
|
|
|
|
// updateController();
|
|
|
|
|
|
|
|
//boat has passed onto new leg
|
|
|
|
//boat has passed onto new leg
|
|
|
|
if (boat.getCurrentLeg().getName().equals("Finish")) {
|
|
|
|
if (boat.getCurrentLeg().getName().equals("Finish")) {
|
|
|
|
//boat has finished
|
|
|
|
//boat has finished
|
|
|
|
@ -219,12 +241,15 @@ public abstract class Race implements Runnable {
|
|
|
|
boat.setFinished(true);
|
|
|
|
boat.setFinished(true);
|
|
|
|
boat.setTimeFinished(timeElapsed);
|
|
|
|
boat.setTimeFinished(timeElapsed);
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
|
|
|
|
//Calculate how much the boat overshot the marker by
|
|
|
|
boat.setDistanceTravelledInLeg(boat.getDistanceTravelledInLeg() - boat.getCurrentLeg().getDistance());
|
|
|
|
boat.setDistanceTravelledInLeg(boat.getDistanceTravelledInLeg() - boat.getCurrentLeg().getDistance());
|
|
|
|
|
|
|
|
//Move boat on to next leg
|
|
|
|
Leg nextLeg = legs.get(boat.getCurrentLeg().getLegNumber() + 1);
|
|
|
|
Leg nextLeg = legs.get(boat.getCurrentLeg().getLegNumber() + 1);
|
|
|
|
|
|
|
|
|
|
|
|
boat.setCurrentLeg(nextLeg);
|
|
|
|
boat.setCurrentLeg(nextLeg);
|
|
|
|
|
|
|
|
//Add overshoot distance into the distance travelled for the next leg
|
|
|
|
boat.setDistanceTravelledInLeg(boat.getDistanceTravelledInLeg());
|
|
|
|
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());
|
|
|
|
FXCollections.sort(startingBoats, (a, b) -> b.getCurrentLeg().getLegNumber() - a.getCurrentLeg().getLegNumber());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@ -238,6 +263,7 @@ public abstract class Race implements Runnable {
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Returns the boats that have started the race.
|
|
|
|
* Returns the boats that have started the race.
|
|
|
|
|
|
|
|
*
|
|
|
|
* @return ObservableList of BoatInRace class that participated in the race.
|
|
|
|
* @return ObservableList of BoatInRace class that participated in the race.
|
|
|
|
* @see ObservableList
|
|
|
|
* @see ObservableList
|
|
|
|
* @see BoatInRace
|
|
|
|
* @see BoatInRace
|
|
|
|
@ -246,12 +272,12 @@ public abstract class Race implements Runnable {
|
|
|
|
return startingBoats;
|
|
|
|
return startingBoats;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* This function is a function that generates the Race and populates the events list.
|
|
|
|
* Updates the boat's gps coordinates depending on time elapsed
|
|
|
|
* Is automatically called by the initialiser function, so that simulateRace() does not return an empty race.
|
|
|
|
* @param boat
|
|
|
|
* @see Race#simulateRace()
|
|
|
|
* @param millisecondsElapsed
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
protected abstract void updatePosition(BoatInRace boat, int millisecondsElapsed);
|
|
|
|
protected abstract void updatePosition(BoatInRace boat, int millisecondsElapsed);
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|