Hooked up key press actions to the GameState, applying the relevant maths to update headings etc.

tags: #story[989]  #pair[ptg19, wmu16]
This commit is contained in:
Peter Galloway
2017-07-21 16:50:09 +12:00
parent 12c2f31af9
commit 913e5fee7b
2 changed files with 60 additions and 178 deletions
@@ -15,9 +15,11 @@ import seng302.server.messages.BoatActionType;
*/
public class GameState {
private static final Integer HEADING_STEP = 3;
private static Long previousUpdateTime;
private static Double windDirection = 0d;
private static Double windSpeed = 0d;
public static Double windDirection;
private static Double windSpeed;
private static String hostIpAddress;
private static List<Player> players;
@@ -26,6 +28,10 @@ public class GameState {
private static GameStages currentStage;
public GameState(String hostIpAddress) {
windDirection = 170d;
windSpeed = 0d;
GameState.hostIpAddress = hostIpAddress;
players = new ArrayList<>();
currentStage = GameStages.LOBBYING;
@@ -75,18 +81,24 @@ public class GameState {
}
public static void updateBoat(Integer sourceId, BoatActionType actionType) {
Yacht playerYacht = yachts.get(sourceId);
switch (actionType) {
case VMG:
break;
case SAILS_IN:
playerYacht.toggleSailIn();
break;
case SAILS_OUT:
playerYacht.toggleSailIn();
break;
case TACK_GYBE:
playerYacht.tackGybe(windDirection);
break;
case UPWIND:
playerYacht.turnUpwind();
break;
case DOWNWIND:
playerYacht.turnDownwind();
break;
}
}
+43 -173
View File
@@ -7,6 +7,7 @@ import java.text.SimpleDateFormat;
import java.util.Map;
import javafx.scene.paint.Color;
import seng302.controllers.RaceViewController;
import seng302.gameServer.GameState;
import seng302.models.mark.Mark;
import seng302.utilities.GeoPoint;
@@ -18,83 +19,38 @@ import seng302.utilities.GeoPoint;
*/
public class Yacht {
// Used in boat group
private Color colour;
private final Double TURN_STEP = 3.0;
private String boatType;
private Integer sourceID;
private String hullID; //matches HullNum in the XML spec.
private String shortName;
private String boatName;
private String country;
// Situational data
// Boat status
private Integer boatStatus;
private Integer legNumber;
private Integer penaltiesAwarded;
private Integer penaltiesServed;
private Long estimateTimeAtFinish;
private String position;
private GeoPoint location;
private Double heading;
private Double lastHeading;
private Double velocity;
private Long timeTillNext;
private Long markRoundTime;
// Mark rounding
private Mark lastMarkRounded;
private Mark nextMark;
private Boolean sailIn;
/**
* Used in EventTest and RaceTest.
*
* @param boatName Create a yacht object with name.
* @param location latlon location of the boat stored in a geopoint
* @param heading heading of the boat in degrees from 0 to 365 with 0 being north
*/
public Yacht(String boatName, String shortName, GeoPoint location, Double heading) {
this.boatName = boatName;
this.shortName = shortName;
public Yacht(GeoPoint location, Double heading) {
this.location = location;
this.heading = heading;
this.velocity = 0.0;
}
/**
* Used in BoatGroupTest.
*
* @param boatName The name of the team sailing the boat
* @param boatVelocity The speed of the boat in meters/second
* @param shortName A shorter version of the teams name
*/
public Yacht(String boatName, double boatVelocity, String shortName, int id) {
this.boatName = boatName;
this.velocity = boatVelocity;
this.shortName = shortName;
this.sourceID = id;
}
public Yacht(String boatType, Integer sourceID, String hullID, String shortName,
String boatName, String country) {
this.boatType = boatType;
this.sourceID = sourceID;
this.hullID = hullID;
this.shortName = shortName;
this.boatName = boatName;
this.country = country;
this.position = "-";
this.sailIn = false;
}
/**
* @param timeInterval since last update in milliseconds
*/
public void update(Long timeInterval) {
if (sailIn) {
Double secondsElapsed = timeInterval / 1000000.0;
Double metersCovered = velocity * secondsElapsed;
location = getGeoCoordinate(location, heading, metersCovered);
}
}
/**
* Adjusts the yachts velocity based on the wind direction and speed from the polar table.
@@ -121,135 +77,49 @@ public class Yacht {
velocity = polarsFromClosestSpd.get(closest_key);
}
public String getBoatType() {
return boatType;
public Double getHeading() {
return heading;
}
public Integer getSourceID() {
return sourceID;
public void adjustHeading(Double amount) {
lastHeading = heading;
heading = (heading + amount) % 360.0;
}
public String getHullID() {
return hullID;
public void tackGybe(Double windDirection) {
adjustHeading(-2 * ((heading - windDirection) % 360));
}
public String getShortName() {
return shortName;
public void toggleSailIn() {
sailIn = !sailIn;
}
public String getBoatName() {
return boatName;
public void turnUpwind() {
Double normalizedHeading = (heading - GameState.windDirection) % 360;
if (normalizedHeading == 0) {
if (lastHeading < 180) {
adjustHeading(-TURN_STEP);
} else {
adjustHeading(TURN_STEP);
}
} else if (normalizedHeading == 180) {
if (lastHeading < 180) {
adjustHeading(TURN_STEP);
} else {
adjustHeading(-TURN_STEP);
}
} else if (normalizedHeading < 180) {
adjustHeading(-TURN_STEP);
} else {
adjustHeading(TURN_STEP);
}
}
public String getCountry() {
return country;
public void turnDownwind() {
if ((heading - GameState.windDirection) % 360 < 180) {
adjustHeading(TURN_STEP);
} else {
adjustHeading(-TURN_STEP);
}
public Integer getBoatStatus() {
return boatStatus;
}
public void setBoatStatus(Integer boatStatus) {
this.boatStatus = boatStatus;
}
public Integer getLegNumber() {
return legNumber;
}
public void setLegNumber(Integer legNumber) {
if (colour != null && position != "-" && legNumber != this.legNumber&& RaceViewController.sparkLineStatus(sourceID)) {
RaceViewController.updateYachtPositionSparkline(this, legNumber);
}
this.legNumber = legNumber;
}
public Integer getPenaltiesAwarded() {
return penaltiesAwarded;
}
public void setPenaltiesAwarded(Integer penaltiesAwarded) {
this.penaltiesAwarded = penaltiesAwarded;
}
public Integer getPenaltiesServed() {
return penaltiesServed;
}
public void setPenaltiesServed(Integer penaltiesServed) {
this.penaltiesServed = penaltiesServed;
}
public void setEstimateTimeAtNextMark(Long estimateTimeAtNextMark) {
timeTillNext = estimateTimeAtNextMark;
}
public String getEstimateTimeAtFinish() {
DateFormat format = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
return format.format(estimateTimeAtFinish);
}
public void setEstimateTimeAtFinish(Long estimateTimeAtFinish) {
this.estimateTimeAtFinish = estimateTimeAtFinish;
}
public String getPosition() {
return position;
}
public void setPosition(String position) {
this.position = position;
}
public Color getColour() {
return colour;
}
public void setColour(Color colour) {
this.colour = colour;
}
public void setVelocity(double velocity) {
this.velocity = velocity;
}
public void setMarkRoundingTime(Long markRoundingTime) {
this.markRoundTime = markRoundingTime;
}
public double getVelocity() {
return velocity;
}
public Long getTimeTillNext() {
return timeTillNext;
}
public Long getMarkRoundTime() {
return markRoundTime;
}
public Mark getLastMarkRounded() {
return lastMarkRounded;
}
public void setLastMarkRounded(Mark lastMarkRounded) {
this.lastMarkRounded = lastMarkRounded;
}
public void setNextMark(Mark nextMark) {
this.nextMark = nextMark;
}
public Mark getNextMark(){
return nextMark;
}
@Override
public String toString() {
return boatName;
}
}