From 00a8f116025957539d57d5c8c7d61a8a96cefbaa Mon Sep 17 00:00:00 2001 From: Filip Szepielewicz Date: Mon, 14 Oct 2024 07:39:31 +0200 Subject: [PATCH] task 13 + debug --- Projekte/battleship/client/client.properties | 2 +- .../pp/battleship/client/BattleshipApp.java | 12 +-- .../java/pp/battleship/client/GameSound.java | 2 +- .../main/java/pp/battleship/client/Menu.java | 3 +- .../pp/battleship/client/NetworkDialog.java | 5 +- .../pp/battleship/client/gui/GameMusic.java | 4 +- .../client/gui/MapViewSynchronizer.java | 18 ++++ .../battleship/client/gui/VolumeSlider.java | 4 +- .../sever => server}/BattleshipServer.java | 11 ++- .../sever => server}/ReceivedMessage.java | 11 ++- .../battleship/game/client/BattleState.java | 3 - .../game/client/ShootingAnimationState.java | 93 +++++++++++++++++++ .../game/server/ServerGameLogic.java | 49 ++++++++-- .../battleship/game/server/ServerState.java | 6 +- .../battleship/game/singlemode/Copycat.java | 7 ++ .../message/client/ClientInterpreter.java | 3 + .../client/ShellAnimationFinishedMessage.java | 10 ++ .../main/java/pp/battleship/model/Shell.java | 47 ++++++++++ .../pp/battleship/model/ShellControl.java | 48 ++++++++++ .../java/pp/battleship/model/ShipMap.java | 2 - Projekte/settings.gradle | 1 + 21 files changed, 304 insertions(+), 37 deletions(-) rename Projekte/battleship/client/src/main/java/pp/battleship/{client/sever => server}/BattleshipServer.java (96%) rename Projekte/battleship/client/src/main/java/pp/battleship/{client/sever => server}/ReceivedMessage.java (53%) create mode 100644 Projekte/battleship/model/src/main/java/pp/battleship/game/client/ShootingAnimationState.java create mode 100644 Projekte/battleship/model/src/main/java/pp/battleship/message/client/ShellAnimationFinishedMessage.java create mode 100644 Projekte/battleship/model/src/main/java/pp/battleship/model/Shell.java create mode 100644 Projekte/battleship/model/src/main/java/pp/battleship/model/ShellControl.java diff --git a/Projekte/battleship/client/client.properties b/Projekte/battleship/client/client.properties index 89d59df..4403fa3 100644 --- a/Projekte/battleship/client/client.properties +++ b/Projekte/battleship/client/client.properties @@ -9,7 +9,7 @@ # # Specifies the map used by the opponent in single mode. # Single mode is activated if this property is set. -map.opponent=maps/map2.json +#map.opponent=maps/map2.json # # Specifies the map used by the player in single mode. # The player must define their own map if this property is not set. diff --git a/Projekte/battleship/client/src/main/java/pp/battleship/client/BattleshipApp.java b/Projekte/battleship/client/src/main/java/pp/battleship/client/BattleshipApp.java index e3acfe2..25ef74b 100644 --- a/Projekte/battleship/client/src/main/java/pp/battleship/client/BattleshipApp.java +++ b/Projekte/battleship/client/src/main/java/pp/battleship/client/BattleshipApp.java @@ -249,8 +249,8 @@ public class BattleshipApp extends SimpleApplication implements BattleshipClient private void setupInput() { inputManager.deleteMapping(INPUT_MAPPING_EXIT); inputManager.setCursorVisible(false); - inputManager.addMapping(CLICK, new MouseButtonTrigger(MouseInput.BUTTON_LEFT)); inputManager.addMapping(ESC, new KeyTrigger(KeyInput.KEY_ESCAPE)); + inputManager.addMapping(CLICK, new MouseButtonTrigger(MouseInput.BUTTON_LEFT)); inputManager.addListener(escapeListener, ESC); } @@ -309,18 +309,14 @@ public class BattleshipApp extends SimpleApplication implements BattleshipClient * * @param isPressed Indicates whether the Escape key is pressed. */ - private void escape(boolean isPressed) { if (!isPressed) return; - LOGGER.log(Level.INFO, "Escape key pressed"); // Debugging - if (dialogManager.showsDialog()) { - LOGGER.log(Level.INFO, "Closing current dialog"); + if (dialogManager.showsDialog()) dialogManager.escape(); - } else { - LOGGER.log(Level.INFO, "Opening menu"); + else new Menu(this).open(); - } } + /** * Returns the {@link Draw} instance used for rendering graphical elements in the game. * diff --git a/Projekte/battleship/client/src/main/java/pp/battleship/client/GameSound.java b/Projekte/battleship/client/src/main/java/pp/battleship/client/GameSound.java index 60c4af8..0fdcef6 100644 --- a/Projekte/battleship/client/src/main/java/pp/battleship/client/GameSound.java +++ b/Projekte/battleship/client/src/main/java/pp/battleship/client/GameSound.java @@ -67,7 +67,7 @@ public class GameSound extends AbstractAppState implements GameEventListener { /** * Initializes the sound effects for the game. - * Overrides {@link com.jme3.app.state.AbstractAppState#initialize(com.jme3.app.state.AppStateManager, com.jme3.app.Application)} + * Overrides {@link AbstractAppState#initialize(AppStateManager, Application)} * * @param stateManager The state manager * @param app The application diff --git a/Projekte/battleship/client/src/main/java/pp/battleship/client/Menu.java b/Projekte/battleship/client/src/main/java/pp/battleship/client/Menu.java index 6c2a299..9143899 100644 --- a/Projekte/battleship/client/src/main/java/pp/battleship/client/Menu.java +++ b/Projekte/battleship/client/src/main/java/pp/battleship/client/Menu.java @@ -11,6 +11,7 @@ import com.simsilica.lemur.Button; import com.simsilica.lemur.Checkbox; import com.simsilica.lemur.Label; import com.simsilica.lemur.style.ElementId; + import pp.battleship.client.gui.GameMusic; import pp.battleship.client.gui.VolumeSlider; import pp.dialog.Dialog; @@ -92,7 +93,7 @@ class Menu extends Dialog { * Executes a file action. * * @param file the file to be processed - * @throws java.io.IOException if an I/O error occurs + * @throws IOException if an I/O error occurs */ void run(File file) throws IOException; } diff --git a/Projekte/battleship/client/src/main/java/pp/battleship/client/NetworkDialog.java b/Projekte/battleship/client/src/main/java/pp/battleship/client/NetworkDialog.java index 460fc14..2e043c1 100644 --- a/Projekte/battleship/client/src/main/java/pp/battleship/client/NetworkDialog.java +++ b/Projekte/battleship/client/src/main/java/pp/battleship/client/NetworkDialog.java @@ -13,7 +13,7 @@ import com.simsilica.lemur.Label; import com.simsilica.lemur.TextField; import com.simsilica.lemur.component.SpringGridLayout; -import pp.battleship.client.sever.BattleshipServer; +import pp.battleship.server.BattleshipServer; import pp.dialog.Dialog; import pp.dialog.DialogBuilder; import pp.dialog.SimpleDialog; @@ -36,6 +36,7 @@ class NetworkDialog extends SimpleDialog { 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; @@ -69,7 +70,7 @@ class NetworkDialog extends SimpleDialog { .setOkClose(false) .setNoClose(false) .build(this); - + //Add the button to start the sever addChild(serverButton).addClickCommands(s -> ifTopDialog(this::startServerInThread)); } diff --git a/Projekte/battleship/client/src/main/java/pp/battleship/client/gui/GameMusic.java b/Projekte/battleship/client/src/main/java/pp/battleship/client/gui/GameMusic.java index 975177a..8b3c44e 100644 --- a/Projekte/battleship/client/src/main/java/pp/battleship/client/gui/GameMusic.java +++ b/Projekte/battleship/client/src/main/java/pp/battleship/client/gui/GameMusic.java @@ -31,7 +31,7 @@ public class GameMusic extends AbstractAppState{ return PREFERENCES.getBoolean(ENABLED_PREF, true); } - /** + /** * Checks if sound is enabled in the preferences. * * @return float to which the volume is set @@ -95,7 +95,7 @@ public class GameMusic extends AbstractAppState{ music.stop(); } } - + super.setEnabled(enabled); LOGGER.log(Level.INFO, "Sound enabled: {0}", enabled); //NON-NLS PREFERENCES.putBoolean(ENABLED_PREF, enabled); diff --git a/Projekte/battleship/client/src/main/java/pp/battleship/client/gui/MapViewSynchronizer.java b/Projekte/battleship/client/src/main/java/pp/battleship/client/gui/MapViewSynchronizer.java index a6075fd..453e1d6 100644 --- a/Projekte/battleship/client/src/main/java/pp/battleship/client/gui/MapViewSynchronizer.java +++ b/Projekte/battleship/client/src/main/java/pp/battleship/client/gui/MapViewSynchronizer.java @@ -8,10 +8,12 @@ package pp.battleship.client.gui; 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 pp.battleship.model.Battleship; +import pp.battleship.model.Shell; import pp.battleship.model.Shot; import pp.util.Position; @@ -36,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. @@ -122,4 +125,19 @@ 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); } + + /** + * + * @param deltaTime + */ + public void update(float deltaTime) { + if (shell != null) { + shell.updatePosition(deltaTime); + drawShell(shell.getCurrentPosition()); + } + } + + private void drawShell(Vector3f position) { + // Methode zum Zeichnen des Geschosses auf der 2D-Karte TODO + } } diff --git a/Projekte/battleship/client/src/main/java/pp/battleship/client/gui/VolumeSlider.java b/Projekte/battleship/client/src/main/java/pp/battleship/client/gui/VolumeSlider.java index df8a4b3..aa27292 100644 --- a/Projekte/battleship/client/src/main/java/pp/battleship/client/gui/VolumeSlider.java +++ b/Projekte/battleship/client/src/main/java/pp/battleship/client/gui/VolumeSlider.java @@ -4,7 +4,7 @@ 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 + * with the help of the Slider in the GUI */ public class VolumeSlider extends Slider { @@ -31,5 +31,5 @@ public class VolumeSlider extends Slider { music.setVolume( (float) vol); } } - + } diff --git a/Projekte/battleship/client/src/main/java/pp/battleship/client/sever/BattleshipServer.java b/Projekte/battleship/client/src/main/java/pp/battleship/server/BattleshipServer.java similarity index 96% rename from Projekte/battleship/client/src/main/java/pp/battleship/client/sever/BattleshipServer.java rename to Projekte/battleship/client/src/main/java/pp/battleship/server/BattleshipServer.java index d55fe6c..1ad0a2e 100644 --- a/Projekte/battleship/client/src/main/java/pp/battleship/client/sever/BattleshipServer.java +++ b/Projekte/battleship/client/src/main/java/pp/battleship/server/BattleshipServer.java @@ -1,4 +1,11 @@ -package pp.battleship.client.sever; +//////////////////////////////////////// +// 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; @@ -169,4 +176,4 @@ public class BattleshipServer implements MessageListener, Conn else LOGGER.log(Level.ERROR, "there is no connection with id={0}", id); //NON-NLS } -} +} \ No newline at end of file diff --git a/Projekte/battleship/client/src/main/java/pp/battleship/client/sever/ReceivedMessage.java b/Projekte/battleship/client/src/main/java/pp/battleship/server/ReceivedMessage.java similarity index 53% rename from Projekte/battleship/client/src/main/java/pp/battleship/client/sever/ReceivedMessage.java rename to Projekte/battleship/client/src/main/java/pp/battleship/server/ReceivedMessage.java index 420cac7..dedbda2 100644 --- a/Projekte/battleship/client/src/main/java/pp/battleship/client/sever/ReceivedMessage.java +++ b/Projekte/battleship/client/src/main/java/pp/battleship/server/ReceivedMessage.java @@ -1,4 +1,11 @@ -package pp.battleship.client.sever; +//////////////////////////////////////// +// 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; @@ -7,4 +14,4 @@ record ReceivedMessage(ClientMessage message, int from) { void process(ClientInterpreter interpreter) { message.accept(interpreter, from); } -} \ No newline at end of file +} diff --git a/Projekte/battleship/model/src/main/java/pp/battleship/game/client/BattleState.java b/Projekte/battleship/model/src/main/java/pp/battleship/game/client/BattleState.java index bd866ab..99df8d0 100644 --- a/Projekte/battleship/model/src/main/java/pp/battleship/game/client/BattleState.java +++ b/Projekte/battleship/model/src/main/java/pp/battleship/game/client/BattleState.java @@ -100,6 +100,3 @@ class BattleState extends ClientState { logic.playSound(Sound.DESTROYED_SHIP); } } - - - diff --git a/Projekte/battleship/model/src/main/java/pp/battleship/game/client/ShootingAnimationState.java b/Projekte/battleship/model/src/main/java/pp/battleship/game/client/ShootingAnimationState.java new file mode 100644 index 0000000..2ad0ead --- /dev/null +++ b/Projekte/battleship/model/src/main/java/pp/battleship/game/client/ShootingAnimationState.java @@ -0,0 +1,93 @@ +package pp.battleship.game.client; + +import pp.battleship.message.client.ShellAnimationFinishedMessage; +import pp.battleship.message.server.EffectMessage; +import pp.battleship.model.Shell; + +import java.lang.System.Logger.Level; + +/** + * This class represents the client state during a shooting animation. + * It handles the logic for the animation of a fired shell and ensures + * that the client notifies the server when the animation is complete. + */ +public class ShootingAnimationState extends ClientState { + + // The shell object representing the fired shell's movement + private final Shell shell; + + /** + * Constructs the ShootingAnimationState with the given game logic and shell. + * + * @param logic the game logic instance managing the state + * @param shell the shell object representing the fired projectile + */ + ShootingAnimationState(ClientGameLogic logic, Shell shell) { + super(logic); + this.shell = shell; + } + + /** + * Indicates that the battle scene should be shown in this state. + * + * @return true because the battle view is active during the shooting animation + */ + @Override + public boolean showBattle() { + return true; + } + + /** + * Handles the effect message received from the server. + * Logs the received effect for debugging purposes. + * + * @param msg the effect message received from the server + */ + @Override + void receivedEffect(EffectMessage msg) { + ClientGameLogic.LOGGER.log(Level.INFO, "report effect: {0}", msg); //NON-NLS + // Here, you could implement additional logic to display the effects of the shot, + // like a visual indication of a hit or a miss. + } + + /** + * Called once per frame to update the shell's position and check if it has reached the target. + * If the shell reaches the target, a message is sent to the server. + * + * @param deltaTime time in seconds since the last update call + */ + @Override + public void update(float deltaTime) { + if (shell != null) { + // Update the position of the shell based on the elapsed time + shell.updatePosition(deltaTime); + + // Check if the shell has reached its target + if (shell.isAtTarget()) { + // If the shell has reached the target, notify the server that the animation is complete + sendAnimationCompleteMessage(); + } + } + } + + /** + * Sends a message to the server indicating that the shooting animation has finished. + */ + private void sendAnimationCompleteMessage() { + ShellAnimationFinishedMessage message = new ShellAnimationFinishedMessage(); + // Send the message to the server via the game's logic communication system + logic.send(message); + logic.setState(new BattleState(logic, true)); + ClientGameLogic.LOGGER.log(Level.INFO, "Shell animation complete, message sent to server."); + } + + /** + * Optionally, you can add an entry method if there is any setup that needs to happen when entering the state. + * By default, this is empty. + */ + @Override + void entry() { + super.entry(); // Call the parent entry method + ClientGameLogic.LOGGER.log(Level.INFO, "Entered ShootingAnimationState."); + } +} diff --git a/Projekte/battleship/model/src/main/java/pp/battleship/game/server/ServerGameLogic.java b/Projekte/battleship/model/src/main/java/pp/battleship/game/server/ServerGameLogic.java index 91b235e..f0db4c1 100644 --- a/Projekte/battleship/model/src/main/java/pp/battleship/game/server/ServerGameLogic.java +++ b/Projekte/battleship/model/src/main/java/pp/battleship/game/server/ServerGameLogic.java @@ -9,8 +9,8 @@ package pp.battleship.game.server; import pp.battleship.BattleshipConfig; import pp.battleship.message.client.ClientInterpreter; -import pp.battleship.message.client.ClientMessage; import pp.battleship.message.client.MapMessage; +import pp.battleship.message.client.ShellAnimationFinishedMessage; import pp.battleship.message.client.ShootMessage; import pp.battleship.message.server.EffectMessage; import pp.battleship.message.server.GameDetails; @@ -36,6 +36,7 @@ public class ServerGameLogic implements ClientInterpreter { private final BattleshipConfig config; private final List players = new ArrayList<>(2); private final Set readyPlayers = new HashSet<>(); + private Set playersFinishedShellAnimation = new HashSet<>(); private final ServerSender serverSender; private Player activePlayer; private ServerState state = ServerState.WAIT; @@ -52,7 +53,7 @@ public class ServerGameLogic implements ClientInterpreter { } /** - * Returns the state of the game. + * Returns the state of the game. */ ServerState getState() { return state; @@ -144,7 +145,7 @@ public class ServerGameLogic implements ClientInterpreter { public void received(MapMessage msg, int from) { if (state != ServerState.SET_UP) LOGGER.log(Level.ERROR, "playerReady not allowed in {0}", state); //NON-NLS - + Player cp = getPlayerById(from); List ships = msg.getShips(); @@ -161,18 +162,18 @@ public class ServerGameLogic implements ClientInterpreter { /** * Validates a list of ships. * Validates the list, that no ships are out of bounds or overlap with each other - * + * * @param ships list of ships to validate * @return {@code true} if all ships positions are valid, {@code false} otherwise */ private boolean validateShips(List ships) { Set occupiedPoints = new HashSet<>(); - + for (Battleship ship : ships) { if (!isWithinBounds(ship)) { return false; } - + for (int x = ship.getMinX(); x <= ship.getMaxX(); x++) { for (int y = ship.getMinY(); y <= ship.getMaxY(); y++) { IntPoint point = new IntPoint(x, y); @@ -182,10 +183,10 @@ public class ServerGameLogic implements ClientInterpreter { } } } - + return true; } - + /** * Test if a ship is inside the map @@ -196,7 +197,7 @@ public class ServerGameLogic implements ClientInterpreter { return ship.getMinX() >= 0 && ship.getMaxX() < config.getMapWidth() && ship.getMinY() >= 0 && ship.getMaxY() < config.getMapHeight(); } - + /** * Handles the reception of a ShootMessage. @@ -240,6 +241,10 @@ public class ServerGameLogic implements ClientInterpreter { */ void shoot(Player p, IntPoint pos) { if (p != activePlayer) return; + + setState(ServerState.SHELL_IN_FLIGHT); + // setState(ServerState.BATTLE); + final Player otherPlayer = getOpponent(activePlayer); final Battleship selectedShip = otherPlayer.getMap().findShipAt(pos); if (selectedShip == null) { @@ -269,5 +274,29 @@ public class ServerGameLogic implements ClientInterpreter { } } } -} + /** + * Handles the reception of ShellAnimationFinishedMessage. + * This method is called when a client signals that its shell animation is complete. + * + * @param msg the received ShellAnimationFinishedMessage + * @param from the ID of the sender client + */ + @Override + public void received(ShellAnimationFinishedMessage msg, int from) { + // Add the player to the set of players who have finished the animation + playersFinishedShellAnimation.add(from); + + // Check if both players have finished the shell animation + if (playersFinishedShellAnimation.size() == 2) { + // Clear the set of players who have finished the animation for the next shot + playersFinishedShellAnimation.clear(); + + // Transition back to the BATTLE state + setState(ServerState.BATTLE); + + // Log the completion of the shell animation + LOGGER.log(Level.INFO, "Both players finished shell animation. Returning to BATTLE state."); + } + } +} diff --git a/Projekte/battleship/model/src/main/java/pp/battleship/game/server/ServerState.java b/Projekte/battleship/model/src/main/java/pp/battleship/game/server/ServerState.java index 507aa7a..e4e494a 100644 --- a/Projekte/battleship/model/src/main/java/pp/battleship/game/server/ServerState.java +++ b/Projekte/battleship/model/src/main/java/pp/battleship/game/server/ServerState.java @@ -29,5 +29,9 @@ enum ServerState { /** * The game has ended because all the ships of one player have been destroyed. */ - GAME_OVER + GAME_OVER, + /** + * The games is frozen as long as the shell animation is running + */ + SHELL_IN_FLIGHT } diff --git a/Projekte/battleship/model/src/main/java/pp/battleship/game/singlemode/Copycat.java b/Projekte/battleship/model/src/main/java/pp/battleship/game/singlemode/Copycat.java index f6e89be..eb21c74 100644 --- a/Projekte/battleship/model/src/main/java/pp/battleship/game/singlemode/Copycat.java +++ b/Projekte/battleship/model/src/main/java/pp/battleship/game/singlemode/Copycat.java @@ -10,6 +10,7 @@ package pp.battleship.game.singlemode; import pp.battleship.message.client.ClientInterpreter; import pp.battleship.message.client.ClientMessage; import pp.battleship.message.client.MapMessage; +import pp.battleship.message.client.ShellAnimationFinishedMessage; import pp.battleship.message.client.ShootMessage; import pp.battleship.model.Battleship; @@ -72,4 +73,10 @@ class Copycat implements ClientInterpreter { private static Battleship copy(Battleship ship) { return new Battleship(ship.getLength(), ship.getX(), ship.getY(), ship.getRot()); } + + @Override + public void received(ShellAnimationFinishedMessage shellAnimationFinishedMessage, int from) { + // TODO Auto-generated method stub + throw new UnsupportedOperationException("Unimplemented method 'received'"); + } } diff --git a/Projekte/battleship/model/src/main/java/pp/battleship/message/client/ClientInterpreter.java b/Projekte/battleship/model/src/main/java/pp/battleship/message/client/ClientInterpreter.java index 6a50c1d..bc5974d 100644 --- a/Projekte/battleship/model/src/main/java/pp/battleship/message/client/ClientInterpreter.java +++ b/Projekte/battleship/model/src/main/java/pp/battleship/message/client/ClientInterpreter.java @@ -26,4 +26,7 @@ public interface ClientInterpreter { * @param from the connection ID from which the message was received */ void received(MapMessage msg, int from); + + // TODO + void received(ShellAnimationFinishedMessage shellAnimationFinishedMessage, int from); } diff --git a/Projekte/battleship/model/src/main/java/pp/battleship/message/client/ShellAnimationFinishedMessage.java b/Projekte/battleship/model/src/main/java/pp/battleship/message/client/ShellAnimationFinishedMessage.java new file mode 100644 index 0000000..dba92d2 --- /dev/null +++ b/Projekte/battleship/model/src/main/java/pp/battleship/message/client/ShellAnimationFinishedMessage.java @@ -0,0 +1,10 @@ +package pp.battleship.message.client; + +public class ShellAnimationFinishedMessage extends ClientMessage{ + + @Override + public void accept(ClientInterpreter interpreter, int from) { + interpreter.received(this, from); + } + +} diff --git a/Projekte/battleship/model/src/main/java/pp/battleship/model/Shell.java b/Projekte/battleship/model/src/main/java/pp/battleship/model/Shell.java new file mode 100644 index 0000000..c0d488c --- /dev/null +++ b/Projekte/battleship/model/src/main/java/pp/battleship/model/Shell.java @@ -0,0 +1,47 @@ +package pp.battleship.model; + +import com.jme3.math.Vector3f; + +public class Shell { + private Vector3f startPosition; + private Vector3f targetPosition; + private Vector3f currentPosition; + private float speed; + private boolean isAtTarget; + + public Shell(Vector3f startPosition, Vector3f targetPosition, float speed) { + this.startPosition = startPosition; + this.targetPosition = targetPosition; + this.currentPosition = new Vector3f(startPosition); + this.speed = speed; + this.isAtTarget = true; + } + + // Aktualisiert die Position des Geschosses basierend auf der verstrichenen Zeit + public void updatePosition(float deltaTime) { + if (!isAtTarget) { + // Berechne die Richtung des Geschosses + Vector3f direction = targetPosition.subtract(currentPosition).normalize(); + // Berechne die Bewegung basierend auf der Geschwindigkeit und der verstrichenen Zeit + Vector3f movement = direction.mult(speed * deltaTime); + currentPosition.addLocal(movement); + + // Prüfe, ob das Geschoss das Ziel erreicht hat + if (currentPosition.distance(targetPosition) < speed * deltaTime) { + currentPosition.set(targetPosition); + isAtTarget = true; + } + } + } + + // Gibt die aktuelle Position des Geschosses zurück + public Vector3f getCurrentPosition() { + return currentPosition; + } + + // Überprüft, ob das Geschoss das Ziel erreicht hat + public boolean isAtTarget() { + return isAtTarget; + } +} + diff --git a/Projekte/battleship/model/src/main/java/pp/battleship/model/ShellControl.java b/Projekte/battleship/model/src/main/java/pp/battleship/model/ShellControl.java new file mode 100644 index 0000000..a4c8c45 --- /dev/null +++ b/Projekte/battleship/model/src/main/java/pp/battleship/model/ShellControl.java @@ -0,0 +1,48 @@ +package pp.battleship.model; + +import com.jme3.math.Vector3f; +import com.jme3.renderer.RenderManager; +import com.jme3.renderer.ViewPort; +import com.jme3.scene.Spatial; +import com.jme3.scene.control.AbstractControl; + +public class ShellControl extends AbstractControl { + + private Shell shell; // Das Shell-Objekt, das die Bewegung des Geschosses enthält + + public ShellControl(Shell shell) { + this.shell = shell; + } + + // Die Methode wird in jedem Frame aufgerufen, um die Logik zu aktualisieren + @Override + protected void controlUpdate(float deltaTime) { + if (shell != null) { + // Aktualisiere die Position des Geschosses basierend auf der verstrichenen Zeit + shell.updatePosition(deltaTime); + + // Setze die neue Position des Geschosses im 3D-Raum + spatial.setLocalTranslation(shell.getCurrentPosition()); + + // Optionale Animation oder Effekte hinzufügen (z.B. Rauch oder Funkenflug) + // addParticleEffects(); + } + } + + + // Setze das Shell-Objekt neu, um es während des Spiels zu ändern + public void setShell(Shell shell) { + this.shell = shell; + } + + // Gibt das aktuell verwendete Shell-Objekt zurück + public Shell getShell() { + return this.shell; + } + + @Override + protected void controlRender(RenderManager rm, ViewPort vp) { + // TODO Auto-generated method stub + } +} + diff --git a/Projekte/battleship/model/src/main/java/pp/battleship/model/ShipMap.java b/Projekte/battleship/model/src/main/java/pp/battleship/model/ShipMap.java index 3fe0538..5a16278 100644 --- a/Projekte/battleship/model/src/main/java/pp/battleship/model/ShipMap.java +++ b/Projekte/battleship/model/src/main/java/pp/battleship/model/ShipMap.java @@ -250,5 +250,3 @@ public class ShipMap { eventBroker.notifyListeners(event); } } - - diff --git a/Projekte/settings.gradle b/Projekte/settings.gradle index 7440607..bb7e8c7 100644 --- a/Projekte/settings.gradle +++ b/Projekte/settings.gradle @@ -19,6 +19,7 @@ dependencyResolutionManagement { library('jme3-testdata', 'org.jmonkeyengine', 'jme3-testdata').versionRef('jme') library('jme3-lwjgl', 'org.jmonkeyengine', 'jme3-lwjgl').versionRef('jme') library('jme3-lwjgl3', 'org.jmonkeyengine', 'jme3-lwjgl3').versionRef('jme') + library('jme3-effects', 'org.jmonkeyengine', 'jme3-effects').versionRef('jme') library('lemur', 'com.simsilica:lemur:1.16.0') library('lemur-proto', 'com.simsilica:lemur-proto:1.13.0')