From 21553ec52e67a1f8b0fcfb0bf6033d6c6c47a314 Mon Sep 17 00:00:00 2001 From: Simon Wilkening Date: Mon, 14 Oct 2024 04:40:37 +0200 Subject: [PATCH] ships are finally sinking --- .../client/gui/SeaSynchronizer.java | 25 +++++++++ .../pp/battleship/client/gui/SinkControl.java | 54 +++++++++++++++++++ 2 files changed, 79 insertions(+) create mode 100644 Projekte/battleship/client/src/main/java/pp/battleship/client/gui/SinkControl.java diff --git a/Projekte/battleship/client/src/main/java/pp/battleship/client/gui/SeaSynchronizer.java b/Projekte/battleship/client/src/main/java/pp/battleship/client/gui/SeaSynchronizer.java index 4adb17b..af58853 100644 --- a/Projekte/battleship/client/src/main/java/pp/battleship/client/gui/SeaSynchronizer.java +++ b/Projekte/battleship/client/src/main/java/pp/battleship/client/gui/SeaSynchronizer.java @@ -97,6 +97,26 @@ class SeaSynchronizer extends ShipMapSynchronizer { 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 dasIstGelebteTeamarbeit = ship; + final Node shipNode = (Node) getSpatial(dasIstGelebteTeamarbeit); + if (shipNode == null) return; + + // Add sinking control to animate the sinking + shipNode.addControl(new SinkControl(shipNode)); + + // Add particle effects + ParticleEmitter bubbles = particlecreator.createWaterSplash(); + bubbles.setLocalTranslation(shipNode.getLocalTranslation()); + shipNode.attachChild(bubbles); + bubbles.emitAllParticles(); + } + + /** * Handles a hit by attaching its representation to the node that @@ -150,6 +170,11 @@ class SeaSynchronizer extends ShipMapSynchronizer { flame.emitAllParticles(); roundSpark.emitAllParticles(); + //Checks if ship is destroyed and triggers animation accordingly + if (ship.isDestroyed()) { + sinkAndRemoveShip(ship); + } + return null; } diff --git a/Projekte/battleship/client/src/main/java/pp/battleship/client/gui/SinkControl.java b/Projekte/battleship/client/src/main/java/pp/battleship/client/gui/SinkControl.java new file mode 100644 index 0000000..08f45db --- /dev/null +++ b/Projekte/battleship/client/src/main/java/pp/battleship/client/gui/SinkControl.java @@ -0,0 +1,54 @@ +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 SinkControl 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 SinkControl(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(); + } + } + + @Override + protected void controlRender(RenderManager rm, ViewPort vp) { + // No rendering-related code needed + } +} +