WIP: Yachts now power up upon collecting an item

Power up is speed boost x2 multiplier
Lasts 10 seconds
Do not stack

#story[1250]
This commit is contained in:
William Muir
2017-08-29 22:11:37 +12:00
parent 0d0b2e59d5
commit 4bd7291a4a
4 changed files with 42 additions and 13 deletions
+30 -10
View File
@@ -59,6 +59,8 @@ public class GameState implements Runnable {
private static final int TIME_TILL_START = 10 * 1000; private static final int TIME_TILL_START = 10 * 1000;
static Integer MAX_PLAYERS = 8; static Integer MAX_PLAYERS = 8;
private static final Long POWERUP_TIMEOUT_MS = 10_000L;
private static final Integer STATE_UPDATES_PER_SECOND = 60; private static final Integer STATE_UPDATES_PER_SECOND = 60;
private static Double ROUNDING_DISTANCE = 50d; // TODO: 14/08/17 wmu16 - Look into this value further private static Double ROUNDING_DISTANCE = 50d; // TODO: 14/08/17 wmu16 - Look into this value further
private static final Double MARK_COLLISION_DISTANCE = 15d; private static final Double MARK_COLLISION_DISTANCE = 15d;
@@ -302,6 +304,7 @@ public class GameState implements Runnable {
} }
for (ServerYacht yacht : yachts.values()) { for (ServerYacht yacht : yachts.values()) {
updateVelocity(yacht); updateVelocity(yacht);
checkPowerUpTimeout(yacht);
yacht.runAutoPilot(); yacht.runAutoPilot();
yacht.updateLocation(timeInterval); yacht.updateLocation(timeInterval);
if (yacht.getBoatStatus() != BoatStatus.FINISHED) { if (yacht.getBoatStatus() != BoatStatus.FINISHED) {
@@ -319,6 +322,17 @@ public class GameState implements Runnable {
} }
} }
private void checkPowerUpTimeout(ServerYacht yacht) {
if (yacht.getPowerUp() != null) {
if (System.currentTimeMillis() - yacht.getPowerUpStartTime() > POWERUP_TIMEOUT_MS) {
yacht.powerDown();
logger.debug("Yacht: " + yacht.getShortName() + " powered down!");
}
}
}
/** /**
* Check if the yacht has crossed the course limit * Check if the yacht has crossed the course limit
* *
@@ -349,7 +363,7 @@ public class GameState implements Runnable {
Double distance = GeoUtility.getDistance(token, serverYacht.getLocation()); Double distance = GeoUtility.getDistance(token, serverYacht.getLocation());
if (distance < YACHT_COLLISION_DISTANCE) { if (distance < YACHT_COLLISION_DISTANCE) {
tokens.remove(token); tokens.remove(token);
serverYacht.setPowerUp(token.getTokenType()); serverYacht.powerUp(token.getTokenType());
logger.debug("Yacht: " + serverYacht.getShortName() + " got powerup " + token logger.debug("Yacht: " + serverYacht.getShortName() + " got powerup " + token
.getTokenType()); .getTokenType());
notifyMessageListeners(MessageFactory.getRaceXML()); notifyMessageListeners(MessageFactory.getRaceXML());
@@ -410,25 +424,31 @@ public class GameState implements Runnable {
private void updateVelocity(ServerYacht yacht) { private void updateVelocity(ServerYacht yacht) {
Double velocity = yacht.getCurrentVelocity();
Double trueWindAngle = Math.abs(windDirection - yacht.getHeading()); Double trueWindAngle = Math.abs(windDirection - yacht.getHeading());
Double boatSpeedInKnots = PolarTable.getBoatSpeed(getWindSpeedKnots(), trueWindAngle); Double boatSpeedInKnots = PolarTable.getBoatSpeed(getWindSpeedKnots(), trueWindAngle);
Double maxBoatSpeed = GeoUtility.knotsToMMS(boatSpeedInKnots); Double maxBoatSpeed = GeoUtility.knotsToMMS(boatSpeedInKnots);
if (yacht.getPowerUp() != null) {
if (yacht.getPowerUp().equals(TokenType.BOOST)) {
maxBoatSpeed *= 2;
}
}
Double currentVelocity = yacht.getCurrentVelocity();
// TODO: 15/08/17 remove magic numbers from these equations. // TODO: 15/08/17 remove magic numbers from these equations.
if (yacht.getSailIn()) { if (yacht.getSailIn()) {
if (velocity < maxBoatSpeed - 500) { if (currentVelocity < maxBoatSpeed - 500) {
yacht.changeVelocity(maxBoatSpeed / 100); yacht.changeVelocity(maxBoatSpeed / 100);
} else if (velocity > maxBoatSpeed + 500) { } else if (currentVelocity > maxBoatSpeed + 500) {
yacht.changeVelocity(-velocity / 200); yacht.changeVelocity(-currentVelocity / 200);
} else { } else {
yacht.setCurrentVelocity(maxBoatSpeed); yacht.setCurrentVelocity(maxBoatSpeed);
} }
} else { } else {
if (velocity > 3000) { if (currentVelocity > 3000) {
yacht.changeVelocity(-velocity / 200); yacht.changeVelocity(-currentVelocity / 200);
} else if (velocity > 100) { } else if (currentVelocity > 100) {
yacht.changeVelocity(-velocity / 50); yacht.changeVelocity(-currentVelocity / 50);
} else if (velocity <= 100) { } else if (currentVelocity <= 100) {
yacht.setCurrentVelocity(0d); yacht.setCurrentVelocity(0d);
} }
} }
@@ -210,7 +210,7 @@ public class MainServerThread implements Runnable, ClientConnectionDelegate {
GameState.clearTokens(); GameState.clearTokens();
Random random = new Random(); Random random = new Random();
Collections.shuffle(allTokens); Collections.shuffle(allTokens);
for (int i = 0; i < random.nextInt(allTokens.size() - 1); i++) { for (int i = 0; i < random.nextInt(allTokens.size()); i++) {
GameState.addToken(allTokens.get(i)); GameState.addToken(allTokens.get(i));
} }
} }
+11 -1
View File
@@ -53,6 +53,7 @@ public class ServerYacht {
//PowerUp //PowerUp
private TokenType powerUp; private TokenType powerUp;
private Long powerUpStartTime;
public ServerYacht(String boatType, Integer sourceId, String hullID, String shortName, public ServerYacht(String boatType, Integer sourceId, String hullID, String shortName,
@@ -104,8 +105,17 @@ public class ServerYacht {
location = geoPoint; location = geoPoint;
} }
public void setPowerUp(TokenType powerUp) { public void powerUp(TokenType powerUp) {
this.powerUp = powerUp; this.powerUp = powerUp;
powerUpStartTime = System.currentTimeMillis();
}
public void powerDown() {
this.powerUp = null;
}
public Long getPowerUpStartTime() {
return powerUpStartTime;
} }
public TokenType getPowerUp() { public TokenType getPowerUp() {
@@ -18,5 +18,4 @@ public class Token extends GeoPoint {
public TokenType getTokenType() { public TokenType getTokenType() {
return tokenType; return tokenType;
} }
} }