From 6dcfb92dba09d9c99b94e2cfa09c74e187e1b964 Mon Sep 17 00:00:00 2001 From: Johannes Schmelz Date: Mon, 18 Nov 2024 05:07:41 +0100 Subject: [PATCH] resolve merge conflict --- .../java/pp/monopoly/client/GameSound.java | 83 +++++++++++++------ 1 file changed, 56 insertions(+), 27 deletions(-) diff --git a/Projekte/monopoly/client/src/main/java/pp/monopoly/client/GameSound.java b/Projekte/monopoly/client/src/main/java/pp/monopoly/client/GameSound.java index df498f5..ec54da2 100644 --- a/Projekte/monopoly/client/src/main/java/pp/monopoly/client/GameSound.java +++ b/Projekte/monopoly/client/src/main/java/pp/monopoly/client/GameSound.java @@ -1,28 +1,39 @@ +//////////////////////////////////////// +// Programming project code +// UniBw M, 2022, 2023, 2024 +// www.unibw.de/inf2 +// (c) Mark Minas (mark.minas@unibw.de) +//////////////////////////////////////// + package pp.monopoly.client; +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; +import pp.monopoly.notification.GameEventListener; +import pp.monopoly.notification.SoundEvent; + 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.audio.AudioData; -import com.jme3.audio.AudioNode; - -import pp.monopoly.notification.GameEventListener; -import pp.monopoly.notification.SoundEvent; import static pp.util.PreferencesUtils.getPreferences; /** - * An application state that plays sounds based on game events. + * An application state that plays sounds. */ public class GameSound extends AbstractAppState implements GameEventListener { private static final Logger LOGGER = System.getLogger(GameSound.class.getName()); private static final Preferences PREFERENCES = getPreferences(GameSound.class); - private static final String ENABLED_PREF = "enabled"; + private static final String ENABLED_PREF = "enabled"; //NON-NLS - private Application app; // Feld zum Speichern der Application-Instanz + private AudioNode splashSound; + private AudioNode shipDestroyedSound; + private AudioNode explosionSound; /** * Checks if sound is enabled in the preferences. @@ -42,6 +53,7 @@ public class GameSound extends AbstractAppState implements GameEventListener { /** * 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. */ @@ -49,52 +61,69 @@ public class GameSound extends AbstractAppState implements GameEventListener { public void setEnabled(boolean enabled) { if (isEnabled() == enabled) return; super.setEnabled(enabled); - LOGGER.log(Level.INFO, "Sound enabled: {0}", enabled); + LOGGER.log(Level.INFO, "Sound enabled: {0}", enabled); //NON-NLS PREFERENCES.putBoolean(ENABLED_PREF, enabled); } /** - * Initializes the sound effects for the game and stores the application reference. + * Initializes the sound effects for the game. + * Overrides {@link AbstractAppState#initialize(AppStateManager, Application)} * * @param stateManager The state manager - * @param app The application instance + * @param app The application */ @Override public void initialize(AppStateManager stateManager, Application app) { super.initialize(stateManager, app); - this.app = app; // Speichert die Application-Instanz } /** * 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(String name) { + private AudioNode loadSound(Application app, String name) { try { - 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.setPositional(false); return sound; - } catch (Exception ex) { + } + catch (AssetLoadException | AssetNotFoundException ex) { LOGGER.log(Level.ERROR, ex.getMessage(), ex); } return null; } /** - * Handles sound-related game events to play specific sounds. - * - * @param event The sound event received. + * Plays the splash sound effect. */ + public void splash() { + if (isEnabled() && splashSound != null) + splashSound.playInstance(); + } + + /** + * Plays the explosion sound effect. + */ + public void explosion() { + if (isEnabled() && explosionSound != null) + explosionSound.playInstance(); + } + + /** + * Plays sound effect when a ship has been destroyed. + */ + public void shipDestroyed() { + if (isEnabled() && shipDestroyedSound != null) + shipDestroyedSound.playInstance(); + } + @Override public void receivedEvent(SoundEvent event) { - if (isEnabled()) { - AudioNode sound = loadSound(event.getSoundFileName()); - if (sound != null) { - sound.play(); - } + switch (event.sound()) { } } -}//heloo +}