added the logic to play diffrent Background music during diffrent states
added the a Enum for the Music and the MusicEvent added a song for the game victory adjusted the backgroundmusic class to handle the logic of the new song added the Backgroundmusic to the ClientLogic as a EventListener.
This commit is contained in:
parent
961242bb20
commit
b18705f064
@@ -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.BATTLE_THEME);
|
||||
this.myTurn = myTurn;
|
||||
}
|
||||
|
||||
@@ -62,7 +64,7 @@ public void receivedEffect(EffectMessage msg) {
|
||||
}
|
||||
if (msg.isGameOver()) {
|
||||
msg.getRemainingOpponentShips().forEach(logic.getOpponentMap()::add);
|
||||
logic.setState(new GameOverState(logic));
|
||||
logic.setState(new GameOverState(logic, msg.isGameLost()));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -258,6 +260,15 @@ public void playSound(Sound sound) {
|
||||
notifyListeners(new SoundEvent(sound));
|
||||
}
|
||||
|
||||
/**
|
||||
* Emits an event to play the specified music
|
||||
*
|
||||
* @param music the music to be played
|
||||
*/
|
||||
public void playMusic(Music music) {
|
||||
notifyListeners(new MusicEvent(music));
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads a map from the specified 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,13 @@ class GameOverState extends ClientState {
|
||||
*
|
||||
* @param logic the client game logic
|
||||
*/
|
||||
GameOverState(ClientGameLogic logic) {
|
||||
GameOverState(ClientGameLogic logic, boolean lost) {
|
||||
super(logic);
|
||||
if (lost){
|
||||
logic.playMusic(Music.GAME_OVER_THEME_L);
|
||||
} else {
|
||||
logic.playMusic(Music.GAME_OVER_THEME_V);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -45,4 +45,11 @@ default void receivedEvent(SoundEvent event) { /* do nothing */ }
|
||||
* @param event the received event
|
||||
*/
|
||||
default void receivedEvent(ClientStateEvent event) { /* do nothing */ }
|
||||
|
||||
/**
|
||||
* Indicates that the music should be changed
|
||||
*
|
||||
* @param event the received Event
|
||||
*/
|
||||
default void receivedEvent(MusicEvent event) { /* do nothing */ };
|
||||
}
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
package pp.battleship.notification;
|
||||
|
||||
/**
|
||||
* Enumeration representing different types of sounds used in the game.
|
||||
*/
|
||||
public enum Music {
|
||||
/**
|
||||
* Menu music
|
||||
*/
|
||||
MENU_THEME,
|
||||
/**
|
||||
* Battle music
|
||||
*/
|
||||
BATTLE_THEME,
|
||||
/**
|
||||
* Game over music for a loss
|
||||
*/
|
||||
GAME_OVER_THEME_L,
|
||||
/**
|
||||
* Game over music for a victory
|
||||
*/
|
||||
GAME_OVER_THEME_V,
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package pp.battleship.notification;
|
||||
|
||||
/**
|
||||
* Event when the background music is to be changed
|
||||
*
|
||||
* @param music the music to be played
|
||||
*/
|
||||
public record MusicEvent(Music music) implements GameEvent {
|
||||
|
||||
/**
|
||||
* Notifies the game event listener of this event.
|
||||
*
|
||||
* @param listener the game event listener
|
||||
*/
|
||||
@Override
|
||||
public void notifyListener(GameEventListener listener) {
|
||||
listener.receivedEvent(this);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user