package org.pollux3d.core;
import java.util.concurrent.atomic.AtomicBoolean;
import org.pollux3d.app.PolluxApplication;
import org.pollux3d.cam.CamTarget;
import org.pollux3d.core.event.MapEntityClickEvent;
import org.pollux3d.core.event.NothingHitClickEvent;
import org.pollux3d.core.event.PolluxEvent;
import org.pollux3d.core.event.PolluxEventListener;
import org.pollux3d.gesture.handler.CamGestureHandler;
import org.pollux3d.gesture.handler.ClickGestureHandler;
import org.pollux3d.map.GeometryManager;
import org.pollux3d.map.GeometryManagerImpl;
import org.pollux3d.map.MapEntity;
import org.pollux3d.menu.Markable;
import org.pollux3d.scene.LoadingScreen;
import org.pollux3d.scene.PolluxScene;
import org.pollux3d.state.PolluxStateManager;
import com.jme3.app.Application;
import com.jme3.app.SimpleApplication;
import com.jme3.asset.AssetManager;
import com.jme3.audio.AudioNode;
import com.jme3.scene.Node;
import com.jme3.system.AppSettings;
public class Pollux implements PolluxEventListener {
private static Pollux instance = null;
private Markable activeMark = null;
public static void create(PolluxApplication app) {
if (instance != null) throw new IllegalStateException();
instance = new Pollux(app);
}
public static void createDebug(SimpleApplication app) {
if (instance != null) throw new IllegalStateException();
instance = new Pollux(app);
}
public static Pollux get() {
return instance;
}
private Application app;
private PolluxApplication polluxApp = null;
private PolluxStateManager stateManager;
private CamGestureHandler camHandler;
private ClickGestureHandler clickHandler;
private GeometryManager clickableGeometries;
private AtomicBoolean init = new AtomicBoolean(false);
private Node lodingNode = null;
private AudioNode audio_ambient;
private AudioNode audio_click;
private Pollux(Application app) {
this.app = app;
clickableGeometries = new GeometryManagerImpl();
if (app instanceof PolluxApplication) {
polluxApp = ((PolluxApplication)app);
stateManager = new PolluxStateManager(app.getStateManager(), polluxApp.getRootNode(), polluxApp.getGuiNode());
}
}
public boolean isInitialized() {
return init.get();
}
public AssetManager getAssetManager() {
return app.getAssetManager();
}
public AppSettings getSettings() {
return app.getContext().getSettings();
}
public GeometryManager getGeometryManager() {
return clickableGeometries;
}
public void showLoadingScreen() {
if (polluxApp != null) {
PolluxScene loadingScene = new LoadingScreen();
lodingNode = loadingScene.create(app.getAssetManager(), app.getCamera());
polluxApp.getRootNode().attachChild(lodingNode);
}
}
private void hideLoadingScreen() {
if (lodingNode != null) {
polluxApp.getRootNode().detachChild(lodingNode);
}
}
public void init() {
init.set(true);
if (polluxApp != null) {
polluxApp.initTuio();
}
stateManager.loadStats();
camHandler = new CamGestureHandler(stateManager.getCamAnimation());
camHandler.setActive(true);
clickHandler = new ClickGestureHandler(stateManager.getCam(), stateManager.getMap());
clickHandler.setListener(this);
clickHandler.setActive(true);
stateManager.getCamAnimation().setTarget(stateManager.getMap().getDefaultTarget());
/*
stateManager.getShipMotion().startDemoMotion(stateManager.getMap().getShip());
*/
hideLoadingScreen();
}
public PolluxStateManager getStateManager() {
return stateManager;
}
public void onPolluxEvent(PolluxEvent event) {
if (event instanceof MapEntityClickEvent) {
MapEntityClickEvent clickEvent = (MapEntityClickEvent) event;
MapEntity entity = clickEvent.getEntity();
//added by floh -- 31/10/2011
//play sound when clicked on markable
polluxApp.getAudioRenderer().playSourceInstance(audio_click);
if (entity instanceof CamTarget) {
stateManager.getCamAnimation().setTarget((CamTarget) entity);
}
if (entity instanceof Markable) {
mark((Markable) entity);
/*
if (entity instanceof Planet) {
stateManager.getCamAnimation().setRotationOffest(0, -0.2f);
stateManager.getHudAnimation().showInfoBox((Planet) entity);
}
*/
}
} else if(event instanceof NothingHitClickEvent) {
mark(null);
/*
stateManager.getHudAnimation().hideInfoBox();
stateManager.getCamAnimation().setRotationOffest(0, 0);
*/
}
}
private void mark(Markable entity) {
if (activeMark != null) activeMark.hideMark();
if (entity != null) entity.showMark();
activeMark = entity;
}
//added by floh -- 31/10/2011
//Create Audio Nodes for Used Sound
public void initAudio() {
//System.out.println("############# AUDIO: " + audio_ambient.getStatus());
if (polluxApp != null) {
System.out.println("################# INIT AUDIO ####################");
audio_ambient = new AudioNode(app.getAssetManager(), "Sound/Ambient_test.ogg", false);
audio_ambient.setLooping(true); // for continuous playing
audio_ambient.setVolume(0.2f);
polluxApp.getRootNode().attachChild(audio_ambient);
polluxApp.getAudioRenderer().playSource(audio_ambient);
System.out.println("############# AUDIO: " + audio_ambient.getStatus());
audio_click = new AudioNode(app.getAssetManager(), "Sound/Click_test.wav", false);
audio_click.setLooping(false);
audio_click.setVolume(2);
polluxApp.getRootNode().attachChild(audio_click);
}
}
}