ships are finally sinking

This commit is contained in:
Simon Wilkening 2024-10-14 04:40:37 +02:00
parent 5f79e8e00c
commit 21553ec52e
2 changed files with 79 additions and 0 deletions

View File

@ -97,6 +97,26 @@ class SeaSynchronizer extends ShipMapSynchronizer {
return shotNode; 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 * Handles a hit by attaching its representation to the node that
@ -150,6 +170,11 @@ class SeaSynchronizer extends ShipMapSynchronizer {
flame.emitAllParticles(); flame.emitAllParticles();
roundSpark.emitAllParticles(); roundSpark.emitAllParticles();
//Checks if ship is destroyed and triggers animation accordingly
if (ship.isDestroyed()) {
sinkAndRemoveShip(ship);
}
return null; return null;
} }

View File

@ -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
}
}