Mostly got boats going to marks on the canvas (code currently broken) #story[377] #pair[zyt10, ptg19]

This commit is contained in:
zyt10
2017-03-17 16:18:01 +13:00
parent 6383b9a6f8
commit 683f4ba94e
5 changed files with 267 additions and 74 deletions
@@ -11,6 +11,13 @@ import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;
import javafx.util.Duration;
import seng302.models.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import static java.lang.Math.abs;
/**
* Created by ptg19 on 15/03/17.
@@ -18,19 +25,73 @@ import javafx.util.Duration;
public class CanvasController {
@FXML private Canvas canvas;
public void initialize() {
GraphicsContext gc = canvas.getGraphicsContext2D();
gc.scale(5,5);
RaceController raceController = new RaceController();
raceController.initializeRace();
Race race = raceController.getRace();
HashMap<Boat, TimelineInfo> timelineInfos = new HashMap<>();
HashMap<Boat, List> boat_events = race.getEvents();
// System.out.println(boat_events);
for (Boat boat : boat_events.keySet()) {
DoubleProperty x = new SimpleDoubleProperty();
DoubleProperty y = new SimpleDoubleProperty();
List<KeyFrame> keyFrames = new ArrayList<>();
List<Event> events = boat_events.get(boat);
for (Event event: events){
keyFrames.add(
// new KeyFrame(Duration.seconds(event.getDistanceBetweenMarks()/event.getBoat().getVelocity()),
new KeyFrame(Duration.seconds(event.getTime()/60/60/5),
new KeyValue(x, event.getMark().getLatitude()),
new KeyValue(y, event.getMark().getLongitude())
)
);
// drawBoat(gc, event.getMark().getLatitude(), event.getMark().getLongitude(), Colors.getColor());
System.out.println(event.getMark().getName());
}
timelineInfos.put(boat, new TimelineInfo(new Timeline(keyFrames.toArray(new KeyFrame[keyFrames.size()])), x, y));
}
AnimationTimer timer = new AnimationTimer() {
@Override
public void handle(long now) {
GraphicsContext gc = canvas.getGraphicsContext2D();
gc.clearRect(0,0,760,360);
gc.setFill(Color.FORESTGREEN);
for (Boat boat: timelineInfos.keySet()){
TimelineInfo timelineInfo = timelineInfos.get(boat);
// System.out.println(timelineInfo.getX().doubleValue());
// System.out.println(timelineInfo.getY().doubleValue());
drawBoat(gc, timelineInfo.getX().doubleValue(), timelineInfo.getY().doubleValue(), boat.getColor());
}
}
};
timer.start();
for (TimelineInfo timelineInfo: timelineInfos.values()){
Timeline timeline = timelineInfo.getTimeline();
timeline.play();
}
// drawBoat(gc, 0, 0, Color.GREEN);
// drawBoat(gc, 100, 100, Color.BLUE);
// drawBoat(gc, 32.296577, -64.854304, Color.RED);
// drawBoat(gc, 32.293771, -64.855242, Color.RED);
// drawBoat(gc, 32.317379, -64.839291, Color.GREEN);
// drawBoat(gc, 32.317257, -64.836260, Color.GREEN);
// drawBoat(gc, 32.313291, -64.887057, Color.YELLOW);
drawBoat(gc, 0, 0, Color.GREEN);
drawBoat(gc, 100, 100, Color.BLUE);
drawBoat(gc, 32.296577, -64.854304, Color.RED);
drawBoat(gc, 32.293771 , -64.855242, Color.RED);
}
private void drawBoat(GraphicsContext gc, double x, double y, Color color) {
x += 180; // to prevent negative longtitude
y += 90; // to prevent negative latitude
int diameter = 10;
x = abs(x - 32.313291) * 1000; // to prevent negative longtitude
y = abs(y + 64.887057) * 1000; // to prevent negative latitude
// y = abs(y);
int diameter = 2;
gc.setFill(color);
gc.fillOval(x, y, diameter, diameter);
}
@@ -0,0 +1,92 @@
package seng302.controllers;
import seng302.models.Boat;
import seng302.models.FileParser;
import seng302.models.Mark;
import seng302.models.Race;
import java.io.FileNotFoundException;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Map;
import java.util.Random;
/**
* Created by zyt10 on 17/03/17.
* run before CanvasController to initialize race events
* the CanvasController then uses the event data to make the animations
*/
public class RaceController {
Race race = null;
public void initializeRace() {
String raceConfigFile;
raceConfigFile = "doc/examples/config.json";
try {
race = createRace(raceConfigFile);
} catch (Exception e) {
System.out.println("There was an error creating the race.");
}
if (race != null) {
race.startRace();
} else {
System.out.println("There was an error creating the race. Exiting.");
}
}
public Race createRace(String configFile) throws Exception {
Race race = new Race();
FileParser fp;
// Read team names from file
try{
fp = new FileParser(configFile);
}
catch (FileNotFoundException e){
System.out.println("Config file does not exist");
return null;
}
ArrayList<String> boatNames = new ArrayList<>();
ArrayList<Map<String, Object>> teams = fp.getTeams();
//get race size
int numberOfBoats = (int) fp.getRaceSize();
//get time scale
double timeScale = fp.getTimeScale();
race.setTimeScale(timeScale);
for (Map<String, Object> team : teams) {
boatNames.add((String) team.get("team-name"));
}
// Shuffle team names
long seed = System.nanoTime();
Collections.shuffle(boatNames, new Random(seed));
if (numberOfBoats > Array.getLength(boatNames.toArray())) {
return null;
}
// Add boats to the race
for (int i = 0; i < numberOfBoats; i++) {
race.addBoat(new Boat(boatNames.get(i), (Double) (teams.get(i).get("velocity"))));
}
// Add marks to race in order
race.addMark(new Mark("Start", 32.296038,-64.854401 ));
race.addMark(new Mark("Mid Mark", 32.292881,-64.843231 ));
race.addMark(new Mark("Leeward Gate", 32.283808,-64.850012 ));
race.addMark(new Mark("Windward Gate", 32.309908,-64.833665 ));
race.addMark(new Mark("Finish", 32.318439,-64.837367 ));
return race;
}
public Race getRace() {
return race;
}
}