package seng302.model; import javafx.beans.property.ReadOnlyDoubleProperty; import javafx.beans.property.ReadOnlyDoubleWrapper; import javafx.beans.property.ReadOnlyLongProperty; import javafx.beans.property.ReadOnlyLongWrapper; import javafx.scene.paint.Color; import javafx.scene.paint.Paint; import seng302.model.mark.Mark; import static seng302.utilities.GeoUtility.getGeoCoordinate; import java.text.DateFormat; import java.text.SimpleDateFormat; import seng302.gameServer.GameState; import seng302.utilities.GeoPoint; /** * Yacht class for the racing boat. * * Class created to store more variables (eg. boat statuses) compared to the XMLParser boat class, * also done outside Boat class because some old variables are not used anymore. */ public class Yacht { //BOTH AFAIK private String boatType; private Integer sourceId; private String hullID; //matches HullNum in the XML spec. private String shortName; private String boatName; private String country; private Long estimateTimeAtFinish; private Long timeTillNext; private Long markRoundTime; private Double heading; private Double lat; private Double lon; private Integer legNumber = 0; //SERVER SIDE private final Double TURN_STEP = 5.0; private Double lastHeading; private Boolean sailIn; private String position; private GeoPoint location; private Integer boatStatus; private Double velocity; //CLIENT SIDE private ReadOnlyDoubleWrapper velocityProperty = new ReadOnlyDoubleWrapper(); private ReadOnlyLongWrapper timeTillNextProperty = new ReadOnlyLongWrapper(); private ReadOnlyLongWrapper timeSinceLastMarkProperty = new ReadOnlyLongWrapper(); private ReadOnlyDoubleProperty headingProperty = new ReadOnlyDoubleWrapper(); private Mark lastMarkRounded; private Mark nextMark; private Integer positionInt = 0; private Color colour; /** * @param location latlon location of the boat stored in a geopoint * @param heading heading of the boat in degrees from 0 to 365 with 0 being north */ public Yacht(GeoPoint location, Double heading) { this.location = location; this.heading = heading; this.velocity = 0.0; this.sailIn = false; } /** * Used in EventTest and RaceTest. * * @param boatName Create a yacht object with name. */ public Yacht(String boatName, String shortName, GeoPoint location, Double heading) { this.boatName = boatName; this.shortName = shortName; this.location = location; this.heading = heading; this.velocity = 0.0; this.sailIn = false; } /** * Used in BoatGroupTest. * * @param boatName The name of the team sailing the boat * @param boatVelocity The speed of the boat in meters/second * @param shortName A shorter version of the teams name */ public Yacht(String boatName, double boatVelocity, String shortName, int id) { this.boatName = boatName; this.velocity = boatVelocity; this.shortName = shortName; this.sourceId = id; this.sailIn = false; } public Yacht(String boatType, Integer sourceId, String hullID, String shortName, String boatName, String country) { this.boatType = boatType; this.sourceId = sourceId; this.hullID = hullID; this.shortName = shortName; this.boatName = boatName; this.country = country; this.position = "-"; this.sailIn = false; this.location = new GeoPoint(57.670341, 11.826856); this.heading = 120.0; this.velocity = 50000.0; } /** * @param timeInterval since last update in milliseconds */ public void update(Long timeInterval) { if (sailIn) { Double secondsElapsed = timeInterval / 1000000.0; Double thisHeading = ((double) Math.floorMod(heading.longValue(), 360L)); Double windSpeedKnots = 0d; Double boatSpeedInKnots = PolarTable.getBoatSpeed(windSpeedKnots, thisHeading); velocity = boatSpeedInKnots / 1.94384449 * 3000; // TODO: 25/07/17 cir27 - remove magic numbers //System.out.println("velocity = " + velocity); Double metersCovered = velocity * secondsElapsed; location = getGeoCoordinate(location, heading, metersCovered); } } public void adjustHeading(Double amount) { lastHeading = heading; // TODO: 24/07/17 wmu16 - '%' in java does remainder, we need modulo. All this must be changed here, this is why we have neg values! heading = (heading + amount) % 360.0; } public void tackGybe(Double windDirection) { adjustHeading(-2 * ((heading - windDirection) % 360)); } public void toggleSailIn() { sailIn = !sailIn; } public void turnUpwind() { Double normalizedHeading = (heading - GameState.windDirection) % 360; if (normalizedHeading == 0) { if (lastHeading < 180) { adjustHeading(-TURN_STEP); } else { adjustHeading(TURN_STEP); } } else if (normalizedHeading == 180) { if (lastHeading < 180) { adjustHeading(TURN_STEP); } else { adjustHeading(-TURN_STEP); } } else if (normalizedHeading < 180) { adjustHeading(-TURN_STEP); } else { adjustHeading(TURN_STEP); } } public void turnDownwind() { Double normalizedHeading = (heading - GameState.windDirection) % 360; if (normalizedHeading == 0) { if (lastHeading < 180) { adjustHeading(TURN_STEP); } else { adjustHeading(-TURN_STEP); } } else if (normalizedHeading == 180) { if (lastHeading < 180) { adjustHeading(-TURN_STEP); } else { adjustHeading(TURN_STEP); } } else if (normalizedHeading < 180) { adjustHeading(TURN_STEP); } else { adjustHeading(-TURN_STEP); } } public String getBoatType() { return boatType; } public Integer getSourceId() { //@TODO Remove and merge with Creating Game Loop if (sourceId == null) return 0; return sourceId; } public String getHullID() { if (hullID == null) return ""; return hullID; } public String getShortName() { return shortName; } public String getBoatName() { return boatName; } public String getCountry() { if (country == null) return ""; return country; } public Integer getBoatStatus() { return boatStatus; } public void setBoatStatus(Integer boatStatus) { this.boatStatus = boatStatus; } public Integer getLegNumber() { return legNumber; } public void setLegNumber(Integer legNumber) { // if (colour != null && position != "-" && legNumber != this.legNumber) { // RaceViewController.updateYachtPositionSparkline(this, legNumber); // } this.legNumber = legNumber; } public void setEstimateTimeTillNextMark(Long estimateTimeTillNextMark) { timeTillNext = estimateTimeTillNextMark; } public String getEstimateTimeAtFinish() { DateFormat format = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss"); return format.format(estimateTimeAtFinish); } public void setEstimateTimeAtFinish(Long estimateTimeAtFinish) { this.estimateTimeAtFinish = estimateTimeAtFinish; } public Integer getPositionInteger() { return positionInt; } public void setPositionInteger(Integer position) { this.positionInt = position; } public void setVelocityProperty(double velocity) { this.velocityProperty.set(velocity); } public void setMarkRoundingTime(Long markRoundingTime) { this.markRoundTime = markRoundingTime; } public ReadOnlyDoubleProperty getVelocityProperty() { return velocityProperty.getReadOnlyProperty(); } public ReadOnlyLongProperty timeTillNextProperty() { return timeTillNextProperty.getReadOnlyProperty(); } public Long getMarkRoundTime() { return markRoundTime; } public Mark getLastMarkRounded() { return lastMarkRounded; } public void setLastMarkRounded(Mark lastMarkRounded) { this.lastMarkRounded = lastMarkRounded; } public void setNextMark(Mark nextMark) { this.nextMark = nextMark; } public Mark getNextMark(){ return nextMark; } public Double getLat() { return lat; } public void setLat(Double lat) { this.lat = lat; } public Double getLon() { return lon; } public void setLon(Double lon) { this.lon = lon; } public Double getHeading() { return heading; } public void setHeading(Double heading) { this.heading = heading; } public Boolean getSailIn() { return sailIn; } @Override public String toString() { return boatName; } public GeoPoint getLocation() { return location; } public void updateTimeSinceLastMarkProperty(long timeSinceLastMark) { this.timeSinceLastMarkProperty.set(timeSinceLastMark); } public ReadOnlyLongProperty timeSinceLastMarkProperty () { return timeSinceLastMarkProperty.getReadOnlyProperty(); } public Long getTimeTillNext() { return timeTillNext; } public void setTimeTillNext(Long timeTillNext) { this.timeTillNext = timeTillNext; } public Color getColour() { return colour; } public void setColour(Color colour) { this.colour = colour; } public Double getVelocity() { return velocity; } public void setVelocity(Double velocity) { this.velocity = velocity; } }