Solution for exercise 13:

edited another state in the server and client, added the rocket, added the 'Shell.java' and 'ShellControl.java'
edited the logic for the states and 3 messages for the server client comunication, edited the 'SeaSynchronizer' and ShipMapSynchronizer', so that the animations will be displayed, added the sound for the rocket
This commit is contained in:
Benjamin Feyer
2024-10-12 00:41:24 +02:00
parent 30a735bd6e
commit 3755cca62e
33 changed files with 433782 additions and 173 deletions

View File

@@ -33,6 +33,7 @@ public class GameSound extends AbstractAppState implements GameEventListener {
private AudioNode splashSound;
private AudioNode shipDestroyedSound;
private AudioNode explosionSound;
private AudioNode rocketSound;
/**
* Checks if sound is enabled in the preferences.
@@ -77,6 +78,7 @@ public void initialize(AppStateManager stateManager, Application app) {
shipDestroyedSound = loadSound(app, "Sound/Effects/sunken.wav"); //NON-NLS
splashSound = loadSound(app, "Sound/Effects/splash.wav"); //NON-NLS
explosionSound = loadSound(app, "Sound/Effects/explosion.wav"); //NON-NLS
rocketSound = loadSound(app, "Sound/Effects/rocket-loop-99748.wav");
}
/**
@@ -92,8 +94,7 @@ private AudioNode loadSound(Application app, String name) {
sound.setLooping(false);
sound.setPositional(false);
return sound;
}
catch (AssetLoadException | AssetNotFoundException ex) {
} catch (AssetLoadException | AssetNotFoundException ex) {
LOGGER.log(Level.ERROR, ex.getMessage(), ex);
}
return null;
@@ -129,6 +130,25 @@ public void receivedEvent(SoundEvent event) {
case EXPLOSION -> explosion();
case SPLASH -> splash();
case DESTROYED_SHIP -> shipDestroyed();
case ROCKET -> rocket();
case ROCKET_STOP -> rocketStopped();
}
}
/**
* this method plays the sound of the rocket
*/
private void rocket() {
if (isEnabled() && splashSound != null)
rocketSound.playInstance();
}
/**
* this method stops the sound of the rocket
*/
private void rocketStopped() {
rocketSound.stop();
}
}

View File

@@ -12,20 +12,26 @@
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
import pp.battleship.model.Battleship;
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.
* It handles the rendering of ships and shots on the map view, updating the view
* whenever changes occur in the model.
*/
class MapViewSynchronizer extends ShipMapSynchronizer {
private static final Logger LOGGER = System.getLogger(MapViewSynchronizer.class.getName());
// Constants for rendering properties
private static final float SHIP_LINE_WIDTH = 6f;
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_IN_GRID = 0.1f;
// Colors used for different visual elements
private static final ColorRGBA HIT_COLOR = ColorRGBA.Red;
@@ -65,9 +71,9 @@ public Spatial visit(Shot shot) {
// Create and return a rectangle representing the shot
return view.getApp().getDraw().makeRectangle(p1.getX(), p1.getY(),
SHOT_DEPTH,
p2.getX() - p1.getX(), p2.getY() - p1.getY(),
color);
SHOT_DEPTH,
p2.getX() - p1.getX(), p2.getY() - p1.getY(),
color);
}
/**
@@ -109,6 +115,35 @@ public Spatial visit(Battleship ship) {
return shipNode;
}
/**
* this method will create a representation of a shell in the map
*
* @param shell the Shell element to visit
* @return the node the representation is attached to
*/
@Override
public Spatial visit(Shell shell) {
LOGGER.log(Logger.Level.DEBUG, "Visiting {0}", shell);
final Node shellNode = new Node("shell");
final Position target = view.modelToView(shell.getX(), shell.getY());
final Position startPosition = view.modelToView(SHELL_IN_GRID, SHELL_IN_GRID);
shellNode.attachChild(createShell());
shellNode.setLocalTranslation(startPosition.getX(), startPosition.getY(), SHELL_DEPTH);
shellNode.scale(18f);
shellNode.addControl(new ShellMapControl(view.getApp(), target, shell));
return shellNode;
}
/**
* returns the red dot for the shell in the MapViewSynchronizer
*
* @return Spatial
*/
private Spatial createShell() {
return view.getApp().getDraw().makeFilledCircle(ColorRGBA.Red);
}
/**
* Creates a line geometry representing part of the ship's border.
*

View File

@@ -9,9 +9,6 @@
import com.jme3.effect.ParticleEmitter;
import com.jme3.effect.ParticleMesh.Type;
import com.jme3.light.AmbientLight;
import com.jme3.light.DirectionalLight;
import com.jme3.light.Light;
import com.jme3.material.Material;
import com.jme3.material.RenderState.BlendMode;
import com.jme3.math.ColorRGBA;
@@ -21,18 +18,15 @@
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
import com.jme3.scene.shape.Box;
import com.jme3.scene.shape.Cylinder;
import com.simsilica.lemur.effect.EffectControl;
import pp.battleship.client.BattleshipApp;
import pp.battleship.model.Battleship;
import pp.battleship.model.Rotation;
import pp.battleship.model.Shell;
import pp.battleship.model.ShipMap;
import pp.battleship.model.Shot;
import java.awt.Point;
import java.awt.geom.Point2D;
import java.lang.System.Logger;
import java.lang.System.Logger.Level;
import java.util.Timer;
import static java.util.Objects.requireNonNull;
import static pp.util.FloatMath.HALF_PI;
@@ -45,6 +39,8 @@
* logic for the sea map.
*/
class SeaSynchronizer extends ShipMapSynchronizer {
private static final Logger LOGGER = System.getLogger(SeaSynchronizer.class.getName());
private static final String UNSHADED = "Common/MatDefs/Misc/Unshaded.j3md"; //NON-NLS
private static final String KING_GEORGE_V_MODEL = "Models/KingGeorgeV/KingGeorgeV.j3o"; //NON-NLS
private static final String SUBMARINE = "Models/Submarine/submarine.obj";
@@ -54,6 +50,7 @@ class SeaSynchronizer extends ShipMapSynchronizer {
private static final String COLOR = "Color"; //NON-NLS
private static final String SHIP = "ship"; //NON-NLS
private static final String SHOT = "shot"; //NON-NLS
private static final String ROCKET = "Models/Rocket/rocket.j3o";
private static final ColorRGBA BOX_COLOR = ColorRGBA.Gray;
private static final ColorRGBA SPLASH_COLOR = new ColorRGBA(0f, 0f, 1f, 0.4f);
private static final ColorRGBA HIT_COLOR = new ColorRGBA(1f, 0f, 0f, 0.4f);
@@ -145,8 +142,7 @@ private ParticleEmitter createFire(Shot shot) {
Vector3f firePos = shotWorld.subtract(shipNodePos);
if (map.findShipAt(shot.getX(), shot.getY()).getLength() == 2) {
hitEffect.setLocalTranslation(firePos.x, 0.25f, firePos.z);
}
else {
} else {
hitEffect.setLocalTranslation(firePos.x, 0.5f, firePos.z);
}
return hitEffect;
@@ -240,6 +236,38 @@ public Spatial visit(Battleship ship) {
return node;
}
/**
* Visits a {@link Shell} and creates a graphical representation of it.
* The representation is a 3D model
*
* @param shell the shell to be represented
* @return the node containing the graphical representation of the shell
*/
@Override
public Spatial visit(Shell shell) {
LOGGER.log(Level.INFO, "was visited by SeaSynchronizer");
final Node node = new Node("Shell");
node.attachChild(createShell());
node.setLocalTranslation(shell.getY() + 0.5f, 10, shell.getX() + 0.5f);
node.addControl(new ShellControl(shell, app));
return node;
}
/**
* this method loads the Model
*
* @return model
*/
private Spatial createShell() {
LOGGER.log(Level.INFO, "created Shell");
final Spatial model = app.getAssetManager().loadModel(ROCKET);
model.rotate(PI, PI, 0);
model.scale(0.0005f);
model.move(0, 0, 0);
model.setShadowMode(ShadowMode.CastAndReceive);
return model;
}
/**
* Creates the appropriate graphical representation of the specified battleship.
* The representation is either a detailed model or a simple box based on the length of the ship.
@@ -266,8 +294,8 @@ private Spatial createShip(Battleship ship) {
*/
private Spatial createBox(Battleship ship) {
final Box box = new Box(0.5f * (ship.getMaxY() - ship.getMinY()) + 0.3f,
0.3f,
0.5f * (ship.getMaxX() - ship.getMinX()) + 0.3f);
0.3f,
0.5f * (ship.getMaxX() - ship.getMinX()) + 0.3f);
final Geometry geometry = new Geometry(SHIP, box);
geometry.setMaterial(createColoredMaterial(BOX_COLOR));
geometry.setShadowMode(ShadowMode.CastAndReceive);
@@ -275,6 +303,12 @@ private Spatial createBox(Battleship ship) {
return geometry;
}
/**
* Creates a Destroyer to represent a battleship that has the length of 3.
*
* @param ship the battleship to be represented
* @return the model representing the battleship
*/
private Spatial createDestroyer(Battleship ship) {
final Spatial model = app.getAssetManager().loadModel(DESTROYER);
model.rotate(-HALF_PI, calculateRotationAngle(ship.getRot()) - HALF_PI, 0f);
@@ -284,6 +318,12 @@ private Spatial createDestroyer(Battleship ship) {
return model;
}
/**
* Creates a Submarine to represent a battleship that has the length 2.
*
* @param ship the battleship to be represented
* @return the model representing the battleship
*/
private Spatial createSubmarine(Battleship ship) {
final Spatial model = app.getAssetManager().loadModel(SUBMARINE);
model.rotate(-HALF_PI, calculateRotationAngle(ship.getRot()), 0f);
@@ -295,6 +335,12 @@ private Spatial createSubmarine(Battleship ship) {
return model;
}
/**
* Creates a SmallShip to represent a battleship that has the length y.
*
* @param ship the battleship to be represented
* @return the model representing the battleship
*/
private Spatial createSmallShip(Battleship ship) {
final Spatial model = app.getAssetManager().loadModel(SMALL_SHIP);

View File

@@ -2,40 +2,67 @@
import com.jme3.renderer.RenderManager;
import com.jme3.renderer.ViewPort;
import com.jme3.scene.Spatial;
import com.jme3.scene.control.AbstractControl;
import pp.battleship.client.BattleshipApp;
import pp.battleship.message.client.AnimationEndMessage;
import pp.battleship.model.IntPoint;
import pp.battleship.model.Shell;
import pp.battleship.model.ShipMap;
import java.lang.System.Logger;
import java.lang.System.Logger.Level;
/**
* this class controls the Shell in the view
*/
public class ShellControl extends AbstractControl {
private Shell shell;
private ShipMap map;
private static final Logger LOGGER = System.getLogger(ShellControl.class.getName());
/**
* the shell, that is displayed
*/
private Shell shell;
/**
* the Height, when the shell will despawn
*/
private static final Float HEIGHT = 0f;
public ShellControl(Shell shell, ShipMap map){
this.shell=shell;
this.map=map;
/**
* the battleship app
*/
private BattleshipApp app;
/**
* the constructor for this class
*
* @param shell the shell it displays
* @param app the BattleshipApp
*/
public ShellControl(Shell shell, BattleshipApp app) {
LOGGER.log(Level.INFO, "ShellControl has been initialized");
this.shell = shell;
this.app = app;
}
/**
* this method controls the movement of the shell in dependent on fpt
*
* @param tpf time per frame (in seconds)
*/
@Override
protected void controlUpdate(float tpf) {
if (spatial == null) return;
if(spatial.getLocalTranslation().getY()<=HEIGHT){
if (spatial.getLocalTranslation().getY() <= HEIGHT) {
spatial.getParent().detachChild(spatial);
}
else{
spatial.move(0,-0.5f*tpf,0);
app.getGameLogic().send(new AnimationEndMessage(new IntPoint(shell.getX(), shell.getY())));
} else {
spatial.move(0, -1 * 4f * tpf, 0);
}
}
@Override
protected void controlRender(RenderManager rm, ViewPort vp) {
}
public Shell getShell(){
return shell;
//not in use
}
}

View File

@@ -0,0 +1,77 @@
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.battleship.model.Shell;
import pp.battleship.notification.Sound;
import pp.util.Position;
/**
* this class controls the behaviour of a shell in a 2d map
*/
public class ShellMapControl extends AbstractControl {
private static final System.Logger LOGGER = System.getLogger(ShellMapControl.class.getName());
/**
* the position in map-coordinates
*/
private final Position position;
/**
* the vector, the shell is going on the 2d screen
*/
private static final Vector3f vector = new Vector3f();
/**
* the battleship app
*/
private final BattleshipApp app;
/**
* the shell displayed
*/
private final Shell shell;
/**
* the constructor for this class
*
* @param app the Battleship app
* @param position the position shot at in map-coordinates
* @param shell the shell shot
*/
public ShellMapControl(BattleshipApp app, Position position, Shell shell) {
super();
this.position = position;
this.app = app;
this.shell = shell;
vector.set(new Vector3f(position.getX(), position.getY(), 0));
}
/**
* the update loop for this shell
*
* @param tpf time per frame (in seconds)
*/
protected void controlUpdate(float tpf) {
if (spatial.getLocalTranslation().getX() >= position.getX() && spatial.getLocalTranslation().getY() >= position.getY()) {
app.getGameLogic().playSound(Sound.ROCKET_STOP);
spatial.getParent().detachChild(spatial);
app.getGameLogic().send(new AnimationEndMessage(new IntPoint(shell.getX(), shell.getY())));
LOGGER.log(System.Logger.Level.DEBUG, "shell has been deleted", spatial.getLocalTranslation());
} else {
spatial.move(vector.mult(tpf));
}
}
@Override
protected void controlRender(RenderManager rm, ViewPort vp) {
}
}

View File

@@ -18,13 +18,11 @@
import pp.battleship.game.server.Player;
import pp.battleship.game.server.ServerGameLogic;
import pp.battleship.game.server.ServerSender;
import pp.battleship.message.client.AnimationEndMessage;
import pp.battleship.message.client.ClientMessage;
import pp.battleship.message.client.MapMessage;
import pp.battleship.message.client.ShootMessage;
import pp.battleship.message.server.EffectMessage;
import pp.battleship.message.server.GameDetails;
import pp.battleship.message.server.ServerMessage;
import pp.battleship.message.server.StartBattleMessage;
import pp.battleship.message.server.*;
import pp.battleship.model.Battleship;
import pp.battleship.model.IntPoint;
import pp.battleship.model.Shot;
@@ -117,11 +115,15 @@ private void initializeSerializables() {
Serializer.registerClass(Battleship.class);
Serializer.registerClass(IntPoint.class);
Serializer.registerClass(Shot.class);
Serializer.registerClass(AnimationEndMessage.class);
Serializer.registerClass(AnimationStartMessage.class);
Serializer.registerClass(BackToBattleStateMessage.class);
}
private void registerListeners() {
myServer.addMessageListener(this, MapMessage.class);
myServer.addMessageListener(this, ShootMessage.class);
myServer.addMessageListener(this, AnimationEndMessage.class);
myServer.addConnectionListener(this);
}

View File

@@ -0,0 +1,3 @@
Rocket origin:
https://free3d.com/3d-model/proton-rocket-31617.html
Licence: free for personal use

View File

@@ -0,0 +1,3 @@
RocketSound origin:
https://pixabay.com/sound-effects/rocket-loop-99748/
Licence: free to use