diff --git a/visualiser/src/main/java/seng302/Controllers/RaceController.java b/visualiser/src/main/java/seng302/Controllers/RaceController.java index 1657a671..5fab8a44 100644 --- a/visualiser/src/main/java/seng302/Controllers/RaceController.java +++ b/visualiser/src/main/java/seng302/Controllers/RaceController.java @@ -61,7 +61,7 @@ public class RaceController extends Controller { * * @param race Race to listen to. */ - public void setInfoTable(Race race) { + public void setInfoTable(StreamedRace race) { //boatInfoTable.getItems().clear(); ObservableList startingBoats = race.getStartingBoats(); boatInfoTable.setItems(race.getStartingBoats()); diff --git a/visualiser/src/main/java/seng302/Mock/StreamedRace.java b/visualiser/src/main/java/seng302/Mock/StreamedRace.java index 5c06ddc1..3ad7768b 100644 --- a/visualiser/src/main/java/seng302/Mock/StreamedRace.java +++ b/visualiser/src/main/java/seng302/Mock/StreamedRace.java @@ -1,24 +1,48 @@ package seng302.Mock; +import javafx.animation.AnimationTimer; +import javafx.application.Platform; +import javafx.collections.FXCollections; +import javafx.collections.ObservableList; +import seng302.Controllers.FinishController; import seng302.Controllers.RaceController; import seng302.GPSCoordinate; import seng302.Model.Boat; import seng302.Model.Leg; import seng302.Model.Marker; -import seng302.Model.Race; import seng302.Networking.Messages.BoatLocation; import seng302.Networking.Messages.BoatStatus; import seng302.Networking.Messages.Enums.BoatStatusEnum; import seng302.VisualiserInput; +import java.util.List; + /** * Created by jjg64 on 21/04/17. */ -public class StreamedRace extends Race { +public class StreamedRace implements Runnable { private final VisualiserInput visualiserInput; + protected final ObservableList startingBoats; + protected final ObservableList boatMarkers; + protected final List legs; + private RaceController controller; + protected FinishController finishController; + protected int boatsFinished = 0; + private long totalTimeElapsed; + + private int lastFPS = 20; public StreamedRace(VisualiserInput visualiserInput, RaceController controller) { - super(visualiserInput.getCourse(), controller, 1); + StreamedCourse course = visualiserInput.getCourse(); + + this.startingBoats = FXCollections.observableArrayList(course.getBoats()); + this.boatMarkers = FXCollections.observableArrayList(course.getMarkers()); + this.legs = course.getLegs(); + this.legs.add(new Leg("Finish", this.legs.size())); + this.controller = controller; + if (startingBoats != null && startingBoats.size() > 0) { + initialiseBoats(); + } this.visualiserInput = visualiserInput; } @@ -128,4 +152,116 @@ public class StreamedRace extends Race { boat.setCurrentPosition(coordinate); } + + public void setController(RaceController controller) { + this.controller = controller; + } + + + /** + * Runnable for the thread. + */ + public void run() { + setControllerListeners(); + initialiseBoats(); + simulateRace(); + } + + + /** + * 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)); + } + + /** + * 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 (Boat boat : startingBoats) { + boat.setStarted(true); + } + + new AnimationTimer() { + + final 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) { + if (boatsFinished < startingBoats.size()) { + boatsFinished = 0; + totalTimeElapsed = System.currentTimeMillis() - timeRaceStarted; + + for (Boat boat : startingBoats) { + if (boat != null && !boat.isFinished()) { + updatePosition(boat, Math.round(1000 / lastFPS) > 20 ? 15 : Math.round(1000 / lastFPS)); + checkPosition(boat, totalTimeElapsed); + } + if (boat.isFinished()){ + boatsFinished++; + } + } + for (Marker mark: boatMarkers){ + if (mark != null){ + updateMarker(mark); + } + } + //System.out.println(boatsFinished + ":" + startingBoats.size()); + } else { + controller.finishRace(startingBoats); + stop(); + } + controller.updateMap(startingBoats, boatMarkers); + fps++; + if ((System.currentTimeMillis() - timeCurrent) > 1000) { + updateFPS(fps); + lastFPS = fps; + fps = 0; + timeCurrent = System.currentTimeMillis(); + } + } + }.start(); + } + + /** + * Update position of boats in race, no position if on starting leg or DNF. + */ + protected void updatePositions() { + FXCollections.sort(startingBoats, (a, b) -> b.getCurrentLeg().getLegNumber() - a.getCurrentLeg().getLegNumber()); + for(Boat boat: startingBoats) { + if(boat != null) { + boat.setPosition(Integer.toString(startingBoats.indexOf(boat) + 1)); + if (boat.isDnf() || !boat.isStarted() || boat.getCurrentLeg().getLegNumber() < 0) + boat.setPosition("-"); + } + } + } + + /** + * Update call for the controller. + */ + private void setControllerListeners() { + if (controller != null) controller.setInfoTable(this); + } + + /** + * Returns the boats that have started the race. + * + * @return ObservableList of Boat class that participated in the race. + * @see ObservableList + * @see Boat + */ + public ObservableList getStartingBoats() { + return startingBoats; + } + } diff --git a/visualiser/src/main/java/seng302/Model/Race.java b/visualiser/src/main/java/seng302/Model/Race.java deleted file mode 100644 index 6d6ca022..00000000 --- a/visualiser/src/main/java/seng302/Model/Race.java +++ /dev/null @@ -1,200 +0,0 @@ -package seng302.Model; - -import javafx.animation.AnimationTimer; -import javafx.application.Platform; -import javafx.collections.FXCollections; -import javafx.collections.ObservableList; -import seng302.Controllers.FinishController; -import seng302.Controllers.RaceController; -import seng302.RaceDataSource; - -import java.util.Arrays; -import java.util.List; - -/** - * Parent class for races - * Created by fwy13 on 3/03/17. - */ -public abstract class Race implements Runnable { - protected final ObservableList startingBoats; - protected final ObservableList boatMarkers; - protected final List legs; - private RaceController controller; - protected FinishController finishController; - protected int boatsFinished = 0; - private long totalTimeElapsed; - - private int lastFPS = 20; - - /** - * Initializer 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. - * @param controller race controller - * @param scaleFactor for race - */ - private Race(List boats, List boatMarkers, List legs, RaceController controller, int scaleFactor) { - - this.startingBoats = FXCollections.observableArrayList(boats); - this.boatMarkers = FXCollections.observableArrayList(boatMarkers); - this.legs = legs; - this.legs.add(new Leg("Finish", this.legs.size())); - this.controller = controller; - if (startingBoats != null && startingBoats.size() > 0) { - initialiseBoats(); - } - } - - protected Race(RaceDataSource raceData, RaceController controller, int scaleFactor) { - this(raceData.getBoats(), raceData.getMarkers(), raceData.getLegs(), controller, scaleFactor); - } - - /** - * @deprecated - * @param startingBoats boats starting the race - * @param legs legs in race - * @param controller controller for the race - * @param scaleFactor factor to scale by - */ - public Race(Boat[] startingBoats, List legs, RaceController controller, int scaleFactor) { - this(Arrays.asList(startingBoats), null, legs, controller, scaleFactor); - } - - public void setController(RaceController controller) { - this.controller = controller; - } - - protected abstract void initialiseBoats(); - - /** - * Checks if the boat cannot finish the race - * @return True if boat cannot finish the race - */ - protected abstract boolean doNotFinish(); - - /** - * Checks the position of the boat, this updates the boats current position. - * - * @param boat Boat that the position is to be updated for. - * @param timeElapsed Time that has elapse since the start of the the race. - * @see Boat - */ - protected abstract void checkPosition(Boat boat, long timeElapsed); - - protected abstract void updateMarker(Marker mark); - - /** - * Updates the boat's gps coordinates - * - * @param boat to be updated - * @param millisecondsElapsed time since last update - */ - protected abstract void updatePosition(Boat boat, int millisecondsElapsed); - - /** - * Runnable for the thread. - */ - public void run() { - setControllerListeners(); - initialiseBoats(); - simulateRace(); - } - - - /** - * 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)); - } - - /** - * 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 (Boat boat : startingBoats) { - boat.setStarted(true); - } - - new AnimationTimer() { - - final 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) { - if (boatsFinished < startingBoats.size()) { - boatsFinished = 0; - totalTimeElapsed = System.currentTimeMillis() - timeRaceStarted; - - for (Boat boat : startingBoats) { - if (boat != null && !boat.isFinished()) { - updatePosition(boat, Math.round(1000 / lastFPS) > 20 ? 15 : Math.round(1000 / lastFPS)); - checkPosition(boat, totalTimeElapsed); - } - if (boat.isFinished()){ - boatsFinished++; - } - } - for (Marker mark: boatMarkers){ - if (mark != null){ - updateMarker(mark); - } - } - //System.out.println(boatsFinished + ":" + startingBoats.size()); - } else { - controller.finishRace(startingBoats); - stop(); - } - controller.updateMap(startingBoats, boatMarkers); - fps++; - if ((System.currentTimeMillis() - timeCurrent) > 1000) { - updateFPS(fps); - lastFPS = fps; - fps = 0; - timeCurrent = System.currentTimeMillis(); - } - } - }.start(); - } - - /** - * Update position of boats in race, no position if on starting leg or DNF. - */ - protected void updatePositions() { - FXCollections.sort(startingBoats, (a, b) -> b.getCurrentLeg().getLegNumber() - a.getCurrentLeg().getLegNumber()); - for(Boat boat: startingBoats) { - if(boat != null) { - boat.setPosition(Integer.toString(startingBoats.indexOf(boat) + 1)); - if (boat.isDnf() || !boat.isStarted() || boat.getCurrentLeg().getLegNumber() < 0) - boat.setPosition("-"); - } - } - } - - /** - * Update call for the controller. - */ - private void setControllerListeners() { - if (controller != null) controller.setInfoTable(this); - } - - /** - * Returns the boats that have started the race. - * - * @return ObservableList of Boat class that participated in the race. - * @see ObservableList - * @see Boat - */ - public ObservableList getStartingBoats() { - return startingBoats; - } -}