Code cleanup

added JavaDocs
removed unnecessary lines
This commit is contained in:
Timo Brennförder
2024-10-13 01:59:46 +02:00
parent febdd63422
commit 8c45784246
131 changed files with 2291 additions and 1548 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -12,6 +12,9 @@
import java.lang.System.Logger.Level;
import java.util.prefs.Preferences;
/**
* Class to play Background music in game
*/
public class BackgroundMusic implements GameEventListener {
private static final String VOLUME_PREF = "volume";
private static final String MUSIC_ENABLED_PREF = "musicEnabled";
@@ -32,7 +35,7 @@ public class BackgroundMusic implements GameEventListener {
private Application app;
/**
* Initializes and controls the BackgroundMusic
* Constructor for BackgroundMusic class
*
* @param app The main Application
*/
@@ -50,13 +53,14 @@ public BackgroundMusic(Application app) {
stop(loseMusic);
lastPlayedMusic = menuMusic.getName();
if(musicEnabled) {
if (musicEnabled) {
play(menuMusic);
}
}
/**
* creates an audio node for the music
* Creates an audio node for the music
*
* @param musicFilePath the file path to the music
* @return the created audio node
*/
@@ -71,7 +75,8 @@ private AudioNode createMusicNode(String musicFilePath) {
}
/**
* starts the music
* Starts the music
*
* @param audioNode the audio node to be played
*/
public void play(AudioNode audioNode) {
@@ -82,45 +87,47 @@ public void play(AudioNode audioNode) {
}
/**
* pauses the music
* Pauses the music
*
* @param audioNode the audio node to be paused
*/
public void pause(AudioNode audioNode){
if(audioNode.getStatus() == Status.Playing){
public void pause(AudioNode audioNode) {
if (audioNode.getStatus() == Status.Playing) {
audioNode.pause();
}
}
/**
* stops the music
* Stops the music
*
* @param audioNode the audio node to be stopped
*/
public void stop(AudioNode audioNode){
public void stop(AudioNode audioNode) {
if (audioNode.getStatus() == Status.Playing) {
audioNode.stop();
}
}
/**
* Toggle Method to control the music
* Controls which music should be played
*/
public void toggleMusic() {
this.musicEnabled = !this.musicEnabled;
if (musicEnabled) {
switch (lastPlayedMusic){
case MENU_MUSIC:
play(menuMusic);
break;
case GAME_MUSIC:
play(gameMusic);
break;
case VICTORY_MUSIC:
play(victoryMusic);
break;
case LOSE_MUSIC:
play(loseMusic);
break;
}
switch (lastPlayedMusic) {
case MENU_MUSIC:
play(menuMusic);
break;
case GAME_MUSIC:
play(gameMusic);
break;
case VICTORY_MUSIC:
play(victoryMusic);
break;
case LOSE_MUSIC:
play(loseMusic);
break;
}
}
else {
pause(menuMusic);
@@ -133,33 +140,36 @@ public void toggleMusic() {
}
/**
* changes the music to the specified music if it isn't already playing
* Changes the music to the specified music if it isn't already playing
*
* @param music the music to play
*/
public void changeMusic(Music music) {
if(music == Music.MENU_THEME && !lastPlayedMusic.equals(MENU_MUSIC)) {
if (music == Music.MENU_THEME && !lastPlayedMusic.equals(MENU_MUSIC)) {
LOGGER.log(Level.DEBUG, "Received Music change Event {0}", music.toString());
stop(gameMusic);
stop(victoryMusic);
stop(loseMusic);
play(menuMusic);
lastPlayedMusic = menuMusic.getName();
} else if (music == Music.GAME_THEME && !lastPlayedMusic.equals(GAME_MUSIC)) {
}
else if (music == Music.GAME_THEME && !lastPlayedMusic.equals(GAME_MUSIC)) {
LOGGER.log(Level.DEBUG, "Received Music change Event {0}", music.toString());
stop(menuMusic);
stop(loseMusic);
stop(victoryMusic);
play(gameMusic);
lastPlayedMusic = gameMusic.getName();
} else if (music == Music.VICTORY_THEME && !lastPlayedMusic.equals(VICTORY_MUSIC)) {
}
else if (music == Music.VICTORY_THEME && !lastPlayedMusic.equals(VICTORY_MUSIC)) {
LOGGER.log(Level.DEBUG, "Received Music change Event {0}", music.toString());
stop(menuMusic);
stop(gameMusic);
stop(loseMusic);
play(victoryMusic);
lastPlayedMusic = victoryMusic.getName();
} else if (music == Music.LOSE_THEME && !lastPlayedMusic.equals(LOSE_MUSIC)){
}
else if (music == Music.LOSE_THEME && !lastPlayedMusic.equals(LOSE_MUSIC)) {
LOGGER.log(Level.DEBUG, "Received Music change Event {0}", music.toString());
stop(menuMusic);
stop(gameMusic);
@@ -170,14 +180,14 @@ public void changeMusic(Music music) {
}
/**
* the method which receives the Event
* Receives the different MusicEvents
*
* @param music the received Event
*/
@Override
public void receivedEvent (MusicEvent music){
public void receivedEvent(MusicEvent music) {
LOGGER.log(Level.DEBUG, "Received Music change Event {0}", music.toString());
switch (music.music()){
switch (music.music()) {
case MENU_THEME:
changeMusic(Music.MENU_THEME);
break;
@@ -193,9 +203,8 @@ public void receivedEvent (MusicEvent music){
}
}
/**
* Method to set the volume for the music
* Set the volume for the music
*
* @param volume float to transfer the new volume
*/
@@ -210,7 +219,7 @@ public void setVolume(float volume) {
}
/**
* This method returns the volume
* Returns the volume
*
* @return the current volume as a float
*/

View File

@@ -1,10 +1,3 @@
////////////////////////////////////////
// Programming project code
// UniBw M, 2022, 2023, 2024
// www.unibw.de/inf2
// (c) Mark Minas (mark.minas@unibw.de)
////////////////////////////////////////
package pp.battleship.client;
import com.jme3.app.DebugKeysAppState;
@@ -232,7 +225,6 @@ public void simpleInitApp() {
serverConnection.connect();
backgroundMusic = new BackgroundMusic(this);
logic.addListener(backgroundMusic);
}
/**
@@ -323,7 +315,7 @@ public Draw getDraw() {
return draw;
}
public BackgroundMusic getBackgroundMusic(){
public BackgroundMusic getBackgroundMusic() {
return backgroundMusic;
}

View File

@@ -1,10 +1,3 @@
////////////////////////////////////////
// Programming project code
// UniBw M, 2022, 2023, 2024
// www.unibw.de/inf2
// (c) Mark Minas (mark.minas@unibw.de)
////////////////////////////////////////
package pp.battleship.client;
import com.jme3.math.ColorRGBA;

View File

@@ -1,10 +1,3 @@
////////////////////////////////////////
// Programming project code
// UniBw M, 2022, 2023, 2024
// www.unibw.de/inf2
// (c) Mark Minas (mark.minas@unibw.de)
////////////////////////////////////////
package pp.battleship.client;
import com.jme3.app.Application;

View File

@@ -1,10 +1,3 @@
////////////////////////////////////////
// Programming project code
// UniBw M, 2022, 2023, 2024
// www.unibw.de/inf2
// (c) Mark Minas (mark.minas@unibw.de)
////////////////////////////////////////
package pp.battleship.client;
import com.jme3.app.Application;
@@ -135,6 +128,11 @@ public void shipDestroyed() {
shipDestroyedSound.playInstance();
}
/**
* Plays sound according to the received SoundEvent
*
* @param event the received SoundEvent
*/
@Override
public void receivedEvent(SoundEvent event) {
switch (event.sound()) {

View File

@@ -1,10 +1,3 @@
////////////////////////////////////////
// Programming project code
// UniBw M, 2022, 2023, 2024
// www.unibw.de/inf2
// (c) Mark Minas (mark.minas@unibw.de)
////////////////////////////////////////
package pp.battleship.client;
import com.simsilica.lemur.Button;
@@ -56,13 +49,12 @@ public Menu(BattleshipApp app) {
addChild(new Label(lookup("menu.music.volume"), new ElementId("slider_label")));
Slider volumeSlider = new Slider();
volumeSlider.setModel(new DefaultRangedValueModel(0.00 , 1.00, app.getBackgroundMusic().getVolume()));
volumeSlider.setModel(new DefaultRangedValueModel(0.00, 1.00, app.getBackgroundMusic().getVolume()));
volumeSlider.setDelta(0.05);
addChild(volumeSlider);
volumeRef = volumeSlider.getModel().createReference();
addChild(loadButton)
.addClickCommands(s -> ifTopDialog(this::loadDialog));
addChild(saveButton)
@@ -76,19 +68,20 @@ public Menu(BattleshipApp app) {
}
/**
* this method is used update the volume when the slider is used
* Updates the volume when the slider is moved
*
* @param tpf time per frame
*/
@Override
public void update(float tpf){
if(volumeRef.update()){
public void update(float tpf) {
if (volumeRef.update()) {
double newVolume = volumeRef.get();
adjustVolume(newVolume);
}
}
/**
* this method adjust the volume for the background music
* Adjusts the volume for the background music
*
* @param volume is the double value of the volume
*/
@@ -97,13 +90,12 @@ private void adjustVolume(double volume) {
}
/**
* this method toggles the background music on and off
* Toggles the background music on and off
*/
private void toggleMusic() {
app.getBackgroundMusic().toggleMusic();
}
/**
* Updates the state of the load and save buttons based on the game logic.
*/

View File

@@ -1,10 +1,3 @@
////////////////////////////////////////
// Programming project code
// UniBw M, 2022, 2023, 2024
// www.unibw.de/inf2
// (c) Mark Minas (mark.minas@unibw.de)
////////////////////////////////////////
package pp.battleship.client;
import com.simsilica.lemur.Checkbox;
@@ -55,7 +48,7 @@ class NetworkDialog extends SimpleDialog {
Checkbox hostServer = new Checkbox(lookup("start.own.server"));
hostServer.setChecked(false);
hostServer.addClickCommands(s->toggleOwnServer());
hostServer.addClickCommands(s -> toggleOwnServer());
final BattleshipApp app = network.getApp();
final Container input = new Container(new SpringGridLayout());
@@ -164,38 +157,40 @@ private void failure(Throwable e) {
* If hostServer-Checkbox is active, starts a new Server on the clients machine, else tries to connect to existing server
*/
private void connect() {
if(hostServer){
if (hostServer) {
startServer();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
catch (InterruptedException e) {
LOGGER.log(Level.WARNING, e.getMessage(), e);
}
connectToServer();
} else {
}
else {
connectToServer();
}
}
/**
* starts a server on the clients machine
* Starts a server on the clients machine
*/
private void startServer() {
new Thread(() -> {
try{
try {
BattleshipServer battleshipServer = new BattleshipServer(Integer.parseInt(port.getText()));
battleshipServer.run();
} catch (Exception e) {
LOGGER.log(Level.ERROR,e);
}
catch (Exception e) {
LOGGER.log(Level.ERROR, e);
}
}).start();
}
/**
* handles the action for the hostServer-Checkbox
* Handles the action for the hostServer-Checkbox
*/
private void toggleOwnServer(){
private void toggleOwnServer() {
hostServer = !hostServer;
}
}

View File

@@ -1,10 +1,3 @@
////////////////////////////////////////
// Programming project code
// UniBw M, 2022, 2023, 2024
// www.unibw.de/inf2
// (c) Mark Minas (mark.minas@unibw.de)
////////////////////////////////////////
package pp.battleship.client;
import com.jme3.network.Client;

View File

@@ -1,9 +1,4 @@
////////////////////////////////////////
// Programming project code
// UniBw M, 2022, 2023, 2024
// www.unibw.de/inf2
// (c) Mark Minas (mark.minas@unibw.de)
////////////////////////////////////////
package pp.battleship.client.gui;

View File

@@ -1,9 +1,4 @@
////////////////////////////////////////
// Programming project code
// UniBw M, 2022, 2023, 2024
// www.unibw.de/inf2
// (c) Mark Minas (mark.minas@unibw.de)
////////////////////////////////////////
package pp.battleship.client.gui;

View File

@@ -14,25 +14,26 @@
import java.lang.System.Logger;
import java.lang.System.Logger.Level;
public class HitEffectHandler {
private final AssetManager assetManager;
private static final Logger LOGGER = System.getLogger(HitEffectHandler.class.getName());
/**
* Constructor for the HitEffectHandler class
*
* @param app the main application
*/
public HitEffectHandler(Application app){
public HitEffectHandler(Application app) {
assetManager = app.getAssetManager();
}
/**
* Creates an explosion effect when ship gets hit
* Creates explosion, debris and fire effects when ship gets hit
*
* @param battleshipNode the node of the ship that gets hit and the effect should be attached to
* @param shot The shot taken on a field
* @param shot The shot taken on a field
*/
public void hitEffect(Node battleshipNode, Shot shot){
public void hitEffect(Node battleshipNode, Shot shot) {
//Explosion
ParticleEmitter explosion = new ParticleEmitter("Explosion", Type.Triangle, 30);
explosion.setMaterial(new Material(assetManager, "Common/MatDefs/Misc/Particle.j3md"));
@@ -47,10 +48,10 @@ public void hitEffect(Node battleshipNode, Shot shot){
explosion.setLowLife(1f);
explosion.setHighLife(3.5f);
explosion.setParticlesPerSec(0);
explosion.setLocalTranslation(shot.getY() + 0.5f, 0 , shot.getX() + 0.5f);
explosion.setLocalTranslation(shot.getY() + 0.5f, 0, shot.getX() + 0.5f);
explosion.emitAllParticles();
//Debris
//Debris
ParticleEmitter debris = new ParticleEmitter("Debris", Type.Triangle, 6);
Material debrisMaterial = new Material(assetManager, "Common/MatDefs/Misc/Particle.j3md");
debrisMaterial.setTexture("Texture", assetManager.loadTexture("Textures/Debris/debris.png"));
@@ -67,7 +68,7 @@ public void hitEffect(Node battleshipNode, Shot shot){
debris.setLowLife(1f);
debris.setHighLife(3.5f);
debris.setParticlesPerSec(0);
debris.setLocalTranslation(shot.getY() + 0.5f, 0 , shot.getX() + 0.5f);
debris.setLocalTranslation(shot.getY() + 0.5f, 0, shot.getX() + 0.5f);
debris.emitAllParticles();
//Fire
@@ -92,8 +93,7 @@ public void hitEffect(Node battleshipNode, Shot shot){
// LOGGER.log(Level.DEBUG, "Created HitEffect at {0}", explosion.getLocalTranslation().toString());
// LOGGER.log(Level.DEBUG, "Created HitEffect at {0}", debris.getLocalTranslation().toString());
LOGGER.log(Level.INFO, "Created HitEffect at {0}", fire.getLocalTranslation().toString());
// LOGGER.log(Level.INFO, "Created HitEffect at {0}", fire.getLocalTranslation().toString());
battleshipNode.attachChild(explosion);
explosion.addControl(new EffectControl(explosion, battleshipNode));
@@ -103,10 +103,11 @@ public void hitEffect(Node battleshipNode, Shot shot){
}
/**
* Creates a splash effect if shot hits the water
* Creates a splash effect if the shot hits the water
*
* @param shot The shot taken on a field
*/
public ParticleEmitter missEffect(Shot shot){
public ParticleEmitter missEffect(Shot shot) {
ParticleEmitter missEffect = new ParticleEmitter("HitEffect", Type.Triangle, 45);
missEffect.setMaterial(new Material(assetManager, "Common/MatDefs/Misc/Particle.j3md"));
missEffect.setImagesX(2);
@@ -120,7 +121,7 @@ public ParticleEmitter missEffect(Shot shot){
missEffect.setLowLife(0.7f);
missEffect.setHighLife(1.8f);
missEffect.setParticlesPerSec(0);
missEffect.setLocalTranslation(shot.getY() + 0.5f, 0 , shot.getX() + 0.5f);
missEffect.setLocalTranslation(shot.getY() + 0.5f, 0, shot.getX() + 0.5f);
LOGGER.log(Level.DEBUG, "Created MissEffect at {0}", missEffect.getLocalTranslation().toString());
@@ -139,7 +140,7 @@ private static class EffectControl extends AbstractControl {
/**
* Constructor used to attach effect to a node
*
* @param emitter the particle emitter to be controlled
* @param emitter the particle emitter to be controlled
* @param parentNode the node to be attached
*/
public EffectControl(ParticleEmitter emitter, Node parentNode) {
@@ -152,7 +153,7 @@ public EffectControl(ParticleEmitter emitter, Node parentNode) {
*
* @param emitter the particle emitter to be controlled
*/
public EffectControl(ParticleEmitter emitter){
public EffectControl(ParticleEmitter emitter) {
this.emitter = emitter;
this.parentNode = null;
}
@@ -177,5 +178,4 @@ protected void controlUpdate(float tpf) {
@Override
protected void controlRender(com.jme3.renderer.RenderManager rm, com.jme3.renderer.ViewPort vp) {}
}
}

View File

@@ -1,9 +1,4 @@
////////////////////////////////////////
// Programming project code
// UniBw M, 2022, 2023, 2024
// www.unibw.de/inf2
// (c) Mark Minas (mark.minas@unibw.de)
////////////////////////////////////////
package pp.battleship.client.gui;

View File

@@ -1,9 +1,4 @@
////////////////////////////////////////
// Programming project code
// UniBw M, 2022, 2023, 2024
// www.unibw.de/inf2
// (c) Mark Minas (mark.minas@unibw.de)
////////////////////////////////////////
package pp.battleship.client.gui;
@@ -117,6 +112,12 @@ public Spatial visit(Battleship ship) {
return shipNode;
}
/**
* Creates a visual representation on the map
*
* @param shell the Shell element to visit
* @return the node the visual representation gets attached to.
*/
@Override
public Spatial visit(Shell shell) {
LOGGER.log(Logger.Level.DEBUG, "Visiting {0}", shell);

View File

@@ -1,9 +1,4 @@
////////////////////////////////////////
// Programming project code
// UniBw M, 2022, 2023, 2024
// www.unibw.de/inf2
// (c) Mark Minas (mark.minas@unibw.de)
////////////////////////////////////////
package pp.battleship.client.gui;

View File

@@ -1,9 +1,4 @@
////////////////////////////////////////
// Programming project code
// UniBw M, 2022, 2023, 2024
// www.unibw.de/inf2
// (c) Mark Minas (mark.minas@unibw.de)
////////////////////////////////////////
package pp.battleship.client.gui;
@@ -51,7 +46,7 @@ class SeaSynchronizer extends ShipMapSynchronizer {
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);
private HitEffectHandler hitEffectHandler;
private final HitEffectHandler hitEffectHandler;
private final ShipMap map;
private final BattleshipApp app;
@@ -146,8 +141,8 @@ public Spatial visit(Battleship ship) {
/**
* Visits a Shell and creates a graphical representation of it.
*
* @param shell the battleship to be represented
* @return the node containing the graphical representation of the battleship
* @param shell the Shell to be represented
* @return the node containing the graphical representation of the Shell
*/
@Override
public Spatial visit(Shell shell) {
@@ -156,12 +151,11 @@ public Spatial visit(Shell shell) {
final float x = shell.getY();
final float z = shell.getX();
node.setLocalTranslation(x+0.5f, 10f, z+0.5f);
node.setLocalTranslation(x + 0.5f, 10f, z + 0.5f);
node.addControl(new ShellControl(shell, app));
return node;
}
/**
* 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.
@@ -170,7 +164,7 @@ public Spatial visit(Shell shell) {
* @return the spatial representing the battleship
*/
private Spatial createShip(Battleship ship) {
return switch (ship.getLength()){
return switch (ship.getLength()) {
case 1 -> createPatrolBoat(ship);
case 2 -> createModernBattleship(ship);
case 3 -> createUboat(ship);
@@ -250,7 +244,7 @@ private Spatial createModernBattleship(Battleship ship) {
model.rotate(-HALF_PI, calculateRotationAngle(ship.getRot()) + HALF_PI, 0f);
model.scale(0.000075f);
model.setShadowMode(ShadowMode.CastAndReceive);
model.move(0,0.2f,0);
model.move(0, 0.2f, 0);
return model;
}
@@ -269,15 +263,13 @@ private Spatial createMissile() {
model.setQueueBucket(RenderQueue.Bucket.Opaque);
model.rotate(-HALF_PI,0,0);
model.rotate(-HALF_PI, 0, 0);
model.scale(0.009f);
model.setShadowMode(ShadowMode.CastAndReceive);
model.move(0,0f,0);
model.move(0, 0f, 0);
return model;
}
/**
* Creates a detailed 3D model to represent a Uboat.
*
@@ -291,7 +283,7 @@ private Spatial createUboat(Battleship ship) {
model.rotate(-HALF_PI, calculateRotationAngle(ship.getRot()), 0f);
model.scale(0.45f);
model.setShadowMode(ShadowMode.CastAndReceive);
model.move(0,-0.25f,0);
model.move(0, -0.25f, 0);
return model;
}
@@ -313,7 +305,6 @@ private Spatial createPatrolBoat(Battleship ship) {
return model;
}
/**
* Calculates the rotation angle for the specified rotation.
*

View File

@@ -11,6 +11,9 @@
import java.lang.System.Logger;
import java.lang.System.Logger.Level;
/**
* Class to control the 3D representation of a shell
*/
public class ShellControl extends AbstractControl {
private final Shell shell;
@@ -18,24 +21,42 @@ public class ShellControl extends AbstractControl {
private static final float TRAVEL_SPEED = 8.5f;
static final Logger LOGGER = System.getLogger(BattleshipApp.class.getName());
/**
* Constructor for ShellControl class
*
* @param shell The Shell to be displayed
* @param app the main application
*/
public ShellControl(Shell shell, BattleshipApp app) {
this.shell = shell;
this.app = app;
}
/**
* Method to control movement of the Shell and remove it when target is reached
*
* @param tpf time per frame (in seconds)
*/
@Override
public void controlUpdate(float tpf) {
//LOGGER.log(Level.DEBUG, "missile at x=" + shell.getX() + ", y=" + shell.getY());
spatial.move(0, -TRAVEL_SPEED*tpf, 0);
if(spatial.getLocalTranslation().getY() <= 0.2){
spatial.move(0, -TRAVEL_SPEED * tpf, 0);
if (spatial.getLocalTranslation().getY() <= 0.2) {
spatial.getParent().detachChild(spatial);
app.getGameLogic().send(new EndAnimationMessage(new IntPoint(shell.getX(), shell.getY())));
}
}
/**
* This method is called during the rendering phase, but it does not perform any
* operations in this implementation as the control only influences the spatial's
* transformation, not its rendering process.
*
* @param rm the RenderManager rendering the controlled Spatial (not null)
* @param vp the ViewPort being rendered (not null)
*/
@Override
protected void controlRender(RenderManager rm, ViewPort vp) {
}
}

View File

@@ -9,29 +9,52 @@
import pp.battleship.model.IntPoint;
import pp.util.Position;
/**
* Class to control the 2D representation of a shell
*/
public class ShellMapControl extends AbstractControl {
private final Position position;
private final IntPoint pos;
private static final Vector3f vector = new Vector3f();
private static final Vector3f VECTOR_3_F = new Vector3f();
private final BattleshipApp app;
/**
* Constructor for ShellMapControl
*
* @param position the target position of the shell
* @param app the main application
* @param pos the position the then to render shot goes to
*/
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));
VECTOR_3_F.set(new Vector3f(position.getX(), position.getY(), 0));
}
/**
* Method to control movement of the Shell on the map and remove it when target is reached
*
* @param tpf time per frame (in seconds)
*/
@Override
protected void controlUpdate(float tpf) {
spatial.move(vector.mult(tpf));
spatial.move(VECTOR_3_F.mult(tpf));
if (spatial.getLocalTranslation().getX() >= position.getX() && spatial.getLocalTranslation().getY() >= position.getY()) {
spatial.getParent().detachChild(spatial);
app.getGameLogic().send(new EndAnimationMessage(pos));
}
}
/**
* This method is called during the rendering phase, but it does not perform any
* operations in this implementation as the control only influences the spatial's
* transformation, not its rendering process.
*
* @param rm the RenderManager rendering the controlled Spatial (not null)
* @param vp the ViewPort being rendered (not null)
*/
@Override
protected void controlRender(RenderManager rm, ViewPort vp) {

View File

@@ -1,9 +1,4 @@
////////////////////////////////////////
// Programming project code
// UniBw M, 2022, 2023, 2024
// www.unibw.de/inf2
// (c) Mark Minas (mark.minas@unibw.de)
////////////////////////////////////////
package pp.battleship.client.gui;
@@ -88,12 +83,12 @@ protected void controlUpdate(float tpf) {
// If spatial is null, do nothing
if (spatial == null) return;
if(battleship.isDestroyed() && spatial.getLocalTranslation().getY() < -0.6f){
if (battleship.isDestroyed() && spatial.getLocalTranslation().getY() < -0.6f) {
LOGGER.log(Level.INFO, "Ship removed {0}", spatial.getName());
spatial.getParent().detachChild(spatial);
}
else if (battleship.isDestroyed()) {
spatial.move(0,-0.2f*tpf,0);
spatial.move(0, -0.2f * tpf, 0);
}
else {
// Update the time within the oscillation cycle

View File

@@ -1,9 +1,4 @@
////////////////////////////////////////
// Programming project code
// UniBw M, 2022, 2023, 2024
// www.unibw.de/inf2
// (c) Mark Minas (mark.minas@unibw.de)
////////////////////////////////////////
package pp.battleship.client.gui;

View File

@@ -1,9 +1,4 @@
////////////////////////////////////////
// Programming project code
// UniBw M, 2022, 2023, 2024
// www.unibw.de/inf2
// (c) Mark Minas (mark.minas@unibw.de)
////////////////////////////////////////
package server;
@@ -77,12 +72,18 @@ public BattleshipServer(int port) {
logic = new ServerGameLogic(this, config);
}
/**
* Runs a server
*/
public void run() {
startServer();
while (true)
processNextMessage();
}
/**
* Starts a server
*/
private void startServer() {
try {
LOGGER.log(Level.INFO, "Starting server..."); //NON-NLS
@@ -98,6 +99,9 @@ private void startServer() {
}
}
/**
* Processes next received message
*/
private void processNextMessage() {
try {
pendingMessages.take().process(logic);
@@ -108,6 +112,9 @@ private void processNextMessage() {
}
}
/**
* Registers all serializable classes
*/
private void initializeSerializables() {
Serializer.registerClass(GameDetails.class);
Serializer.registerClass(StartBattleMessage.class);
@@ -122,6 +129,9 @@ private void initializeSerializables() {
Serializer.registerClass(SwitchToBattleState.class);
}
/**
* Registers all listeners
*/
private void registerListeners() {
myServer.addMessageListener(this, MapMessage.class);
myServer.addMessageListener(this, ShootMessage.class);
@@ -129,6 +139,11 @@ private void registerListeners() {
myServer.addConnectionListener(this);
}
/**
* Handles a received message
* @param source the connection the message comes from
* @param message the received message
*/
@Override
public void messageReceived(HostedConnection source, Message message) {
LOGGER.log(Level.INFO, "message received from {0}: {1}", source.getId(), message); //NON-NLS
@@ -136,12 +151,22 @@ public void messageReceived(HostedConnection source, Message message) {
pendingMessages.add(new ReceivedMessage(clientMessage, source.getId()));
}
/**
* Adds a new connection to a server
* @param server the server to add the connection to
* @param hostedConnection the connection to be added
*/
@Override
public void connectionAdded(Server server, HostedConnection hostedConnection) {
LOGGER.log(Level.INFO, "new connection {0}", hostedConnection); //NON-NLS
logic.addPlayer(hostedConnection.getId());
}
/**
* Removes a standing connection from a server
* @param server the server to add the connection to
* @param hostedConnection the connection to be added
*/
@Override
public void connectionRemoved(Server server, HostedConnection hostedConnection) {
LOGGER.log(Level.INFO, "connection closed: {0}", hostedConnection); //NON-NLS
@@ -154,6 +179,10 @@ public void connectionRemoved(Server server, HostedConnection hostedConnection)
}
}
/**
* Shuts down the server and terminates the application with the given exit code
* @param exitValue the exit status code
*/
private void exit(int exitValue) { //NON-NLS
LOGGER.log(Level.INFO, "close request"); //NON-NLS
if (myServer != null)

View File

@@ -1,15 +1,15 @@
////////////////////////////////////////
// Programming project code
// UniBw M, 2022, 2023, 2024
// www.unibw.de/inf2
// (c) Mark Minas (mark.minas@unibw.de)
////////////////////////////////////////
package server;
import pp.battleship.message.client.ClientInterpreter;
import pp.battleship.message.client.ClientMessage;
/**
* Represents a message received from a client
* @param message the received message
* @param from the ID of the client that sent the message
*/
record ReceivedMessage(ClientMessage message, int from) {
void process(ClientInterpreter interpreter) {
message.accept(interpreter, from);

View File

@@ -2,4 +2,3 @@ Epic Cinematic Trailer | ELITE by Alex-Productions | https://onsound.eu/
Music promoted by https://www.chosic.com/free-music/all/
Creative Commons CC BY 3.0
https://creativecommons.org/licenses/by/3.0/

View File

@@ -1,9 +1,4 @@
////////////////////////////////////////
// Programming project code
// UniBw M, 2022, 2023, 2024
// www.unibw.de/inf2
// (c) Mark Minas (mark.minas@unibw.de)
////////////////////////////////////////
package pp.battleship.exporter;

View File

@@ -1,10 +1,3 @@
////////////////////////////////////////
// Programming project code
// UniBw M, 2022, 2023, 2024
// www.unibw.de/inf2
// (c) Mark Minas (mark.minas@unibw.de)
////////////////////////////////////////
package pp.battleship;
import pp.util.config.Config;

View File

@@ -1,10 +1,3 @@
////////////////////////////////////////
// Programming project code
// UniBw M, 2022, 2023, 2024
// www.unibw.de/inf2
// (c) Mark Minas (mark.minas@unibw.de)
////////////////////////////////////////
package pp.battleship;
import java.util.ResourceBundle;

View File

@@ -11,15 +11,25 @@
import java.lang.System.Logger.Level;
public class AnimationState extends ClientState{
/**
* Represents the state in which the animation is played
*/
public class AnimationState extends ClientState {
private boolean myTurn;
/**
* Constructor for the AnimationState class
*
* @param logic the client logic
* @param turn a boolean containing if it's the client's turn
* @param position the position a Shell gets created
*/
public AnimationState(ClientGameLogic logic, boolean turn, IntPoint position) {
super(logic);
logic.playMusic(Music.GAME_THEME);
myTurn = turn;
if (myTurn){
if (myTurn) {
logic.getOpponentMap().add(new Shell(position));
}
else {
@@ -27,8 +37,13 @@ public AnimationState(ClientGameLogic logic, boolean turn, IntPoint position) {
}
}
/**
* Makes sure the client renders the correct view
*
* @return true
*/
@Override
boolean showBattle(){
boolean showBattle() {
return true;
}
@@ -52,8 +67,13 @@ public void receivedEffect(EffectMessage msg) {
}
}
/**
* Sets the client back to the battle state
*
* @param msg the received SwitchToBattleState message
*/
@Override
public void receivedSwitchToBattleState(SwitchToBattleState msg){
public void receivedSwitchToBattleState(SwitchToBattleState msg) {
logic.setState(new BattleState(logic, msg.getTurn()));
}
@@ -89,6 +109,7 @@ private void playSound(EffectMessage msg) {
else if (msg.getDestroyedShip() == null)
logic.playSound(Sound.EXPLOSION);
else
logic.playSound(Sound.DESTROYED_SHIP);
logic.playSound(Sound.EXPLOSION);
logic.playSound(Sound.DESTROYED_SHIP);
}
}

View File

@@ -1,9 +1,4 @@
////////////////////////////////////////
// Programming project code
// UniBw M, 2022, 2023, 2024
// www.unibw.de/inf2
// (c) Mark Minas (mark.minas@unibw.de)
////////////////////////////////////////
package pp.battleship.game.client;
@@ -35,11 +30,21 @@ public BattleState(ClientGameLogic logic, boolean myTurn) {
this.myTurn = myTurn;
}
/**
* Makes sure the client renders the correct view
*
* @return true
*/
@Override
public boolean showBattle() {
return true;
}
/**
* Triggers a shoot event if it's client's turn
*
* @param pos the position where the click occurred
*/
@Override
public void clickOpponentMap(IntPoint pos) {
if (!myTurn)
@@ -48,9 +53,14 @@ else if (logic.getOpponentMap().isValid(pos))
logic.send(new ShootMessage(pos));
}
/**
* Triggers an animation if StartAnimationMessage is received
*
* @param msg the received Startanimation message
*/
@Override
public void receivedStartAnimation(StartAnimationMessage msg){
logic.setState(new AnimationState(logic, msg.isMyTurn(), msg.getPosition() ));
public void receivedStartAnimation(StartAnimationMessage msg) {
logic.setState(new AnimationState(logic, msg.isMyTurn(), msg.getPosition()));
logic.playSound(Sound.MISSILE_LAUNCH);
}
}

View File

@@ -1,9 +1,4 @@
////////////////////////////////////////
// Programming project code
// UniBw M, 2022, 2023, 2024
// www.unibw.de/inf2
// (c) Mark Minas (mark.minas@unibw.de)
////////////////////////////////////////
package pp.battleship.game.client;

View File

@@ -1,9 +1,4 @@
////////////////////////////////////////
// Programming project code
// UniBw M, 2022, 2023, 2024
// www.unibw.de/inf2
// (c) Mark Minas (mark.minas@unibw.de)
////////////////////////////////////////
package pp.battleship.game.client;
@@ -231,6 +226,8 @@ public void received(EffectMessage msg) {
}
/**
* Reports that client should play an animation
*
* @param msg
*/
@Override
@@ -239,6 +236,8 @@ public void received(StartAnimationMessage msg) {
}
/**
* Reports that client should switch to the battle state
*
* @param msg
*/
@Override
@@ -375,9 +374,10 @@ public void update(float delta) {
/**
* Triggers an event to play specified music
*
* @param music the music to be played
*/
public void playMusic(Music music){
public void playMusic(Music music) {
notifyListeners(new MusicEvent(music));
}
}

View File

@@ -1,9 +1,4 @@
////////////////////////////////////////
// Programming project code
// UniBw M, 2022, 2023, 2024
// www.unibw.de/inf2
// (c) Mark Minas (mark.minas@unibw.de)
////////////////////////////////////////
package pp.battleship.game.client;

View File

@@ -1,9 +1,4 @@
////////////////////////////////////////
// Programming project code
// UniBw M, 2022, 2023, 2024
// www.unibw.de/inf2
// (c) Mark Minas (mark.minas@unibw.de)
////////////////////////////////////////
package pp.battleship.game.client;
@@ -167,10 +162,20 @@ void receivedEffect(EffectMessage msg) {
ClientGameLogic.LOGGER.log(Level.ERROR, "receivedEffect not allowed in {0}", getName()); //NON-NLS
}
/**
* Reports that client should switch to battle state
*
* @param msg the received SwitchToBattleState message
*/
void receivedSwitchToBattleState(SwitchToBattleState msg) {
ClientGameLogic.LOGGER.log(Level.ERROR, "receivedSwitchToBattleState not allowed in {0}", getName());
}
/**
* Reports that the client should start an animation
*
* @param msg the received StartAnimation message
*/
void receivedStartAnimation(StartAnimationMessage msg) {
ClientGameLogic.LOGGER.log(Level.ERROR, "receivedStartAnimation not allowed in {0}", getName());
}

View File

@@ -1,9 +1,4 @@
////////////////////////////////////////
// Programming project code
// UniBw M, 2022, 2023, 2024
// www.unibw.de/inf2
// (c) Mark Minas (mark.minas@unibw.de)
////////////////////////////////////////
package pp.battleship.game.client;
@@ -251,6 +246,12 @@ else if (!checkMapToLoad(dto)) {
selectedInHarbor = null;
}
/**
* Checks if the provided map meets the requirements
*
* @param dto the data transfer object to check
* @return boolean if the map meets the requirements
*/
private boolean checkMapToLoad(ShipMapDTO dto) {
int mapWidth = dto.getWidth();
int mapHeight = dto.getHeight();
@@ -266,7 +267,7 @@ private boolean checkMapToLoad(ShipMapDTO dto) {
// check if ships overlap
List<Battleship> ships = dto.getShips();
for(Battleship ship:ships) {
for (Battleship ship : ships) {
for (Battleship compareShip : ships) {
if (!(ship == compareShip)) {
if (ship.collidesWith(compareShip)) {

View File

@@ -1,9 +1,4 @@
////////////////////////////////////////
// Programming project code
// UniBw M, 2022, 2023, 2024
// www.unibw.de/inf2
// (c) Mark Minas (mark.minas@unibw.de)
////////////////////////////////////////
package pp.battleship.game.client;
@@ -20,7 +15,7 @@ class GameOverState extends ClientState {
*/
GameOverState(ClientGameLogic logic, boolean loser) {
super(logic);
if(loser){
if (loser) {
logic.playMusic(Music.LOSE_THEME);
}
else {

View File

@@ -1,9 +1,4 @@
////////////////////////////////////////
// Programming project code
// UniBw M, 2022, 2023, 2024
// www.unibw.de/inf2
// (c) Mark Minas (mark.minas@unibw.de)
////////////////////////////////////////
package pp.battleship.game.client;
@@ -58,6 +53,11 @@ private void fillHarbor(GameDetails details) {
}
}
/**
* Checks if map may be saved to file
*
* @return false
*/
@Override
public boolean maySaveMap() {
return false;

View File

@@ -1,9 +1,4 @@
////////////////////////////////////////
// Programming project code
// UniBw M, 2022, 2023, 2024
// www.unibw.de/inf2
// (c) Mark Minas (mark.minas@unibw.de)
////////////////////////////////////////
package pp.battleship.game.client;

View File

@@ -1,9 +1,4 @@
////////////////////////////////////////
// Programming project code
// UniBw M, 2022, 2023, 2024
// www.unibw.de/inf2
// (c) Mark Minas (mark.minas@unibw.de)
////////////////////////////////////////
package pp.battleship.game.client;
@@ -40,6 +35,11 @@ public void receivedStartBattle(StartBattleMessage msg) {
logic.setState(new BattleState(logic, msg.isMyTurn()));
}
/**
* Reverts the client back to the editor state if an invalid map is provided
*
* @param details the game details including map size and ships
*/
@Override
public void receivedGameDetails(GameDetails details) {
ClientGameLogic.LOGGER.log(Level.WARNING, "Invalid Map"); //NON-NLS

View File

@@ -1,9 +1,4 @@
////////////////////////////////////////
// Programming project code
// UniBw M, 2022, 2023, 2024
// www.unibw.de/inf2
// (c) Mark Minas (mark.minas@unibw.de)
////////////////////////////////////////
package pp.battleship.game.server;

View File

@@ -1,10 +1,3 @@
////////////////////////////////////////
// Programming project code
// UniBw M, 2022, 2023, 2024
// www.unibw.de/inf2
// (c) Mark Minas (mark.minas@unibw.de)
////////////////////////////////////////
package pp.battleship.game.server;
import pp.battleship.BattleshipConfig;
@@ -157,33 +150,32 @@ else if (!checkMap(msg, from)) {
}
/**
* @param msg
* Handles the reception of an EndAnimation message
* @param msg received EndAnimation message
*/
@Override
public void received(EndAnimationMessage msg, int from){
if(state != ServerState.WAIT_ANIMATION)
public void received(EndAnimationMessage msg, int from) {
if (state != ServerState.WAIT_ANIMATION)
LOGGER.log(Level.ERROR, "animation not allowed in {0}", state);
else
if(getPlayerById(from) == players.get(0)){
LOGGER.log(Level.DEBUG, "{0} set to true", getPlayerById(from));
p1AnimationFinished = true;
shoot(getPlayerById(from), msg.getPosition());
}
else if (getPlayerById(from) == players.get(1)){
LOGGER.log(Level.DEBUG, "{0} set to true {1}", getPlayerById(from), getPlayerById(from).toString());
p2AnimationFinished = true;
shoot(getPlayerById(from), msg.getPosition());
}
if(p1AnimationFinished && p2AnimationFinished) {
setState(ServerState.BATTLE);
for (Player player : players)
send(player, new SwitchToBattleState(player == activePlayer));
p1AnimationFinished = false;
p2AnimationFinished = false;
}
else if (getPlayerById(from) == players.get(0)) {
LOGGER.log(Level.DEBUG, "{0} set to true", getPlayerById(from));
p1AnimationFinished = true;
shoot(getPlayerById(from), msg.getPosition());
}
else if (getPlayerById(from) == players.get(1)) {
LOGGER.log(Level.DEBUG, "{0} set to true {1}", getPlayerById(from), getPlayerById(from).toString());
p2AnimationFinished = true;
shoot(getPlayerById(from), msg.getPosition());
}
if (p1AnimationFinished && p2AnimationFinished) {
setState(ServerState.BATTLE);
for (Player player : players)
send(player, new SwitchToBattleState(player == activePlayer));
p1AnimationFinished = false;
p2AnimationFinished = false;
}
}
/**
* Returns true if the map contains correct ship placement and is of the correct size
*
@@ -258,6 +250,11 @@ void playerReady(Player player, List<Battleship> ships) {
}
}
/**
* Handles what Effect should be triggered based on the shot
* @param player the player receiving the message
* @param position the position the shot hit
*/
void shoot(Player player, IntPoint position) {
final Battleship selectedShip;
selectedShip = getSelectedShip(player, position);
@@ -269,6 +266,12 @@ void shoot(Player player, IntPoint position) {
}
}
/**
* Returns the ship at a given position
* @param player the player whose map will be checked for a ship
* @param position the position to be checked for a ship
* @return if there is a ship at the given position, returns the ship, else null
*/
Battleship getSelectedShip(Player player, IntPoint position) {
if (player != activePlayer) {
return player.getMap().findShipAt(position);
@@ -278,6 +281,11 @@ Battleship getSelectedShip(Player player, IntPoint position) {
}
}
/**
* Sends a message to the client that the shot missed
* @param player the player receiving the message
* @param position the position at which the shot hit in the water
*/
void shotMissed(Player player, IntPoint position) {
if (player != activePlayer) {
send(player, EffectMessage.miss(false, position));
@@ -292,6 +300,12 @@ void shotMissed(Player player, IntPoint position) {
activePlayer = player;
}
/**
* Sends a message to the client that the shot missed
* @param player the player receiving the message
* @param position the position at which the shot hit in the ship
* @param ship the ship that has been hit
*/
void shotHit(Player player, IntPoint position, Battleship ship) {
ship.hit(position);
if (getOpponent(activePlayer).getMap().getRemainingShips().isEmpty()) {
@@ -319,5 +333,4 @@ else if (ship.isDestroyed()) {
}
}
}
}

View File

@@ -1,10 +1,3 @@
////////////////////////////////////////
// Programming project code
// UniBw M, 2022, 2023, 2024
// www.unibw.de/inf2
// (c) Mark Minas (mark.minas@unibw.de)
////////////////////////////////////////
package pp.battleship.game.server;
import pp.battleship.message.server.ServerMessage;

View File

@@ -1,9 +1,4 @@
////////////////////////////////////////
// Programming project code
// UniBw M, 2022, 2023, 2024
// www.unibw.de/inf2
// (c) Mark Minas (mark.minas@unibw.de)
////////////////////////////////////////
package pp.battleship.game.server;

View File

@@ -1,10 +1,3 @@
////////////////////////////////////////
// Programming project code
// UniBw M, 2022, 2023, 2024
// www.unibw.de/inf2
// (c) Mark Minas (mark.minas@unibw.de)
////////////////////////////////////////
package pp.battleship.game.singlemode;
import pp.battleship.BattleshipConfig;

View File

@@ -1,9 +1,4 @@
////////////////////////////////////////
// Programming project code
// UniBw M, 2022, 2023, 2024
// www.unibw.de/inf2
// (c) Mark Minas (mark.minas@unibw.de)
////////////////////////////////////////
package pp.battleship.game.singlemode;
@@ -65,8 +60,9 @@ public void received(MapMessage msg, int from) {
}
/**
* @param msg
* @param from
* Creates a copy of the provided EndAnimation message
* @param msg thr received EndAnimation message
* @param from the identifier of the sender
*/
@Override
public void received(EndAnimationMessage msg, int from) {

View File

@@ -1,10 +1,3 @@
////////////////////////////////////////
// Programming project code
// UniBw M, 2022, 2023, 2024
// www.unibw.de/inf2
// (c) Mark Minas (mark.minas@unibw.de)
////////////////////////////////////////
package pp.battleship.game.singlemode;
import pp.battleship.game.client.BattleshipClient;
@@ -21,6 +14,7 @@ class InterpreterProxy implements ServerInterpreter {
private final BattleshipClient playerClient;
static final System.Logger LOGGER = System.getLogger(InterpreterProxy.class.getName());
/**
* Constructs an InterpreterProxy with the specified BattleshipClient.
*
@@ -96,7 +90,7 @@ public void received(StartAnimationMessage msg) {
* @param msg the SwitchBattleState received from the server
*/
@Override
public void received(SwitchToBattleState msg){
public void received(SwitchToBattleState msg) {
LOGGER.log(System.Logger.Level.INFO, "Received SwitchBattleState");
forward(msg);
}

View File

@@ -137,7 +137,7 @@ public void received(StartAnimationMessage msg) {
* @param msg the SwitchBattleState received
*/
@Override
public void received(SwitchToBattleState msg){
public void received(SwitchToBattleState msg) {
LOGGER.log(Level.INFO, "Received SwitchBattleStateMessage: {0}", msg);
if (msg.getTurn())
shoot();

View File

@@ -1,9 +1,4 @@
////////////////////////////////////////
// Programming project code
// UniBw M, 2022, 2023, 2024
// www.unibw.de/inf2
// (c) Mark Minas (mark.minas@unibw.de)
////////////////////////////////////////
package pp.battleship.game.singlemode;

View File

@@ -1,10 +1,3 @@
////////////////////////////////////////
// Programming project code
// UniBw M, 2022, 2023, 2024
// www.unibw.de/inf2
// (c) Mark Minas (mark.minas@unibw.de)
////////////////////////////////////////
package pp.battleship.message.client;
/**
@@ -27,5 +20,10 @@ public interface ClientInterpreter {
*/
void received(MapMessage msg, int from);
/**
* Processes a received EndAnimation message
* @param msg the received EndAnimation message
* @param from the connection ID from which the message was received
*/
void received(EndAnimationMessage msg, int from);
}

View File

@@ -1,10 +1,3 @@
////////////////////////////////////////
// Programming project code
// UniBw M, 2022, 2023, 2024
// www.unibw.de/inf2
// (c) Mark Minas (mark.minas@unibw.de)
////////////////////////////////////////
package pp.battleship.message.client;
import com.jme3.network.AbstractMessage;

View File

@@ -3,14 +3,24 @@
import com.jme3.network.serializing.Serializable;
import pp.battleship.model.IntPoint;
/**
* A message sent by the client telling the server the animation is finished
*/
@Serializable
public class EndAnimationMessage extends ClientMessage{
public class EndAnimationMessage extends ClientMessage {
private IntPoint position;
private EndAnimationMessage(){/*do nothing */}
/**
* Default constructor for serialization purposes.
*/
private EndAnimationMessage() {/*do nothing */}
public EndAnimationMessage(final IntPoint position){
/**
* Constructs an EndAnimation message
* @param position the position to be effected
*/
public EndAnimationMessage(final IntPoint position) {
this.position = position;
}
@@ -25,9 +35,11 @@ public void accept(ClientInterpreter interpreter, int from) {
interpreter.received(this, from);
}
/**
* Getter for the position
* @return IntPoint position
*/
public IntPoint getPosition() {
return position;
}
}

View File

@@ -1,10 +1,3 @@
////////////////////////////////////////
// Programming project code
// UniBw M, 2022, 2023, 2024
// www.unibw.de/inf2
// (c) Mark Minas (mark.minas@unibw.de)
////////////////////////////////////////
package pp.battleship.message.client;
import com.jme3.network.serializing.Serializable;

View File

@@ -1,10 +1,3 @@
////////////////////////////////////////
// Programming project code
// UniBw M, 2022, 2023, 2024
// www.unibw.de/inf2
// (c) Mark Minas (mark.minas@unibw.de)
////////////////////////////////////////
package pp.battleship.message.client;
import com.jme3.network.serializing.Serializable;

View File

@@ -1,10 +1,3 @@
////////////////////////////////////////
// Programming project code
// UniBw M, 2022, 2023, 2024
// www.unibw.de/inf2
// (c) Mark Minas (mark.minas@unibw.de)
////////////////////////////////////////
package pp.battleship.message.server;
import com.jme3.network.serializing.Serializable;

View File

@@ -1,10 +1,3 @@
////////////////////////////////////////
// Programming project code
// UniBw M, 2022, 2023, 2024
// www.unibw.de/inf2
// (c) Mark Minas (mark.minas@unibw.de)
////////////////////////////////////////
package pp.battleship.message.server;
import com.jme3.network.serializing.Serializable;

View File

@@ -1,10 +1,3 @@
////////////////////////////////////////
// Programming project code
// UniBw M, 2022, 2023, 2024
// www.unibw.de/inf2
// (c) Mark Minas (mark.minas@unibw.de)
////////////////////////////////////////
package pp.battleship.message.server;
import pp.battleship.message.client.EndAnimationMessage;
@@ -36,7 +29,15 @@ public interface ServerInterpreter {
*/
void received(EffectMessage msg);
/**
* Handles a StartAnimation message received from the server
* @param msg the received StartAnimation message
*/
void received(StartAnimationMessage msg);
/**
* Handles a SwitchToBattleState message received from the server
* @param msg the received SwitchToBattleState message
*/
void received(SwitchToBattleState msg);
}

View File

@@ -1,10 +1,3 @@
////////////////////////////////////////
// Programming project code
// UniBw M, 2022, 2023, 2024
// www.unibw.de/inf2
// (c) Mark Minas (mark.minas@unibw.de)
////////////////////////////////////////
package pp.battleship.message.server;
import com.jme3.network.AbstractMessage;

View File

@@ -3,14 +3,25 @@
import com.jme3.network.serializing.Serializable;
import pp.battleship.model.IntPoint;
/**
* A message sent by the server to inform clients about the start of an animation
*/
@Serializable
public class StartAnimationMessage extends ServerMessage {
private IntPoint position;
private boolean myTurn;
private StartAnimationMessage(){/*do nothing */}
/**
* Default constructor for serialization purposes.
*/
private StartAnimationMessage() {/*do nothing */}
/**
* Constructs a StartAnimation message
* @param position the position a Shell will affect
* @param myTurn boolean if it's client's turn
*/
public StartAnimationMessage(IntPoint position, boolean myTurn) {
this.position = position;
this.myTurn = myTurn;
@@ -37,9 +48,15 @@ public String getInfoTextKey() {
return "started animation at " + position;
}
/**
* Getter for the position
* @return IntPoint position
*/
public IntPoint getPosition() {return position;}
/**
* Getter for myTurn
* @return boolean myTurn
*/
public boolean isMyTurn() {return myTurn;}
}

View File

@@ -1,10 +1,3 @@
////////////////////////////////////////
// Programming project code
// UniBw M, 2022, 2023, 2024
// www.unibw.de/inf2
// (c) Mark Minas (mark.minas@unibw.de)
////////////////////////////////////////
package pp.battleship.message.server;
import com.jme3.network.serializing.Serializable;

View File

@@ -2,14 +2,24 @@
import com.jme3.network.serializing.Serializable;
/**
* A message sent by the server to tell client to switch to battle state
*/
@Serializable
public class SwitchToBattleState extends ServerMessage{
public class SwitchToBattleState extends ServerMessage {
private boolean myTurn;
private SwitchToBattleState(){/*do nothing */}
/**
* Default constructor for serialization purposes.
*/
private SwitchToBattleState() {/*do nothing */}
public SwitchToBattleState(boolean turn){
/**
* Constructs a SwitchToBattleState message
* @param turn boolean it's client's turn
*/
public SwitchToBattleState(boolean turn) {
myTurn = turn;
}
@@ -34,7 +44,9 @@ public String getInfoTextKey() {
return "switched to battle state";
}
public boolean getTurn(){return myTurn;}
/**
* Getter for myTurn
* @return boolean myTurn
*/
public boolean getTurn() {return myTurn;}
}

View File

@@ -1,10 +1,3 @@
////////////////////////////////////////
// Programming project code
// UniBw M, 2022, 2023, 2024
// www.unibw.de/inf2
// (c) Mark Minas (mark.minas@unibw.de)
////////////////////////////////////////
package pp.battleship.model;
import com.jme3.network.serializing.Serializable;

View File

@@ -1,10 +1,3 @@
////////////////////////////////////////
// Programming project code
// UniBw M, 2022, 2023, 2024
// www.unibw.de/inf2
// (c) Mark Minas (mark.minas@unibw.de)
////////////////////////////////////////
package pp.battleship.model;
import com.jme3.network.serializing.Serializable;

View File

@@ -1,10 +1,3 @@
////////////////////////////////////////
// Programming project code
// UniBw M, 2022, 2023, 2024
// www.unibw.de/inf2
// (c) Mark Minas (mark.minas@unibw.de)
////////////////////////////////////////
package pp.battleship.model;
/**

View File

@@ -1,10 +1,3 @@
////////////////////////////////////////
// Programming project code
// UniBw M, 2022, 2023, 2024
// www.unibw.de/inf2
// (c) Mark Minas (mark.minas@unibw.de)
////////////////////////////////////////
package pp.battleship.model;
/**

View File

@@ -1,10 +1,3 @@
////////////////////////////////////////
// Programming project code
// UniBw M, 2022, 2023, 2024
// www.unibw.de/inf2
// (c) Mark Minas (mark.minas@unibw.de)
////////////////////////////////////////
package pp.battleship.model;
import java.io.Serializable;

View File

@@ -8,7 +8,7 @@ public class Shell implements Item {
private int y;
/**
* constructs a new shell object
* Constructs a new Shell object
*
* @param position the end position of the shell
*/

View File

@@ -1,10 +1,3 @@
////////////////////////////////////////
// Programming project code
// UniBw M, 2022, 2023, 2024
// www.unibw.de/inf2
// (c) Mark Minas (mark.minas@unibw.de)
////////////////////////////////////////
package pp.battleship.model;
import pp.battleship.notification.GameEvent;
@@ -92,8 +85,9 @@ public void add(Shot shot) {
}
/**
* Registers a shell on the map
* @param shell the shell to be registered
* Registers a Shell on the map
*
* @param shell the Shell to be registered
*/
public void add(Shell shell) {
addItem(shell);

View File

@@ -1,10 +1,3 @@
////////////////////////////////////////
// Programming project code
// UniBw M, 2022, 2023, 2024
// www.unibw.de/inf2
// (c) Mark Minas (mark.minas@unibw.de)
////////////////////////////////////////
package pp.battleship.model;
import com.jme3.network.serializing.Serializable;

View File

@@ -1,10 +1,3 @@
////////////////////////////////////////
// Programming project code
// UniBw M, 2022, 2023, 2024
// www.unibw.de/inf2
// (c) Mark Minas (mark.minas@unibw.de)
////////////////////////////////////////
package pp.battleship.model;
/**
@@ -29,5 +22,10 @@ public interface Visitor<T> {
*/
T visit(Battleship ship);
/**
* Visits a Shell element
* @param shell the Shell element to visit
* @return the result of visiting the Shell element
*/
T visit(Shell shell);
}

View File

@@ -1,10 +1,3 @@
////////////////////////////////////////
// Programming project code
// UniBw M, 2022, 2023, 2024
// www.unibw.de/inf2
// (c) Mark Minas (mark.minas@unibw.de)
////////////////////////////////////////
package pp.battleship.model;
/**
@@ -26,5 +19,9 @@ public interface VoidVisitor {
*/
void visit(Battleship ship);
/**
* Visits a Shell element
* @param shell the Shell to be visited
*/
void visit(Shell shell);
}

View File

@@ -1,10 +1,3 @@
////////////////////////////////////////
// Programming project code
// UniBw M, 2022, 2023, 2024
// www.unibw.de/inf2
// (c) Mark Minas (mark.minas@unibw.de)
////////////////////////////////////////
package pp.battleship.model.dto;
import pp.battleship.model.Battleship;

View File

@@ -1,10 +1,3 @@
////////////////////////////////////////
// Programming project code
// UniBw M, 2022, 2023, 2024
// www.unibw.de/inf2
// (c) Mark Minas (mark.minas@unibw.de)
////////////////////////////////////////
package pp.battleship.model.dto;
import com.google.gson.Gson;
@@ -117,15 +110,15 @@ public static ShipMapDTO loadFrom(File file) throws IOException {
/**
* This method returns the width of the DTO
*
* @return with of DTO
*/
public int getWidth() {return width;}
/**
* This method returns the height of the DTO
*
* @return height of DTO
*/
public int getHeight(){return height;}
public int getHeight() {return height;}
}

View File

@@ -1,10 +1,3 @@
////////////////////////////////////////
// Programming project code
// UniBw M, 2022, 2023, 2024
// www.unibw.de/inf2
// (c) Mark Minas (mark.minas@unibw.de)
////////////////////////////////////////
package pp.battleship.notification;
/**

View File

@@ -1,10 +1,3 @@
////////////////////////////////////////
// Programming project code
// UniBw M, 2022, 2023, 2024
// www.unibw.de/inf2
// (c) Mark Minas (mark.minas@unibw.de)
////////////////////////////////////////
package pp.battleship.notification;
/**

View File

@@ -1,10 +1,3 @@
////////////////////////////////////////
// Programming project code
// UniBw M, 2022, 2023, 2024
// www.unibw.de/inf2
// (c) Mark Minas (mark.minas@unibw.de)
////////////////////////////////////////
package pp.battleship.notification;
/**

View File

@@ -1,10 +1,3 @@
////////////////////////////////////////
// Programming project code
// UniBw M, 2022, 2023, 2024
// www.unibw.de/inf2
// (c) Mark Minas (mark.minas@unibw.de)
////////////////////////////////////////
package pp.battleship.notification;
/**
@@ -52,5 +45,4 @@ default void receivedEvent(ClientStateEvent event) { /* do nothing */ }
* @param event the received Event
*/
default void receivedEvent(MusicEvent event) { /* do nothing */ }
}

View File

@@ -1,10 +1,3 @@
////////////////////////////////////////
// Programming project code
// UniBw M, 2022, 2023, 2024
// www.unibw.de/inf2
// (c) Mark Minas (mark.minas@unibw.de)
////////////////////////////////////////
package pp.battleship.notification;
/**

View File

@@ -1,10 +1,3 @@
////////////////////////////////////////
// Programming project code
// UniBw M, 2022, 2023, 2024
// www.unibw.de/inf2
// (c) Mark Minas (mark.minas@unibw.de)
////////////////////////////////////////
package pp.battleship.notification;
import pp.battleship.model.Item;

View File

@@ -1,10 +1,3 @@
////////////////////////////////////////
// Programming project code
// UniBw M, 2022, 2023, 2024
// www.unibw.de/inf2
// (c) Mark Minas (mark.minas@unibw.de)
////////////////////////////////////////
package pp.battleship.notification;
import pp.battleship.model.Item;

View File

@@ -5,19 +5,19 @@
*/
public enum Music {
/**
* menu music
* Menu music
*/
MENU_THEME,
/**
* ingame music
* In game music
*/
GAME_THEME,
/**
* music for a victory
* Victory music
*/
VICTORY_THEME,
/**
* music for a loss
* Loss music
*/
LOSE_THEME,
}

View File

@@ -1,10 +1,3 @@
////////////////////////////////////////
// Programming project code
// UniBw M, 2022, 2023, 2024
// www.unibw.de/inf2
// (c) Mark Minas (mark.minas@unibw.de)
////////////////////////////////////////
package pp.battleship.notification;
/**

View File

@@ -1,10 +1,3 @@
////////////////////////////////////////
// Programming project code
// UniBw M, 2022, 2023, 2024
// www.unibw.de/inf2
// (c) Mark Minas (mark.minas@unibw.de)
////////////////////////////////////////
package pp.battleship.notification;
/**

View File

@@ -37,9 +37,9 @@ dialog.error=Error
dialog.question=Question
port.must.be.integer=Port must be an integer number
map.doesnt.fit=The map doesn't fit to this game
map.invalid = map is invalid
map.invalid=map is invalid
ships.dont.fit.the.map=Ships are out of bounds
menu.music.toggle = Music on/off
menu.music.volume = Volume
start.own.server = start server
menu.music.toggle=Music on/off
menu.music.volume=Volume
start.own.server=start server

View File

@@ -37,9 +37,9 @@ dialog.error=Fehler
dialog.question=Frage
port.must.be.integer=Der Port muss eine ganze Zahl sein
map.doesnt.fit=Diese Karte passt nicht zu diesem Spiel
map.invalid = Karte ung<6E>ltig
map.invalid=Karte ung<6E>ltig
ships.dont.fit.the.map=Schiffe sind au<61>erhalb der Map
menu.music.toggle = Musik an/aus
menu.music.volume = Lautst<EFBFBD>rke
start.own.server = Server starten
menu.music.toggle=Musik an/aus
menu.music.volume=Lautst<EFBFBD>rke
start.own.server=Server starten

View File

@@ -1,9 +1,4 @@
////////////////////////////////////////
// Programming project code
// UniBw M, 2022, 2023, 2024
// www.unibw.de/inf2
// (c) Mark Minas (mark.minas@unibw.de)
////////////////////////////////////////
package pp.battleship.client;

View File

@@ -1,9 +1,4 @@
////////////////////////////////////////
// Programming project code
// UniBw M, 2022, 2023, 2024
// www.unibw.de/inf2
// (c) Mark Minas (mark.minas@unibw.de)
////////////////////////////////////////
package pp.battleship.game.client;

View File

@@ -1,9 +1,4 @@
////////////////////////////////////////
// Programming project code
// UniBw M, 2022, 2023, 2024
// www.unibw.de/inf2
// (c) Mark Minas (mark.minas@unibw.de)
////////////////////////////////////////
package pp.battleship.game.client;

View File

@@ -1,9 +1,4 @@
////////////////////////////////////////
// Programming project code
// UniBw M, 2022, 2023, 2024
// www.unibw.de/inf2
// (c) Mark Minas (mark.minas@unibw.de)
////////////////////////////////////////
package pp.battleship.game.client;

View File

@@ -1,9 +1,4 @@
////////////////////////////////////////
// Programming project code
// UniBw M, 2022, 2023, 2024
// www.unibw.de/inf2
// (c) Mark Minas (mark.minas@unibw.de)
////////////////////////////////////////
package pp.battleship.game.client;

View File

@@ -1,9 +1,4 @@
////////////////////////////////////////
// Programming project code
// UniBw M, 2022, 2023, 2024
// www.unibw.de/inf2
// (c) Mark Minas (mark.minas@unibw.de)
////////////////////////////////////////
package pp.battleship.game.client;

View File

@@ -1,9 +1,4 @@
////////////////////////////////////////
// Programming project code
// UniBw M, 2022, 2023, 2024
// www.unibw.de/inf2
// (c) Mark Minas (mark.minas@unibw.de)
////////////////////////////////////////
package pp.battleship.game.client;

View File

@@ -1,9 +1,4 @@
////////////////////////////////////////
// Programming project code
// UniBw M, 2022, 2023, 2024
// www.unibw.de/inf2
// (c) Mark Minas (mark.minas@unibw.de)
////////////////////////////////////////
package pp.battleship.game.server;

View File

@@ -1,9 +1,4 @@
////////////////////////////////////////
// Programming project code
// UniBw M, 2022, 2023, 2024
// www.unibw.de/inf2
// (c) Mark Minas (mark.minas@unibw.de)
////////////////////////////////////////
package pp.battleship.game.server;

View File

@@ -1,9 +1,4 @@
////////////////////////////////////////
// Programming project code
// UniBw M, 2022, 2023, 2024
// www.unibw.de/inf2
// (c) Mark Minas (mark.minas@unibw.de)
////////////////////////////////////////
package pp.battleship.game.server;

View File

@@ -1,9 +1,4 @@
////////////////////////////////////////
// Programming project code
// UniBw M, 2022, 2023, 2024
// www.unibw.de/inf2
// (c) Mark Minas (mark.minas@unibw.de)
////////////////////////////////////////
package pp.battleship.game.server;

View File

@@ -1,9 +1,4 @@
////////////////////////////////////////
// Programming project code
// UniBw M, 2022, 2023, 2024
// www.unibw.de/inf2
// (c) Mark Minas (mark.minas@unibw.de)
////////////////////////////////////////
package pp.battleship.model;

View File

@@ -1,9 +1,4 @@
////////////////////////////////////////
// Programming project code
// UniBw M, 2022, 2023, 2024
// www.unibw.de/inf2
// (c) Mark Minas (mark.minas@unibw.de)
////////////////////////////////////////
package pp.battleship.model;

View File

@@ -1,5 +1,4 @@
########################################
## Programming project code
########################################## Programming project code
## UniBw M, 2022, 2023, 2024
## www.unibw.de/inf2
## (c) Mark Minas (mark.minas@unibw.de)

View File

@@ -1,10 +1,3 @@
////////////////////////////////////////
// Programming project code
// UniBw M, 2022, 2023, 2024
// www.unibw.de/inf2
// (c) Mark Minas (mark.minas@unibw.de)
////////////////////////////////////////
package pp.battleship.server;
import com.jme3.network.ConnectionListener;
@@ -81,12 +74,18 @@ public static void main(String[] args) {
logic = new ServerGameLogic(this, config);
}
/**
* Runs a server
*/
public void run() {
startServer();
while (true)
processNextMessage();
}
/**
* Starts a server
*/
private void startServer() {
try {
LOGGER.log(Level.INFO, "Starting server..."); //NON-NLS
@@ -102,6 +101,9 @@ private void startServer() {
}
}
/**
* Processes next received message
*/
private void processNextMessage() {
try {
pendingMessages.take().process(logic);
@@ -112,6 +114,9 @@ private void processNextMessage() {
}
}
/**
* Registers all serializable classes
*/
private void initializeSerializables() {
Serializer.registerClass(GameDetails.class);
Serializer.registerClass(StartBattleMessage.class);
@@ -126,6 +131,9 @@ private void initializeSerializables() {
Serializer.registerClass(SwitchToBattleState.class);
}
/**
* Registers all listeners
*/
private void registerListeners() {
myServer.addMessageListener(this, MapMessage.class);
myServer.addMessageListener(this, ShootMessage.class);
@@ -133,6 +141,11 @@ private void registerListeners() {
myServer.addConnectionListener(this);
}
/**
* Handles a received message
* @param source the connection the message comes from
* @param message the received message
*/
@Override
public void messageReceived(HostedConnection source, Message message) {
LOGGER.log(Level.INFO, "message received from {0}: {1}", source.getId(), message); //NON-NLS
@@ -140,12 +153,22 @@ public void messageReceived(HostedConnection source, Message message) {
pendingMessages.add(new ReceivedMessage(clientMessage, source.getId()));
}
/**
* Adds a new connection to a server
* @param server the server to add the connection to
* @param hostedConnection the connection to be added
*/
@Override
public void connectionAdded(Server server, HostedConnection hostedConnection) {
LOGGER.log(Level.INFO, "new connection {0}", hostedConnection); //NON-NLS
logic.addPlayer(hostedConnection.getId());
}
/**
* Removes a standing connection from a server
* @param server the server to add the connection to
* @param hostedConnection the connection to be added
*/
@Override
public void connectionRemoved(Server server, HostedConnection hostedConnection) {
LOGGER.log(Level.INFO, "connection closed: {0}", hostedConnection); //NON-NLS
@@ -158,6 +181,10 @@ public void connectionRemoved(Server server, HostedConnection hostedConnection)
}
}
/**
* Shuts down the server and terminates the application with the given exit code
* @param exitValue the exit status code
*/
private void exit(int exitValue) { //NON-NLS
LOGGER.log(Level.INFO, "close request"); //NON-NLS
if (myServer != null)

View File

@@ -1,15 +1,13 @@
////////////////////////////////////////
// Programming project code
// UniBw M, 2022, 2023, 2024
// www.unibw.de/inf2
// (c) Mark Minas (mark.minas@unibw.de)
////////////////////////////////////////
package pp.battleship.server;
import pp.battleship.message.client.ClientInterpreter;
import pp.battleship.message.client.ClientMessage;
/**
* Represents a message received from a client
* @param message the received message
* @param from the ID of the client that sent the message
*/
record ReceivedMessage(ClientMessage message, int from) {
void process(ClientInterpreter interpreter) {
message.accept(interpreter, from);

View File

@@ -1,9 +1,4 @@
////////////////////////////////////////
// Programming project code
// UniBw M, 2022, 2023, 2024
// www.unibw.de/inf2
// (c) Mark Minas (mark.minas@unibw.de)
////////////////////////////////////////
package pp.util;

View File

@@ -1,9 +1,4 @@
////////////////////////////////////////
// Programming project code
// UniBw M, 2022, 2023, 2024
// www.unibw.de/inf2
// (c) Mark Minas (mark.minas@unibw.de)
////////////////////////////////////////
package pp.util;

Some files were not shown because too many files have changed in this diff Show More