package org.pollux3d.state;
import java.util.ArrayList;
import java.util.List;
import org.pollux3d.animation.cam.CamDistanceAnimation;
import org.pollux3d.animation.cam.CamPositionAnimation;
import org.pollux3d.animation.cam.CamRotationOffsetAnimation;
import org.pollux3d.cam.Cam;
import org.pollux3d.cam.CamLocationListener;
import org.pollux3d.cam.CamTarget;
import org.pollux3d.cam.PolluxCam;
import com.jme3.app.Application;
import com.jme3.app.state.AbstractAppState;
import com.jme3.app.state.AppStateManager;
import com.jme3.math.Ray;
import com.jme3.math.Vector2f;
import com.jme3.math.Vector3f;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
/**
* Animates the camera
* @author mawi
*
*/
public class CamAnimationState extends AbstractAppState implements CamAnimation {
/**
* the camera
*/
private PolluxCam cam;
/**
* Camera direction listeners
*/
private List<CamLocationListener> camDirectionListeners = new ArrayList<CamLocationListener>();
/**
* position animation
*/
private Spatial position = new Node("PolluxCamPos");
private CamPositionAnimation positionAnimation = new CamPositionAnimation(position, 1, 0.5f);
/**
* distances (zoom) animation
*/
private CamDistanceAnimation distanceAnimation = new CamDistanceAnimation(100, 1, 0.5f);
private Spatial target = null;
/**
* rotation offsets (vertical & horizontal) animation
*/
private CamRotationOffsetAnimation rotationOffsetAnimation = new CamRotationOffsetAnimation(1, 0.5f);
public void initialize(AppStateManager stateManager, Application app){
super.initialize(stateManager, app);
cam = new PolluxCam(app.getCamera(), position);
}
public Cam getCamera() {
return cam;
}
/* (non-Javadoc)
* @see org.pollux3d.state.Cam#setTarget(org.pollux3d.cam.CamTarget)
*/
public void setTarget(CamTarget target) {
this.target = target.getTarget();
cam.setSpatial(position);
positionAnimation.animate(this.target);
distanceAnimation.animate(target.getZoomDistance(), cam.getZoomDistance());
}
public void update(float tpf){
positionAnimation.update(tpf);
distanceAnimation.update(tpf);
rotationOffsetAnimation.update(tpf);
if (distanceAnimation.isFinished()) {
cam.setCanZoom(true);
} else {
cam.setCanZoom(false);
cam.setZoomDistance(distanceAnimation.getDistance());
}
if (!rotationOffsetAnimation.isFinished()) {
Vector2f rotationOffset = rotationOffsetAnimation.getRotationOffset();
cam.setRotationOffset(rotationOffset.x, rotationOffset.y);
}
if (positionAnimation.isFinished() && target != null) {
cam.setSpatial(target);
}
cam.updateCamera();
cam.updateListeners();
}
public void setRotationOffest(float vertical, float horizontal) {
rotationOffsetAnimation.animate(vertical, horizontal);
}
/* (non-Javadoc)
* @see org.pollux3d.state.Cam#move(com.jme3.math.Vector2f)
*/
public void move(Vector2f move) {
cam.hRotateCamera(move.x);
cam.vRotateCamera(move.y);
}
/* (non-Javadoc)
* @see org.pollux3d.state.Cam#getClickRay(com.jme3.math.Vector2f)
*/
public Ray getClickRay(Vector2f point) {
return cam.getClickRay(point);
}
/* (non-Javadoc)
* @see org.pollux3d.state.Cam#zoom(float)
*/
public void zoom(float zoom) {
cam.zoomCamera(zoom);
}
/*
public void addCamDirectionListener(CamDirectionListener listener) {
if (listener == null) throw new NullPointerException();
camDirectionListeners.add(listener);
}
public void removeAllCamDirectionListener() {
camDirectionListeners.clear();
}
public void removeCamDirectionListener(CamDirectionListener listener) {
if (listener == null) throw new NullPointerException();
camDirectionListeners.remove(listener);
}
*/
}