package javara.world.effects;
import javara.world.VisibleObject;
import javara.world.World;
import com.jme3.audio.AudioNode;
import com.jme3.audio.AudioRenderer;
import com.jme3.effect.ParticleEmitter;
import com.jme3.effect.ParticleMesh;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
import com.jme3.scene.Node;
import java.util.Random;
public class Explosion extends VisibleObject {
protected ParticleEmitter explosion;
protected float duration, elapsed = 0.0f;
protected AudioNode sound;
protected AudioRenderer renderer;
public Explosion(World world, Vector3f location, float duration, ColorRGBA color) {
super(color);
this.duration = duration;
explosion = new ParticleEmitter(identifier, ParticleMesh.Type.Triangle, 10);
Material mat = new Material(world.getAssetManager(), "Common/MatDefs/Misc/Particle.j3md");
mat.setTexture("Texture", world.getAssetManager().loadTexture("Effects/Shrapnel.png"));
explosion.setLocalTranslation(location);
explosion.setMaterial(mat);
explosion.setStartColor(color);
explosion.setEndColor(color);
explosion.setImagesX(4);
explosion.setImagesY(4);
explosion.setStartSize(0.1f);
explosion.setEndSize(0.1f);
// explosion.setRandomAngle(true);
explosion.setGravity(0, 15.0f, 0);
explosion.setRotateSpeed(0.25f);
explosion.getParticleInfluencer().setInitialVelocity(new Vector3f(0, 5, 0));
explosion.getParticleInfluencer().setVelocityVariation(0.4f);
spatial = explosion;
Random rand = new Random();
sound = new AudioNode(world.getAssetManager(), String.format("Sound/Effects/Explosion%d.ogg", rand.nextInt(4) + 1));
sound.setLooping(false);
sound.setVolume(0.5f);
sound.setPositional(true);
sound.setLocalTranslation(location);
renderer = world.getAudioRenderer();
}
public void attachedToWorld() {
explosion.emitAllParticles();
renderer.playSource(sound);
}
public void update(float tpf) {
elapsed += tpf;
if (elapsed > duration) {
world.removeObject(this);
}
}
}