More sounds! Closes #4

This commit is contained in:
Matt Low 2020-01-07 01:06:12 +04:00
parent c7f352ea0c
commit 982e4ea5ad
3 changed files with 55 additions and 21 deletions

View File

@ -54,20 +54,25 @@ 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);
long id = playing[effect.ordinal()];
if (id > -1) {
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;
stop(Effect.values()[i]);
}
}

View File

@ -123,6 +123,7 @@ public class Ghost extends MovableEntity {
if (currentPath instanceof EnterGhostHousePath) {
inHouse = true;
state.updateBackgroundAudio();
}
currentPath = null;
}

View File

@ -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());
}
}
}