Compare commits
	
		
			15 Commits
		
	
	
		
			4a9eba255b
			...
			b_Mueller_
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
|  | e0bea47583 | ||
|  | 38979bdf86 | ||
|  | 68beedcf29 | ||
|  | 7d70e8bd13 | ||
|  | 0fd0555dea | ||
|  | b962444506 | ||
|  | c0a3e8b37a | ||
|  | c8621e02c6 | ||
|  | 25d7884cad | ||
|  | 2192f6dbc3 | ||
|  | 2b8bfb82ba | ||
|  | 70ed981ea2 | ||
|  | d5450df77c | ||
|  | 1f75f7bf30 | ||
|  | 1ac55a9570 | 
							
								
								
									
										1
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							
							
						
						| @@ -4,7 +4,6 @@ build | ||||
|  | ||||
| # VSC | ||||
| bin | ||||
| .vscode | ||||
|  | ||||
| # IntelliJ | ||||
| *.iml | ||||
|   | ||||
| @@ -1,18 +0,0 @@ | ||||
| <component name="ProjectRunConfigurationManager"> | ||||
|   <configuration default="false" name="MonopolyApp" type="Application" factoryName="Application" singleton="false" | ||||
|                  nameIsGenerated="true"> | ||||
|     <option name="MAIN_CLASS_NAME" value="pp.monopoly.client.MonopolyApp"/> | ||||
|     <module name="Projekte.monopoly.client.main"/> | ||||
|     <option name="VM_PARAMETERS" value="-Djava.util.logging.config.file=logging.properties"/> | ||||
|     <option name="WORKING_DIRECTORY" value="$MODULE_WORKING_DIR$"/> | ||||
|     <extension name="coverage"> | ||||
|       <pattern> | ||||
|         <option name="PATTERN" value="pp.monopoly.client.*"/> | ||||
|         <option name="ENABLED" value="true"/> | ||||
|       </pattern> | ||||
|     </extension> | ||||
|     <method v="2"> | ||||
|       <option name="Make" enabled="true"/> | ||||
|     </method> | ||||
|   </configuration> | ||||
| </component> | ||||
| @@ -1,17 +0,0 @@ | ||||
| <component name="ProjectRunConfigurationManager"> | ||||
|   <configuration default="false" name="MonopolyServer" type="Application" factoryName="Application" | ||||
|                  nameIsGenerated="true"> | ||||
|     <option name="MAIN_CLASS_NAME" value="pp.monopoly.server.MonopolyServer"/> | ||||
|     <module name="Projekte.monopoly.server.main"/> | ||||
|     <option name="WORKING_DIRECTORY" value="$MODULE_WORKING_DIR$"/> | ||||
|     <extension name="coverage"> | ||||
|       <pattern> | ||||
|         <option name="PATTERN" value="pp.monopoly.server.*"/> | ||||
|         <option name="ENABLED" value="true"/> | ||||
|       </pattern> | ||||
|     </extension> | ||||
|     <method v="2"> | ||||
|       <option name="Make" enabled="true"/> | ||||
|     </method> | ||||
|   </configuration> | ||||
| </component> | ||||
							
								
								
									
										
											BIN
										
									
								
								Projekte/Alienship.j3o
									
									
									
									
									
										Normal file
									
								
							
							
						
						
							
								
								
									
										
											BIN
										
									
								
								Projekte/Boje.j3o
									
									
									
									
									
										Normal file
									
								
							
							
						
						
							
								
								
									
										
											BIN
										
									
								
								Projekte/KingGeorgeV.j3o
									
									
									
									
									
										Normal file
									
								
							
							
						
						
							
								
								
									
										
											BIN
										
									
								
								Projekte/Marlow66.j3o
									
									
									
									
									
										Normal file
									
								
							
							
						
						
							
								
								
									
										
											BIN
										
									
								
								Projekte/Models/Alienship.j3o
									
									
									
									
									
										Normal file
									
								
							
							
						
						
							
								
								
									
										
											BIN
										
									
								
								Projekte/UX23.j3o
									
									
									
									
									
										Normal file
									
								
							
							
						
						| @@ -10,6 +10,8 @@ dependencies { | ||||
|  | ||||
|     implementation libs.jme3.desktop | ||||
|     implementation libs.jme3.effects | ||||
|     implementation project(path: ':battleship:server') | ||||
|     implementation project(path: ':battleship:server') | ||||
|  | ||||
|     runtimeOnly libs.jme3.awt.dialogs | ||||
|     runtimeOnly libs.jme3.plugins | ||||
|   | ||||
| @@ -22,10 +22,10 @@ import com.simsilica.lemur.GuiGlobals; | ||||
| import com.simsilica.lemur.style.BaseStyles; | ||||
| import pp.battleship.client.gui.BattleAppState; | ||||
| import pp.battleship.client.gui.EditorAppState; | ||||
| import pp.battleship.client.gui.GameMusic; | ||||
| import pp.battleship.client.gui.SeaAppState; | ||||
| import pp.battleship.game.client.BattleshipClient; | ||||
| import pp.battleship.game.client.ClientGameLogic; | ||||
| import pp.battleship.game.client.GameMusic; | ||||
| import pp.battleship.game.client.ServerConnection; | ||||
| import pp.battleship.game.singlemode.BattleshipClientConfig; | ||||
| import pp.battleship.game.singlemode.ServerConnectionMockup; | ||||
| @@ -266,14 +266,24 @@ public class BattleshipApp extends SimpleApplication implements BattleshipClient | ||||
|         flyCam.setEnabled(false); | ||||
|         stateManager.detach(stateManager.getState(StatsAppState.class)); | ||||
|         stateManager.detach(stateManager.getState(DebugKeysAppState.class)); | ||||
|         atttachGameMusic(); | ||||
|  | ||||
|         attachGameSound(); | ||||
|         attachGameMusic(); | ||||
|         stateManager.attachAll(new EditorAppState(), new BattleAppState(), new SeaAppState()); | ||||
|     } | ||||
|  | ||||
|     private void atttachGameMusic() { | ||||
|         final GameMusic gameSound = new GameMusic(); | ||||
|         gameSound.setEnabled(GameMusic.enabledInPreferences()); | ||||
|         stateManager.attach(gameSound); | ||||
|  | ||||
|     } | ||||
|  | ||||
|  | ||||
|  | ||||
|     /** | ||||
|      * Attaches the game sound state and sets its initial enabled state. | ||||
|      * | ||||
|      */ | ||||
|     private void attachGameSound() { | ||||
|         final GameSound gameSound = new GameSound(); | ||||
| @@ -282,15 +292,6 @@ public class BattleshipApp extends SimpleApplication implements BattleshipClient | ||||
|         stateManager.attach(gameSound); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Attaches the background music state and sets its initial enabled state. | ||||
|      */ | ||||
|     private void attachGameMusic() { | ||||
|         final GameMusic gameSound = new GameMusic(); | ||||
|         gameSound.setEnabled(GameMusic.enabledInPreferences()); | ||||
|         stateManager.attach(gameSound); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Updates the application state every frame. | ||||
|      * This method is called once per frame during the game loop. | ||||
|   | ||||
| @@ -12,6 +12,8 @@ import com.jme3.app.state.AbstractAppState; | ||||
| import com.jme3.app.state.AppStateManager; | ||||
| import pp.battleship.game.client.ClientGameLogic; | ||||
|  | ||||
|  | ||||
|  | ||||
| /** | ||||
|  * Abstract class representing a state in the Battleship game. | ||||
|  * Extends the AbstractAppState from jMonkeyEngine to manage state behavior. | ||||
|   | ||||
| @@ -34,7 +34,6 @@ 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. | ||||
| @@ -79,7 +78,6 @@ public class GameSound extends AbstractAppState implements GameEventListener { | ||||
|         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"); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
| @@ -89,7 +87,7 @@ public class GameSound extends AbstractAppState implements GameEventListener { | ||||
|      * @param name The name of the sound file. | ||||
|      * @return The loaded AudioNode. | ||||
|      */ | ||||
|     private AudioNode loadSound(Application app, String name) { | ||||
|     public AudioNode loadSound(Application app, String name) { | ||||
|         try { | ||||
|             final AudioNode sound = new AudioNode(app.getAssetManager(), name, AudioData.DataType.Buffer); | ||||
|             sound.setLooping(false); | ||||
| @@ -126,27 +124,12 @@ public class GameSound extends AbstractAppState implements GameEventListener { | ||||
|             shipDestroyedSound.playInstance(); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Plays the shell flying sound effect. | ||||
|      */ | ||||
|     public void shellFly() { | ||||
|         if (isEnabled() && shellFlyingSound != null) { | ||||
|             shellFlyingSound.playInstance(); | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Handles a recieved {@code SoundEvent} and plays the according sound. | ||||
|      *  | ||||
|      * @param event the Sound event to be processed | ||||
|      */ | ||||
|     @Override | ||||
|     public void receivedEvent(SoundEvent event) { | ||||
|         switch (event.sound()) { | ||||
|             case EXPLOSION -> explosion(); | ||||
|             case SPLASH -> splash(); | ||||
|             case DESTROYED_SHIP -> shipDestroyed(); | ||||
|             case SHELL_FLYING -> shellFly(); | ||||
|         } | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -7,22 +7,20 @@ | ||||
|  | ||||
| package pp.battleship.client; | ||||
|  | ||||
| import java.io.File; | ||||
| import java.io.IOException; | ||||
| import java.util.prefs.Preferences; | ||||
|  | ||||
| import com.simsilica.lemur.Button; | ||||
| import com.simsilica.lemur.Checkbox; | ||||
| import com.simsilica.lemur.Label; | ||||
| import com.simsilica.lemur.style.ElementId; | ||||
|  | ||||
| import static pp.battleship.Resources.lookup; | ||||
| import pp.battleship.client.gui.GameMusic; | ||||
| import pp.battleship.game.client.GameMusic; | ||||
| import pp.dialog.Dialog; | ||||
| import pp.dialog.StateCheckboxModel; | ||||
| import pp.dialog.TextInputDialog; | ||||
| import pp.battleship.client.gui.VolumeSlider; | ||||
|  | ||||
| import java.io.File; | ||||
| import java.io.IOException; | ||||
| import java.util.prefs.Preferences; | ||||
|  | ||||
| import static pp.battleship.Resources.lookup; | ||||
| import static pp.util.PreferencesUtils.getPreferences; | ||||
|  | ||||
| /** | ||||
| @@ -33,33 +31,39 @@ import static pp.util.PreferencesUtils.getPreferences; | ||||
| class Menu extends Dialog { | ||||
|     private static final Preferences PREFERENCES = getPreferences(Menu.class); | ||||
|     private static final String LAST_PATH = "last.file.path"; | ||||
|  | ||||
|   //  private final VolumeSlider slider; | ||||
|     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 slider; | ||||
|  | ||||
|     /** | ||||
|      * Constructs the Menu dialog for the Battleship application.+ | ||||
|      * Constructs the Menu dialog for the Battleship application. | ||||
|      * | ||||
|      * @param app the BattleshipApp instance | ||||
|      */ | ||||
|  | ||||
|     public Menu(BattleshipApp app) { | ||||
|         super(app.getDialogManager()); | ||||
|         this.app = app; | ||||
|         slider = 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))); | ||||
|        // slider = new VolumeSlider(app.getStateManager().getState(GameMusic.class)); | ||||
|         addChild(loadButton) | ||||
|                 .addClickCommands(s -> ifTopDialog(this::loadDialog)); | ||||
|         addChild(saveButton) | ||||
|                 .addClickCommands(s -> ifTopDialog(this::saveDialog)); | ||||
|         addChild(new Button(lookup("menu.return-to-game"))) | ||||
|                 .addClickCommands(s -> ifTopDialog(this::close)); | ||||
|         addChild(new Button(lookup("menu.quit"))) | ||||
|                 .addClickCommands(s -> ifTopDialog(app::closeApp)); | ||||
|         update(); | ||||
|         addChild(new Checkbox(lookup("menu.sound-enabled"), new StateCheckboxModel(app, GameSound.class))); | ||||
|  | ||||
|         addChild(new Checkbox(lookup("menu.background-sound-enabled"), new StateCheckboxModel(app, GameMusic.class))); | ||||
|       //  addChild(slider); | ||||
|  | ||||
|         addChild(slider); | ||||
|  | ||||
|         addChild(loadButton).addClickCommands(s -> ifTopDialog(this::loadDialog)); | ||||
|         addChild(saveButton).addClickCommands(s -> ifTopDialog(this::saveDialog)); | ||||
|         addChild(new Button(lookup("menu.return-to-game"))).addClickCommands(s -> ifTopDialog(this::close)); | ||||
|         addChild(new Button(lookup("menu.quit"))).addClickCommands(s -> ifTopDialog(app::closeApp)); | ||||
|          | ||||
|         update(); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
| @@ -71,11 +75,14 @@ class Menu extends Dialog { | ||||
|         saveButton.setEnabled(app.getGameLogic().maySaveMap()); | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|   /*  @Override | ||||
|     public void update(float delta) { | ||||
|         slider.update(); | ||||
|     } | ||||
|  | ||||
|    */ | ||||
|  | ||||
|  | ||||
|     /** | ||||
|      * As an escape action, this method closes the menu if it is the top dialog. | ||||
|      */ | ||||
|   | ||||
| @@ -7,23 +7,23 @@ | ||||
|  | ||||
| package pp.battleship.client; | ||||
|  | ||||
| import java.lang.System.Logger; | ||||
| import java.lang.System.Logger.Level; | ||||
| import java.util.concurrent.ExecutionException; | ||||
| import java.util.concurrent.Future; | ||||
|  | ||||
| import com.simsilica.lemur.Button; | ||||
| import com.simsilica.lemur.Container; | ||||
| import com.simsilica.lemur.Label; | ||||
| import com.simsilica.lemur.TextField; | ||||
| import com.simsilica.lemur.component.SpringGridLayout; | ||||
|  | ||||
| import static pp.battleship.Resources.lookup; | ||||
| import pp.battleship.server.BattleshipServer; | ||||
| import pp.dialog.Dialog; | ||||
| import pp.dialog.DialogBuilder; | ||||
| import pp.dialog.SimpleDialog; | ||||
|  | ||||
| import java.lang.System.Logger; | ||||
| import java.lang.System.Logger.Level; | ||||
| import java.util.concurrent.ExecutionException; | ||||
| import java.util.concurrent.Future; | ||||
|  | ||||
| import static pp.battleship.Resources.lookup; | ||||
|  | ||||
| /** | ||||
|  * Represents a dialog for setting up a network connection in the Battleship game. | ||||
|  * Allows users to specify the host and port for connecting to a game server. | ||||
| @@ -31,14 +31,13 @@ import pp.dialog.SimpleDialog; | ||||
| class NetworkDialog extends SimpleDialog { | ||||
|     private static final Logger LOGGER = System.getLogger(NetworkDialog.class.getName()); | ||||
|     private static final String LOCALHOST = "localhost"; //NON-NLS | ||||
|     private static final String DEFAULT_PORT = "42069"; //NON-NLS | ||||
|     private static final String DEFAULT_PORT = "1234"; //NON-NLS | ||||
|     private final NetworkSupport network; | ||||
|     private final TextField host = new TextField(LOCALHOST); | ||||
|     private final TextField port = new TextField(DEFAULT_PORT); | ||||
|     // private final Button serverButton = new Button(lookup("client.server-star")); | ||||
|     private final Button serverButton = new Button(lookup("client.server-start")); | ||||
|     private String hostname; | ||||
|     private int portNumber; | ||||
|     private final Button serverButton = new Button(lookup("client.server-start")); | ||||
|     private Future<Object> connectionFuture; | ||||
|     private Dialog progressDialog; | ||||
|  | ||||
| @@ -69,10 +68,11 @@ class NetworkDialog extends SimpleDialog { | ||||
|                      .setOkClose(false) | ||||
|                      .setNoClose(false) | ||||
|                      .build(this); | ||||
|          | ||||
|         //Add the button to start the sever | ||||
|         addChild(serverButton).addClickCommands(s -> ifTopDialog(this::startServerInThread)); | ||||
|     } | ||||
|     //Add the button to start the sever | ||||
|  | ||||
|  | ||||
|  | ||||
|     /** | ||||
|      * Handles the action for the connect button in the connection dialog. | ||||
| @@ -157,7 +157,6 @@ class NetworkDialog extends SimpleDialog { | ||||
|         network.getApp().errorDialog(lookup("server.connection.failed")); | ||||
|         network.getApp().setInfoText(e.getLocalizedMessage()); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Starts the server in a separate thread. | ||||
|      */ | ||||
| @@ -174,4 +173,5 @@ class NetworkDialog extends SimpleDialog { | ||||
|         }); | ||||
|         serverThread.start(); | ||||
|     } | ||||
|  | ||||
| } | ||||
|   | ||||
| @@ -1,122 +0,0 @@ | ||||
| package pp.battleship.client.gui; | ||||
|  | ||||
| import static pp.util.PreferencesUtils.getPreferences; | ||||
|  | ||||
| import java.lang.System.Logger; | ||||
| import java.lang.System.Logger.Level; | ||||
| import java.util.prefs.Preferences; | ||||
|  | ||||
| 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; | ||||
|  | ||||
| /** | ||||
|  * Handles the background music beeing played. Is able to start and stop the music. Set the Volume of the Audio. | ||||
|  */ | ||||
| public class GameMusic extends AbstractAppState{ | ||||
|     private static final Logger LOGGER = System.getLogger(GameMusic.class.getName()); | ||||
|     private static final Preferences PREFERENCES = getPreferences(GameMusic.class); | ||||
|     private static final String ENABLED_PREF = "enabled"; //NON-NLS | ||||
|     private static final String VOLUME_PREF = "volume"; //NON-NLS | ||||
|  | ||||
|     private AudioNode music; | ||||
|  | ||||
|     /** | ||||
|      * Checks if sound is enabled in the preferences. | ||||
|      * | ||||
|      * @return {@code true} if sound is enabled, {@code false} otherwise. | ||||
|      */ | ||||
|     public static boolean enabledInPreferences() { | ||||
|         return PREFERENCES.getBoolean(ENABLED_PREF, true); | ||||
|     } | ||||
|  | ||||
|         /** | ||||
|      * Checks if sound is enabled in the preferences. | ||||
|      * | ||||
|      * @return float to which the volume is set | ||||
|      */ | ||||
|     public static float volumeInPreferences() { | ||||
|         return PREFERENCES.getFloat(VOLUME_PREF, 0.5f); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Initializes the sound effects for the game. | ||||
|      * Overrides {@link AbstractAppState#initialize(AppStateManager, Application)} | ||||
|      * | ||||
|      * @param stateManager The state manager | ||||
|      * @param app          The application | ||||
|      */ | ||||
|     @Override | ||||
|     public void initialize(AppStateManager stateManager, Application app) { | ||||
|         super.initialize(stateManager, app); | ||||
|         music = loadSound(app, "Sound/background.ogg"); | ||||
|         setVolume(volumeInPreferences()); | ||||
|         music.setLooping(true); | ||||
|         if (isEnabled() && music != null) { | ||||
|             music.play(); | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Loads a sound from the specified file. | ||||
|      * | ||||
|      * @param app  The application | ||||
|      * @param name The name of the sound file. | ||||
|      * @return The loaded AudioNode. | ||||
|      */ | ||||
|     private AudioNode loadSound(Application app, String name) { | ||||
|         try { | ||||
|             final AudioNode sound = new AudioNode(app.getAssetManager(), name, AudioData.DataType.Buffer); | ||||
|             sound.setLooping(false); | ||||
|             sound.setPositional(false); | ||||
|             return sound; | ||||
|         } | ||||
|         catch (AssetLoadException | AssetNotFoundException ex) { | ||||
|             LOGGER.log(Level.ERROR, ex.getMessage(), ex); | ||||
|         } | ||||
|         return null; | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Sets the enabled state of this AppState. | ||||
|      * Overrides {@link com.jme3.app.state.AbstractAppState#setEnabled(boolean)} | ||||
|      * | ||||
|      * @param enabled {@code true} to enable the AppState, {@code false} to disable it. | ||||
|      */ | ||||
|     @Override | ||||
|     public void setEnabled(boolean enabled) { | ||||
|         if (isEnabled() == enabled) return; | ||||
|  | ||||
|         if (music != null) { | ||||
|             if (enabled) { | ||||
|                 music.play(); | ||||
|             } else { | ||||
|                 music.stop(); | ||||
|             } | ||||
|         } | ||||
|      | ||||
|         super.setEnabled(enabled); | ||||
|         LOGGER.log(Level.INFO, "Sound enabled: {0}", enabled); //NON-NLS | ||||
|         PREFERENCES.putBoolean(ENABLED_PREF, enabled); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Toggles the game sound on or off. | ||||
|      */ | ||||
|     public void toggleSound() { | ||||
|         setEnabled(!isEnabled()); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Sets the volume of music | ||||
|      * @param vol the volume to which the music should be set | ||||
|      */ | ||||
|     public void setVolume(float vol){ | ||||
|         music.setVolume(vol); | ||||
|         PREFERENCES.putFloat(VOLUME_PREF, vol); | ||||
|     } | ||||
| } | ||||
| @@ -28,7 +28,7 @@ import pp.util.Position; | ||||
|  * and interaction between the model and the view. | ||||
|  */ | ||||
| class MapView { | ||||
|     public static final float FIELD_SIZE = 40f; | ||||
|     private 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,23 +7,16 @@ | ||||
|  | ||||
| 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.math.Vector3f; | ||||
| 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 | ||||
| @@ -45,6 +38,7 @@ class MapViewSynchronizer extends ShipMapSynchronizer { | ||||
|  | ||||
|     // The MapView associated with this synchronizer | ||||
|     private final MapView view; | ||||
|     private Shell shell; | ||||
|  | ||||
|     /** | ||||
|      * Constructs a new MapViewSynchronizer for the given MapView. | ||||
| @@ -131,24 +125,16 @@ class MapViewSynchronizer extends ShipMapSynchronizer { | ||||
|     private Geometry shipLine(float x1, float y1, float x2, float y2, ColorRGBA color) { | ||||
|         return view.getApp().getDraw().makeFatLine(x1, y1, x2, y2, SHIP_DEPTH, color, SHIP_LINE_WIDTH); | ||||
|     } | ||||
|  | ||||
|         /** | ||||
|      * 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; | ||||
|     public void update(float deltaTime) { | ||||
|         if (shell != null) { | ||||
|             shell.updatePosition(deltaTime); | ||||
|             drawShell(shell.getCurrentPosition()); | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     private void drawShell(Vector3f position){ | ||||
|         //TODO implement | ||||
|     } | ||||
|  | ||||
|  | ||||
| } | ||||
|   | ||||
| @@ -240,7 +240,6 @@ public class ParticleEffectFactory { | ||||
|  | ||||
|         return smokeEmitter; | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Creates a one-time water splash particle emitter. | ||||
|      * | ||||
| @@ -282,4 +281,5 @@ public class ParticleEffectFactory { | ||||
|  | ||||
|         return waterSplash; | ||||
|     } | ||||
|  | ||||
| } | ||||
|   | ||||
| @@ -20,10 +20,8 @@ import com.jme3.scene.shape.Cylinder; | ||||
| 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 static java.util.Objects.requireNonNull; | ||||
| import static pp.util.FloatMath.HALF_PI; | ||||
| import static pp.util.FloatMath.PI; | ||||
| @@ -36,11 +34,13 @@ import static pp.util.FloatMath.PI; | ||||
|  */ | ||||
| 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 BOAT_SMALL_MODEL = "Models/BoatSmall/12219_boat_v2_L2.j3o"; //NON-NLS | ||||
|     private static final String CV_MODEL = "Models/CV/CV.j3o"; //NON-NLS | ||||
|     private static final String BATTLE_MODEL = "Models/Battle/Battle.j3o"; //NON-NLS | ||||
|     private static final String LIGHTING = "Common/MatDefs/Light/Lighting.j3md"; | ||||
|     private static final String UX23 = "Models/UX23/UX23.j3o"; | ||||
|     private static final String BOJE = "Models/Boje/Boje.j3o"; | ||||
|  | ||||
|     private static final String ALIENSHIP = "Models/Alienship/Alienship.j3o"; | ||||
|     private static final String MARLOW66 = "Models/Marlow66/Marlow66.j3o"; | ||||
|     private static final String KING_GEORGE_V_MODEL = "Models/KingGeorgeV/KingGeorgeV.j3o"; | ||||
|  | ||||
|     private static final String COLOR = "Color"; //NON-NLS | ||||
|     private static final String SHIP = "ship"; //NON-NLS | ||||
|     private static final String SHOT = "shot"; //NON-NLS | ||||
| @@ -80,49 +80,13 @@ class SeaSynchronizer extends ShipMapSynchronizer { | ||||
|         return shot.isHit() ? handleHit(shot) : handleMiss(shot); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Handles a miss by representing it with a blue cylinder | ||||
|      * and attaching a water splash effect to it. | ||||
|      * @param shot the shot to be processed | ||||
|      * @return a Spatial simulating a miss with water splash effect | ||||
|      */ | ||||
|     private Spatial handleMiss(Shot shot) { | ||||
|         Node shotNode = new Node("ShotNode"); | ||||
|         Geometry shotCylinder = createCylinder(shot); | ||||
|         shotNode.attachChild(shotCylinder); | ||||
|         ParticleEmitter waterSplash = particleFactory.createWaterSplash(); | ||||
|         waterSplash.setLocalTranslation(shot.getY() + 0.5f, 0f, shot.getX() + 0.5f); | ||||
|         shotNode.attachChild(waterSplash); | ||||
|         waterSplash.emitAllParticles(); | ||||
|  | ||||
|         return shotNode; | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Handles the sinking animation and removal of ship if destroyed | ||||
|      * @param ship the ship to be sunk | ||||
|      */ | ||||
|     private void sinkAndRemoveShip(Battleship ship) { | ||||
|         Battleship wilkeningklaunichtmeinencode = ship; | ||||
|         final Node shipNode = (Node) getSpatial(wilkeningklaunichtmeinencode); | ||||
|         if (shipNode == null) return; | ||||
|  | ||||
|         // Add sinking control to animate the sinking | ||||
|         shipNode.addControl(new SinkingControl(shipNode)); | ||||
|  | ||||
|         // Add particle effects | ||||
|         ParticleEmitter bubbles = particleFactory.createWaterSplash(); | ||||
|         bubbles.setLocalTranslation(shipNode.getLocalTranslation()); | ||||
|         shipNode.attachChild(bubbles); | ||||
|         bubbles.emitAllParticles(); | ||||
|     } | ||||
|  | ||||
|         /** | ||||
|      * Handles a hit by attaching its representation to the node that | ||||
|      * contains the ship model as a child so that it moves with the ship. | ||||
|      * | ||||
|      * @param shot a hit | ||||
|      * @return always null to prevent the representation from being attached to the items node as well | ||||
|      * @return always null to prevent the representation from being attached | ||||
|      * to the items node as well | ||||
|      */ | ||||
|     private Spatial handleHit(Shot shot) { | ||||
|         final Battleship ship = requireNonNull(map.findShipAt(shot), "Missing ship"); | ||||
| @@ -170,13 +134,20 @@ class SeaSynchronizer extends ShipMapSynchronizer { | ||||
|         flame.emitAllParticles(); | ||||
|         roundSpark.emitAllParticles(); | ||||
|          | ||||
|         //Checks if ship is destroyed and triggers animation accordingly | ||||
|         if (ship.isDestroyed()) { | ||||
|             sinkAndRemoveShip(ship); | ||||
|         } | ||||
|  | ||||
|         return null; | ||||
|     } | ||||
|     private Spatial handleMiss(Shot shot) { | ||||
|         Node shotNode = new Node("ShotNode"); | ||||
|         Geometry shotCylinder = createCylinder(shot); | ||||
|         shotNode.attachChild(shotCylinder); | ||||
|         ParticleEmitter waterSplash = particleFactory.createWaterSplash(); | ||||
|         waterSplash.setLocalTranslation(shot.getY() + 0.5f, 0f, shot.getX() + 0.5f); | ||||
|         shotNode.attachChild(waterSplash); | ||||
|         waterSplash.emitAllParticles(); | ||||
|  | ||||
|         return shotNode; | ||||
|     } | ||||
|  | ||||
|  | ||||
|     /** | ||||
|      * Creates a cylinder geometry representing the specified shot. | ||||
| @@ -230,9 +201,9 @@ class SeaSynchronizer extends ShipMapSynchronizer { | ||||
|     private Spatial createShip(Battleship ship) { | ||||
|         switch (ship.getLength()) { | ||||
|             case 4: return createBattleship(ship); | ||||
|             case 3: return createCV(ship); | ||||
|             case 2: return createBattle(ship); | ||||
|             case 1: return createSmallship(ship); | ||||
|             case 3: return createMarlow66(ship); | ||||
|             case 2: return createUX23(ship); | ||||
|             case 1: return createAllienship(ship); | ||||
|             default: return createBox(ship); | ||||
|         } | ||||
|     } | ||||
| @@ -290,51 +261,36 @@ class SeaSynchronizer extends ShipMapSynchronizer { | ||||
|         return model; | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Creates a detailed 3D model to represent a small tug boat. | ||||
|      * | ||||
|      * @param ship the battleship to be represented | ||||
|      * @return the spatial representing a small tug boat | ||||
|      */ | ||||
|     private Spatial createSmallship(Battleship ship) { | ||||
|         final Spatial model = app.getAssetManager().loadModel(BOAT_SMALL_MODEL); | ||||
|     private Spatial createAllienship(Battleship ship) { | ||||
|         final Spatial model = app.getAssetManager().loadModel(ALIENSHIP); | ||||
|  | ||||
|         model.rotate(-HALF_PI, calculateRotationAngle(ship.getRot()), 0f); | ||||
|         model.scale(0.0005f); | ||||
|         model.scale(0.10f); | ||||
|         model.setShadowMode(ShadowMode.CastAndReceive); | ||||
|  | ||||
|         return model;} | ||||
|     private Spatial createUX23(Battleship ship) { | ||||
|         final Spatial model = app.getAssetManager().loadModel(UX23); | ||||
|  | ||||
|         model.rotate(-HALF_PI, calculateRotationAngle(ship.getRot()), 0f); | ||||
|         // model.move(0f, -0.05f, 0f); | ||||
|         model.scale(0.89f); | ||||
|         model.setShadowMode(ShadowMode.CastAndReceive); | ||||
|  | ||||
|         return model; | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Creates a detailed 3D model to represent a "German WWII UBoat". | ||||
|      * | ||||
|      * @param ship the battleship to be represented | ||||
|      * @return the spatial representing the "German WWII UBoat" | ||||
|      */ | ||||
|     private Spatial createCV(Battleship ship) { | ||||
|         final Spatial model = app.getAssetManager().loadModel(CV_MODEL); | ||||
|  | ||||
|  | ||||
|  | ||||
|  | ||||
|  | ||||
|     private Spatial createMarlow66(Battleship ship) { | ||||
|         final Spatial model = app.getAssetManager().loadModel(MARLOW66); | ||||
|  | ||||
|         model.rotate(0f, calculateRotationAngle(ship.getRot()), 0f); | ||||
|         model.move(0f, 0.25f, 0f); | ||||
|         model.scale(0.85f); | ||||
|         model.setShadowMode(ShadowMode.CastAndReceive); | ||||
|  | ||||
|         return model; | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Creates a detailed 3D model to represent a battleship. | ||||
|      * | ||||
|      * @param ship the battleship to be represented | ||||
|      * @return the spatial representing a battleship | ||||
|      */ | ||||
|     private Spatial createBattle(Battleship ship) { | ||||
|         final Spatial model = app.getAssetManager().loadModel(BATTLE_MODEL); | ||||
|  | ||||
|         model.rotate(-HALF_PI, calculateRotationAngle(ship.getRot()), 0f); | ||||
|         model.move(0f, -0.06f, 0f); | ||||
|         model.scale(0.27f); | ||||
|         model.scale(0.135f); | ||||
|         model.setShadowMode(ShadowMode.CastAndReceive); | ||||
|  | ||||
|         return model; | ||||
| @@ -354,25 +310,4 @@ class SeaSynchronizer extends ShipMapSynchronizer { | ||||
|             case UP -> PI; | ||||
|         }; | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * 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; | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -1,57 +0,0 @@ | ||||
| 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); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * 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-specific behavior required for this control | ||||
|     } | ||||
| } | ||||
| @@ -1,58 +0,0 @@ | ||||
| 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.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 = new Vector3f(pos.z + 0.5f, pos.y, pos.x + 0.5f); | ||||
|         fixed.setY(pos.y); | ||||
|         spatial.setLocalTranslation(fixed); | ||||
|         spatial.setLocalRotation(shell.getRotation()); | ||||
|         spatial.rotate(PI/2,0,0); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * 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-specific behavior required for this control | ||||
|     } | ||||
| } | ||||
| @@ -1,61 +0,0 @@ | ||||
| package pp.battleship.client.gui; | ||||
|  | ||||
| import com.jme3.scene.control.AbstractControl; | ||||
| import com.jme3.math.Vector3f; | ||||
| import com.jme3.renderer.RenderManager; | ||||
| import com.jme3.renderer.ViewPort; | ||||
| import com.jme3.scene.Node; | ||||
|  | ||||
| /** | ||||
|  * Control that handles the sinking effect for destroyed ships. | ||||
|  * It will gradually move the ship downwards and then remove it from the scene. | ||||
|  */ | ||||
| class SinkingControl extends AbstractControl { | ||||
|     private static final float SINK_DURATION = 5f;  // Duration of the sinking animation | ||||
|     private static final float SINK_SPEED = 0.1f;   // Speed at which the ship sinks | ||||
|     private float elapsedTime = 0; | ||||
|  | ||||
|     private final Node shipNode; | ||||
|  | ||||
|     /** | ||||
|      * Constructs a {@code SinkingControl} object with the shipNode to be to be sunk | ||||
|      * @param shipNode the node to handeld | ||||
|      */ | ||||
|     public SinkingControl(Node shipNode) { | ||||
|         this.shipNode = shipNode; | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Updated the Map to sink the ship | ||||
|      *  | ||||
|      * @param tpf time per frame | ||||
|      */ | ||||
|     @Override | ||||
|     protected void controlUpdate(float tpf) { | ||||
|         // Update the sinking effect | ||||
|         elapsedTime += tpf; | ||||
|  | ||||
|         // Move the ship down over time | ||||
|         Vector3f currentPos = shipNode.getLocalTranslation(); | ||||
|         shipNode.setLocalTranslation(currentPos.x, currentPos.y - SINK_SPEED * tpf, currentPos.z); | ||||
|  | ||||
|         // Check if sinking duration has passed | ||||
|         if (elapsedTime >= SINK_DURATION) { | ||||
|             // Remove the ship from the scene | ||||
|             shipNode.removeFromParent(); | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * 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-related code needed | ||||
|     } | ||||
| } | ||||
| @@ -1,36 +0,0 @@ | ||||
| package pp.battleship.client.gui; | ||||
|  | ||||
| import com.simsilica.lemur.Slider; | ||||
|  | ||||
| /** | ||||
|  * The VolumeSlider class represents the Volume Slider in the Menu. | ||||
|  * It extends the Slider class and provides functionalities for setting the music volume, | ||||
|  * with the help of the Slider in the GUI  | ||||
|  */ | ||||
| public class VolumeSlider extends Slider { | ||||
|  | ||||
|     private final GameMusic music; | ||||
|     private double vol; | ||||
|  | ||||
|     /** | ||||
|      * Constructs the Volume Slider for the Menu dialog | ||||
|      * @param music the music instance | ||||
|      */ | ||||
|     public VolumeSlider(GameMusic music) { | ||||
|         super(); | ||||
|         this.music = music; | ||||
|         vol = GameMusic.volumeInPreferences(); | ||||
|         getModel().setPercent(vol); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * when triggered it updates the volume to the value set with the slider | ||||
|      */ | ||||
|     public void update() { | ||||
|         if (vol != getModel().getPercent()) { | ||||
|             vol = getModel().getPercent(); | ||||
|             music.setVolume( (float) vol); | ||||
|         } | ||||
|     } | ||||
|      | ||||
| } | ||||
| @@ -1,182 +0,0 @@ | ||||
| //////////////////////////////////////// | ||||
| // Programming project code | ||||
| // UniBw M, 2022, 2023, 2024 | ||||
| // www.unibw.de/inf2 | ||||
| // (c) Mark Minas (mark.minas@unibw.de) | ||||
| //////////////////////////////////////// | ||||
|  | ||||
| package pp.battleship.server; | ||||
|  | ||||
| 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 BattleshipServer implements MessageListener<HostedConnection>, ConnectionListener, ServerSender { | ||||
|     private static final Logger LOGGER = System.getLogger(BattleshipServer.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<ReceivedMessage> 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()); | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Starts the Battleships server. | ||||
|      */ | ||||
|     public static void main(String[] args) { | ||||
|         new BattleshipServer().run(); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Creates the server. | ||||
|      */ | ||||
|     BattleshipServer() { | ||||
|         config.readFromIfExists(CONFIG_FILE); | ||||
|         LOGGER.log(Level.INFO, "Configuration: {0}", config); //NON-NLS | ||||
|         logic = new ServerGameLogic(this, config); | ||||
|     } | ||||
|  | ||||
|     public void run() { | ||||
|         startServer(); | ||||
|         while (true) | ||||
|             processNextMessage(); | ||||
|     } | ||||
|  | ||||
|     private void startServer() { | ||||
|         try { | ||||
|             LOGGER.log(Level.INFO, "Starting server..."); //NON-NLS | ||||
|             myServer = Network.createServer(config.getPort()); | ||||
|             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); | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     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(); | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     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); | ||||
|     } | ||||
|  | ||||
|     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 ReceivedMessage(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); | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     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 | ||||
|     } | ||||
| } | ||||
| @@ -1,17 +0,0 @@ | ||||
| //////////////////////////////////////// | ||||
| // Programming project code | ||||
| // UniBw M, 2022, 2023, 2024 | ||||
| // www.unibw.de/inf2 | ||||
| // (c) Mark Minas (mark.minas@unibw.de) | ||||
| //////////////////////////////////////// | ||||
|  | ||||
| package pp.battleship.server; | ||||
|  | ||||
| import pp.battleship.message.client.ClientInterpreter; | ||||
| import pp.battleship.message.client.ClientMessage; | ||||
|  | ||||
| record ReceivedMessage(ClientMessage message, int from) { | ||||
|     void process(ClientInterpreter interpreter) { | ||||
|         message.accept(interpreter, from); | ||||
|     } | ||||
| } | ||||
| Before Width: | Height: | Size: 16 KiB After Width: | Height: | Size: 16 KiB | 
| Before Width: | Height: | Size: 28 KiB After Width: | Height: | Size: 28 KiB | 
| Before Width: | Height: | Size: 46 KiB After Width: | Height: | Size: 46 KiB | 
| Before Width: | Height: | Size: 63 KiB After Width: | Height: | Size: 63 KiB | 
| Before Width: | Height: | Size: 2.0 KiB After Width: | Height: | Size: 2.0 KiB | 
| Before Width: | Height: | Size: 16 KiB After Width: | Height: | Size: 16 KiB | 
| Before Width: | Height: | Size: 32 KiB After Width: | Height: | Size: 32 KiB | 
| Before Width: | Height: | Size: 1.8 KiB After Width: | Height: | Size: 1.8 KiB | 
| After Width: | Height: | Size: 78 KiB | 
| Before Width: | Height: | Size: 168 KiB | 
| Before Width: | Height: | Size: 56 KiB | 
| Before Width: | Height: | Size: 166 KiB | 
| Before Width: | Height: | Size: 98 KiB | 
| Before Width: | Height: | Size: 35 KiB | 
| Before Width: | Height: | Size: 54 KiB | 
| Before Width: | Height: | Size: 9.3 KiB | 
| Before Width: | Height: | Size: 66 KiB | 
| Before Width: | Height: | Size: 3.2 KiB | 
| Before Width: | Height: | Size: 30 KiB | 
| Before Width: | Height: | Size: 10 KiB | 
| Before Width: | Height: | Size: 9.4 KiB | 
| Before Width: | Height: | Size: 264 KiB | 
| Before Width: | Height: | Size: 80 KiB | 
| Before Width: | Height: | Size: 91 KiB | 
| Before Width: | Height: | Size: 33 KiB | 
| Before Width: | Height: | Size: 98 B | 
| Before Width: | Height: | Size: 79 B | 
| Before Width: | Height: | Size: 79 B | 
| Before Width: | Height: | Size: 79 B | 
| Before Width: | Height: | Size: 78 B | 
| Before Width: | Height: | Size: 78 B | 
| Before Width: | Height: | Size: 79 B | 
| Before Width: | Height: | Size: 78 B | 
| Before Width: | Height: | Size: 78 B | 
| Before Width: | Height: | Size: 78 B | 
| Before Width: | Height: | Size: 79 B | 
| Before Width: | Height: | Size: 79 B | 
| Before Width: | Height: | Size: 79 B | 
| Before Width: | Height: | Size: 78 B | 
| Before Width: | Height: | Size: 78 B | 
| Before Width: | Height: | Size: 79 B | 
| Before Width: | Height: | Size: 79 B | 
| Before Width: | Height: | Size: 79 B | 
| Before Width: | Height: | Size: 78 B | 
| Before Width: | Height: | Size: 79 B | 
| Before Width: | Height: | Size: 79 B | 
| Before Width: | Height: | Size: 78 B | 
| Before Width: | Height: | Size: 78 B | 
| Before Width: | Height: | Size: 78 B | 
| Before Width: | Height: | Size: 78 B | 
| Before Width: | Height: | Size: 79 B | 
| Before Width: | Height: | Size: 79 B | 
| Before Width: | Height: | Size: 79 B | 
| Before Width: | Height: | Size: 78 B | 
| Before Width: | Height: | Size: 78 B | 
| Before Width: | Height: | Size: 78 B | 
| Before Width: | Height: | Size: 79 B | 
| Before Width: | Height: | Size: 96 KiB | 
| Before Width: | Height: | Size: 82 KiB | 
| Before Width: | Height: | Size: 670 KiB | 
| After Width: | Height: | Size: 235 KiB | 
| After Width: | Height: | Size: 76 KiB | 
| After Width: | Height: | Size: 127 KiB | 
| After Width: | Height: | Size: 70 KiB | 
| After Width: | Height: | Size: 142 KiB | 
| After Width: | Height: | Size: 3.1 MiB | 
| After Width: | Height: | Size: 3.2 MiB |