reworked the shell animation
@ -28,7 +28,7 @@ import pp.util.Position;
|
||||
* and interaction between the model and the view.
|
||||
*/
|
||||
class MapView {
|
||||
private static final float FIELD_SIZE = 40f;
|
||||
public static final float FIELD_SIZE = 40f;
|
||||
private static final float GRID_LINE_WIDTH = 2f;
|
||||
private static final float BACKGROUND_DEPTH = -4f;
|
||||
private static final float GRID_DEPTH = -1f;
|
||||
|
@ -7,14 +7,21 @@
|
||||
|
||||
package pp.battleship.client.gui;
|
||||
|
||||
import com.jme3.material.Material;
|
||||
import com.jme3.material.RenderState;
|
||||
import com.jme3.material.RenderState.BlendMode;
|
||||
import com.jme3.math.ColorRGBA;
|
||||
import com.jme3.scene.Geometry;
|
||||
import com.jme3.scene.Node;
|
||||
import com.jme3.scene.Spatial;
|
||||
import com.jme3.scene.shape.Sphere;
|
||||
import pp.battleship.model.Battleship;
|
||||
import pp.battleship.model.Shell;
|
||||
import pp.battleship.model.Shot;
|
||||
import pp.util.Position;
|
||||
|
||||
import static com.jme3.material.Materials.UNSHADED;
|
||||
|
||||
/**
|
||||
* Synchronizes the visual representation of the ship map with the game model.
|
||||
* It handles the rendering of ships and shots on the map view, updating the view
|
||||
@ -122,4 +129,25 @@ class MapViewSynchronizer extends ShipMapSynchronizer {
|
||||
private Geometry shipLine(float x1, float y1, float x2, float y2, ColorRGBA color) {
|
||||
return view.getApp().getDraw().makeFatLine(x1, y1, x2, y2, SHIP_DEPTH, color, SHIP_LINE_WIDTH);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates and returns a Spatial representation of the given {@code Shell} object
|
||||
* for 2D visualization in the game. The shell is represented as a circle.
|
||||
*
|
||||
* @param shell The {@code Shell} object to be visualized.
|
||||
* @return A {@code Spatial} object representing the shell on the map.
|
||||
*/
|
||||
@Override
|
||||
public Spatial visit(Shell shell) {
|
||||
final ColorRGBA color = ColorRGBA.Black;
|
||||
Geometry ellipse = new Geometry("ellipse", new Sphere(50, 50, MapView.FIELD_SIZE / 2 * 0.8f));
|
||||
Material mat = new Material(view.getApp().getAssetManager(), UNSHADED); //NON-NLS
|
||||
mat.getAdditionalRenderState().setBlendMode(BlendMode.Alpha);
|
||||
mat.getAdditionalRenderState().setFaceCullMode(RenderState.FaceCullMode.Off);
|
||||
mat.setColor("Color", color);
|
||||
ellipse.setMaterial(mat);
|
||||
ellipse.addControl(new Shell2DControl(view, shell));
|
||||
return ellipse;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -44,7 +44,7 @@ class SeaSynchronizer extends ShipMapSynchronizer {
|
||||
private static final String SMALL_BOAT_MODEL = "Models/BoatSmall/12219_boat_v2_L2.j3o";
|
||||
private static final String BATTLE_MODEL = "Models/Battle/Battle.j3o"; //NON-NLS
|
||||
private static final String CV_MODEL = "Models/CV/CV.j3o"; //NON-NLS
|
||||
private static final String SHELL_MODEL = "Models/Shell/45.j3o";
|
||||
private static final String LIGHTING = "Common/MatDefs/Light/Lighting.j3md";
|
||||
private static final String COLOR = "Color"; //NON-NLS
|
||||
private static final String SHIP = "ship"; //NON-NLS
|
||||
private static final String SHOT = "shot"; //NON-NLS
|
||||
@ -322,21 +322,6 @@ class SeaSynchronizer extends ShipMapSynchronizer {
|
||||
return waterSplash;
|
||||
}
|
||||
|
||||
private void initiateShellFlight(Shot shot) {
|
||||
Vector3f startPosition = new Vector3f(0, 1, 0); // The position where the shell starts, can be adjusted
|
||||
Vector3f targetPosition = new Vector3f(shot.getX() + 0.5f, 0, shot.getY() + 0.5f);
|
||||
|
||||
shell = new Shell(startPosition, targetPosition, 2f); // 2 seconds flight duration
|
||||
shellModel = app.getAssetManager().loadModel(SHELL_MODEL);
|
||||
Material material = new Material(app.getAssetManager(), "Common/MatDefs/Misc/Unshaded.j3md");
|
||||
material.setColor("Color", ColorRGBA.Yellow);
|
||||
shellModel.setMaterial(material);
|
||||
|
||||
// Set the initial position for the shell model
|
||||
shellModel.setLocalTranslation(startPosition);
|
||||
//.attachChild(shellModel); TODO
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a {@code SeaSynchronizer} object with the specified application, root node, and ship map.
|
||||
*
|
||||
@ -629,4 +614,26 @@ class SeaSynchronizer extends ShipMapSynchronizer {
|
||||
case UP -> PI;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates and returns a 3D model representation of the given {@code Shell} object
|
||||
* for visualization in the game.
|
||||
*
|
||||
* @param shell The {@code Shell} object to be visualized.
|
||||
* @return A {@code Spatial} object representing the 3D model of the shell.
|
||||
*/
|
||||
@Override
|
||||
public Spatial visit(Shell shell) {
|
||||
final Spatial model = app.getAssetManager().loadModel("Models/Shell/shell.j3o");
|
||||
model.setLocalScale(.05f);
|
||||
model.setShadowMode(ShadowMode.CastAndReceive);
|
||||
Material mat = new Material(app.getAssetManager(), LIGHTING);
|
||||
mat.setTexture("DiffuseMap", app.getAssetManager().loadTexture("Models/Shell/shell_color.png"));
|
||||
mat.setReceivesShadows(true);
|
||||
model.setMaterial(mat);
|
||||
|
||||
model.addControl(new ShellControl(shell));
|
||||
return model;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,50 @@
|
||||
package pp.battleship.client.gui;
|
||||
|
||||
import com.jme3.math.Vector3f;
|
||||
import com.jme3.renderer.RenderManager;
|
||||
import com.jme3.renderer.ViewPort;
|
||||
import com.jme3.scene.control.AbstractControl;
|
||||
import pp.battleship.model.Shell;
|
||||
import pp.util.Position;
|
||||
|
||||
/**
|
||||
* Controls the 2D representation of a {@code Shell} in the game, updating its position
|
||||
* based on the shell's current state in the game model. The {@code Shell2DControl} class
|
||||
* is responsible for translating the shell's 3D position to a 2D view position within
|
||||
* the game's map view.
|
||||
*/
|
||||
public class Shell2DControl extends AbstractControl {
|
||||
private final Shell shell;
|
||||
private final MapView view;
|
||||
|
||||
/**
|
||||
* Constructs a new {@code Shell2DControl} to manage the 2D visualization of the given {@code Shell}.
|
||||
*
|
||||
* @param view The {@code MapView} used to get information about the map to display.
|
||||
* @param shell The {@code Shell} being visualized.
|
||||
*/
|
||||
public Shell2DControl(MapView view, Shell shell){
|
||||
this.shell = shell;
|
||||
this.view = view;
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates the position of the shell's 2D representation based on the shell's current
|
||||
* 3D position in the game model. The position is mapped from model space to view space
|
||||
* coordinates and translated to the appropriate location within the {@code MapView}.
|
||||
*
|
||||
* @param tpf Time per frame, representing the time elapsed since the last frame.
|
||||
*/
|
||||
@Override
|
||||
protected void controlUpdate(float tpf) {
|
||||
Vector3f shellPos = shell.getPosition();
|
||||
Position viewPos = view.modelToView(shellPos.x, shellPos.z);
|
||||
spatial.setLocalTranslation(viewPos.getX() + MapView.FIELD_SIZE / 2, viewPos.getY() + MapView.FIELD_SIZE / 2, 0);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void controlRender(RenderManager rm, ViewPort vp) {
|
||||
// No rendering-specific behavior required for this control
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,51 @@
|
||||
package pp.battleship.client.gui;
|
||||
|
||||
import com.jme3.math.Vector3f;
|
||||
import com.jme3.renderer.RenderManager;
|
||||
import com.jme3.renderer.ViewPort;
|
||||
import com.jme3.scene.control.AbstractControl;
|
||||
import pp.battleship.model.Shell;
|
||||
|
||||
import static pp.util.FloatMath.PI;
|
||||
|
||||
/**
|
||||
* Controls the 3D representation of a {@code Shell} in the game, updating its position
|
||||
* and rotation based on the shell's current state in the game model. The {@code ShellControl}
|
||||
* class ensures that the spatial associated with the shell is positioned and oriented correctly
|
||||
* within the world.
|
||||
*/
|
||||
public class ShellControl extends AbstractControl {
|
||||
private final Shell shell;
|
||||
|
||||
/**
|
||||
* Constructs a new {@code ShellControl} to manage the 3D visualization of the given {@code Shell}.
|
||||
*
|
||||
* @param shell The {@code Shell} being visualized and controlled.
|
||||
*/
|
||||
public ShellControl(Shell shell){
|
||||
super();
|
||||
this.shell = shell;
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates the 3D position and rotation of the shell based on its current state.
|
||||
* Converts map coordinates to world coordinates and applies the shell's orientation.
|
||||
*
|
||||
* @param tpf Time per frame, representing the elapsed time since the last update.
|
||||
*/
|
||||
@Override
|
||||
protected void controlUpdate(float tpf) {
|
||||
Vector3f pos = shell.getPosition();
|
||||
Vector3f fixed = new Vector3f(pos.z + 0.5f, pos.y, pos.x + 0.5f);
|
||||
fixed.setY(pos.y);
|
||||
spatial.setLocalTranslation(fixed);
|
||||
spatial.setLocalRotation(shell.getRotation());
|
||||
spatial.rotate(PI/2,0,0);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void controlRender(RenderManager rm, ViewPort vp) {
|
||||
// No rendering-specific behavior required for this control
|
||||
}
|
||||
}
|
||||
|
Before Width: | Height: | Size: 127 KiB |
Before Width: | Height: | Size: 94 KiB |
Before Width: | Height: | Size: 90 KiB |
Before Width: | Height: | Size: 147 KiB |
Before Width: | Height: | Size: 109 KiB |
Before Width: | Height: | Size: 101 KiB |
Before Width: | Height: | Size: 102 KiB |
After Width: | Height: | Size: 43 KiB |
Before Width: | Height: | Size: 35 KiB |
Before Width: | Height: | Size: 3.0 MiB |
Before Width: | Height: | Size: 246 KiB |
Before Width: | Height: | Size: 330 KiB |
@ -11,6 +11,7 @@ import pp.battleship.message.client.ShootMessage;
|
||||
import pp.battleship.message.server.EffectMessage;
|
||||
import pp.battleship.model.IntPoint;
|
||||
import pp.battleship.model.ShipMap;
|
||||
import pp.battleship.model.Shell;
|
||||
import pp.battleship.notification.Sound;
|
||||
|
||||
import java.lang.System.Logger.Level;
|
||||
@ -53,16 +54,13 @@ class BattleState extends ClientState {
|
||||
@Override
|
||||
public void receivedEffect(EffectMessage msg) {
|
||||
ClientGameLogic.LOGGER.log(Level.INFO, "report effect: {0}", msg); //NON-NLS
|
||||
playSound(msg);
|
||||
myTurn = msg.isMyTurn();
|
||||
logic.setInfoText(msg.getInfoTextKey());
|
||||
affectedMap(msg).add(msg.getShot());
|
||||
if (destroyedOpponentShip(msg))
|
||||
logic.getOpponentMap().add(msg.getDestroyedShip());
|
||||
if (msg.isGameOver()) {
|
||||
msg.getRemainingOpponentShips().forEach(logic.getOpponentMap()::add);
|
||||
logic.setState(new GameOverState(logic));
|
||||
}
|
||||
Shell shell = new Shell(msg.getShot());
|
||||
affectedMap(msg).add(shell);
|
||||
logic.playSound(Sound.SHELL_FLYING);
|
||||
logic.setState(new ShootingState(logic, shell, myTurn, msg));
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
@ -74,29 +72,4 @@ class BattleState extends ClientState {
|
||||
private ShipMap affectedMap(EffectMessage msg) {
|
||||
return msg.isOwnShot() ? logic.getOpponentMap() : logic.getOwnMap();
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the opponent's ship was destroyed by the player's shot.
|
||||
*
|
||||
* @param msg the effect message received from the server
|
||||
* @return true if the shot destroyed an opponent's ship, false otherwise
|
||||
*/
|
||||
private boolean destroyedOpponentShip(EffectMessage msg) {
|
||||
return msg.getDestroyedShip() != null && msg.isOwnShot();
|
||||
}
|
||||
|
||||
/**
|
||||
* Plays a sound based on the outcome of the shot. Different sounds are played for a miss, hit,
|
||||
* or destruction of a ship.
|
||||
*
|
||||
* @param msg the effect message containing the result of the shot
|
||||
*/
|
||||
private void playSound(EffectMessage msg) {
|
||||
if (!msg.getShot().isHit())
|
||||
logic.playSound(Sound.SPLASH);
|
||||
else if (msg.getDestroyedShip() == null)
|
||||
logic.playSound(Sound.EXPLOSION);
|
||||
else
|
||||
logic.playSound(Sound.DESTROYED_SHIP);
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,114 @@
|
||||
package pp.battleship.game.client;
|
||||
|
||||
import pp.battleship.message.client.AnimationFinishedMessage;
|
||||
import pp.battleship.message.server.EffectMessage;
|
||||
import pp.battleship.model.Battleship;
|
||||
import pp.battleship.model.Shell;
|
||||
import pp.battleship.model.ShipMap;
|
||||
import pp.battleship.notification.Sound;
|
||||
|
||||
/**
|
||||
* Represents the shooting state of the game where a shell is fired at the opponent.
|
||||
*/
|
||||
public class ShootingState extends ClientState {
|
||||
private float shootValue;
|
||||
private final static float SHELL_SPEED = 0.3f;
|
||||
private final Shell shell;
|
||||
private final boolean myTurn;
|
||||
private final EffectMessage msg;
|
||||
|
||||
/**
|
||||
* Constructs a shooting state with the specified game logic.
|
||||
*
|
||||
* @param logic the game logic
|
||||
* @param shell the shell being shot
|
||||
* @param myTurn indicates if it is the player's turn
|
||||
* @param msg the effect message associated with the shooting action
|
||||
*/
|
||||
public ShootingState(ClientGameLogic logic, Shell shell, boolean myTurn, EffectMessage msg) {
|
||||
super(logic);
|
||||
this.msg = msg;
|
||||
this.myTurn = myTurn;
|
||||
this.shell = shell;
|
||||
this.shootValue = 0;
|
||||
shell.move(shootValue);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean showBattle() {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates the shooting state by moving the shell based on the elapsed time.
|
||||
*
|
||||
* @param delta the time in seconds since the last update
|
||||
*/
|
||||
@Override
|
||||
void update(float delta) {
|
||||
super.update(delta);
|
||||
if (shootValue > 1) {
|
||||
endState();
|
||||
}
|
||||
else {
|
||||
shootValue += delta * SHELL_SPEED;
|
||||
shell.move(shootValue);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Ends the shooting state and processes the effects of the shot.
|
||||
*/
|
||||
private void endState() {
|
||||
playSound(msg);
|
||||
affectedMap(msg).add(msg.getShot());
|
||||
affectedMap(msg).remove(shell);
|
||||
|
||||
if (destroyedOpponentShip(msg))
|
||||
logic.getOpponentMap().add(msg.getDestroyedShip());
|
||||
if (msg.isGameOver()) {
|
||||
for (Battleship ship : msg.getRemainingOpponentShips()) {
|
||||
logic.getOpponentMap().add(ship);
|
||||
}
|
||||
logic.setState(new GameOverState(logic));
|
||||
return;
|
||||
}
|
||||
logic.send(new AnimationFinishedMessage());
|
||||
logic.setState(new BattleState(logic, myTurn));
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if an opponent's ship was destroyed by the shot.
|
||||
*
|
||||
* @param msg the effect message containing the shot details
|
||||
* @return true if an opponent's ship was destroyed, false otherwise
|
||||
*/
|
||||
private boolean destroyedOpponentShip(EffectMessage msg) {
|
||||
return msg.getDestroyedShip() != null && msg.isOwnShot();
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the affected map based on whether the shot was owned by the player or the opponent.
|
||||
*
|
||||
* @param msg the effect message containing shot details
|
||||
* @return the ShipMap that was affected by the shot
|
||||
*/
|
||||
private ShipMap affectedMap(EffectMessage msg) {
|
||||
return msg.isOwnShot() ? logic.getOpponentMap() : logic.getOwnMap();
|
||||
}
|
||||
|
||||
/**
|
||||
* Plays a sound based on the outcome of the shot. Different sounds are played for a miss, hit,
|
||||
* or destruction of a ship.
|
||||
*
|
||||
* @param msg the effect message containing the result of the shot
|
||||
*/
|
||||
private void playSound(EffectMessage msg) {
|
||||
if (!msg.getShot().isHit())
|
||||
logic.playSound(Sound.SPLASH);
|
||||
else if (msg.getDestroyedShip() == null)
|
||||
logic.playSound(Sound.EXPLOSION);
|
||||
else
|
||||
logic.playSound(Sound.DESTROYED_SHIP);
|
||||
}
|
||||
}
|
@ -11,7 +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.client.AnimationFinishedMessage;
|
||||
import pp.battleship.message.server.EffectMessage;
|
||||
import pp.battleship.message.server.GameDetails;
|
||||
import pp.battleship.message.server.ServerMessage;
|
||||
@ -36,7 +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 Set<Player> waitPlayers = new HashSet<>();
|
||||
private final ServerSender serverSender;
|
||||
private Player activePlayer;
|
||||
private ServerState state = ServerState.WAIT;
|
||||
@ -145,18 +145,19 @@ public class ServerGameLogic implements ClientInterpreter {
|
||||
public void received(MapMessage msg, int from) {
|
||||
if (state != ServerState.SET_UP)
|
||||
LOGGER.log(Level.ERROR, "playerReady not allowed in {0}", state); //NON-NLS
|
||||
|
||||
List<Battleship> ships = msg.getShips();
|
||||
|
||||
if (!shipsValid(ships)){
|
||||
LOGGER.log(Level.ERROR, "ship placement by player {0} is Invalid", from);
|
||||
send(getPlayerById(from),null);
|
||||
return;
|
||||
else {
|
||||
if (checkMap(msg.getShips())) {
|
||||
playerReady(getPlayerById(from), msg.getShips());
|
||||
}
|
||||
else {
|
||||
LOGGER.log(Level.WARNING, "Invalid Map sent from player {0}", from); //NON-NLS
|
||||
send(players.get(from), new GameDetails(config));
|
||||
}
|
||||
}
|
||||
|
||||
playerReady(getPlayerById(from), msg.getShips());
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Handles the reception of a ShootMessage.
|
||||
*
|
||||
@ -167,8 +168,11 @@ public class ServerGameLogic implements ClientInterpreter {
|
||||
public void received(ShootMessage msg, int from) {
|
||||
if (state != ServerState.BATTLE)
|
||||
LOGGER.log(Level.ERROR, "shoot not allowed in {0}", state); //NON-NLS
|
||||
else
|
||||
else{
|
||||
setState(ServerState.ANIMATION);
|
||||
shoot(getPlayerById(from), msg.getPosition());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
@ -199,8 +203,6 @@ 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);
|
||||
@ -231,65 +233,61 @@ public class ServerGameLogic implements ClientInterpreter {
|
||||
}
|
||||
}
|
||||
}
|
||||
/* Tests if a given ship is positioned inside of Map constrains
|
||||
*
|
||||
*/
|
||||
|
||||
private boolean isInBounds(Battleship ship){
|
||||
return ship.getMinX() >= 0 && ship.getMaxX() < config.getMapWidth() &&
|
||||
ship.getMinY() >= 0 && ship.getMaxY() < config.getMapHeight();
|
||||
/**
|
||||
* Handles the reception of a AnimationFinishedMessage.
|
||||
*
|
||||
* @param msg the received MapMessage
|
||||
* @param from the ID of the sender client
|
||||
*/
|
||||
@Override
|
||||
public void received(AnimationFinishedMessage msg, int from) {
|
||||
if (state != ServerState.ANIMATION) {
|
||||
LOGGER.log(Level.ERROR, "animation finished not allowed in {0}", state);
|
||||
}
|
||||
else {
|
||||
LOGGER.log(Level.DEBUG, "anim received from {0}", getPlayerById(from));
|
||||
Player player = getPlayerById(from);
|
||||
if (!waitPlayers.add(player)) {
|
||||
LOGGER.log(Level.ERROR, "{0} already sent animation finished", player); //NON-NLS
|
||||
return;
|
||||
}
|
||||
if (waitPlayers.size() == 2) {
|
||||
waitPlayers = new HashSet<>();
|
||||
setState(ServerState.BATTLE);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* Tests if a ship occupies the space of another ship and marks occupied points in the Map.
|
||||
/**
|
||||
* Validates the placement of battleships on the map.
|
||||
* Ensures that:
|
||||
* <ul>
|
||||
* <li>The number of ships matches the configuration.</li>
|
||||
* <li>Ships are within the map's boundaries.</li>
|
||||
* <li>Ships do not overlap.</li>
|
||||
* </ul>
|
||||
*
|
||||
* @param ships the list of {@link Battleship} objects to validate
|
||||
* @return {@code true} if all ships are placed correctly; {@code false} otherwise
|
||||
*/
|
||||
private boolean shipsValid(List<Battleship> ships) {
|
||||
private boolean checkMap(List<Battleship> ships) {
|
||||
int numShips = config.getShipNums().values().stream().mapToInt(Integer::intValue).sum();
|
||||
if (numShips != ships.size()) return false;
|
||||
|
||||
Set<IntPoint> occupied = new HashSet<>();
|
||||
|
||||
for (Battleship ship : ships) {
|
||||
if (!isInBounds(ship)){
|
||||
return false;
|
||||
}
|
||||
for (int x = ship.getMinX(); x <= ship.getMaxX(); x++) {
|
||||
for (int y = ship.getMinY(); y <= ship.getMaxY(); y++) {
|
||||
IntPoint point = new IntPoint(x,y);
|
||||
if (!occupied.add(point)){
|
||||
return false;
|
||||
|
||||
}
|
||||
List<IntPoint> occupied = new ArrayList<>();
|
||||
|
||||
for (Battleship battleship : ships) {
|
||||
int x = battleship.getX();
|
||||
int y = battleship.getY();
|
||||
for (int i = 0; i < battleship.getLength(); i++) {
|
||||
if (x >= 0 && x < config.getMapWidth() && y >= 0 && y < config.getMapHeight() && !occupied.contains(new IntPoint(x, y))) {
|
||||
occupied.add(new IntPoint(x, y));
|
||||
x += battleship.getRot().dx();
|
||||
y += battleship.getRot().dy();
|
||||
}
|
||||
else return false;
|
||||
}
|
||||
}
|
||||
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.");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -26,13 +26,14 @@ enum ServerState {
|
||||
*/
|
||||
BATTLE,
|
||||
|
||||
/**
|
||||
* The server is waiting for all clients to finish the shoot animation.
|
||||
*/
|
||||
ANIMATION,
|
||||
|
||||
|
||||
/**
|
||||
* The game has ended because all the ships of one player have been destroyed.
|
||||
*/
|
||||
GAME_OVER,
|
||||
/**
|
||||
* The games is frozen as long as the shell animation is running
|
||||
*/
|
||||
SHELL_IN_FLIGHT
|
||||
|
||||
}
|
||||
|
@ -11,8 +11,8 @@ import pp.battleship.message.client.ClientInterpreter;
|
||||
import pp.battleship.message.client.ClientMessage;
|
||||
import pp.battleship.message.client.MapMessage;
|
||||
import pp.battleship.message.client.ShootMessage;
|
||||
import pp.battleship.message.client.AnimationFinishedMessage;
|
||||
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}.
|
||||
@ -64,6 +64,12 @@ class Copycat implements ClientInterpreter {
|
||||
copiedMessage = new MapMessage(msg.getShips().stream().map(Copycat::copy).toList());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void received(AnimationFinishedMessage msg, int from) {
|
||||
copiedMessage = msg;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Creates a copy of the provided {@link Battleship}.
|
||||
*
|
||||
@ -74,10 +80,4 @@ class Copycat implements ClientInterpreter {
|
||||
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'");
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -3,6 +3,7 @@ package pp.battleship.game.singlemode;
|
||||
import pp.battleship.game.client.BattleshipClient;
|
||||
import pp.battleship.message.client.MapMessage;
|
||||
import pp.battleship.message.client.ShootMessage;
|
||||
import pp.battleship.message.client.AnimationFinishedMessage;
|
||||
import pp.battleship.message.server.EffectMessage;
|
||||
import pp.battleship.message.server.GameDetails;
|
||||
import pp.battleship.message.server.ServerInterpreter;
|
||||
@ -121,6 +122,7 @@ class RobotClient implements ServerInterpreter {
|
||||
@Override
|
||||
public void received(EffectMessage msg) {
|
||||
LOGGER.log(Level.INFO, "Received EffectMessage: {0}", msg); //NON-NLS
|
||||
connection.sendRobotMessage(new AnimationFinishedMessage());
|
||||
if (msg.isMyTurn())
|
||||
shoot();
|
||||
}
|
||||
|
@ -0,0 +1,24 @@
|
||||
package pp.battleship.message.client;
|
||||
|
||||
import com.jme3.network.serializing.Serializable;
|
||||
|
||||
/**
|
||||
* Represents a message indicating that an animation has finished on the client side.
|
||||
*/
|
||||
@Serializable
|
||||
public class AnimationFinishedMessage extends ClientMessage {
|
||||
public AnimationFinishedMessage() {
|
||||
super();
|
||||
}
|
||||
|
||||
/**
|
||||
* Accepts a visitor to process this message.
|
||||
*
|
||||
* @param interpreter the visitor to process this message
|
||||
* @param from the connection ID from which the message was received
|
||||
*/
|
||||
@Override
|
||||
public void accept(ClientInterpreter interpreter, int from) {
|
||||
interpreter.received(this, from);
|
||||
}
|
||||
}
|
@ -19,10 +19,6 @@ public interface ClientInterpreter {
|
||||
*/
|
||||
void received(ShootMessage msg, int from);
|
||||
|
||||
// TODO
|
||||
void received(ShellAnimationFinishedMessage shellAnimationFinishedMessage, int from);
|
||||
|
||||
|
||||
/**
|
||||
* Processes a received MapMessage.
|
||||
*
|
||||
@ -30,4 +26,12 @@ public interface ClientInterpreter {
|
||||
* @param from the connection ID from which the message was received
|
||||
*/
|
||||
void received(MapMessage msg, int from);
|
||||
|
||||
/**
|
||||
* Processes a received AnimationFinishedMessage.
|
||||
*
|
||||
* @param animationFinishedMessage the MapMessage to be processed
|
||||
* @param from the connection ID from which the message was received
|
||||
*/
|
||||
void received(AnimationFinishedMessage animationFinishedMessage, int from);
|
||||
}
|
||||
|
@ -1,10 +0,0 @@
|
||||
package pp.battleship.message.client;
|
||||
|
||||
public class ShellAnimationFinishedMessage extends ClientMessage{
|
||||
|
||||
@Override
|
||||
public void accept(ClientInterpreter interpreter, int from) {
|
||||
interpreter.received(this, from);
|
||||
}
|
||||
|
||||
}
|
@ -1,48 +1,126 @@
|
||||
package pp.battleship.model;
|
||||
|
||||
|
||||
import com.jme3.math.Quaternion;
|
||||
import com.jme3.math.Vector3f;
|
||||
|
||||
public class Shell {
|
||||
private Vector3f startPosition;
|
||||
private Vector3f targetPosition;
|
||||
private Vector3f currentPosition;
|
||||
private float speed;
|
||||
private boolean isAtTarget;
|
||||
/**
|
||||
* The {@code Shell} class represents a projectile fired by a ship in the Battleship game.
|
||||
* It models the position and rotation of the projectile and allows for its movement along
|
||||
* a Bezier curve.
|
||||
*/
|
||||
public class Shell implements Item {
|
||||
/**
|
||||
* Initial position of the shell
|
||||
*/
|
||||
private final static Vector3f INIT_POS = new Vector3f(-3, 7, -3);
|
||||
/**
|
||||
* The overshot difference vector used to get a shallower flight path
|
||||
*/
|
||||
private final static Vector3f OVER_SHOT_DIFF = new Vector3f(-1, -1, -1);
|
||||
// Target shot position
|
||||
private final Vector3f shotPosition;
|
||||
// Position on top of shotPosition used for Bezier curve
|
||||
private final Vector3f overShotPosition;
|
||||
// Current position of the shell
|
||||
private Vector3f position;
|
||||
// Current rotation of the shell
|
||||
private final Quaternion rotation;
|
||||
|
||||
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;
|
||||
/**
|
||||
* Constructs a new {@code Shell} object using the given {@code Shot} target.
|
||||
* The initial position, target position, and overshot position are calculated.
|
||||
*
|
||||
* @param shot The target {@code Shot} object containing the destination coordinates.
|
||||
*/
|
||||
public Shell(Shot shot) {
|
||||
this.shotPosition = new Vector3f(shot.getX(), 0, shot.getY());
|
||||
this.overShotPosition = new Vector3f(shotPosition.x, INIT_POS.y, shotPosition.z).add(OVER_SHOT_DIFF);
|
||||
this.position = INIT_POS;
|
||||
this.rotation = new Quaternion();
|
||||
}
|
||||
|
||||
// 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);
|
||||
/**
|
||||
* Gets the current position of the shell.
|
||||
*
|
||||
* @return The current position as a {@code Vector3f}.
|
||||
*/
|
||||
public Vector3f getPosition() {
|
||||
return this.position;
|
||||
}
|
||||
|
||||
// Prüfe, ob das Geschoss das Ziel erreicht hat
|
||||
if (currentPosition.distance(targetPosition) < speed * deltaTime) {
|
||||
currentPosition.set(targetPosition);
|
||||
isAtTarget = true;
|
||||
}
|
||||
/**
|
||||
* Gets the current rotation of the shell.
|
||||
*
|
||||
* @return The current rotation as a {@code Quaternion}.
|
||||
*/
|
||||
public Quaternion getRotation() {
|
||||
return this.rotation;
|
||||
}
|
||||
|
||||
/**
|
||||
* Moves the shell along a Bezier curve based on the given time factor {@code t}.
|
||||
* The position and rotation of the shell are updated.
|
||||
*
|
||||
* @param t The time factor between 0 and 1, representing the progress of the shell's flight.
|
||||
*/
|
||||
public void move(float t) {
|
||||
if (t > 1f) t = 1f;
|
||||
Vector3f newPosition = bezInt(INIT_POS, overShotPosition, shotPosition, t);
|
||||
updateRotation(position, newPosition);
|
||||
this.position = newPosition;
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs a quadratic Bezier interpolation between three points based on the time factor {@code t}.
|
||||
*
|
||||
* @param p1 The start position.
|
||||
* @param p2 The overshot position.
|
||||
* @param p3 The target position.
|
||||
* @param t The time factor for interpolation.
|
||||
* @return The interpolated position as a {@code Vector3f}.
|
||||
*/
|
||||
private Vector3f bezInt(Vector3f p1, Vector3f p2, Vector3f p3, float t) {
|
||||
Vector3f inA = linInt(p1, p2, t);
|
||||
Vector3f inB = linInt(p2, p3, t);
|
||||
return linInt(inA, inB, t);
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs linear interpolation between two points {@code p1} and {@code p2} based on the time factor {@code t}.
|
||||
*
|
||||
* @param p1 The start position.
|
||||
* @param p2 The end position.
|
||||
* @param t The time factor for interpolation.
|
||||
* @return The interpolated position as a {@code Vector3f}.
|
||||
*/
|
||||
private Vector3f linInt(Vector3f p1, Vector3f p2, float t) {
|
||||
float x = p1.getX() + t * (p2.getX() - p1.getX());
|
||||
float y = p1.getY() + t * (p2.getY() - p1.getY());
|
||||
float z = p1.getZ() + t * (p2.getZ() - p1.getZ());
|
||||
return new Vector3f(x, y, z);
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates the rotation of the shell to face the new position along its flight path.
|
||||
*
|
||||
* @param oldPos The previous position of the shell.
|
||||
* @param newPos The new position of the shell.
|
||||
*/
|
||||
private void updateRotation(Vector3f oldPos, Vector3f newPos) {
|
||||
Vector3f direction = newPos.subtract(oldPos).normalize();
|
||||
if (direction.lengthSquared() > 0) {
|
||||
this.rotation.lookAt(direction, Vector3f.UNIT_Y);
|
||||
}
|
||||
}
|
||||
|
||||
// Gibt die aktuelle Position des Geschosses zurück
|
||||
public Vector3f getCurrentPosition() {
|
||||
return currentPosition;
|
||||
@Override
|
||||
public <T> T accept(Visitor<T> visitor) {
|
||||
return visitor.visit(this);
|
||||
}
|
||||
|
||||
// Überprüft, ob das Geschoss das Ziel erreicht hat
|
||||
public boolean isAtTarget() {
|
||||
return isAtTarget;
|
||||
@Override
|
||||
public void accept(VoidVisitor visitor) {
|
||||
visitor.visit(this);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,47 +0,0 @@
|
||||
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
|
||||
}
|
||||
}
|
@ -91,6 +91,16 @@ public class ShipMap {
|
||||
addItem(shot);
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers a shot on the map and triggers an item addition event.
|
||||
*
|
||||
* @param shell the shell to be registered on the map
|
||||
*/
|
||||
public void add(Shell shell) {
|
||||
addItem(shell);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Removes an item from the map and triggers an item removal event.
|
||||
*
|
||||
|
@ -28,4 +28,14 @@ public interface Visitor<T> {
|
||||
* @return the result of visiting the Battleship element
|
||||
*/
|
||||
T visit(Battleship ship);
|
||||
/**
|
||||
* Visits a Shell element
|
||||
*
|
||||
* @param shell the Shell element to visit
|
||||
* @return the result of visitung the Battleship element
|
||||
*/
|
||||
|
||||
T visit(Shell shell);
|
||||
|
||||
|
||||
}
|
||||
|
@ -25,4 +25,11 @@ public interface VoidVisitor {
|
||||
* @param ship the Battleship element to visit
|
||||
*/
|
||||
void visit(Battleship ship);
|
||||
|
||||
/**
|
||||
* * Visits a Shell element
|
||||
* @param shell the Shell element to visit
|
||||
*/
|
||||
void visit(Shell shell);
|
||||
|
||||
}
|
||||
|
@ -22,5 +22,10 @@ public enum Sound {
|
||||
/**
|
||||
* Sound of a ship being destroyed.
|
||||
*/
|
||||
DESTROYED_SHIP
|
||||
DESTROYED_SHIP,
|
||||
/**
|
||||
* Sound of a shell flying
|
||||
*/
|
||||
SHELL_FLYING
|
||||
|
||||
}
|
||||
|
@ -21,6 +21,7 @@ import pp.battleship.game.server.ServerSender;
|
||||
import pp.battleship.message.client.ClientMessage;
|
||||
import pp.battleship.message.client.MapMessage;
|
||||
import pp.battleship.message.client.ShootMessage;
|
||||
import pp.battleship.message.client.AnimationFinishedMessage;
|
||||
import pp.battleship.message.server.EffectMessage;
|
||||
import pp.battleship.message.server.GameDetails;
|
||||
import pp.battleship.message.server.ServerMessage;
|
||||
@ -80,8 +81,9 @@ public class BattleshipServer implements MessageListener<HostedConnection>, Conn
|
||||
|
||||
public void run() {
|
||||
startServer();
|
||||
while (true)
|
||||
while (true) {
|
||||
processNextMessage();
|
||||
}
|
||||
}
|
||||
|
||||
private void startServer() {
|
||||
@ -115,14 +117,17 @@ public class BattleshipServer implements MessageListener<HostedConnection>, Conn
|
||||
Serializer.registerClass(MapMessage.class);
|
||||
Serializer.registerClass(ShootMessage.class);
|
||||
Serializer.registerClass(EffectMessage.class);
|
||||
Serializer.registerClass(AnimationFinishedMessage.class);
|
||||
Serializer.registerClass(Battleship.class);
|
||||
Serializer.registerClass(IntPoint.class);
|
||||
Serializer.registerClass(Shot.class);
|
||||
|
||||
}
|
||||
|
||||
private void registerListeners() {
|
||||
myServer.addMessageListener(this, MapMessage.class);
|
||||
myServer.addMessageListener(this, ShootMessage.class);
|
||||
myServer.addMessageListener(this, AnimationFinishedMessage.class);
|
||||
myServer.addConnectionListener(this);
|
||||
}
|
||||
|
||||
|