Sound pausing/resuming

This commit is contained in:
Matt Low 2020-01-07 14:58:20 +04:00
parent c0c0a86a4c
commit b8fcfb4de7
2 changed files with 77 additions and 19 deletions

View File

@ -26,11 +26,14 @@ public class Sound {
private PacDude game; private PacDude game;
private long[] playing; private long[] playing;
private long[] looping;
public Sound(PacDude game) { public Sound(PacDude game) {
this.game = game; this.game = game;
this.playing = new long[Effect.values().length]; this.playing = new long[Effect.values().length];
this.looping = new long[Effect.values().length];
Arrays.fill(this.playing, -1); Arrays.fill(this.playing, -1);
Arrays.fill(this.looping, -1);
} }
private float volume() { private float volume() {
@ -42,23 +45,26 @@ public class Sound {
} }
public long play(Effect effect) { public long play(Effect effect) {
return muted? -1 : getSound(effect).play(volume()); long id = getSound(effect).play(volume());
}
public long loop(Effect effect) {
if (muted) {
return -1;
}
long id = getSound(effect).loop(volume());
playing[effect.ordinal()] = id; playing[effect.ordinal()] = id;
return 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; 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); com.badlogic.gdx.audio.Sound sound = getSound(effect);
long id = playing[effect.ordinal()]; long id = playing[effect.ordinal()];
if (id > -1) { if (id > -1) {
@ -67,12 +73,59 @@ public class Sound {
} }
} }
public void stopAll() { public void stopLooping(Effect effect) {
for (int i = 0; i < playing.length; i++) { com.badlogic.gdx.audio.Sound sound = getSound(effect);
if (playing[i] < 0) { long id = looping[effect.ordinal()];
continue; if (id > -1) {
sound.stop(id);
looping[effect.ordinal()] = -1;
}
}
public void stopPlays() {
for (int i = 0; i < playing.length; i++) {
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]);
} }
} }

View File

@ -104,7 +104,7 @@ public class PlayState extends LevelState {
if ((ghost.currentBehaviour instanceof ReturnToBase if ((ghost.currentBehaviour instanceof ReturnToBase
|| ghost.currentPath instanceof EnterGhostHousePath) && !ghost.inHouse) { || ghost.currentPath instanceof EnterGhostHousePath) && !ghost.inHouse) {
if (!game.sound.isLooping(Sound.Effect.RETURN_BASE)) { if (!game.sound.isLooping(Sound.Effect.RETURN_BASE)) {
game.sound.stopAll(); game.sound.stopLoops();
game.sound.loop(Sound.Effect.RETURN_BASE); game.sound.loop(Sound.Effect.RETURN_BASE);
} }
return; return;
@ -113,14 +113,14 @@ public class PlayState extends LevelState {
if (frightTimer > 0) { if (frightTimer > 0) {
if (!game.sound.isLooping(Sound.Effect.FRIGHT)) { if (!game.sound.isLooping(Sound.Effect.FRIGHT)) {
game.sound.stopAll(); game.sound.stopLoops();
game.sound.loop(Sound.Effect.FRIGHT); game.sound.loop(Sound.Effect.FRIGHT);
} }
return; return;
} }
if (!game.sound.isLooping(Sound.Effect.SIREN)) { if (!game.sound.isLooping(Sound.Effect.SIREN)) {
game.sound.stopAll(); game.sound.stopLoops();
game.sound.loop(Sound.Effect.SIREN); game.sound.loop(Sound.Effect.SIREN);
} }
} }
@ -339,7 +339,7 @@ public class PlayState extends LevelState {
secondsSinceLastDot = 0; secondsSinceLastDot = 0;
if (pelletsRemaining == 0) { if (pelletsRemaining == 0) {
game.sound.stopAll(); game.sound.stopLoops();
setGameState(GameState.ROUND_WON_WAIT); setGameState(GameState.ROUND_WON_WAIT);
} }
} }
@ -365,7 +365,7 @@ public class PlayState extends LevelState {
} }
private void pacmanCaught() { private void pacmanCaught() {
game.sound.stop(Sound.Effect.SIREN); game.sound.stopLooping(Sound.Effect.SIREN);
pacman.moving = false; pacman.moving = false;
pelletsEatenSinceDeath = 0; pelletsEatenSinceDeath = 0;
pelletsEatenSinceDeathCounterEnabled = true; pelletsEatenSinceDeathCounterEnabled = true;
@ -521,6 +521,11 @@ public class PlayState extends LevelState {
case Input.Keys.ESCAPE: case Input.Keys.ESCAPE:
case Input.Keys.P: case Input.Keys.P:
paused = !paused; paused = !paused;
if (paused) {
game.sound.pauseAll();
} else {
game.sound.resumeAll();
}
break; break;
case Input.Keys.N: case Input.Keys.N:
preNewGame(); preNewGame();