diff --git a/racevisionGame/src/main/java/mock/model/MockBoat.java b/racevisionGame/src/main/java/mock/model/MockBoat.java index b6cd7a24..a5dc98fe 100644 --- a/racevisionGame/src/main/java/mock/model/MockBoat.java +++ b/racevisionGame/src/main/java/mock/model/MockBoat.java @@ -34,7 +34,10 @@ public class MockBoat extends Boat { */ private boolean autoVMG = false; - + /** + * Indicates whether boat velocity is determined by wind + */ + private boolean velocityDefault = true; /** * Constructs a boat object with a given sourceID, name, country/team abbreviation, and polars table. @@ -300,4 +303,12 @@ public class MockBoat extends Boat { public void setAutoVMG(boolean autoVMG) { this.autoVMG = autoVMG; } + + public boolean isVelocityDefault() { + return velocityDefault; + } + + public void setVelocityDefault(boolean velocityDefault) { + this.velocityDefault = velocityDefault; + } } diff --git a/racevisionGame/src/main/java/mock/model/MockRace.java b/racevisionGame/src/main/java/mock/model/MockRace.java index 60ed2498..2b689276 100644 --- a/racevisionGame/src/main/java/mock/model/MockRace.java +++ b/racevisionGame/src/main/java/mock/model/MockRace.java @@ -355,11 +355,11 @@ public class MockRace extends RaceState { //Checks if the current boat has finished the race or not. boolean finish = this.isLastLeg(boat.getCurrentLeg()); - if (!finish && totalElapsedMilliseconds >= updatePeriodMilliseconds && boat.isSailsOut()) { + if (!finish && totalElapsedMilliseconds >= updatePeriodMilliseconds) { checkPosition(boat, totalElapsedMilliseconds); - setBoatSpeed(boat); + if(boat.isVelocityDefault()) setBoatSpeed(boat); //Calculates the distance travelled, in meters, in the current timeslice. double distanceTravelledMeters = boat.calculateMetersTravelled(updatePeriodMilliseconds) * this.scaleFactor; @@ -373,8 +373,6 @@ public class MockRace extends RaceState { boat.setAutoVMG(false); } - } else { - boat.setCurrentSpeed(0); } this.updateEstimatedTime(boat); diff --git a/racevisionGame/src/main/java/mock/model/commandFactory/SailsCommand.java b/racevisionGame/src/main/java/mock/model/commandFactory/SailsCommand.java index efddc8df..e4a0bf4b 100644 --- a/racevisionGame/src/main/java/mock/model/commandFactory/SailsCommand.java +++ b/racevisionGame/src/main/java/mock/model/commandFactory/SailsCommand.java @@ -10,7 +10,6 @@ import java.util.Observable; public class SailsCommand extends ObserverCommand { private boolean sailsOut; private double goalVelocity; - private double acceleration = 1; public SailsCommand(MockRace race, MockBoat boat, boolean sailsOut) { super(race, boat); @@ -20,6 +19,8 @@ public class SailsCommand extends ObserverCommand { @Override public void execute() { this.boat.setSailsOut(this.sailsOut); + boat.setVelocityDefault(false); + if(sailsOut) { // Accelerate to VMG speed double polarSpeed = NewPolars.calculateSpeed(race.getWindDirection(), race.getWindSpeed(), boat.getBearing()); @@ -33,12 +34,18 @@ public class SailsCommand extends ObserverCommand { @Override public void update(Observable o, Object arg) { + double acceleration = 0.5; + if(sailsOut && boat.getCurrentSpeed() < goalVelocity) { - // Apply acceleration + boat.setCurrentSpeed(Math.min(goalVelocity, boat.getCurrentSpeed() + acceleration)); } else if (!sailsOut && boat.getCurrentSpeed() > goalVelocity) { - // Apply deceleration + // Apply deceleration to strictly 0 speed + boat.setCurrentSpeed(Math.max(0, boat.getCurrentSpeed() - acceleration)); } else { System.out.println(goalVelocity + " " + boat.getCurrentSpeed()); + + // Release boat from SailsCommand control + if(sailsOut) boat.setVelocityDefault(true); race.deleteObserver(this); } }