From 65c0e6f77d7582420ad6cf71930d108e1a7b64e7 Mon Sep 17 00:00:00 2001 From: Calum Date: Thu, 27 Apr 2017 11:58:50 +1200 Subject: [PATCH] Fixed markergroup bugs. Improved wakes. Still WIP --- .../seng302/controllers/CanvasController.java | 59 +++++++++------ src/main/java/seng302/models/BoatGroup.java | 39 ++++++++-- src/main/java/seng302/models/Wake.java | 22 ++++-- .../java/seng302/models/mark/MarkGroup.java | 75 ++++++++++++++----- .../seng302/models/parsers/StreamPacket.java | 6 +- .../seng302/models/parsers/StreamParser.java | 3 +- 6 files changed, 142 insertions(+), 62 deletions(-) diff --git a/src/main/java/seng302/controllers/CanvasController.java b/src/main/java/seng302/controllers/CanvasController.java index 3891f3a1..bfdf6a08 100644 --- a/src/main/java/seng302/controllers/CanvasController.java +++ b/src/main/java/seng302/controllers/CanvasController.java @@ -37,7 +37,6 @@ public class CanvasController { private ResizableCanvas canvas; private Group group; private GraphicsContext gc; - private List boatGroups = new ArrayList<>(); private final int MARK_SIZE = 10; private final int BUFFER_SIZE = 25; @@ -154,6 +153,7 @@ public class CanvasController { Mark nextMark; //if (countdown == 0) { + //System.out.println("called the at"); for (RaceObject raceObject : raceObjects) { //if (currentRaceMarker[boatIndex] < marks.size()) { //if (currentRaceMarker[boatIndex] == 6) { @@ -166,18 +166,23 @@ public class CanvasController { //descending = nextMark.getY() > boatGroup.getLayoutY(); //leftToRight = nextMark.getX() < boatGroup.getLayoutX(); + raceObject.updatePosition(1000 / 60); for (int id : raceObject.getRaceIds()) { - if (StreamParser.boatPositions.containsKey(id)) { - Point3D p = StreamParser.boatPositions.get((long) id); - Point2D p2d = latLonToXY(p.getX(), p.getY()); - //System.out.println("p2d = " + p2d); + //System.out.println("id = " + id); + if (id != 0 && StreamParser.boatPositions.size() > 0) { + boolean test = StreamParser.boatPositions.containsKey(id); + if (StreamParser.boatPositions.containsKey((long) id)) { + Point3D p = StreamParser.boatPositions.get((long) id); + Point2D p2d = latLonToXY(p.getX(), p.getY()); + //System.out.println("p2d = " + p2d); //System.out.println("p.toString() = " + p.toString()); - double heading = 360.0 / 0xffff * p.getZ(); + double heading = 360.0 / 0xffff * p.getZ(); //System.out.println("heading = " + heading); - raceObject.setDestination(p2d.getX(), p2d.getY(), heading, id); + raceObject.setDestination(p2d.getX(), p2d.getY(), heading, id); + } + StreamParser.boatPositions.remove((long) id); } - StreamParser.boatPositions.remove((long) id); } //Point3D p = StreamParser.boatPositions.get((long) raceObject.getRaceIds()[0]); //System.out.println("boatGroup = " + boatGroup.getBoat().getId()); @@ -292,10 +297,11 @@ public class CanvasController { private void drawBoats() { // Map timelineInfos = raceViewController.getTimelineInfos(); List boats = raceViewController.getStartingBoats(); - Double startingX = group.getChildren().get(0).getLayoutX(); - Double startingY = group.getChildren().get(0).getLayoutY(); - Double firstMarkX = group.getChildren().get(1).getLayoutX(); - Double firstMarkY = group.getChildren().get(1).getLayoutY(); + System.out.println("raceObjects " + raceObjects); + Double startingX = raceObjects.get(0).getLayoutX(); + Double startingY = raceObjects.get(0).getLayoutY(); + Double firstMarkX = raceObjects.get(1).getLayoutX(); + Double firstMarkY = raceObjects.get(1).getLayoutY(); for (Boat boat : boats) { BoatGroup boatGroup = new BoatGroup(boat, Colors.getColor()); @@ -303,7 +309,7 @@ public class CanvasController { boatGroup.setDestination(firstMarkX, firstMarkY); boatGroup.forceRotation(); group.getChildren().add(boatGroup); - boatGroups.add(boatGroup); + raceObjects.add(boatGroup); // drawBoat(boat.getLongitude(), boat.getLatitude(), boat.getColor(), boat.getShortName(), boat.getSpeedInKnots(), boat.getHeading()); } } @@ -529,14 +535,16 @@ public class CanvasController { */ private void givePointsXY() { //Point2D canvasLocation; -// List allPoints = new ArrayList<>(raceViewController.getRace().getCourse()); - Set unqiuePoints = new HashSet<>(raceViewController.getRace().getCourse()); - System.out.println("unqiuePoints = " + unqiuePoints); + List allPoints = new ArrayList<>(raceViewController.getRace().getCourse()); + List processed = new ArrayList<>(); + //Set unqiuePoints = new HashSet<>(raceViewController.getRace().getCourse()); + //System.out.println("unqiuePoints = " + unqiuePoints); RaceObject markGroup; - for (Mark mark : unqiuePoints) { - if (mark.getMarkType() != MarkType.SINGLE_MARK) { - GateMark gateMark = (GateMark) mark; + for (Mark mark : allPoints) { + if (!processed.contains(mark)) { + if (mark.getMarkType() != MarkType.SINGLE_MARK) { + GateMark gateMark = (GateMark) mark; // canvasLocation = findScaledXY(gateMark.getSingleMark1()); // gateMark.getSingleMark1().setX((int) canvasLocation.getX()); // gateMark.getSingleMark1().setY((int) canvasLocation.getY()); @@ -545,14 +553,17 @@ public class CanvasController { // gateMark.getSingleMark2().setX((int) canvasLocation.getX()); // gateMark.getSingleMark2().setY((int) canvasLocation.getY()); - markGroup = new MarkGroup(mark, findScaledXY(gateMark.getSingleMark1()), findScaledXY(gateMark.getSingleMark2())); - group.getChildren().add(markGroup); - } - else { + markGroup = new MarkGroup(mark, findScaledXY(gateMark.getSingleMark1()), findScaledXY(gateMark.getSingleMark2())); + group.getChildren().add(markGroup); + raceObjects.add(markGroup); + } else { // canvasLocation = findScaledXY(mark); // mark.setX((int) canvasLocation.getX()); // mark.setY((int) canvasLocation.getY()); - markGroup = new MarkGroup(mark, findScaledXY(mark)); + markGroup = new MarkGroup(mark, findScaledXY(mark)); + group.getChildren().add(markGroup); + } + processed.add(mark); } } } diff --git a/src/main/java/seng302/models/BoatGroup.java b/src/main/java/seng302/models/BoatGroup.java index a37f1852..3b314ab8 100644 --- a/src/main/java/seng302/models/BoatGroup.java +++ b/src/main/java/seng302/models/BoatGroup.java @@ -7,6 +7,9 @@ import javafx.scene.text.Text; import javafx.scene.transform.Rotate; import javafx.scene.transform.Translate; +import java.util.ArrayList; +import java.util.List; + /** * Created by CJIRWIN on 25/04/2017. */ @@ -21,10 +24,11 @@ public class BoatGroup extends RaceObject{ private static final double BOAT_WIDTH = 10d; //Time between sections of race - Should be changed to 200 for actual program. private static double expectedUpdateInterval = 200; - private static int WAKE_FRAME_INTERVAL = 40; + private static int WAKE_FRAME_INTERVAL = 80; private Boat boat; private int wakeCounter = WAKE_FRAME_INTERVAL; + private List wakes = new ArrayList<>(); public BoatGroup (Boat boat, Color color){ this.boat = boat; @@ -107,21 +111,40 @@ public class BoatGroup extends RaceObject{ rotation = rotationalVelocity * timeInterval; } moveGroupBy(dx, dy, rotation); - for (Node wake : super.getChildren().subList(4, super.getChildren().size())) { - if (!((Wake) wake).updatePosition(timeInterval)) +// if (super.getChildren().size() > 3) { +// for (Node wake : super.getChildren().subList(4, super.getChildren().size())) { +// if (!((Wake) wake).updatePosition(timeInterval)) +// super.getChildren().remove(wake); +// } +// } + for (Wake wake : wakes) { + if (wake.updatePosition(timeInterval)) { super.getChildren().remove(wake); + } } if (wakeCounter-- == 0) { wakeCounter = WAKE_FRAME_INTERVAL; - super.getChildren().add( - new Wake( - super.getLayoutX(), super.getLayoutY(), pixelVelocityX, pixelVelocityY - ) - ); + if (pixelVelocityX > 0 && pixelVelocityY > 0) { +// super.getChildren().add( +// new Wake( +// super.getLayoutX() + BOAT_HEIGHT, super.getLayoutY() + BOAT_HEIGHT, pixelVelocityX, pixelVelocityY +// ) +// ); + Wake wake = new Wake( + super.getLayoutX(), + super.getLayoutY(), + pixelVelocityX, + pixelVelocityY + ); + super.getChildren().add(wake); + wakes.add(wake); + } + } } public void setDestination (double newXValue, double newYValue, double rotation, int... raceIds) { + //System.out.println("MADE IT"); if (hasRaceId(raceIds)) { this.pixelVelocityX = (newXValue - super.getLayoutX()) / expectedUpdateInterval; this.pixelVelocityY = (newYValue - super.getLayoutY()) / expectedUpdateInterval; diff --git a/src/main/java/seng302/models/Wake.java b/src/main/java/seng302/models/Wake.java index 25e5ea59..d2d80764 100644 --- a/src/main/java/seng302/models/Wake.java +++ b/src/main/java/seng302/models/Wake.java @@ -10,27 +10,35 @@ import javafx.scene.shape.ArcType; class Wake extends Arc { private static int VELOCITY_SCALE_FACTOR = 3; - private static int MAX_LIFESPAN = 180; + private static int MAX_LIFESPAN = 420; private static double LIFESPAN_PER_FRAME = 1.0 / MAX_LIFESPAN; + //private static double LENGTH_PER_FRAME = 120 / MAX_LIFESPAN; + private static double LENGTH_PER_FRAME = 0.08; private double velocityX; private double velocityY; + private double opacity; private int lifespan = MAX_LIFESPAN; Wake (double startingX, double startingY, double velocityX, double velocityY) { - super(startingX, startingY, 25, 15, 160, 40); - super.setFill(Color.BLUE); + super(0, 0, 20, 30, 180, 0); + //super.setFill(Color.BLUE); + super.setStroke(Color.BLUE); super.setType(ArcType.OPEN); super.setStrokeWidth(2.0); - this.velocityX = -velocityX; - this.velocityY = -velocityY; + this.velocityX = -velocityX / 2; + this.velocityY = -velocityY / 2; } boolean updatePosition (double timeInterval) { lifespan--; + //super.setOpacity(LIFESPAN_PER_FRAME * lifespan * super.getOpacity()); + opacity = LIFESPAN_PER_FRAME * lifespan * opacity; + super.setFill(new Color(0.0f, 0.0f, 1.0f, opacity)); super.setLayoutX(super.getLayoutX() + velocityX * timeInterval); - super.setLayoutX(super.getLayoutX() + velocityX * timeInterval); - super.setOpacity(LIFESPAN_PER_FRAME * lifespan * super.getOpacity()); + super.setLayoutY(super.getLayoutY() + velocityY * timeInterval); + super.setStartAngle(super.getStartAngle() - LENGTH_PER_FRAME); + super.setLength(super.getLength() + LENGTH_PER_FRAME * 2); return lifespan == 0; } diff --git a/src/main/java/seng302/models/mark/MarkGroup.java b/src/main/java/seng302/models/mark/MarkGroup.java index c15d8f8c..55581ade 100644 --- a/src/main/java/seng302/models/mark/MarkGroup.java +++ b/src/main/java/seng302/models/mark/MarkGroup.java @@ -28,6 +28,10 @@ public class MarkGroup extends RaceObject { private Point2D[] nodeDestinations; public MarkGroup (Mark mark, Point2D... points) { +// for (Point2D p : points) { +// System.out.println("p.getX() = " + p.getX()); +// System.out.println("p.getY() = " + p.getY()); +// } marks.add(mark); mainMark = mark; Color color = Color.BLACK; @@ -36,43 +40,75 @@ public class MarkGroup extends RaceObject { } else if (mark.getName().equals("Finish")){ color = Color.RED; } + System.out.println("HERE ARE THE CHILDREN LOL"); if (mark.getMarkType() == MarkType.SINGLE_MARK) { super.getChildren().add(new Circle(0, 0, MARK_RADIUS, color)); +// System.out.println("SingleMark?"); +// System.out.println("super.getChildren().get(0).getLayoutX() = " + super.getChildren().get(0).getLayoutX()); +// System.out.println("super.getChildren().get(0).getLayoutY() = " + super.getChildren().get(0).getLayoutY()); } else { marks.add(((GateMark) mark).getSingleMark1()); marks.add(((GateMark) mark).getSingleMark2()); super.getChildren().add( new Circle( - (points[1].getX() - points[0].getX() / 2), - (points[1].getY() - points[0].getY() / 2), + (points[1].getX() - points[0].getX()) / 2d, + (points[1].getY() - points[0].getY()) / 2d, MARK_RADIUS, color ) ); +// super.getChildren().add(new Circle(0, 0, MARK_RADIUS, color)); +// super.getChildren().get(0).setLayoutX((points[1].getX() - points[0].getX()) / 2d); +// super.getChildren().get(0).setLayoutY((points[1].getY() - points[0].getY()) / 2d); +// System.out.println("!!!!!!!!!!!!!!!!!"); +// System.out.println((points[1].getX() - points[0].getX()) / 2d); +// System.out.println((points[1].getY() - points[0].getY()) / 2d); +// System.out.println(super.getChildren().get(0)); super.getChildren().add( new Circle( - -(points[1].getX() - points[0].getX() / 2), - -(points[1].getY() - points[0].getY() / 2), + -(points[1].getX() - points[0].getX()) / 2d, + -(points[1].getY() - points[0].getY()) / 2d, MARK_RADIUS, color ) ); +// super.getChildren().add(new Circle(0, 0, MARK_RADIUS, color)); +// super.getChildren().get(1).setLayoutX(-(points[1].getX() - points[0].getX()) / 2d); +// super.getChildren().get(1).setLayoutY(-(points[1].getY() - points[0].getY()) / 2d); Line line = new Line( - 0, - 0, - points[1].getX() - points[0].getX(), - points[1].getY() - points[0].getY() + (points[1].getX() - points[0].getX()) / 2d, + (points[1].getY() - points[0].getY()) / 2d, + -(points[1].getX() - points[0].getX()) / 2d, + -(points[1].getY() - points[0].getY()) / 2d ); line.setStrokeWidth(LINE_THICKNESS); + line.setStroke(color); if (mark.getMarkType() == MarkType.OPEN_GATE) { line.getStrokeDashArray().addAll(DASHED_GAP_LEN, DASHED_LINE_LEN); } super.getChildren().add(line); - nodePixelVelocitiesX = new double[2]; - nodePixelVelocitiesY = new double[2]; - nodeDestinations = new Point2D[2]; + nodePixelVelocitiesX = new double[]{0d,0d}; + nodePixelVelocitiesY = new double[]{0d,0d}; + nodeDestinations = new Point2D[]{ + new Point2D(super.getChildren().get(0).getLayoutX(), super.getChildren().get(0).getLayoutY()), + new Point2D(super.getChildren().get(1).getLayoutX(), super.getChildren().get(1).getLayoutY()) + }; +// nodeDestinations = new Point2D[]{new Point2D(0,0), new Point2D(0,0)}; +// System.out.println("super.getChildren().get(0).getLayoutX() = " + super.getChildren().get(0).getLayoutX()); +// System.out.println("super.getChildren().get(0).getLayoutY() = " + super.getChildren().get(0).getLayoutY()); +// System.out.println("super.getChildren().get(1).getLayoutX() = " + super.getChildren().get(1).getLayoutX()); +// System.out.println("super.getChildren().get(1).getLayoutY() = " + super.getChildren().get(1).getLayoutY()); } moveTo(points[0].getX(), points[0].getY()); +// System.out.println("OKAY HERE IS A MARK"); +// System.out.println("super.getLayoutX() = " + super.getLayoutX()); +// System.out.println("super.getLayoutY() = " + super.getLayoutY()); +// System.out.println("super.getChildren().get(0).getLayoutX() = " + super.getChildren().get(0).getLayoutX()); +// System.out.println("super.getChildren().get(0).getLayoutY() = " + super.getChildren().get(0).getLayoutY()); +// pixelVelocityX = 0; +// pixelVelocityY = 0; +// rotationalVelocity = 0; +// rotationalGoal = 0; } public void setDestination (double x, double y, double rotation, int... raceIds) { @@ -118,13 +154,14 @@ public class MarkGroup extends RaceObject { public void rotateTo (double rotation) { super.getTransforms().clear(); - super.getTransforms().add( - new Rotate( - rotation, - super.getChildren().get(1).getLayoutX() - super.getChildren().get(0).getLayoutX(), - super.getChildren().get(1).getLayoutY() - super.getChildren().get(0).getLayoutY() - ) - ); +// super.getTransforms().add( +// new Rotate( +// rotation, +// super.getChildren().get(1).getLayoutX() - super.getChildren().get(0).getLayoutX(), +// super.getChildren().get(1).getLayoutY() - super.getChildren().get(0).getLayoutY() +// ) +// ); + super.getTransforms().add(new Rotate(rotation, 0 , 0)); } public void updatePosition (double timeInterval) { @@ -137,7 +174,7 @@ public class MarkGroup extends RaceObject { public void moveGroupBy (double x, double y, double rotation) { super.setLayoutX(super.getLayoutX() + x); - super.setLayoutY(super.getOpacity() + y); + super.setLayoutY(super.getLayoutY() + y); rotateTo(rotation + currentRotation); } diff --git a/src/main/java/seng302/models/parsers/StreamPacket.java b/src/main/java/seng302/models/parsers/StreamPacket.java index e4b929ca..c27f9654 100644 --- a/src/main/java/seng302/models/parsers/StreamPacket.java +++ b/src/main/java/seng302/models/parsers/StreamPacket.java @@ -17,11 +17,11 @@ public class StreamPacket { this.messageLength = messageLength; this.timeStamp = timeStamp; this.payload = payload; - System.out.println("type = " + this.type.toString()); + //System.out.println("type = " + this.type.toString()); //switch the packet type to deal with what ever specific packet you want to deal with if (this.type == PacketType.XML_MESSAGE){ - System.out.println("--------"); - System.out.println(new String(payload)); + //System.out.println("--------"); + //System.out.println(new String(payload)); StreamParser.parsePacket(this); } } diff --git a/src/main/java/seng302/models/parsers/StreamParser.java b/src/main/java/seng302/models/parsers/StreamParser.java index 438bc5b9..68572c6a 100644 --- a/src/main/java/seng302/models/parsers/StreamParser.java +++ b/src/main/java/seng302/models/parsers/StreamParser.java @@ -96,7 +96,8 @@ public class StreamParser extends Thread{ extractAvgWind(packet); break; default: - System.out.println(packet.getType().toString()); + break; + //System.out.println(packet.getType().toString()); } }