Spun off Locatable interface to make collision code more reusable

#story[1100]
This commit is contained in:
cbt24
2017-08-16 16:05:25 +12:00
parent ce14e93404
commit f44929f376
17 changed files with 98 additions and 664 deletions
@@ -10,7 +10,7 @@ import java.time.ZonedDateTime;
/**
* Boat Model that is used to store information on the boats that are running in the race.
*/
public class Boat {
public class Boat implements Locatable {
/**
* The name of the boat/team.
*/
@@ -30,7 +30,7 @@ public class Boat {
/**
* The current position of the boat.
*/
private GPSCoordinate currentPosition;
private GPSCoordinate position;
/**
* The country or team abbreviation of the boat.
@@ -57,7 +57,7 @@ public class Boat {
/**
* The boat's position within the race (e.g., 5th).
*/
private StringProperty positionInRace = new SimpleStringProperty();
private StringProperty placing = new SimpleStringProperty();
/**
@@ -107,7 +107,7 @@ public class Boat {
this.bearing = Bearing.fromDegrees(0d);
setCurrentPosition(new GPSCoordinate(0, 0));
setPosition(new GPSCoordinate(0, 0));
this.status = BoatStatusEnum.UNDEFINED;
}
@@ -256,41 +256,32 @@ public class Boat {
* Returns the position within the race the boat has (e.g., 5th).
* @return The boat's position in race.
*/
public StringProperty positionProperty() {
return positionInRace;
public StringProperty placingProperty() {
return placing;
}
/**
* Sets the position within the race the boat has (e.g., 5th).
* @param position The boat's position in race.
*/
public void setPosition(String position) {
this.positionInRace.set(position);
public void setPlacing(String position) {
this.placing.set(position);
}
/**
* Returns the position within the race the boat has (e.g., 5th).
* @return The boat's position in race.
*/
public String getPosition() {
return this.positionInRace.get();
}
/**
* Returns the current position of the boat.
* @return The current position of the boat.
*/
public GPSCoordinate getCurrentPosition() {
return currentPosition;
public GPSCoordinate getPosition() {
return position;
}
/**
* Sets the current position of the boat.
* @param currentPosition The new position for the boat.
* @param position The new position for the boat.
*/
public void setCurrentPosition(GPSCoordinate currentPosition) {
this.currentPosition = currentPosition;
public void setPosition(GPSCoordinate position) {
this.position = position;
}
@@ -1,16 +1,11 @@
package shared.model;
import mock.model.MockBoat;
import mock.model.collider.Collider;
import mock.model.collider.Collision;
import shared.enums.RoundingType;
/**
* Represents a compound mark - that is, either one or two individual marks which form a single compound mark.
*/
public class CompoundMark implements Collider {
public class CompoundMark {
/**
* The ID of the compound mark.
@@ -146,38 +141,6 @@ public class CompoundMark implements Collider {
}
private Mark closestMark(GPSCoordinate coordinate) {
double mark1distance = GPSCoordinate.calculateDistanceMeters(getMark1Position(), coordinate);
double mark2distance = GPSCoordinate.calculateDistanceMeters(getMark2Position(), coordinate);
return mark1distance <= mark2distance? mark1 : mark2;
}
@Override
public boolean rayCast(MockBoat boat, double distance) {
GPSCoordinate position = boat.getCurrentPosition();
double actualDistance = GPSCoordinate.calculateDistanceMeters(position, closestMark(position).getPosition());
// Compass direction of collider
Bearing absolute = Bearing.fromAzimuth(GPSCoordinate.calculateAzimuth(position, closestMark(position).getPosition()));
// Direction of collider from heading
Bearing relative = Bearing.fromDegrees(absolute.degrees() - boat.getBearing().degrees());
if(actualDistance <= distance) {
onCollisionEnter(new Collision(relative, distance));
return true;
} else return false;
}
@Override
public boolean rayCast(MockBoat boat) {
return rayCast(boat, 100);
}
@Override
public void onCollisionEnter(Collision e) {
if(e.getBearing().degrees() < 90) System.out.println("Starboard");
else if(e.getBearing().degrees() > 270) System.out.println("Port");
}
/**
* Used to find how far apart the marks that make up this gate are
* If this compound mark is only one point return base length of 250m
@@ -0,0 +1,9 @@
package shared.model;
/**
* Created by cbt24 on 16/08/17.
*/
public interface Locatable {
GPSCoordinate getPosition();
void setPosition(GPSCoordinate position);
}
@@ -1,10 +1,14 @@
package shared.model;
import mock.model.MockBoat;
import mock.model.collider.Collider;
import mock.model.collider.Collision;
/**
* Represents an individual mark.
* Has a source ID, name, and position.
*/
public class Mark {
public class Mark implements Locatable, Collider {
/**
* The source ID of the mark.
@@ -75,4 +79,31 @@ public class Mark {
public void setPosition(GPSCoordinate position) {
this.position = position;
}
@Override
public boolean rayCast(MockBoat boat, double distance) {
double actualDistance = GPSCoordinate.calculateDistanceMeters(boat.getPosition(), this.position);
// Compass direction of collider
Bearing absolute = Bearing.fromAzimuth(GPSCoordinate.calculateAzimuth(boat.getPosition(), this.position));
// Direction of collider from heading
Bearing relative = Bearing.fromDegrees(absolute.degrees() - boat.getBearing().degrees());
if(actualDistance <= distance) {
onCollisionEnter(new Collision(relative, distance));
return true;
} else return false;
}
@Override
public boolean rayCast(MockBoat boat) {
return rayCast(boat, 100);
}
@Override
public void onCollisionEnter(Collision e) {
if(e.getBearing().degrees() == 0) System.out.println("Ahead");
else if(e.getBearing().degrees() < 90) System.out.println("Starboard");
else if(e.getBearing().degrees() > 270) System.out.println("Port");
else System.out.println("Behind");
}
}
@@ -118,7 +118,11 @@ public abstract class Race {
this.setWind(Bearing.fromDegrees(0), 0);
// Set up colliders
this.colliderRegistry = new ColliderRegistry();
this.colliderRegistry.addAllColliders(compoundMarks);
for(CompoundMark mark: compoundMarks) {
colliderRegistry.addCollider(mark.getMark1());
if(mark.getMark2() != null) colliderRegistry.addCollider(mark.getMark2());
}
}
public ColliderRegistry getColliderRegistry() {