diff --git a/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/client/Sound.java b/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/client/Sound.java index 9848a45..3ef125c 100644 --- a/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/client/Sound.java +++ b/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/client/Sound.java @@ -2,10 +2,14 @@ package ch.unibas.dmi.dbis.cs108.multiplayer.client; import java.io.File; import java.net.URL; +import java.util.Random; import javafx.scene.media.*; public class Sound { + + static final double defaultvolume = 0.7; + static URL backgroundSoundsURL = Sound.class.getResource("sounds/tracknoise.wav"); static AudioClip backgroundSounds = new AudioClip(backgroundSoundsURL.toString()); static boolean playingBackgroundSounds = false; @@ -46,15 +50,15 @@ public class Sound { static URL voteforhumanURL = Sound.class.getResource("sounds/voteforhuman.wav"); static AudioClip voteforhuman = new AudioClip(voteforhumanURL.toString()); + static Random random = new Random(); + public static void main(String[] args) throws Exception { - //startPlayingBackgroundSounds(); - + startPlayingBackgroundSounds(); ghost(); - while(true) { } @@ -66,37 +70,66 @@ public class Sound { public static void startPlayingBackgroundSounds() { playingBackgroundSounds = true; backgroundSounds.setCycleCount(AudioClip.INDEFINITE); - backgroundSounds.play(); + backgroundSounds.play(defaultvolume, 0.0, 1.0, 0.0, 6 ); } public static void bell() { - bell.play(); + bell.play(defaultvolume - 0.5); } public static void startDaynoises() { daynoises.setCycleCount(AudioClip.INDEFINITE); - daynoises.play(); + daynoises.play(defaultvolume - 0.5); } public static void stopDaynoises() { daynoises.stop(); } + public static void startNightnoises() { + nightnoises.setCycleCount(AudioClip.INDEFINITE); + nightnoises.play(defaultvolume - 0.5); + } + + public static void stopNightnoises() { + nightnoises.stop(); + } + + public static void gameoverghosts() { gameoverghosts.play(defaultvolume); } + + public static void gameoverhumans() { gameoverhumans.play(defaultvolume); } + + public static void musicday() { + musicday.play(defaultvolume); + } + + public static void stopmusicday() { + //todo: gentle fade out + musicday.stop(); + } + public static void ghost() { double playbackspeed = (Math.random() / 5.0) + 0.9; - System.out.println(playbackspeed); - ghost01.play(0.5, 0.0, playbackspeed, 0.0, 5); + int ghostsoundnr = random.nextInt(4) + 1; + System.out.println(ghostsoundnr); + AudioClip ghost; + switch (ghostsoundnr) { + case 1: + ghost = ghost01; + break; + case 2: + ghost = ghost02; + break; + case 3: + ghost = ghost03; + break; + default: + ghost = ghost04; + break; + } + ghost.play(defaultvolume, 0.0, playbackspeed, 0.0, 5); } } - - - - - - - - - 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 9dc374f..df7e751 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.multiplayer.client.Sound; 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.Sprites; @@ -36,6 +37,9 @@ public class GameController implements Initializable { public static final Logger LOGGER = LogManager.getLogger(GameController.class); public static final BudaLogConfig l = new BudaLogConfig(LOGGER); + static boolean justRangBell = false; //used to track if the bell has been rung recently + static final int minimumBellTime = 1000; //minimal time that has to pass between bells, in ms + static boolean playingDayNoises = true; //true if playing day noises, false if playing night noises private static ClientModel client; @@ -135,8 +139,20 @@ public class GameController implements Initializable { public void run() { try{ if(gameStateModel.getDayClone()) { + if (!playingDayNoises) { + Sound.startDaynoises(); + Sound.musicday(); + Sound.stopNightnoises(); + playingDayNoises = true; + } Sprites.updateDayRoomSprites(gameStateModel.getPassengerTrainClone()[1], gameStateModel.getKickedOff()); } else { + if (playingDayNoises) { + Sound.startNightnoises(); + Sound.stopmusicday(); + Sound.stopDaynoises(); + playingDayNoises = false; + } Sprites.updateNightRoomSprites(gameStateModel.getPassengerTrainClone()[1], gameStateModel.getKickedOff()); } /*room0ImageView.setImage(Sprites.getARoom(0)); @@ -518,6 +534,7 @@ public class GameController implements Initializable { if(!gameStateModel.getKickedOff()[0]) { Animation bell = new BellAnimation(noiseImage5, bells); bell.play(); + ringBellSound(); } } catch (Exception e) { e.printStackTrace(); @@ -539,6 +556,7 @@ public class GameController implements Initializable { if(!gameStateModel.getKickedOff()[1]) { Animation bell = new BellAnimation(noiseImage4, bells); bell.play(); + ringBellSound(); } } catch (Exception e) { e.printStackTrace(); @@ -559,6 +577,7 @@ public class GameController implements Initializable { if(!gameStateModel.getKickedOff()[2]) { Animation bell = new BellAnimation(noiseImage3, bells); bell.play(); + ringBellSound(); } } catch (Exception e) { e.printStackTrace(); @@ -579,6 +598,7 @@ public class GameController implements Initializable { if(!gameStateModel.getKickedOff()[3]) { Animation bell = new BellAnimation(noiseImage2, bells); bell.play(); + ringBellSound(); } } catch (Exception e) { e.printStackTrace(); @@ -599,6 +619,7 @@ public class GameController implements Initializable { if(!gameStateModel.getKickedOff()[4]) { Animation bell = new BellAnimation(noiseImage1, bells); bell.play(); + ringBellSound(); } } catch (Exception e) { e.printStackTrace(); @@ -619,6 +640,7 @@ public class GameController implements Initializable { if(!gameStateModel.getKickedOff()[5]) { Animation bell = new BellAnimation(noiseImage0, bells); bell.play(); + ringBellSound(); } } catch (Exception e) { e.printStackTrace(); @@ -658,4 +680,32 @@ public class GameController implements Initializable { noiseButton.toFront(); ChatApp.setGameController(this); } + + /** + * plays bell sound, but only if it hasn't been played recently, to avoid artefacts due to + * overlapping sounds + */ + public static void ringBellSound() { + if (!justRangBell) { + justRangBell = true; + Sound.bell(); + try { + System.out.println(justRangBell); + Thread.sleep(minimumBellTime); + new Thread(new Runnable() { + @Override + public void run() { + try { + Thread.sleep(minimumBellTime); + } catch (InterruptedException e) { + e.printStackTrace(); + } + justRangBell = false; + } + }).start(); + } catch (Exception e) { + e.printStackTrace(); + } + } + } }