added the rocket sound wav and fixed code and check style

also includes minor fixes
This commit is contained in:
Hanno Fleischer
2024-10-13 08:09:19 +02:00
parent 22d827b074
commit 487305dccc
7 changed files with 51 additions and 40 deletions

View File

@@ -14,7 +14,7 @@
public class BackgroundMusic implements GameEventListener { public class BackgroundMusic implements GameEventListener {
private static final String VOLUME_PREF = "volume"; private static final String VOLUME_PREF = "volume";
private static final String MUSIC_ENABLED_PREF = "musicEnabled"; private static final String MUSIC_ENABLED_PREF = "musicEnabled";
private static final Preferences prefs = Preferences.userNodeForPackage(BackgroundMusic.class); private static final Preferences PREFS = Preferences.userNodeForPackage(BackgroundMusic.class);
static final Logger LOGGER = System.getLogger(BackgroundMusic.class.getName()); static final Logger LOGGER = System.getLogger(BackgroundMusic.class.getName());
@@ -40,8 +40,8 @@ public class BackgroundMusic implements GameEventListener {
* @param app The main Application * @param app The main Application
*/ */
public BackgroundMusic(BattleshipApp app) { public BackgroundMusic(BattleshipApp app) {
this.volume = prefs.getFloat(VOLUME_PREF, 1.0f); this.volume = PREFS.getFloat(VOLUME_PREF, 1.0f);
this.musicEnabled = prefs.getBoolean(MUSIC_ENABLED_PREF, true); this.musicEnabled = PREFS.getBoolean(MUSIC_ENABLED_PREF, true);
this.app = app; this.app = app;
menuMusic = createAudioNode(MENU_MUSIC); menuMusic = createAudioNode(MENU_MUSIC);
@@ -135,14 +135,14 @@ public void toggleMusic() {
pause(gameOverMusicV); pause(gameOverMusicV);
} }
prefs.putBoolean(MUSIC_ENABLED_PREF, musicEnabled); PREFS.putBoolean(MUSIC_ENABLED_PREF, musicEnabled);
} }
/** /**
* this method is used when the main volume changes * this method is used when the main volume changes
*/ */
public void setVolume(){ public void setVolume(){
setVolume(prefs.getFloat(VOLUME_PREF, 1.0f)); setVolume(PREFS.getFloat(VOLUME_PREF, 1.0f));
} }
/** /**
@@ -158,7 +158,7 @@ public void setVolume(float volume) {
gameOverMusicL.setVolume(volume * mainVolume); gameOverMusicL.setVolume(volume * mainVolume);
gameOverMusicV.setVolume(volume * mainVolume); gameOverMusicV.setVolume(volume * mainVolume);
prefs.putFloat(VOLUME_PREF, volume); PREFS.putFloat(VOLUME_PREF, volume);
} }
/** /**

View File

@@ -14,6 +14,7 @@
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 com.jme3.audio.AudioSource;
import pp.battleship.notification.GameEventListener; import pp.battleship.notification.GameEventListener;
import pp.battleship.notification.SoundEvent; import pp.battleship.notification.SoundEvent;
@@ -81,9 +82,9 @@ public void setEnabled(boolean enabled) {
public void initialize(AppStateManager stateManager, Application app) { public void initialize(AppStateManager stateManager, Application app) {
super.initialize(stateManager, app); super.initialize(stateManager, app);
this.app = (BattleshipApp) app; this.app = (BattleshipApp) app;
shipDestroyedSound = loadSound(app, "Sound/Effects/sunken.wav"); //NON-NLS shipDestroyedSound = loadSound(app, "Sound/Effects/sunken.wav", 1); //NON-NLS
splashSound = loadSound(app, "Sound/Effects/splash.wav"); //NON-NLS splashSound = loadSound(app, "Sound/Effects/splash.wav", 2); //NON-NLS
explosionSound = loadSound(app, "Sound/Effects/explosion.wav"); //NON-NLS explosionSound = loadSound(app, "Sound/Effects/explosion.wav",3); //NON-NLS
volume = PREFERENCES.getFloat(SOUND_VOLUME_PREF, 1.0f); volume = PREFERENCES.getFloat(SOUND_VOLUME_PREF, 1.0f);
} }
@@ -94,11 +95,12 @@ public void initialize(AppStateManager stateManager, Application app) {
* @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(Application app, String name) { private AudioNode loadSound(Application app, String name, int channel) {
try { try {
final 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);
sound.setChannel(channel);
return sound; return sound;
} }
catch (AssetLoadException | AssetNotFoundException ex) { catch (AssetLoadException | AssetNotFoundException ex) {
@@ -138,9 +140,10 @@ public void shipDestroyed() {
*/ */
public void setSoundVolume(float volume) { public void setSoundVolume(float volume) {
float mainVolume = app.getMainVolumeControl().getMainVolume(); float mainVolume = app.getMainVolumeControl().getMainVolume();
shipDestroyedSound.setVolume(volume); float calculatedVolume = volume * mainVolume;
splashSound.setVolume(volume); shipDestroyedSound.setVolume(calculatedVolume);
explosionSound.setVolume(volume); splashSound.setVolume(calculatedVolume);
explosionSound.setVolume(calculatedVolume);
this.volume = volume; this.volume = volume;
PREFERENCES.putFloat(SOUND_VOLUME_PREF, volume); PREFERENCES.putFloat(SOUND_VOLUME_PREF, volume);
} }
@@ -168,9 +171,15 @@ public float getVolume(){
@Override @Override
public void receivedEvent(SoundEvent event) { public void receivedEvent(SoundEvent event) {
switch (event.sound()) { switch (event.sound()) {
case EXPLOSION -> explosion(); case EXPLOSION :
case SPLASH -> splash(); explosion();
case DESTROYED_SHIP -> shipDestroyed(); break;
case SPLASH :
splash();
break;
case DESTROYED_SHIP:
shipDestroyed();
break;
} }
} }
} }

View File

@@ -5,7 +5,7 @@
import java.util.prefs.Preferences; import java.util.prefs.Preferences;
public class MainVolume { public class MainVolume {
private static final Preferences prefs = Preferences.userNodeForPackage(MainVolume.class); private static final Preferences PREFS = Preferences.userNodeForPackage(MainVolume.class);
private static final String MAIN_VOLUME_PREFS = "MainVolume"; private static final String MAIN_VOLUME_PREFS = "MainVolume";
static final Logger LOGGER = System.getLogger(MainVolume.class.getName()); static final Logger LOGGER = System.getLogger(MainVolume.class.getName());
@@ -14,7 +14,7 @@ public class MainVolume {
private BattleshipApp app; private BattleshipApp app;
public MainVolume(BattleshipApp app) { public MainVolume(BattleshipApp app) {
this.mainVolume = prefs.getFloat(MAIN_VOLUME_PREFS, 1.0f); this.mainVolume = PREFS.getFloat(MAIN_VOLUME_PREFS, 1.0f);
this.app = app; this.app = app;
} }
@@ -23,7 +23,7 @@ public void setMainVolume(float mainVolume) {
app.getBackgroundMusic().setVolume(); app.getBackgroundMusic().setVolume();
app.getStateManager().getState(GameSound.class).setSoundVolume(); app.getStateManager().getState(GameSound.class).setSoundVolume();
this.mainVolume = mainVolume; this.mainVolume = mainVolume;
prefs.putFloat(MAIN_VOLUME_PREFS, mainVolume); PREFS.putFloat(MAIN_VOLUME_PREFS, mainVolume);
} }
public float getMainVolume() { public float getMainVolume() {

View File

@@ -15,7 +15,7 @@
public class ShellMapControl extends AbstractControl { public class ShellMapControl extends AbstractControl {
private final Position position; private final Position position;
private final IntPoint pos; private final IntPoint pos;
private static final Vector3f vector = new Vector3f(); private static final Vector3f VECTOR = new Vector3f();
private final BattleshipApp app; private final BattleshipApp app;
/** /**
@@ -30,7 +30,7 @@ public ShellMapControl(Position position, BattleshipApp app, IntPoint pos) {
this.position = position; this.position = position;
this.pos = pos; this.pos = pos;
this.app = app; this.app = app;
vector.set(new Vector3f(position.getX(), position.getY(), 0)); VECTOR.set(new Vector3f(position.getX(), position.getY(), 0));
} }
/** /**
@@ -41,7 +41,7 @@ public ShellMapControl(Position position, BattleshipApp app, IntPoint pos) {
*/ */
@Override @Override
protected void controlUpdate(float tpf) { protected void controlUpdate(float tpf) {
spatial.move(vector.mult(tpf)); spatial.move(VECTOR.mult(tpf));
if (spatial.getLocalTranslation().getX() >= position.getX() && spatial.getLocalTranslation().getY() >= position.getY()) { if (spatial.getLocalTranslation().getX() >= position.getX() && spatial.getLocalTranslation().getY() >= position.getY()) {
spatial.getParent().detachChild(spatial); spatial.getParent().detachChild(spatial);
app.getGameLogic().send(new AnimationEndMessage(pos)); app.getGameLogic().send(new AnimationEndMessage(pos));

View File

@@ -213,25 +213,26 @@ public void received(ShootMessage msg, int from) {
*/ */
@Override @Override
public void received(AnimationEndMessage msg, int from){ public void received(AnimationEndMessage msg, int from){
if(state != ServerState.ANIMATION_WAIT) if(state != ServerState.ANIMATION_WAIT) {
LOGGER.log(Level.ERROR, "animation not allowed in {0}", state); LOGGER.log(Level.ERROR, "animation not allowed in {0}", state);
else return;
if(getPlayerById(from) == players.get(0)){ }
LOGGER.log(Level.DEBUG, "{0} set to true", getPlayerById(from)); if(getPlayerById(from) == players.get(0)){
player1AnimationReady = true; LOGGER.log(Level.DEBUG, "{0} set to true", getPlayerById(from));
shoot(getPlayerById(from), msg.getPosition()); player1AnimationReady = true;
} else if (getPlayerById(from) == players.get(1)){ shoot(getPlayerById(from), msg.getPosition());
LOGGER.log(Level.DEBUG, "{0} set to true {1}", getPlayerById(from), getPlayerById(from).toString()); } else if (getPlayerById(from) == players.get(1)){
player2AnimationReady = true; LOGGER.log(Level.DEBUG, "{0} set to true {1}", getPlayerById(from), getPlayerById(from).toString());
shoot(getPlayerById(from), msg.getPosition()); player2AnimationReady = true;
} shoot(getPlayerById(from), msg.getPosition());
if(player1AnimationReady && player2AnimationReady){ }
setState(ServerState.BATTLE); if(player1AnimationReady && player2AnimationReady){
for (Player player : players) setState(ServerState.BATTLE);
send(player, new SwitchBattleState(player == activePlayer)); for (Player player : players)
player1AnimationReady = false; send(player, new SwitchBattleState(player == activePlayer));
player2AnimationReady = false; player1AnimationReady = false;
} player2AnimationReady = false;
}
} }
/** /**

View File

@@ -26,6 +26,7 @@ class InterpreterProxy implements ServerInterpreter {
* *
* @param playerClient the client to which the server messages are forwarded * @param playerClient the client to which the server messages are forwarded
*/ */
InterpreterProxy(BattleshipClient playerClient) { InterpreterProxy(BattleshipClient playerClient) {
this.playerClient = playerClient; this.playerClient = playerClient;
} }