You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

61 lines
2.0 KiB

package mock.model.commandFactory;
import mock.model.MockBoat;
import mock.model.MockRace;
import mock.model.VMG;
import shared.model.Bearing;
/**
* Command class for tacking and gybing
*/
public class TackGybeCommand implements Command {
private MockRace race;
private MockBoat boat;
/**
* Constructor for class
* @param race mock race
* @param boat mock boat to update
*/
public TackGybeCommand(MockRace race, MockBoat boat) {
this.race = race;
this.boat = boat;
}
//The refactoring of MockRace will require changes to be made
@Override
public void execute() {
/*if(boat.getBearing().degrees()>180){
boat.setBearing(Bearing.fromDegrees(360 - race.getWindDirection().degrees()));
} else {
boat.setBearing(Bearing.fromDegrees(race.getWindDirection().degrees()));
}*/
/*double angle = Math.max(race.getWindDirection().degrees(), boat.getBearing().degrees()) - Math.min(race.getWindDirection().degrees(), boat.getBearing().degrees());
boat.setBearing(Bearing.fromDegrees(angle));*/
double boatAngle = boat.getBearing().degrees();
double windAngle =race.getWindDirection().degrees();
double differenceAngle = calcDistance(boatAngle, windAngle);
double angleA = windAngle + differenceAngle;
double angleB = windAngle - differenceAngle;
if(angleA % 360 == boatAngle){
boat.setBearing(Bearing.fromDegrees(angleB));
} else {
boat.setBearing(Bearing.fromDegrees(angleA));
}
}
/**
* Method to calculate smallest angle between 2 angles
* @param degreeA first angle degree
* @param degreeB second angle degree
* @return the calculated smallest angle
*/
public double calcDistance(double degreeA, double degreeB){
double phi = Math.abs(degreeB - degreeA) % 360;
double distance = phi > 180 ? 360 - phi : phi;
return distance;
}
}