Added sounds to coin pick up

Refactored check for token collision to move into the generic check for collision method
Created a YachtEventType enum to differentiate between collision and pickup on client side.
Client now plays a sound when they pick up a token

#story[1250]
This commit is contained in:
William Muir
2017-09-11 14:45:04 +12:00
parent 02e6d2a98b
commit 9fcb8915c2
5 changed files with 118 additions and 52 deletions
+49 -19
View File
@@ -24,6 +24,7 @@ import seng302.gameServer.messages.MarkType;
import seng302.gameServer.messages.Message;
import seng302.gameServer.messages.RoundingBoatStatus;
import seng302.gameServer.messages.YachtEventCodeMessage;
import seng302.gameServer.messages.YachtEventType;
import seng302.model.GeoPoint;
import seng302.model.Limit;
import seng302.model.Player;
@@ -49,7 +50,7 @@ public class GameState implements Runnable {
void notify(Message message);
}
private Logger logger = LoggerFactory.getLogger(GameState.class);
private static Logger logger = LoggerFactory.getLogger(GameState.class);
static final int WARNING_TIME = 10 * -1000;
@@ -300,7 +301,13 @@ public class GameState implements Runnable {
}
/**
* Called periodically in this GameState thread to update the GameState values
* Called periodically in this GameState thread to update the GameState values.
* -Updates yachts velocity
* -Updates locations
* -Checks for collisions
* -Checks for progression
*
* -Also checks things like the end of the race and race start time etc
*/
public void update() {
Boolean raceFinished = true;
@@ -317,7 +324,6 @@ public class GameState implements Runnable {
yacht.updateLocation(timeInterval);
if (yacht.getBoatStatus() != BoatStatus.FINISHED) {
checkCollision(yacht);
checkTokenPickUp(yacht);
checkForLegProgression(yacht);
raceFinished = false;
}
@@ -361,25 +367,34 @@ public class GameState implements Runnable {
/**
* Checks all tokensInPlay to see if a yacht has picked one up
*
* @param serverYacht The yacht to check for
* @return Token which was collided with
* @param serverYacht The yacht to check for collision with a token
*/
private void checkTokenPickUp(ServerYacht serverYacht) {
private static Token checkTokenPickUp(ServerYacht serverYacht) {
for (Token token : tokensInPlay) {
Double distance = GeoUtility.getDistance(token, serverYacht.getLocation());
if (distance < YACHT_COLLISION_DISTANCE) {
tokensInPlay.remove(token);
serverYacht.powerUp(token.getTokenType());
logger.debug("Yacht: " + serverYacht.getShortName() + " got powerup " + token
.getTokenType());
notifyMessageListeners(MessageFactory.getRaceXML());
break;
}
return token;
}
}
return null;
}
/**
* Checks for collision with other in game objects for the given serverYacht. To be called each
* update. If there is a collision, Notifies the server to send the appropriate messages out.
* Checks for these items in turn:
* - Other yachts
* - Marks
* - Boundary
* - Tokens
*
* @param serverYacht The server yacht to check collisions with
*/
public static void checkCollision(ServerYacht serverYacht) {
//Yacht Collision
ServerYacht collidedYacht = checkYachtCollision(serverYacht);
if (collidedYacht != null) {
GeoPoint originalLocation = serverYacht.getLocation();
@@ -396,9 +411,11 @@ public class GameState implements Runnable {
collidedYacht.getCurrentVelocity() * COLLISION_VELOCITY_PENALTY
);
notifyMessageListeners(
new YachtEventCodeMessage(serverYacht.getSourceId())
new YachtEventCodeMessage(serverYacht.getSourceId(), YachtEventType.COLLISION)
);
} else {
}
//Mark Collision
Mark collidedMark = checkMarkCollision(serverYacht);
if (collidedMark != null) {
serverYacht.setLocation(
@@ -408,10 +425,11 @@ public class GameState implements Runnable {
serverYacht.getCurrentVelocity() * COLLISION_VELOCITY_PENALTY
);
notifyMessageListeners(
new YachtEventCodeMessage(serverYacht.getSourceId())
new YachtEventCodeMessage(serverYacht.getSourceId(), YachtEventType.COLLISION)
);
}
else{
//Boundary Collision
if (checkBoundaryCollision(serverYacht)) {
serverYacht.setLocation(
calculateBounceBack(serverYacht, serverYacht.getLocation(),
@@ -421,11 +439,23 @@ public class GameState implements Runnable {
serverYacht.getCurrentVelocity() * COLLISION_VELOCITY_PENALTY
);
notifyMessageListeners(
new YachtEventCodeMessage(serverYacht.getSourceId())
new YachtEventCodeMessage(serverYacht.getSourceId(), YachtEventType.COLLISION)
);
}
//Token Collision
Token collidedToken = checkTokenPickUp(serverYacht);
if (collidedToken != null) {
tokensInPlay.remove(collidedToken);
serverYacht.powerUp(collidedToken.getTokenType());
logger.debug("Yacht: " + serverYacht.getShortName() + " got powerup " + collidedToken
.getTokenType());
notifyMessageListeners(MessageFactory.getRaceXML());
notifyMessageListeners(
new YachtEventCodeMessage(serverYacht.getSourceId(), YachtEventType.TOKEN));
}
}
}
@@ -18,13 +18,13 @@ public class YachtEventCodeMessage extends Message {
private int eventId;
public YachtEventCodeMessage(Integer subjectId) {
public YachtEventCodeMessage(Integer subjectId, YachtEventType yachtEventType) {
timeStamp = System.currentTimeMillis() / 1000L;
ack = 0;
raceId = 1;
destSourceId = subjectId; // collision boat source id
incidentId = 0;
eventId = 33;
eventId = yachtEventType.getCode();
setHeader(new Header(MESSAGE_TYPE, 0x01, (short) getSize()));
allocateBuffer();
@@ -0,0 +1,19 @@
package seng302.gameServer.messages;
/**
* Created by wmu16 on 11/09/17.
*/
public enum YachtEventType {
COLLISION(33),
TOKEN(34);
private int code;
YachtEventType(int code) {
this.code = code;
}
public int getCode() {
return code;
}
}
@@ -151,6 +151,16 @@ public class Sounds {
}
}
public static void playTokenPickupSound() {
if (!soundEffectsMuted) {
Media pickupSound = new Media(
Sounds.class.getClassLoader().getResource("sounds/Coin-pick-up-sound-effect.mp3")
.toString());
soundPlayer = new MediaPlayer(pickupSound);
soundPlayer.play();
}
}
public static void playHoverSound() {
if (!soundEffectsMuted) {
Media hoverSound = new Media(Sounds.class.getClassLoader().getResource("sounds/sound-over.wav").toString());
@@ -23,6 +23,7 @@ import seng302.gameServer.GameState;
import seng302.gameServer.MainServerThread;
import seng302.gameServer.messages.BoatAction;
import seng302.gameServer.messages.BoatStatus;
import seng302.gameServer.messages.YachtEventType;
import seng302.model.ClientYacht;
import seng302.model.RaceState;
import seng302.model.stream.packets.StreamPacket;
@@ -296,7 +297,12 @@ public class GameClient {
break;
case YACHT_EVENT_CODE:
YachtEventData yachtEventData = StreamParser.extractYachtEventCode(packet);
if (yachtEventData.getEventId() == YachtEventType.COLLISION.getCode()) {
showCollisionAlert(StreamParser.extractYachtEventCode(packet));
} else if (yachtEventData.getEventId() == YachtEventType.TOKEN.getCode()) {
showPickUp();
}
break;
case CHATTER_TEXT:
@@ -446,8 +452,6 @@ public class GameClient {
* Tells race view to show a collision animation.
*/
private void showCollisionAlert(YachtEventData yachtEventData) {
// 33 is the agreed code to show collision
if (yachtEventData.getEventId() == 33) {
Sounds.playCrashSound();
raceState.storeCollision(
allBoatsMap.get(
@@ -455,6 +459,9 @@ public class GameClient {
)
);
}
private void showPickUp() {
Sounds.playTokenPickupSound();
}
private void formatAndSendChatMessage(String rawChat) {