Updated Bell timing for performance optimization, changed some values in Timer class to make gameplay less hectic

This commit is contained in:
Jonas 2022-05-16 20:20:49 +02:00
parent 7a81987316
commit 2e901490b2
3 changed files with 40 additions and 69 deletions

View File

@ -16,13 +16,13 @@ public class Timer {
/** /**
* The maximum length of the ghost vote in the night, in seconds * 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 / * 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. * 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 * 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, * The length of time in seconds after the human vote, as the 'winner' of the vote is announced,
* before the night begins * before the night begins
*/ */
public static final int humanAfterVoteTime = 5; public static final int humanAfterVoteTime = 8;
/** /**
* The checking interval in seconds * The checking interval in seconds

View File

@ -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; 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 { public class DayNightChangeListener implements Runnable {
@ -63,7 +63,7 @@ public class DayNightChangeListener implements Runnable {
chatApp.getGameController().updateRoomLabels(); chatApp.getGameController().updateRoomLabels();
gameStateModel.setRoleFromPosition(position); gameStateModel.setRoleFromPosition(position);
try { try {
Thread.sleep(100); Thread.sleep(200);
} catch (InterruptedException e) { } catch (InterruptedException e) {
e.printStackTrace(); e.printStackTrace();
} }

View File

@ -2,6 +2,7 @@ package ch.unibas.dmi.dbis.cs108.multiplayer.client.gui.game;
import static javafx.scene.AccessibleRole.PARENT; 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.Sound;
import ch.unibas.dmi.dbis.cs108.multiplayer.client.gui.ChatApp; import ch.unibas.dmi.dbis.cs108.multiplayer.client.gui.ChatApp;
import ch.unibas.dmi.dbis.cs108.multiplayer.client.gui.GameStateModel; import ch.unibas.dmi.dbis.cs108.multiplayer.client.gui.GameStateModel;
@ -534,17 +535,7 @@ public class GameController implements Initializable {
try { try {
if(!gameStateModel.getKickedOff()[0]) { if(!gameStateModel.getKickedOff()[0]) {
Animation bell = new BellAnimation(noiseImage5, bells); Animation bell = new BellAnimation(noiseImage5, bells);
//wait until it's day: waitForDayThenRingBell(bell);
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();
} }
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
@ -565,16 +556,7 @@ public class GameController implements Initializable {
try { try {
if(!gameStateModel.getKickedOff()[1]) { if(!gameStateModel.getKickedOff()[1]) {
Animation bell = new BellAnimation(noiseImage4, bells); Animation bell = new BellAnimation(noiseImage4, bells);
//wait until it's day: waitForDayThenRingBell(bell);
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();
} }
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
@ -594,17 +576,7 @@ public class GameController implements Initializable {
try { try {
if(!gameStateModel.getKickedOff()[2]) { if(!gameStateModel.getKickedOff()[2]) {
Animation bell = new BellAnimation(noiseImage3, bells); Animation bell = new BellAnimation(noiseImage3, bells);
//wait until it's day: waitForDayThenRingBell(bell);
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();
} }
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
@ -624,17 +596,7 @@ public class GameController implements Initializable {
try { try {
if(!gameStateModel.getKickedOff()[3]) { if(!gameStateModel.getKickedOff()[3]) {
Animation bell = new BellAnimation(noiseImage2, bells); Animation bell = new BellAnimation(noiseImage2, bells);
//wait until it's day: waitForDayThenRingBell(bell);
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();
} }
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
@ -654,17 +616,7 @@ public class GameController implements Initializable {
try { try {
if(!gameStateModel.getKickedOff()[4]) { if(!gameStateModel.getKickedOff()[4]) {
Animation bell = new BellAnimation(noiseImage1, bells); Animation bell = new BellAnimation(noiseImage1, bells);
//wait until it's day: waitForDayThenRingBell(bell);
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();
} }
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
@ -684,16 +636,7 @@ public class GameController implements Initializable {
try { try {
if(!gameStateModel.getKickedOff()[5]) { if(!gameStateModel.getKickedOff()[5]) {
Animation bell = new BellAnimation(noiseImage0, bells); Animation bell = new BellAnimation(noiseImage0, bells);
//wait until it's day: waitForDayThenRingBell(bell);
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();
} }
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
@ -734,6 +677,34 @@ public class GameController implements Initializable {
ChatApp.setGameController(this); 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 * plays bell sound, but only if it hasn't been played recently, to avoid artefacts due to
* overlapping sounds * overlapping sounds