From aa9c073931d46d8364d1394bf12bbca37d5cf252 Mon Sep 17 00:00:00 2001 From: Johannes Schmelz Date: Sun, 13 Oct 2024 20:52:33 +0200 Subject: [PATCH] added sinking animation for destroyed ships --- .../client/gui/SeaSynchronizer.java | 24 +++++++++ .../battleship/client/gui/SinkingControl.java | 53 +++++++++++++++++++ 2 files changed, 77 insertions(+) create mode 100644 Projekte/battleship/client/src/main/java/pp/battleship/client/gui/SinkingControl.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 fc2a538..d330c74 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 @@ -96,6 +96,25 @@ 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 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. @@ -149,6 +168,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/SinkingControl.java b/Projekte/battleship/client/src/main/java/pp/battleship/client/gui/SinkingControl.java new file mode 100644 index 0000000..845d50e --- /dev/null +++ b/Projekte/battleship/client/src/main/java/pp/battleship/client/gui/SinkingControl.java @@ -0,0 +1,53 @@ +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(); + } + } + + @Override + protected void controlRender(RenderManager rm, ViewPort vp) { + // No rendering-related code needed + } +}