From eba70ab2d4d00e60510bc3e38b7e5f2b4fdc2bef Mon Sep 17 00:00:00 2001 From: cbt24 Date: Thu, 7 Sep 2017 14:27:20 +1200 Subject: [PATCH] Sails command now listens to race to check if goal velocity is met #story[1196] --- .../src/main/java/mock/model/MockRace.java | 26 +------------------ .../model/commandFactory/SailsCommand.java | 22 ++++++++++++++-- 2 files changed, 21 insertions(+), 27 deletions(-) diff --git a/racevisionGame/src/main/java/mock/model/MockRace.java b/racevisionGame/src/main/java/mock/model/MockRace.java index a338cd5d..60ed2498 100644 --- a/racevisionGame/src/main/java/mock/model/MockRace.java +++ b/racevisionGame/src/main/java/mock/model/MockRace.java @@ -362,11 +362,7 @@ public class MockRace extends RaceState { setBoatSpeed(boat); //Calculates the distance travelled, in meters, in the current timeslice. - double distanceTravelledMeters = boat.calculateMetersTravelled(updatePeriodMilliseconds); - - //Scale it. - distanceTravelledMeters = distanceTravelledMeters * this.scaleFactor; - + double distanceTravelledMeters = boat.calculateMetersTravelled(updatePeriodMilliseconds) * this.scaleFactor; //Move the boat forwards that many meters, and advances its time counters by enough milliseconds. boat.moveForwards(distanceTravelledMeters); @@ -388,32 +384,12 @@ public class MockRace extends RaceState { private void newOptimalVMG(MockBoat boat) { long tackPeriod = 1000; if (boat.getTimeSinceTackChange() > tackPeriod) { - //System.out.println("optim called"); - //Calculate the new VMG. -// VMG newVMG = boat.getPolars().calculateVMG( -// this.getWindDirection(), -// this.getWindSpeed(), -// boat.calculateBearingToNextMarker(), -// Bearing.fromDegrees(0d), -// Bearing.fromDegrees(359.99999d)); - VMG newVMG = NewPolars.setBestVMG(this.getWindDirection(), this.getWindSpeed(), boat.getBearing()); - //System.out.println(newVMG); - //If the new vmg improves velocity, use it. - /*if (improvesVelocity(boat, newVMG)) { - }*/ boat.setVMG(newVMG); } } private void setBoatSpeed(MockBoat boat) { -// VMG vmg = boat.getPolars().calculateVMG( -// this.getWindDirection(), -// this.getWindSpeed(), -// boat.getBearing(), -// Bearing.fromDegrees(boat.getBearing().degrees() - 1), -// Bearing.fromDegrees(boat.getBearing().degrees() + 1)); - //VMG vmg = boat.getPolars().setBestVMG(this.getWindDirection(), this.getWindSpeed(), boat.getBearing()); VMG vmg = new VMG(NewPolars.calculateSpeed( this.getWindDirection(), this.getWindSpeed(), diff --git a/racevisionGame/src/main/java/mock/model/commandFactory/SailsCommand.java b/racevisionGame/src/main/java/mock/model/commandFactory/SailsCommand.java index 60ebb584..efddc8df 100644 --- a/racevisionGame/src/main/java/mock/model/commandFactory/SailsCommand.java +++ b/racevisionGame/src/main/java/mock/model/commandFactory/SailsCommand.java @@ -2,12 +2,15 @@ package mock.model.commandFactory; import mock.model.MockBoat; import mock.model.MockRace; +import mock.model.NewPolars; +import mock.model.VMG; 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); @@ -17,11 +20,26 @@ public class SailsCommand extends ObserverCommand { @Override public void execute() { this.boat.setSailsOut(this.sailsOut); - + if(sailsOut) { + // Accelerate to VMG speed + double polarSpeed = NewPolars.calculateSpeed(race.getWindDirection(), race.getWindSpeed(), boat.getBearing()); + VMG vmg = new VMG(polarSpeed, boat.getBearing()); + goalVelocity = vmg.getSpeed(); + } else { + // Decelerate to 0 + goalVelocity = 0; + } } @Override public void update(Observable o, Object arg) { - + if(sailsOut && boat.getCurrentSpeed() < goalVelocity) { + // Apply acceleration + } else if (!sailsOut && boat.getCurrentSpeed() > goalVelocity) { + // Apply deceleration + } else { + System.out.println(goalVelocity + " " + boat.getCurrentSpeed()); + race.deleteObserver(this); + } } }