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 { public class GameState {
private static final Integer HEADING_STEP = 3;
private static Long previousUpdateTime; private static Long previousUpdateTime;
private static Double windDirection = 0d; public static Double windDirection;
private static Double windSpeed = 0d; private static Double windSpeed;
private static String hostIpAddress; private static String hostIpAddress;
private static List<Player> players; private static List<Player> players;
@@ -26,6 +28,10 @@ public class GameState {
private static GameStages currentStage; private static GameStages currentStage;
public GameState(String hostIpAddress) { public GameState(String hostIpAddress) {
windDirection = 170d;
windSpeed = 0d;
GameState.hostIpAddress = hostIpAddress; GameState.hostIpAddress = hostIpAddress;
players = new ArrayList<>(); players = new ArrayList<>();
currentStage = GameStages.LOBBYING; currentStage = GameStages.LOBBYING;
@@ -75,18 +81,24 @@ public class GameState {
} }
public static void updateBoat(Integer sourceId, BoatActionType actionType) { public static void updateBoat(Integer sourceId, BoatActionType actionType) {
Yacht playerYacht = yachts.get(sourceId);
switch (actionType) { switch (actionType) {
case VMG: case VMG:
break; break;
case SAILS_IN: case SAILS_IN:
playerYacht.toggleSailIn();
break; break;
case SAILS_OUT: case SAILS_OUT:
playerYacht.toggleSailIn();
break; break;
case TACK_GYBE: case TACK_GYBE:
playerYacht.tackGybe(windDirection);
break; break;
case UPWIND: case UPWIND:
playerYacht.turnUpwind();
break; break;
case DOWNWIND: case DOWNWIND:
playerYacht.turnDownwind();
break; break;
} }
} }
+43 -173
View File
@@ -7,6 +7,7 @@ import java.text.SimpleDateFormat;
import java.util.Map; import java.util.Map;
import javafx.scene.paint.Color; import javafx.scene.paint.Color;
import seng302.controllers.RaceViewController; import seng302.controllers.RaceViewController;
import seng302.gameServer.GameState;
import seng302.models.mark.Mark; import seng302.models.mark.Mark;
import seng302.utilities.GeoPoint; import seng302.utilities.GeoPoint;
@@ -18,83 +19,38 @@ import seng302.utilities.GeoPoint;
*/ */
public class Yacht { public class Yacht {
// Used in boat group private final Double TURN_STEP = 3.0;
private Color colour;
private String boatType;
private Integer sourceID; 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 GeoPoint location;
private Double heading; private Double heading;
private Double lastHeading;
private Double velocity; private Double velocity;
private Long timeTillNext;
private Long markRoundTime;
// Mark rounding private Boolean sailIn;
private Mark lastMarkRounded;
private Mark nextMark;
/** /**
* Used in EventTest and RaceTest. * @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
* @param boatName Create a yacht object with name.
*/ */
public Yacht(String boatName, String shortName, GeoPoint location, Double heading) { public Yacht(GeoPoint location, Double heading) {
this.boatName = boatName;
this.shortName = shortName;
this.location = location; this.location = location;
this.heading = heading; this.heading = heading;
this.velocity = 0.0; this.velocity = 0.0;
} this.sailIn = false;
/**
* 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 = "-";
} }
/** /**
* @param timeInterval since last update in milliseconds * @param timeInterval since last update in milliseconds
*/ */
public void update(Long timeInterval) { public void update(Long timeInterval) {
if (sailIn) {
Double secondsElapsed = timeInterval / 1000000.0; Double secondsElapsed = timeInterval / 1000000.0;
Double metersCovered = velocity * secondsElapsed; Double metersCovered = velocity * secondsElapsed;
location = getGeoCoordinate(location, heading, metersCovered); location = getGeoCoordinate(location, heading, metersCovered);
} }
}
/** /**
* Adjusts the yachts velocity based on the wind direction and speed from the polar table. * 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); velocity = polarsFromClosestSpd.get(closest_key);
} }
public Double getHeading() {
public String getBoatType() { return heading;
return boatType;
} }
public Integer getSourceID() { public void adjustHeading(Double amount) {
return sourceID; lastHeading = heading;
heading = (heading + amount) % 360.0;
} }
public String getHullID() { public void tackGybe(Double windDirection) {
return hullID; adjustHeading(-2 * ((heading - windDirection) % 360));
} }
public String getShortName() { public void toggleSailIn() {
return shortName; sailIn = !sailIn;
} }
public String getBoatName() { public void turnUpwind() {
return boatName; 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() { public void turnDownwind() {
return country; 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;
}
} }