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

@ -377,6 +377,11 @@ public class MockRace extends RaceState {
boat.setTimeSinceTackChange(boat.getTimeSinceTackChange() + updatePeriodMilliseconds); 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); this.updateEstimatedTime(boat);
} }

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

@ -71,6 +71,9 @@ public class RaceServer {
//Parse the boat locations. //Parse the boat locations.
snapshotMessages.addAll(parseBoatLocations()); snapshotMessages.addAll(parseBoatLocations());
//Parse the boat states
snapshotMessages.addAll(parseBoatStates());
//Parse the marks. //Parse the marks.
snapshotMessages.addAll(parseMarks()); 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. * Parses the race status, and returns it.

@ -103,6 +103,11 @@ public class Boat extends Collider {
*/ */
private boolean isColliding = false; 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. * 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) { public Boat(int sourceID, String name, String country) {
this.sourceID = sourceID; this.sourceID = sourceID;
this.health = 100;
this.setName(name); this.setName(name);
this.setCountry(country); this.setCountry(country);
@ -419,6 +425,8 @@ public class Boat extends Collider {
@Override @Override
public void onCollisionEnter(Collision e) { public void onCollisionEnter(Collision e) {
if(e.getBearing().degrees() > 270 || e.getBearing().degrees() < 90) { if(e.getBearing().degrees() > 270 || e.getBearing().degrees() < 90) {
// Deplete health
e.getBoat().updateHealth(-5);
// Notify observers of collision // Notify observers of collision
this.setChanged(); this.setChanged();
notifyObservers(e); notifyObservers(e);
@ -432,4 +440,22 @@ public class Boat extends Collider {
public void setColliding(boolean colliding) { public void setColliding(boolean colliding) {
isColliding = 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 @Override
public void onCollisionEnter(Collision e) { public void onCollisionEnter(Collision e) {
e.getBoat().updateHealth(-10);
this.setChanged(); this.setChanged();
notifyObservers(e); 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()) { switch (message.getType()) {
case BOATLOCATION: case BOATLOCATION:
//System.out.println("Boat location received");
return new BoatLocationCommand((BoatLocation) message, visualiserRace); return new BoatLocationCommand((BoatLocation) message, visualiserRace);
case RACESTATUS: return new RaceStatusCommand((RaceStatus) message, visualiserRace); case RACESTATUS: return new RaceStatusCommand((RaceStatus) message, visualiserRace);
@ -36,7 +35,8 @@ public class VisualiserRaceCommandFactory {
case YACHTEVENTCODE: case YACHTEVENTCODE:
return new BoatCollisionCommand((YachtEvent) message, visualiserRace); 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()); 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.MARKROUNDING, incomingMessages);
this.messageRouter.addRoute(MessageType.XMLMESSAGE, incomingMessages); this.messageRouter.addRoute(MessageType.XMLMESSAGE, incomingMessages);
this.messageRouter.addRoute(MessageType.ASSIGN_PLAYER_BOAT, 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. this.messageRouter.removeDefaultRoute(); //We no longer want to keep un-routed messages.

Loading…
Cancel
Save