mirror of
https://athene2.informatik.unibw-muenchen.de/progproj/gruppen-ht24/Gruppe-02.git
synced 2025-08-07 00:46:24 +02:00
implements shell
This commit is contained in:
@@ -34,6 +34,7 @@ public class GameSound extends AbstractAppState implements GameEventListener {
|
||||
private AudioNode splashSound;
|
||||
private AudioNode shipDestroyedSound;
|
||||
private AudioNode explosionSound;
|
||||
private AudioNode shellFlyingSound;
|
||||
|
||||
/**
|
||||
* Checks if sound is enabled in the preferences.
|
||||
@@ -78,6 +79,7 @@ public class GameSound extends AbstractAppState implements GameEventListener {
|
||||
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
|
||||
shellFlyingSound = loadSound(app, "Sound/Effects/shell_flying.wav");
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -123,6 +125,20 @@ public class GameSound extends AbstractAppState implements GameEventListener {
|
||||
if (isEnabled() && shipDestroyedSound != null)
|
||||
shipDestroyedSound.playInstance();
|
||||
}
|
||||
/**
|
||||
* Plays the shell flying sound effect.
|
||||
*/
|
||||
public void shellFly() {
|
||||
if (isEnabled() && shellFlyingSound != null) {S
|
||||
shellFlyingSound.playInstance();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles a recieved {@code SoundEvent} and plays the according sound.
|
||||
*
|
||||
* @param event the Sound event to be processed
|
||||
*/
|
||||
|
||||
@Override
|
||||
public void receivedEvent(SoundEvent event) {
|
||||
|
@@ -16,6 +16,7 @@ import com.jme3.scene.Geometry;
|
||||
import com.jme3.scene.Node;
|
||||
import com.jme3.scene.Spatial.CullHint;
|
||||
import com.jme3.scene.shape.Quad;
|
||||
|
||||
import pp.battleship.client.BattleshipApp;
|
||||
import pp.battleship.model.IntPoint;
|
||||
import pp.battleship.model.ShipMap;
|
||||
@@ -28,7 +29,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;
|
||||
|
@@ -10,10 +10,16 @@ package pp.battleship.client.gui;
|
||||
import com.jme3.math.ColorRGBA;
|
||||
import com.jme3.scene.Geometry;
|
||||
import com.jme3.scene.Node;
|
||||
import com.jme3.material.Material;
|
||||
import com.jme3.material.RenderState;
|
||||
import com.jme3.material.RenderState.BlendMode;
|
||||
import com.jme3.scene.shape.Sphere;
|
||||
|
||||
import com.jme3.scene.Spatial;
|
||||
import pp.battleship.model.Battleship;
|
||||
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.
|
||||
@@ -109,6 +115,8 @@ class MapViewSynchronizer extends ShipMapSynchronizer {
|
||||
return shipNode;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Creates a line geometry representing part of the ship's border.
|
||||
*
|
||||
@@ -122,4 +130,24 @@ 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;
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -28,6 +28,7 @@ import pp.battleship.model.Battleship;
|
||||
import pp.battleship.model.Rotation;
|
||||
import pp.battleship.model.ShipMap;
|
||||
import pp.battleship.model.Shot;
|
||||
import pp.battleship.model.Shell;
|
||||
import static pp.util.FloatMath.HALF_PI;
|
||||
import static pp.util.FloatMath.PI;
|
||||
|
||||
@@ -384,4 +385,25 @@ 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,49 @@
|
||||
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,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 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
|
||||
}
|
||||
}
|
@@ -15,7 +15,7 @@ import com.jme3.scene.control.AbstractControl;
|
||||
*/
|
||||
public class SinkingShip extends AbstractControl {
|
||||
|
||||
private float elapsedTime = 0f;
|
||||
private float elapsedTime = 0.2f;
|
||||
private final float burnTiltDuration;
|
||||
private final float sinkDuration;
|
||||
private final float sinkDepth;
|
||||
@@ -79,7 +79,7 @@ public class SinkingShip extends AbstractControl {
|
||||
|
||||
// Apply the tilt angle (remains constant during sinking)
|
||||
float sinkingTiltAngle = FastMath.DEG_TO_RAD * tiltAngle * progress;
|
||||
float sinkingRotationAngle = FastMath.DEG_TO_RAD * 45f * progress; // Additional rotation during sinking
|
||||
float sinkingRotationAngle = FastMath.DEG_TO_RAD * 45f * progress; // Additional rotation during sinking (test)
|
||||
Quaternion tiltRotation = new Quaternion().fromAngles(-sinkingTiltAngle, sinkingRotationAngle, sinkingRotationAngle);
|
||||
spatial.setLocalRotation(tiltRotation);
|
||||
|
||||
@@ -94,6 +94,28 @@ public class SinkingShip extends AbstractControl {
|
||||
}
|
||||
spatial.removeControl(this);
|
||||
}
|
||||
// public void checkAndTriggerExplosion() {
|
||||
// // Prüfen, ob das sinkende Schiff den Boden erreicht hat
|
||||
// if (spatial != null && spatial.getLocalTranslation().y <= sinkDepth) {
|
||||
// // Explosion ausführen, wenn der Boden erreicht ist
|
||||
// triggerExplosionAnimation();
|
||||
//
|
||||
// // Entfernen des Schiffs aus dem Elternknoten
|
||||
// Node parentNode = (Node) spatial.getParent();
|
||||
// if (parentNode != null) {
|
||||
// parentNode.detachChild(spatial);
|
||||
// }
|
||||
//
|
||||
// // Entfernen der Kontrolle vom Spatial
|
||||
// spatial.removeControl(this);
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// private void triggerExplosionAnimation() {
|
||||
// // Code für die Explosion-Animation (alibi Methode)
|
||||
// System.out.println("Explosion wird ausgeführt: Das Schiff hat den Boden erreicht!");
|
||||
// // Hier könnte eine echte Animation eingefügt werden, z.B. Partikeleffekte
|
||||
// }
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -1,4 +1,11 @@
|
||||
|
||||
////////////////////////////////////////
|
||||
// Programming project code
|
||||
// UniBw M, 2022, 2023, 2024
|
||||
// www.unibw.de/inf2
|
||||
// (c) Mark Minas (mark.minas@unibw.de)
|
||||
////////////////////////////////////////
|
||||
|
||||
package pp.battleship.client.server;
|
||||
|
||||
import pp.battleship.message.client.ClientInterpreter;
|
||||
@@ -8,5 +15,4 @@ record ReceivedMessageSelfhost(ClientMessage message, int from) {
|
||||
void process(ClientInterpreter interpreter) {
|
||||
message.accept(interpreter, from);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Binary file not shown.
Binary file not shown.
After Width: | Height: | Size: 43 KiB |
Binary file not shown.
Binary file not shown.
Before Width: | Height: | Size: 15 KiB After Width: | Height: | Size: 17 KiB |
Reference in New Issue
Block a user