added game sound logic

This commit is contained in:
Johannes Schmelz 2024-11-19 19:27:52 +01:00
parent 3c21165efb
commit cc8c3795b8
2 changed files with 223 additions and 17 deletions

View File

@ -0,0 +1,122 @@
package pp.monopoly.client;
import static pp.util.PreferencesUtils.getPreferences;
import java.lang.System.Logger;
import java.lang.System.Logger.Level;
import java.util.prefs.Preferences;
import com.jme3.app.Application;
import com.jme3.app.state.AbstractAppState;
import com.jme3.app.state.AppStateManager;
import com.jme3.asset.AssetLoadException;
import com.jme3.asset.AssetNotFoundException;
import com.jme3.audio.AudioData;
import com.jme3.audio.AudioNode;
/**
* Handles the background music beeing played. Is able to start and stop the music. Set the Volume of the Audio.
*/
public class GameMusic extends AbstractAppState{
private static final Logger LOGGER = System.getLogger(GameMusic.class.getName());
private static final Preferences PREFERENCES = getPreferences(GameMusic.class);
private static final String ENABLED_PREF = "enabled"; //NON-NLS
private static final String VOLUME_PREF = "volume"; //NON-NLS
private AudioNode 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 app The application
*/
@Override
public void initialize(AppStateManager stateManager, Application app) {
super.initialize(stateManager, app);
music = loadSound(app, "Sound/background.ogg");
setVolume(volumeInPreferences());
music.setLooping(true);
if (isEnabled() && music != null) {
music.play();
}
}
/**
* Loads a sound from the specified file.
*
* @param app The application
* @param name The name of the sound file.
* @return The loaded AudioNode.
*/
private AudioNode loadSound(Application app, String name) {
try {
final AudioNode sound = new AudioNode(app.getAssetManager(), name, AudioData.DataType.Buffer);
sound.setLooping(false);
sound.setPositional(false);
return sound;
}
catch (AssetLoadException | AssetNotFoundException ex) {
LOGGER.log(Level.ERROR, ex.getMessage(), ex);
}
return null;
}
/**
* Sets the enabled state of this AppState.
* Overrides {@link com.jme3.app.state.AbstractAppState#setEnabled(boolean)}
*
* @param enabled {@code true} to enable the AppState, {@code false} to disable it.
*/
@Override
public void setEnabled(boolean enabled) {
if (isEnabled() == enabled) return;
if (music != null) {
if (enabled) {
music.play();
} else {
music.stop();
}
}
super.setEnabled(enabled);
LOGGER.log(Level.INFO, "Sound enabled: {0}", enabled); //NON-NLS
PREFERENCES.putBoolean(ENABLED_PREF, enabled);
}
/**
* Toggles the game sound on or off.
*/
public void toggleSound() {
setEnabled(!isEnabled());
}
/**
* Sets the volume of music
* @param vol the volume to which the music should be set
*/
public void setVolume(float vol){
music.setVolume(vol);
PREFERENCES.putFloat(VOLUME_PREF, vol);
}
}

View File

@ -31,9 +31,17 @@ public class GameSound extends AbstractAppState implements GameEventListener {
private static final Preferences PREFERENCES = getPreferences(GameSound.class); private static final Preferences PREFERENCES = getPreferences(GameSound.class);
private static final String ENABLED_PREF = "enabled"; //NON-NLS private static final String ENABLED_PREF = "enabled"; //NON-NLS
private AudioNode splashSound; private AudioNode passStartSound;
private AudioNode shipDestroyedSound; private AudioNode eventCardSound;
private AudioNode explosionSound; private AudioNode gulagSound;
private AudioNode diceRollSound;
private AudioNode moneyCollectSound;
private AudioNode moneyLostSound;
private AudioNode tradeAcceptedSound;
private AudioNode tradeRejectedSound;
private AudioNode winnerSound;
private AudioNode looserSound;
private AudioNode buttonSound;
/** /**
* Checks if sound is enabled in the preferences. * Checks if sound is enabled in the preferences.
@ -75,6 +83,17 @@ public class GameSound extends AbstractAppState implements GameEventListener {
@Override @Override
public void initialize(AppStateManager stateManager, Application app) { public void initialize(AppStateManager stateManager, Application app) {
super.initialize(stateManager, app); super.initialize(stateManager, app);
passStartSound = loadSound(app, "Sound/Effects/passStart.ogg");
eventCardSound = loadSound(app, "Sound/Effects/eventCard.ogg");
gulagSound = loadSound(app, "Sound/Effects/gulag.ogg");
diceRollSound = loadSound(app, "Sound/Effects/diceRoll.ogg");
moneyCollectSound = loadSound(app, "Sound/Effects/moneyCollect.ogg");
moneyLostSound = loadSound(app, "Sound/Effects/moneyLost.ogg");
tradeAcceptedSound = loadSound(app, "Sound/Effects/tradeAccepted.ogg");
tradeRejectedSound = loadSound(app, "Sound/Effects/tradeRejected.ogg");
winnerSound = loadSound(app, "Sound/Effects/winner.ogg");
looserSound = loadSound(app, "Sound/Effects/looser.ogg");
buttonSound = loadSound(app, "Sound/Effects/button.ogg");
} }
/** /**
@ -98,32 +117,97 @@ public class GameSound extends AbstractAppState implements GameEventListener {
} }
/** /**
* Plays the splash sound effect. * Plays the passStart sound effect.
*/ */
public void splash() { public void passStart() {
if (isEnabled() && splashSound != null) if (isEnabled() && passStartSound != null)
splashSound.playInstance(); passStartSound.playInstance();
} }
/** /**
* Plays the explosion sound effect. * Plays the eventCard sound effect.
*/ */
public void explosion() { public void eventCard() {
if (isEnabled() && explosionSound != null) if (isEnabled() && eventCardSound != null)
explosionSound.playInstance(); eventCardSound.playInstance();
} }
/** /**
* Plays sound effect when a ship has been destroyed. * Plays the gulag sound effect.
*/ */
public void shipDestroyed() { public void gulag() {
if (isEnabled() && shipDestroyedSound != null) if (isEnabled() && gulagSound != null)
shipDestroyedSound.playInstance(); gulagSound.playInstance();
}
/**
* Plays the diceRoll sound effect.
*/
public void diceRoll() {
if (isEnabled() && diceRollSound != null)
diceRollSound.playInstance();
}
/**
* Plays the moneyCollect sound effect.
*/
public void moneyCollect() {
if (isEnabled() && moneyCollectSound != null)
moneyCollectSound.playInstance();
}
/**
* Plays the moneyLost sound effect.
*/
public void moneyLost() {
if (isEnabled() && moneyLostSound != null)
moneyLostSound.playInstance();
}
/**
* Plays the tradeAccepted sound effect.
*/
public void tradeAccepted() {
if (isEnabled() && tradeAcceptedSound != null)
tradeAcceptedSound.playInstance();
}
/**
* Plays the tradeRejected sound effect.
*/
public void tradeRejected() {
if (isEnabled() && tradeRejectedSound != null)
tradeRejectedSound.playInstance();
}
/**
* Plays the winner sound effect.
*/
public void winner() {
if (isEnabled() && winnerSound != null)
winnerSound.playInstance();
}
/**
* Plays the looser sound effect.
*/
public void looser() {
if (isEnabled() && looserSound != null)
looserSound.playInstance();
}
/**
* Plays the button sound effect.
*/
public void button() {
if (isEnabled() && buttonSound != null)
buttonSound.playInstance();
} }
@Override @Override
public void receivedEvent(SoundEvent event) { public void receivedEvent(SoundEvent event) {
switch (event.sound()) { switch (event.sound()) {
case PASS_START -> passStart();
case EVENT_CARD -> eventCard();
case GULAG -> eventCard();
case DICE_ROLL -> eventCard();
case MONEY_COLLECTED -> eventCard();
case MONEY_LOST -> eventCard();
case TRADE_ACCEPTED -> eventCard();
case TRADE_REJECTED -> eventCard();
case WINNER -> eventCard();
case LOSER -> eventCard();
case BUTTON -> eventCard();
} }
} }
} }