BoatState message can be received from mock by visualiser

- Basic penalties have been made for out of bounds and collisions
- RaceServer packages and sends BoatState messages for each boat
- ServerConnection routes BoatState messages instead of dropping by default
- VisualiserRaceCommandFactory produces BoatStateCommands

#story[1291]
main
Connor Taylor-Brown 8 years ago
parent a295728e89
commit 50d5dd02a2

@ -27,7 +27,7 @@ public class MockBoat extends Boat {
* 1: passed only first check
* 2: passed first and second check
*/
private Integer roundingStatus = 0;
private int roundingStatus = 0;
/**
* Stores whether the boat is on autoVMG or not
@ -284,8 +284,8 @@ public class MockBoat extends Boat {
(this.isStarboardSide(mark1) && this.isPortSide(mark2));
}
public Integer getRoundingStatus() {
return Integer.valueOf(roundingStatus);
public int getRoundingStatus() {
return roundingStatus;
}
public void increaseRoundingStatus() {

@ -377,6 +377,11 @@ public class MockRace extends RaceState {
boat.setTimeSinceTackChange(boat.getTimeSinceTackChange() + updatePeriodMilliseconds);
}
// Remove one unit of health for every frame spent outside boundary
if(!finish && checkBearingInsideCourse(boat.getBearing(), boat.getPosition())) {
boat.updateHealth(-1);
}
this.updateEstimatedTime(boat);
}

@ -173,12 +173,9 @@ public class RaceLogic implements RunnableWithFramePeriod, Observer {
race.updatePosition(boat, framePeriod, race.getRaceClock().getDurationMilli());
if(race.getColliderRegistry().rayCast(boat)){
//System.out.println("Collision!");
//Add boat to list
collisionBoats.add(boat);
}
//System.out.println(race.getColliderRegistry().rayCast(boat));
}

@ -71,6 +71,9 @@ public class RaceServer {
//Parse the boat locations.
snapshotMessages.addAll(parseBoatLocations());
//Parse the boat states
snapshotMessages.addAll(parseBoatStates());
//Parse the marks.
snapshotMessages.addAll(parseMarks());
@ -250,7 +253,29 @@ public class RaceServer {
}
/**
* Generates BoatState messages for every boat in the race
* @return list of BoatState messages
*/
private List<BoatState> parseBoatStates() {
List<BoatState> boatStates = new ArrayList<>();
for(MockBoat boat: race.getBoats()) {
boatStates.add(parseIndividualBoatState(boat));
}
return boatStates;
}
/**
* Creates a BoatState message for the current state of the given boat
* @param boat to generate message for
* @return BoatState message
*/
private BoatState parseIndividualBoatState(MockBoat boat) {
return new BoatState(
boat.getSourceID(),
boat.getHealth()
);
}
/**
* Parses the race status, and returns it.

@ -103,6 +103,11 @@ public class Boat extends Collider {
*/
private boolean isColliding = false;
/**
* Amount of health boat currently has, between 0 and 100
*/
private int health;
/**
* Constructs a boat object with a given sourceID, name, country/team abbreviation, and polars table.
*
@ -113,6 +118,7 @@ public class Boat extends Collider {
public Boat(int sourceID, String name, String country) {
this.sourceID = sourceID;
this.health = 100;
this.setName(name);
this.setCountry(country);
@ -419,6 +425,8 @@ public class Boat extends Collider {
@Override
public void onCollisionEnter(Collision e) {
if(e.getBearing().degrees() > 270 || e.getBearing().degrees() < 90) {
// Deplete health
e.getBoat().updateHealth(-5);
// Notify observers of collision
this.setChanged();
notifyObservers(e);
@ -432,4 +440,22 @@ public class Boat extends Collider {
public void setColliding(boolean colliding) {
isColliding = colliding;
}
public int getHealth() {
return health;
}
public void setHealth(int health) {
this.health = health;
}
/**
* Add a given amount of HP to boat health
* @param delta amount of HP to add
*/
public void updateHealth(int delta) {
health += delta;
if(health < 0) health = 0;
else if(health > 100) health = 100;
}
}

@ -97,6 +97,7 @@ public class Mark extends Collider{
@Override
public void onCollisionEnter(Collision e) {
e.getBoat().updateHealth(-10);
this.setChanged();
notifyObservers(e);
}

@ -0,0 +1,23 @@
package visualiser.Commands.VisualiserRaceCommands;
import mock.model.commandFactory.Command;
import network.Messages.BoatState;
import visualiser.model.VisualiserRaceState;
/**
* Created by connortaylorbrown on 20/09/17.
*/
public class BoatStateCommand implements Command {
private BoatState boatState;
private VisualiserRaceState visualiserRace;
public BoatStateCommand(BoatState boatState, VisualiserRaceState visualiserRace) {
this.boatState = boatState;
this.visualiserRace = visualiserRace;
}
@Override
public void execute() {
System.out.println(boatState.getBoatHealth());
}
}

@ -24,7 +24,6 @@ public class VisualiserRaceCommandFactory {
switch (message.getType()) {
case BOATLOCATION:
//System.out.println("Boat location received");
return new BoatLocationCommand((BoatLocation) message, visualiserRace);
case RACESTATUS: return new RaceStatusCommand((RaceStatus) message, visualiserRace);
@ -36,7 +35,8 @@ public class VisualiserRaceCommandFactory {
case YACHTEVENTCODE:
return new BoatCollisionCommand((YachtEvent) message, visualiserRace);
case BOATSTATE:
return new BoatStateCommand((BoatState) message, visualiserRace);
default: throw new CommandConstructionException("Could not create VisualiserRaceCommand. Unrecognised or unsupported MessageType: " + message.getType());

@ -297,6 +297,7 @@ public class ServerConnection implements RunnableWithFramePeriod {
this.messageRouter.addRoute(MessageType.MARKROUNDING, incomingMessages);
this.messageRouter.addRoute(MessageType.XMLMESSAGE, incomingMessages);
this.messageRouter.addRoute(MessageType.ASSIGN_PLAYER_BOAT, incomingMessages);
this.messageRouter.addRoute(MessageType.BOATSTATE, incomingMessages);
this.messageRouter.removeDefaultRoute(); //We no longer want to keep un-routed messages.

Loading…
Cancel
Save