package org.pollux3d.state;
import org.pollux3d.tuio.TuioInput;
import org.pollux3d.tuio.TuioInputListener;
import com.jme3.app.Application;
import com.jme3.app.state.AbstractAppState;
import com.jme3.app.state.AppStateManager;
import com.jme3.asset.AssetManager;
import com.jme3.effect.EmitterSphereShape;
import com.jme3.effect.ParticleEmitter;
import com.jme3.effect.ParticleMesh.Type;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector2f;
import com.jme3.math.Vector3f;
import com.jme3.renderer.Camera;
import com.jme3.scene.Node;
public class FireFingerState extends AbstractAppState implements TuioInputListener {
private TuioInput tuioInput;
private Node tuioFireNode = new Node("TUIO Fire Node");
private ParticleEmitter flame1;
private ParticleEmitter flame2;
private Camera cam;
private static float zPos = 0.96f;
private AssetManager assetManager;
public Node getTuioFireNode() {
return tuioFireNode;
}
public void initialize(AppStateManager stateManager, Application app) {
super.initialize(stateManager, app);
assetManager = app.getAssetManager();
tuioInput = TuioInput.get();
tuioInput.addListener(this);
cam = app.getCamera();
flame1 = getEmitter("Flame 1");
flame2 = getEmitter("Flame 2");
tuioFireNode.attachChild(flame1);
tuioFireNode.attachChild(flame2);
}
private ParticleEmitter getEmitter(String name) {
ParticleEmitter flame = new ParticleEmitter(name, Type.Triangle, 32);
flame.setSelectRandomImage(true);
flame.setStartColor(new ColorRGBA(1f, 0.4f, 0.05f, 1f));
flame.setEndColor(new ColorRGBA(.4f, .22f, .12f, 0f));
flame.setStartSize(1.3f);
flame.setEndSize(2f);
flame.setShape(new EmitterSphereShape(Vector3f.ZERO, 1f));
flame.setParticlesPerSec(0);
flame.setGravity(-5f);
flame.setLowLife(1f);
flame.setHighLife(1f);
flame.setInitialVelocity(new Vector3f(0, 7, 0));
flame.setVelocityVariation(1f);
flame.setImagesX(2);
flame.setImagesY(2);
Material mat = new Material(assetManager, "Common/MatDefs/Misc/Particle.j3md");
mat.setTexture("Texture", assetManager.loadTexture("Effects/Explosion/flame.png"));
mat.setBoolean("PointSprite", true);
flame.setMaterial(mat);
return flame;
}
public void onMove(int cursorId, int delta, int delta2, int newX, int newY) {
if (cursorId == 0) {
flame1.setLocalTranslation(cam.getWorldCoordinates(new Vector2f(newX, newY), zPos));
} else if(cursorId == 1) {
flame2.setLocalTranslation(cam.getWorldCoordinates(new Vector2f(newX, newY), zPos));
}
}
public void onNew(int cursorId, int x, int y) {
if (cursorId == 0) {
flame1.setLocalTranslation(cam.getWorldCoordinates(new Vector2f(x, y), zPos));
flame1.setParticlesPerSec(100);
} else if(cursorId == 1) {
flame2.setLocalTranslation(cam.getWorldCoordinates(new Vector2f(x, y), zPos));
flame2.setParticlesPerSec(100);
}
}
public void onRelease(int cursorId) {
if (cursorId == 0) {
flame1.setParticlesPerSec(0);
} else if(cursorId == 1) {
flame2.setParticlesPerSec(0);
}
}
}