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 {
private static final String VOLUME_PREF = "volume";
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());
@@ -40,8 +40,8 @@ public class BackgroundMusic implements GameEventListener {
* @param app The main Application
*/
public BackgroundMusic(BattleshipApp app) {
this.volume = prefs.getFloat(VOLUME_PREF, 1.0f);
this.musicEnabled = prefs.getBoolean(MUSIC_ENABLED_PREF, true);
this.volume = PREFS.getFloat(VOLUME_PREF, 1.0f);
this.musicEnabled = PREFS.getBoolean(MUSIC_ENABLED_PREF, true);
this.app = app;
menuMusic = createAudioNode(MENU_MUSIC);
@@ -135,14 +135,14 @@ public void toggleMusic() {
pause(gameOverMusicV);
}
prefs.putBoolean(MUSIC_ENABLED_PREF, musicEnabled);
PREFS.putBoolean(MUSIC_ENABLED_PREF, musicEnabled);
}
/**
* this method is used when the main volume changes
*/
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);
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.audio.AudioData;
import com.jme3.audio.AudioNode;
import com.jme3.audio.AudioSource;
import pp.battleship.notification.GameEventListener;
import pp.battleship.notification.SoundEvent;
@@ -81,9 +82,9 @@ public void setEnabled(boolean enabled) {
public void initialize(AppStateManager stateManager, Application app) {
super.initialize(stateManager, app);
this.app = (BattleshipApp) app;
shipDestroyedSound = loadSound(app, "Sound/Effects/sunken.wav"); //NON-NLS
splashSound = loadSound(app, "Sound/Effects/splash.wav"); //NON-NLS
explosionSound = loadSound(app, "Sound/Effects/explosion.wav"); //NON-NLS
shipDestroyedSound = loadSound(app, "Sound/Effects/sunken.wav", 1); //NON-NLS
splashSound = loadSound(app, "Sound/Effects/splash.wav", 2); //NON-NLS
explosionSound = loadSound(app, "Sound/Effects/explosion.wav",3); //NON-NLS
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.
* @return The loaded AudioNode.
*/
private AudioNode loadSound(Application app, String name) {
private AudioNode loadSound(Application app, String name, int channel) {
try {
final AudioNode sound = new AudioNode(app.getAssetManager(), name, AudioData.DataType.Buffer);
sound.setLooping(false);
sound.setPositional(false);
sound.setChannel(channel);
return sound;
}
catch (AssetLoadException | AssetNotFoundException ex) {
@@ -138,9 +140,10 @@ public void shipDestroyed() {
*/
public void setSoundVolume(float volume) {
float mainVolume = app.getMainVolumeControl().getMainVolume();
shipDestroyedSound.setVolume(volume);
splashSound.setVolume(volume);
explosionSound.setVolume(volume);
float calculatedVolume = volume * mainVolume;
shipDestroyedSound.setVolume(calculatedVolume);
splashSound.setVolume(calculatedVolume);
explosionSound.setVolume(calculatedVolume);
this.volume = volume;
PREFERENCES.putFloat(SOUND_VOLUME_PREF, volume);
}
@@ -168,9 +171,15 @@ public float getVolume(){
@Override
public void receivedEvent(SoundEvent event) {
switch (event.sound()) {
case EXPLOSION -> explosion();
case SPLASH -> splash();
case DESTROYED_SHIP -> shipDestroyed();
case EXPLOSION :
explosion();
break;
case SPLASH :
splash();
break;
case DESTROYED_SHIP:
shipDestroyed();
break;
}
}
}

View File

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

View File

@@ -15,7 +15,7 @@
public class ShellMapControl extends AbstractControl {
private final Position position;
private final IntPoint pos;
private static final Vector3f vector = new Vector3f();
private static final Vector3f VECTOR = new Vector3f();
private final BattleshipApp app;
/**
@@ -30,7 +30,7 @@ public ShellMapControl(Position position, BattleshipApp app, IntPoint pos) {
this.position = position;
this.pos = pos;
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
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()) {
spatial.getParent().detachChild(spatial);
app.getGameLogic().send(new AnimationEndMessage(pos));

View File

@@ -213,9 +213,10 @@ public void received(ShootMessage msg, int from) {
*/
@Override
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);
else
return;
}
if(getPlayerById(from) == players.get(0)){
LOGGER.log(Level.DEBUG, "{0} set to true", getPlayerById(from));
player1AnimationReady = true;

View File

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