diff --git a/core/src/com/me/pacman/Sound.java b/core/src/com/me/pacman/Sound.java index 45bf94e..537b7de 100644 --- a/core/src/com/me/pacman/Sound.java +++ b/core/src/com/me/pacman/Sound.java @@ -26,11 +26,14 @@ public class Sound { private PacDude game; private long[] playing; + private long[] looping; public Sound(PacDude game) { this.game = game; this.playing = new long[Effect.values().length]; + this.looping = new long[Effect.values().length]; Arrays.fill(this.playing, -1); + Arrays.fill(this.looping, -1); } private float volume() { @@ -42,23 +45,26 @@ public class Sound { } public long play(Effect effect) { - return muted? -1 : getSound(effect).play(volume()); - } - - public long loop(Effect effect) { - if (muted) { - return -1; - } - long id = getSound(effect).loop(volume()); + long id = getSound(effect).play(volume()); playing[effect.ordinal()] = id; return id; } - public boolean isLooping(Effect effect) { + public long loop(Effect effect) { + long id = getSound(effect).loop(volume()); + looping[effect.ordinal()] = id; + return id; + } + + public boolean isPlaying(Effect effect) { return playing[effect.ordinal()] > -1; } - public void stop(Effect effect) { + public boolean isLooping(Effect effect) { + return looping[effect.ordinal()] > -1; + } + + public void stopPlaying(Effect effect) { com.badlogic.gdx.audio.Sound sound = getSound(effect); long id = playing[effect.ordinal()]; if (id > -1) { @@ -67,12 +73,59 @@ public class Sound { } } - public void stopAll() { + public void stopLooping(Effect effect) { + com.badlogic.gdx.audio.Sound sound = getSound(effect); + long id = looping[effect.ordinal()]; + if (id > -1) { + sound.stop(id); + looping[effect.ordinal()] = -1; + } + } + + public void stopPlays() { for (int i = 0; i < playing.length; i++) { - if (playing[i] < 0) { - continue; + if (playing[i] > -1) { + getSound(Effect.values()[i]).stop(playing[i]); + playing[i] = -1; + } + } + } + + public void stopLoops() { + for (int i = 0; i < looping.length; i++) { + if (looping[i] > -1) { + getSound(Effect.values()[i]).stop(looping[i]); + looping[i] = -1; + } + } + } + + public void stopAll() { + stopPlays(); + stopLoops(); + } + + public void pauseAll() { + for (int i = 0, n = Effect.values().length; i < n; i++) { + com.badlogic.gdx.audio.Sound sound = getSound(Effect.values()[i]); + if (playing[i] > -1) { + sound.pause(playing[i]); + } + if (looping[i] > -1) { + sound.pause(looping[i]); + } + } + } + + public void resumeAll() { + for (int i = 0, n = Effect.values().length; i < n; i++) { + com.badlogic.gdx.audio.Sound sound = getSound(Effect.values()[i]); + if (playing[i] > -1) { + sound.resume(playing[i]); + } + if (looping[i] > -1) { + sound.resume(looping[i]); } - stop(Effect.values()[i]); } } diff --git a/core/src/com/me/pacman/state/PlayState.java b/core/src/com/me/pacman/state/PlayState.java index 126d27d..3997143 100644 --- a/core/src/com/me/pacman/state/PlayState.java +++ b/core/src/com/me/pacman/state/PlayState.java @@ -104,7 +104,7 @@ public class PlayState extends LevelState { if ((ghost.currentBehaviour instanceof ReturnToBase || ghost.currentPath instanceof EnterGhostHousePath) && !ghost.inHouse) { if (!game.sound.isLooping(Sound.Effect.RETURN_BASE)) { - game.sound.stopAll(); + game.sound.stopLoops(); game.sound.loop(Sound.Effect.RETURN_BASE); } return; @@ -113,14 +113,14 @@ public class PlayState extends LevelState { if (frightTimer > 0) { if (!game.sound.isLooping(Sound.Effect.FRIGHT)) { - game.sound.stopAll(); + game.sound.stopLoops(); game.sound.loop(Sound.Effect.FRIGHT); } return; } if (!game.sound.isLooping(Sound.Effect.SIREN)) { - game.sound.stopAll(); + game.sound.stopLoops(); game.sound.loop(Sound.Effect.SIREN); } } @@ -339,7 +339,7 @@ public class PlayState extends LevelState { secondsSinceLastDot = 0; if (pelletsRemaining == 0) { - game.sound.stopAll(); + game.sound.stopLoops(); setGameState(GameState.ROUND_WON_WAIT); } } @@ -365,7 +365,7 @@ public class PlayState extends LevelState { } private void pacmanCaught() { - game.sound.stop(Sound.Effect.SIREN); + game.sound.stopLooping(Sound.Effect.SIREN); pacman.moving = false; pelletsEatenSinceDeath = 0; pelletsEatenSinceDeathCounterEnabled = true; @@ -521,6 +521,11 @@ public class PlayState extends LevelState { case Input.Keys.ESCAPE: case Input.Keys.P: paused = !paused; + if (paused) { + game.sound.pauseAll(); + } else { + game.sound.resumeAll(); + } break; case Input.Keys.N: preNewGame();