Compare commits

..

No commits in common. "e65c2661f78da738a417277d42e96e5c11b62472" and "38824895bb9e78580c48b3e157e7ffa2e05530d6" have entirely different histories.

3 changed files with 52 additions and 119 deletions

View File

@ -15,8 +15,7 @@ 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 music being played. Is able to start and stop the music. Set the Volume of the Audio.
* 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(GameMusic.class.getName());
@ -24,25 +23,41 @@ public class GameMusic extends AbstractAppState {
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
private AudioNode mainMusic; private AudioNode music;
private AudioNode secondaryMusic;
private boolean isMainMusicPlaying = false;
private boolean isSecondaryMusicPlaying = false;
/** /**
* Initializes the GameMusic app state and loads the background music. * Checks if sound is enabled in the preferences.
*
* @return {@code true} if sound is enabled, {@code false} otherwise.
*/
public static boolean enabledInPreferences() {
return PREFERENCES.getBoolean(ENABLED_PREF, true);
}
/**
* Checks if sound is enabled in the preferences.
*
* @return float to which the volume is set
*/
public static float volumeInPreferences() {
return PREFERENCES.getFloat(VOLUME_PREF, 0.5f);
}
/**
* Initializes the sound effects for the game.
* Overrides {@link AbstractAppState#initialize(AppStateManager, Application)}
* *
* @param stateManager The state manager * @param stateManager The state manager
* @param app The application instance * @param app The application
*/ */
@Override @Override
public void initialize(AppStateManager stateManager, Application app) { public void initialize(AppStateManager stateManager, Application app) {
super.initialize(stateManager, app); super.initialize(stateManager, app);
mainMusic = loadSound(app, "Sound/background.ogg"); music = loadSound(app, "Sound/background.ogg");
secondaryMusic = loadSound(app, "Sound/ChooseYourCharakter.ogg");
setVolume(volumeInPreferences()); setVolume(volumeInPreferences());
if (isEnabled()) { music.setLooping(true);
playMainMusic(); if (isEnabled() && music != null) {
music.play();
} }
} }
@ -56,7 +71,7 @@ public class GameMusic extends AbstractAppState {
private AudioNode loadSound(Application app, String name) { private AudioNode loadSound(Application app, String name) {
try { try {
final AudioNode sound = new AudioNode(app.getAssetManager(), name, AudioData.DataType.Buffer); final AudioNode sound = new AudioNode(app.getAssetManager(), name, AudioData.DataType.Buffer);
sound.setLooping(true); sound.setLooping(false);
sound.setPositional(false); sound.setPositional(false);
return sound; return sound;
} }
@ -67,98 +82,21 @@ public class GameMusic extends AbstractAppState {
} }
/** /**
* Plays the main music. * Sets the enabled state of this AppState.
*/ * Overrides {@link com.jme3.app.state.AbstractAppState#setEnabled(boolean)}
public void playMainMusic() {
if (mainMusic != null && !isMainMusicPlaying) {
mainMusic.play();
isMainMusicPlaying = true;
}
}
/**
* Stops the main music.
*/
public void stopMainMusic() {
if (mainMusic != null && isMainMusicPlaying) {
mainMusic.stop();
isMainMusicPlaying = false;
}
}
/**
* Plays the secondary music and stops the main music.
* *
* @param app The application instance * @param enabled {@code true} to enable the AppState, {@code false} to disable it.
* @param secondaryMusicFile The file path of the secondary audio file
*/
public void playSecondaryMusic() {
if (isSecondaryMusicPlaying) {
return; // Secondary music is already playing
}
stopMainMusic();
if (secondaryMusic != null) {
secondaryMusic.setVolume(volumeInPreferences());
secondaryMusic.play();
isSecondaryMusicPlaying = true;
}
}
/**
* Stops the secondary music.
*/
public void stopSecondaryMusic() {
if (secondaryMusic != null && isSecondaryMusicPlaying) {
secondaryMusic.stop();
isSecondaryMusicPlaying = false;
}
}
/**
* Toggles between the background music and the secondary track.
* 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.
*
* @param app The application instance
* @param secondaryMusicFile The file path of the secondary audio file
*/
public void toggleMusic() {
if (isSecondaryMusicPlaying) {
stopSecondaryMusic();
playMainMusic();
} else {
playSecondaryMusic();
}
}
/**
* Sets the audio volume for both the main and secondary tracks.
*
* @param vol The volume level (0.0f to 1.0f)
*/
public void setVolume(float vol) {
if (mainMusic != null) mainMusic.setVolume(vol);
if (secondaryMusic != null) secondaryMusic.setVolume(vol);
PREFERENCES.putFloat(VOLUME_PREF, vol);
}
/**
* Enables or disables the sound system.
* When disabled, all music stops.
*
* @param enabled {@code true} to enable, {@code false} to disable
*/ */
@Override @Override
public void setEnabled(boolean enabled) { public void setEnabled(boolean enabled) {
if (isEnabled() == enabled) return; if (isEnabled() == enabled) return;
if (music != null) {
if (enabled) { if (enabled) {
playMainMusic(); music.play();
} else { } else {
stopMainMusic(); music.stop();
stopSecondaryMusic(); }
} }
super.setEnabled(enabled); super.setEnabled(enabled);
@ -167,20 +105,18 @@ public class GameMusic extends AbstractAppState {
} }
/** /**
* Retrieves the current sound volume preference. * Toggles the game sound on or off.
*
* @return The volume level (0.0f to 1.0f)
*/ */
public static float volumeInPreferences() { public void toggleSound() {
return PREFERENCES.getFloat(VOLUME_PREF, 0.5f); setEnabled(!isEnabled());
} }
/** /**
* Checks if sound is enabled in the preferences. * Sets the volume of music
* * @param vol the volume to which the music should be set
* @return {@code true} if sound is enabled, {@code false} otherwise
*/ */
public static boolean enabledInPreferences() { public void setVolume(float vol){
return PREFERENCES.getBoolean(ENABLED_PREF, true); music.setVolume(vol);
PREFERENCES.putFloat(VOLUME_PREF, vol);
} }
} }

View File

@ -1,5 +1,7 @@
package pp.monopoly.client.gui; package pp.monopoly.client.gui;
import com.jme3.app.Application;
import com.jme3.app.state.BaseAppState;
import com.jme3.material.Material; import com.jme3.material.Material;
import com.jme3.math.ColorRGBA; import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f; import com.jme3.math.Vector3f;
@ -22,7 +24,6 @@ import com.simsilica.lemur.core.VersionedReference;
import com.simsilica.lemur.style.ElementId; import com.simsilica.lemur.style.ElementId;
import pp.dialog.Dialog; import pp.dialog.Dialog;
import pp.monopoly.client.GameMusic;
import pp.monopoly.client.MonopolyApp; import pp.monopoly.client.MonopolyApp;
import pp.monopoly.game.server.Player; import pp.monopoly.game.server.Player;
import pp.monopoly.message.client.PlayerReady; import pp.monopoly.message.client.PlayerReady;
@ -78,9 +79,6 @@ public class LobbyMenu extends Dialog {
super(app.getDialogManager()); super(app.getDialogManager());
this.app = app; this.app = app;
GameMusic music = app.getStateManager().getState(GameMusic.class);
music.toggleMusic();
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();
@ -196,7 +194,6 @@ public class LobbyMenu extends Dialog {
readyButton.setFontSize(18); // Adjust font size readyButton.setFontSize(18); // Adjust font size
readyButton.setBackground(new QuadBackgroundComponent(ColorRGBA.Green)); // Add color to match the style readyButton.setBackground(new QuadBackgroundComponent(ColorRGBA.Green)); // Add color to match the style
readyButton.addClickCommands(s -> ifTopDialog(() -> { readyButton.addClickCommands(s -> ifTopDialog(() -> {
music.toggleMusic();
toggleReady(); toggleReady();
app.getGameLogic().playSound(Sound.BUTTON); app.getGameLogic().playSound(Sound.BUTTON);
readyButton.setBackground(new QuadBackgroundComponent(ColorRGBA.DarkGray)); readyButton.setBackground(new QuadBackgroundComponent(ColorRGBA.DarkGray));