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:
parent
44a25a2e1f
commit
ca57507b53
@@ -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,6 +90,12 @@ 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;
|
||||||
|
|
||||||
|
if (battleship.isDestroyed() && spatial.getLocalTranslation().getY() <= -0.6f) {
|
||||||
|
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
|
// Update the time within the oscillation cycle
|
||||||
time = (time + tpf) % cycle;
|
time = (time + tpf) % cycle;
|
||||||
|
|
||||||
@@ -91,6 +109,8 @@ protected void controlUpdate(float tpf) {
|
|||||||
spatial.setLocalRotation(pitch);
|
spatial.setLocalRotation(pitch);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This method is called during the rendering phase, but it does not perform any
|
* This method is called during the rendering phase, but it does not perform any
|
||||||
* operations in this implementation as the control only influences the spatial's
|
* operations in this implementation as the control only influences the spatial's
|
||||||
|
|||||||
Reference in New Issue
Block a user