added the 'SeaSynchronizer'

so if the shot hits or misses, a particle is shown, hereby I modified the visit(Shot shot) handleHit
and added the 'createMissParticle' and 'createHitParticle' methods
This commit is contained in:
Benjamin Feyer
2024-10-08 17:16:09 +02:00
parent 387bda04b9
commit 54d8ad57cb

View File

@@ -7,6 +7,8 @@
package pp.battleship.client.gui;
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;
@@ -20,6 +22,7 @@
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;
@@ -30,7 +33,6 @@
import static pp.util.FloatMath.HALF_PI;
import static pp.util.FloatMath.PI;
/**
* The {@code SeaSynchronizer} class is responsible for synchronizing the graphical
* representation of the ships and shots on the sea map with the underlying data model.
@@ -43,6 +45,7 @@ class SeaSynchronizer extends ShipMapSynchronizer {
private static final String SUBMARINE = "Models/Submarine/submarine.obj";
private static final String DESTROYER = "Models/Destroyer/10619_Battleship.obj";
private static final String SMALL_SHIP = "Models/SmallShip/10634_SpeedBoat_v01_LOD3.obj";
private static final String PARTICLE = "Common/MatDefs/Misc/Particle.j3md";
private static final String COLOR = "Color"; //NON-NLS
private static final String SHIP = "ship"; //NON-NLS
private static final String SHOT = "shot"; //NON-NLS
@@ -77,7 +80,20 @@ public SeaSynchronizer(BattleshipApp app, Node root, ShipMap map) {
*/
@Override
public Spatial visit(Shot shot) {
return shot.isHit() ? handleHit(shot) : createCylinder(shot);
return shot.isHit() ? handleHit(shot) : handleMiss(shot);
}
/**
* Handles a miss
*
* @param shot a hit
* @return always null to prevent the representation from being attached
* to the items node as well
*/
private Spatial handleMiss(Shot shot) {
final ParticleEmitter emitter = createMissParticle(shot);
return emitter;
}
/**
@@ -91,14 +107,74 @@ public Spatial visit(Shot shot) {
private Spatial handleHit(Shot shot) {
final Battleship ship = requireNonNull(map.findShipAt(shot), "Missing ship");
final Node shipNode = requireNonNull((Node) getSpatial(ship), "Missing ship node");
final Geometry representation = createCylinder(shot);
representation.getLocalTranslation().subtractLocal(shipNode.getLocalTranslation());
shipNode.attachChild(representation);
final ParticleEmitter particleEmitter = createHitParticle(shot);
particleEmitter.getLocalTranslation().subtractLocal(shipNode.getLocalTranslation());
shipNode.attachChild(particleEmitter);
if (ship.isDestroyed()) {
for (int i = 0; i < 50; i++) {
float r = (float) 0.001 * i;
shipNode.move(0, -r, 0);
}
}
return null;
}
/**
* this method creates the particles, when the shot misses
*
* @param shot is the parameter, where was shot
* @return the particle
*/
private ParticleEmitter createMissParticle(Shot shot) {
ParticleEmitter hitEffect = new ParticleEmitter("HitEffect", Type.Triangle, 30);
hitEffect.setMaterial(new Material(app.getAssetManager(), PARTICLE));
hitEffect.setImagesX(2);
hitEffect.setImagesY(2);
hitEffect.setStartColor(ColorRGBA.Cyan);
hitEffect.setEndColor(ColorRGBA.Blue);
hitEffect.getParticleInfluencer().setInitialVelocity(new Vector3f(0.1f, 0.1f, 0.1f));
hitEffect.move(0, -2, 0);
hitEffect.setStartSize(0.45f);
hitEffect.setEndSize(0.45f);
hitEffect.setGravity(0, -0.5f, 0);
hitEffect.setLowLife(1f);
hitEffect.setHighLife(1f);
hitEffect.setParticlesPerSec(0);
hitEffect.setLocalTranslation(shot.getY() + 0.5f, 0, shot.getX() + 0.5f);
hitEffect.emitAllParticles();
return hitEffect;
}
/**
* this method creates the particles, when a ship is hit
*
* @param shot says, where was shot
* @return the particle
*/
private ParticleEmitter createHitParticle(Shot shot) {
ParticleEmitter hitEffect = new ParticleEmitter("HitEffect", Type.Triangle, 30);
hitEffect.setMaterial(new Material(app.getAssetManager(), PARTICLE));
hitEffect.setImagesX(2);
hitEffect.setImagesY(2);
hitEffect.setStartColor(ColorRGBA.Orange);
hitEffect.setEndColor(ColorRGBA.Red);
hitEffect.getParticleInfluencer().setInitialVelocity(new Vector3f(1, 1, 1));
hitEffect.setStartSize(0.45f);
hitEffect.setEndSize(0.1f);
hitEffect.setGravity(0, -0.5f, 0);
hitEffect.setLowLife(1f);
hitEffect.setHighLife(2f);
hitEffect.setParticlesPerSec(0);
hitEffect.setLocalTranslation(shot.getY() + 0.5f, 0, shot.getX() + 0.5f);
hitEffect.emitAllParticles();
return hitEffect;
}
/**
* Creates a cylinder geometry representing the specified shot.
* The appearance of the cylinder depends on whether the shot is a hit or a miss.
@@ -182,7 +258,7 @@ private Spatial createDestroyer(Battleship ship){
model.scale(0.0001f);
model.move(0, 0.3f, 0);
model.setShadowMode(ShadowMode.CastAndReceive);
model.setMaterial(app.getAssetManager().loadMaterial("Models/Destroyer/10619_Battleship.mtl"));
//model.setMaterial(app.getAssetManager().loadMaterial("Models/Destroyer/10619_Battleship.mtl"));
//TODO
//Material m = new Material();
@@ -198,7 +274,6 @@ private Spatial createSubmarine(Battleship ship){
model.setShadowMode(ShadowMode.CastAndReceive);
return model;
}