@ -10,7 +10,6 @@ import java.util.Observable;
public class SailsCommand extends ObserverCommand {
public class SailsCommand extends ObserverCommand {
private boolean sailsOut ;
private boolean sailsOut ;
private double goalVelocity ;
private double goalVelocity ;
private double acceleration = 1 ;
public SailsCommand ( MockRace race , MockBoat boat , boolean sailsOut ) {
public SailsCommand ( MockRace race , MockBoat boat , boolean sailsOut ) {
super ( race , boat ) ;
super ( race , boat ) ;
@ -20,6 +19,8 @@ public class SailsCommand extends ObserverCommand {
@Override
@Override
public void execute ( ) {
public void execute ( ) {
this . boat . setSailsOut ( this . sailsOut ) ;
this . boat . setSailsOut ( this . sailsOut ) ;
boat . setVelocityDefault ( false ) ;
if ( sailsOut ) {
if ( sailsOut ) {
// Accelerate to VMG speed
// Accelerate to VMG speed
double polarSpeed = NewPolars . calculateSpeed ( race . getWindDirection ( ) , race . getWindSpeed ( ) , boat . getBearing ( ) ) ;
double polarSpeed = NewPolars . calculateSpeed ( race . getWindDirection ( ) , race . getWindSpeed ( ) , boat . getBearing ( ) ) ;
@ -33,12 +34,18 @@ public class SailsCommand extends ObserverCommand {
@Override
@Override
public void update ( Observable o , Object arg ) {
public void update ( Observable o , Object arg ) {
double acceleration = 0.5 ;
if ( sailsOut & & boat . getCurrentSpeed ( ) < goalVelocity ) {
if ( sailsOut & & boat . getCurrentSpeed ( ) < goalVelocity ) {
// Apply acceleration
boat . setCurrentSpeed ( Math . min ( goalVelocity , boat . getCurrentSpeed ( ) + acceleration ) ) ;
} else if ( ! sailsOut & & boat . getCurrentSpeed ( ) > goalVelocity ) {
} else if ( ! sailsOut & & boat . getCurrentSpeed ( ) > goalVelocity ) {
// Apply deceleration
// Apply deceleration to strictly 0 speed
boat . setCurrentSpeed ( Math . max ( 0 , boat . getCurrentSpeed ( ) - acceleration ) ) ;
} else {
} else {
System . out . println ( goalVelocity + " " + boat . getCurrentSpeed ( ) ) ;
System . out . println ( goalVelocity + " " + boat . getCurrentSpeed ( ) ) ;
// Release boat from SailsCommand control
if ( sailsOut ) boat . setVelocityDefault ( true ) ;
race . deleteObserver ( this ) ;
race . deleteObserver ( this ) ;
}
}
}
}