space-game/src/sg/states/InGameState.java

119 lines
3.8 KiB
Java
Raw Normal View History

package sg.states;
import java.util.logging.Logger;
import sg.env.Environment;
import sg.util.SGUtil;
2009-03-11 14:15:06 +00:00
import com.jme.input.FirstPersonHandler;
import com.jme.input.InputHandler;
2009-03-11 14:55:21 +00:00
import com.jme.light.PointLight;
import com.jme.math.Vector3f;
2009-03-11 14:55:21 +00:00
import com.jme.renderer.ColorRGBA;
import com.jme.renderer.Renderer;
import com.jme.scene.Node;
2009-03-11 14:55:21 +00:00
import com.jme.scene.state.LightState;
import com.jme.system.DisplaySystem;
import com.jmex.game.state.CameraGameState;
import com.jmex.game.state.GameStateManager;
import com.jmex.game.state.load.LoadingGameState;
import com.jmex.physics.DynamicPhysicsNode;
import com.jmex.physics.PhysicsDebugger;
import com.jmex.physics.PhysicsSpace;
import com.jmex.physics.StaticPhysicsNode;
public class InGameState extends CameraGameState {
/** The Keyboard/ mous input handler */
protected InputHandler input;
/** The environment of the game */
protected Environment environment;
/** The physics space */
private PhysicsSpace physicsSpace;
/** The Speed of the physics */
private float physicsSpeed = 1;
2009-03-11 14:15:06 +00:00
/** Render pass manager */
//protected BasicPassManager passManager;
public InGameState(String name) {
super(name);
2009-03-11 14:15:06 +00:00
LoadingGameState loader = new LoadingGameState(2);
GameStateManager.getInstance().attachChild(loader);
loader.setActive(true);
// ***************** Load the Game **********************************
2009-03-11 14:15:06 +00:00
// Pass (render thingies)
//passManager = new BasicPassManager();
//physics
physicsSpace = PhysicsSpace.create();
2009-03-11 14:15:06 +00:00
physicsSpace.setDirectionalGravity(new Vector3f(0f, 0f, 0f));
StaticPhysicsNode staticNode = physicsSpace.createStaticNode();
DynamicPhysicsNode dynamicNode = physicsSpace.createDynamicNode();
rootNode.attachChild(dynamicNode);
rootNode.attachChild(staticNode);
2009-03-11 14:15:06 +00:00
// environment
environment = new Environment(rootNode);
rootNode.attachChild(environment);
loader.increment();
2009-03-11 14:55:21 +00:00
// ship
Node ship = SGUtil.loadModel("sg/data/models/ships/G6.3ds");
ship.setLocalTranslation(new Vector3f( 0, 0, 40));
rootNode.attachChild(ship);
loader.increment();
// inputs
2009-03-11 14:15:06 +00:00
FirstPersonHandler first = new FirstPersonHandler( cam, 50, 1 );
input = new InputHandler();
input.addToAttachedHandlers( first );
loader.increment();
2009-03-11 14:55:21 +00:00
// lights
/** Set up a basic, default light. */
PointLight light = new PointLight();
light.setDiffuse( new ColorRGBA( 0.75f, 0.75f, 0.75f, 0.75f ) );
light.setAmbient( new ColorRGBA( 0.5f, 0.5f, 0.5f, 1.0f ) );
light.setLocation( new Vector3f( 100, 100, 100 ) );
light.setEnabled( true );
/** Attach the light to a lightState and the lightState to rootNode. */
LightState lightState = DisplaySystem.getDisplaySystem().getRenderer().createLightState();
lightState.setEnabled( true );
lightState.attach( light );
rootNode.setRenderState( lightState );
rootNode.updateRenderState();
2009-03-11 14:15:06 +00:00
// ***************** Done loading **********************************
loader.setActive(false);
GameStateManager.getInstance().detachChild(loader);
this.setActive(true);
}
public void stateUpdate(float tpf) {
input.update(tpf);
2009-03-11 14:15:06 +00:00
environment.update();
2009-03-11 14:15:06 +00:00
if ( tpf > 0.2 || Float.isNaN( tpf ) ) {
Logger.getLogger( PhysicsSpace.LOGGER_NAME ).warning( "Maximum physics update interval is 0.2 seconds - capped." );
physicsSpace.update( 0.2f * physicsSpeed );
}
else physicsSpace.update( tpf * physicsSpeed );
//passManager.updatePasses(tpf);
}
2009-03-11 14:15:06 +00:00
public void stateRender(float tpf){
Renderer r = DisplaySystem.getDisplaySystem().getRenderer();
2009-03-11 14:15:06 +00:00
// debug
PhysicsDebugger.drawPhysics( physicsSpace, r );
2009-03-11 14:15:06 +00:00
/** Have the PassManager render. */
//passManager.renderPasses(r);
}
}