From a30a1aa7c7807124bddfb14fb129e19014c7b4a9 Mon Sep 17 00:00:00 2001 From: William Muir Date: Mon, 24 Jul 2017 21:14:17 +1200 Subject: [PATCH] Tweaking to server loop making it send packets at 5Hz Commented out some smoothing code in BoatGroup that was dependend on FPS screwing with movement --- .../java/seng302/fxObjects/BoatGroup.java | 42 +++++++++---------- .../seng302/gameServer/MainServerThread.java | 4 +- .../gameServer/ServerToClientThread.java | 13 +++++- src/main/java/seng302/models/Yacht.java | 2 +- 4 files changed, 35 insertions(+), 26 deletions(-) diff --git a/src/main/java/seng302/fxObjects/BoatGroup.java b/src/main/java/seng302/fxObjects/BoatGroup.java index 1f557318..7a11d181 100644 --- a/src/main/java/seng302/fxObjects/BoatGroup.java +++ b/src/main/java/seng302/fxObjects/BoatGroup.java @@ -172,27 +172,27 @@ public class BoatGroup extends Group { isStopped = true; } - if (distanceTravelled > 70) { - distanceTravelled = 0d; - - if (lastPoint != null) { - Line l = new Line( - lastPoint.getX(), - lastPoint.getY(), - boatPoly.getLayoutX(), - boatPoly.getLayoutY() - ); - l.getStrokeDashArray().setAll(3d, 7d); - l.setStroke(boat.getColour()); - l.setCache(true); - l.setCacheHint(CacheHint.SPEED); - lineGroup.getChildren().add(l); - } - - if (destinationSet) { - lastPoint = new Point2D(boatPoly.getLayoutX(), boatPoly.getLayoutY()); - } - } +// if (distanceTravelled > 70) { +// distanceTravelled = 0d; +// +// if (lastPoint != null) { +// Line l = new Line( +// lastPoint.getX(), +// lastPoint.getY(), +// boatPoly.getLayoutX(), +// boatPoly.getLayoutY() +// ); +// l.getStrokeDashArray().setAll(3d, 7d); +// l.setStroke(boat.getColour()); +// l.setCache(true); +// l.setCacheHint(CacheHint.SPEED); +// lineGroup.getChildren().add(l); +// } +// +// if (destinationSet) { +// lastPoint = new Point2D(boatPoly.getLayoutX(), boatPoly.getLayoutY()); +// } +// } wake.updatePosition(); } diff --git a/src/main/java/seng302/gameServer/MainServerThread.java b/src/main/java/seng302/gameServer/MainServerThread.java index 80539881..aa14b5eb 100644 --- a/src/main/java/seng302/gameServer/MainServerThread.java +++ b/src/main/java/seng302/gameServer/MainServerThread.java @@ -19,6 +19,7 @@ public class MainServerThread implements Runnable, PacketBufferDelegate, ClientC private static final int PORT = 4942; private static final Integer MAX_NUM_PLAYERS = 3; + private static final Integer UPDATES_PER_SECOND = 5; private static final int LOG_LEVEL = 1; private Thread thread; @@ -57,7 +58,7 @@ public class MainServerThread implements Runnable, PacketBufferDelegate, ClientC //You should handle interrupts in some way, so that the thread won't keep on forever if you exit the app. while (!thread.isInterrupted()) { try { - Thread.sleep(1000 / 60); //60 times per second we should calculate the game state + Thread.sleep(1000 / UPDATES_PER_SECOND); //60 times per second we should calculate the game state } catch (InterruptedException e) { e.printStackTrace(); } @@ -77,7 +78,6 @@ public class MainServerThread implements Runnable, PacketBufferDelegate, ClientC else if (GameState.getCurrentStage() == GameStages.FINISHED) { } - updateClients(); while (!packetBuffer.isEmpty()){ try { diff --git a/src/main/java/seng302/gameServer/ServerToClientThread.java b/src/main/java/seng302/gameServer/ServerToClientThread.java index 374c3b6c..0028c9f9 100644 --- a/src/main/java/seng302/gameServer/ServerToClientThread.java +++ b/src/main/java/seng302/gameServer/ServerToClientThread.java @@ -184,7 +184,7 @@ public class ServerToClientThread implements Runnable { // } catch (IOException e) { // System.out.println("IO error in server thread upon writing to output stream"); // } - sendBoatLocationPackets(); +// sendBoatLocationPackets(); updateClient = false; } @@ -251,6 +251,7 @@ public class ServerToClientThread implements Runnable { } public void updateClient() { + sendBoatLocationPackets(); updateClient = true; } @@ -347,7 +348,15 @@ public class ServerToClientThread implements Runnable { ArrayList yachts = new ArrayList<>(GameState.getYachts().values()); for (Yacht yacht: yachts){ // System.out.println("[SERVER] Lat: " + yacht.getLocation().getLat() + " Lon: " + yacht.getLocation().getLng()); - BoatLocationMessage boatLocationMessage = new BoatLocationMessage(sourceId, getSeqNo(), yacht.getLocation().getLat(), yacht.getLocation().getLng(), yacht.getHeading(), (long) yacht.getVelocity()); + BoatLocationMessage boatLocationMessage = + new BoatLocationMessage( + sourceId, + getSeqNo(), + yacht.getLocation().getLat(), + yacht.getLocation().getLng(), + yacht.getHeading(), + (long) yacht.getVelocity()); + sendMessage(boatLocationMessage); } } diff --git a/src/main/java/seng302/models/Yacht.java b/src/main/java/seng302/models/Yacht.java index 2d1abfe9..3105383d 100644 --- a/src/main/java/seng302/models/Yacht.java +++ b/src/main/java/seng302/models/Yacht.java @@ -19,7 +19,7 @@ import seng302.utilities.GeoPoint; */ public class Yacht { - private final Double TURN_STEP = 3.0; + private final Double TURN_STEP = 2.0; private Double lastHeading; private Boolean sailIn;