package seng302.Mock; import javafx.collections.FXCollections; import org.geotools.referencing.GeodeticCalculator; import seng302.Constants; import seng302.Controllers.RaceController; import seng302.GPSCoordinate; import seng302.Model.*; import seng302.RaceDataSource; import java.awt.geom.Point2D; import java.util.*; /** * Created by jjg64 on 21/04/17. */ public class StreamedRace extends Race { private RaceDataSource raceData; public StreamedRace(RaceDataSource raceData, RaceController controller, int scaleFactor) { super(raceData.getBoats(), raceData.getLegs(), controller, scaleFactor); this.raceData = raceData; this.scaleFactor = 1; // There will be no scaling in a live streamed race } public void initialiseBoats() { Leg officialStart = legs.get(0); String name = officialStart.getName(); Marker endMarker = officialStart.getEndMarker(); for (int i = 0; i < startingBoats.size(); i++) { BoatInRace boat = startingBoats.get(i); if (boat != null) { Leg startLeg = new Leg(name, 0); startLeg.setEndMarker(endMarker); boat.setCurrentLeg(startLeg); boat.setHeading(boat.calculateHeading()); } } } /** * Checks if the boat cannot finish the race * @return True if boat cannot finish the race */ protected boolean doNotFinish() { // DNF is no longer random and is now determined by a dnf packet return false; } /** * 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(StreamedBoat boat, long timeElapsed) { if (boat.getCurrentLeg().getName().toLowerCase().contains("finish")) { //boat has finished boatsFinished++; boat.setFinished(true); boat.setTimeFinished(timeElapsed); } else if (boat.isDnf()) { boatsFinished++; boat.setFinished(true); boat.setCurrentLeg(new Leg("DNF", -1)); boat.setVelocity(0); boat.setScaledVelocity(0); } //Update the boat display table in the GUI to reflect the leg change updatePositions(); } /** * Updates the boat's gps coordinates * * @param boat to be updated * @param millisecondsElapsed time since last update */ protected void updatePosition(StreamedBoat boat, int millisecondsElapsed) { //distanceTravelled = velocity (nm p hr) * time taken to update loop double distanceTravelled = (boat.getVelocity() * millisecondsElapsed) / 3600000; boolean finish = boat.getCurrentLeg().getName().toLowerCase().contains("finish"); if (!finish) { //Calculate boat's new position by adding the distance travelled onto the start point of the leg boat.setCurrentPosition(calculatePosition(boat.getCurrentPosition(), distanceTravelled, boat.calculateAzimuth())); } } protected void setPostion(StreamedBoat boat, GPSCoordinate coordinate) { boat.setCurrentPosition(coordinate); } /** * Calculates the boats next GPS position based on its distance travelled and heading * * @param oldCoordinates GPS coordinates of the boat's starting position * @param distanceTravelled distance in nautical miles * @param azimuth boat's current direction. Value between -180 and 180 * @return The boat's new coordinate */ public static GPSCoordinate calculatePosition(GPSCoordinate oldCoordinates, double distanceTravelled, double azimuth) { //Find new coordinate using current heading and distance GeodeticCalculator geodeticCalculator = new GeodeticCalculator(); //Load start point into calculator Point2D startPoint = new Point2D.Double(oldCoordinates.getLongitude(), oldCoordinates.getLatitude()); geodeticCalculator.setStartingGeographicPoint(startPoint); //load direction and distance travelled into calculator geodeticCalculator.setDirection(azimuth, distanceTravelled * Constants.NMToMetersConversion); //get new point Point2D endPoint = geodeticCalculator.getDestinationGeographicPoint(); return new GPSCoordinate(endPoint.getY(), endPoint.getX()); } /** * Checks the position of the boat, this updates the boats current position. * @deprecated * @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) { } /** * Updates the boat's gps coordinates * @deprecated * @param boat to be updated * @param millisecondsElapsed time since last update */ protected void updatePosition(BoatInRace boat, int millisecondsElapsed){ } }