mirror of
https://github.com/michaelrausch/Party-Parrots-At-Sea.git
synced 2026-05-09 14:28:43 +00:00
Stripped back codebase to make to create basic model for streaming data
Removed many classes involved with visualisation such as controllers and multiple fxmls. Now there is just one for debugging Merged in Boat updating pattern from team 27 #story[828]
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
package seng302.models;
|
||||
|
||||
import javafx.animation.AnimationTimer;
|
||||
import seng302.controllers.Controller;
|
||||
import seng302.models.mark.Mark;
|
||||
|
||||
import java.util.*;
|
||||
@@ -8,47 +10,137 @@ import java.util.*;
|
||||
* Race class containing the boats and legs in the race
|
||||
* Created by mra106 on 8/3/2017.
|
||||
*/
|
||||
public class Race {
|
||||
public class Race extends Thread {
|
||||
|
||||
private static final double UPDATE_TIME = 0.016666; // 1 / 60 ie 60fps
|
||||
|
||||
|
||||
private ArrayList<Boat> boats; // The boats in the race
|
||||
private ArrayList<Boat> finishingOrder; // The order in which the boats finish the race
|
||||
private HashMap<Boat, List> events = new HashMap<>(); // The events that occur in the race
|
||||
private List<Mark> course; // Marks in the race
|
||||
private long startTime = 0;
|
||||
private double timeScale = 1;
|
||||
private List<Mark> markers; // Marks in the race
|
||||
private List<Leg> raceLegs;
|
||||
private boolean raceFinished = false; // Race is finished
|
||||
private int raceTime = -2; // Current time in the race
|
||||
private Double timeScale = null;
|
||||
private boolean raceStarted = false;
|
||||
private Controller controller;
|
||||
|
||||
/**
|
||||
* Race class containing the boats and legs in the race
|
||||
*/
|
||||
public Race() {
|
||||
this.boats = new ArrayList<>();
|
||||
public Race(List<Mark> markers, ArrayList<Boat> boats, Double timescale, Controller controller) {
|
||||
this.boats = boats;
|
||||
this.markers = markers;
|
||||
this.raceLegs = makeRaceLegs();
|
||||
this.controller = controller;
|
||||
|
||||
this.timeScale = timescale;
|
||||
this.finishingOrder = new ArrayList<>();
|
||||
this.course = new ArrayList<>();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Makes the race legs out of all the marker points for the course
|
||||
* @return ArrayList of raceLegs
|
||||
*/
|
||||
private ArrayList<Leg> makeRaceLegs() {
|
||||
ArrayList<Leg> raceLegs = new ArrayList<>();
|
||||
for (int i=0; i < markers.size()-1; i++) {
|
||||
raceLegs.add(new Leg(markers.get(i), markers.get(i+1)));
|
||||
}
|
||||
return raceLegs;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* A timer that is called every frame to call the action of moving the boats
|
||||
*/
|
||||
private AnimationTimer at = new AnimationTimer() {
|
||||
@Override
|
||||
public void handle(long now) {
|
||||
|
||||
//Updating Boat positions
|
||||
if(finishingOrder.size() != boats.size()) {
|
||||
// update the time
|
||||
boats.stream().filter(boat -> !finishingOrder.contains(boat)).forEach(boat -> {
|
||||
updateBoatPosition(boat);
|
||||
});
|
||||
} else {
|
||||
at.stop();
|
||||
}
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Begins the race simulation
|
||||
*/
|
||||
@Override
|
||||
public void run() {
|
||||
if (!raceStarted) {
|
||||
// controller.getPlayPauseButton().setDisable(true);
|
||||
at.start();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Moves the coordinates of the boats.
|
||||
* @param boat The boat to update the position of
|
||||
*/
|
||||
private void updateBoatPosition(Boat boat) {
|
||||
Double thisHeading = boat.getHeading();
|
||||
// TODO: 4/8/17 wmu16 - Add a distance scale factor from lat long distance in Metres to xy equivalent
|
||||
Double hypMove = boat.getVelocity() * UPDATE_TIME * timeScale; //* distanceScaleFactor
|
||||
boat.setLegDistance(boat.getLegDistance() + hypMove);
|
||||
moveBoat(boat, thisHeading, hypMove);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a boat to the race
|
||||
*
|
||||
* @param boat, the boat to add
|
||||
* Moves a boat along coordinates by breaking down the distance moved along the hypotenuse into x and y components
|
||||
* @param boat The Boat to move
|
||||
* @param heading The heading the boat is moving at
|
||||
* @param hypMove The distance moved along the hypotenuse (absolute distance)
|
||||
*/
|
||||
public void addBoat(Boat boat) {
|
||||
boats.add(boat);
|
||||
private void moveBoat(Boat boat, Double heading, Double hypMove) {
|
||||
//If the boat has not yet passed the marker at the end of the leg increase its (x,y) as per normal
|
||||
// TODO: 4/8/17 wmu16 - Initialising boat positions and legs and headings etc.
|
||||
if(boat.getLegDistance() <= boat.getCurrentLeg().getDistance()) {
|
||||
Double xMove = hypMove * Math.sin(Math.toRadians(heading));
|
||||
Double yMove = - hypMove * Math.cos(Math.toRadians(heading));
|
||||
boat.moveBoatBy(xMove, yMove);
|
||||
|
||||
//If the boat has finished...
|
||||
} else if (getNextRaceLeg(boat.getCurrentLeg()).equals(boat.getCurrentLeg())) {
|
||||
finishingOrder.add(boat);
|
||||
//Otherwise apply the overflow distance of the leg to the next leg
|
||||
} else {
|
||||
Double overflowDistance = boat.getLegDistance() - boat.getCurrentLeg().getDistance();
|
||||
boat.setCurrentLeg(getNextRaceLeg(boat.getCurrentLeg()));
|
||||
boat.setHeading(boat.getCurrentLeg().getHeading());
|
||||
boat.setLegDistance(overflowDistance);
|
||||
moveBoat(boat, boat.getHeading(), overflowDistance);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list of boats in a random order
|
||||
*
|
||||
* @return a list of boats
|
||||
* Returns the next leg in the race
|
||||
* @param currentRaceLeg The leg that you are currently on
|
||||
* @return The next race leg or the same race leg if it has reached the end
|
||||
*/
|
||||
public Boat[] getShuffledBoats() {
|
||||
// Shuffle the list of boats
|
||||
long seed = System.nanoTime();
|
||||
Collections.shuffle(this.boats, new Random(seed));
|
||||
|
||||
return boats.toArray(new Boat[boats.size()]);
|
||||
private Leg getNextRaceLeg(Leg currentRaceLeg) {
|
||||
Leg nextRaceLeg = currentRaceLeg;
|
||||
for(int i = 0; i< raceLegs.size() - 1; i++) {
|
||||
if (raceLegs.get(i).equals(currentRaceLeg)) {
|
||||
nextRaceLeg = raceLegs.get(i + 1);
|
||||
}
|
||||
}
|
||||
return nextRaceLeg;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns a list of boats in the order that they
|
||||
* finished the race (position 0 is first place)
|
||||
@@ -69,85 +161,28 @@ public class Race {
|
||||
return boats.toArray(new Boat[boats.size()]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets time scale
|
||||
*
|
||||
* @param timeScale
|
||||
*/
|
||||
public void setTimeScale(double timeScale) {
|
||||
this.timeScale = timeScale;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate all events that will happen during the race.
|
||||
*/
|
||||
private void generateEvents() {
|
||||
|
||||
for (Boat boat : this.boats) {
|
||||
double totalDistance = 0;
|
||||
int numberOfMarks = this.course.size();
|
||||
|
||||
for (int i = 0; i < numberOfMarks; i++) {
|
||||
Double time = (totalDistance / boat.getVelocity() / timeScale);
|
||||
|
||||
// If there are singleMarks after this event
|
||||
if (i < numberOfMarks - 1) {
|
||||
Event event = new Event(time, boat, course.get(i), course.get(i + 1), i);
|
||||
|
||||
try {
|
||||
events.get(boat).add(event);
|
||||
|
||||
} catch (NullPointerException e) {
|
||||
events.put(boat, new ArrayList<>(Arrays.asList(event)));
|
||||
}
|
||||
totalDistance += event.getDistanceBetweenMarks();
|
||||
System.out.println(totalDistance);
|
||||
System.out.println(boat.getVelocity());
|
||||
}
|
||||
|
||||
// There are no more marks after this event
|
||||
|
||||
else{
|
||||
Event event = new Event(time, boat, course.get(i), i);
|
||||
events.get(boat).add(event);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Starts a race and generates all events for the race.
|
||||
*/
|
||||
public void startRace() {
|
||||
// record start time.
|
||||
this.startTime = System.currentTimeMillis();
|
||||
generateEvents();
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
public void pause() {
|
||||
at.stop();
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the race course
|
||||
* @param course a list of marks in the course
|
||||
*/
|
||||
public void addCourse(List<Mark> course) {
|
||||
this.course = course;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a list of marks in the course
|
||||
* Get a list of marks in the markers
|
||||
* @return
|
||||
*/
|
||||
public List<Mark> getCourse() {
|
||||
return course;
|
||||
public List<Mark> getMarkers() {
|
||||
return markers;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a map of the events in the race
|
||||
* @return
|
||||
*/
|
||||
public HashMap<Boat, List> getEvents() {
|
||||
return events;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a boat as finished
|
||||
@@ -191,7 +226,11 @@ public class Race {
|
||||
/**
|
||||
* Increment the race time by one second
|
||||
*/
|
||||
public void incrementRaceTime(){
|
||||
public void incrementRaceTime() {
|
||||
this.raceTime += this.timeScale;
|
||||
}
|
||||
|
||||
public List<Leg> getRaceLegs() {
|
||||
return raceLegs;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user