Changed it so Boat can be used as a superclass for StreamedBoat and BoatInRace properly and removed ConstantVelocityRace. Everything that used to use BoatInRace or StreamedBoat now uses it superclass (boat). #pair[jjg64, cbt24] #refactor #story[782]

This commit is contained in:
Joseph Gardner
2017-04-28 12:35:00 +12:00
parent 3d284c4a2b
commit f377cf6471
16 changed files with 572 additions and 498 deletions
@@ -5,6 +5,7 @@ import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import seng302.Model.Boat;
import seng302.XMLReader;
import javax.xml.parsers.ParserConfigurationException;
@@ -153,4 +154,8 @@ public class BoatXMLReader extends XMLReader {
public Map<Integer, StreamedBoat> getStreamedBoatMap() {
return streamedBoatMap;
}
public List<Boat> getBoats() {
return new ArrayList<>(streamedBoatMap.values());
}
}
@@ -11,26 +11,22 @@ import seng302.Model.Marker;
/**
* Created by Joseph on 24/04/2017.
*/
public class StreamedBoat extends BoatInRace {
public class StreamedBoat extends Boat {
private int sourceID;
private boolean dnf = false;
private void init() {
this.velocity = 0;
this.scaledVelocity = 0;
this.heading = 0;
this.setCurrentLeg(new Leg("None", -1));
this.setCurrentPosition(new GPSCoordinate(0, 0));
}
public StreamedBoat(int sourceID, String name, Color colour, String abbrev) {
super(name, colour, abbrev);
super(name, abbrev);
this.sourceID = sourceID;
this.init();
}
public StreamedBoat(int sourceID) {
super("None", Color.BLACK, "None");
super("None", "None");
this.sourceID = sourceID;
this.init();
}
@@ -49,12 +45,4 @@ public class StreamedBoat extends BoatInRace {
}
return azimuth;
}
public boolean isDnf() {
return dnf;
}
public void setDnf(boolean dnf) {
this.dnf = dnf;
}
}
@@ -1,10 +1,7 @@
package seng302.Mock;
import seng302.GPSCoordinate;
import seng302.Model.BoatInRace;
import seng302.Model.Leg;
import seng302.Model.Marker;
import seng302.Model.RaceClock;
import seng302.Model.*;
import seng302.RaceDataSource;
import java.time.ZonedDateTime;
@@ -51,8 +48,8 @@ public class StreamedCourse implements RaceDataSource {
return regattaXMLReader;
}
public List<BoatInRace> getBoats() {
return new ArrayList<>(boatXMLReader.getStreamedBoatMap().values());
public List<Boat> getBoats() {
return boatXMLReader.getBoats();
}
public List<Leg> getLegs() {
@@ -29,12 +29,11 @@ public class StreamedRace extends Race {
Marker endMarker = officialStart.getEndMarker();
for (int i = 0; i < startingBoats.size(); i++) {
BoatInRace boat = startingBoats.get(i);
Boat boat = startingBoats.get(i);
if (boat != null) {
Leg startLeg = new Leg(name, 0);
startLeg.setEndMarker(endMarker);
boat.setCurrentLeg(startLeg);
boat.setHeading(boat.calculateHeading());
}
}
}
@@ -55,18 +54,12 @@ public class StreamedRace extends Race {
* @param timeElapsed Time that has elapse since the start of the the race.
* @see BoatInRace
*/
protected void checkPosition(StreamedBoat boat, long timeElapsed) {
protected void checkPosition(Boat 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();
@@ -78,64 +71,13 @@ public class StreamedRace extends Race {
* @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 updatePosition(Boat boat, int millisecondsElapsed) {
//TODO Grab info from network
// setPostiion(boat, coordinate);
}
protected void setPostion(StreamedBoat boat, GPSCoordinate coordinate) {
protected void setPostion(Boat 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){
}
}