From 765f27f9877e1a9936084e2c7b655f3a486e346a Mon Sep 17 00:00:00 2001 From: Calum Date: Fri, 28 Apr 2017 17:08:08 +1200 Subject: [PATCH] Starting new wake implementation. --- src/main/java/seng302/models/BoatGroup.java | 97 +++++++++++-------- src/main/java/seng302/models/RaceObject.java | 2 +- src/main/java/seng302/models/Wake2.java | 86 ++++++++++++++++ .../java/seng302/models/mark/MarkGroup.java | 2 +- 4 files changed, 143 insertions(+), 44 deletions(-) create mode 100644 src/main/java/seng302/models/Wake2.java diff --git a/src/main/java/seng302/models/BoatGroup.java b/src/main/java/seng302/models/BoatGroup.java index 44f5aa1f..dd28b6b5 100644 --- a/src/main/java/seng302/models/BoatGroup.java +++ b/src/main/java/seng302/models/BoatGroup.java @@ -1,6 +1,7 @@ package seng302.models; import javafx.geometry.Point2D; +import javafx.scene.Group; import javafx.scene.Node; import javafx.scene.paint.Color; import javafx.scene.shape.Line; @@ -24,20 +25,23 @@ public class BoatGroup extends RaceObject{ private static final double VELOCITY_WAKE_RATIO = 2d; private static final double BOAT_HEIGHT = 15d; private static final double BOAT_WIDTH = 10d; + private static final int LINE_INTERVAL = 120; //Time between sections of race - Should be changed to 200 for actual program. private static double expectedUpdateInterval = 200; private static int WAKE_FRAME_INTERVAL = 30; - private double framesForNewLine = 0; + private double framesForNewLine = LINE_INTERVAL; private Point2D lastPoint; private Boat boat; private int wakeCounter = WAKE_FRAME_INTERVAL; private List wakes = new ArrayList<>(); - private List lines = new ArrayList<>(); + //private List lines = new ArrayList<>(); + private Group lines = new Group(); private Polygon boatPoly; // private Polygon wakePoly; private Text teamNameObject; private Text velocityObject; + private Wake2 wake; public BoatGroup (Boat boat, Color color){ this.boat = boat; @@ -74,8 +78,9 @@ public class BoatGroup extends RaceObject{ velocityObject.setY(VELOCITY_Y_OFFSET); velocityObject.relocate(velocityObject.getX(), velocityObject.getY()); + wake = new Wake2(0, 0, 0); // super.getChildren().addAll(wakePoly, boatPoly, teamNameObject, velocityObject); - super.getChildren().addAll(teamNameObject, velocityObject, boatPoly); + super.getChildren().addAll(lines, wake, teamNameObject, velocityObject, boatPoly); } private void initChildren (Color color) { @@ -119,11 +124,12 @@ public class BoatGroup extends RaceObject{ teamNameObject.setLayoutY(y); velocityObject.setLayoutX(x); velocityObject.setLayoutY(y); + wake.moveTo(x, y, currentRotation); // wakePoly.setLayoutX(x); // wakePoly.setLayoutY(y); } - public void updatePosition (double timeInterval) { + public void updatePosition (long timeInterval) { double dx = pixelVelocityX * timeInterval; double dy = pixelVelocityY * timeInterval; double rotation = 0d; @@ -139,44 +145,51 @@ public class BoatGroup extends RaceObject{ // super.getChildren().remove(wake); // } // } - for (Wake wake : wakes) { - if (wake.updatePosition(timeInterval)) { - super.getChildren().remove(wake); - } - } - if (wakeCounter-- == 0) { -// if (boat.getShortName().equals("BAR")) -// System.out.println("thinking"); - wakeCounter = WAKE_FRAME_INTERVAL; - if (pixelVelocityX > 0 && pixelVelocityY > 0) { -// super.getChildren().add( -// new Wake( -// super.getLayoutX() + BOAT_HEIGHT, super.getLayoutY() + BOAT_HEIGHT, pixelVelocityX, pixelVelocityY -// ) -// ); - Wake wake = new Wake( - boatPoly.getLayoutX(), - boatPoly.getLayoutY(), - pixelVelocityX, - pixelVelocityY, rotation); -// wake.getTransforms().clear(); -// wake.getTransforms().add(new Rotate(rotation, 0, 0)); - super.getChildren().add(wake); - wakes.add(wake); - } - - } +// for (Wake wake : wakes) { +// if (wake.updatePosition(timeInterval)) { +// super.getChildren().remove(wake); +// } +// } +// if (wakeCounter-- == 0) { +//// if (boat.getShortName().equals("BAR")) +//// System.out.println("thinking"); +// wakeCounter = WAKE_FRAME_INTERVAL; +// if (pixelVelocityX > 0 && pixelVelocityY > 0) { +//// super.getChildren().add( +//// new Wake( +//// super.getLayoutX() + BOAT_HEIGHT, super.getLayoutY() + BOAT_HEIGHT, pixelVelocityX, pixelVelocityY +//// ) +//// ); +// Wake wake = new Wake( +// boatPoly.getLayoutX(), +// boatPoly.getLayoutY(), +// pixelVelocityX, +// pixelVelocityY, rotation); +//// wake.getTransforms().clear(); +//// wake.getTransforms().add(new Rotate(rotation, 0, 0)); +// super.getChildren().add(wake); +// wakes.add(wake); +// } +// +// } if (framesForNewLine == 0) { - framesForNewLine = 121; + framesForNewLine = LINE_INTERVAL; if (lastPoint != null) { - Line l = new Line(lastPoint.getX(), lastPoint.getY(), boatPoly.getLayoutX(), boatPoly.getLayoutY()); - l.getStrokeDashArray().setAll(4d, 4d); - lines.add(l); - super.getChildren().add(l); + Line l = new Line( + lastPoint.getX(), + lastPoint.getY(), + boatPoly.getLayoutX(), + boatPoly.getLayoutY() + ); + l.getStrokeDashArray().setAll(4d, 6d); + l.setStroke(boatPoly.getFill()); + //lines.add(l); + lines.getChildren().add(l); } lastPoint = new Point2D(boatPoly.getLayoutX(), boatPoly.getLayoutY()); } framesForNewLine -= 1; + wake.updatePosition(timeInterval); } public void setDestination (double newXValue, double newYValue, double rotation, int... raceIds) { @@ -187,6 +200,7 @@ public class BoatGroup extends RaceObject{ this.rotationalGoal = rotation; calculateRotationalVelocity(); rotateTo(rotation); + wake.setRotationalVelocity(rotationalVelocity); } } @@ -213,11 +227,12 @@ public class BoatGroup extends RaceObject{ } public void rotateTo (double rotation) { - if(rotation != 0) { - rotationalGoal = rotation; + //if(rotation != 0) { + //rotationalGoal = rotation; + currentRotation = rotation; boatPoly.getTransforms().clear(); boatPoly.getTransforms().add(new Rotate(rotation, BOAT_WIDTH / 2, 0)); - } + //} // wakePoly.getTransforms().clear(); // wakePoly.getTransforms().add(new Rotate(rotation, 0, 0)); } @@ -232,9 +247,7 @@ public class BoatGroup extends RaceObject{ for (Wake wake : wakes) { wake.setVisible(!wake.isVisible()); } - for (Line line : lines) { - line.setVisible(!line.isVisible()); - } + lines.setVisible(!lines.isVisible()); } public Boat getBoat() { diff --git a/src/main/java/seng302/models/RaceObject.java b/src/main/java/seng302/models/RaceObject.java index a5dcf6f5..9ec22fd5 100644 --- a/src/main/java/seng302/models/RaceObject.java +++ b/src/main/java/seng302/models/RaceObject.java @@ -49,7 +49,7 @@ public abstract class RaceObject extends Group { public abstract void setDestination (double x, double y, int... raceIds); - public abstract void updatePosition (double timeInterval); + public abstract void updatePosition (long timeInterval); public abstract void moveTo (double x, double y, double rotation); diff --git a/src/main/java/seng302/models/Wake2.java b/src/main/java/seng302/models/Wake2.java new file mode 100644 index 00000000..98090a45 --- /dev/null +++ b/src/main/java/seng302/models/Wake2.java @@ -0,0 +1,86 @@ +package seng302.models; + +import javafx.scene.paint.Color; +import javafx.scene.shape.Arc; +import javafx.scene.transform.Rotate; + +/** + * Created by cir27 on 28/04/17. + */ +class Wake2 extends Arc { + private final double OPACITY_INCREASE = -0.2; + private final double RADIUS_INCREASE = 10; + private final int DELAY = 60; + private double opacity; + private double rotationalVelocity; + private Wake2 subWake; + private boolean hasSubWake = false; + private int delayCount = DELAY; + + Wake2 (double startingX, double startingY, double rotationalVelocity) { + super(startingX, startingY, 30, 30, 150, 60); + opacity = 1.0; + super.setFill(new Color(0.0, 0.0, 0.0, opacity)); + subWake = new Wake2( + startingX, + startingY, + super.getRadiusX() + RADIUS_INCREASE, + rotationalVelocity, + opacity + OPACITY_INCREASE + ); + hasSubWake = true; + this.rotationalVelocity = rotationalVelocity; + + } + Wake2 (double startingX, double startingY, double radius, double rotationalVelocity, double opacity) { + super(startingX, startingY, radius, radius, 150, 60); + super.setFill(new Color(0.0, 0.0, 0.0, opacity)); + this.opacity = opacity; + if (!(opacity < 0)) { + subWake = new Wake2( + startingX, + startingY, + super.getRadiusX() + RADIUS_INCREASE, + rotationalVelocity, + opacity + OPACITY_INCREASE + ); + hasSubWake = true; + } + this.rotationalVelocity = rotationalVelocity; + } + + void setRotationalVelocity (double rotationalVelocity) { + this.rotationalVelocity = rotationalVelocity; + delayCount = DELAY; + } + + void updatePosition (long timeInterval) { + if (delayCount-- == 0) + subWake.setRotationalVelocity(rotationalVelocity); + super.getTransforms().clear(); + super.getTransforms().add( + new Rotate( + rotationalVelocity * timeInterval, + super.getCenterX(), + super.getCenterY() + ) + ); + if(hasSubWake) + subWake.updatePosition(timeInterval); + } + + void moveTo (double x, double y, double rotation) { + super.setLayoutX(x); + super.setLayoutY(y); + super.getTransforms().clear(); + super.getTransforms().add( + new Rotate( + rotation, + super.getCenterX(), + super.getCenterY() + ) + ); + if(hasSubWake) + subWake.moveTo(x, y, rotation); + } +} diff --git a/src/main/java/seng302/models/mark/MarkGroup.java b/src/main/java/seng302/models/mark/MarkGroup.java index eba7da82..caf260fe 100644 --- a/src/main/java/seng302/models/mark/MarkGroup.java +++ b/src/main/java/seng302/models/mark/MarkGroup.java @@ -156,7 +156,7 @@ public class MarkGroup extends RaceObject { super.getTransforms().add(new Rotate(rotation, 0 , 0)); } - public void updatePosition (double timeInterval) { + public void updatePosition (long timeInterval) { double x = pixelVelocityX * timeInterval; double y = pixelVelocityY * timeInterval; double rotation = rotationalVelocity * timeInterval;