mirror of
https://athene2.informatik.unibw-muenchen.de/progproj/gruppen-ht24/Gruppe-02.git
synced 2025-01-18 22:56:15 +01:00
choose your charakter
This commit is contained in:
parent
a78261594d
commit
88db193b5e
@ -15,49 +15,34 @@ import com.jme3.audio.AudioData;
|
|||||||
import com.jme3.audio.AudioNode;
|
import com.jme3.audio.AudioNode;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Handles the background music being played. Is able to start and stop the music. Set the Volume of the Audio.
|
* Handles the background and secondary music in the game.
|
||||||
|
* 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());
|
||||||
private static final Preferences PREFERENCES = getPreferences(GameMusic.class);
|
private static final Preferences PREFERENCES = getPreferences(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
|
||||||
|
|
||||||
private AudioNode music;
|
private AudioNode mainMusic;
|
||||||
|
private AudioNode secondaryMusic;
|
||||||
|
private boolean isMainMusicPlaying = false;
|
||||||
|
private boolean isSecondaryMusicPlaying = false;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Checks if sound is enabled in the preferences.
|
* Initializes the GameMusic app state and loads the background music.
|
||||||
*
|
|
||||||
* @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
|
* @param app The application instance
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void initialize(AppStateManager stateManager, Application app) {
|
public void initialize(AppStateManager stateManager, Application app) {
|
||||||
super.initialize(stateManager, app);
|
super.initialize(stateManager, app);
|
||||||
music = loadSound(app, "Sound/background.ogg");
|
mainMusic = loadSound(app, "Sound/background.ogg");
|
||||||
|
secondaryMusic = loadSound(app, "Sound/ChooseYourCharakter.ogg");
|
||||||
setVolume(volumeInPreferences());
|
setVolume(volumeInPreferences());
|
||||||
music.setLooping(true);
|
if (isEnabled()) {
|
||||||
if (isEnabled() && music != null) {
|
playMainMusic();
|
||||||
music.play();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -71,7 +56,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(false);
|
sound.setLooping(true);
|
||||||
sound.setPositional(false);
|
sound.setPositional(false);
|
||||||
return sound;
|
return sound;
|
||||||
}
|
}
|
||||||
@ -82,41 +67,120 @@ public class GameMusic extends AbstractAppState{
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the enabled state of this AppState.
|
* Plays the main music.
|
||||||
* 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 enabled {@code true} to enable the AppState, {@code false} to disable it.
|
* @param app The application instance
|
||||||
|
* @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);
|
||||||
LOGGER.log(Level.INFO, "Sound enabled: {0}", enabled); //NON-NLS
|
LOGGER.log(Level.INFO, "Sound enabled: {0}", enabled); // NON-NLS
|
||||||
PREFERENCES.putBoolean(ENABLED_PREF, enabled);
|
PREFERENCES.putBoolean(ENABLED_PREF, enabled);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Toggles the game sound on or off.
|
* Retrieves the current sound volume preference.
|
||||||
|
*
|
||||||
|
* @return The volume level (0.0f to 1.0f)
|
||||||
*/
|
*/
|
||||||
public void toggleSound() {
|
public static float volumeInPreferences() {
|
||||||
setEnabled(!isEnabled());
|
return PREFERENCES.getFloat(VOLUME_PREF, 0.5f);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the volume of music
|
* Checks if sound is enabled in the preferences.
|
||||||
* @param vol the volume to which the music should be set
|
*
|
||||||
|
* @return {@code true} if sound is enabled, {@code false} otherwise
|
||||||
*/
|
*/
|
||||||
public void setVolume(float vol){
|
public static boolean enabledInPreferences() {
|
||||||
music.setVolume(vol);
|
return PREFERENCES.getBoolean(ENABLED_PREF, true);
|
||||||
PREFERENCES.putFloat(VOLUME_PREF, vol);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,5 @@
|
|||||||
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;
|
||||||
@ -24,6 +22,7 @@ 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;
|
||||||
@ -79,6 +78,9 @@ 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();
|
||||||
@ -194,6 +196,7 @@ 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));
|
||||||
|
Binary file not shown.
Loading…
Reference in New Issue
Block a user