added background music to the game

- added a class BackgroundMusic: is an AbstractAppState and GameEventListener that handles the backgroundmusic
- attached the BackgroundMusic to the stateManager  in the BattleshipApp
- added to the Menu a CheckBox and Slider to manipulate the volume of the backgroundmusic
- added four different music files (for different states of the game)
- edited the WaitState and BattleState to play different music files when chaing to that state
- added to ClientGameLogic a new method playMusic(Music) to play the right music (depends on the current state)
- added a new method receivedEvent(MusicEvent) to handle the music events
- added a new enum Music, that represents different types of music
- added a new record MusicEvent(Music), that decides which music shall play
This commit is contained in:
Daniel Grigencha
2024-10-09 02:14:46 +02:00
parent ec80dd40ce
commit a44cbf2a72
17 changed files with 421 additions and 35 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;
@@ -62,6 +63,10 @@ public void receivedEffect(EffectMessage msg) {
if (msg.isGameOver()) {
msg.getRemainingOpponentShips().forEach(logic.getOpponentMap()::add);
logic.setState(new GameOverState(logic));
if (msg.isOwnShot())
logic.playMusic(Music.VICTORY_MUSIC);
else
logic.playMusic(Music.DEFEAT_MUSIC);
}
}

View File

@@ -15,13 +15,7 @@
import pp.battleship.model.IntPoint;
import pp.battleship.model.ShipMap;
import pp.battleship.model.dto.ShipMapDTO;
import pp.battleship.notification.ClientStateEvent;
import pp.battleship.notification.GameEvent;
import pp.battleship.notification.GameEventBroker;
import pp.battleship.notification.GameEventListener;
import pp.battleship.notification.InfoTextEvent;
import pp.battleship.notification.Sound;
import pp.battleship.notification.SoundEvent;
import pp.battleship.notification.*;
import java.io.File;
import java.io.IOException;
@@ -258,6 +252,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.
*

View File

@@ -10,7 +10,6 @@
import pp.battleship.message.client.MapMessage;
import pp.battleship.model.Battleship;
import pp.battleship.model.IntPoint;
import pp.battleship.model.Rotation;
import pp.battleship.model.ShipMap;
import pp.battleship.model.dto.ShipMapDTO;
@@ -113,8 +112,7 @@ private void placeShip(IntPoint cursor) {
harbor().remove(selectedInHarbor);
preview = null;
selectedInHarbor = null;
}
else {
} else {
preview.setStatus(INVALID_PREVIEW);
ownMap().add(preview);
}
@@ -137,8 +135,7 @@ public void clickHarbor(IntPoint pos) {
harbor().add(selectedInHarbor);
preview = null;
selectedInHarbor = null;
}
else if (shipAtCursor != null) {
} else if (shipAtCursor != null) {
selectedInHarbor = shipAtCursor;
selectedInHarbor.setStatus(VALID_PREVIEW);
harbor().remove(selectedInHarbor);

View File

@@ -9,6 +9,7 @@
import pp.battleship.message.server.GameDetails;
import pp.battleship.message.server.StartBattleMessage;
import pp.battleship.notification.Music;
import java.lang.System.Logger.Level;
@@ -38,6 +39,7 @@ public void receivedStartBattle(StartBattleMessage msg) {
ClientGameLogic.LOGGER.log(Level.INFO, "start battle, {0} turn", msg.isMyTurn() ? "my" : "other's"); //NON-NLS
logic.setInfoText(msg.getInfoTextKey());
logic.setState(new BattleState(logic, msg.isMyTurn()));
logic.playMusic(Music.GAME_MUSIC);
}
/**

View File

@@ -39,6 +39,13 @@ default void receivedEvent(InfoTextEvent event) { /* do nothing */ }
*/
default void receivedEvent(SoundEvent event) { /* do nothing */ }
/**
* Indicates that music shall be played.
*
* @param event the received event
*/
default void receivedEvent(MusicEvent event) { /* do nothing */ }
/**
* Indicates that the client's state has changed.
*

View File

@@ -0,0 +1,23 @@
package pp.battleship.notification;
/**
* Enumeration representing different types of music used in the game.
*/
public enum Music {
/**
* Music for the game.
*/
GAME_MUSIC,
/**
* Music for the menu.
*/
MENU_MUSIC,
/**
* Music for victory.
*/
VICTORY_MUSIC,
/**
* Music for defeat.
*/
DEFEAT_MUSIC
}

View File

@@ -0,0 +1,20 @@
package pp.battleship.notification;
/**
* Event when music is played in the game.
*
* @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);
}
}

View File

@@ -31,6 +31,7 @@ menu.return-to-game=Return to game
menu.sound-enabled=Sound switched on
menu.map.load=Load map from file...
menu.map.save=Save map in file...
menu.music-toggle=Music on/off
label.file=File:
label.connecting=Connecting...
dialog.error=Error

View File

@@ -31,6 +31,7 @@ menu.return-to-game=Zur
menu.sound-enabled=Sound eingeschaltet
menu.map.load=Karte von Datei laden...
menu.map.save=Karte in Datei speichern...
menu.music-toggle=Musik an/aus
label.file=Datei:
label.connecting=Verbindung wird aufgebaut...
dialog.error=Fehler