mirror of
https://athene2.informatik.unibw-muenchen.de/progproj/gruppen-ht24/Gruppe-02.git
synced 2025-01-19 00:06:16 +01:00
fixed background music overlap
This commit is contained in:
parent
944b6b674e
commit
392a81b2d8
@ -13,14 +13,13 @@ import com.jme3.asset.AssetLoadException;
|
|||||||
import com.jme3.asset.AssetNotFoundException;
|
import com.jme3.asset.AssetNotFoundException;
|
||||||
import com.jme3.audio.AudioData;
|
import com.jme3.audio.AudioData;
|
||||||
import com.jme3.audio.AudioNode;
|
import com.jme3.audio.AudioNode;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Handles the background and secondary music in the game.
|
* Handles the background and secondary music in the game.
|
||||||
* Allows playing, stopping, and toggling between background music and a secondary track.
|
* Allows playing, stopping, and toggling between background music and a secondary track.
|
||||||
*/
|
*/
|
||||||
public class GameMusic extends AbstractAppState {
|
public class GameMusic extends AbstractAppState {
|
||||||
private static final Logger LOGGER = System.getLogger(GameMusic.class.getName());
|
private static final Logger LOGGER = System.getLogger(pp.monopoly.client.GameMusic.class.getName());
|
||||||
private static final Preferences PREFERENCES = getPreferences(GameMusic.class);
|
private static final Preferences PREFERENCES = getPreferences(pp.monopoly.client.GameMusic.class);
|
||||||
private static final String ENABLED_PREF = "enabled"; // NON-NLS
|
private static final String ENABLED_PREF = "enabled"; // NON-NLS
|
||||||
private static final String VOLUME_PREF = "volume"; // NON-NLS
|
private static final String VOLUME_PREF = "volume"; // NON-NLS
|
||||||
|
|
||||||
@ -68,8 +67,9 @@ public class GameMusic extends AbstractAppState {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Plays the main music.
|
* Plays the main music.
|
||||||
|
|
||||||
*/
|
*/
|
||||||
private void playMainMusic() {
|
public void playMainMusic() {
|
||||||
if (!isEnabled()) {
|
if (!isEnabled()) {
|
||||||
return; // Sound is disabled
|
return; // Sound is disabled
|
||||||
}
|
}
|
||||||
@ -92,11 +92,10 @@ public class GameMusic extends AbstractAppState {
|
|||||||
/**
|
/**
|
||||||
* Plays the secondary music and stops the main music.
|
* Plays the secondary music and stops the main music.
|
||||||
*
|
*
|
||||||
* @param app The application instance
|
|
||||||
* @param secondaryMusicFile The file path of the secondary audio file
|
|
||||||
*/
|
*/
|
||||||
private void playSecondaryMusic() {
|
public void playSecondaryMusic() {
|
||||||
if(!isEnabled()) {
|
if (!isEnabled()) {
|
||||||
|
stopAllMusic();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (isSecondaryMusicPlaying) {
|
if (isSecondaryMusicPlaying) {
|
||||||
@ -115,7 +114,7 @@ public class GameMusic extends AbstractAppState {
|
|||||||
/**
|
/**
|
||||||
* Stops the secondary music.
|
* Stops the secondary music.
|
||||||
*/
|
*/
|
||||||
private void stopSecondaryMusic() {
|
public void stopSecondaryMusic() {
|
||||||
if (secondaryMusic != null && isSecondaryMusicPlaying) {
|
if (secondaryMusic != null && isSecondaryMusicPlaying) {
|
||||||
secondaryMusic.stop();
|
secondaryMusic.stop();
|
||||||
isSecondaryMusicPlaying = false;
|
isSecondaryMusicPlaying = false;
|
||||||
@ -127,17 +126,17 @@ public class GameMusic extends AbstractAppState {
|
|||||||
* If the secondary track is playing, it stops and resumes the background music.
|
* If the secondary track is playing, it stops and resumes the background music.
|
||||||
* If the background music is playing, it pauses and plays the secondary track.
|
* If the background music is playing, it pauses and plays the secondary track.
|
||||||
*
|
*
|
||||||
* @param app The application instance
|
|
||||||
* @param secondaryMusicFile The file path of the secondary audio file
|
|
||||||
*/
|
*/
|
||||||
public void toggleMusic() {
|
public void toggleMusic() {
|
||||||
if(!isEnabled()) {
|
if (!isEnabled()) {
|
||||||
|
stopAllMusic();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (isSecondaryMusicPlaying) {
|
if (isSecondaryMusicPlaying) {
|
||||||
stopSecondaryMusic();
|
stopSecondaryMusic();
|
||||||
playMainMusic();
|
playMainMusic();
|
||||||
} else {
|
} else {
|
||||||
|
stopMainMusic();
|
||||||
playSecondaryMusic();
|
playSecondaryMusic();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -153,6 +152,14 @@ public class GameMusic extends AbstractAppState {
|
|||||||
PREFERENCES.putFloat(VOLUME_PREF, vol);
|
PREFERENCES.putFloat(VOLUME_PREF, vol);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Stops all music (both main and secondary).
|
||||||
|
*/
|
||||||
|
public void stopAllMusic() {
|
||||||
|
stopMainMusic();
|
||||||
|
stopSecondaryMusic();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Enables or disables the sound system.
|
* Enables or disables the sound system.
|
||||||
* When disabled, all music stops.
|
* When disabled, all music stops.
|
||||||
@ -161,13 +168,14 @@ public class GameMusic extends AbstractAppState {
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void setEnabled(boolean enabled) {
|
public void setEnabled(boolean enabled) {
|
||||||
if (isEnabled() == enabled) return;
|
if (isEnabled() == enabled) return; // Avoid redundant operations
|
||||||
|
|
||||||
|
PREFERENCES.putBoolean(ENABLED_PREF, enabled);
|
||||||
|
|
||||||
if (enabled) {
|
if (enabled) {
|
||||||
playMainMusic();
|
playMainMusic();
|
||||||
} else {
|
} else {
|
||||||
stopMainMusic();
|
stopAllMusic();
|
||||||
stopSecondaryMusic();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
super.setEnabled(enabled);
|
super.setEnabled(enabled);
|
||||||
|
@ -79,8 +79,9 @@ public class LobbyMenu extends Dialog {
|
|||||||
this.app = app;
|
this.app = app;
|
||||||
|
|
||||||
GameMusic music = app.getStateManager().getState(GameMusic.class);
|
GameMusic music = app.getStateManager().getState(GameMusic.class);
|
||||||
music.toggleMusic();
|
if (music != null && music.isEnabled()) {
|
||||||
|
music.playSecondaryMusic();
|
||||||
|
}
|
||||||
playerInputField = new TextField("Spieler "+(app.getId()+1));
|
playerInputField = new TextField("Spieler "+(app.getId()+1));
|
||||||
// Hintergrundbild laden und hinzufügen
|
// Hintergrundbild laden und hinzufügen
|
||||||
addBackgroundImage();
|
addBackgroundImage();
|
||||||
@ -276,6 +277,12 @@ public class LobbyMenu extends Dialog {
|
|||||||
new SettingsMenu(app).open();
|
new SettingsMenu(app).open();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Updates the menu at regular intervals.
|
||||||
|
* Checks if the dropdown selection has been updated and invokes the selection change handler.
|
||||||
|
*
|
||||||
|
* @param tpf Time per frame, in seconds, since the last update.
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void update(float tpf) {
|
public void update(float tpf) {
|
||||||
if (selectionRef.update()) {
|
if (selectionRef.update()) {
|
||||||
@ -283,10 +290,27 @@ public class LobbyMenu extends Dialog {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Closes the current menu and transitions music playback.
|
||||||
|
* Stops the secondary music (if playing) and resumes the main background music
|
||||||
|
* if music is enabled in the preferences. Ensures smooth transitions in audio.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void close() {
|
||||||
|
GameMusic music = app.getStateManager().getState(GameMusic.class);
|
||||||
|
if (music != null) {
|
||||||
|
music.stopSecondaryMusic();
|
||||||
|
if (music.isEnabled()) {
|
||||||
|
music.playMainMusic();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
super.close();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Updates the selected figure based on the dropdown menu selection.
|
* Updates the selected figure based on the dropdown menu selection.
|
||||||
*
|
*
|
||||||
* @param selected the selected figure
|
* @param selector the selected figure
|
||||||
*/
|
*/
|
||||||
private void onDropdownSelectionChanged(Selector<String> selector) {
|
private void onDropdownSelectionChanged(Selector<String> selector) {
|
||||||
app.getGameLogic().playSound(Sound.BUTTON);
|
app.getGameLogic().playSound(Sound.BUTTON);
|
||||||
|
@ -51,7 +51,17 @@ public class SettingsMenu extends Dialog {
|
|||||||
private final SoundSlider soundSlider;
|
private final SoundSlider soundSlider;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructs the Menu dialog for the Battleship application.
|
* Checkbox for toggling sound effects.
|
||||||
|
*/
|
||||||
|
private final Checkbox soundCheckbox;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checkbox for toggling background music.
|
||||||
|
*/
|
||||||
|
private final Checkbox musicCheckbox;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructs the Menu dialog for the Monopoly application.
|
||||||
*
|
*
|
||||||
* @param app the MonopolyApp instance
|
* @param app the MonopolyApp instance
|
||||||
*/
|
*/
|
||||||
@ -65,11 +75,15 @@ public class SettingsMenu extends Dialog {
|
|||||||
|
|
||||||
addChild(soundSlider);
|
addChild(soundSlider);
|
||||||
|
|
||||||
addChild(new Checkbox("Soundeffekte an / aus", new StateCheckboxModel(app, GameSound.class)));
|
soundCheckbox = new Checkbox("Soundeffekte an / aus", new StateCheckboxModel(app, GameSound.class));
|
||||||
|
addChild(soundCheckbox);
|
||||||
|
|
||||||
addChild(new Label("Hintergrund Musik", new ElementId("label"))); //NON-NLS
|
addChild(new Label("Hintergrund Musik", new ElementId("label"))); //NON-NLS
|
||||||
addChild(new Checkbox("Musik an / aus", new StateCheckboxModel(app, GameMusic.class)));
|
|
||||||
|
musicCheckbox = new Checkbox("Musik an / aus", new StateCheckboxModel(app, GameMusic.class));
|
||||||
|
musicCheckbox.addClickCommands(s -> toggleMusicPreference());
|
||||||
|
addChild(musicCheckbox);
|
||||||
|
|
||||||
addChild(musicSlider);
|
addChild(musicSlider);
|
||||||
|
|
||||||
addChild(new Button("Zurück zum Spiel", new ElementId("button"))).addClickCommands(s -> ifTopDialog(() -> {
|
addChild(new Button("Zurück zum Spiel", new ElementId("button"))).addClickCommands(s -> ifTopDialog(() -> {
|
||||||
@ -84,16 +98,42 @@ public class SettingsMenu extends Dialog {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Updates the state of the load and save buttons based on the game logic.
|
* Toggles the music preference based on the state of the musicCheckbox.
|
||||||
|
* Enables or disables background music and starts playback if enabled.
|
||||||
|
*/
|
||||||
|
private void toggleMusicPreference() {
|
||||||
|
boolean enabled = musicCheckbox.isChecked();
|
||||||
|
GameMusic gameMusic = app.getStateManager().getState(GameMusic.class);
|
||||||
|
if (gameMusic != null) {
|
||||||
|
gameMusic.setEnabled(enabled);
|
||||||
|
if (enabled) {
|
||||||
|
gameMusic.playMainMusic();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Updates the state of the music checkbox to match the current preferences.
|
||||||
|
* This ensures the checkbox reflects the actual enabled/disabled state of the music.
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void update() {
|
public void update() {
|
||||||
|
GameMusic gameMusic = app.getStateManager().getState(GameMusic.class);
|
||||||
|
if (gameMusic != null) {
|
||||||
|
musicCheckbox.setChecked(gameMusic.isEnabled());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Updates UI elements such as sliders and synchronizes the state of the settings menu.
|
||||||
|
*
|
||||||
|
* @param delta the time in seconds since the last update
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void update(float delta) {
|
public void update(float delta) {
|
||||||
musicSlider.update();
|
musicSlider.update();
|
||||||
soundSlider.update();
|
soundSlider.update();
|
||||||
|
update();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
Reference in New Issue
Block a user