From c56767d9943cb933a7c78db6ddf6476d84965675 Mon Sep 17 00:00:00 2001 From: Hanno Fleischer Date: Fri, 11 Oct 2024 00:45:49 +0200 Subject: [PATCH] added rest solution for exercise 13 added the representation for the shell element in the map which will be displayed when you shoot, --- .../client/gui/MapViewSynchronizer.java | 22 ++++++++--- .../battleship/client/gui/ShellControl.java | 2 + .../client/gui/ShellMapControl.java | 39 +++++++++++++++++++ .../battleship/game/client/AnimatonState.java | 2 +- 4 files changed, 58 insertions(+), 7 deletions(-) create mode 100644 Projekte/battleship/client/src/main/java/pp/battleship/client/gui/ShellMapControl.java diff --git a/Projekte/battleship/client/src/main/java/pp/battleship/client/gui/MapViewSynchronizer.java b/Projekte/battleship/client/src/main/java/pp/battleship/client/gui/MapViewSynchronizer.java index 7116d7ba..2792aa09 100644 --- a/Projekte/battleship/client/src/main/java/pp/battleship/client/gui/MapViewSynchronizer.java +++ b/Projekte/battleship/client/src/main/java/pp/battleship/client/gui/MapViewSynchronizer.java @@ -12,9 +12,11 @@ import com.jme3.scene.Node; import com.jme3.scene.Spatial; import pp.battleship.model.Battleship; +import pp.battleship.model.IntPoint; import pp.battleship.model.Shell; import pp.battleship.model.Shot; import pp.util.Position; +import java.lang.System.Logger; /** * Synchronizes the visual representation of the ship map with the game model. @@ -27,6 +29,10 @@ class MapViewSynchronizer extends ShipMapSynchronizer { private static final float SHOT_DEPTH = -2f; private static final float SHIP_DEPTH = 0f; private static final float INDENT = 4f; + private static final float SHELL_DEPTH = 8f; + + private static final float SHELL_SIZE = 0.75f; + private static final float SHELL_CENTERED_IN_MAP_GRID = 0.0625f; // Colors used for different visual elements private static final ColorRGBA HIT_COLOR = ColorRGBA.Red; @@ -38,6 +44,8 @@ class MapViewSynchronizer extends ShipMapSynchronizer { // The MapView associated with this synchronizer private final MapView view; + static final Logger LOGGER = System.getLogger(MapViewSynchronizer.class.getName()); + /** * Constructs a new MapViewSynchronizer for the given MapView. * Initializes the synchronizer and adds existing elements from the model to the view. @@ -59,6 +67,7 @@ public MapViewSynchronizer(MapView view) { */ @Override public Spatial visit(Shot shot) { + LOGGER.log(Logger.Level.DEBUG, "Visiting " + shot); // Convert the shot's model coordinates to view coordinates final Position p1 = view.modelToView(shot.getX(), shot.getY()); final Position p2 = view.modelToView(shot.getX() + 1, shot.getY() + 1); @@ -112,16 +121,16 @@ public Spatial visit(Battleship ship) { @Override public Spatial visit(Shell shell) { + LOGGER.log(Logger.Level.DEBUG, "Visiting {0}", shell); final Node shellNode = new Node("shell"); - final Position p1 = view.modelToView(shell.getX(), shell.getY()); - final Position p2 = view.modelToView(shell.getX() + 1, shell.getY() + 1); + final Position p2 = view.modelToView(shell.getX() + SHELL_SIZE, shell.getY() + SHELL_SIZE); - final float x1 = p1.getX() + INDENT; - final float y1 = p1.getY() + INDENT; - final float x2 = p2.getX() - INDENT; - final float y2 = p2.getY() - INDENT; + final Position startPosition = view.modelToView(SHELL_CENTERED_IN_MAP_GRID, SHELL_CENTERED_IN_MAP_GRID); + shellNode.attachChild(view.getApp().getDraw().makeRectangle(startPosition.getX(), startPosition.getY(), SHELL_DEPTH, p2.getX() - p1.getX(), p2.getY() - p1.getY(), ColorRGBA.Black)); + shellNode.setLocalTranslation(startPosition.getX(), startPosition.getY(), SHELL_DEPTH); + shellNode.addControl(new ShellMapControl(p1, view.getApp(), new IntPoint(shell.getX(), shell.getY()))); return shellNode; } @@ -136,6 +145,7 @@ public Spatial visit(Shell shell) { * @return a Geometry representing the line */ private Geometry shipLine(float x1, float y1, float x2, float y2, ColorRGBA color) { + LOGGER.log(Logger.Level.DEBUG, "created ship line"); return view.getApp().getDraw().makeFatLine(x1, y1, x2, y2, SHIP_DEPTH, color, SHIP_LINE_WIDTH); } } diff --git a/Projekte/battleship/client/src/main/java/pp/battleship/client/gui/ShellControl.java b/Projekte/battleship/client/src/main/java/pp/battleship/client/gui/ShellControl.java index a9093044..685e8693 100644 --- a/Projekte/battleship/client/src/main/java/pp/battleship/client/gui/ShellControl.java +++ b/Projekte/battleship/client/src/main/java/pp/battleship/client/gui/ShellControl.java @@ -1,5 +1,6 @@ package pp.battleship.client.gui; +import com.jme3.math.Quaternion; import com.jme3.renderer.RenderManager; import com.jme3.renderer.ViewPort; import com.jme3.scene.control.AbstractControl; @@ -26,6 +27,7 @@ public ShellControl(Shell shell, BattleshipApp app) { @Override protected void controlUpdate(float tpf) { spatial.move(0, -MOVE_SPEED * tpf, 0); + spatial.rotate(0f, 0.05f, 0f); //LOGGER.log(System.Logger.Level.DEBUG, "moved rocket {0}", spatial.getLocalTranslation().getY()); if (spatial.getLocalTranslation().getY() <= 1.5){ spatial.getParent().detachChild(spatial); diff --git a/Projekte/battleship/client/src/main/java/pp/battleship/client/gui/ShellMapControl.java b/Projekte/battleship/client/src/main/java/pp/battleship/client/gui/ShellMapControl.java new file mode 100644 index 00000000..c5d4dd6f --- /dev/null +++ b/Projekte/battleship/client/src/main/java/pp/battleship/client/gui/ShellMapControl.java @@ -0,0 +1,39 @@ +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.client.BattleshipApp; +import pp.battleship.message.client.AnimationEndMessage; +import pp.battleship.model.IntPoint; +import pp.util.Position; + +public class ShellMapControl extends AbstractControl { + private final Position position; + private final IntPoint pos; + private static final Vector3f vector = new Vector3f(); + private final BattleshipApp app; + + public ShellMapControl(Position position, BattleshipApp app, IntPoint pos) { + super(); + this.position = position; + this.pos = pos; + this.app = app; + vector.set(new Vector3f(position.getX(), position.getY(), 0)); + } + + @Override + protected void controlUpdate(float tpf) { + spatial.move(vector.mult(tpf)); + if (spatial.getLocalTranslation().getX() >= position.getX() && spatial.getLocalTranslation().getY() >= position.getY()) { + spatial.getParent().detachChild(spatial); + app.getGameLogic().send(new AnimationEndMessage(pos)); + } + } + + @Override + protected void controlRender(RenderManager rm, ViewPort vp) { + + } +} diff --git a/Projekte/battleship/model/src/main/java/pp/battleship/game/client/AnimatonState.java b/Projekte/battleship/model/src/main/java/pp/battleship/game/client/AnimatonState.java index a9ffd18d..f332feb1 100644 --- a/Projekte/battleship/model/src/main/java/pp/battleship/game/client/AnimatonState.java +++ b/Projekte/battleship/model/src/main/java/pp/battleship/game/client/AnimatonState.java @@ -22,7 +22,7 @@ public AnimatonState(ClientGameLogic logic, boolean myTurn, IntPoint position) { logic.playMusic(Music.BATTLE_THEME); this.myTurn = myTurn; if(myTurn) { - logic.send(new AnimationEndMessage(position)); + logic.getOpponentMap().add(new Shell(position)); }else { logic.getOwnMap().add(new Shell(position)); }