From 00cedf4d4d30a3763480f120a48b4580b250771a Mon Sep 17 00:00:00 2001 From: Joseph Gardner Date: Wed, 6 Sep 2017 15:19:30 +1200 Subject: [PATCH] TackGybeCommand now smooths the rotation of the boat. #story[1196] --- .../src/main/java/mock/model/RaceLogic.java | 10 +++--- .../model/commandFactory/TackGybeCommand.java | 35 ++++++++++++++----- .../src/main/java/shared/model/Constants.java | 4 ++- 3 files changed, 35 insertions(+), 14 deletions(-) diff --git a/racevisionGame/src/main/java/mock/model/RaceLogic.java b/racevisionGame/src/main/java/mock/model/RaceLogic.java index 977bd8d6..308c4b46 100644 --- a/racevisionGame/src/main/java/mock/model/RaceLogic.java +++ b/racevisionGame/src/main/java/mock/model/RaceLogic.java @@ -145,9 +145,13 @@ public class RaceLogic implements RunnableWithFramePeriod, Observer { //Get the current time. long currentTime = System.currentTimeMillis(); - //Execute commands from clients. + // Execute commands from clients. commands.execute(); + // Notify Observers + race.setChanged(); + race.notifyObservers(); + //Update race time. race.updateRaceTime(currentTime); @@ -185,10 +189,6 @@ public class RaceLogic implements RunnableWithFramePeriod, Observer { previousFrameTime = currentTime; } - // Notify Observers - race.setChanged(); - race.notifyObservers(); - waitForFramePeriod(previousFrameTime, currentTime, 50); previousFrameTime = currentTime; } diff --git a/racevisionGame/src/main/java/mock/model/commandFactory/TackGybeCommand.java b/racevisionGame/src/main/java/mock/model/commandFactory/TackGybeCommand.java index 28ace831..0fa17279 100644 --- a/racevisionGame/src/main/java/mock/model/commandFactory/TackGybeCommand.java +++ b/racevisionGame/src/main/java/mock/model/commandFactory/TackGybeCommand.java @@ -10,6 +10,10 @@ import java.util.Observable; * Command class for tacking and gybing */ public class TackGybeCommand extends ObserverCommand { + private double goalRotation; + private double totalRotation = 0; + private int direction; // -1 for anticlockwise, 1 for clockwise + private double goalAngle; /** * Constructor for class @@ -22,18 +26,26 @@ public class TackGybeCommand extends ObserverCommand { @Override public void execute() { - - boat.setAutoVMG(false); - double boatAngle = boat.getBearing().degrees(); - double windAngle =race.getWindDirection().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)); + if (angleA % 360 == boatAngle) { + goalAngle = angleB % 360; } else { - boat.setBearing(Bearing.fromDegrees(angleA)); + goalAngle = angleA % 360; + } + + goalRotation = goalAngle - boatAngle; + if (goalRotation < 0) { + goalRotation += 360; + } + if (goalRotation > 180) { + goalRotation = 360 - goalRotation; + direction = -1; + } else { + direction = 1; } } @@ -50,7 +62,14 @@ public class TackGybeCommand extends ObserverCommand { @Override public void update(Observable o, Object arg) { - + double offset = 3.0; + if (totalRotation < goalRotation) { + boat.setBearing(Bearing.fromDegrees(boat.getBearing().degrees() + offset * direction)); + totalRotation += offset; + } else { + System.out.println(goalAngle + " " + boat.getBearing().degrees()); + race.deleteObserver(this); + } } } diff --git a/racevisionGame/src/main/java/shared/model/Constants.java b/racevisionGame/src/main/java/shared/model/Constants.java index bb7ec598..a415ece2 100644 --- a/racevisionGame/src/main/java/shared/model/Constants.java +++ b/racevisionGame/src/main/java/shared/model/Constants.java @@ -38,7 +38,9 @@ public class Constants { /** * The race pre-start time, in milliseconds. 3 minutes. */ - public static final long RacePreStartTime = 3 * 60 * 1000; + // + // TODO: CHANGE BACK TO 3 MINUTES BEFORE MERGING TO MASTER + public static final long RacePreStartTime = 30000; //3 * 60 * 1000; /**