Aufgabe 1-10 bearbeitet. Musik (Piratenmusik.ogg) hinzugefügt. Schiebregler hinzugefügt (VolumeLSider); update Methode überschrieben; Lemur Doc gelesen

This commit is contained in:
Dennis_Malkmus
2024-10-13 23:45:26 +02:00
parent f47cda7dc4
commit f5dc8a0f05
66 changed files with 476090 additions and 4 deletions

View File

@@ -9,6 +9,8 @@ dependencies {
implementation project(":battleship:model")
implementation libs.jme3.desktop
implementation 'com.simsilica:lemur:1.14.0'
implementation 'com.simsilica:lemur-proto:1.10.0'
runtimeOnly libs.jme3.awt.dialogs
runtimeOnly libs.jme3.plugins

View File

@@ -0,0 +1,127 @@
package pp.battleship.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.battleship.notification.GameEventListener;
import pp.battleship.notification.SoundEvent;
import java.lang.System.Logger;
import java.lang.System.Logger.Level;
import java.util.prefs.Preferences;
import static pp.util.PreferencesUtils.getPreferences;
/**
* An application state that plays music.
*/
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 piratenMusic;
/**
* Checks if sound is enabled in the preferences.
*
* @return {@code true} if sound is enabled, {@code false} otherwise.
*/
//Prüft, ob die Musik eingeschalten ist.
public static boolean enabledInPreferences() {
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
*
*/
//Standardeinstellung für Musik
public static float volumePreference() {
return PREFERENCES.getFloat(VOLUME_PREF, 0.33f);
}
/**
* Toggles the game sound on or off.
*/
public void toggleSound() {
setEnabled(!isEnabled());
}
/**
* 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;
else if (!isEnabled() && enabled) {
if (piratenMusic != null) piratenMusic.play();
} else if (isEnabled() && !enabled) {
if (piratenMusic != null) piratenMusic.stop();
}
super.setEnabled(enabled);
LOGGER.log(Level.INFO, "Music enabled: {0}", enabled); //NON-NLS
PREFERENCES.putBoolean(ENABLED_PREF, enabled);
}
/**
* Initializes the music.
* 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);
piratenMusic =loadSound(app, "Sound/BackgroundMusic/Piratenmusik.ogg");
setVolume(volumePreference());
piratenMusic.setLooping(true);
if (isEnabled() && piratenMusic != null) {
piratenMusic.play();
}
}
/**
* Loads the music 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;
}
//Einstellung der Lautstärke
public void setVolume(float vol){
piratenMusic.setVolume(vol);
PREFERENCES.putFloat(VOLUME_PREF, vol);
}
}

View File

@@ -7,10 +7,14 @@
package pp.battleship.client;
import com.jme3.math.Vector3f;
import com.simsilica.lemur.Button;
import com.simsilica.lemur.Checkbox;
import com.simsilica.lemur.Label;
import com.simsilica.lemur.RangedValueModel;
import com.simsilica.lemur.style.ElementId;
import com.simsilica.lemur.Slider;
import pp.battleship.client.gui.VolumeSlider;
import pp.dialog.Dialog;
import pp.dialog.StateCheckboxModel;
import pp.dialog.TextInputDialog;
@@ -33,6 +37,7 @@ class Menu extends Dialog {
private final BattleshipApp app;
private final Button loadButton = new Button(lookup("menu.map.load"));
private final Button saveButton = new Button(lookup("menu.map.save"));
private final VolumeSlider volumeSlider;
/**
* Constructs the Menu dialog for the Battleship application.
@@ -45,6 +50,26 @@ class Menu extends Dialog {
addChild(new Label(lookup("battleship.name"), new ElementId("header"))); //NON-NLS
addChild(new Checkbox(lookup("menu.sound-enabled"),
new StateCheckboxModel(app, GameSound.class)));
addChild(new Label(lookup("battleship.name"), new ElementId("header")));//NON-NLS
addChild(new Checkbox(lookup("menu.sound-enabled"), new StateCheckboxModel(app, GameSound.class)));
addChild(new Checkbox(lookup("menu.background-sound-enabled"), new StateCheckboxModel(app, GameMusic.class)));
volumeSlider = new VolumeSlider(app.getStateManager().getState(GameMusic.class));
addChild(volumeSlider);
addChild(loadButton)
.addClickCommands(s -> ifTopDialog(this::loadDialog));
addChild(saveButton);
saveButton.setEnabled(app.getGameLogic().maySaveMap());
// Füge den Slider zum GUI hinzu
addChild(volumeSlider);
addChild(loadButton)
.addClickCommands(s -> ifTopDialog(this::loadDialog));
addChild(saveButton)
@@ -140,4 +165,6 @@ class Menu extends Dialog {
private void saveDialog() {
fileDialog(app.getGameLogic()::saveMap, lookup("menu.map.save"));
}
}

View File

@@ -32,9 +32,13 @@ import static pp.util.FloatMath.PI;
* It extends the {@link ShipMapSynchronizer} to provide specific synchronization
* logic for the sea map.
*/
//ANPASSEN!!!!
class SeaSynchronizer extends ShipMapSynchronizer {
private static final String UNSHADED = "Common/MatDefs/Misc/Unshaded.j3md"; //NON-NLS
private static final String KING_GEORGE_V_MODEL = "Models/KingGeorgeV/KingGeorgeV.j3o"; //NON-NLS
private static final String FLUGZEUGTRAEGER = "Models/Flugzeugträger_5er/Flugzeugträger.j3o";
private static final String KORVETTE = "Models/Korvette_3er/Korvette.j3o";
private static final String ZERSTOERER = "Models/Zerstörer_2er/Zerstörer.j3o";
private static final String COLOR = "Color"; //NON-NLS
private static final String SHIP = "ship"; //NON-NLS
private static final String SHOT = "shot"; //NON-NLS
@@ -140,8 +144,14 @@ class SeaSynchronizer extends ShipMapSynchronizer {
* @param ship the battleship to be represented
* @return the spatial representing the battleship
*/
//ANPASSEN!!!!!!
private Spatial createShip(Battleship ship) {
return ship.getLength() == 4 ? createBattleship(ship) : createBox(ship);
if( ship.getLength() == 4){ createBox(ship); return createBattleship(ship); }
else if(ship.getLength()==5){createBox(ship);return createFlugzeugtraeger(ship); }
else if(ship.getLength()==3){createBox(ship);return createKorvette(ship); }
else if(ship.getLength()==2){createBox(ship);return createZerstoerer(ship); }
else if(ship.getLength()==1){createBox(ship);return createZerstoerer(ship); }
else throw new IllegalArgumentException("Deine Länge ist nicht definiert!");
}
/**
@@ -186,6 +196,7 @@ class SeaSynchronizer extends ShipMapSynchronizer {
* @param ship the battleship to be represented
* @return the spatial representing the "King George V" battleship
*/
//ANPASSEN!!!!
private Spatial createBattleship(Battleship ship) {
final Spatial model = app.getAssetManager().loadModel(KING_GEORGE_V_MODEL);
@@ -196,6 +207,36 @@ class SeaSynchronizer extends ShipMapSynchronizer {
return model;
}
private Spatial createFlugzeugtraeger(Battleship ship) {
final Spatial model = app.getAssetManager().loadModel(FLUGZEUGTRAEGER);
model.rotate(-HALF_PI, calculateRotationAngle(ship.getRot()), 0f);
model.scale(1.48f);
model.setShadowMode(ShadowMode.CastAndReceive);
return model;
}
private Spatial createKorvette(Battleship ship) {
final Spatial model = app.getAssetManager().loadModel(KORVETTE);
model.rotate(-HALF_PI, calculateRotationAngle(ship.getRot()), 0f);
model.scale(1.48f);
model.setShadowMode(ShadowMode.CastAndReceive);
return model;
}
private Spatial createZerstoerer(Battleship ship) {
final Spatial model = app.getAssetManager().loadModel(ZERSTOERER);
model.rotate(-HALF_PI, calculateRotationAngle(ship.getRot()), 0f);
model.scale(1.48f);
model.setShadowMode(ShadowMode.CastAndReceive);
return model;
}
/**
* Calculates the rotation angle for the specified rotation.
*

View File

@@ -0,0 +1,29 @@
package pp.battleship.client.gui;
import com.simsilica.lemur.Slider;
import pp.battleship.client.GameMusic;
public class VolumeSlider extends Slider {
private final GameMusic piratenMusic;
private double vol;
public VolumeSlider(GameMusic music) {
super();
this.piratenMusic = music;
vol = GameMusic.volumePreference();
getModel().setPercent(vol);
}
public void update() {
if (vol != getModel().getPercent()) {
vol = getModel().getPercent();
piratenMusic.setVolume( (float) vol);
}
}
}