Sails command now listens to race to check if goal velocity is met

#story[1196]
This commit is contained in:
cbt24
2017-09-07 14:27:20 +12:00
parent b62344261a
commit eba70ab2d4
2 changed files with 21 additions and 27 deletions
@@ -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);
}
}
}