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 seng302.Controllers.RaceController; import seng302.GPSCoordinate; 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 startingBoats; protected ArrayList legs; protected RaceController controller; protected int boatsFinished = 0; protected long totalTimeElapsed; 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 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.legs = legs; this.legs.add(new Leg("Finish", this.legs.size())); this.controller = controller; this.scaleFactor = scaleFactor; } /** * 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 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; } /** * Runnable for the thread. */ public void run() { setControllerListeners(); preRace(); if(timerEnabled) countdownTimer(); simulateRace(); } /** * Disable the timer */ public void disableTimer() { timerEnabled = false; } /** * Set up the state in waiting for the race starts. */ private void preRace() { //show the boats participating. System.out.println("Boats Participating:"); System.out.println("===================="); for (int i = 0; i < startingBoats.size(); i++) { 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)); } } } /** * 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);}); ======= * Updates the GUI race clock * @param time */ protected void updateTime(String time) { Platform.runLater(() -> { controller.setTimer(time); }); >>>>>>> Temporary merge branch 2 } /** * 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"); 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, SLEEP_TIME); 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); 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(); } /** * 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)); } 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 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); }