mirror of
https://github.com/michaelrausch/Party-Parrots-At-Sea.git
synced 2026-05-09 14:28:43 +00:00
Fix for some movement on racestart issues
#bug
This commit is contained in:
@@ -7,33 +7,46 @@ import javafx.scene.shape.Line;
|
||||
import javafx.scene.shape.Polygon;
|
||||
import javafx.scene.text.Text;
|
||||
import javafx.scene.transform.Rotate;
|
||||
import seng302.models.parsers.StreamParser;
|
||||
import javafx.stage.Stage;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* BoatGroup is a javafx group that by default contains a graphical objects for representing a 2 dimensional boat.
|
||||
* It contains a single polygon for the boat, a group of lines to show it's path, a wake object and two text labels to
|
||||
* annotate the boat teams name and the boats velocity.
|
||||
* annotate the boat teams name and the boats velocity. The boat will update it's position onscreen everytime
|
||||
* UpdatePosition is called unless the window is minimized in which case it attempts to store animations and apply them
|
||||
* when the window is maximised.
|
||||
*/
|
||||
public class BoatGroup extends RaceObject{
|
||||
|
||||
//Constants for drawing
|
||||
private static final double TEAMNAME_X_OFFSET = 10d;
|
||||
private static final double TEAMNAME_Y_OFFSET = -15d;
|
||||
private static final double VELOCITY_X_OFFSET = 10d;
|
||||
private static final double VELOCITY_Y_OFFSET = -5d;
|
||||
private static final double BOAT_HEIGHT = 15d;
|
||||
private static final double BOAT_WIDTH = 10d;
|
||||
private static double expectedUpdateInterval = 200;
|
||||
private boolean destinationSet;
|
||||
//Variables for boat logic.
|
||||
private Point2D lastPoint;
|
||||
private int wakeGenerationDelay = 10;
|
||||
private double distanceTravelled;
|
||||
|
||||
//Graphical objects
|
||||
private Boat boat;
|
||||
private Group lineGroup = new Group();
|
||||
private Polygon boatPoly;
|
||||
private Text teamNameObject;
|
||||
private Text velocityObject;
|
||||
private Wake wake;
|
||||
//Handles boat moving when connecting to a stream
|
||||
private boolean setToInitialLocation = false;
|
||||
private boolean destinationSet;
|
||||
//Variables for handling minimization
|
||||
private Stage stage;
|
||||
private boolean isMaximized= true;
|
||||
private List<Line> lineStorage = new ArrayList<>();
|
||||
private int setCallCount = 5;
|
||||
|
||||
/**
|
||||
* Creates a BoatGroup with the default triangular boat polygon.
|
||||
@@ -146,62 +159,65 @@ public class BoatGroup extends RaceObject{
|
||||
*/
|
||||
public void updatePosition (long timeInterval) {
|
||||
//Calculate the movement of the boat.
|
||||
double dx = pixelVelocityX * timeInterval;
|
||||
double dy = pixelVelocityY * timeInterval;
|
||||
double rotation = rotationalVelocity * timeInterval;
|
||||
distanceTravelled += Math.abs(dx) + Math.abs(dy);
|
||||
moveGroupBy(dx, dy, rotation);
|
||||
//Draw a new section of the trail every 20 pixels of movement.
|
||||
if (distanceTravelled > 20) {
|
||||
distanceTravelled = 0;
|
||||
if (lastPoint != null) {
|
||||
Line l = new Line(
|
||||
lastPoint.getX(),
|
||||
lastPoint.getY(),
|
||||
boatPoly.getLayoutX(),
|
||||
boatPoly.getLayoutY()
|
||||
);
|
||||
l.getStrokeDashArray().setAll(3d, 7d);
|
||||
l.setStroke(boatPoly.getFill());
|
||||
lineGroup.getChildren().add(l);
|
||||
}
|
||||
if (destinationSet){ //Only begin drawing after the first destination is set
|
||||
lastPoint = new Point2D(boatPoly.getLayoutX(), boatPoly.getLayoutY());
|
||||
if (isMaximized) {
|
||||
double dx = pixelVelocityX * timeInterval;
|
||||
double dy = pixelVelocityY * timeInterval;
|
||||
double rotation = rotationalVelocity * timeInterval;
|
||||
distanceTravelled += Math.abs(dx) + Math.abs(dy);
|
||||
moveGroupBy(dx, dy, rotation);
|
||||
//Draw a new section of the trail every 20 pixels of movement.
|
||||
if (distanceTravelled > 20) {
|
||||
distanceTravelled = 0;
|
||||
if (lastPoint != null) {
|
||||
Line l = new Line(
|
||||
lastPoint.getX(),
|
||||
lastPoint.getY(),
|
||||
boatPoly.getLayoutX(),
|
||||
boatPoly.getLayoutY()
|
||||
);
|
||||
l.getStrokeDashArray().setAll(3d, 7d);
|
||||
l.setStroke(boatPoly.getFill());
|
||||
lineGroup.getChildren().add(l);
|
||||
}
|
||||
if (destinationSet) { //Only begin drawing after the first destination is set
|
||||
lastPoint = new Point2D(boatPoly.getLayoutX(), boatPoly.getLayoutY());
|
||||
}
|
||||
}
|
||||
wake.updatePosition(timeInterval);
|
||||
}
|
||||
wake.updatePosition(timeInterval);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the destination of the boat and the headng it should have once it reaches
|
||||
* @param newXValue
|
||||
* @param newYValue
|
||||
* @param newXValue The X co-ordinate the boat needs to move to.
|
||||
* @param newYValue The Y co-ordinate the boat needs to move to.
|
||||
* @param rotation Rotation to move graphics to.
|
||||
* @param raceIds RaceID of the object to move.
|
||||
*/
|
||||
public void setDestination (double newXValue, double newYValue, double rotation, double groundSpeed, int... raceIds) {
|
||||
if (hasRaceId(raceIds)) {
|
||||
destinationSet = true;
|
||||
boat.setVelocity(groundSpeed);
|
||||
if (currentRotation < 0)
|
||||
currentRotation = 360 - currentRotation;
|
||||
double dx = newXValue - boatPoly.getLayoutX();
|
||||
if ((dx > 0 && pixelVelocityX < 0) || (dx < 0 && pixelVelocityX > 0)) {
|
||||
pixelVelocityX = 0;
|
||||
} else {
|
||||
pixelVelocityX = dx / expectedUpdateInterval;
|
||||
}
|
||||
double dy = newYValue - boatPoly.getLayoutY();
|
||||
//Check movement is reasonable. Assumes a 1000 * 1000 canvas
|
||||
if (Math.abs(dx) > 50 || Math.abs(dy) > 50) {
|
||||
if (setToInitialLocation) {
|
||||
destinationSet = true;
|
||||
boat.setVelocity(groundSpeed);
|
||||
if (currentRotation < 0)
|
||||
currentRotation = 360 - currentRotation;
|
||||
double dx = newXValue - boatPoly.getLayoutX();
|
||||
// if ((dx > 0 && pixelVelocityX < 0) || (dx < 0 && pixelVelocityX > 0)) {
|
||||
// pixelVelocityX = 0;
|
||||
// } else {
|
||||
pixelVelocityX = dx / expectedUpdateInterval;
|
||||
// }
|
||||
double dy = newYValue - boatPoly.getLayoutY();
|
||||
//Check movement is reasonable. Assumes a 1000 * 1000 canvas
|
||||
if (Math.abs(dx) > 50 || Math.abs(dy) > 50) {
|
||||
// System.out.println("dx = " + dx);
|
||||
// System.out.println("dy = " + dy);
|
||||
dx = 0;
|
||||
dy = 0;
|
||||
moveTo(newXValue, newYValue);
|
||||
}
|
||||
//Slight delay on changing X/Y direction that could help jitter. Disabled since there was an issue with
|
||||
//packets that might be causing it.
|
||||
dx = 0;
|
||||
dy = 0;
|
||||
moveTo(newXValue, newYValue);
|
||||
}
|
||||
//Slight delay on changing X/Y direction that could help jitter. Disabled since there was an issue with
|
||||
//packets that might be causing it.
|
||||
// if ((dx > 0 && pixelVelocityX < 0) || (dx < 0 && pixelVelocityX > 0)) {
|
||||
// pixelVelocityX = 0;
|
||||
// } else {
|
||||
@@ -212,17 +228,46 @@ public class BoatGroup extends RaceObject{
|
||||
// } else {
|
||||
// pixelVelocityY = dy / expectedUpdateInterval;
|
||||
// }
|
||||
pixelVelocityX = dx / expectedUpdateInterval;
|
||||
pixelVelocityY = dy / expectedUpdateInterval;
|
||||
rotationalGoal = rotation;
|
||||
calculateRotationalVelocity();
|
||||
if (wakeGenerationDelay > 0) {
|
||||
wake.rotate(rotationalGoal);
|
||||
wakeGenerationDelay--;
|
||||
pixelVelocityX = dx / expectedUpdateInterval;
|
||||
pixelVelocityY = dy / expectedUpdateInterval;
|
||||
rotationalGoal = rotation;
|
||||
calculateRotationalVelocity();
|
||||
if (wakeGenerationDelay > 0) {
|
||||
wake.rotate(rotationalGoal);
|
||||
rotateTo(rotationalGoal);
|
||||
rotationalVelocity = 0;
|
||||
wakeGenerationDelay--;
|
||||
} else {
|
||||
wake.setRotationalVelocity(rotationalVelocity, currentRotation, boat.getVelocity());
|
||||
}
|
||||
velocityObject.setText(String.format("%.2f m/s", boat.getVelocity()));
|
||||
} else {
|
||||
wake.setRotationalVelocity(rotationalVelocity, currentRotation, boat.getVelocity());
|
||||
setToInitialLocation = true;
|
||||
rotationalGoal = rotation;
|
||||
moveTo(newXValue, newYValue, rotation);
|
||||
}
|
||||
}
|
||||
//If minimized generate lines every 5 calls to set destination.
|
||||
if (!isMaximized) {
|
||||
setToInitialLocation = false;
|
||||
wakeGenerationDelay = 2;
|
||||
if(setCallCount-- == 0) {
|
||||
setCallCount = 5;
|
||||
if (lastPoint != null) {
|
||||
Line l = new Line(
|
||||
lastPoint.getX(),
|
||||
lastPoint.getY(),
|
||||
newXValue,
|
||||
newYValue
|
||||
);
|
||||
l.getStrokeDashArray().setAll(3d, 7d);
|
||||
l.setStroke(boatPoly.getFill());
|
||||
lineStorage.add(l);
|
||||
}
|
||||
if (destinationSet) { //Only begin drawing after the first destination is set
|
||||
lastPoint = new Point2D(newXValue, newYValue);
|
||||
}
|
||||
}
|
||||
velocityObject.setText(String.format("%.2f m/s", boat.getVelocity()));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -307,4 +352,22 @@ public class BoatGroup extends RaceObject{
|
||||
group.getChildren().addAll(wake, lineGroup);
|
||||
return group;
|
||||
}
|
||||
|
||||
/**
|
||||
* Use this function to let the BoatGroup know about the stage it is in. If it knows about it's stage then it will
|
||||
* listen to the iconified property of that stage and change it's behaviour upon minimization. Without setting the
|
||||
* Stage there is guarantee that the BoatGroup will draw properly when the stage is minimized.
|
||||
*
|
||||
* @param stage The stage that the BoatGroup is added to.
|
||||
*/
|
||||
public void setStage (Stage stage) {
|
||||
this.stage = stage;
|
||||
this.stage.iconifiedProperty().addListener(e -> {
|
||||
isMaximized = !stage.isIconified();
|
||||
if (!lineStorage.isEmpty()) {
|
||||
lineGroup.getChildren().addAll(lineStorage);
|
||||
lineStorage.clear();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user