Added mock yachts to the game state for each client #story[1047]

This commit is contained in:
Peter Galloway
2017-07-20 13:11:37 +12:00
parent 176d65e0b2
commit e317de7562
4 changed files with 31 additions and 37 deletions
@@ -15,6 +15,9 @@ import seng302.models.Yacht;
public class GameState {
private static Long previousUpdateTime;
private static Double windDirection = 0d;
private static Double windSpeed = 0d;
private static String hostIpAddress;
private static List<Player> players;
private static Map<Integer, Yacht> yachts;
@@ -46,6 +49,10 @@ public class GameState {
players.remove(player);
}
public static void addYacht(Integer sourceId, Yacht yatch) {
yachts.put(sourceId, yatch);
}
public static Boolean getIsRaceStarted() {
return isRaceStarted;
}
@@ -58,7 +65,8 @@ public class GameState {
GameState.currentStage = currentStage;
}
public void update() {
public static void update() {
Long timeInterval = System.currentTimeMillis() - previousUpdateTime;
previousUpdateTime = System.currentTimeMillis();
for (Yacht yacht : yachts.values()) {
@@ -57,10 +57,12 @@ public class MainServerThread extends Thread implements PacketBufferDelegate, Cl
e.printStackTrace();
}
if (GameState.getCurrentStage() == GameStages.PRE_RACE) {
GameState.update();
}
//RACING
if (GameState.getCurrentStage() == GameStages.RACING) {
GameState.update();
}
@@ -1,7 +1,9 @@
package seng302.gameServer;
import java.util.Random;
import seng302.gameServer.GameState;
import seng302.models.Player;
import seng302.models.Yacht;
import seng302.models.stream.PacketBufferDelegate;
import seng302.models.stream.StreamParser;
import seng302.models.stream.packets.StreamPacket;
@@ -11,6 +13,7 @@ import java.io.*;
import java.net.Socket;
import java.util.zip.CRC32;
import java.util.zip.Checksum;
import seng302.utilities.GeoPoint;
/**
* A class describing a single connection to a Client for the purposes of sending and receiving on its own thread.
@@ -31,6 +34,7 @@ public class ServerToClientThread extends Thread {
private Boolean connected = true;
private Boolean updateClient = true;
private Integer mockId = 100;
public ServerToClientThread(Socket socket) {
this.socket = socket;
try {
@@ -41,6 +45,8 @@ public class ServerToClientThread extends Thread {
}
// threeWayHandshake();
GameState.addPlayer(new Player(socket));
GameState.addYacht(mockId, new Yacht("Kappa", "Kap", new GeoPoint(0.0, 0.0), 0.0));
mockId += 1;
}
public void run() {
+12 -34
View File
@@ -1,11 +1,14 @@
package seng302.models;
import static seng302.utilities.GeoUtility.getGeoCoordinate;
import javafx.scene.paint.Color;
import seng302.models.mark.Mark;
import seng302.controllers.RaceViewController;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import seng302.utilities.GeoPoint;
/**
* Yacht class for the racing boat.
@@ -35,10 +38,9 @@ public class Yacht {
private Integer penaltiesServed;
private Long estimateTimeAtFinish;
private String position;
private Double lat;
private Double lon;
private Float heading;
private double velocity;
private GeoPoint location;
private Double heading;
private Double velocity;
private Long timeTillNext;
private Long markRoundTime;
@@ -52,8 +54,12 @@ public class Yacht {
*
* @param boatName Create a yacht object with name.
*/
public Yacht(String boatName) {
public Yacht(String boatName, String shortName, GeoPoint location, Double heading) {
this.boatName = boatName;
this.shortName = shortName;
this.location = location;
this.heading = heading;
this.velocity = 0.0;
}
/**
@@ -87,11 +93,7 @@ public class Yacht {
public void update(Long timeInterval) {
Double secondsElapsed = timeInterval / 1000000.0;
Double metersCovered = velocity * secondsElapsed;
// 111111 meters is approximately 1 degree of lat/long at the equator
lat = lat + (metersCovered * Math.sin(heading * (2 * Math.PI / 360))) / 111111;
lon = lon + (metersCovered * Math.cos(heading * (2 * Math.PI / 360))) / (111111 * Math
.cos(lat * (2 * Math.PI / 360)));
// formula take from https://gis.stackexchange.com/questions/2951/algorithm-for-offsetting-a-latitude-longitude-by-some-amount-of-meters
location = getGeoCoordinate(location, heading, metersCovered);
}
public String getBoatType() {
@@ -219,30 +221,6 @@ public class Yacht {
return nextMark;
}
public Double getLat() {
return lat;
}
public void setLat(Double lat) {
this.lat = lat;
}
public Double getLon() {
return lon;
}
public void setLon(Double lon) {
this.lon = lon;
}
public Float getHeading() {
return heading;
}
public void setHeading(Float heading) {
this.heading = heading;
}
@Override
public String toString() {
return boatName;