mirror of
https://athene2.informatik.unibw-muenchen.de/progproj/gruppen-ht24/Gruppe-02.git
synced 2024-11-24 23:59:44 +01:00
resolve merge conflict
This commit is contained in:
parent
a7ea5773da
commit
6dcfb92dba
@ -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;
|
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;
|
||||||
import java.lang.System.Logger.Level;
|
import java.lang.System.Logger.Level;
|
||||||
import java.util.prefs.Preferences;
|
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;
|
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 {
|
public class GameSound extends AbstractAppState implements GameEventListener {
|
||||||
private static final Logger LOGGER = System.getLogger(GameSound.class.getName());
|
private static final Logger LOGGER = System.getLogger(GameSound.class.getName());
|
||||||
private static final Preferences PREFERENCES = getPreferences(GameSound.class);
|
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.
|
* 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.
|
* 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.
|
* @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) {
|
public void setEnabled(boolean enabled) {
|
||||||
if (isEnabled() == enabled) return;
|
if (isEnabled() == enabled) return;
|
||||||
super.setEnabled(enabled);
|
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);
|
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 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);
|
||||||
this.app = app; // Speichert die Application-Instanz
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Loads a sound from the specified file.
|
* Loads a sound from the specified file.
|
||||||
*
|
*
|
||||||
|
* @param app The application
|
||||||
* @param name The name of the sound file.
|
* @param name The name of the sound file.
|
||||||
* @return The loaded AudioNode.
|
* @return The loaded AudioNode.
|
||||||
*/
|
*/
|
||||||
private AudioNode loadSound(String name) {
|
private AudioNode loadSound(Application app, String name) {
|
||||||
try {
|
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.setLooping(false);
|
||||||
sound.setPositional(false);
|
sound.setPositional(false);
|
||||||
return sound;
|
return sound;
|
||||||
} catch (Exception ex) {
|
}
|
||||||
|
catch (AssetLoadException | AssetNotFoundException ex) {
|
||||||
LOGGER.log(Level.ERROR, ex.getMessage(), ex);
|
LOGGER.log(Level.ERROR, ex.getMessage(), ex);
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Handles sound-related game events to play specific sounds.
|
* Plays the splash sound effect.
|
||||||
*
|
|
||||||
* @param event The sound event received.
|
|
||||||
*/
|
*/
|
||||||
|
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
|
@Override
|
||||||
public void receivedEvent(SoundEvent event) {
|
public void receivedEvent(SoundEvent event) {
|
||||||
if (isEnabled()) {
|
switch (event.sound()) {
|
||||||
AudioNode sound = loadSound(event.getSoundFileName());
|
|
||||||
if (sound != null) {
|
|
||||||
sound.play();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}//heloo
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user