diff --git a/src/main/java/seng302/Constants.java b/src/main/java/seng302/Constants.java index 785448c2..afec2bcf 100644 --- a/src/main/java/seng302/Constants.java +++ b/src/main/java/seng302/Constants.java @@ -25,12 +25,12 @@ public class Constants { public static final GPSCoordinate finishLineMarker2 = new GPSCoordinate(32.317257, -64.836260); public static final BoatInRace[] OFFICIAL_AC35_COMPETITORS = new BoatInRace[] - {new BoatInRace("Oracle Team USA", 300.0, Color.BLUEVIOLET), - new BoatInRace("Land Rover BAR", 500.0, Color.BLACK), - new BoatInRace("SoftBank Team Japan", 400.0, Color.RED), - new BoatInRace("Groupama Team France", 350.0, Color.ORANGE), - new BoatInRace("Artemis Racing", 440.0, Color.DARKOLIVEGREEN), - new BoatInRace("Emirates Team New Zealand", 620, Color.LIMEGREEN)}; + {new BoatInRace("Oracle Team USA", 300.0, Color.BLUEVIOLET, "USA"), + new BoatInRace("Land Rover BAR", 500.0, Color.BLACK, "BAR"), + new BoatInRace("SoftBank Team Japan", 400.0, Color.RED, "JAP"), + new BoatInRace("Groupama Team France", 350.0, Color.ORANGE, "FRN"), + new BoatInRace("Artemis Racing", 440.0, Color.DARKOLIVEGREEN, "ART"), + new BoatInRace("Emirates Team New Zealand", 620, Color.LIMEGREEN, "ENZ")}; //public static final Leg bermudaCourseStartToMark1 = new Leg(0, , new ) } diff --git a/src/main/java/seng302/Controllers/RaceController.java b/src/main/java/seng302/Controllers/RaceController.java index 20b85b77..317d69da 100644 --- a/src/main/java/seng302/Controllers/RaceController.java +++ b/src/main/java/seng302/Controllers/RaceController.java @@ -2,7 +2,6 @@ package seng302.Controllers; import javafx.beans.property.ReadOnlyObjectWrapper; -import javafx.beans.property.StringProperty; import javafx.beans.value.ObservableValue; import javafx.collections.FXCollections; import javafx.collections.ObservableList; @@ -40,13 +39,15 @@ public class RaceController extends Controller{ Label timer; @FXML - TableView boatInfoTable; + TableView boatInfoTable; @FXML TableColumn boatPlacingColumn; @FXML TableColumn boatTeamColumn; @FXML TableColumn boatMarkColumn; + @FXML + TableColumn boatSpeedColumn; /** * updates the ResizableRaceCanvas (raceMap) with most recent data @@ -64,12 +65,13 @@ public class RaceController extends Controller{ * Updates the array listened by the TableView (boatInfoTable) that displays the boat information. * @param race Race to listen to. */ - public void updateInfoTable(Race race) { - boatInfoTable.getItems().clear(); + public void setInfoTable(Race race) { + //boatInfoTable.getItems().clear(); boatInfoTable.setItems(race.getStartingBoats()); - boatTeamColumn.setCellValueFactory(new PropertyValueFactory("Name")); - boatMarkColumn.setCellValueFactory(new PropertyValueFactory("CurrentLeg")); + boatTeamColumn.setCellValueFactory(cellData -> cellData.getValue().getName()); + boatSpeedColumn.setCellValueFactory(cellData -> cellData.getValue().getVelocityProp()); + boatMarkColumn.setCellValueFactory(cellData -> cellData.getValue().getCurrentLegName()); boatPlacingColumn.setCellValueFactory(new Callback, ObservableValue>() { @Override public ObservableValue call(TableColumn.CellDataFeatures table) { diff --git a/src/main/java/seng302/Model/Boat.java b/src/main/java/seng302/Model/Boat.java index 25d67520..4cd2503b 100644 --- a/src/main/java/seng302/Model/Boat.java +++ b/src/main/java/seng302/Model/Boat.java @@ -1,32 +1,45 @@ package seng302.Model; +import javafx.beans.property.DoubleProperty; +import javafx.beans.property.SimpleDoubleProperty; +import javafx.beans.property.SimpleStringProperty; +import javafx.beans.property.StringProperty; + import java.util.ArrayList; /** * Created by fwy13 on 3/03/17. */ public class Boat { - private String name; + private StringProperty name; private double velocity; + private StringProperty velocityProp; + private String abbrev; /** * Boat initialiser which keeps all of the information of the boat. * @param name Name of the Boat. * @param velocity Speed in m/s that the boat travels at. */ - public Boat(String name, double velocity){ + public Boat(String name, double velocity, String abbrev){ this.velocity = velocity; - this.name = name; + this.velocityProp = new SimpleStringProperty(String.valueOf(velocity)); + this.abbrev = abbrev; + this.name = new SimpleStringProperty(name); } /** * * @return The name of the boat */ - public String getName() { + public StringProperty getName() { return name; } + public void setName(String name) { + this.name.setValue(name); + } + /** * * @return returns the speed of the boat. @@ -40,7 +53,13 @@ public class Boat { * @return The Name of the boat. */ public String toString(){ - return getName(); + return getName().getValue(); } + public StringProperty getVelocityProp() { + return velocityProp; + } + + public String getAbbrev() { return abbrev; } + } diff --git a/src/main/java/seng302/Model/BoatInRace.java b/src/main/java/seng302/Model/BoatInRace.java index 0c278222..560feb21 100644 --- a/src/main/java/seng302/Model/BoatInRace.java +++ b/src/main/java/seng302/Model/BoatInRace.java @@ -1,5 +1,7 @@ package seng302.Model; +import javafx.beans.property.SimpleStringProperty; +import javafx.beans.property.StringProperty; import javafx.scene.paint.Color; import org.geotools.referencing.GeodeticCalculator; import seng302.GPSCoordinate; @@ -18,6 +20,7 @@ public class BoatInRace extends Boat { private long timeFinished; private Color colour; private boolean finished = false; + private StringProperty currentLegName; /** * Constructor method. @@ -25,9 +28,10 @@ public class BoatInRace extends Boat { * @param velocity Speed that the boat travels. * @param colour Colour the boat will be displayed as on the map */ - public BoatInRace(String name, double velocity, Color colour) { - super(name, velocity); + public BoatInRace(String name, double velocity, Color colour, String abbrev) { + super(name, velocity, abbrev); setColour(colour); + currentLegName = new SimpleStringProperty(""); } /** @@ -88,6 +92,11 @@ public class BoatInRace extends Boat { */ public void setCurrentLeg(Leg currentLeg) { this.currentLeg = currentLeg; + this.currentLegName.setValue(currentLeg.getName()); + } + + public StringProperty getCurrentLegName(){ + return currentLegName; } /** diff --git a/src/main/java/seng302/Model/Leg.java b/src/main/java/seng302/Model/Leg.java index 0d1f5649..84ee1529 100644 --- a/src/main/java/seng302/Model/Leg.java +++ b/src/main/java/seng302/Model/Leg.java @@ -34,8 +34,9 @@ public class Leg { * Construction Method * @param name Name of the Leg */ - public Leg(String name) { + public Leg(String name, int number) { this.name = name; + this.legNumber = number; } /** diff --git a/src/main/java/seng302/Model/Race.java b/src/main/java/seng302/Model/Race.java index 113ef4fe..7ae9f5c9 100644 --- a/src/main/java/seng302/Model/Race.java +++ b/src/main/java/seng302/Model/Race.java @@ -25,7 +25,8 @@ public abstract class Race implements Runnable { private int SLEEP_TIME = 100; //time in milliseconds to pause in a paced loop - private int PRERACE_TIME = 1000; //time in milliseconds to pause during pre-race + private int PRERACE_TIME = 10000;//Integer.MAX_VALUE; //time in milliseconds to pause during pre-race + private boolean timerEnabled = true; /** * Initailiser for Race @@ -35,7 +36,7 @@ public abstract class Race implements Runnable { public Race(BoatInRace[] boats, ArrayList legs, RaceController controller) { this.startingBoats = FXCollections.observableArrayList(boats); this.legs = legs; - this.legs.add(new Leg("Finish")); + this.legs.add(new Leg("Finish", this.legs.size())); this.controller = controller; } @@ -52,12 +53,16 @@ public abstract class Race implements Runnable { * Runnable for the thread. */ public void run() { - updateController(); + setControllerListeners(); preRace(); - countdownTimer(); + if(timerEnabled) countdownTimer(); simulateRace(); } + public void disableTimer() { + timerEnabled = false; + } + /** * Set up the state in waiting for the race starts. */ @@ -146,8 +151,8 @@ public abstract class Race implements Runnable { } } - controller.updateMap(startingBoats); - updateTime(calcTimer()); + if(controller != null) controller.updateMap(startingBoats); + if(timerEnabled) updateTime(calcTimer()); try { timeLoopEnded = System.currentTimeMillis(); Thread.sleep(SLEEP_TIME - (timeLoopEnded - timeLoopStarted)); @@ -178,14 +183,15 @@ public abstract class Race implements Runnable { boat.setCurrentLeg(nextLeg); boat.setDistanceTravelledInLeg(boat.getDistanceTravelledInLeg()); } + FXCollections.sort(startingBoats, (a,b) -> b.getCurrentLeg().getLegNumber() - a.getCurrentLeg().getLegNumber()); } } /** * Update call for the controller. */ - protected void updateController() { - if(controller != null) controller.updateInfoTable(this); + protected void setControllerListeners() { + if(controller != null) controller.setInfoTable(this); } /** diff --git a/src/main/java/seng302/Model/ResizableRaceCanvas.java b/src/main/java/seng302/Model/ResizableRaceCanvas.java index 46c23f5a..bcb44e1e 100644 --- a/src/main/java/seng302/Model/ResizableRaceCanvas.java +++ b/src/main/java/seng302/Model/ResizableRaceCanvas.java @@ -124,6 +124,17 @@ public class ResizableRaceCanvas extends Canvas { gc.setTransform(r.getMxx(), r.getMyx(), r.getMxy(), r.getMyy(), r.getTx(), r.getTy()); } + /** + * Display given name and speed of boat at a graph coordinate + * @param name name of the boat + * @param speed speed of the boat + * @param coordinate coordinate the text appears + */ + public void displayText(String name, double speed, GraphCoordinate coordinate){ + String text = name + ", " + speed + " knots"; + gc.fillText(text, coordinate.getX()+20, coordinate.getY()); + } + /** * Draws the Race Map */ @@ -133,7 +144,7 @@ public class ResizableRaceCanvas extends Canvas { double height = getHeight(); gc.clearRect(0, 0, width, height); - System.out.println("Race Map Canvas Width: "+ width + ", Height:" + height); + //System.out.println("Race Map Canvas Width: "+ width + ", Height:" + height); this.map = new RaceMap(32.278, -64.863, 32.320989, -64.821, (int)width, (int)height); if (map == null){ @@ -166,6 +177,7 @@ public class ResizableRaceCanvas extends Canvas { if (boat != null) { // System.out.print("Drawing Boat At: " + boat.getCurrentPosition()); displayMark(this.map.convertGPS(boat.getCurrentPosition()), boat.getColour()); + displayText(boat.getAbbrev(), boat.getVelocity(), this.map.convertGPS(boat.getCurrentPosition())); } } } diff --git a/src/main/resources/scenes/racepane.fxml b/src/main/resources/scenes/racepane.fxml index d699ff46..ed538a38 100644 --- a/src/main/resources/scenes/racepane.fxml +++ b/src/main/resources/scenes/racepane.fxml @@ -20,6 +20,7 @@ + diff --git a/src/test/java/seng302/Model/RaceTest.java b/src/test/java/seng302/Model/RaceTest.java index 0ead45f7..f448d092 100644 --- a/src/test/java/seng302/Model/RaceTest.java +++ b/src/test/java/seng302/Model/RaceTest.java @@ -1,5 +1,8 @@ package seng302.Model; +import javafx.collections.FXCollections; +import javafx.collections.ObservableList; +import javafx.scene.paint.Color; import org.junit.Ignore; import org.junit.Test; import seng302.GPSCoordinate; @@ -8,6 +11,8 @@ import seng302.Model.ConstantVelocityRace; import seng302.Model.Leg; import java.util.ArrayList; +import java.util.Arrays; +import java.util.Observable; import static org.junit.Assert.assertTrue; @@ -41,4 +46,24 @@ public class RaceTest { // ConstantVelocityRace race = new ConstantVelocityRace(boats, legs); // race.run(); // } + + @Test + public void finishOrderDeterminedByVelocity() { + BoatInRace[] boats = { + new BoatInRace("NZ", 2000, Color.BEIGE, "NZ"), + new BoatInRace("AU", 2800, Color.BEIGE, "AU") + }; + ArrayList legs = new ArrayList<>(); + legs.add(new Leg("Start", new GPSCoordinate(32.296577, -64.854304),new GPSCoordinate(32.293039, -64.843983),0)); + legs.add(new Leg("Start", new GPSCoordinate(32.293039, -64.843983),new GPSCoordinate(32.284680, -64.850045),1)); + Race race = new ConstantVelocityRace(boats, legs); + race.disableTimer(); + + // Boats should finish in an order determined by their velocity + Arrays.sort(boats, (a,b) -> (int)(b.getVelocity()-a.getVelocity())); + race.run(); + + for(int i = 0; i < boats.length; i++) + assertTrue(boats[i].equals(race.getStartingBoats().get(i))); + } }