Add central Sound class
Stop sounds when last pellet is eaten.
This commit is contained in:
parent
02a4c7d5ed
commit
c7f352ea0c
@ -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);
|
||||
|
109
core/src/com/me/pacman/Sound.java
Normal file
109
core/src/com/me/pacman/Sound.java
Normal file
@ -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;
|
||||
}
|
||||
|
||||
}
|
@ -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;
|
||||
|
Loading…
Reference in New Issue
Block a user