mirror of
https://athene2.informatik.unibw-muenchen.de/progproj/gruppen-ht24/Gruppe-02.git
synced 2024-11-28 22:29:43 +01:00
Update 2 files
- /Projekte/battleship/client/src/main/java/pp/battleship/client/BattleshipApp.java - /Projekte/battleship/client/src/main/java/pp/battleship/client/GameSound.java
This commit is contained in:
parent
430bac86bd
commit
cd50358181
@ -23,6 +23,7 @@ import com.simsilica.lemur.style.BaseStyles;
|
|||||||
import pp.battleship.client.gui.BattleAppState;
|
import pp.battleship.client.gui.BattleAppState;
|
||||||
import pp.battleship.client.gui.EditorAppState;
|
import pp.battleship.client.gui.EditorAppState;
|
||||||
import pp.battleship.client.gui.SeaAppState;
|
import pp.battleship.client.gui.SeaAppState;
|
||||||
|
import pp.battleship.client.GameMusic;
|
||||||
import pp.battleship.game.client.BattleshipClient;
|
import pp.battleship.game.client.BattleshipClient;
|
||||||
import pp.battleship.game.client.ClientGameLogic;
|
import pp.battleship.game.client.ClientGameLogic;
|
||||||
import pp.battleship.game.client.ServerConnection;
|
import pp.battleship.game.client.ServerConnection;
|
||||||
@ -267,6 +268,8 @@ public class BattleshipApp extends SimpleApplication implements BattleshipClient
|
|||||||
stateManager.detach(stateManager.getState(DebugKeysAppState.class));
|
stateManager.detach(stateManager.getState(DebugKeysAppState.class));
|
||||||
|
|
||||||
attachGameSound();
|
attachGameSound();
|
||||||
|
attachGameMusic();
|
||||||
|
|
||||||
stateManager.attachAll(new EditorAppState(), new BattleAppState(), new SeaAppState());
|
stateManager.attachAll(new EditorAppState(), new BattleAppState(), new SeaAppState());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -280,6 +283,15 @@ public class BattleshipApp extends SimpleApplication implements BattleshipClient
|
|||||||
stateManager.attach(gameSound);
|
stateManager.attach(gameSound);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Attaches the music state and sets its initial enabled state.
|
||||||
|
*/
|
||||||
|
private void attachGameMusic() {
|
||||||
|
final GameMusic gameMusic = new GameMusic();
|
||||||
|
gameMusic.setEnabled(GameMusic.enabledInPreferences());
|
||||||
|
stateManager.attach(gameMusic);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Updates the application state every frame.
|
* Updates the application state every frame.
|
||||||
* This method is called once per frame during the game loop.
|
* This method is called once per frame during the game loop.
|
||||||
@ -427,3 +439,4 @@ public class BattleshipApp extends SimpleApplication implements BattleshipClient
|
|||||||
.open();
|
.open();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,12 +1,6 @@
|
|||||||
////////////////////////////////////////
|
|
||||||
// Programming project code
|
|
||||||
// UniBw M, 2022, 2023, 2024
|
|
||||||
// www.unibw.de/inf2
|
|
||||||
// (c) Mark Minas (mark.minas@unibw.de)
|
|
||||||
////////////////////////////////////////
|
|
||||||
|
|
||||||
package pp.battleship.client;
|
package pp.battleship.client;
|
||||||
|
|
||||||
|
|
||||||
import com.jme3.app.Application;
|
import com.jme3.app.Application;
|
||||||
import com.jme3.app.state.AbstractAppState;
|
import com.jme3.app.state.AbstractAppState;
|
||||||
import com.jme3.app.state.AppStateManager;
|
import com.jme3.app.state.AppStateManager;
|
||||||
@ -14,8 +8,7 @@ import com.jme3.asset.AssetLoadException;
|
|||||||
import com.jme3.asset.AssetNotFoundException;
|
import com.jme3.asset.AssetNotFoundException;
|
||||||
import com.jme3.audio.AudioData;
|
import com.jme3.audio.AudioData;
|
||||||
import com.jme3.audio.AudioNode;
|
import com.jme3.audio.AudioNode;
|
||||||
import pp.battleship.notification.GameEventListener;
|
|
||||||
import pp.battleship.notification.SoundEvent;
|
|
||||||
|
|
||||||
import java.lang.System.Logger;
|
import java.lang.System.Logger;
|
||||||
import java.lang.System.Logger.Level;
|
import java.lang.System.Logger.Level;
|
||||||
@ -24,16 +17,17 @@ import java.util.prefs.Preferences;
|
|||||||
import static pp.util.PreferencesUtils.getPreferences;
|
import static pp.util.PreferencesUtils.getPreferences;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* An application state that plays sounds.
|
* An application state that plays music.
|
||||||
*/
|
*/
|
||||||
public class GameSound extends AbstractAppState implements GameEventListener {
|
public class GameMusic extends AbstractAppState {
|
||||||
private static final Logger LOGGER = System.getLogger(GameSound.class.getName());
|
private static final Logger LOGGER = System.getLogger(GameMusic.class.getName());
|
||||||
private static final Preferences PREFERENCES = getPreferences(GameSound.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 AudioNode splashSound;
|
private AudioNode battleMusic;
|
||||||
private AudioNode shipDestroyedSound;
|
private AudioNode menuMusicModern;
|
||||||
private AudioNode explosionSound;
|
private AudioNode menuMusicTraditional;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Checks if sound is enabled in the preferences.
|
* Checks if sound is enabled in the preferences.
|
||||||
@ -43,6 +37,17 @@ public class GameSound extends AbstractAppState implements GameEventListener {
|
|||||||
public static boolean enabledInPreferences() {
|
public static boolean enabledInPreferences() {
|
||||||
return PREFERENCES.getBoolean(ENABLED_PREF, true);
|
return PREFERENCES.getBoolean(ENABLED_PREF, true);
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
|
* Checks enabled volume in preferences.
|
||||||
|
*
|
||||||
|
* @return {@code float} if a volumePreference is set, the volume is set to the Value in PREFERENCES,
|
||||||
|
* {@code 0.25f} if no volumePreference is set in PREFERENCES, the Volume is set to a default of 0.25f
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
public static float volumePreference() {
|
||||||
|
return PREFERENCES.getFloat(VOLUME_PREF, 0.25f);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Toggles the game sound on or off.
|
* Toggles the game sound on or off.
|
||||||
@ -60,13 +65,19 @@ public class GameSound extends AbstractAppState implements GameEventListener {
|
|||||||
@Override
|
@Override
|
||||||
public void setEnabled(boolean enabled) {
|
public void setEnabled(boolean enabled) {
|
||||||
if (isEnabled() == enabled) return;
|
if (isEnabled() == enabled) return;
|
||||||
|
else if (!isEnabled() && enabled) {
|
||||||
|
if (menuMusicModern != null) menuMusicModern.play();
|
||||||
|
} else if (isEnabled() && !enabled) {
|
||||||
|
if (menuMusicModern != null) menuMusicModern.stop();
|
||||||
|
}
|
||||||
|
|
||||||
super.setEnabled(enabled);
|
super.setEnabled(enabled);
|
||||||
LOGGER.log(Level.INFO, "Sound enabled: {0}", enabled); //NON-NLS
|
LOGGER.log(Level.INFO, "Music enabled: {0}", enabled); //NON-NLS
|
||||||
PREFERENCES.putBoolean(ENABLED_PREF, enabled);
|
PREFERENCES.putBoolean(ENABLED_PREF, enabled);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initializes the sound effects for the game.
|
* Initializes the music.
|
||||||
* Overrides {@link AbstractAppState#initialize(AppStateManager, Application)}
|
* Overrides {@link AbstractAppState#initialize(AppStateManager, Application)}
|
||||||
*
|
*
|
||||||
* @param stateManager The state manager
|
* @param stateManager The state manager
|
||||||
@ -75,13 +86,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);
|
||||||
shipDestroyedSound = loadSound(app, "Sound/Effects/sunken.wav"); //NON-NLS
|
menuMusicModern =loadSound(app, "Sound/BackgroundMusic/menu-music-modern.ogg");
|
||||||
splashSound = loadSound(app, "Sound/Effects/splash.wav"); //NON-NLS
|
setVolume(volumePreference());
|
||||||
explosionSound = loadSound(app, "Sound/Effects/explosion.wav"); //NON-NLS
|
menuMusicModern.setLooping(true);
|
||||||
|
if (isEnabled() && menuMusicModern != null) {
|
||||||
|
menuMusicModern.play();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Loads a sound from the specified file.
|
* Loads the music from the specified file.
|
||||||
*
|
*
|
||||||
* @param app The application
|
* @param app The application
|
||||||
* @param name The name of the sound file.
|
* @param name The name of the sound file.
|
||||||
@ -101,35 +116,18 @@ public class GameSound extends AbstractAppState implements GameEventListener {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Plays the splash sound effect.
|
* Sets the vol param to the level set in PREFERENCES
|
||||||
|
*
|
||||||
|
* @param vol Volume level of the music as indicated by the Volume control Slider
|
||||||
|
*
|
||||||
*/
|
*/
|
||||||
public void splash() {
|
public void setVolume(float vol){
|
||||||
if (isEnabled() && splashSound != null)
|
menuMusicModern.setVolume(vol);
|
||||||
splashSound.playInstance();
|
PREFERENCES.putFloat(VOLUME_PREF, vol);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 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) {
|
|
||||||
switch (event.sound()) {
|
|
||||||
case EXPLOSION -> explosion();
|
|
||||||
case SPLASH -> splash();
|
|
||||||
case DESTROYED_SHIP -> shipDestroyed();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
Loading…
Reference in New Issue
Block a user