Compare commits
	
		
			19 Commits
		
	
	
		
			dev/model
			...
			b_Beck_Ced
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 
						 | 
					5035aa5a96 | ||
| 
						 | 
					67b99317d2 | ||
| 
						 | 
					bb1e3858bb | ||
| 
						 | 
					9b85030050 | ||
| 
						 | 
					4492843ca1 | ||
| 
						 | 
					5edd4ebe0b | ||
| 
						 | 
					216bd60d84 | ||
| 
						 | 
					8275778d66 | ||
| 
						 | 
					1d114c8d24 | ||
| 
						 | 
					8b6a787115 | ||
| 
						 | 
					8a5afe0b18 | ||
| 
						 | 
					6496a5a6b7 | ||
| 
						 | 
					af221ad693 | ||
| 
						 | 
					ae61e8061c | ||
| 
						 | 
					5f596d5797 | ||
| 
						 | 
					82d86a378a | ||
| 
						 | 
					02f7a6542e | ||
| 
						 | 
					1afa621bec | ||
| 
						 | 
					546872dd83 | 
@@ -26,10 +26,13 @@ map.own=maps/map1.json
 | 
			
		||||
robot.targets=2, 0,\
 | 
			
		||||
              2, 1,\
 | 
			
		||||
              2, 2,\
 | 
			
		||||
              2, 3
 | 
			
		||||
              2, 3,\
 | 
			
		||||
              2, 4,\
 | 
			
		||||
              2, 5,\
 | 
			
		||||
              2, 6
 | 
			
		||||
#
 | 
			
		||||
# Delay in milliseconds between each shot fired by the RobotClient.
 | 
			
		||||
robot.delay=500
 | 
			
		||||
robot.delay=4000
 | 
			
		||||
#
 | 
			
		||||
# The dimensions of the game map used in single mode.
 | 
			
		||||
# 'map.width' defines the number of columns, and 'map.height' defines the number of rows.
 | 
			
		||||
 
 | 
			
		||||
@@ -122,6 +122,8 @@ public class BattleshipApp extends SimpleApplication implements BattleshipClient
 | 
			
		||||
     */
 | 
			
		||||
    private final ActionListener escapeListener = (name, isPressed, tpf) -> escape(isPressed);
 | 
			
		||||
 | 
			
		||||
    private EffectHandler effectHandler;
 | 
			
		||||
 | 
			
		||||
    static {
 | 
			
		||||
        // Configure logging
 | 
			
		||||
        LogManager manager = LogManager.getLogManager();
 | 
			
		||||
@@ -155,6 +157,7 @@ private BattleshipApp() {
 | 
			
		||||
        logic.addListener(this);
 | 
			
		||||
        setShowSettings(config.getShowSettings());
 | 
			
		||||
        setSettings(makeSettings());
 | 
			
		||||
        effectHandler = null;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
@@ -224,6 +227,7 @@ public void simpleInitApp() {
 | 
			
		||||
        setupInput();
 | 
			
		||||
        setupStates();
 | 
			
		||||
        setupGui();
 | 
			
		||||
        effectHandler = new EffectHandler(this);
 | 
			
		||||
        serverConnection.connect();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -265,11 +269,20 @@ private void setupStates() {
 | 
			
		||||
        flyCam.setEnabled(false);
 | 
			
		||||
        stateManager.detach(stateManager.getState(StatsAppState.class));
 | 
			
		||||
        stateManager.detach(stateManager.getState(DebugKeysAppState.class));
 | 
			
		||||
 | 
			
		||||
        attachGameMusic();
 | 
			
		||||
        attachGameSound();
 | 
			
		||||
        stateManager.attachAll(new EditorAppState(), new BattleAppState(), new SeaAppState());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Attaches the game sound state and sets its initial enabled state.
 | 
			
		||||
     */
 | 
			
		||||
    private void attachGameMusic() {
 | 
			
		||||
        final GameMusic gameMusic = new GameMusic();
 | 
			
		||||
        gameMusic.setEnabled(GameMusic.enabledInPreferences());
 | 
			
		||||
        stateManager.attach(gameMusic);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Attaches the game sound state and sets its initial enabled state.
 | 
			
		||||
     */
 | 
			
		||||
@@ -426,4 +439,8 @@ void errorDialog(String errorMessage) {
 | 
			
		||||
                     .build()
 | 
			
		||||
                     .open();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public EffectHandler getEffectHandler() {
 | 
			
		||||
        return effectHandler;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -0,0 +1,197 @@
 | 
			
		||||
package pp.battleship.client;
 | 
			
		||||
 | 
			
		||||
import com.jme3.effect.ParticleEmitter;
 | 
			
		||||
import com.jme3.effect.ParticleMesh;
 | 
			
		||||
import com.jme3.material.Material;
 | 
			
		||||
import com.jme3.math.ColorRGBA;
 | 
			
		||||
import com.jme3.math.Vector3f;
 | 
			
		||||
import com.jme3.scene.Geometry;
 | 
			
		||||
import com.jme3.scene.Node;
 | 
			
		||||
import pp.battleship.model.Battleship;
 | 
			
		||||
 | 
			
		||||
import java.util.ArrayList;
 | 
			
		||||
import java.util.HashMap;
 | 
			
		||||
import java.util.List;
 | 
			
		||||
import java.util.Map;
 | 
			
		||||
import java.util.Timer;
 | 
			
		||||
import java.util.TimerTask;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * EffectHandler manages the creation and manipulation of particle effects in the Battleship application.
 | 
			
		||||
 */
 | 
			
		||||
public class EffectHandler {
 | 
			
		||||
    private final BattleshipApp app;
 | 
			
		||||
    private final Map<Battleship, List<ParticleEmitter>> effects;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Constructs an EffectHandler with the specified BattleshipApp instance.
 | 
			
		||||
     *
 | 
			
		||||
     * @param app the BattleshipApp instance
 | 
			
		||||
     */
 | 
			
		||||
    public EffectHandler(BattleshipApp app) {
 | 
			
		||||
        this.app = app;
 | 
			
		||||
        effects = new HashMap<>();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Creates a fire effect at the specified position for the given Battleship.
 | 
			
		||||
     *
 | 
			
		||||
     * @param point the position where the fire effect will be created
 | 
			
		||||
     * @param ship  the Battleship associated with the fire effect
 | 
			
		||||
     * @return a Node containing the fire effect
 | 
			
		||||
     */
 | 
			
		||||
    public Node createFire(Vector3f point, Battleship ship) {
 | 
			
		||||
        Node parent = new Node();
 | 
			
		||||
        parent.setLocalTranslation(point);
 | 
			
		||||
 | 
			
		||||
        ParticleEmitter fire = initializeParticleEmitter(
 | 
			
		||||
                "Effects/Explosion/flame.png",
 | 
			
		||||
                2,2,
 | 
			
		||||
                new ColorRGBA(1f, 0f, 0f, 1f),
 | 
			
		||||
                new ColorRGBA(1f, 1f, 0f, 0.5f),
 | 
			
		||||
                new Vector3f(0, 1.5f, 0),
 | 
			
		||||
                50,
 | 
			
		||||
                .4f,
 | 
			
		||||
                0.05f,
 | 
			
		||||
                1f,
 | 
			
		||||
                2f,
 | 
			
		||||
                0.2f,
 | 
			
		||||
                new Vector3f(0, 0, 0)
 | 
			
		||||
        );
 | 
			
		||||
 | 
			
		||||
        ParticleEmitter smoke = initializeParticleEmitter(
 | 
			
		||||
                "Effects/Smoke/Smoke.png",
 | 
			
		||||
                15,
 | 
			
		||||
                1,
 | 
			
		||||
                new ColorRGBA(1f, 1f, 1f, 0f),
 | 
			
		||||
                new ColorRGBA(0.5f, 0.5f, 0.5f, 0.5f),
 | 
			
		||||
                new Vector3f(0, 1f, 0),
 | 
			
		||||
                600,
 | 
			
		||||
                .2f,
 | 
			
		||||
                0.1f,
 | 
			
		||||
                1f,
 | 
			
		||||
                5f,
 | 
			
		||||
                0.25f,
 | 
			
		||||
                new Vector3f(0, 0, 0)
 | 
			
		||||
        );
 | 
			
		||||
 | 
			
		||||
        parent.attachChild(fire);
 | 
			
		||||
        parent.attachChild(smoke);
 | 
			
		||||
 | 
			
		||||
        List<ParticleEmitter> oldEffects = new ArrayList<>(effects.getOrDefault(ship, new ArrayList<>()));
 | 
			
		||||
        oldEffects.add(fire);
 | 
			
		||||
        oldEffects.add(smoke);
 | 
			
		||||
        effects.put(ship, oldEffects);
 | 
			
		||||
 | 
			
		||||
        return parent;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Creates a water splash effect at the specified position.
 | 
			
		||||
     *
 | 
			
		||||
     * @param pos the position where the water splash effect will be created
 | 
			
		||||
     * @return a Geometry representing the water splash effect
 | 
			
		||||
     */
 | 
			
		||||
    public Geometry waterSplash(Vector3f pos) {
 | 
			
		||||
        ParticleEmitter water = initializeParticleEmitter(
 | 
			
		||||
                "Effects/Explosion/flash.png",
 | 
			
		||||
                2,2,
 | 
			
		||||
                new ColorRGBA(0.3f, 0.8f, 1f, 0f),
 | 
			
		||||
                new ColorRGBA(0f, 0f, 1f, 1f),
 | 
			
		||||
                new Vector3f(0, 3, 0),
 | 
			
		||||
                100,
 | 
			
		||||
                .6f,
 | 
			
		||||
                0.05f,
 | 
			
		||||
                1f,
 | 
			
		||||
                1.5f,
 | 
			
		||||
                0.3f,
 | 
			
		||||
                new Vector3f(0, 4f, 0)
 | 
			
		||||
        );
 | 
			
		||||
 | 
			
		||||
        water.setLocalTranslation(pos);
 | 
			
		||||
        water.emitAllParticles();
 | 
			
		||||
        water.setParticlesPerSec(0);
 | 
			
		||||
 | 
			
		||||
        new Timer().schedule(new TimerTask() {
 | 
			
		||||
            @Override
 | 
			
		||||
            public void run() {
 | 
			
		||||
                deleteSplash(water);
 | 
			
		||||
            }
 | 
			
		||||
        }, 2000);
 | 
			
		||||
 | 
			
		||||
        return water;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Creates a debris splash effect at the specified position.
 | 
			
		||||
     *
 | 
			
		||||
     * @param pos the position where the debris splash effect will be created
 | 
			
		||||
     * @return a Geometry representing the debris splash effect
 | 
			
		||||
     */
 | 
			
		||||
    public Geometry debrisSplash(Vector3f pos) {
 | 
			
		||||
        ParticleEmitter debris = initializeParticleEmitter(
 | 
			
		||||
                "Effects/Explosion/Debris.png",
 | 
			
		||||
                3,3,
 | 
			
		||||
                new ColorRGBA(0.1f, 0.1f, 0.1f, 0f),
 | 
			
		||||
                new ColorRGBA(0.5f, 0.5f, 0.5f, .8f),
 | 
			
		||||
                new Vector3f(0, 2f, 0),
 | 
			
		||||
                50,
 | 
			
		||||
                0.1f,
 | 
			
		||||
                0.5f,
 | 
			
		||||
                1f,
 | 
			
		||||
                1.5f,
 | 
			
		||||
                0.5f,
 | 
			
		||||
                new Vector3f(0, 0, 0)
 | 
			
		||||
        );
 | 
			
		||||
        debris.setLocalTranslation(pos);
 | 
			
		||||
 | 
			
		||||
        debris.emitAllParticles();
 | 
			
		||||
        debris.setParticlesPerSec(0);
 | 
			
		||||
 | 
			
		||||
        return debris;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Deletes the specified splash effect from the scene.
 | 
			
		||||
     *
 | 
			
		||||
     * @param splash the Geometry representing the splash effect to be deleted
 | 
			
		||||
     */
 | 
			
		||||
    private void deleteSplash(Geometry splash) {
 | 
			
		||||
        splash.getParent().detachChild(splash);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Stops all particle effects associated with the specified Battleship.
 | 
			
		||||
     *
 | 
			
		||||
     * @param ship the Battleship whose effects are to be destroyed
 | 
			
		||||
     */
 | 
			
		||||
    public void destroyShip(Battleship ship) {
 | 
			
		||||
        for (ParticleEmitter emitter : effects.get(ship)) {
 | 
			
		||||
            emitter.setParticlesPerSec(0);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private ParticleEmitter initializeParticleEmitter(
 | 
			
		||||
            String texturePath, int imagesX, int imagesY, ColorRGBA endColor, ColorRGBA startColor, Vector3f initialVelocity,
 | 
			
		||||
            int particleCount, float startSize, float endSize, float lowLife, float highLife,
 | 
			
		||||
            float velocityVariation, Vector3f gravity
 | 
			
		||||
    ) {
 | 
			
		||||
        ParticleEmitter emitter = new ParticleEmitter("Emitter", ParticleMesh.Type.Triangle, particleCount);
 | 
			
		||||
        Material mat = new Material(app.getAssetManager(), "Common/MatDefs/Misc/Particle.j3md");
 | 
			
		||||
        mat.setTexture("Texture", app.getAssetManager().loadTexture(texturePath));
 | 
			
		||||
        emitter.setMaterial(mat);
 | 
			
		||||
        emitter.setImagesX(imagesX);
 | 
			
		||||
        emitter.setImagesY(imagesY);
 | 
			
		||||
        emitter.setEndColor(endColor);
 | 
			
		||||
        emitter.setStartColor(startColor);
 | 
			
		||||
        emitter.getParticleInfluencer().setInitialVelocity(initialVelocity);
 | 
			
		||||
        emitter.setStartSize(startSize);
 | 
			
		||||
        emitter.setEndSize(endSize);
 | 
			
		||||
        emitter.setLowLife(lowLife);
 | 
			
		||||
        emitter.setHighLife(highLife);
 | 
			
		||||
        emitter.getParticleInfluencer().setVelocityVariation(velocityVariation);
 | 
			
		||||
        emitter.setGravity(gravity);
 | 
			
		||||
 | 
			
		||||
        return emitter;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -0,0 +1,114 @@
 | 
			
		||||
package pp.battleship.client;
 | 
			
		||||
 | 
			
		||||
import com.jme3.app.Application;
 | 
			
		||||
import com.jme3.app.state.AbstractAppState;
 | 
			
		||||
import com.jme3.app.state.AppStateManager;
 | 
			
		||||
import com.jme3.audio.AudioNode;
 | 
			
		||||
 | 
			
		||||
import java.util.prefs.Preferences;
 | 
			
		||||
 | 
			
		||||
import static pp.JmeUtil.loadSound;
 | 
			
		||||
import static pp.util.PreferencesUtils.getPreferences;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * An application state that plays sounds.
 | 
			
		||||
 */
 | 
			
		||||
public class GameMusic extends AbstractAppState {
 | 
			
		||||
    private static final Preferences PREFERENCES = getPreferences(GameMusic.class);
 | 
			
		||||
    private static final String ENABLED_PREF = "enabled";
 | 
			
		||||
    private static final String VOLUME_PREF = "volume";
 | 
			
		||||
 | 
			
		||||
    private static final String MUSIC_PATH = "Sound/background.wav";
 | 
			
		||||
 | 
			
		||||
    private AudioNode backgroundMusic;
 | 
			
		||||
    private float volume;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Returns whether the music is enabled in the user preferences.
 | 
			
		||||
     *
 | 
			
		||||
     * @return true if music is enabled, false otherwise
 | 
			
		||||
     */
 | 
			
		||||
    public static boolean enabledInPreferences() {
 | 
			
		||||
        return PREFERENCES.getBoolean(ENABLED_PREF, true);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Returns the music volume level stored in the user preferences.
 | 
			
		||||
     *
 | 
			
		||||
     * @return the volume level as a float (default is 0.5f)
 | 
			
		||||
     */
 | 
			
		||||
    public static float volumeInPreferences() {
 | 
			
		||||
        return PREFERENCES.getFloat(VOLUME_PREF, 0.5f);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Initializes the game music system
 | 
			
		||||
     *
 | 
			
		||||
     * @param stateManager the state manager of the game
 | 
			
		||||
     * @param app          the main application
 | 
			
		||||
     */
 | 
			
		||||
    @Override
 | 
			
		||||
    public void initialize(AppStateManager stateManager, Application app) {
 | 
			
		||||
        super.initialize(stateManager, app);
 | 
			
		||||
        backgroundMusic = loadSound(app, MUSIC_PATH);
 | 
			
		||||
        setMusicVolume(volumeInPreferences());
 | 
			
		||||
        if (isEnabled()) playMusic();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 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 (enabled && !isEnabled()) {
 | 
			
		||||
            playMusic();
 | 
			
		||||
        }
 | 
			
		||||
        else if (!enabled && isEnabled()) {
 | 
			
		||||
            stopMusic();
 | 
			
		||||
        }
 | 
			
		||||
        super.setEnabled(enabled);
 | 
			
		||||
        PREFERENCES.putBoolean(ENABLED_PREF, enabled);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Plays the background music.
 | 
			
		||||
     */
 | 
			
		||||
    public void playMusic() {
 | 
			
		||||
        if (backgroundMusic != null) {
 | 
			
		||||
            backgroundMusic.play();
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Stops background music.
 | 
			
		||||
     */
 | 
			
		||||
    public void stopMusic() {
 | 
			
		||||
        if (backgroundMusic != null) {
 | 
			
		||||
            backgroundMusic.stop();
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Sets the volume of the background music and saves the volume setting in user preferences.
 | 
			
		||||
     *
 | 
			
		||||
     * @param volume the volume level to set (0.0f to 1.0f)
 | 
			
		||||
     */
 | 
			
		||||
    public void setMusicVolume(float volume) {
 | 
			
		||||
        if (backgroundMusic != null) {
 | 
			
		||||
            backgroundMusic.setVolume(volume);
 | 
			
		||||
            this.volume = volume;
 | 
			
		||||
            PREFERENCES.putFloat(VOLUME_PREF, volume);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Returns volume stored in class
 | 
			
		||||
     * @return volume
 | 
			
		||||
     */
 | 
			
		||||
    public float getVolume() {
 | 
			
		||||
        return this.volume;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -10,9 +10,6 @@
 | 
			
		||||
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;
 | 
			
		||||
@@ -21,6 +18,7 @@
 | 
			
		||||
import java.lang.System.Logger.Level;
 | 
			
		||||
import java.util.prefs.Preferences;
 | 
			
		||||
 | 
			
		||||
import static pp.JmeUtil.loadSound;
 | 
			
		||||
import static pp.util.PreferencesUtils.getPreferences;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
@@ -34,6 +32,7 @@ public class GameSound extends AbstractAppState implements GameEventListener {
 | 
			
		||||
    private AudioNode splashSound;
 | 
			
		||||
    private AudioNode shipDestroyedSound;
 | 
			
		||||
    private AudioNode explosionSound;
 | 
			
		||||
    private AudioNode shellFlyingSound;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Checks if sound is enabled in the preferences.
 | 
			
		||||
@@ -78,26 +77,16 @@ public void initialize(AppStateManager stateManager, Application 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
 | 
			
		||||
        shellFlyingSound = loadSound(app, "Sound/Effects/shell_flying.wav");
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Loads a sound from the specified file.
 | 
			
		||||
     *
 | 
			
		||||
     * @param app  The application
 | 
			
		||||
     * @param name The name of the sound file.
 | 
			
		||||
     * @return The loaded AudioNode.
 | 
			
		||||
     * Plays the shell flying sound effect.
 | 
			
		||||
     */
 | 
			
		||||
    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;
 | 
			
		||||
    public void shellFly() {
 | 
			
		||||
        if (isEnabled() && shellFlyingSound != null) {
 | 
			
		||||
            shellFlyingSound.playInstance();
 | 
			
		||||
        }
 | 
			
		||||
        catch (AssetLoadException | AssetNotFoundException ex) {
 | 
			
		||||
            LOGGER.log(Level.ERROR, ex.getMessage(), ex);
 | 
			
		||||
        }
 | 
			
		||||
        return null;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
@@ -130,6 +119,7 @@ public void receivedEvent(SoundEvent event) {
 | 
			
		||||
            case EXPLOSION -> explosion();
 | 
			
		||||
            case SPLASH -> splash();
 | 
			
		||||
            case DESTROYED_SHIP -> shipDestroyed();
 | 
			
		||||
            case SHELL_FLYING -> shellFly();
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -33,6 +33,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.
 | 
			
		||||
@@ -42,9 +43,14 @@ class Menu extends Dialog {
 | 
			
		||||
    public Menu(BattleshipApp app) {
 | 
			
		||||
        super(app.getDialogManager());
 | 
			
		||||
        this.app = app;
 | 
			
		||||
        volumeSlider = new VolumeSlider(app.getStateManager().getState(GameMusic.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.music-enabled"), new StateCheckboxModel(app, GameMusic.class)));
 | 
			
		||||
        addChild(volumeSlider);
 | 
			
		||||
 | 
			
		||||
        addChild(loadButton)
 | 
			
		||||
                .addClickCommands(s -> ifTopDialog(this::loadDialog));
 | 
			
		||||
        addChild(saveButton)
 | 
			
		||||
@@ -65,6 +71,11 @@ public void update() {
 | 
			
		||||
        saveButton.setEnabled(app.getGameLogic().maySaveMap());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void update(float delta) {
 | 
			
		||||
        volumeSlider.update();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * As an escape action, this method closes the menu if it is the top dialog.
 | 
			
		||||
     */
 | 
			
		||||
 
 | 
			
		||||
@@ -7,10 +7,12 @@
 | 
			
		||||
 | 
			
		||||
package pp.battleship.client;
 | 
			
		||||
 | 
			
		||||
import com.simsilica.lemur.Checkbox;
 | 
			
		||||
import com.simsilica.lemur.Container;
 | 
			
		||||
import com.simsilica.lemur.Label;
 | 
			
		||||
import com.simsilica.lemur.TextField;
 | 
			
		||||
import com.simsilica.lemur.component.SpringGridLayout;
 | 
			
		||||
import pp.battleship.client.clienthost.BattleshipServerClient;
 | 
			
		||||
import pp.dialog.Dialog;
 | 
			
		||||
import pp.dialog.DialogBuilder;
 | 
			
		||||
import pp.dialog.SimpleDialog;
 | 
			
		||||
@@ -36,7 +38,10 @@ class NetworkDialog extends SimpleDialog {
 | 
			
		||||
    private String hostname;
 | 
			
		||||
    private int portNumber;
 | 
			
		||||
    private Future<Object> connectionFuture;
 | 
			
		||||
    private Future<Object> serverFuture;
 | 
			
		||||
    private Dialog progressDialog;
 | 
			
		||||
    private BattleshipServerClient server;
 | 
			
		||||
    private final Checkbox clientHostCheckbox;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Constructs a new NetworkDialog.
 | 
			
		||||
@@ -56,7 +61,8 @@ class NetworkDialog extends SimpleDialog {
 | 
			
		||||
        input.addChild(host, 1);
 | 
			
		||||
        input.addChild(new Label(lookup("port.number") + ":  "));
 | 
			
		||||
        input.addChild(port, 1);
 | 
			
		||||
 | 
			
		||||
        clientHostCheckbox = new Checkbox("Host Server");
 | 
			
		||||
        input.addChild(clientHostCheckbox);
 | 
			
		||||
        DialogBuilder.simple(app.getDialogManager())
 | 
			
		||||
                     .setTitle(lookup("server.dialog"))
 | 
			
		||||
                     .setExtension(d -> d.addChild(input))
 | 
			
		||||
@@ -77,11 +83,22 @@ private void connect() {
 | 
			
		||||
            hostname = host.getText().trim().isEmpty() ? LOCALHOST : host.getText();
 | 
			
		||||
            portNumber = Integer.parseInt(port.getText());
 | 
			
		||||
            openProgressDialog();
 | 
			
		||||
 | 
			
		||||
            if (clientHostCheckbox.isChecked()) {
 | 
			
		||||
                serverFuture = network.getApp().getExecutor().submit(this::initServer);
 | 
			
		||||
 | 
			
		||||
                while (server == null || !server.isReady()) {
 | 
			
		||||
                    Thread.sleep(100);
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
            connectionFuture = network.getApp().getExecutor().submit(this::initNetwork);
 | 
			
		||||
        }
 | 
			
		||||
        catch (NumberFormatException e) {
 | 
			
		||||
            network.getApp().errorDialog(lookup("port.must.be.integer"));
 | 
			
		||||
        }
 | 
			
		||||
        catch (InterruptedException e) {
 | 
			
		||||
            throw new RuntimeException(e);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
@@ -109,6 +126,23 @@ private Object initNetwork() {
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Tries to initialize the server hosted by the client.
 | 
			
		||||
     *
 | 
			
		||||
     * @throws RuntimeException If an error occurs when starting the server.
 | 
			
		||||
     */
 | 
			
		||||
    private Object initServer() {
 | 
			
		||||
        try {
 | 
			
		||||
            server = new BattleshipServerClient();
 | 
			
		||||
            server.run(Integer.parseInt(port.getText()));
 | 
			
		||||
            return null;
 | 
			
		||||
        }
 | 
			
		||||
        catch (Exception e) {
 | 
			
		||||
            LOGGER.log(Level.ERROR, "Error while starting server", e);
 | 
			
		||||
            throw new RuntimeException(e);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * This method is called by {@linkplain pp.dialog.DialogManager#update(float)} for periodically
 | 
			
		||||
     * updating this dialog. T
 | 
			
		||||
@@ -127,6 +161,19 @@ public void update(float delta) {
 | 
			
		||||
                LOGGER.log(Level.WARNING, "Interrupted!", e); //NON-NLS
 | 
			
		||||
                Thread.currentThread().interrupt();
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
        if (serverFuture != null && serverFuture.isDone()) {
 | 
			
		||||
            try {
 | 
			
		||||
                serverFuture.get();
 | 
			
		||||
            }
 | 
			
		||||
            catch (ExecutionException e) {
 | 
			
		||||
                LOGGER.log(Level.ERROR, "Failed to start server", e.getCause());
 | 
			
		||||
            }
 | 
			
		||||
            catch (InterruptedException e) {
 | 
			
		||||
                LOGGER.log(Level.WARNING, "Server thread was interrupted", e);
 | 
			
		||||
                Thread.currentThread().interrupt();
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
 
 | 
			
		||||
@@ -0,0 +1,38 @@
 | 
			
		||||
package pp.battleship.client;
 | 
			
		||||
 | 
			
		||||
import com.simsilica.lemur.Slider;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Represents a volume slider for controlling the background music volume in the Battleship game.
 | 
			
		||||
 * This class extends the {@link Slider} class and interfaces with the {@link GameMusic} instance
 | 
			
		||||
 * to adjust the volume settings based on user input.
 | 
			
		||||
 */
 | 
			
		||||
public class VolumeSlider extends Slider {
 | 
			
		||||
    private final GameMusic gameMusic;
 | 
			
		||||
    private float volume;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Constructs a new VolumeSlider instance and initializes it with the current volume level
 | 
			
		||||
     * from the game music preferences.
 | 
			
		||||
     *
 | 
			
		||||
     * @param gameMusic the instance of {@link GameMusic} to control music volume
 | 
			
		||||
     */
 | 
			
		||||
    public VolumeSlider(GameMusic gameMusic) {
 | 
			
		||||
        super();
 | 
			
		||||
        this.gameMusic = gameMusic;
 | 
			
		||||
        volume = gameMusic.getVolume();
 | 
			
		||||
        getModel().setPercent(volume);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Updates the volume setting based on the current slider position.
 | 
			
		||||
     * If the slider's percent value has changed, it updates the music volume
 | 
			
		||||
     * in the associated {@link GameMusic} instance.
 | 
			
		||||
     */
 | 
			
		||||
    public void update() {
 | 
			
		||||
        if (getModel().getPercent() != volume) {
 | 
			
		||||
            this.volume = (float) getModel().getPercent();
 | 
			
		||||
            gameMusic.setMusicVolume(volume);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -0,0 +1,205 @@
 | 
			
		||||
////////////////////////////////////////
 | 
			
		||||
// Programming project code
 | 
			
		||||
// UniBw M, 2022, 2023, 2024
 | 
			
		||||
// www.unibw.de/inf2
 | 
			
		||||
// (c) Mark Minas (mark.minas@unibw.de)
 | 
			
		||||
////////////////////////////////////////
 | 
			
		||||
 | 
			
		||||
package pp.battleship.client.clienthost;
 | 
			
		||||
 | 
			
		||||
import com.jme3.network.ConnectionListener;
 | 
			
		||||
import com.jme3.network.HostedConnection;
 | 
			
		||||
import com.jme3.network.Message;
 | 
			
		||||
import com.jme3.network.MessageListener;
 | 
			
		||||
import com.jme3.network.Network;
 | 
			
		||||
import com.jme3.network.Server;
 | 
			
		||||
import com.jme3.network.serializing.Serializer;
 | 
			
		||||
import pp.battleship.BattleshipConfig;
 | 
			
		||||
import pp.battleship.game.server.Player;
 | 
			
		||||
import pp.battleship.game.server.ServerGameLogic;
 | 
			
		||||
import pp.battleship.game.server.ServerSender;
 | 
			
		||||
import pp.battleship.message.client.AnimationFinishedMessage;
 | 
			
		||||
import pp.battleship.message.client.ClientMessage;
 | 
			
		||||
import pp.battleship.message.client.MapMessage;
 | 
			
		||||
import pp.battleship.message.client.ShootMessage;
 | 
			
		||||
import pp.battleship.message.server.EffectMessage;
 | 
			
		||||
import pp.battleship.message.server.GameDetails;
 | 
			
		||||
import pp.battleship.message.server.ServerMessage;
 | 
			
		||||
import pp.battleship.message.server.StartBattleMessage;
 | 
			
		||||
import pp.battleship.model.Battleship;
 | 
			
		||||
import pp.battleship.model.IntPoint;
 | 
			
		||||
import pp.battleship.model.Shot;
 | 
			
		||||
 | 
			
		||||
import java.io.File;
 | 
			
		||||
import java.io.FileInputStream;
 | 
			
		||||
import java.io.IOException;
 | 
			
		||||
import java.lang.System.Logger;
 | 
			
		||||
import java.lang.System.Logger.Level;
 | 
			
		||||
import java.util.concurrent.BlockingQueue;
 | 
			
		||||
import java.util.concurrent.LinkedBlockingQueue;
 | 
			
		||||
import java.util.logging.LogManager;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Server implementing the visitor pattern as MessageReceiver for ClientMessages
 | 
			
		||||
 */
 | 
			
		||||
public class BattleshipServerClient implements MessageListener<HostedConnection>, ConnectionListener, ServerSender {
 | 
			
		||||
    private static final Logger LOGGER = System.getLogger(BattleshipServerClient.class.getName());
 | 
			
		||||
    private static final File CONFIG_FILE = new File("server.properties");
 | 
			
		||||
 | 
			
		||||
    private final BattleshipConfig config = new BattleshipConfig();
 | 
			
		||||
    private Server myServer;
 | 
			
		||||
    private final ServerGameLogic logic;
 | 
			
		||||
    private final BlockingQueue<ReceivedMessageClient> pendingMessages = new LinkedBlockingQueue<>();
 | 
			
		||||
 | 
			
		||||
    static {
 | 
			
		||||
        // Configure logging
 | 
			
		||||
        LogManager manager = LogManager.getLogManager();
 | 
			
		||||
        try {
 | 
			
		||||
            manager.readConfiguration(new FileInputStream("logging.properties"));
 | 
			
		||||
            LOGGER.log(Level.INFO, "Successfully read logging properties"); //NON-NLS
 | 
			
		||||
        }
 | 
			
		||||
        catch (IOException e) {
 | 
			
		||||
            LOGGER.log(Level.INFO, e.getMessage());
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Creates the server and reads the configuration from the specified file.
 | 
			
		||||
     * Initializes the game logic and sets up logging.
 | 
			
		||||
     */
 | 
			
		||||
    public BattleshipServerClient() {
 | 
			
		||||
        config.readFromIfExists(CONFIG_FILE);
 | 
			
		||||
        LOGGER.log(Level.INFO, "Configuration: {0}", config); //NON-NLS
 | 
			
		||||
        logic = new ServerGameLogic(this, config);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Checks if the server is ready.
 | 
			
		||||
     *
 | 
			
		||||
     * @return true if the server is running, false otherwise
 | 
			
		||||
     */
 | 
			
		||||
    public boolean isReady() {
 | 
			
		||||
        return myServer != null && myServer.isRunning();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Starts the server and continuously processes incoming messages.
 | 
			
		||||
     */
 | 
			
		||||
    public void run(int port) {
 | 
			
		||||
        startServer(port);
 | 
			
		||||
        while (true)
 | 
			
		||||
            processNextMessage();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Starts the server by creating a network server on the specified port.
 | 
			
		||||
     */
 | 
			
		||||
    private void startServer(int port) {
 | 
			
		||||
        try {
 | 
			
		||||
            LOGGER.log(Level.INFO, "Starting server..."); //NON-NLS
 | 
			
		||||
            myServer = Network.createServer(port);
 | 
			
		||||
            initializeSerializables();
 | 
			
		||||
            myServer.start();
 | 
			
		||||
            registerListeners();
 | 
			
		||||
            LOGGER.log(Level.INFO, "Server started: {0}", myServer.isRunning()); //NON-NLS
 | 
			
		||||
        }
 | 
			
		||||
        catch (IOException e) {
 | 
			
		||||
            LOGGER.log(Level.ERROR, "Couldn't start server: {0}", e.getMessage()); //NON-NLS
 | 
			
		||||
            exit(1);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Processes the next message in the queue.
 | 
			
		||||
     */
 | 
			
		||||
    private void processNextMessage() {
 | 
			
		||||
        try {
 | 
			
		||||
            pendingMessages.take().process(logic);
 | 
			
		||||
        }
 | 
			
		||||
        catch (InterruptedException ex) {
 | 
			
		||||
            LOGGER.log(Level.INFO, "Interrupted while waiting for messages"); //NON-NLS
 | 
			
		||||
            Thread.currentThread().interrupt();
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Registers all serializable message classes for network transmission.
 | 
			
		||||
     */
 | 
			
		||||
    private void initializeSerializables() {
 | 
			
		||||
        Serializer.registerClass(GameDetails.class);
 | 
			
		||||
        Serializer.registerClass(StartBattleMessage.class);
 | 
			
		||||
        Serializer.registerClass(MapMessage.class);
 | 
			
		||||
        Serializer.registerClass(ShootMessage.class);
 | 
			
		||||
        Serializer.registerClass(EffectMessage.class);
 | 
			
		||||
        Serializer.registerClass(AnimationFinishedMessage.class);
 | 
			
		||||
        Serializer.registerClass(Battleship.class);
 | 
			
		||||
        Serializer.registerClass(IntPoint.class);
 | 
			
		||||
        Serializer.registerClass(Shot.class);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Registers the message and connection listeners for the server.
 | 
			
		||||
     */
 | 
			
		||||
    private void registerListeners() {
 | 
			
		||||
        myServer.addMessageListener(this, MapMessage.class);
 | 
			
		||||
        myServer.addMessageListener(this, ShootMessage.class);
 | 
			
		||||
        myServer.addMessageListener(this, AnimationFinishedMessage.class);
 | 
			
		||||
        myServer.addConnectionListener(this);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void messageReceived(HostedConnection source, Message message) {
 | 
			
		||||
        LOGGER.log(Level.INFO, "message received from {0}: {1}", source.getId(), message); //NON-NLS
 | 
			
		||||
        if (message instanceof ClientMessage clientMessage)
 | 
			
		||||
            pendingMessages.add(new ReceivedMessageClient(clientMessage, source.getId()));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void connectionAdded(Server server, HostedConnection hostedConnection) {
 | 
			
		||||
        LOGGER.log(Level.INFO, "new connection {0}", hostedConnection); //NON-NLS
 | 
			
		||||
        logic.addPlayer(hostedConnection.getId());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void connectionRemoved(Server server, HostedConnection hostedConnection) {
 | 
			
		||||
        LOGGER.log(Level.INFO, "connection closed: {0}", hostedConnection); //NON-NLS
 | 
			
		||||
        final Player player = logic.getPlayerById(hostedConnection.getId());
 | 
			
		||||
        if (player == null)
 | 
			
		||||
            LOGGER.log(Level.INFO, "closed connection does not belong to an active player"); //NON-NLS
 | 
			
		||||
        else { //NON-NLS
 | 
			
		||||
            LOGGER.log(Level.INFO, "closed connection belongs to {0}", player); //NON-NLS
 | 
			
		||||
            exit(0);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Shuts down the server and closes all active connections.
 | 
			
		||||
     *
 | 
			
		||||
     * @param exitValue the exit code to terminate the program with
 | 
			
		||||
     */
 | 
			
		||||
    private void exit(int exitValue) { //NON-NLS
 | 
			
		||||
        LOGGER.log(Level.INFO, "close request"); //NON-NLS
 | 
			
		||||
        if (myServer != null)
 | 
			
		||||
            for (HostedConnection client : myServer.getConnections()) //NON-NLS
 | 
			
		||||
                if (client != null) client.close("Game over"); //NON-NLS
 | 
			
		||||
        System.exit(exitValue);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Send the specified message to the specified connection.
 | 
			
		||||
     *
 | 
			
		||||
     * @param id      the connection id
 | 
			
		||||
     * @param message the message
 | 
			
		||||
     */
 | 
			
		||||
    public void send(int id, ServerMessage message) {
 | 
			
		||||
        if (myServer == null || !myServer.isRunning()) {
 | 
			
		||||
            LOGGER.log(Level.ERROR, "no server running when trying to send {0}", message); //NON-NLS
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
        final HostedConnection connection = myServer.getConnection(id);
 | 
			
		||||
        if (connection != null)
 | 
			
		||||
            connection.send(message);
 | 
			
		||||
        else
 | 
			
		||||
            LOGGER.log(Level.ERROR, "there is no connection with id={0}", id); //NON-NLS
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -0,0 +1,17 @@
 | 
			
		||||
////////////////////////////////////////
 | 
			
		||||
// Programming project code
 | 
			
		||||
// UniBw M, 2022, 2023, 2024
 | 
			
		||||
// www.unibw.de/inf2
 | 
			
		||||
// (c) Mark Minas (mark.minas@unibw.de)
 | 
			
		||||
////////////////////////////////////////
 | 
			
		||||
 | 
			
		||||
package pp.battleship.client.clienthost;
 | 
			
		||||
 | 
			
		||||
import pp.battleship.message.client.ClientInterpreter;
 | 
			
		||||
import pp.battleship.message.client.ClientMessage;
 | 
			
		||||
 | 
			
		||||
record ReceivedMessageClient(ClientMessage message, int from) {
 | 
			
		||||
    void process(ClientInterpreter interpreter) {
 | 
			
		||||
        message.accept(interpreter, from);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -28,7 +28,7 @@
 | 
			
		||||
 * and interaction between the model and the view.
 | 
			
		||||
 */
 | 
			
		||||
class MapView {
 | 
			
		||||
    private static final float FIELD_SIZE = 40f;
 | 
			
		||||
    public static final float FIELD_SIZE = 40f;
 | 
			
		||||
    private static final float GRID_LINE_WIDTH = 2f;
 | 
			
		||||
    private static final float BACKGROUND_DEPTH = -4f;
 | 
			
		||||
    private static final float GRID_DEPTH = -1f;
 | 
			
		||||
 
 | 
			
		||||
@@ -7,14 +7,21 @@
 | 
			
		||||
 | 
			
		||||
package pp.battleship.client.gui;
 | 
			
		||||
 | 
			
		||||
import com.jme3.material.Material;
 | 
			
		||||
import com.jme3.material.RenderState;
 | 
			
		||||
import com.jme3.material.RenderState.BlendMode;
 | 
			
		||||
import com.jme3.math.ColorRGBA;
 | 
			
		||||
import com.jme3.scene.Geometry;
 | 
			
		||||
import com.jme3.scene.Node;
 | 
			
		||||
import com.jme3.scene.Spatial;
 | 
			
		||||
import com.jme3.scene.shape.Sphere;
 | 
			
		||||
import pp.battleship.model.Battleship;
 | 
			
		||||
import pp.battleship.model.Shell;
 | 
			
		||||
import pp.battleship.model.Shot;
 | 
			
		||||
import pp.util.Position;
 | 
			
		||||
 | 
			
		||||
import static com.jme3.material.Materials.UNSHADED;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Synchronizes the visual representation of the ship map with the game model.
 | 
			
		||||
 * It handles the rendering of ships and shots on the map view, updating the view
 | 
			
		||||
@@ -25,6 +32,7 @@ class MapViewSynchronizer extends ShipMapSynchronizer {
 | 
			
		||||
    private static final float SHIP_LINE_WIDTH = 6f;
 | 
			
		||||
    private static final float SHOT_DEPTH = -2f;
 | 
			
		||||
    private static final float SHIP_DEPTH = 0f;
 | 
			
		||||
    private static final float SHELL_DEPTH = 1f;
 | 
			
		||||
    private static final float INDENT = 4f;
 | 
			
		||||
 | 
			
		||||
    // Colors used for different visual elements
 | 
			
		||||
@@ -109,6 +117,26 @@ public Spatial visit(Battleship ship) {
 | 
			
		||||
        return shipNode;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Creates and returns a Spatial representation of the given {@code Shell} object
 | 
			
		||||
     * for 2D visualization in the game. The shell is represented as a circle.
 | 
			
		||||
     *
 | 
			
		||||
     * @param shell The {@code Shell} object to be visualized.
 | 
			
		||||
     * @return A {@code Spatial} object representing the shell on the map.
 | 
			
		||||
     */
 | 
			
		||||
    @Override
 | 
			
		||||
    public Spatial visit(Shell shell) {
 | 
			
		||||
        final ColorRGBA color = ColorRGBA.Black;
 | 
			
		||||
        Geometry ellipse = new Geometry("ellipse", new Sphere(50, 50, MapView.FIELD_SIZE / 2 * 0.8f));
 | 
			
		||||
        Material mat = new Material(view.getApp().getAssetManager(), UNSHADED); //NON-NLS
 | 
			
		||||
        mat.getAdditionalRenderState().setBlendMode(BlendMode.Alpha);
 | 
			
		||||
        mat.getAdditionalRenderState().setFaceCullMode(RenderState.FaceCullMode.Off);
 | 
			
		||||
        mat.setColor("Color", color);
 | 
			
		||||
        ellipse.setMaterial(mat);
 | 
			
		||||
        ellipse.addControl(new Shell2DControl(view, shell));
 | 
			
		||||
        return ellipse;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Creates a line geometry representing part of the ship's border.
 | 
			
		||||
     *
 | 
			
		||||
 
 | 
			
		||||
@@ -19,10 +19,15 @@
 | 
			
		||||
import pp.battleship.client.BattleshipApp;
 | 
			
		||||
import pp.battleship.model.Battleship;
 | 
			
		||||
import pp.battleship.model.Rotation;
 | 
			
		||||
import pp.battleship.model.Shell;
 | 
			
		||||
import pp.battleship.model.ShipMap;
 | 
			
		||||
import pp.battleship.model.Shot;
 | 
			
		||||
 | 
			
		||||
import java.util.Timer;
 | 
			
		||||
import java.util.TimerTask;
 | 
			
		||||
 | 
			
		||||
import static java.util.Objects.requireNonNull;
 | 
			
		||||
import static pp.JmeUtil.mapToWorldCord;
 | 
			
		||||
import static pp.util.FloatMath.HALF_PI;
 | 
			
		||||
import static pp.util.FloatMath.PI;
 | 
			
		||||
 | 
			
		||||
@@ -34,7 +39,7 @@
 | 
			
		||||
 */
 | 
			
		||||
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 LIGHTING = "Common/MatDefs/Light/Lighting.j3md";
 | 
			
		||||
    private static final String COLOR = "Color"; //NON-NLS
 | 
			
		||||
    private static final String SHIP = "ship"; //NON-NLS
 | 
			
		||||
    private static final String SHOT = "shot"; //NON-NLS
 | 
			
		||||
@@ -69,7 +74,11 @@ public SeaSynchronizer(BattleshipApp app, Node root, ShipMap map) {
 | 
			
		||||
     */
 | 
			
		||||
    @Override
 | 
			
		||||
    public Spatial visit(Shot shot) {
 | 
			
		||||
        return shot.isHit() ? handleHit(shot) : createCylinder(shot);
 | 
			
		||||
        return shot.isHit() ? handleHit(shot) : handleMiss(shot);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private Spatial handleMiss(Shot shot) {
 | 
			
		||||
        return app.getEffectHandler().waterSplash(mapToWorldCord(shot.getX(), shot.getY()));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
@@ -83,14 +92,24 @@ public Spatial visit(Shot shot) {
 | 
			
		||||
    private Spatial handleHit(Shot shot) {
 | 
			
		||||
        final Battleship ship = requireNonNull(map.findShipAt(shot), "Missing ship");
 | 
			
		||||
        final Node shipNode = requireNonNull((Node) getSpatial(ship), "Missing ship node");
 | 
			
		||||
        shipNode.getControl(ShipControl.class).hit(shot);
 | 
			
		||||
        if (ship.isDestroyed()) {
 | 
			
		||||
            shipNode.getControl(ShipControl.class).destroyed();
 | 
			
		||||
 | 
			
		||||
        final Geometry representation = createCylinder(shot);
 | 
			
		||||
        representation.getLocalTranslation().subtractLocal(shipNode.getLocalTranslation());
 | 
			
		||||
        shipNode.attachChild(representation);
 | 
			
		||||
 | 
			
		||||
            new Timer().schedule(new TimerTask() {
 | 
			
		||||
                @Override
 | 
			
		||||
                public void run() {
 | 
			
		||||
                    handleShipDestroy(shipNode);
 | 
			
		||||
                }
 | 
			
		||||
            }, 10000);
 | 
			
		||||
        }
 | 
			
		||||
        return null;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private void handleShipDestroy(Node shipNode) {
 | 
			
		||||
        shipNode.getParent().detachChild(shipNode);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Creates a cylinder geometry representing the specified shot.
 | 
			
		||||
     * The appearance of the cylinder depends on whether the shot is a hit or a miss.
 | 
			
		||||
@@ -129,10 +148,31 @@ public Spatial visit(Battleship ship) {
 | 
			
		||||
        final float x = 0.5f * (ship.getMinY() + ship.getMaxY() + 1f);
 | 
			
		||||
        final float z = 0.5f * (ship.getMinX() + ship.getMaxX() + 1f);
 | 
			
		||||
        node.setLocalTranslation(x, 0f, z);
 | 
			
		||||
        node.addControl(new ShipControl(ship));
 | 
			
		||||
        node.addControl(new ShipControl(ship, node, app.getEffectHandler()));
 | 
			
		||||
        return node;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Creates and returns a 3D model representation of the given {@code Shell} object
 | 
			
		||||
     * for visualization in the game.
 | 
			
		||||
     *
 | 
			
		||||
     * @param shell The {@code Shell} object to be visualized.
 | 
			
		||||
     * @return A {@code Spatial} object representing the 3D model of the shell.
 | 
			
		||||
     */
 | 
			
		||||
    @Override
 | 
			
		||||
    public Spatial visit(Shell shell) {
 | 
			
		||||
        final Spatial model = app.getAssetManager().loadModel("Models/Shell/shell.j3o");
 | 
			
		||||
        model.setLocalScale(.05f);
 | 
			
		||||
        model.setShadowMode(ShadowMode.CastAndReceive);
 | 
			
		||||
        Material mat = new Material(app.getAssetManager(), LIGHTING);
 | 
			
		||||
        mat.setTexture("DiffuseMap", app.getAssetManager().loadTexture("Models/Shell/shell_color.png"));
 | 
			
		||||
        mat.setReceivesShadows(true);
 | 
			
		||||
        model.setMaterial(mat);
 | 
			
		||||
 | 
			
		||||
        model.addControl(new ShellControl(shell));
 | 
			
		||||
        return model;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Creates the appropriate graphical representation of the specified battleship.
 | 
			
		||||
     * The representation is either a detailed model or a simple box based on the length of the ship.
 | 
			
		||||
@@ -141,7 +181,13 @@ public Spatial visit(Battleship ship) {
 | 
			
		||||
     * @return the spatial representing the battleship
 | 
			
		||||
     */
 | 
			
		||||
    private Spatial createShip(Battleship ship) {
 | 
			
		||||
        return ship.getLength() == 4 ? createBattleship(ship) : createBox(ship);
 | 
			
		||||
        return switch (ship.getLength()) {
 | 
			
		||||
            case 1 -> createBattleship(ship, ShipModel.SHIP1);
 | 
			
		||||
            case 2 -> createBattleship(ship, ShipModel.SHIP2);
 | 
			
		||||
            case 3 -> createBattleship(ship, ShipModel.SHIP3);
 | 
			
		||||
            case 4 -> createBattleship(ship, ShipModel.SHIP4);
 | 
			
		||||
            default -> createBox(ship);
 | 
			
		||||
        };
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
@@ -186,13 +232,23 @@ private Material createColoredMaterial(ColorRGBA color) {
 | 
			
		||||
     * @param ship the battleship to be represented
 | 
			
		||||
     * @return the spatial representing the "King George V" battleship
 | 
			
		||||
     */
 | 
			
		||||
    private Spatial createBattleship(Battleship ship) {
 | 
			
		||||
        final Spatial model = app.getAssetManager().loadModel(KING_GEORGE_V_MODEL);
 | 
			
		||||
    private Spatial createBattleship(Battleship ship, ShipModel shipModel) {
 | 
			
		||||
        final Spatial model = app.getAssetManager().loadModel(shipModel.getPath());
 | 
			
		||||
 | 
			
		||||
        model.rotate(-HALF_PI, calculateRotationAngle(ship.getRot()), 0f);
 | 
			
		||||
        model.scale(1.48f);
 | 
			
		||||
        model.scale(shipModel.getScale());
 | 
			
		||||
        model.setLocalTranslation(shipModel.getTranslation());
 | 
			
		||||
        model.setShadowMode(ShadowMode.CastAndReceive);
 | 
			
		||||
 | 
			
		||||
        Material mat = new Material(app.getAssetManager(), LIGHTING);
 | 
			
		||||
 | 
			
		||||
        String colorPath = shipModel.getColorPath();
 | 
			
		||||
        String bumpPath = shipModel.getBumpPath();
 | 
			
		||||
        if (colorPath != null) mat.setTexture("DiffuseMap", app.getAssetManager().loadTexture(colorPath));
 | 
			
		||||
        if (bumpPath != null) mat.setTexture("NormalMap", app.getAssetManager().loadTexture(bumpPath));
 | 
			
		||||
 | 
			
		||||
        mat.setReceivesShadows(true);
 | 
			
		||||
        model.setMaterial(mat);
 | 
			
		||||
        return model;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -0,0 +1,49 @@
 | 
			
		||||
package pp.battleship.client.gui;
 | 
			
		||||
 | 
			
		||||
import com.jme3.math.Vector3f;
 | 
			
		||||
import com.jme3.renderer.RenderManager;
 | 
			
		||||
import com.jme3.renderer.ViewPort;
 | 
			
		||||
import com.jme3.scene.control.AbstractControl;
 | 
			
		||||
import pp.battleship.model.Shell;
 | 
			
		||||
import pp.util.Position;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Controls the 2D representation of a {@code Shell} in the game, updating its position
 | 
			
		||||
 * based on the shell's current state in the game model. The {@code Shell2DControl} class
 | 
			
		||||
 * is responsible for translating the shell's 3D position to a 2D view position within
 | 
			
		||||
 * the game's map view.
 | 
			
		||||
 */
 | 
			
		||||
public class Shell2DControl extends AbstractControl {
 | 
			
		||||
    private final Shell shell;
 | 
			
		||||
    private final MapView view;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Constructs a new {@code Shell2DControl} to manage the 2D visualization of the given {@code Shell}.
 | 
			
		||||
     *
 | 
			
		||||
     * @param view  The {@code MapView} used to get information about the map to display.
 | 
			
		||||
     * @param shell The {@code Shell} being visualized.
 | 
			
		||||
     */
 | 
			
		||||
    public Shell2DControl(MapView view, Shell shell){
 | 
			
		||||
        this.shell = shell;
 | 
			
		||||
        this.view = view;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Updates the position of the shell's 2D representation based on the shell's current
 | 
			
		||||
     * 3D position in the game model. The position is mapped from model space to view space
 | 
			
		||||
     * coordinates and translated to the appropriate location within the {@code MapView}.
 | 
			
		||||
     *
 | 
			
		||||
     * @param tpf Time per frame, representing the time elapsed since the last frame.
 | 
			
		||||
     */
 | 
			
		||||
    @Override
 | 
			
		||||
    protected void controlUpdate(float tpf) {
 | 
			
		||||
        Vector3f shellPos = shell.getPosition();
 | 
			
		||||
        Position viewPos = view.modelToView(shellPos.x, shellPos.z);
 | 
			
		||||
        spatial.setLocalTranslation(viewPos.getX() + MapView.FIELD_SIZE / 2, viewPos.getY() + MapView.FIELD_SIZE / 2, 0);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    protected void controlRender(RenderManager rm, ViewPort vp) {
 | 
			
		||||
        // No rendering-specific behavior required for this control
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -0,0 +1,51 @@
 | 
			
		||||
package pp.battleship.client.gui;
 | 
			
		||||
 | 
			
		||||
import com.jme3.math.Vector3f;
 | 
			
		||||
import com.jme3.renderer.RenderManager;
 | 
			
		||||
import com.jme3.renderer.ViewPort;
 | 
			
		||||
import com.jme3.scene.control.AbstractControl;
 | 
			
		||||
import pp.battleship.model.Shell;
 | 
			
		||||
 | 
			
		||||
import static pp.JmeUtil.mapToWorldCord;
 | 
			
		||||
import static pp.util.FloatMath.PI;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Controls the 3D representation of a {@code Shell} in the game, updating its position
 | 
			
		||||
 * and rotation based on the shell's current state in the game model. The {@code ShellControl}
 | 
			
		||||
 * class ensures that the spatial associated with the shell is positioned and oriented correctly
 | 
			
		||||
 * within the world.
 | 
			
		||||
 */
 | 
			
		||||
public class ShellControl extends AbstractControl {
 | 
			
		||||
    private final Shell shell;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Constructs a new {@code ShellControl} to manage the 3D visualization of the given {@code Shell}.
 | 
			
		||||
     *
 | 
			
		||||
     * @param shell The {@code Shell} being visualized and controlled.
 | 
			
		||||
     */
 | 
			
		||||
    public ShellControl(Shell shell){
 | 
			
		||||
        super();
 | 
			
		||||
        this.shell = shell;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Updates the 3D position and rotation of the shell based on its current state.
 | 
			
		||||
     * Converts map coordinates to world coordinates and applies the shell's orientation.
 | 
			
		||||
     *
 | 
			
		||||
     * @param tpf Time per frame, representing the elapsed time since the last update.
 | 
			
		||||
     */
 | 
			
		||||
    @Override
 | 
			
		||||
    protected void controlUpdate(float tpf) {
 | 
			
		||||
        Vector3f pos = shell.getPosition();
 | 
			
		||||
        Vector3f fixed = mapToWorldCord(pos.x, pos.z);
 | 
			
		||||
        fixed.setY(pos.y);
 | 
			
		||||
        spatial.setLocalTranslation(fixed);
 | 
			
		||||
        spatial.setLocalRotation(shell.getRotation());
 | 
			
		||||
        spatial.rotate(PI/2,0,0);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    protected void controlRender(RenderManager rm, ViewPort vp) {
 | 
			
		||||
        // No rendering-specific behavior required for this control
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -11,96 +11,157 @@
 | 
			
		||||
import com.jme3.math.Vector3f;
 | 
			
		||||
import com.jme3.renderer.RenderManager;
 | 
			
		||||
import com.jme3.renderer.ViewPort;
 | 
			
		||||
import com.jme3.scene.Node;
 | 
			
		||||
import com.jme3.scene.control.AbstractControl;
 | 
			
		||||
import pp.battleship.client.EffectHandler;
 | 
			
		||||
import pp.battleship.model.Battleship;
 | 
			
		||||
import pp.battleship.model.Rotation;
 | 
			
		||||
import pp.battleship.model.Shot;
 | 
			
		||||
 | 
			
		||||
import java.util.Timer;
 | 
			
		||||
import java.util.TimerTask;
 | 
			
		||||
 | 
			
		||||
import static pp.JmeUtil.mapToWorldCord;
 | 
			
		||||
import static pp.util.FloatMath.DEG_TO_RAD;
 | 
			
		||||
import static pp.util.FloatMath.TWO_PI;
 | 
			
		||||
import static pp.util.FloatMath.sin;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Controls the oscillating pitch motion of a battleship model in the game.
 | 
			
		||||
 * The ship oscillates to simulate a realistic movement on water, based on its orientation and length.
 | 
			
		||||
 * Controls the pitch oscillation and sinking behavior of a {@code Battleship} model in the game.
 | 
			
		||||
 * The ship tilts back and forth to simulate movement on water, and can also be animated to sink
 | 
			
		||||
 * when destroyed.
 | 
			
		||||
 */
 | 
			
		||||
class ShipControl extends AbstractControl {
 | 
			
		||||
    /**
 | 
			
		||||
     * The axis of rotation for the ship's pitch (tilting forward and backward).
 | 
			
		||||
     */
 | 
			
		||||
    private static final float SINK_SPEED = 0.04f;
 | 
			
		||||
    private static final float SINK_ROT_SPEED = 0.1f;
 | 
			
		||||
 | 
			
		||||
    // The axis of rotation for the ship's pitch (tilting).
 | 
			
		||||
    private final Vector3f axis;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * The duration of one complete oscillation cycle in seconds.
 | 
			
		||||
     */
 | 
			
		||||
    // The duration of one oscillation cycle in seconds.
 | 
			
		||||
    private final float cycle;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * The amplitude of the pitch oscillation in radians, determining how much the ship tilts.
 | 
			
		||||
     */
 | 
			
		||||
    // The amplitude of the pitch oscillation in radians.
 | 
			
		||||
    private final float amplitude;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * A quaternion representing the ship's current pitch rotation.
 | 
			
		||||
     */
 | 
			
		||||
    // Quaternion representing the ship's pitch rotation.
 | 
			
		||||
    private final Quaternion pitch = new Quaternion();
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * The current time within the oscillation cycle, used to calculate the ship's pitch angle.
 | 
			
		||||
     */
 | 
			
		||||
    // The current time within the oscillation cycle.
 | 
			
		||||
    private float time;
 | 
			
		||||
 | 
			
		||||
    // Flag indicating if the ship is sinking.
 | 
			
		||||
    private boolean sinking;
 | 
			
		||||
 | 
			
		||||
    // The battleship being controlled.
 | 
			
		||||
    private final Battleship battleship;
 | 
			
		||||
 | 
			
		||||
    // Node representing the ship in the scene graph.
 | 
			
		||||
    private final Node shipNode;
 | 
			
		||||
 | 
			
		||||
    // Handles visual effects for the ship.
 | 
			
		||||
    private final EffectHandler effectHandler;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Constructs a new ShipControl instance for the specified Battleship.
 | 
			
		||||
     * The ship's orientation determines the axis of rotation, while its length influences
 | 
			
		||||
     * the cycle duration and amplitude of the oscillation.
 | 
			
		||||
     * Constructs a new {@code ShipControl} instance to manage the effects for a specified {@code Battleship}.
 | 
			
		||||
     * The ship's orientation determines the axis of rotation, and its length affects the oscillation cycle
 | 
			
		||||
     * and amplitude.
 | 
			
		||||
     *
 | 
			
		||||
     * @param ship the Battleship object to control
 | 
			
		||||
     * @param battleship    The {@code Battleship} being controlled.
 | 
			
		||||
     * @param shipNode      The scene graph node representing the ship.
 | 
			
		||||
     * @param effectHandler The {@code EffectHandler} for creating visual effects.
 | 
			
		||||
     */
 | 
			
		||||
    public ShipControl(Battleship ship) {
 | 
			
		||||
        // Determine the axis of rotation based on the ship's orientation
 | 
			
		||||
        axis = switch (ship.getRot()) {
 | 
			
		||||
    public ShipControl(Battleship battleship, Node shipNode, EffectHandler effectHandler) {
 | 
			
		||||
        this.battleship = battleship;
 | 
			
		||||
        this.shipNode = shipNode;
 | 
			
		||||
        this.effectHandler = effectHandler;
 | 
			
		||||
 | 
			
		||||
        sinking = false;
 | 
			
		||||
        // Determine the axis of rotation based on the ship's orientation.
 | 
			
		||||
        axis = switch (battleship.getRot()) {
 | 
			
		||||
            case LEFT, RIGHT -> Vector3f.UNIT_X;
 | 
			
		||||
            case UP, DOWN -> Vector3f.UNIT_Z;
 | 
			
		||||
        };
 | 
			
		||||
 | 
			
		||||
        // Set the cycle duration and amplitude based on the ship's length
 | 
			
		||||
        cycle = ship.getLength() * 2f;
 | 
			
		||||
        amplitude = 5f * DEG_TO_RAD / ship.getLength();
 | 
			
		||||
        // Set the cycle duration and amplitude based on the ship's length.
 | 
			
		||||
        cycle = battleship.getLength() * 2f;
 | 
			
		||||
        amplitude = 5f * DEG_TO_RAD / battleship.getLength();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Updates the ship's pitch oscillation each frame. The ship's pitch is adjusted
 | 
			
		||||
     * to create a continuous tilting motion, simulating the effect of waves.
 | 
			
		||||
     * Updates the ship's motion. If the ship is sinking, it animates the sinking process.
 | 
			
		||||
     * Otherwise, it oscillates the ship to simulate wave motion.
 | 
			
		||||
     *
 | 
			
		||||
     * @param tpf time per frame (in seconds), used to calculate the new pitch angle
 | 
			
		||||
     * @param tpf Time per frame, used to update the ship's motion.
 | 
			
		||||
     */
 | 
			
		||||
    @Override
 | 
			
		||||
    protected void controlUpdate(float tpf) {
 | 
			
		||||
        // If spatial is null, do nothing
 | 
			
		||||
        if (sinking) {
 | 
			
		||||
            handleSinking(tpf);
 | 
			
		||||
        }
 | 
			
		||||
        else {
 | 
			
		||||
            handlePitch(tpf);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // Handles the sinking animation.
 | 
			
		||||
    private void handleSinking(float tpf) {
 | 
			
		||||
        if (spatial == null) return;
 | 
			
		||||
 | 
			
		||||
        // Update the time within the oscillation cycle
 | 
			
		||||
        spatial.setLocalTranslation(spatial.getLocalTranslation().add(new Vector3f(0, -1, 0).mult(tpf * SINK_SPEED)));
 | 
			
		||||
        if (battleship.getRot() == Rotation.UP || battleship.getRot() == Rotation.DOWN) {
 | 
			
		||||
            spatial.rotate(tpf * SINK_ROT_SPEED, 0, 0);
 | 
			
		||||
        }
 | 
			
		||||
        else {
 | 
			
		||||
            spatial.rotate(0, 0, tpf * SINK_ROT_SPEED);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // Handles the pitch oscillation to simulate wave movement.
 | 
			
		||||
    private void handlePitch(float tpf) {
 | 
			
		||||
        if (spatial == null) return;
 | 
			
		||||
 | 
			
		||||
        // Update time in the oscillation cycle.
 | 
			
		||||
        time = (time + tpf) % cycle;
 | 
			
		||||
 | 
			
		||||
        // Calculate the current angle of the oscillation
 | 
			
		||||
        final float angle = amplitude * sin(time * TWO_PI / cycle);
 | 
			
		||||
        // Calculate the pitch angle.
 | 
			
		||||
        float angle = amplitude * sin(time * TWO_PI / cycle);
 | 
			
		||||
 | 
			
		||||
        // Update the pitch Quaternion with the new angle
 | 
			
		||||
        // Update pitch rotation.
 | 
			
		||||
        pitch.fromAngleAxis(angle, axis);
 | 
			
		||||
 | 
			
		||||
        // Apply the pitch rotation to the spatial
 | 
			
		||||
        // Apply rotation to the spatial.
 | 
			
		||||
        spatial.setLocalRotation(pitch);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * This method is called during the rendering phase, but it does not perform any
 | 
			
		||||
     * operations in this implementation as the control only influences the spatial's
 | 
			
		||||
     * transformation, not its rendering process.
 | 
			
		||||
     *
 | 
			
		||||
     * @param rm the RenderManager rendering the controlled Spatial (not null)
 | 
			
		||||
     * @param vp the ViewPort being rendered (not null)
 | 
			
		||||
     */
 | 
			
		||||
    @Override
 | 
			
		||||
    protected void controlRender(RenderManager rm, ViewPort vp) {
 | 
			
		||||
        // No rendering logic is needed for this control
 | 
			
		||||
        // No rendering-specific behavior required.
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Initiates the ship's sinking animation and schedules its destruction.
 | 
			
		||||
     */
 | 
			
		||||
    public void destroyed() {
 | 
			
		||||
        sinking = true;
 | 
			
		||||
        shipNode.attachChild(effectHandler.debrisSplash(shipNode.getLocalTranslation()));
 | 
			
		||||
        new Timer().schedule(new TimerTask() {
 | 
			
		||||
            @Override
 | 
			
		||||
            public void run() {
 | 
			
		||||
                effectHandler.destroyShip(battleship);
 | 
			
		||||
            }
 | 
			
		||||
        }, 4000);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Triggers an effect when the ship is hit by a shot, creating a fire effect at the impact location.
 | 
			
		||||
     *
 | 
			
		||||
     * @param shot The shot that hit the ship.
 | 
			
		||||
     */
 | 
			
		||||
    public void hit(Shot shot) {
 | 
			
		||||
        Vector3f shipNodePos = shipNode.getLocalTranslation();
 | 
			
		||||
        Vector3f shotWorld = mapToWorldCord(shot.getX(), shot.getY());
 | 
			
		||||
        Vector3f firePos = shotWorld.subtract(shipNodePos);
 | 
			
		||||
        shipNode.attachChild(effectHandler.createFire(firePos, battleship));
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -0,0 +1,82 @@
 | 
			
		||||
package pp.battleship.client.gui;
 | 
			
		||||
 | 
			
		||||
import com.jme3.math.Vector3f;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Enum representing different ship models for the Battleship game.
 | 
			
		||||
 * Each ship model has a corresponding 3D model path, scale, translation, color texture, and optional bump texture.
 | 
			
		||||
 */
 | 
			
		||||
public enum ShipModel {
 | 
			
		||||
    SHIP1("Models/Ships/1/ship1.j3o", 0.15f, new Vector3f(0f, 0f, 0f), "Models/Ships/1/ship1_color.png", null),
 | 
			
		||||
    SHIP2("Models/Ships/2/ship2.j3o", 0.03f, new Vector3f(0f, 0.2f, 0f), "Models/Ships/2/ship2.jpg", null),
 | 
			
		||||
    SHIP3("Models/Ships/3/ship3.j3o", 0.47f, new Vector3f(0f, -0.2f, 0f), "Models/Ships/3/ship3_color.jpg", null),
 | 
			
		||||
    SHIP4("Models/Ships/4/ship4.j3o", 1.48f, new Vector3f(0f, 0f, 0f), "Models/Ships/4/ship4_color.jpg", "Models/Ships/4/ship4_bump.jpg");
 | 
			
		||||
 | 
			
		||||
    private final String modelPath;
 | 
			
		||||
    private final float modelScale;
 | 
			
		||||
    private final Vector3f translation;
 | 
			
		||||
    private final String colorPath;
 | 
			
		||||
    private final String bumpPath;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Constructs a new ShipModel with the specified parameters.
 | 
			
		||||
     *
 | 
			
		||||
     * @param modelPath   the path to the 3D model of the ship
 | 
			
		||||
     * @param modelScale  the scale factor to be applied to the model
 | 
			
		||||
     * @param translation the translation to be applied to the model
 | 
			
		||||
     * @param colorPath   the path to the color texture of the model
 | 
			
		||||
     * @param bumpPath    the optional path to the bump texture of the model (may be null)
 | 
			
		||||
     */
 | 
			
		||||
    ShipModel(String modelPath, float modelScale, Vector3f translation, String colorPath, String bumpPath) {
 | 
			
		||||
        this.modelPath = modelPath;
 | 
			
		||||
        this.modelScale = modelScale;
 | 
			
		||||
        this.translation = translation;
 | 
			
		||||
        this.colorPath = colorPath;
 | 
			
		||||
        this.bumpPath = bumpPath;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Returns the path to the bump texture of the ship model.
 | 
			
		||||
     *
 | 
			
		||||
     * @return the bump texture path, or null if no bump texture is defined
 | 
			
		||||
     */
 | 
			
		||||
    public String getBumpPath() {
 | 
			
		||||
        return bumpPath;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Returns the path to the color texture of the ship model.
 | 
			
		||||
     *
 | 
			
		||||
     * @return the color texture path
 | 
			
		||||
     */
 | 
			
		||||
    public String getColorPath() {
 | 
			
		||||
        return colorPath;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Returns the scale factor of the ship model.
 | 
			
		||||
     *
 | 
			
		||||
     * @return the scale factor
 | 
			
		||||
     */
 | 
			
		||||
    public float getScale() {
 | 
			
		||||
        return modelScale;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Returns the path to the 3D model of the ship.
 | 
			
		||||
     *
 | 
			
		||||
     * @return the model path
 | 
			
		||||
     */
 | 
			
		||||
    public String getPath() {
 | 
			
		||||
        return modelPath;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Returns the translation to be applied to the ship model.
 | 
			
		||||
     *
 | 
			
		||||
     * @return the translation vector
 | 
			
		||||
     */
 | 
			
		||||
    public Vector3f getTranslation() {
 | 
			
		||||
        return translation;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
| 
		 After Width: | Height: | Size: 16 KiB  | 
| 
		 After Width: | Height: | Size: 46 KiB  | 
| 
		 After Width: | Height: | Size: 63 KiB  | 
| 
		 After Width: | Height: | Size: 2.0 KiB  | 
| 
		 After Width: | Height: | Size: 16 KiB  | 
| 
		 After Width: | Height: | Size: 32 KiB  | 
| 
		 After Width: | Height: | Size: 1.8 KiB  | 
| 
		 After Width: | Height: | Size: 28 KiB  | 
@@ -1,3 +0,0 @@
 | 
			
		||||
based on:
 | 
			
		||||
https://free3d.com/3d-model/wwii-ship-uk-king-george-v-class-battleship-v1--185381.html
 | 
			
		||||
License: Free Personal Use Only
 | 
			
		||||
| 
		 After Width: | Height: | Size: 43 KiB  | 
@@ -0,0 +1 @@
 | 
			
		||||
"Fishing Boat" (https://skfb.ly/6UGtr) by JasperTobias is licensed under Creative Commons Attribution-NonCommercial (http://creativecommons.org/licenses/by-nc/4.0/).
 | 
			
		||||
@@ -0,0 +1,22 @@
 | 
			
		||||
# Blender 3.6.5 MTL File: 'None'
 | 
			
		||||
# www.blender.org
 | 
			
		||||
 | 
			
		||||
newmtl Boat
 | 
			
		||||
Ns 0.000000
 | 
			
		||||
Ka 1.000000 1.000000 1.000000
 | 
			
		||||
Ks 0.500000 0.500000 0.500000
 | 
			
		||||
Ke 0.000000 0.000000 0.000000
 | 
			
		||||
Ni 1.450000
 | 
			
		||||
d 1.000000
 | 
			
		||||
illum 2
 | 
			
		||||
map_Kd ship1_color.png
 | 
			
		||||
 | 
			
		||||
newmtl Boat_2
 | 
			
		||||
Ns 0.000000
 | 
			
		||||
Ka 1.000000 1.000000 1.000000
 | 
			
		||||
Ks 0.500000 0.500000 0.500000
 | 
			
		||||
Ke 0.000000 0.000000 0.000000
 | 
			
		||||
Ni 1.450000
 | 
			
		||||
d 1.000000
 | 
			
		||||
illum 2
 | 
			
		||||
map_Kd ship1_color.png
 | 
			
		||||
| 
		 After Width: | Height: | Size: 87 KiB  | 
| 
		 After Width: | Height: | Size: 360 KiB  | 
@@ -0,0 +1,82 @@
 | 
			
		||||
# Blender 3.6.5 MTL File: 'None'
 | 
			
		||||
# www.blender.org
 | 
			
		||||
 | 
			
		||||
newmtl Battleship
 | 
			
		||||
Ns 250.000000
 | 
			
		||||
Ka 1.000000 1.000000 1.000000
 | 
			
		||||
Ks 0.500000 0.500000 0.500000
 | 
			
		||||
Ke 0.000000 0.000000 0.000000
 | 
			
		||||
Ni 1.450000
 | 
			
		||||
d 1.000000
 | 
			
		||||
illum 2
 | 
			
		||||
map_Kd ship2.jpg
 | 
			
		||||
 | 
			
		||||
newmtl blinn2SG
 | 
			
		||||
Ns 250.000000
 | 
			
		||||
Ka 1.000000 1.000000 1.000000
 | 
			
		||||
Kd 0.356400 0.356400 0.366253
 | 
			
		||||
Ks 0.500000 0.500000 0.500000
 | 
			
		||||
Ke 0.000000 0.000000 0.000000
 | 
			
		||||
Ni 1.450000
 | 
			
		||||
d 1.000000
 | 
			
		||||
illum 2
 | 
			
		||||
 | 
			
		||||
newmtl blinn3SG
 | 
			
		||||
Ns 250.000000
 | 
			
		||||
Ka 1.000000 1.000000 1.000000
 | 
			
		||||
Kd 0.346704 0.346704 0.356400
 | 
			
		||||
Ks 0.500000 0.500000 0.500000
 | 
			
		||||
Ke 0.000000 0.000000 0.000000
 | 
			
		||||
Ni 1.450000
 | 
			
		||||
d 1.000000
 | 
			
		||||
illum 2
 | 
			
		||||
 | 
			
		||||
newmtl blinn4SG
 | 
			
		||||
Ns 250.000000
 | 
			
		||||
Ka 1.000000 1.000000 1.000000
 | 
			
		||||
Kd 0.351533 0.346704 0.361307
 | 
			
		||||
Ks 0.500000 0.500000 0.500000
 | 
			
		||||
Ke 0.000000 0.000000 0.000000
 | 
			
		||||
Ni 1.450000
 | 
			
		||||
d 1.000000
 | 
			
		||||
illum 2
 | 
			
		||||
 | 
			
		||||
newmtl blinn5SG
 | 
			
		||||
Ns 250.000000
 | 
			
		||||
Ka 1.000000 1.000000 1.000000
 | 
			
		||||
Kd 0.346704 0.346704 0.356400
 | 
			
		||||
Ks 0.500000 0.500000 0.500000
 | 
			
		||||
Ke 0.000000 0.000000 0.000000
 | 
			
		||||
Ni 1.450000
 | 
			
		||||
d 1.000000
 | 
			
		||||
illum 2
 | 
			
		||||
 | 
			
		||||
newmtl blinn6SG
 | 
			
		||||
Ns 250.000000
 | 
			
		||||
Ka 1.000000 1.000000 1.000000
 | 
			
		||||
Kd 0.349118 0.346704 0.358854
 | 
			
		||||
Ks 0.500000 0.500000 0.500000
 | 
			
		||||
Ke 0.000000 0.000000 0.000000
 | 
			
		||||
Ni 1.450000
 | 
			
		||||
d 1.000000
 | 
			
		||||
illum 2
 | 
			
		||||
 | 
			
		||||
newmtl blinn7SG
 | 
			
		||||
Ns 250.000000
 | 
			
		||||
Ka 1.000000 1.000000 1.000000
 | 
			
		||||
Kd 0.346704 0.346704 0.356400
 | 
			
		||||
Ks 0.500000 0.500000 0.500000
 | 
			
		||||
Ke 0.000000 0.000000 0.000000
 | 
			
		||||
Ni 1.450000
 | 
			
		||||
d 1.000000
 | 
			
		||||
illum 2
 | 
			
		||||
 | 
			
		||||
newmtl blinn8SG
 | 
			
		||||
Ns 250.000000
 | 
			
		||||
Ka 1.000000 1.000000 1.000000
 | 
			
		||||
Kd 0.351533 0.346704 0.361307
 | 
			
		||||
Ks 0.500000 0.500000 0.500000
 | 
			
		||||
Ke 0.000000 0.000000 0.000000
 | 
			
		||||
Ni 1.450000
 | 
			
		||||
d 1.000000
 | 
			
		||||
illum 2
 | 
			
		||||
							
								
								
									
										104952
									
								
								Projekte/battleship/client/src/main/resources/Models/Ships/2/ship2.obj
									
									
									
									
									
										Normal file
									
								
							
							
						
						@@ -0,0 +1,15 @@
 | 
			
		||||
# 3ds Max Wavefront OBJ Exporter v0.97b - (c)2007 guruware
 | 
			
		||||
# File Created: 29.03.2012 14:25:39
 | 
			
		||||
 | 
			
		||||
newmtl default
 | 
			
		||||
	Ns 35.0000
 | 
			
		||||
	Ni 1.5000
 | 
			
		||||
	d 1.0000
 | 
			
		||||
	Tr 0.0000
 | 
			
		||||
	illum 2
 | 
			
		||||
	Ka 1.0000 1.0000 1.0000
 | 
			
		||||
	Kd 1.0000 1.0000 1.0000
 | 
			
		||||
	Ks 0.5400 0.5400 0.5400
 | 
			
		||||
	Ke 0.0000 0.0000 0.0000
 | 
			
		||||
	map_Ka ship3_color.jpg
 | 
			
		||||
	map_Kd ship3_color.jpg
 | 
			
		||||
							
								
								
									
										203098
									
								
								Projekte/battleship/client/src/main/resources/Models/Ships/3/ship3.obj
									
									
									
									
									
										Normal file
									
								
							
							
						
						| 
		 After Width: | Height: | Size: 168 KiB  | 
@@ -0,0 +1,13 @@
 | 
			
		||||
# Blender 3.6.5 MTL File: 'None'
 | 
			
		||||
# www.blender.org
 | 
			
		||||
 | 
			
		||||
newmtl _King_George_V
 | 
			
		||||
Ns 60.000008
 | 
			
		||||
Ka 1.000000 1.000000 1.000000
 | 
			
		||||
Ks 0.450000 0.450000 0.450000
 | 
			
		||||
Ke 0.000000 0.000000 0.000000
 | 
			
		||||
Ni 1.500000
 | 
			
		||||
d 1.000000
 | 
			
		||||
illum 2
 | 
			
		||||
map_Kd ship4_color.jpg
 | 
			
		||||
map_Bump -bm 1.000000 ship4_bump.jpg
 | 
			
		||||
							
								
								
									
										29326
									
								
								Projekte/battleship/client/src/main/resources/Models/Ships/4/ship4.obj
									
									
									
									
									
										Normal file
									
								
							
							
						
						| 
		 Before Width: | Height: | Size: 94 KiB After Width: | Height: | Size: 94 KiB  | 
| 
		 Before Width: | Height: | Size: 235 KiB After Width: | Height: | Size: 235 KiB  | 
@@ -41,7 +41,7 @@ public static void main(String[] args) {
 | 
			
		||||
     */
 | 
			
		||||
    @Override
 | 
			
		||||
    public void simpleInitApp() {
 | 
			
		||||
        export("Models/KingGeorgeV/King_George_V.obj", "KingGeorgeV.j3o"); //NON-NLS
 | 
			
		||||
        export("shell.obj", "shell.j3o"); //NON-NLS
 | 
			
		||||
 | 
			
		||||
        stop();
 | 
			
		||||
    }
 | 
			
		||||
 
 | 
			
		||||
| 
		 Before Width: | Height: | Size: 235 KiB  | 
@@ -1,18 +0,0 @@
 | 
			
		||||
# 3ds Max Wavefront OBJ Exporter v0.97b - (c)2007 guruware
 | 
			
		||||
# File Created: 16.03.2012 14:15:53
 | 
			
		||||
 | 
			
		||||
newmtl _King_George_V
 | 
			
		||||
	Ns 60.0000
 | 
			
		||||
	Ni 1.5000
 | 
			
		||||
	d 1.0000
 | 
			
		||||
	Tr 0.0000
 | 
			
		||||
	Tf 1.0000 1.0000 1.0000 
 | 
			
		||||
	illum 2
 | 
			
		||||
	Ka 1.0000 1.0000 1.0000
 | 
			
		||||
	Kd 1.0000 1.0000 1.0000
 | 
			
		||||
	Ks 0.4500 0.4500 0.4500
 | 
			
		||||
	Ke 0.0000 0.0000 0.0000
 | 
			
		||||
	map_Ka King_George_V.jpg
 | 
			
		||||
	map_Kd King_George_V.jpg
 | 
			
		||||
	map_bump King_George_V_bump.jpg
 | 
			
		||||
	bump King_George_V_bump.jpg
 | 
			
		||||
| 
		 Before Width: | Height: | Size: 94 KiB  | 
@@ -1,3 +0,0 @@
 | 
			
		||||
based on:
 | 
			
		||||
https://free3d.com/3d-model/wwii-ship-uk-king-george-v-class-battleship-v1--185381.html
 | 
			
		||||
License: Free Personal Use Only
 | 
			
		||||
							
								
								
									
										42
									
								
								Projekte/battleship/converter/src/main/resources/shell.mtl
									
									
									
									
									
										Normal file
									
								
							
							
						
						@@ -0,0 +1,42 @@
 | 
			
		||||
# Blender 3.6.5 MTL File: 'untitled.blend'
 | 
			
		||||
# www.blender.org
 | 
			
		||||
 | 
			
		||||
newmtl base
 | 
			
		||||
Ns 467.358765
 | 
			
		||||
Ka 0.636364 0.636364 0.636364
 | 
			
		||||
Kd 0.000000 0.000000 0.000000
 | 
			
		||||
Ks 0.500000 0.500000 0.500000
 | 
			
		||||
Ke 0.000000 0.000000 0.000000
 | 
			
		||||
Ni 1.000000
 | 
			
		||||
d 1.000000
 | 
			
		||||
illum 3
 | 
			
		||||
 | 
			
		||||
newmtl ring
 | 
			
		||||
Ns 467.358765
 | 
			
		||||
Ka 0.636364 0.636364 0.636364
 | 
			
		||||
Kd 0.031430 0.012811 0.000000
 | 
			
		||||
Ks 0.500000 0.500000 0.500000
 | 
			
		||||
Ke 0.000000 0.000000 0.000000
 | 
			
		||||
Ni 1.000000
 | 
			
		||||
d 1.000000
 | 
			
		||||
illum 3
 | 
			
		||||
 | 
			
		||||
newmtl tip
 | 
			
		||||
Ns 467.358765
 | 
			
		||||
Ka 0.636364 0.636364 0.636364
 | 
			
		||||
Kd 0.032954 0.004269 0.000000
 | 
			
		||||
Ks 0.500000 0.500000 0.500000
 | 
			
		||||
Ke 0.000000 0.000000 0.000000
 | 
			
		||||
Ni 1.000000
 | 
			
		||||
d 1.000000
 | 
			
		||||
illum 3
 | 
			
		||||
 | 
			
		||||
newmtl top
 | 
			
		||||
Ns 467.358765
 | 
			
		||||
Ka 0.636364 0.636364 0.636364
 | 
			
		||||
Kd 0.000489 0.006614 0.000950
 | 
			
		||||
Ks 0.500000 0.500000 0.500000
 | 
			
		||||
Ke 0.000000 0.000000 0.000000
 | 
			
		||||
Ni 1.000000
 | 
			
		||||
d 1.000000
 | 
			
		||||
illum 3
 | 
			
		||||
							
								
								
									
										8628
									
								
								Projekte/battleship/converter/src/main/resources/shell.obj
									
									
									
									
									
										Normal file
									
								
							
							
						
						@@ -10,6 +10,7 @@
 | 
			
		||||
import pp.battleship.message.client.ShootMessage;
 | 
			
		||||
import pp.battleship.message.server.EffectMessage;
 | 
			
		||||
import pp.battleship.model.IntPoint;
 | 
			
		||||
import pp.battleship.model.Shell;
 | 
			
		||||
import pp.battleship.model.ShipMap;
 | 
			
		||||
import pp.battleship.notification.Sound;
 | 
			
		||||
 | 
			
		||||
@@ -53,16 +54,13 @@ else if (logic.getOpponentMap().isValid(pos))
 | 
			
		||||
    @Override
 | 
			
		||||
    public void receivedEffect(EffectMessage msg) {
 | 
			
		||||
        ClientGameLogic.LOGGER.log(Level.INFO, "report effect: {0}", msg); //NON-NLS
 | 
			
		||||
        playSound(msg);
 | 
			
		||||
 | 
			
		||||
        myTurn = msg.isMyTurn();
 | 
			
		||||
        logic.setInfoText(msg.getInfoTextKey());
 | 
			
		||||
        affectedMap(msg).add(msg.getShot());
 | 
			
		||||
        if (destroyedOpponentShip(msg))
 | 
			
		||||
            logic.getOpponentMap().add(msg.getDestroyedShip());
 | 
			
		||||
        if (msg.isGameOver()) {
 | 
			
		||||
            msg.getRemainingOpponentShips().forEach(logic.getOwnMap()::add);
 | 
			
		||||
            logic.setState(new GameOverState(logic));
 | 
			
		||||
        }
 | 
			
		||||
        Shell shell = new Shell(msg.getShot());
 | 
			
		||||
        affectedMap(msg).add(shell);
 | 
			
		||||
        logic.playSound(Sound.SHELL_FLYING);
 | 
			
		||||
        logic.setState(new ShootingState(logic, shell, myTurn, msg));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
@@ -74,29 +72,4 @@ public void receivedEffect(EffectMessage msg) {
 | 
			
		||||
    private ShipMap affectedMap(EffectMessage msg) {
 | 
			
		||||
        return msg.isOwnShot() ? logic.getOpponentMap() : logic.getOwnMap();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Checks if the opponent's ship was destroyed by the player's shot.
 | 
			
		||||
     *
 | 
			
		||||
     * @param msg the effect message received from the server
 | 
			
		||||
     * @return true if the shot destroyed an opponent's ship, false otherwise
 | 
			
		||||
     */
 | 
			
		||||
    private boolean destroyedOpponentShip(EffectMessage msg) {
 | 
			
		||||
        return msg.getDestroyedShip() != null && msg.isOwnShot();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Plays a sound based on the outcome of the shot. Different sounds are played for a miss, hit,
 | 
			
		||||
     * or destruction of a ship.
 | 
			
		||||
     *
 | 
			
		||||
     * @param msg the effect message containing the result of the shot
 | 
			
		||||
     */
 | 
			
		||||
    private void playSound(EffectMessage msg) {
 | 
			
		||||
        if (!msg.getShot().isHit())
 | 
			
		||||
            logic.playSound(Sound.SPLASH);
 | 
			
		||||
        else if (msg.getDestroyedShip() == null)
 | 
			
		||||
            logic.playSound(Sound.EXPLOSION);
 | 
			
		||||
        else
 | 
			
		||||
            logic.playSound(Sound.DESTROYED_SHIP);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -0,0 +1,114 @@
 | 
			
		||||
package pp.battleship.game.client;
 | 
			
		||||
 | 
			
		||||
import pp.battleship.message.client.AnimationFinishedMessage;
 | 
			
		||||
import pp.battleship.message.server.EffectMessage;
 | 
			
		||||
import pp.battleship.model.Battleship;
 | 
			
		||||
import pp.battleship.model.Shell;
 | 
			
		||||
import pp.battleship.model.ShipMap;
 | 
			
		||||
import pp.battleship.notification.Sound;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Represents the shooting state of the game where a shell is fired at the opponent.
 | 
			
		||||
 */
 | 
			
		||||
public class ShootingState extends ClientState {
 | 
			
		||||
    private float shootValue;
 | 
			
		||||
    private final static float SHELL_SPEED = 0.3f;
 | 
			
		||||
    private final Shell shell;
 | 
			
		||||
    private final boolean myTurn;
 | 
			
		||||
    private final EffectMessage msg;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Constructs a shooting state with the specified game logic.
 | 
			
		||||
     *
 | 
			
		||||
     * @param logic  the game logic
 | 
			
		||||
     * @param shell  the shell being shot
 | 
			
		||||
     * @param myTurn indicates if it is the player's turn
 | 
			
		||||
     * @param msg    the effect message associated with the shooting action
 | 
			
		||||
     */
 | 
			
		||||
    public ShootingState(ClientGameLogic logic, Shell shell, boolean myTurn, EffectMessage msg) {
 | 
			
		||||
        super(logic);
 | 
			
		||||
        this.msg = msg;
 | 
			
		||||
        this.myTurn = myTurn;
 | 
			
		||||
        this.shell = shell;
 | 
			
		||||
        this.shootValue = 0;
 | 
			
		||||
        shell.move(shootValue);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public boolean showBattle() {
 | 
			
		||||
        return true;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Updates the shooting state by moving the shell based on the elapsed time.
 | 
			
		||||
     *
 | 
			
		||||
     * @param delta the time in seconds since the last update
 | 
			
		||||
     */
 | 
			
		||||
    @Override
 | 
			
		||||
    void update(float delta) {
 | 
			
		||||
        super.update(delta);
 | 
			
		||||
        if (shootValue > 1) {
 | 
			
		||||
            endState();
 | 
			
		||||
        }
 | 
			
		||||
        else {
 | 
			
		||||
            shootValue += delta * SHELL_SPEED;
 | 
			
		||||
            shell.move(shootValue);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Ends the shooting state and processes the effects of the shot.
 | 
			
		||||
     */
 | 
			
		||||
    private void endState() {
 | 
			
		||||
        playSound(msg);
 | 
			
		||||
        affectedMap(msg).add(msg.getShot());
 | 
			
		||||
        affectedMap(msg).remove(shell);
 | 
			
		||||
 | 
			
		||||
        if (destroyedOpponentShip(msg))
 | 
			
		||||
            logic.getOpponentMap().add(msg.getDestroyedShip());
 | 
			
		||||
        if (msg.isGameOver()) {
 | 
			
		||||
            for (Battleship ship : msg.getRemainingOpponentShips()) {
 | 
			
		||||
                logic.getOpponentMap().add(ship);
 | 
			
		||||
            }
 | 
			
		||||
            logic.setState(new GameOverState(logic));
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
        logic.send(new AnimationFinishedMessage());
 | 
			
		||||
        logic.setState(new BattleState(logic, myTurn));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Checks if an opponent's ship was destroyed by the shot.
 | 
			
		||||
     *
 | 
			
		||||
     * @param msg the effect message containing the shot details
 | 
			
		||||
     * @return true if an opponent's ship was destroyed, false otherwise
 | 
			
		||||
     */
 | 
			
		||||
    private boolean destroyedOpponentShip(EffectMessage msg) {
 | 
			
		||||
        return msg.getDestroyedShip() != null && msg.isOwnShot();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Retrieves the affected map based on whether the shot was owned by the player or the opponent.
 | 
			
		||||
     *
 | 
			
		||||
     * @param msg the effect message containing shot details
 | 
			
		||||
     * @return the ShipMap that was affected by the shot
 | 
			
		||||
     */
 | 
			
		||||
    private ShipMap affectedMap(EffectMessage msg) {
 | 
			
		||||
        return msg.isOwnShot() ? logic.getOpponentMap() : logic.getOwnMap();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Plays a sound based on the outcome of the shot. Different sounds are played for a miss, hit,
 | 
			
		||||
     * or destruction of a ship.
 | 
			
		||||
     *
 | 
			
		||||
     * @param msg the effect message containing the result of the shot
 | 
			
		||||
     */
 | 
			
		||||
    private void playSound(EffectMessage msg) {
 | 
			
		||||
        if (!msg.getShot().isHit())
 | 
			
		||||
            logic.playSound(Sound.SPLASH);
 | 
			
		||||
        else if (msg.getDestroyedShip() == null)
 | 
			
		||||
            logic.playSound(Sound.EXPLOSION);
 | 
			
		||||
        else
 | 
			
		||||
            logic.playSound(Sound.DESTROYED_SHIP);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -7,6 +7,7 @@
 | 
			
		||||
 | 
			
		||||
package pp.battleship.game.client;
 | 
			
		||||
 | 
			
		||||
import pp.battleship.message.server.GameDetails;
 | 
			
		||||
import pp.battleship.message.server.StartBattleMessage;
 | 
			
		||||
 | 
			
		||||
import java.lang.System.Logger.Level;
 | 
			
		||||
@@ -38,4 +39,11 @@ public void receivedStartBattle(StartBattleMessage msg) {
 | 
			
		||||
        logic.setInfoText(msg.getInfoTextKey());
 | 
			
		||||
        logic.setState(new BattleState(logic, msg.isMyTurn()));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void receivedGameDetails(GameDetails details) {
 | 
			
		||||
        ClientGameLogic.LOGGER.log(Level.WARNING, "Invalid Map"); //NON-NLS
 | 
			
		||||
        logic.setInfoText("invalid.map");
 | 
			
		||||
        logic.setState(new EditorState(logic));
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -8,6 +8,7 @@
 | 
			
		||||
package pp.battleship.game.server;
 | 
			
		||||
 | 
			
		||||
import pp.battleship.BattleshipConfig;
 | 
			
		||||
import pp.battleship.message.client.AnimationFinishedMessage;
 | 
			
		||||
import pp.battleship.message.client.ClientInterpreter;
 | 
			
		||||
import pp.battleship.message.client.MapMessage;
 | 
			
		||||
import pp.battleship.message.client.ShootMessage;
 | 
			
		||||
@@ -38,6 +39,7 @@ public class ServerGameLogic implements ClientInterpreter {
 | 
			
		||||
    private final ServerSender serverSender;
 | 
			
		||||
    private Player activePlayer;
 | 
			
		||||
    private ServerState state = ServerState.WAIT;
 | 
			
		||||
    private Set<Player> waitPlayers = new HashSet<>();
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Constructs a ServerGameLogic with the specified sender and configuration.
 | 
			
		||||
@@ -142,8 +144,73 @@ public Player addPlayer(int id) {
 | 
			
		||||
    public void received(MapMessage msg, int from) {
 | 
			
		||||
        if (state != ServerState.SET_UP)
 | 
			
		||||
            LOGGER.log(Level.ERROR, "playerReady not allowed in {0}", state); //NON-NLS
 | 
			
		||||
        else
 | 
			
		||||
            playerReady(getPlayerById(from), msg.getShips());
 | 
			
		||||
        else {
 | 
			
		||||
            if (checkMap(msg.getShips())) {
 | 
			
		||||
                playerReady(getPlayerById(from), msg.getShips());
 | 
			
		||||
            }
 | 
			
		||||
            else {
 | 
			
		||||
                LOGGER.log(Level.WARNING, "Invalid Map sent from player {0}", from); //NON-NLS
 | 
			
		||||
                send(players.get(from), new GameDetails(config));
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Handles the reception of a AnimationFinishedMessage.
 | 
			
		||||
     *
 | 
			
		||||
     * @param msg  the received MapMessage
 | 
			
		||||
     * @param from the ID of the sender client
 | 
			
		||||
     */
 | 
			
		||||
    @Override
 | 
			
		||||
    public void received(AnimationFinishedMessage msg, int from) {
 | 
			
		||||
        if (state != ServerState.ANIMATION) {
 | 
			
		||||
            LOGGER.log(Level.ERROR, "animation finished not allowed in {0}", state);
 | 
			
		||||
        }
 | 
			
		||||
        else {
 | 
			
		||||
            LOGGER.log(Level.DEBUG, "anim received from {0}", getPlayerById(from));
 | 
			
		||||
            Player player = getPlayerById(from);
 | 
			
		||||
            if (!waitPlayers.add(player)) {
 | 
			
		||||
                LOGGER.log(Level.ERROR, "{0} already sent animation finished", player); //NON-NLS
 | 
			
		||||
                return;
 | 
			
		||||
            }
 | 
			
		||||
            if (waitPlayers.size() == 2) {
 | 
			
		||||
                waitPlayers = new HashSet<>();
 | 
			
		||||
                setState(ServerState.BATTLE);
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Validates the placement of battleships on the map.
 | 
			
		||||
     * Ensures that:
 | 
			
		||||
     * <ul>
 | 
			
		||||
     *   <li>The number of ships matches the configuration.</li>
 | 
			
		||||
     *   <li>Ships are within the map's boundaries.</li>
 | 
			
		||||
     *   <li>Ships do not overlap.</li>
 | 
			
		||||
     * </ul>
 | 
			
		||||
     *
 | 
			
		||||
     * @param ships the list of {@link Battleship} objects to validate
 | 
			
		||||
     * @return {@code true} if all ships are placed correctly; {@code false} otherwise
 | 
			
		||||
     */
 | 
			
		||||
    private boolean checkMap(List<Battleship> ships) {
 | 
			
		||||
        int numShips = config.getShipNums().values().stream().mapToInt(Integer::intValue).sum();
 | 
			
		||||
        if (numShips != ships.size()) return false;
 | 
			
		||||
 | 
			
		||||
        List<IntPoint> occupied = new ArrayList<>();
 | 
			
		||||
 | 
			
		||||
        for (Battleship battleship : ships) {
 | 
			
		||||
            int x = battleship.getX();
 | 
			
		||||
            int y = battleship.getY();
 | 
			
		||||
            for (int i = 0; i < battleship.getLength(); i++) {
 | 
			
		||||
                if (x >= 0 && x < config.getMapWidth() && y >= 0 && y < config.getMapHeight() && !occupied.contains(new IntPoint(x, y))) {
 | 
			
		||||
                    occupied.add(new IntPoint(x, y));
 | 
			
		||||
                    x += battleship.getRot().dx();
 | 
			
		||||
                    y += battleship.getRot().dy();
 | 
			
		||||
                }
 | 
			
		||||
                else return false;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        return true;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
@@ -156,8 +223,11 @@ public void received(MapMessage msg, int from) {
 | 
			
		||||
    public void received(ShootMessage msg, int from) {
 | 
			
		||||
        if (state != ServerState.BATTLE)
 | 
			
		||||
            LOGGER.log(Level.ERROR, "shoot not allowed in {0}", state); //NON-NLS
 | 
			
		||||
        else
 | 
			
		||||
        else{
 | 
			
		||||
            setState(ServerState.ANIMATION);
 | 
			
		||||
            shoot(getPlayerById(from), msg.getPosition());
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
@@ -204,6 +274,7 @@ void shoot(Player p, IntPoint pos) {
 | 
			
		||||
                send(activePlayer, EffectMessage.won(pos, selectedShip));
 | 
			
		||||
                send(otherPlayer, EffectMessage.lost(pos, selectedShip, activePlayer.getMap().getRemainingShips()));
 | 
			
		||||
                setState(ServerState.GAME_OVER);
 | 
			
		||||
                return;
 | 
			
		||||
            }
 | 
			
		||||
            else if (selectedShip.isDestroyed()) {
 | 
			
		||||
                // ship has been destroyed, but game is not yet over
 | 
			
		||||
 
 | 
			
		||||
@@ -26,6 +26,11 @@ enum ServerState {
 | 
			
		||||
     */
 | 
			
		||||
    BATTLE,
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * The server is waiting for all clients to finish the shoot animation.
 | 
			
		||||
     */
 | 
			
		||||
    ANIMATION,
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * The game has ended because all the ships of one player have been destroyed.
 | 
			
		||||
     */
 | 
			
		||||
 
 | 
			
		||||
@@ -7,6 +7,7 @@
 | 
			
		||||
 | 
			
		||||
package pp.battleship.game.singlemode;
 | 
			
		||||
 | 
			
		||||
import pp.battleship.message.client.AnimationFinishedMessage;
 | 
			
		||||
import pp.battleship.message.client.ClientInterpreter;
 | 
			
		||||
import pp.battleship.message.client.ClientMessage;
 | 
			
		||||
import pp.battleship.message.client.MapMessage;
 | 
			
		||||
@@ -63,6 +64,11 @@ public void received(MapMessage msg, int from) {
 | 
			
		||||
        copiedMessage = new MapMessage(msg.getShips().stream().map(Copycat::copy).toList());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void received(AnimationFinishedMessage msg, int from) {
 | 
			
		||||
        copiedMessage = msg;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Creates a copy of the provided {@link Battleship}.
 | 
			
		||||
     *
 | 
			
		||||
 
 | 
			
		||||
@@ -1,6 +1,7 @@
 | 
			
		||||
package pp.battleship.game.singlemode;
 | 
			
		||||
 | 
			
		||||
import pp.battleship.game.client.BattleshipClient;
 | 
			
		||||
import pp.battleship.message.client.AnimationFinishedMessage;
 | 
			
		||||
import pp.battleship.message.client.MapMessage;
 | 
			
		||||
import pp.battleship.message.client.ShootMessage;
 | 
			
		||||
import pp.battleship.message.server.EffectMessage;
 | 
			
		||||
@@ -71,6 +72,7 @@ public void run() {
 | 
			
		||||
     * Makes the RobotClient take a shot by sending a ShootMessage with the target position.
 | 
			
		||||
     */
 | 
			
		||||
    private void robotShot() {
 | 
			
		||||
 | 
			
		||||
        connection.sendRobotMessage(new ShootMessage(getShotPosition()));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -121,6 +123,7 @@ public void received(StartBattleMessage msg) {
 | 
			
		||||
    @Override
 | 
			
		||||
    public void received(EffectMessage msg) {
 | 
			
		||||
        LOGGER.log(Level.INFO, "Received EffectMessage: {0}", msg); //NON-NLS
 | 
			
		||||
        connection.sendRobotMessage(new AnimationFinishedMessage());
 | 
			
		||||
        if (msg.isMyTurn())
 | 
			
		||||
            shoot();
 | 
			
		||||
    }
 | 
			
		||||
 
 | 
			
		||||
@@ -0,0 +1,24 @@
 | 
			
		||||
package pp.battleship.message.client;
 | 
			
		||||
 | 
			
		||||
import com.jme3.network.serializing.Serializable;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Represents a message indicating that an animation has finished on the client side.
 | 
			
		||||
 */
 | 
			
		||||
@Serializable
 | 
			
		||||
public class AnimationFinishedMessage extends ClientMessage {
 | 
			
		||||
    public AnimationFinishedMessage() {
 | 
			
		||||
        super();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Accepts a visitor to process this message.
 | 
			
		||||
     *
 | 
			
		||||
     * @param interpreter the visitor to process this message
 | 
			
		||||
     * @param from        the connection ID from which the message was received
 | 
			
		||||
     */
 | 
			
		||||
    @Override
 | 
			
		||||
    public void accept(ClientInterpreter interpreter, int from) {
 | 
			
		||||
        interpreter.received(this, from);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -26,4 +26,12 @@ public interface ClientInterpreter {
 | 
			
		||||
     * @param from the connection ID from which the message was received
 | 
			
		||||
     */
 | 
			
		||||
    void received(MapMessage msg, int from);
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Processes a received AnimationFinishedMessage.
 | 
			
		||||
     *
 | 
			
		||||
     * @param msg  the MapMessage to be processed
 | 
			
		||||
     * @param from the connection ID from which the message was received
 | 
			
		||||
     */
 | 
			
		||||
    void received(AnimationFinishedMessage msg, int from);
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -0,0 +1,125 @@
 | 
			
		||||
package pp.battleship.model;
 | 
			
		||||
 | 
			
		||||
import com.jme3.math.Quaternion;
 | 
			
		||||
import com.jme3.math.Vector3f;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * The {@code Shell} class represents a projectile fired by a ship in the Battleship game.
 | 
			
		||||
 * It models the position and rotation of the projectile and allows for its movement along
 | 
			
		||||
 * a Bezier curve.
 | 
			
		||||
 */
 | 
			
		||||
public class Shell implements Item {
 | 
			
		||||
    /**
 | 
			
		||||
     * Initial position of the shell
 | 
			
		||||
     */
 | 
			
		||||
    private final static Vector3f INIT_POS = new Vector3f(-3, 7, -3);
 | 
			
		||||
    /**
 | 
			
		||||
     * The overshot difference vector used to get a shallower flight path
 | 
			
		||||
     */
 | 
			
		||||
    private final static Vector3f OVER_SHOT_DIFF = new Vector3f(-1, -1, -1);
 | 
			
		||||
    // Target shot position
 | 
			
		||||
    private final Vector3f shotPosition;
 | 
			
		||||
    // Position on top of shotPosition used for Bezier curve
 | 
			
		||||
    private final Vector3f overShotPosition;
 | 
			
		||||
    // Current position of the shell
 | 
			
		||||
    private Vector3f position;
 | 
			
		||||
    // Current rotation of the shell
 | 
			
		||||
    private final Quaternion rotation;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Constructs a new {@code Shell} object using the given {@code Shot} target.
 | 
			
		||||
     * The initial position, target position, and overshot position are calculated.
 | 
			
		||||
     *
 | 
			
		||||
     * @param shot The target {@code Shot} object containing the destination coordinates.
 | 
			
		||||
     */
 | 
			
		||||
    public Shell(Shot shot) {
 | 
			
		||||
        this.shotPosition = new Vector3f(shot.getX(), 0, shot.getY());
 | 
			
		||||
        this.overShotPosition = new Vector3f(shotPosition.x, INIT_POS.y, shotPosition.z).add(OVER_SHOT_DIFF);
 | 
			
		||||
        this.position = INIT_POS;
 | 
			
		||||
        this.rotation = new Quaternion();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Gets the current position of the shell.
 | 
			
		||||
     *
 | 
			
		||||
     * @return The current position as a {@code Vector3f}.
 | 
			
		||||
     */
 | 
			
		||||
    public Vector3f getPosition() {
 | 
			
		||||
        return this.position;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Gets the current rotation of the shell.
 | 
			
		||||
     *
 | 
			
		||||
     * @return The current rotation as a {@code Quaternion}.
 | 
			
		||||
     */
 | 
			
		||||
    public Quaternion getRotation() {
 | 
			
		||||
        return this.rotation;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Moves the shell along a Bezier curve based on the given time factor {@code t}.
 | 
			
		||||
     * The position and rotation of the shell are updated.
 | 
			
		||||
     *
 | 
			
		||||
     * @param t The time factor between 0 and 1, representing the progress of the shell's flight.
 | 
			
		||||
     */
 | 
			
		||||
    public void move(float t) {
 | 
			
		||||
        if (t > 1f) t = 1f;
 | 
			
		||||
        Vector3f newPosition = bezInt(INIT_POS, overShotPosition, shotPosition, t);
 | 
			
		||||
        updateRotation(position, newPosition);
 | 
			
		||||
        this.position = newPosition;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Performs a quadratic Bezier interpolation between three points based on the time factor {@code t}.
 | 
			
		||||
     *
 | 
			
		||||
     * @param p1 The start position.
 | 
			
		||||
     * @param p2 The overshot position.
 | 
			
		||||
     * @param p3 The target position.
 | 
			
		||||
     * @param t  The time factor for interpolation.
 | 
			
		||||
     * @return The interpolated position as a {@code Vector3f}.
 | 
			
		||||
     */
 | 
			
		||||
    private Vector3f bezInt(Vector3f p1, Vector3f p2, Vector3f p3, float t) {
 | 
			
		||||
        Vector3f inA = linInt(p1, p2, t);
 | 
			
		||||
        Vector3f inB = linInt(p2, p3, t);
 | 
			
		||||
        return linInt(inA, inB, t);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Performs linear interpolation between two points {@code p1} and {@code p2} based on the time factor {@code t}.
 | 
			
		||||
     *
 | 
			
		||||
     * @param p1 The start position.
 | 
			
		||||
     * @param p2 The end position.
 | 
			
		||||
     * @param t  The time factor for interpolation.
 | 
			
		||||
     * @return The interpolated position as a {@code Vector3f}.
 | 
			
		||||
     */
 | 
			
		||||
    private Vector3f linInt(Vector3f p1, Vector3f p2, float t) {
 | 
			
		||||
        float x = p1.getX() + t * (p2.getX() - p1.getX());
 | 
			
		||||
        float y = p1.getY() + t * (p2.getY() - p1.getY());
 | 
			
		||||
        float z = p1.getZ() + t * (p2.getZ() - p1.getZ());
 | 
			
		||||
        return new Vector3f(x, y, z);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Updates the rotation of the shell to face the new position along its flight path.
 | 
			
		||||
     *
 | 
			
		||||
     * @param oldPos The previous position of the shell.
 | 
			
		||||
     * @param newPos The new position of the shell.
 | 
			
		||||
     */
 | 
			
		||||
    private void updateRotation(Vector3f oldPos, Vector3f newPos) {
 | 
			
		||||
        Vector3f direction = newPos.subtract(oldPos).normalize();
 | 
			
		||||
        if (direction.lengthSquared() > 0) {
 | 
			
		||||
            this.rotation.lookAt(direction, Vector3f.UNIT_Y);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public <T> T accept(Visitor<T> visitor) {
 | 
			
		||||
        return visitor.visit(this);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void accept(VoidVisitor visitor) {
 | 
			
		||||
        visitor.visit(this);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -10,6 +10,7 @@
 | 
			
		||||
import pp.battleship.notification.GameEvent;
 | 
			
		||||
import pp.battleship.notification.GameEventBroker;
 | 
			
		||||
import pp.battleship.notification.ItemAddedEvent;
 | 
			
		||||
import pp.battleship.notification.ItemRemovedEvent;
 | 
			
		||||
 | 
			
		||||
import java.util.ArrayList;
 | 
			
		||||
import java.util.Collections;
 | 
			
		||||
@@ -90,6 +91,15 @@ public void add(Shot shot) {
 | 
			
		||||
        addItem(shot);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Registers a shot on the map and triggers an item addition event.
 | 
			
		||||
     *
 | 
			
		||||
     * @param shell the shell to be registered on the map
 | 
			
		||||
     */
 | 
			
		||||
    public void add(Shell shell) {
 | 
			
		||||
        addItem(shell);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Removes an item from the map and triggers an item removal event.
 | 
			
		||||
     *
 | 
			
		||||
@@ -97,7 +107,7 @@ public void add(Shot shot) {
 | 
			
		||||
     */
 | 
			
		||||
    public void remove(Item item) {
 | 
			
		||||
        items.remove(item);
 | 
			
		||||
        notifyListeners(new ItemAddedEvent(item, this));
 | 
			
		||||
        notifyListeners(new ItemRemovedEvent(item, this));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
 
 | 
			
		||||
@@ -28,4 +28,12 @@ public interface Visitor<T> {
 | 
			
		||||
     * @return the result of visiting the Battleship element
 | 
			
		||||
     */
 | 
			
		||||
    T visit(Battleship ship);
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Visits a Shell element.
 | 
			
		||||
     *
 | 
			
		||||
     * @param shell the Battleship element to visit
 | 
			
		||||
     * @return the result of visiting the Battleship element
 | 
			
		||||
     */
 | 
			
		||||
    T visit(Shell shell);
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -25,4 +25,11 @@ public interface VoidVisitor {
 | 
			
		||||
     * @param ship the Battleship element to visit
 | 
			
		||||
     */
 | 
			
		||||
    void visit(Battleship ship);
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Visits a Shell element.
 | 
			
		||||
     *
 | 
			
		||||
     * @param shell the Battleship element to visit
 | 
			
		||||
     */
 | 
			
		||||
    void visit(Shell shell);
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -12,6 +12,7 @@
 | 
			
		||||
import com.google.gson.JsonParseException;
 | 
			
		||||
import pp.battleship.message.server.GameDetails;
 | 
			
		||||
import pp.battleship.model.Battleship;
 | 
			
		||||
import pp.battleship.model.IntPoint;
 | 
			
		||||
import pp.battleship.model.ShipMap;
 | 
			
		||||
 | 
			
		||||
import java.io.File;
 | 
			
		||||
@@ -20,6 +21,7 @@
 | 
			
		||||
import java.io.IOException;
 | 
			
		||||
import java.lang.System.Logger;
 | 
			
		||||
import java.lang.System.Logger.Level;
 | 
			
		||||
import java.util.ArrayList;
 | 
			
		||||
import java.util.List;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
@@ -50,10 +52,14 @@ public ShipMapDTO(ShipMap map) {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Checks if the current ship map fits the game details provided.
 | 
			
		||||
     * Checks if the ship map is compatible with the provided game details.
 | 
			
		||||
     *
 | 
			
		||||
     * @param details the game details to be matched against
 | 
			
		||||
     * @return true if the ship map fits the game details, false otherwise
 | 
			
		||||
     * This method verifies that the ship map's dimensions match the game details,
 | 
			
		||||
     * that the total number of ships is correct, and that the ship placements
 | 
			
		||||
     * do not overlap and stay within the map boundaries.
 | 
			
		||||
     *
 | 
			
		||||
     * @param details the game details containing required dimensions and ship configurations
 | 
			
		||||
     * @return true if the ship map fits the game details; false otherwise
 | 
			
		||||
     */
 | 
			
		||||
    public boolean fits(GameDetails details) {
 | 
			
		||||
        if (width != details.getWidth() || height != details.getHeight())
 | 
			
		||||
@@ -61,6 +67,7 @@ public boolean fits(GameDetails details) {
 | 
			
		||||
        int numShips = details.getShipNums().values().stream().mapToInt(Integer::intValue).sum();
 | 
			
		||||
        if (numShips != ships.size())
 | 
			
		||||
            return false;
 | 
			
		||||
 | 
			
		||||
        for (var e : details.getShipNums().entrySet()) {
 | 
			
		||||
            final int shipLength = e.getKey();
 | 
			
		||||
            final int requiredNum = e.getValue();
 | 
			
		||||
@@ -70,6 +77,20 @@ public boolean fits(GameDetails details) {
 | 
			
		||||
            if (requiredNum != actualNum)
 | 
			
		||||
                return false;
 | 
			
		||||
        }
 | 
			
		||||
        List<IntPoint> occupied = new ArrayList<>();
 | 
			
		||||
        for (BattleshipDTO dto: ships){
 | 
			
		||||
            Battleship battleship = dto.toBattleship();
 | 
			
		||||
            int x = battleship.getX();
 | 
			
		||||
            int y = battleship.getY();
 | 
			
		||||
            for(int i = 0; i < battleship.getLength(); i++){
 | 
			
		||||
                if(x >= 0 && x < details.getWidth() && y >= 0 && y < details.getHeight() && !occupied.contains(new IntPoint(x,y))){
 | 
			
		||||
                    occupied.add(new IntPoint(x,y));
 | 
			
		||||
                    x += battleship.getRot().dx();
 | 
			
		||||
                    y += battleship.getRot().dy();
 | 
			
		||||
                }
 | 
			
		||||
                else return false;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        return true;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -22,5 +22,9 @@ public enum Sound {
 | 
			
		||||
    /**
 | 
			
		||||
     * Sound of a ship being destroyed.
 | 
			
		||||
     */
 | 
			
		||||
    DESTROYED_SHIP
 | 
			
		||||
    DESTROYED_SHIP,
 | 
			
		||||
    /**
 | 
			
		||||
     * Sound of a shell in flight.
 | 
			
		||||
     */
 | 
			
		||||
    SHELL_FLYING
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -31,9 +31,12 @@ menu.return-to-game=Return to game
 | 
			
		||||
menu.sound-enabled=Sound switched on
 | 
			
		||||
menu.map.load=Load map from file...
 | 
			
		||||
menu.map.save=Save map in file...
 | 
			
		||||
menu.music-enabled=Music on
 | 
			
		||||
label.file=File:
 | 
			
		||||
label.connecting=Connecting...
 | 
			
		||||
dialog.error=Error
 | 
			
		||||
dialog.question=Question
 | 
			
		||||
port.must.be.integer=Port must be an integer number
 | 
			
		||||
map.doesnt.fit=The map doesn't fit to this game
 | 
			
		||||
invalid.map=Invalid Map!
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -27,13 +27,15 @@ host.name=Host
 | 
			
		||||
port.number=Port
 | 
			
		||||
wait.its.not.your.turn=Warte, Du bist nicht dran!!
 | 
			
		||||
menu.quit=Spiel beenden
 | 
			
		||||
menu.return-to-game=Zurück zum Spiel
 | 
			
		||||
menu.return-to-game=Zur<EFBFBD>ck zum Spiel
 | 
			
		||||
menu.sound-enabled=Sound eingeschaltet
 | 
			
		||||
menu.map.load=Karte von Datei laden...
 | 
			
		||||
menu.map.save=Karte in Datei speichern...
 | 
			
		||||
menu.music-enabled=Musik an
 | 
			
		||||
label.file=Datei:
 | 
			
		||||
label.connecting=Verbindung wird aufgebaut...
 | 
			
		||||
dialog.error=Fehler
 | 
			
		||||
dialog.question=Frage
 | 
			
		||||
port.must.be.integer=Der Port muss eine ganze Zahl sein
 | 
			
		||||
map.doesnt.fit=Diese Karte passt nicht zu diesem Spiel
 | 
			
		||||
invalid.map=Ung<EFBFBD>ltige Karte!
 | 
			
		||||
 
 | 
			
		||||
@@ -18,6 +18,7 @@
 | 
			
		||||
import pp.battleship.game.server.Player;
 | 
			
		||||
import pp.battleship.game.server.ServerGameLogic;
 | 
			
		||||
import pp.battleship.game.server.ServerSender;
 | 
			
		||||
import pp.battleship.message.client.AnimationFinishedMessage;
 | 
			
		||||
import pp.battleship.message.client.ClientMessage;
 | 
			
		||||
import pp.battleship.message.client.MapMessage;
 | 
			
		||||
import pp.battleship.message.client.ShootMessage;
 | 
			
		||||
@@ -115,6 +116,7 @@ private void initializeSerializables() {
 | 
			
		||||
        Serializer.registerClass(MapMessage.class);
 | 
			
		||||
        Serializer.registerClass(ShootMessage.class);
 | 
			
		||||
        Serializer.registerClass(EffectMessage.class);
 | 
			
		||||
        Serializer.registerClass(AnimationFinishedMessage.class);
 | 
			
		||||
        Serializer.registerClass(Battleship.class);
 | 
			
		||||
        Serializer.registerClass(IntPoint.class);
 | 
			
		||||
        Serializer.registerClass(Shot.class);
 | 
			
		||||
@@ -123,6 +125,7 @@ private void initializeSerializables() {
 | 
			
		||||
    private void registerListeners() {
 | 
			
		||||
        myServer.addMessageListener(this, MapMessage.class);
 | 
			
		||||
        myServer.addMessageListener(this, ShootMessage.class);
 | 
			
		||||
        myServer.addMessageListener(this, AnimationFinishedMessage.class);
 | 
			
		||||
        myServer.addConnectionListener(this);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										42
									
								
								Projekte/jme-common/src/main/java/pp/JmeUtil.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						@@ -0,0 +1,42 @@
 | 
			
		||||
package pp;
 | 
			
		||||
 | 
			
		||||
import com.jme3.app.Application;
 | 
			
		||||
import com.jme3.asset.AssetLoadException;
 | 
			
		||||
import com.jme3.asset.AssetNotFoundException;
 | 
			
		||||
import com.jme3.audio.AudioData;
 | 
			
		||||
import com.jme3.audio.AudioNode;
 | 
			
		||||
import com.jme3.math.Vector3f;
 | 
			
		||||
import pp.util.Util;
 | 
			
		||||
 | 
			
		||||
import java.lang.System.Logger;
 | 
			
		||||
import java.lang.System.Logger.Level;
 | 
			
		||||
 | 
			
		||||
public class JmeUtil {
 | 
			
		||||
    private JmeUtil() { /* Do not initialize */ }
 | 
			
		||||
 | 
			
		||||
    private static final Logger LOGGER = System.getLogger(Util.class.getName());
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Loads a sound from the specified file.
 | 
			
		||||
     *
 | 
			
		||||
     * @param app  The application
 | 
			
		||||
     * @param name The name of the sound file.
 | 
			
		||||
     * @return The loaded AudioNode.
 | 
			
		||||
     */
 | 
			
		||||
    public static 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;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public static Vector3f mapToWorldCord(float x, float y) {
 | 
			
		||||
        return new Vector3f(y + 0.5f, 0, x + 0.5f);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||