diff --git a/src/main/java/ch/unibas/dmi/dbis/cs108/gamelogic/Timer.java b/src/main/java/ch/unibas/dmi/dbis/cs108/gamelogic/Timer.java index bef9f07..b121302 100644 --- a/src/main/java/ch/unibas/dmi/dbis/cs108/gamelogic/Timer.java +++ b/src/main/java/ch/unibas/dmi/dbis/cs108/gamelogic/Timer.java @@ -16,13 +16,13 @@ public class Timer { /** * The maximum length of the ghost vote in the night, in seconds */ - public static final int ghostVoteTime = 30; + public static final int ghostVoteTime = 40; /** * The length of time in seconds after the ghost vote during which the ghosts visually walk to / * from their victim and the timespan within which humans will hear a noise. After this, the day starts. */ - public static final int ghostAfterVoteTime = 6; + public static final int ghostAfterVoteTime = 8; /** * The maximum length of the human vote in the day, in seconds */ @@ -32,7 +32,7 @@ public class Timer { * The length of time in seconds after the human vote, as the 'winner' of the vote is announced, * before the night begins */ - public static final int humanAfterVoteTime = 5; + public static final int humanAfterVoteTime = 8; /** * The checking interval in seconds diff --git a/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/client/gui/DayNightChangeListener.java b/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/client/gui/DayNightChangeListener.java index d5b0622..21e9776 100644 --- a/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/client/gui/DayNightChangeListener.java +++ b/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/client/gui/DayNightChangeListener.java @@ -5,7 +5,7 @@ import ch.unibas.dmi.dbis.cs108.multiplayer.client.gui.game.GameController; import ch.unibas.dmi.dbis.cs108.multiplayer.client.gui.lounge.LoungeSceneViewController; /** - * This class adds methods to listen if there is a change in the day&night state and calls methods accordingly + * This class adds methods to listen if there is a change in the day and night state and calls methods accordingly */ public class DayNightChangeListener implements Runnable { @@ -63,7 +63,7 @@ public class DayNightChangeListener implements Runnable { chatApp.getGameController().updateRoomLabels(); gameStateModel.setRoleFromPosition(position); try { - Thread.sleep(100); + Thread.sleep(200); } catch (InterruptedException e) { e.printStackTrace(); } diff --git a/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/client/gui/game/GameController.java b/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/client/gui/game/GameController.java index 4dcfc21..797c718 100644 --- a/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/client/gui/game/GameController.java +++ b/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/client/gui/game/GameController.java @@ -2,6 +2,7 @@ package ch.unibas.dmi.dbis.cs108.multiplayer.client.gui.game; import static javafx.scene.AccessibleRole.PARENT; +import ch.unibas.dmi.dbis.cs108.gamelogic.Timer; import ch.unibas.dmi.dbis.cs108.multiplayer.client.Sound; import ch.unibas.dmi.dbis.cs108.multiplayer.client.gui.ChatApp; import ch.unibas.dmi.dbis.cs108.multiplayer.client.gui.GameStateModel; @@ -534,17 +535,7 @@ public class GameController implements Initializable { try { if(!gameStateModel.getKickedOff()[0]) { Animation bell = new BellAnimation(noiseImage5, bells); - //wait until it's day: - while (!getGameStateModel().getDayClone()) { - Thread.sleep(100); - } - Thread.sleep(500); - //just so the alarm isn't rung exactly when the day starts, add random delay - Random random = new Random(); - Thread.sleep(random.nextInt(1000)); - - bell.play(); - ringBellSound(); + waitForDayThenRingBell(bell); } } catch (Exception e) { e.printStackTrace(); @@ -565,16 +556,7 @@ public class GameController implements Initializable { try { if(!gameStateModel.getKickedOff()[1]) { Animation bell = new BellAnimation(noiseImage4, bells); - //wait until it's day: - while (!getGameStateModel().getDayClone()) { - Thread.sleep(100); - } - Thread.sleep(500); - //just so the alarm isn't rung exactly when the day starts, add random delay - Random random = new Random(); - Thread.sleep(random.nextInt(1000)); - bell.play(); - ringBellSound(); + waitForDayThenRingBell(bell); } } catch (Exception e) { e.printStackTrace(); @@ -594,17 +576,7 @@ public class GameController implements Initializable { try { if(!gameStateModel.getKickedOff()[2]) { Animation bell = new BellAnimation(noiseImage3, bells); - //wait until it's day: - while (!getGameStateModel().getDayClone()) { - Thread.sleep(100); - } - Thread.sleep(500); - //just so the alarm isn't rung exactly when the day starts, add random delay - Random random = new Random(); - Thread.sleep(random.nextInt(1000)); - - bell.play(); - ringBellSound(); + waitForDayThenRingBell(bell); } } catch (Exception e) { e.printStackTrace(); @@ -624,17 +596,7 @@ public class GameController implements Initializable { try { if(!gameStateModel.getKickedOff()[3]) { Animation bell = new BellAnimation(noiseImage2, bells); - //wait until it's day: - while (!getGameStateModel().getDayClone()) { - Thread.sleep(100); - } - Thread.sleep(500); - //just so the alarm isn't rung exactly when the day starts, add random delay - Random random = new Random(); - Thread.sleep(random.nextInt(1000)); - - bell.play(); - ringBellSound(); + waitForDayThenRingBell(bell); } } catch (Exception e) { e.printStackTrace(); @@ -654,17 +616,7 @@ public class GameController implements Initializable { try { if(!gameStateModel.getKickedOff()[4]) { Animation bell = new BellAnimation(noiseImage1, bells); - //wait until it's day: - while (!getGameStateModel().getDayClone()) { - Thread.sleep(100); - } - Thread.sleep(500); - //just so the alarm isn't rung exactly when the day starts, add random delay - Random random = new Random(); - Thread.sleep(random.nextInt(1000)); - - bell.play(); - ringBellSound(); + waitForDayThenRingBell(bell); } } catch (Exception e) { e.printStackTrace(); @@ -684,16 +636,7 @@ public class GameController implements Initializable { try { if(!gameStateModel.getKickedOff()[5]) { Animation bell = new BellAnimation(noiseImage0, bells); - //wait until it's day: - while (!getGameStateModel().getDayClone()) { - Thread.sleep(100); - } - Thread.sleep(500); - //just so the alarm isn't rung exactly when the day starts, add random delay - Random random = new Random(); - Thread.sleep(random.nextInt(1000)); - bell.play(); - ringBellSound(); + waitForDayThenRingBell(bell); } } catch (Exception e) { e.printStackTrace(); @@ -734,6 +677,34 @@ public class GameController implements Initializable { ChatApp.setGameController(this); } + public static void waitForDayThenRingBell(Animation bell) { + new Thread(new Runnable() { + @Override + public void run() { + try { + //wait until it's day: + int timeoutCounter = 0; //otherwise this thread can get stuck in a loop if player leaves server + if (!getGameStateModel().getDayClone()) { //used to ring bell immediately if it's already day + while (!getGameStateModel().getDayClone() + && timeoutCounter < Timer.ghostAfterVoteTime * 15) { + Thread.sleep(100); + timeoutCounter++; + } + //just so the alarm isn't rung exactly when the day starts, also add random delay + Thread.sleep(1000); + Random random = new Random(); + Thread.sleep(random.nextInt(1000)); + } + + } catch (Exception e) { + e.printStackTrace(); + } + bell.play(); + ringBellSound(); + } + }).start(); + } + /** * plays bell sound, but only if it hasn't been played recently, to avoid artefacts due to * overlapping sounds