Implemented runCommand method in VMGCommand and TackGybeCommand classes. Both classes currently rely on the MockRace class so changes will be required when MockRace is refactored.

#story[1097]
main
zwu18 8 years ago
parent 7fc1347377
commit ad61dc6bce

@ -66,7 +66,6 @@ public class MockBoat extends Boat {
//Calculate bearing.
Bearing bearing = GPSCoordinate.calculateBearing(currentPosition, nextMarkerPosition);
return bearing;
}

@ -383,7 +383,6 @@ public class MockRace extends Race {
//Update race time.
updateRaceTime(currentTime);
//As long as there is at least one boat racing, we still simulate the race.
if (getNumberOfActiveBoats() != 0) {
@ -392,7 +391,6 @@ public class MockRace extends Race {
//For each boat, we update its position, and generate a BoatLocationMessage.
for (MockBoat boat : boats) {
//If it is still racing, update its position.
if (boat.getStatus() == BoatStatusEnum.RACING) {
@ -544,7 +542,7 @@ public class MockRace extends Race {
* @param boat The boat to calculate VMG for.
* @return VMG for the specified boat.
*/
private VMG calculateVMG(MockBoat boat) {
public VMG calculateVMG(MockBoat boat) {
//Find the VMG inside these bounds.
@ -563,14 +561,14 @@ public class MockRace extends Race {
* @param bearingToDestination The bearing between the boat and its destination.
* @return True if the new VMG is improves velocity, false otherwise.
*/
private boolean improvesVelocity(VMG currentVMG, VMG potentialVMG, Bearing bearingToDestination) {
public boolean improvesVelocity(VMG currentVMG, VMG potentialVMG, Bearing bearingToDestination) {
//Calculates the angle between the boat and its destination.
Angle angleBetweenDestAndHeading = Angle.fromDegrees(currentVMG.getBearing().degrees() - bearingToDestination.degrees());
//Calculates the angle between the new VMG and the boat's destination.
Angle angleBetweenDestAndNewVMG = Angle.fromDegrees(potentialVMG.getBearing().degrees() - bearingToDestination.degrees());
//System.out.println(angleBetweenDestAndHeading.degrees() + ":" + angleBetweenDestAndNewVMG.degrees());
//Calculate the boat's current velocity.
double currentVelocity = Math.cos(angleBetweenDestAndHeading.radians()) * currentVMG.getSpeed();
@ -581,6 +579,7 @@ public class MockRace extends Race {
//Return whether or not the new VMG gives better velocity.
return vmgVelocity > currentVelocity;
}
/**
@ -625,13 +624,16 @@ public class MockRace extends Race {
//Move the boat forwards that many meters, and advances its time counters by enough milliseconds.
boat.moveForwards(distanceTravelledMeters, updatePeriodMilliseconds * this.scaleFactor);
long tackPeriod = 15000;
if (boat.getTimeSinceTackChange() > tackPeriod) {
System.out.println("tack loop");
//Calculate the new VMG.
VMG newVMG = this.calculateVMG(boat);
//If the new vmg improves velocity, use it.
if (improvesVelocity(boat, newVMG)) {
System.out.println("NEW VMG");
boat.setVMG(newVMG);
}

@ -1,7 +1,9 @@
package mock.model.commandFactory;
import mock.model.MockBoat;
import mock.model.MockRace;
public interface CommandFactory {
void runCommand(MockBoat boat);
void runCommand(MockBoat boat, MockRace race);
}

@ -1,15 +1,21 @@
package mock.model.commandFactory;
import mock.model.MockBoat;
import mock.model.MockRace;
import mock.model.VMG;
/**
* Created by David on 2/08/2017.
*/
public class TackGybeCommand implements CommandFactory {
//The refactoring of MockRace will require changes to be made
@Override
public void runCommand(MockBoat boat) {
public void runCommand(MockBoat boat, MockRace race) {
VMG newVMG = race.calculateVMG(boat);
VMG boatVMG = new VMG(boat.getCurrentSpeed(), boat.getBearing());
if(race.improvesVelocity(boatVMG, newVMG, boat.calculateBearingToNextMarker())){
boat.setVMG(newVMG);
}
}
}

@ -0,0 +1,19 @@
package mock.model.commandFactory;
import mock.model.MockBoat;
import mock.model.MockRace;
import mock.model.VMG;
import shared.model.Bearing;
/**
* Created by David on 2/08/2017.
*/
public class VMGCommand implements CommandFactory {
//The refactoring of MockRace will require changes to be made
@Override
public void runCommand(MockBoat boat, MockRace race) {
VMG newVMG = race.calculateVMG(boat);
boat.setVMG(newVMG);
}
}

@ -38,7 +38,7 @@ public class Constants {
/**
* The race pre-start time, in milliseconds. 3 minutes.
*/
public static final long RacePreStartTime = 3 * 60 * 1000;
public static final long RacePreStartTime = 1 * 10 * 1000;
/**

Loading…
Cancel
Save