From 982e4ea5ad5f4a7d6b720c31469471dac0b52078 Mon Sep 17 00:00:00 2001 From: Matt Low Date: Tue, 7 Jan 2020 01:06:12 +0400 Subject: [PATCH] More sounds! Closes #4 --- core/src/com/me/pacman/Sound.java | 15 ++++-- core/src/com/me/pacman/entity/Ghost.java | 1 + core/src/com/me/pacman/state/PlayState.java | 60 +++++++++++++++------ 3 files changed, 55 insertions(+), 21 deletions(-) diff --git a/core/src/com/me/pacman/Sound.java b/core/src/com/me/pacman/Sound.java index 1d56820..45bf94e 100644 --- a/core/src/com/me/pacman/Sound.java +++ b/core/src/com/me/pacman/Sound.java @@ -54,11 +54,17 @@ public class Sound { return id; } + public boolean isLooping(Effect effect) { + return playing[effect.ordinal()] > -1; + } + public void stop(Effect effect) { - long id = playing[effect.ordinal()]; com.badlogic.gdx.audio.Sound sound = getSound(effect); - sound.stop(id); - playing[effect.ordinal()] = -1; + long id = playing[effect.ordinal()]; + if (id > -1) { + sound.stop(id); + playing[effect.ordinal()] = -1; + } } public void stopAll() { @@ -66,8 +72,7 @@ public class Sound { if (playing[i] < 0) { continue; } - getSound(Effect.values()[i]).stop(playing[i]); - playing[i] = -1; + stop(Effect.values()[i]); } } diff --git a/core/src/com/me/pacman/entity/Ghost.java b/core/src/com/me/pacman/entity/Ghost.java index efb8eb6..78b4828 100644 --- a/core/src/com/me/pacman/entity/Ghost.java +++ b/core/src/com/me/pacman/entity/Ghost.java @@ -123,6 +123,7 @@ public class Ghost extends MovableEntity { if (currentPath instanceof EnterGhostHousePath) { inHouse = true; + state.updateBackgroundAudio(); } currentPath = null; } diff --git a/core/src/com/me/pacman/state/PlayState.java b/core/src/com/me/pacman/state/PlayState.java index 7595205..126d27d 100644 --- a/core/src/com/me/pacman/state/PlayState.java +++ b/core/src/com/me/pacman/state/PlayState.java @@ -12,6 +12,7 @@ import com.me.pacman.PacDude; import com.me.pacman.Sound; import com.me.pacman.entity.*; import com.me.pacman.entity.ai.ReturnToBase; +import com.me.pacman.entity.path.EnterGhostHousePath; import com.me.pacman.level.Level; import com.me.pacman.level.LevelTile; import com.me.pacman.level.Modifiers; @@ -98,6 +99,32 @@ public class PlayState extends LevelState { super(game); } + public void updateBackgroundAudio() { + for (Ghost ghost : ghosts) { + if ((ghost.currentBehaviour instanceof ReturnToBase + || ghost.currentPath instanceof EnterGhostHousePath) && !ghost.inHouse) { + if (!game.sound.isLooping(Sound.Effect.RETURN_BASE)) { + game.sound.stopAll(); + game.sound.loop(Sound.Effect.RETURN_BASE); + } + return; + } + } + + if (frightTimer > 0) { + if (!game.sound.isLooping(Sound.Effect.FRIGHT)) { + game.sound.stopAll(); + game.sound.loop(Sound.Effect.FRIGHT); + } + return; + } + + if (!game.sound.isLooping(Sound.Effect.SIREN)) { + game.sound.stopAll(); + game.sound.loop(Sound.Effect.SIREN); + } + } + private void setGameState(GameState state) { this.state = state; this.stateTimer = state.timer; @@ -162,6 +189,7 @@ public class PlayState extends LevelState { initializeLevel(); + game.sound.stopAll(); game.sound.play(Sound.Effect.BEGINNING); } @@ -199,38 +227,33 @@ public class PlayState extends LevelState { setGameState(GameState.PRE_NEW_GAME); } - public void stateTransition() { + public GameState stateTransition() { switch (state) { // The state we're transitioning /from/ case PRE_NEW_GAME: newGame(); - setGameState(GameState.START_ROUND_WAIT); - break; + return GameState.START_ROUND_WAIT; case START_ROUND_WAIT: case NEW_ROUND_WAIT: startGame(); - setGameState(GameState.PLAYING); - break; + return GameState.PLAYING; case ROUND_WON_WAIT: - setGameState(GameState.ROUND_WON); - break; + return GameState.ROUND_WON; case ROUND_WON: newRound(); - setGameState(GameState.NEW_ROUND_WAIT); - break; + return GameState.NEW_ROUND_WAIT; case GAME_OVER: preNewGame(); - setGameState(GameState.PRE_NEW_GAME); - break; + return GameState.PRE_NEW_GAME; case GHOST_CAUGHT_POINTS_WAIT: - setGameState(GameState.PLAYING); - break; + updateBackgroundAudio(); + return GameState.PLAYING; case PACMAN_CAUGHT_WAIT: game.sound.play(Sound.Effect.DEATH); pacman.alive = false; - setGameState(GameState.PACMAN_CAUGHT); - break; + return GameState.PACMAN_CAUGHT; } + return null; } @Override @@ -337,6 +360,8 @@ public class PlayState extends LevelState { frightTimer = Modifiers.getFrightTime(round); ghostsCaught = 0; score += 50; + + updateBackgroundAudio(); } private void pacmanCaught() { @@ -357,6 +382,7 @@ public class PlayState extends LevelState { ghostsCaught++; score += ghostsCaught * 200; + game.sound.play(Sound.Effect.EAT_GHOST); setGameState(GameState.GHOST_CAUGHT_POINTS_WAIT); } @@ -379,6 +405,8 @@ public class PlayState extends LevelState { ghost.caught = false; ghost.currentBehaviour = ghost.chaseBehaviour; } + + updateBackgroundAudio(); } } @@ -464,7 +492,7 @@ public class PlayState extends LevelState { if (state.timer > 0) { stateTimer -= dt; if (stateTimer <= 0) { - stateTransition(); + setGameState(stateTransition()); } } }