Re-added wind speed & direction variations

- Wind speed  & direction are randomly updated
- Removed two duplicate threads ;)

Tags: #story[1124]
This commit is contained in:
Michael Rausch
2017-08-16 20:48:37 +12:00
parent 28569506f0
commit 87d6799c10
4 changed files with 80 additions and 23 deletions
+24 -13
View File
@@ -36,7 +36,6 @@ import seng302.utilities.XMLParser;
* Created by wmu16 on 10/07/17.
*/
public class GameState implements Runnable {
@FunctionalInterface
interface NewMessageListener {
@@ -180,6 +179,14 @@ public class GameState implements Runnable {
return windDirection;
}
public static void setWindDirection(Double newWindDirection) {
windDirection = newWindDirection;
}
public static void setWindSpeed(Double newWindSpeed) {
windSpeed = newWindSpeed;
}
public static Double getWindSpeedMMS() {
return windSpeed;
}
@@ -250,7 +257,6 @@ public class GameState implements Runnable {
}
}
/**
* Called periodically in this GameState thread to update the GameState values
*/
@@ -271,6 +277,8 @@ public class GameState implements Runnable {
checkForLegProgression(yacht);
raceFinished = false;
}
}
if (raceFinished) {
@@ -329,17 +337,20 @@ public class GameState implements Runnable {
notifyMessageListeners(
new YachtEventCodeMessage(serverYacht.getSourceId())
);
} else if (checkBoundaryCollision(serverYacht)) {
serverYacht.setLocation(
calculateBounceBack(serverYacht, serverYacht.getLocation(),
BOUNCE_DISTANCE_YACHT)
);
serverYacht.setCurrentVelocity(
serverYacht.getCurrentVelocity() * COLLISION_VELOCITY_PENALTY
);
notifyMessageListeners(
new YachtEventCodeMessage(serverYacht.getSourceId())
);
}
else{
if (checkBoundaryCollision(serverYacht)) {
serverYacht.setLocation(
calculateBounceBack(serverYacht, serverYacht.getLocation(),
BOUNCE_DISTANCE_YACHT)
);
serverYacht.setCurrentVelocity(
serverYacht.getCurrentVelocity() * COLLISION_VELOCITY_PENALTY
);
notifyMessageListeners(
new YachtEventCodeMessage(serverYacht.getSourceId())
);
}
}
}
}
@@ -4,10 +4,7 @@ import gherkin.lexer.Fi;
import java.io.IOException;
import java.net.ServerSocket;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import java.util.*;
import seng302.gameServer.server.messages.*;
import seng302.model.GeoPoint;
@@ -25,12 +22,17 @@ import seng302.visualiser.GameClient;
public class MainServerThread implements Runnable, ClientConnectionDelegate {
private static final int PORT = 4942;
private static final Integer CLIENT_UPDATES_PER_SECOND = 10;
private static final Integer CLIENT_UPDATES_PER_SECOND = 60;
private static final int LOG_LEVEL = 1;
private static final int WARNING_TIME = 10 * -1000;
private static final int PREPATORY_TIME = 5 * -1000;
public static final int TIME_TILL_START = 10 * 1000;
private static final int MAX_WIND_SPEED = 12000;
private static final int MIN_WIND_SPEED = 8000;
public static int windSpeed = 1000;
private boolean terminated;
private Thread thread;
@@ -51,6 +53,7 @@ public class MainServerThread implements Runnable, ClientConnectionDelegate {
GameState.addMarkPassListener(this::broadcastMessage);
terminated = false;
thread = new Thread(this, "MainServer");
startUpdatingWind();
thread.start();
}
@@ -104,6 +107,45 @@ public class MainServerThread implements Runnable, ClientConnectionDelegate {
}
}
private static void updateWind(){
Integer direction = GameState.getWindDirection().intValue();
Integer windSpeed = GameState.getWindSpeedMMS().intValue();
Random random = new Random();
if (Math.floorMod(random.nextInt(), 2) == 0){
direction += random.nextInt(4);
windSpeed += random.nextInt(100) + 500;
}
else{
direction -= random.nextInt(4);
windSpeed -= random.nextInt(100) + 500;
}
direction = Math.floorMod(direction, 360);
if (windSpeed > MAX_WIND_SPEED){
windSpeed -= random.nextInt(1000);
}
if (windSpeed <= MIN_WIND_SPEED){
windSpeed += random.nextInt(1000);
}
GameState.setWindSpeed(Double.valueOf(windSpeed));
GameState.setWindDirection(direction.doubleValue());
}
private static void startUpdatingWind(){
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
updateWind();
}
}, 0, 500);
}
static void serverLog(String message, int logLevel) {
if (logLevel <= LOG_LEVEL) {
@@ -39,7 +39,7 @@ public class RaceStatusMessage extends Message{
this.raceId = raceId;
this.raceStatus = raceStatus;
this.expectedStartTime = expectedStartTime;
this.raceWindDirection = raceWindDirection * windDirFactor;
this.raceWindDirection = raceWindDirection * windDirFactor+100.0;
this.windSpeed = windSpeed;
this.numBoatsInRace = numBoatsInRace;
this.raceType = raceType;
@@ -73,6 +73,9 @@ public class RaceViewController extends Thread implements ImportantAnnotationDel
private ComboBox<ClientYacht> yachtSelectionComboBox;
@FXML
private Text fpsDisplay;
@FXML
private Text windSpeedText;
//Race Data
private Map<Integer, ClientYacht> participants;
private Map<Integer, CompoundMark> markers;
@@ -102,8 +105,7 @@ public class RaceViewController extends Thread implements ImportantAnnotationDel
public void loadRace (
Map<Integer, ClientYacht> participants, RaceXMLData raceData, RaceState raceState,
ClientYacht player
) {
ClientYacht player) {
this.participants = participants;
this.courseData = raceData;
this.markers = raceData.getCompoundMarks();
@@ -315,7 +317,7 @@ public class RaceViewController extends Thread implements ImportantAnnotationDel
@Override
public void run() {
updateRaceTime();
updateWindDirection();
updateWind();
updateOrder();
// updateSparkLine();
}
@@ -355,9 +357,11 @@ public class RaceViewController extends Thread implements ImportantAnnotationDel
/**
* Updates the wind direction arrow and text as from info from the StreamParser
*/
private void updateWindDirection() {
private void updateWind() {
windDirectionText.setText(String.format("%.1f°", raceState.getWindDirection()));
windArrowText.setRotate(raceState.getWindDirection());
windSpeedText.setText("Speed: " + String.format("%.1f", raceState.getWindSpeed()) + " Knots");
}