Made the sails work properly by toggling.

Need to remove the unneeded code I added.

#story[1111]
This commit is contained in:
Kusal Ekanayake
2017-08-03 16:33:51 +12:00
parent a8e70b3631
commit 99d5545ed3
5 changed files with 69 additions and 14 deletions
+12 -4
View File
@@ -23,9 +23,10 @@ import seng302.model.mark.CompoundMark;
*/ */
public class Yacht { public class Yacht {
@FunctionalInterface @FunctionalInterface
public interface YachtLocationListener { public interface YachtLocationListener {
void notifyLocation(Yacht yacht, double lat, double lon, double heading, double velocity); void notifyLocation(Yacht yacht, double lat, double lon, double heading, double velocity, boolean sailIn);
} }
//BOTH AFAIK //BOTH AFAIK
@@ -48,7 +49,7 @@ public class Yacht {
//SERVER SIDE //SERVER SIDE
private final Double TURN_STEP = 5.0; private final Double TURN_STEP = 5.0;
private Double lastHeading; private Double lastHeading;
private Boolean sailIn; private Boolean sailIn = false;
private GeoPoint location; private GeoPoint location;
private Integer boatStatus; private Integer boatStatus;
private Double velocity; private Double velocity;
@@ -61,6 +62,7 @@ public class Yacht {
private CompoundMark lastMarkRounded; private CompoundMark lastMarkRounded;
private Integer positionInt = 0; private Integer positionInt = 0;
private Color colour; private Color colour;
private Boolean clientSailsIn = false;
public Yacht(String boatType, Integer sourceId, String hullID, String shortName, public Yacht(String boatType, Integer sourceId, String hullID, String shortName,
String boatName, String country) { String boatName, String country) {
@@ -70,7 +72,6 @@ public class Yacht {
this.shortName = shortName; this.shortName = shortName;
this.boatName = boatName; this.boatName = boatName;
this.country = country; this.country = country;
this.sailIn = false;
this.location = new GeoPoint(57.670341, 11.826856); this.location = new GeoPoint(57.670341, 11.826856);
this.heading = 120.0; //In degrees this.heading = 120.0; //In degrees
this.velocity = 0d; //in mms-1 this.velocity = 0d; //in mms-1
@@ -281,6 +282,10 @@ public class Yacht {
this.velocityProperty.set(velocity); this.velocityProperty.set(velocity);
} }
public void updateSailsInProperty(Boolean clientSails) {
this.clientSailsIn = clientSails;
}
public void setMarkRoundingTime(Long markRoundingTime) { public void setMarkRoundingTime(Long markRoundingTime) {
this.markRoundTime = markRoundingTime; this.markRoundTime = markRoundingTime;
} }
@@ -383,6 +388,9 @@ public class Yacht {
this.colour = colour; this.colour = colour;
} }
public void toggleClientSail() {
clientSailsIn = !clientSailsIn;
}
public Double getVelocity() { public Double getVelocity() {
return velocity; return velocity;
@@ -399,7 +407,7 @@ public class Yacht {
this.velocity = velocity; this.velocity = velocity;
updateVelocityProperty(velocity); updateVelocityProperty(velocity);
for (YachtLocationListener yll : locationListeners) { for (YachtLocationListener yll : locationListeners) {
yll.notifyLocation(this, lat, lon, heading, velocity); yll.notifyLocation(this, lat, lon, heading, velocity, this.clientSailsIn);
} }
} }
@@ -319,6 +319,7 @@ public class GameClient {
BoatActionMessage boatActionMessage = new BoatActionMessage( BoatActionMessage boatActionMessage = new BoatActionMessage(
BoatActionType.SAILS_IN); BoatActionType.SAILS_IN);
socketThread.sendBoatActionMessage(boatActionMessage); socketThread.sendBoatActionMessage(boatActionMessage);
raceView.getGameView().getPlayerYacht().toggleClientSail();
break; break;
} }
} }
@@ -80,6 +80,7 @@ public class GameView extends Pane {
private Double frameRate = 60.0; private Double frameRate = 60.0;
private int frameTimeIndex = 0; private int frameTimeIndex = 0;
private boolean arrayFilled = false; private boolean arrayFilled = false;
private Yacht playerYacht;
private enum ScaleDirection { private enum ScaleDirection {
HORIZONTAL, HORIZONTAL,
@@ -324,10 +325,10 @@ public class GameView extends Pane {
boatObjectGroup.getChildren().add(newBoat); boatObjectGroup.getChildren().add(newBoat);
trails.getChildren().add(newBoat.getTrail()); trails.getChildren().add(newBoat.getTrail());
// TODO: 1/08/17 Make this less vile to look at. // TODO: 1/08/17 Make this less vile to look at.
yacht.addLocationListener((boat, lat, lon, heading, velocity) ->{ yacht.addLocationListener((boat, lat, lon, heading, velocity, sailIn) ->{
BoatObject bo = boatObjects.get(boat); BoatObject bo = boatObjects.get(boat);
Point2D p2d = findScaledXY(lat, lon); Point2D p2d = findScaledXY(lat, lon);
bo.moveTo(p2d.getX(), p2d.getY(), heading, velocity); bo.moveTo(p2d.getX(), p2d.getY(), heading, velocity, sailIn);
// annotations.get(boat).setLayoutX(p2d.getX()); // annotations.get(boat).setLayoutX(p2d.getX());
// annotations.get(boat).setLayoutY(p2d.getY()); // annotations.get(boat).setLayoutY(p2d.getY());
// annotations.get(boat).setLocation(100d, 100d); // annotations.get(boat).setLocation(100d, 100d);
@@ -569,7 +570,12 @@ public class GameView extends Pane {
timer.start(); timer.start();
} }
public Yacht getPlayerYacht() {
return playerYacht;
}
public void setBoatAsPlayer (Yacht playerYacht) { public void setBoatAsPlayer (Yacht playerYacht) {
this.playerYacht = playerYacht;
boatObjects.get(playerYacht).setAsPlayer(); boatObjects.get(playerYacht).setAsPlayer();
annotations.get(playerYacht).addAnnotation( annotations.get(playerYacht).addAnnotation(
"velocity", "velocity",
@@ -595,4 +595,8 @@ public class RaceViewController extends Thread implements ImportantAnnotationDel
this.courseData = raceData; this.courseData = raceData;
gameView.updateBorder(raceData.getCourseLimit()); gameView.updateBorder(raceData.getCourseLimit());
} }
public GameView getGameView() {
return gameView;
}
} }
@@ -12,6 +12,7 @@ import javafx.scene.shape.Line;
import javafx.scene.shape.Polygon; import javafx.scene.shape.Polygon;
import javafx.scene.shape.Polyline; import javafx.scene.shape.Polyline;
import javafx.scene.transform.Rotate; import javafx.scene.transform.Rotate;
import seng302.gameServer.GameState;
/** /**
* BoatGroup is a javafx group that by default contains a graphical objects for representing a 2 * BoatGroup is a javafx group that by default contains a graphical objects for representing a 2
@@ -30,9 +31,11 @@ public class BoatObject extends Group {
private double xVelocity; private double xVelocity;
private double yVelocity; private double yVelocity;
private double lastHeading; private double lastHeading;
private double sailState;
//Graphical objects //Graphical objects
private Polyline trail = new Polyline(); private Polyline trail = new Polyline();
private Polygon boatPoly; private Polygon boatPoly;
private Polygon sail;
private Wake wake; private Wake wake;
private Line leftLayLine; private Line leftLayLine;
private Line rightLayline; private Line rightLayline;
@@ -87,14 +90,21 @@ public class BoatObject extends Group {
// annotationBox = new AnnotationBox(); // annotationBox = new AnnotationBox();
// annotationBox.setFill(colour); // annotationBox.setFill(colour);
sail = new Polygon(0.0, BOAT_HEIGHT / 4,
0.0, BOAT_HEIGHT);
sailState = 0;
sail.setStrokeWidth(2.0);
sail.setStroke(Color.SILVER);
sail.setFill(Color.TRANSPARENT);
sail.setCache(true);
animateSail();
leftLayLine = new Line(); leftLayLine = new Line();
rightLayline = new Line(); rightLayline = new Line();
trail.getStrokeDashArray().setAll(5d, 10d); trail.getStrokeDashArray().setAll(5d, 10d);
trail.setCache(true); trail.setCache(true);
wake = new Wake(0, -BOAT_HEIGHT); wake = new Wake(0, -BOAT_HEIGHT);
wake.setVisible(true); wake.setVisible(true);
super.getChildren().addAll(boatPoly);//, annotationBox); super.getChildren().addAll(boatPoly, sail);//, annotationBox);
} }
public void setFill (Paint value) { public void setFill (Paint value) {
@@ -105,19 +115,29 @@ public class BoatObject extends Group {
/** /**
* Moves the boat and its children annotations to coordinates specified * Moves the boat and its children annotations to coordinates specified
* * @param x The X coordinate to move the boat to
* @param x The X coordinate to move the boat to
* @param y The Y coordinate to move the boat to * @param y The Y coordinate to move the boat to
* @param rotation The rotation by which the boat moves * @param rotation The rotation by which the boat moves
* @param velocity The velocity the boat is moving * @param velocity The velocity the boat is moving
* @param sailIn
*/ */
public void moveTo(double x, double y, double rotation, double velocity) { public void moveTo(double x, double y, double rotation, double velocity, Boolean sailIn) {
Double dx = Math.abs(boatPoly.getLayoutX() - x); Double dx = Math.abs(boatPoly.getLayoutX() - x);
Double dy = Math.abs(boatPoly.getLayoutY() - y); Double dy = Math.abs(boatPoly.getLayoutY() - y);
Platform.runLater(() -> { Platform.runLater(() -> {
rotateTo(rotation); rotateTo(rotation, sailIn);
boatPoly.setLayoutX(x); boatPoly.setLayoutX(x);
boatPoly.setLayoutY(y); boatPoly.setLayoutY(y);
sail.setLayoutX(x);
sail.setLayoutY(y);
if (!sailIn) {
animateSail();
} else {
sail.getPoints().clear();
sail.getPoints().addAll(0.0,BOAT_HEIGHT / 4,
0.0, BOAT_HEIGHT);
}
wake.setLayoutX(x); wake.setLayoutX(x);
wake.setLayoutY(y); wake.setLayoutY(y);
}); });
@@ -142,8 +162,24 @@ public class BoatObject extends Group {
} }
} }
private void rotateTo(double rotation) { private void rotateTo(double rotation, boolean sailsIn) {
boatPoly.getTransforms().setAll(new Rotate(rotation)); boatPoly.getTransforms().setAll(new Rotate(rotation));
if (sailsIn) {
sail.getTransforms().setAll(new Rotate(GameState.getWindDirection() + 95.0));
} else {
sail.getTransforms().setAll(new Rotate(GameState.getWindDirection()));
}
}
private void animateSail(){
Double[] points = new Double[100];
for (int i = 0; i < 50; i++) {
points[i * 2] = 5 * Math.sin(((Math.PI * i) / 25 + sailState));
points[i * 2 + 1] = (BOAT_HEIGHT * i) / 25 + BOAT_HEIGHT / 4;
}
sailState = sailState + Math.PI / 10;
sail.getPoints().clear();
sail.getPoints().addAll(points);
} }
public void updateLocation() { public void updateLocation() {
@@ -279,7 +315,7 @@ public class BoatObject extends Group {
public void setTrajectory(double heading, double velocity) { public void setTrajectory(double heading, double velocity) {
wake.setRotation(lastHeading - heading, velocity); wake.setRotation(lastHeading - heading, velocity);
rotateTo(heading); rotateTo(heading, false);
xVelocity = Math.cos(Math.toRadians(heading)) * velocity; xVelocity = Math.cos(Math.toRadians(heading)) * velocity;
yVelocity = Math.sin(Math.toRadians(heading)) * velocity; yVelocity = Math.sin(Math.toRadians(heading)) * velocity;
lastHeading = heading; lastHeading = heading;