diff --git a/src/sg/SpaceGame.java b/src/sg/SpaceGame.java index 256930c..2ff0a59 100644 --- a/src/sg/SpaceGame.java +++ b/src/sg/SpaceGame.java @@ -1,7 +1,7 @@ package sg; import sg.env.Environment; -import sg.input.FirstPersonHandler; +import sg.input.SGFirstPersonHandler; import sg.util.SGUtil; import com.jme.scene.Node; @@ -16,7 +16,7 @@ public class SpaceGame extends SimplePhysicsGame { private Environment environment; private Node ship; private DynamicPhysicsNode shipNode; - private FirstPersonHandler firstPersonHandler; + private SGFirstPersonHandler firstPersonHandler; public static void main(String[] args) { //Logger.getLogger("").setLevel( Level.WARNING ); @@ -50,7 +50,7 @@ public class SpaceGame extends SimplePhysicsGame { shipNode.attachChild(ship); //controll - firstPersonHandler = new FirstPersonHandler(ship, shipNode, cam); + firstPersonHandler = new SGFirstPersonHandler(ship, shipNode, cam); } public void simpleUpdate(){ diff --git a/src/sg/input/FirstPersonHandler.java b/src/sg/input/SGFirstPersonHandler.java similarity index 87% rename from src/sg/input/FirstPersonHandler.java rename to src/sg/input/SGFirstPersonHandler.java index ada3728..4f8e0d7 100644 --- a/src/sg/input/FirstPersonHandler.java +++ b/src/sg/input/SGFirstPersonHandler.java @@ -8,14 +8,14 @@ import com.jme.scene.Node; import com.jmex.physics.DynamicPhysicsNode; -public class FirstPersonHandler extends GlobalInputHandler{ +public class SGFirstPersonHandler extends GlobalInputHandler{ private final Vector3f appliedForce = new Vector3f(); private Vector3f rotation; private Node ship; private DynamicPhysicsNode shipNode; private Camera cam; - public FirstPersonHandler(Node ship, DynamicPhysicsNode shipNode, Camera cam){ + public SGFirstPersonHandler(Node ship, DynamicPhysicsNode shipNode, Camera cam){ this.ship = ship; this.shipNode = shipNode; this.cam = cam; diff --git a/src/sg/input/SGThirdPersonHandler.java b/src/sg/input/SGThirdPersonHandler.java new file mode 100644 index 0000000..469a38e --- /dev/null +++ b/src/sg/input/SGThirdPersonHandler.java @@ -0,0 +1,78 @@ +package sg.input; + +import com.jme.input.ChaseCamera; +import com.jme.input.RelativeMouse; +import com.jme.input.action.InputActionEvent; +import com.jme.input.thirdperson.ThirdPersonMouseLook; +import com.jme.math.FastMath; +import com.jme.math.Quaternion; +import com.jme.math.Vector3f; +import com.jme.renderer.Camera; +import com.jme.scene.Node; +import com.jme.scene.Spatial; + + +public class SGThirdPersonHandler extends GlobalInputHandler{ + protected Camera cam; + protected Node ship; + + public SGThirdPersonHandler(Camera cam, Spatial ship){ + SGChaseCamera ccam = new SGChaseCamera(cam, ship); + this.addToAttachedHandlers(ccam); + } + + class SGChaseCamera extends ChaseCamera{ + public SGChaseCamera(Camera arg0, Spatial arg1) { + super(arg0, arg1); + } + + protected void setupMouse() { + RelativeMouse mouse = new RelativeMouse("Mouse Input"); + mouse.registerWithInputHandler(this); + + if (mouseLook != null) + removeAction(mouseLook); + + mouseLook = new SGThirdPersonMouseLook(mouse, this, target); + addAction(mouseLook); + } + + public SGThirdPersonMouseLook getTankMouseLook(){ + return (SGThirdPersonMouseLook) mouseLook; + } + } + + + class SGThirdPersonMouseLook extends ThirdPersonMouseLook{ + float targetTurnSpeed = 1f; + + public SGThirdPersonMouseLook(RelativeMouse mouse, ChaseCamera camera, Spatial target) { + super(mouse, camera, target); + } + + public void performAction(InputActionEvent event) { + super.performAction(event); + + //if (mouse.getLocalTranslation().x != 0 || mouse.getLocalTranslation().y != 0) { + Vector3f camL = camera.getCamera().getLocation(); + //center coordinates to spatial + float x = target.getLocalTranslation().x - camL.x; + float z = target.getLocalTranslation().z - camL.z; + float y = target.getLocalTranslation().y - camL.y; + float c = FastMath.sqrt(x*x + z*z); + + // Tower rotation + float angle = FastMath.acos(x/c); + if(camL.z < 0 && angle > 0) angle = -Math.abs(angle); + if(camL.z > 0 && angle < 0) angle = Math.abs(angle); + target.getLocalRotation().fromAngleAxis(angle+FastMath.PI, new Vector3f(0,0,1)); + + //gun rotation + c = FastMath.sqrt(x*x + y*y + z*z); + angle = FastMath.tan(y/c); + target.getLocalRotation().fromAngleAxis(angle+FastMath.PI/5, new Vector3f(0,1,0)); + //} + + } + } +} diff --git a/src/sg/input/ThirdPersonHandler.java b/src/sg/input/ThirdPersonHandler.java deleted file mode 100644 index 6957312..0000000 --- a/src/sg/input/ThirdPersonHandler.java +++ /dev/null @@ -1,14 +0,0 @@ -package sg.input; - -import com.jme.input.ChaseCamera; -import com.jme.renderer.Camera; -import com.jme.scene.Spatial; - - -public class ThirdPersonHandler extends GlobalInputHandler{ - - public ThirdPersonHandler(Camera cam, Spatial ship){ - ChaseCamera ccam = new ChaseCamera(cam, ship); - this.addToAttachedHandlers(ccam); - } -} diff --git a/src/sg/states/HudState.java b/src/sg/states/HudState.java index 191f0c7..7cd6f3e 100644 --- a/src/sg/states/HudState.java +++ b/src/sg/states/HudState.java @@ -1,6 +1,5 @@ package sg.states; -import java.awt.Font; import com.jme.math.Vector3f; import com.jme.renderer.Renderer; @@ -12,8 +11,6 @@ import com.jme.scene.state.ZBufferState; import com.jme.system.DisplaySystem; import com.jmex.font2d.Font2D; import com.jmex.font2d.Text2D; -import com.jmex.font3d.Font3D; -import com.jmex.font3d.Text3D; import com.jmex.game.state.BasicGameState; public class HudState extends BasicGameState{ diff --git a/src/sg/states/InGameState.java b/src/sg/states/InGameState.java index d5aa085..f214372 100644 --- a/src/sg/states/InGameState.java +++ b/src/sg/states/InGameState.java @@ -1,12 +1,11 @@ package sg.states; -import java.util.concurrent.Callable; import java.util.logging.Logger; import sg.env.Environment; +import sg.input.SGThirdPersonHandler; import sg.util.SGUtil; -import com.jme.input.FirstPersonHandler; import com.jme.input.InputHandler; import com.jme.light.PointLight; import com.jme.math.Vector3f; @@ -15,10 +14,8 @@ import com.jme.renderer.Renderer; import com.jme.scene.Node; import com.jme.scene.state.LightState; import com.jme.system.DisplaySystem; -import com.jme.util.GameTaskQueueManager; 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; @@ -38,23 +35,65 @@ public class InGameState extends CameraGameState { public InGameState(String name) { super(name); + + // Pass (render thingies) + //passManager = new BasicPassManager(); + + //physics + physicsSpace = PhysicsSpace.create(); + + physicsSpace.setDirectionalGravity(new Vector3f(0f, 0f, 0f)); + StaticPhysicsNode staticNode = physicsSpace.createStaticNode(); + DynamicPhysicsNode dynamicNode = physicsSpace.createDynamicNode(); + rootNode.attachChild(dynamicNode); + rootNode.attachChild(staticNode); + + // environment + environment = new Environment(rootNode); + rootNode.attachChild(environment); + + // ship + Node ship = SGUtil.loadModel("sg/data/models/ships/G6.3ds"); + ship.setLocalTranslation(new Vector3f( 0, 0, 40)); + rootNode.attachChild(ship); + + // inputs + input = new SGThirdPersonHandler( cam, ship ); + + // 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(); + + HudState hud = new HudState("hud"); + GameStateManager.getInstance().attachChild(hud); + + cam.setFrustumFar(5000); + + // ***************** Done loading ********************************** + setActive(true); + } - + public void setActive(boolean b){ - if(b){ // initiate the game - if(environment == null){ - GameTaskQueueManager.getManager().update(new LoadingTask(this)); - this.active = false; - } - else { // the game is already initaiated - this.active = true; - onActivate(); - GameStateManager.getInstance().activateChildNamed("hud"); - } + if(b){ + this.active = true; + super.setActive(b); + GameStateManager.getInstance().activateChildNamed("hud"); } else{ this.active = false; - onDeactivate(); + super.setActive(b); GameStateManager.getInstance().deactivateChildNamed("hud"); } } @@ -82,112 +121,5 @@ public class InGameState extends CameraGameState { /** Have the PassManager render. */ //passManager.renderPasses(r); } - - //**************************************************************************** - //************************** Loader ************************************* - - private class LoadingTask implements Callable{ - private static final int STEP_COUNT = 7; - private LoadingGameState loader; - private InGameState parentState; - private int progress; - public LoadingTask(InGameState parentState){ - this.parentState = parentState; - - loader = new LoadingGameState(STEP_COUNT+1); - GameStateManager.getInstance().attachChild(loader); - loader.setActive(true); - - progress = 0; - loader.increment("Loading Passes..."); - } - - public Void call() throws Exception { - // ***************** Load the Game ********************************** - switch(progress){ - case 0: - // Pass (render thingies) - //passManager = new BasicPassManager(); - - loader.increment("Loading Physics..."); - break; - case 1: - //physics - physicsSpace = PhysicsSpace.create(); - - physicsSpace.setDirectionalGravity(new Vector3f(0f, 0f, 0f)); - StaticPhysicsNode staticNode = physicsSpace.createStaticNode(); - DynamicPhysicsNode dynamicNode = physicsSpace.createDynamicNode(); - rootNode.attachChild(dynamicNode); - rootNode.attachChild(staticNode); - - loader.increment("Loading Environment..."); - break; - case 2: - // environment - environment = new Environment(rootNode); - rootNode.attachChild(environment); - - loader.increment("Loading Ships..."); - break; - case 3: - // ship - Node ship = SGUtil.loadModel("sg/data/models/ships/G6.3ds"); - ship.setLocalTranslation(new Vector3f( 0, 0, 40)); - rootNode.attachChild(ship); - - loader.increment("Loading Inputs..."); - break; - case 4: - // inputs - FirstPersonHandler first = new FirstPersonHandler( cam, 50, 1 ); - input = new InputHandler(); - input.addToAttachedHandlers( first ); - - loader.increment("Loading Lights..."); - break; - case 5: - // 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(); - - loader.increment("Loading Hud..."); - break; - case 6: - HudState hud = new HudState("hud"); - GameStateManager.getInstance().attachChild(hud); - - loader.increment("Loading Starting..."); - break; - case 7: - cam.setFrustumFar(5000); - - // ***************** Done loading ********************************** - loader.setActive(false); - GameStateManager.getInstance().detachChild(loader); - - parentState.setActive(true); - break; - } - - if (progress < STEP_COUNT) { - GameTaskQueueManager.getManager().update(this); - } - progress++; - - return null; - } - } }