added groundwork for shell animation

This commit is contained in:
Yvonne Schmidt
2024-10-14 05:31:20 +02:00
parent 563784dbab
commit 47610d9e11
38 changed files with 776156 additions and 1 deletions

View File

@@ -11,6 +11,7 @@ import pp.battleship.BattleshipConfig;
import pp.battleship.message.client.ClientInterpreter;
import pp.battleship.message.client.MapMessage;
import pp.battleship.message.client.ShootMessage;
import pp.battleship.message.client.ShellAnimationFinishedMessage;
import pp.battleship.message.server.EffectMessage;
import pp.battleship.message.server.GameDetails;
import pp.battleship.message.server.ServerMessage;
@@ -35,6 +36,7 @@ public class ServerGameLogic implements ClientInterpreter {
private final BattleshipConfig config;
private final List<Player> players = new ArrayList<>(2);
private final Set<Player> readyPlayers = new HashSet<>();
private Set<Integer> playersFinishedShellAnimation = new HashSet<>();
private final ServerSender serverSender;
private Player activePlayer;
private ServerState state = ServerState.WAIT;
@@ -197,6 +199,9 @@ public class ServerGameLogic implements ClientInterpreter {
*/
void shoot(Player p, IntPoint pos) {
if (p != activePlayer) return;
setState(ServerState.SHELL_IN_FLIGHT);
// setState(ServerState.BATTLE);
final Player otherPlayer = getOpponent(activePlayer);
final Battleship selectedShip = otherPlayer.getMap().findShipAt(pos);
if (selectedShip == null) {
@@ -260,5 +265,31 @@ public class ServerGameLogic implements ClientInterpreter {
}
return true;
}
/**
* Handles the reception of ShellAnimationFinishedMessage.
* This method is called when a client signals that its shell animation is complete.
*
* @param msg the received ShellAnimationFinishedMessage
* @param from the ID of the sender client
*/
@Override
public void received(ShellAnimationFinishedMessage msg, int from) {
// Add the player to the set of players who have finished the animation
playersFinishedShellAnimation.add(from);
// Check if both players have finished the shell animation
if (playersFinishedShellAnimation.size() == 2) {
// Clear the set of players who have finished the animation for the next shot
playersFinishedShellAnimation.clear();
// Transition back to the BATTLE state
setState(ServerState.BATTLE);
// Log the completion of the shell animation
LOGGER.log(Level.INFO, "Both players finished shell animation. Returning to BATTLE state.");
}
}
}

View File

@@ -29,5 +29,10 @@ enum ServerState {
/**
* The game has ended because all the ships of one player have been destroyed.
*/
GAME_OVER
GAME_OVER,
/**
* The games is frozen as long as the shell animation is running
*/
SHELL_IN_FLIGHT
}

View File

@@ -12,6 +12,7 @@ import pp.battleship.message.client.ClientMessage;
import pp.battleship.message.client.MapMessage;
import pp.battleship.message.client.ShootMessage;
import pp.battleship.model.Battleship;
import pp.battleship.message.client.ShellAnimationFinishedMessage;
/**
* The {@code Copycat} class is a utility that creates a copy of a {@link ClientMessage}.
@@ -72,4 +73,11 @@ class Copycat implements ClientInterpreter {
private static Battleship copy(Battleship ship) {
return new Battleship(ship.getLength(), ship.getX(), ship.getY(), ship.getRot());
}
@Override
public void received(ShellAnimationFinishedMessage shellAnimationFinishedMessage, int from) {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'received'");
}
}

View File

@@ -19,6 +19,10 @@ public interface ClientInterpreter {
*/
void received(ShootMessage msg, int from);
// TODO
void received(ShellAnimationFinishedMessage shellAnimationFinishedMessage, int from);
/**
* Processes a received MapMessage.
*

View File

@@ -0,0 +1,10 @@
package pp.battleship.message.client;
public class ShellAnimationFinishedMessage extends ClientMessage{
@Override
public void accept(ClientInterpreter interpreter, int from) {
interpreter.received(this, from);
}
}

View File

@@ -0,0 +1,48 @@
package pp.battleship.model;
import com.jme3.math.Vector3f;
public class Shell {
private Vector3f startPosition;
private Vector3f targetPosition;
private Vector3f currentPosition;
private float speed;
private boolean isAtTarget;
public Shell(Vector3f startPosition, Vector3f targetPosition, float speed) {
this.startPosition = startPosition;
this.targetPosition = targetPosition;
this.currentPosition = new Vector3f(startPosition);
this.speed = speed;
this.isAtTarget = true;
}
// Aktualisiert die Position des Geschosses basierend auf der verstrichenen Zeit
public void updatePosition(float deltaTime) {
if (!isAtTarget) {
// Berechne die Richtung des Geschosses
Vector3f direction = targetPosition.subtract(currentPosition).normalize();
// Berechne die Bewegung basierend auf der Geschwindigkeit und der verstrichenen Zeit
Vector3f movement = direction.mult(speed * deltaTime);
currentPosition.addLocal(movement);
// Prüfe, ob das Geschoss das Ziel erreicht hat
if (currentPosition.distance(targetPosition) < speed * deltaTime) {
currentPosition.set(targetPosition);
isAtTarget = true;
}
}
}
// Gibt die aktuelle Position des Geschosses zurück
public Vector3f getCurrentPosition() {
return currentPosition;
}
// Überprüft, ob das Geschoss das Ziel erreicht hat
public boolean isAtTarget() {
return isAtTarget;
}
}

View File

@@ -0,0 +1,47 @@
package pp.battleship.model;
import com.jme3.math.Vector3f;
import com.jme3.renderer.RenderManager;
import com.jme3.renderer.ViewPort;
import com.jme3.scene.Spatial;
import com.jme3.scene.control.AbstractControl;
public class ShellControl extends AbstractControl {
private Shell shell; // Das Shell-Objekt, das die Bewegung des Geschosses enthält
public ShellControl(Shell shell) {
this.shell = shell;
}
// Die Methode wird in jedem Frame aufgerufen, um die Logik zu aktualisieren
@Override
protected void controlUpdate(float deltaTime) {
if (shell != null) {
// Aktualisiere die Position des Geschosses basierend auf der verstrichenen Zeit
shell.updatePosition(deltaTime);
// Setze die neue Position des Geschosses im 3D-Raum
spatial.setLocalTranslation(shell.getCurrentPosition());
// Optionale Animation oder Effekte hinzufügen (z.B. Rauch oder Funkenflug)
// addParticleEffects();
}
}
// Setze das Shell-Objekt neu, um es während des Spiels zu ändern
public void setShell(Shell shell) {
this.shell = shell;
}
// Gibt das aktuell verwendete Shell-Objekt zurück
public Shell getShell() {
return this.shell;
}
@Override
protected void controlRender(RenderManager rm, ViewPort vp) {
// TODO Auto-generated method stub
}
}