package seng302.Model; import javafx.application.Platform; import javafx.collections.FXCollections; import javafx.collections.ObservableList; import seng302.Controllers.RaceController; import java.util.ArrayList; /** * 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 = 100;//Integer.MAX_VALUE; //time in milliseconds to pause during pre-race private boolean timerEnabled = true; /** * 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(); } public void disableTimer() { timerEnabled = false; } /** * Initialises the boats, * Sets the boats' current to the first leg in the race */ private void preRace() { //show the boats participating. for (int i = 0; i < startingBoats.size(); i++) { if (startingBoats.get(i) != null) { startingBoats.get(i).setCurrentLeg(legs.get(0)); } } } /** * Prerace timer showing time until the race will begin, as a negative value */ 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 elapsed time in minutes and scales it, converts to hh:mm:ss format * @return String formatted race time, scaled */ protected String calcTimer() { long minutes; long currentTimeInSeconds; long remainingSeconds; long hours; currentTimeInSeconds = totalTimeElapsed / 1000; long scaledTimeInSeconds = currentTimeInSeconds * scaleFactor; minutes = scaledTimeInSeconds / 60; remainingSeconds = scaledTimeInSeconds % 60; hours = minutes / 60; minutes = minutes % 60; return String.format("Race clock: %02d:%02d:%02d", hours, minutes, remainingSeconds); } /** * Updates the GUI race clock * @param time */ protected void updateTime(String time) { Platform.runLater(() -> { controller.setTimer(time); }); } /** * 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() { long timeRaceStarted = System.currentTimeMillis(); long timeLoopStarted; long timeLoopEnded; while (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()); try { timeLoopEnded = System.currentTimeMillis(); Thread.sleep(SLEEP_TIME - (timeLoopEnded - timeLoopStarted)); } catch (InterruptedException e) { return; } } } /** * 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 { //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); }