added different music for different game states

This commit is contained in:
Timo Brennförder
2024-10-05 20:56:26 +02:00
parent 329d3d7372
commit dac84388fc
18 changed files with 236 additions and 28 deletions

View File

@@ -11,6 +11,7 @@
import pp.battleship.message.server.EffectMessage;
import pp.battleship.model.IntPoint;
import pp.battleship.model.ShipMap;
import pp.battleship.notification.Music;
import pp.battleship.notification.Sound;
import java.lang.System.Logger.Level;
@@ -29,6 +30,7 @@ class BattleState extends ClientState {
*/
public BattleState(ClientGameLogic logic, boolean myTurn) {
super(logic);
logic.playMusic(Music.GAME_THEME);
this.myTurn = myTurn;
}
@@ -61,7 +63,7 @@ public void receivedEffect(EffectMessage msg) {
logic.getOpponentMap().add(msg.getDestroyedShip());
if (msg.isGameOver()) {
msg.getRemainingOpponentShips().forEach(logic.getOpponentMap()::add);
logic.setState(new GameOverState(logic));
logic.setState(new GameOverState(logic, msg.isGameLost()));
}
}

View File

@@ -20,6 +20,8 @@
import pp.battleship.notification.GameEventBroker;
import pp.battleship.notification.GameEventListener;
import pp.battleship.notification.InfoTextEvent;
import pp.battleship.notification.Music;
import pp.battleship.notification.MusicEvent;
import pp.battleship.notification.Sound;
import pp.battleship.notification.SoundEvent;
@@ -352,4 +354,12 @@ public void notifyListeners(GameEvent event) {
public void update(float delta) {
state.update(delta);
}
/**
* Triggers an event to play specified music
* @param music the music to be played
*/
public void playMusic(Music music){
notifyListeners(new MusicEvent(music));
}
}

View File

@@ -7,6 +7,8 @@
package pp.battleship.game.client;
import pp.battleship.notification.Music;
/**
* Represents the state of the client when the game is over.
*/
@@ -16,8 +18,14 @@ class GameOverState extends ClientState {
*
* @param logic the client game logic
*/
GameOverState(ClientGameLogic logic) {
GameOverState(ClientGameLogic logic, boolean loser) {
super(logic);
if(loser){
logic.playMusic(Music.LOSE_THEME);
}
else {
logic.playMusic(Music.VICTORY_THEME);
}
}
/**

View File

@@ -45,4 +45,12 @@ default void receivedEvent(SoundEvent event) { /* do nothing */ }
* @param event the received event
*/
default void receivedEvent(ClientStateEvent event) { /* do nothing */ }
/**
* Indicates that the music gets changed
*
* @param event the received Event
*/
default void receivedEvent(MusicEvent event) { /* do nothing */ }
}

View File

@@ -0,0 +1,23 @@
package pp.battleship.notification;
/**
* Enumeration representing different types of music used in the game.
*/
public enum Music {
/**
* menu music
*/
MENU_THEME,
/**
* ingame music
*/
GAME_THEME,
/**
* music for a victory
*/
VICTORY_THEME,
/**
* music for a loss
*/
LOSE_THEME,
}

View File

@@ -0,0 +1,19 @@
package pp.battleship.notification;
/**
* Event when background music changes
*
* @param music the music to be played
*/
public record MusicEvent(Music music) implements GameEvent {
/**
* Notifies the game event listener the music event.
*
* @param listener the game event listener
*/
@Override
public void notifyListener(GameEventListener listener) {
listener.receivedEvent(this);
}
}