EffectTrack now disable and cull the emitter once all particles are gone

git-svn-id: https://jmonkeyengine.googlecode.com/svn/trunk@9625 75d07b2b-3a1a-0410-a2c5-0572b91ccdca
3.0
rem..om 12 years ago
parent 2ab5e4abbe
commit ef98888ce7
  1. 52
      engine/src/core/com/jme3/animation/EffectTrack.java

@ -36,6 +36,12 @@ import com.jme3.export.InputCapsule;
import com.jme3.export.JmeExporter; import com.jme3.export.JmeExporter;
import com.jme3.export.JmeImporter; import com.jme3.export.JmeImporter;
import com.jme3.export.OutputCapsule; import com.jme3.export.OutputCapsule;
import com.jme3.renderer.RenderManager;
import com.jme3.renderer.ViewPort;
import com.jme3.scene.Spatial;
import com.jme3.scene.Spatial.CullHint;
import com.jme3.scene.control.AbstractControl;
import com.jme3.scene.control.Control;
import com.jme3.util.TempVars; import com.jme3.util.TempVars;
import java.io.IOException; import java.io.IOException;
@ -64,27 +70,52 @@ public class EffectTrack implements Track {
private float length = 0; private float length = 0;
private boolean emitted = false; private boolean emitted = false;
private boolean initialized = false; private boolean initialized = false;
private boolean stopRequested = false;
//control responsible for disable and cull the emitter once all particles are gone
private AbstractControl killParticles = new AbstractControl() {
@Override
protected void controlUpdate(float tpf) {
if (emitter.getNumVisibleParticles() == 0) {
emitter.setCullHint(CullHint.Always);
emitter.setEnabled(false);
emitter.removeControl(killParticles);
stopRequested = false;
}
}
@Override
protected void controlRender(RenderManager rm, ViewPort vp) {
}
public Control cloneForSpatial(Spatial spatial) {
return null;
}
};
//Anim listener that stops the Emmitter when the animation is finished or changed. //Anim listener that stops the Emmitter when the animation is finished or changed.
private class OnEndListener implements AnimEventListener { private class OnEndListener implements AnimEventListener {
public void onAnimCycleDone(AnimControl control, AnimChannel channel, String animName) { public void onAnimCycleDone(AnimControl control, AnimChannel channel, String animName) {
stop(); if(!stopRequested){
stop();
}
} }
public void onAnimChange(AnimControl control, AnimChannel channel, String animName) { public void onAnimChange(AnimControl control, AnimChannel channel, String animName) {
stop(); if(!stopRequested){
stop();
}
} }
} }
/** /**
* default constructor only for serialization * default constructor only for serialization
*/ */
public EffectTrack() { public EffectTrack() {
} }
/** /**
* Creates and EffectTrack * Creates and EffectTrack
* @param emitter the emmitter of the track * @param emitter the emmitter of the track
@ -125,9 +156,14 @@ public class EffectTrack implements Track {
//checking fo time to trigger the effect //checking fo time to trigger the effect
if (!emitted && time >= startOffset) { if (!emitted && time >= startOffset) {
emitted = true; emitted = true;
stopRequested = false;
emitter.setCullHint(CullHint.Dynamic);
emitter.setEnabled(true);
//if the emitter has 0 particles per seconds emmit all particles in one shot //if the emitter has 0 particles per seconds emmit all particles in one shot
if (particlesPerSeconds == 0) { if (particlesPerSeconds == 0) {
emitter.emitAllParticles(); emitter.emitAllParticles();
emitter.addControl(killParticles);
stopRequested = true;
} else { } else {
//else reset its former particlePerSec value to let it emmit. //else reset its former particlePerSec value to let it emmit.
emitter.setParticlesPerSec(particlesPerSeconds); emitter.setParticlesPerSec(particlesPerSeconds);
@ -139,6 +175,8 @@ public class EffectTrack implements Track {
private void stop() { private void stop() {
emitter.setParticlesPerSec(0); emitter.setParticlesPerSec(0);
emitted = false; emitted = false;
emitter.addControl(killParticles);
stopRequested = true;
} }
/** /**
@ -159,7 +197,7 @@ public class EffectTrack implements Track {
} }
/** /**
* *
* @return the emitter used by this track * @return the emitter used by this track
*/ */
@ -217,8 +255,4 @@ public class EffectTrack implements Track {
length = in.readFloat("length", length); length = in.readFloat("length", length);
startOffset = in.readFloat("startOffset", 0); startOffset = in.readFloat("startOffset", 0);
} }
} }

Loading…
Cancel
Save