From c7f352ea0c03df83c07162652233d5f2b9de56fc Mon Sep 17 00:00:00 2001 From: Matt Low Date: Mon, 6 Jan 2020 16:48:37 +0400 Subject: [PATCH] Add central Sound class Stop sounds when last pellet is eaten. --- core/assets/sounds/{chase.wav => fright.wav} | Bin core/src/com/me/pacman/Assets.java | 6 +- core/src/com/me/pacman/Sound.java | 109 +++++++++++++++++++ core/src/com/me/pacman/state/PlayState.java | 20 ++-- 4 files changed, 121 insertions(+), 14 deletions(-) rename core/assets/sounds/{chase.wav => fright.wav} (100%) create mode 100644 core/src/com/me/pacman/Sound.java diff --git a/core/assets/sounds/chase.wav b/core/assets/sounds/fright.wav similarity index 100% rename from core/assets/sounds/chase.wav rename to core/assets/sounds/fright.wav diff --git a/core/src/com/me/pacman/Assets.java b/core/src/com/me/pacman/Assets.java index 2b9f6e0..bc6d492 100644 --- a/core/src/com/me/pacman/Assets.java +++ b/core/src/com/me/pacman/Assets.java @@ -27,7 +27,7 @@ public class Assets { public TextureRegion[][] volume; public Sound beginning, beginning_alt; - public Sound chaseSound; + public Sound fright; public Sound chomp_1, chomp_2; public Sound deathSound; public Sound eat_fruit, eat_ghost; @@ -56,7 +56,7 @@ public class Assets { manager.load("sounds/beginning.wav", Sound.class); manager.load("sounds/beginning_alt.wav", Sound.class); - manager.load("sounds/chase.wav", Sound.class); + manager.load("sounds/fright.wav", Sound.class); manager.load("sounds/chomp_1.wav", Sound.class); manager.load("sounds/chomp_2.wav", Sound.class); manager.load("sounds/death.wav", Sound.class); @@ -98,7 +98,7 @@ public class Assets { // all our sounds beginning = manager.get("sounds/beginning.wav", Sound.class); beginning_alt = manager.get("sounds/beginning_alt.wav", Sound.class); - chaseSound = manager.get("sounds/chase.wav", Sound.class); + fright = manager.get("sounds/fright.wav", Sound.class); chomp_1 = manager.get("sounds/chomp_1.wav", Sound.class); chomp_2 = manager.get("sounds/chomp_2.wav", Sound.class); deathSound = manager.get("sounds/death.wav", Sound.class); diff --git a/core/src/com/me/pacman/Sound.java b/core/src/com/me/pacman/Sound.java new file mode 100644 index 0000000..1d56820 --- /dev/null +++ b/core/src/com/me/pacman/Sound.java @@ -0,0 +1,109 @@ +package com.me.pacman; + +import java.util.Arrays; + +public class Sound { + + public enum Effect { + BEGINNING, + BEGINNING_ALT, + FRIGHT, + CHOMP_1, + CHOMP_2, + DEATH, + EAT_FRUIT, + EAT_GHOST, + EXTRA_LIFE, + RETURN_BASE, + SIREN, + SIREN_FAST, + SIREN_FASTER, + SIREN_FASTEST + } + + private float volume = 1.0f; + private boolean muted; + + private PacDude game; + private long[] playing; + + public Sound(PacDude game) { + this.game = game; + this.playing = new long[Effect.values().length]; + Arrays.fill(this.playing, -1); + } + + private float volume() { + return muted? 0f : volume; + } + + public void setMuted(boolean muted) { + this.muted = muted; + } + + 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()); + playing[effect.ordinal()] = id; + return id; + } + + 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; + } + + public void stopAll() { + for (int i = 0; i < playing.length; i++) { + if (playing[i] < 0) { + continue; + } + getSound(Effect.values()[i]).stop(playing[i]); + playing[i] = -1; + } + } + + private com.badlogic.gdx.audio.Sound getSound(Effect effect) { + Assets a = game.assets; + switch (effect) { + case BEGINNING: + return a.beginning; + case BEGINNING_ALT: + return a.beginning_alt; + case FRIGHT: + return a.fright; + case CHOMP_1: + return a.chomp_1; + case CHOMP_2: + return a.chomp_2; + case DEATH: + return a.deathSound; + case EAT_FRUIT: + return a.eat_fruit; + case EAT_GHOST: + return a.eat_ghost; + case EXTRA_LIFE: + return a.extra_life; + case RETURN_BASE: + return a.return_base; + case SIREN: + return a.siren; + case SIREN_FAST: + return a.siren_fast; + case SIREN_FASTER: + return a.siren_faster; + case SIREN_FASTEST: + return a.siren_fastest; + } + return null; + } + +} diff --git a/core/src/com/me/pacman/state/PlayState.java b/core/src/com/me/pacman/state/PlayState.java index 667debf..7595205 100644 --- a/core/src/com/me/pacman/state/PlayState.java +++ b/core/src/com/me/pacman/state/PlayState.java @@ -3,13 +3,13 @@ package com.me.pacman.state; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.Input; import com.badlogic.gdx.InputAdapter; -import com.badlogic.gdx.audio.Sound; import com.badlogic.gdx.graphics.Color; import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.graphics.g2d.BitmapFont; import com.badlogic.gdx.graphics.g2d.TextureRegion; import com.badlogic.gdx.math.Vector2; 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.level.Level; @@ -42,8 +42,6 @@ public class PlayState extends LevelState { public Random random; - private long sirenId; - public int pelletsRemaining; private int pelletsEaten; @@ -164,7 +162,7 @@ public class PlayState extends LevelState { initializeLevel(); - game.assets.beginning.play(1.0f); + game.sound.play(Sound.Effect.BEGINNING); } private void newGame() { @@ -180,12 +178,12 @@ public class PlayState extends LevelState { round++; - game.assets.siren.stop(sirenId); - game.assets.beginning_alt.play(1.0f); + game.sound.stopAll(); + game.sound.play(Sound.Effect.BEGINNING_ALT); } private void startGame() { - sirenId = game.assets.siren.loop(1.0f); + game.sound.loop(Sound.Effect.SIREN); pacman.moving = true; } @@ -228,7 +226,7 @@ public class PlayState extends LevelState { setGameState(GameState.PLAYING); break; case PACMAN_CAUGHT_WAIT: - game.assets.deathSound.play(1f); + game.sound.play(Sound.Effect.DEATH); pacman.alive = false; setGameState(GameState.PACMAN_CAUGHT); break; @@ -295,8 +293,7 @@ public class PlayState extends LevelState { private void pelletEaten(float x, float y) { level.setTile(x, y, LevelTile.EMPTY); - Sound chomp = pelletsEaten % 2 == 0? game.assets.chomp_1 : game.assets.chomp_2; - chomp.play(1.0f); + game.sound.play(pelletsEaten % 2 == 0? Sound.Effect.CHOMP_1 : Sound.Effect.CHOMP_2); pelletsEaten++; pelletsRemaining--; @@ -319,6 +316,7 @@ public class PlayState extends LevelState { secondsSinceLastDot = 0; if (pelletsRemaining == 0) { + game.sound.stopAll(); setGameState(GameState.ROUND_WON_WAIT); } } @@ -342,7 +340,7 @@ public class PlayState extends LevelState { } private void pacmanCaught() { - game.assets.siren.stop(sirenId); + game.sound.stop(Sound.Effect.SIREN); pacman.moving = false; pelletsEatenSinceDeath = 0; pelletsEatenSinceDeathCounterEnabled = true;