diff --git a/src/main/java/ch/unibas/dmi/dbis/cs108/gamelogic/GhostifyHandler.java b/src/main/java/ch/unibas/dmi/dbis/cs108/gamelogic/GhostifyHandler.java index b2fb8c0..902e08c 100644 --- a/src/main/java/ch/unibas/dmi/dbis/cs108/gamelogic/GhostifyHandler.java +++ b/src/main/java/ch/unibas/dmi/dbis/cs108/gamelogic/GhostifyHandler.java @@ -38,7 +38,7 @@ public class GhostifyHandler { } game.gameState.addNewPassenger(g, g.getPosition()); - LOGGER.info("Passenger at position " + p.getPosition() + "has been ghostified"); + LOGGER.info("Passenger at position " + p.getPosition() + " has been ghostified"); return g; } } diff --git a/src/main/java/ch/unibas/dmi/dbis/cs108/gamelogic/NoiseHandler.java b/src/main/java/ch/unibas/dmi/dbis/cs108/gamelogic/NoiseHandler.java index 0b3fc50..d5c7bc0 100644 --- a/src/main/java/ch/unibas/dmi/dbis/cs108/gamelogic/NoiseHandler.java +++ b/src/main/java/ch/unibas/dmi/dbis/cs108/gamelogic/NoiseHandler.java @@ -1,10 +1,37 @@ package ch.unibas.dmi.dbis.cs108.gamelogic; +import ch.unibas.dmi.dbis.cs108.gamelogic.klassenstruktur.Ghost; +import ch.unibas.dmi.dbis.cs108.gamelogic.klassenstruktur.Passenger; + /** * Determines who heard something (via Passenger Array currently in GameFunctions 'passengerTrain') * and broadcasts noise message to them (via ServerGameInfoHandler) */ - public class NoiseHandler { - + /** + * Notifies passengers in the train about a ghost walking by them. Differentiates between two + * cases: if the active ghost (predator) is to the right of his victim, the Passenger array is + * being walked through from right to left (from the predator's position back to the victim's + * position), otherwise the other way around. One call of noiseNotifier only deals with one + * predator infecting a victim, so if there are already multiple ghosts in the game, the method + * should be called for each of them individually. + * + * @param passengers passengers of the train the game is played in + * @param predator ghost that has infected a human player during this night (called upon as + * passenger for convenience reasons) + * @param victim human player who has been turned into a ghost this night + * @param game current game instance + */ + public void noiseNotifier(Passenger[] passengers, Passenger predator, Ghost victim, Game game) { + if (predator.getPosition() - victim.getPosition() + > 0) { // if predator is to the right of victim + for (int i = predator.getPosition() - 1; i > victim.getPosition(); i--) { + passengers[i].send(ClientGameInfoHandler.noiseNotification, game); + } + } else { // if predator is to the left of victim + for (int i = predator.getPosition() + 1; i < victim.getPosition(); i++) { + passengers[i].send(ClientGameInfoHandler.noiseNotification, game); + } + } + } } diff --git a/src/main/java/ch/unibas/dmi/dbis/cs108/gamelogic/VoteHandler.java b/src/main/java/ch/unibas/dmi/dbis/cs108/gamelogic/VoteHandler.java index 665b2f4..64ca673 100644 --- a/src/main/java/ch/unibas/dmi/dbis/cs108/gamelogic/VoteHandler.java +++ b/src/main/java/ch/unibas/dmi/dbis/cs108/gamelogic/VoteHandler.java @@ -43,6 +43,7 @@ public class VoteHandler { * * @param passengers: passengers on the train */ + public void ghostVote(Passenger[] passengers, Game game) { LOGGER.debug("ghostVote has been called"); LOGGER.info(game.getGameState().toString()); @@ -113,6 +114,16 @@ public class VoteHandler { passengers[ghostPosition].send( ClientGameInfoHandler.youGotGhostyfied, game); // TODO: ServerGameInfoHandler might deal with this one + /* notify passengers the ghosts passed by - for each ghost that ghostified a player, an instance of NoiseHandler + is being created and the passengers this ghost passed by are being notified. The player who's just been ghostified + is ignored since he didn't participate in this night's ghostification. */ + for (int i = 0; i < passengers.length; i++) { + if (passengers[i].getIsGhost() && i != ghostPosition) { + NoiseHandler n = new NoiseHandler(); + n.noiseNotifier(passengers, passengers[i], g, game); + } + } + LOGGER.info(game.getGameState().toString()); // set hasVoted to false for all passengers for future votings for (Passenger passenger : passengers) {