added the missing solution for exercise 12

ships will now sink when destroyed and will be removed when they are fully submerged
This commit is contained in:
Hanno Fleischer hanno.fleischer@unibw.de
2024-10-05 14:06:45 +02:00
parent 44a25a2e1f
commit ca57507b53

View File

@@ -14,6 +14,9 @@
import com.jme3.scene.control.AbstractControl; import com.jme3.scene.control.AbstractControl;
import pp.battleship.model.Battleship; import pp.battleship.model.Battleship;
import java.lang.System.Logger;
import java.lang.System.Logger.Level;
import static pp.util.FloatMath.DEG_TO_RAD; import static pp.util.FloatMath.DEG_TO_RAD;
import static pp.util.FloatMath.TWO_PI; import static pp.util.FloatMath.TWO_PI;
import static pp.util.FloatMath.sin; import static pp.util.FloatMath.sin;
@@ -48,6 +51,13 @@ class ShipControl extends AbstractControl {
*/ */
private float time; private float time;
/**
* The ship to be controlled
*/
private final Battleship battleship;
static final Logger LOGGER = System.getLogger(ShipControl.class.getName());
/** /**
* Constructs a new ShipControl instance for the specified Battleship. * Constructs a new ShipControl instance for the specified Battleship.
* The ship's orientation determines the axis of rotation, while its length influences * The ship's orientation determines the axis of rotation, while its length influences
@@ -56,6 +66,8 @@ class ShipControl extends AbstractControl {
* @param ship the Battleship object to control * @param ship the Battleship object to control
*/ */
public ShipControl(Battleship ship) { public ShipControl(Battleship ship) {
battleship = ship;
// Determine the axis of rotation based on the ship's orientation // Determine the axis of rotation based on the ship's orientation
axis = switch (ship.getRot()) { axis = switch (ship.getRot()) {
case LEFT, RIGHT -> Vector3f.UNIT_X; case LEFT, RIGHT -> Vector3f.UNIT_X;
@@ -63,7 +75,7 @@ public ShipControl(Battleship ship) {
}; };
// Set the cycle duration and amplitude based on the ship's length // Set the cycle duration and amplitude based on the ship's length
cycle = ship.getLength() * 2f; cycle = battleship.getLength() * 2f;
amplitude = 5f * DEG_TO_RAD / ship.getLength(); amplitude = 5f * DEG_TO_RAD / ship.getLength();
} }
@@ -78,17 +90,25 @@ protected void controlUpdate(float tpf) {
// If spatial is null, do nothing // If spatial is null, do nothing
if (spatial == null) return; if (spatial == null) return;
// Update the time within the oscillation cycle if (battleship.isDestroyed() && spatial.getLocalTranslation().getY() <= -0.6f) {
time = (time + tpf) % cycle; LOGGER.log(Level.INFO, "Ship removed {0}", spatial.getName());
spatial.getParent().detachChild(spatial);
} else if (battleship.isDestroyed()) {
spatial.move(0, -0.05f * tpf, 0);
} else {
// Update the time within the oscillation cycle
time = (time + tpf) % cycle;
// Calculate the current angle of the oscillation // Calculate the current angle of the oscillation
final float angle = amplitude * sin(time * TWO_PI / cycle); final float angle = amplitude * sin(time * TWO_PI / cycle);
// Update the pitch Quaternion with the new angle // Update the pitch Quaternion with the new angle
pitch.fromAngleAxis(angle, axis); pitch.fromAngleAxis(angle, axis);
// Apply the pitch rotation to the spatial
spatial.setLocalRotation(pitch);
}
// Apply the pitch rotation to the spatial
spatial.setLocalRotation(pitch);
} }
/** /**