1
0
mirror of https://github.com/Dejvino/roadtrip synced 2024-11-21 15:32:35 +00:00

Added game pause functionality.

This commit is contained in:
Dejvino 2017-01-17 00:51:29 +01:00
parent 3c104a3d38
commit 39ac6164df
3 changed files with 265 additions and 23 deletions

View File

@ -0,0 +1,46 @@
package roadtrip;
import com.jme3.app.state.AppState;
import com.jme3.bullet.BulletAppState;
import com.jme3.bullet.PhysicsSpace;
/**
* Created by dejvino on 15.01.2017.
*/
public abstract class GameApplication extends NotSoSimpleApplication
{
protected boolean gamePaused = false;
protected BulletAppState bulletAppState;
public GameApplication() {
}
public GameApplication(AppState... initialStates) {
super(initialStates);
attachDebugStates();
}
@Override
public void initializeGame() {
bulletAppState = new BulletAppState();
stateManager.attach(bulletAppState);
}
protected PhysicsSpace getPhysicsSpace(){
return bulletAppState.getPhysicsSpace();
}
public void setGamePaused(boolean paused)
{
boolean changed = (gamePaused != paused);
if (changed) {
gamePaused = paused;
onGamePause(paused);
}
}
protected void onGamePause(boolean paused)
{
bulletAppState.setEnabled(!paused);
}
}

View File

@ -0,0 +1,206 @@
package roadtrip;
import com.jme3.app.Application;
import com.jme3.app.DebugKeysAppState;
import com.jme3.app.StatsAppState;
import com.jme3.app.state.AppState;
import com.jme3.font.BitmapFont;
import com.jme3.font.BitmapText;
import com.jme3.input.controls.ActionListener;
import com.jme3.input.controls.KeyTrigger;
import com.jme3.input.controls.Trigger;
import com.jme3.renderer.RenderManager;
import com.jme3.renderer.queue.RenderQueue;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
import com.jme3.system.AppSettings;
import com.jme3.system.JmeContext;
import com.jme3.system.JmeSystem;
/**
* Cloned from SimpleApplication.
*
* Created by dejvino on 15.01.2017.
*/
public abstract class NotSoSimpleApplication extends Application
{
protected Node rootNode;
protected Node guiNode;
protected BitmapText fpsText;
protected BitmapFont guiFont;
protected boolean showSettings;
private AppActionListener actionListener;
public NotSoSimpleApplication(AppState... initialStates) {
this.rootNode = new Node("Root Node");
this.guiNode = new Node("Gui Node");
this.showSettings = true;
this.actionListener = new AppActionListener();
attachStates(initialStates);
}
public final void attachStates(AppState... states)
{
if(states != null) {
AppState[] arr$ = states;
int len$ = states.length;
for(int i$ = 0; i$ < len$; ++i$) {
AppState a = arr$[i$];
if(a != null) {
this.stateManager.attach(a);
}
}
}
}
public void attachDebugStates()
{
attachStates(new StatsAppState(guiNode, guiFont), new DebugKeysAppState());
}
@Override
public void start() {
boolean loadSettings = false;
if(this.settings == null) {
this.setSettings(new AppSettings(true));
loadSettings = true;
}
if(!this.showSettings || JmeSystem.showSettingsDialog(this.settings, loadSettings)) {
this.setSettings(this.settings);
super.start();
}
}
public Node getGuiNode() {
return this.guiNode;
}
public Node getRootNode() {
return this.rootNode;
}
public boolean isShowSettings() {
return this.showSettings;
}
public void setShowSettings(boolean showSettings) {
this.showSettings = showSettings;
}
protected BitmapFont loadGuiFont() {
return this.assetManager.loadFont("Interface/Fonts/Default.fnt");
}
@Override
public void initialize() {
super.initialize();
this.guiFont = this.loadGuiFont();
this.guiNode.setQueueBucket(RenderQueue.Bucket.Gui);
this.guiNode.setCullHint(Spatial.CullHint.Never);
this.viewPort.attachScene(this.rootNode);
this.guiViewPort.attachScene(this.guiNode);
if(this.inputManager != null) {
if(this.context.getType() == JmeContext.Type.Display) {
this.inputManager.addMapping("SIMPLEAPP_Exit", new Trigger[]{new KeyTrigger(1)});
}
if(this.stateManager.getState(StatsAppState.class) != null) {
this.inputManager.addMapping("SIMPLEAPP_HideStats", new Trigger[]{new KeyTrigger(63)});
this.inputManager.addListener(this.actionListener, new String[]{"SIMPLEAPP_HideStats"});
}
this.inputManager.addListener(this.actionListener, new String[]{"SIMPLEAPP_Exit"});
}
if(this.stateManager.getState(StatsAppState.class) != null) {
StatsAppState sas = (StatsAppState)this.stateManager.getState(StatsAppState.class);
sas.setFont(this.guiFont);
this.fpsText = sas.getFpsText();
}
this.initializeGame();
}
@Override
public void update() {
super.update();
if (this.speed > 0.0f && !this.paused) {
float tpf = this.timer.getTimePerFrame() * this.speed;
updateStates(tpf);
updateGame(tpf);
updateLogicalState(tpf);
updateGeometricState();
updatePreRender(tpf);
updateRender(tpf);
updatePostRender(this.renderManager);
updateStatesPostRender();
}
}
protected void updateStates(float tpf) {
this.stateManager.update(tpf);
}
public void updateGame(float tpf) {
}
protected void updateLogicalState(float tpf) {
this.rootNode.updateLogicalState(tpf);
this.guiNode.updateLogicalState(tpf);
}
protected void updateGeometricState() {
this.rootNode.updateGeometricState();
this.guiNode.updateGeometricState();
}
protected void updatePreRender(float tpf) {
}
protected void updateRender(float tpf) {
this.stateManager.render(this.renderManager);
this.renderManager.render(tpf, this.context.isRenderable());
}
public void updatePostRender(RenderManager rm) {
}
protected void updateStatesPostRender() {
this.stateManager.postRender();
}
public void setDisplayFps(boolean show) {
if(this.stateManager.getState(StatsAppState.class) != null) {
((StatsAppState)this.stateManager.getState(StatsAppState.class)).setDisplayFps(show);
}
}
public void setDisplayStatView(boolean show) {
if(this.stateManager.getState(StatsAppState.class) != null) {
((StatsAppState)this.stateManager.getState(StatsAppState.class)).setDisplayStatView(show);
}
}
public abstract void initializeGame();
private class AppActionListener implements ActionListener {
private AppActionListener() {
}
public void onAction(String name, boolean value, float tpf) {
if(value) {
if(name.equals("SIMPLEAPP_Exit")) {
NotSoSimpleApplication.this.stop();
} else if(name.equals("SIMPLEAPP_HideStats") && NotSoSimpleApplication.this.stateManager.getState(StatsAppState.class) != null) {
((StatsAppState) NotSoSimpleApplication.this.stateManager.getState(StatsAppState.class)).toggleStats();
}
}
}
}
}

View File

@ -1,10 +1,7 @@
package roadtrip;
import com.jme3.app.SimpleApplication;
import com.jme3.audio.AudioNode;
import com.jme3.audio.AudioSource.Status;
import com.jme3.bullet.BulletAppState;
import com.jme3.bullet.PhysicsSpace;
import com.jme3.bullet.collision.shapes.BoxCollisionShape;
import com.jme3.bullet.collision.shapes.CompoundCollisionShape;
import com.jme3.bullet.collision.shapes.HeightfieldCollisionShape;
@ -43,7 +40,7 @@ import roadtrip.view.model.Player;
*
* @author dejvino
*/
public class RoadTrip extends SimpleApplication implements ActionListener {
public class RoadTrip extends GameApplication implements ActionListener {
public static void main(String[] args) {
RoadTrip app = new RoadTrip();
@ -52,8 +49,6 @@ public class RoadTrip extends SimpleApplication implements ActionListener {
public static boolean DEBUG = false;//true;
private BulletAppState bulletAppState;
private GameWorldState gameWorldState;
private GameWorldView gameWorldView;
@ -67,20 +62,13 @@ public class RoadTrip extends SimpleApplication implements ActionListener {
float inputTurning;
float inputAccel;
private PhysicsSpace getPhysicsSpace(){
return bulletAppState.getPhysicsSpace();
}
@Override
public void simpleInitApp() {
bulletAppState = new BulletAppState();
stateManager.attach(bulletAppState);
if (DEBUG) bulletAppState.getPhysicsSpace().enableDebug(assetManager);
PhysicsTestHelper.createPhysicsTestWorld(rootNode, assetManager, bulletAppState.getPhysicsSpace());
public void initializeGame() {
super.initializeGame();
setupKeys();
//audioRenderer.setEnvironment(Environment.Dungeon);
//AL10.alDistanceModel(AL11.AL_EXPONENT_DISTANCE);
@ -104,13 +92,13 @@ public class RoadTrip extends SimpleApplication implements ActionListener {
quad.removeControl(RigidBodyControl.class);
}
quad.addControl(new RigidBodyControl(new HeightfieldCollisionShape(quad.getHeightMap(), gameWorldView.terrain.terrainGrid.getLocalScale()), 0));
bulletAppState.getPhysicsSpace().add(quad);
getPhysicsSpace().add(quad);
}
@Override
public void tileDetached(Vector3f cell, TerrainQuad quad) {
if (quad.getControl(RigidBodyControl.class) != null) {
bulletAppState.getPhysicsSpace().remove(quad);
getPhysicsSpace().remove(quad);
quad.removeControl(RigidBodyControl.class);
}
}
@ -150,6 +138,7 @@ public class RoadTrip extends SimpleApplication implements ActionListener {
inputManager.addMapping("Space", new KeyTrigger(KeyInput.KEY_SPACE));
inputManager.addMapping("Reset", new KeyTrigger(KeyInput.KEY_RETURN));
inputManager.addMapping("Esc", new KeyTrigger(KeyInput.KEY_ESCAPE));
inputManager.addMapping("Pause", new KeyTrigger(KeyInput.KEY_P));
inputManager.addListener(this, "Lefts");
inputManager.addListener(this, "Rights");
inputManager.addListener(this, "Ups");
@ -158,6 +147,7 @@ public class RoadTrip extends SimpleApplication implements ActionListener {
inputManager.addListener(this, "Space");
inputManager.addListener(this, "Reset");
inputManager.addListener(this, "Esc");
inputManager.addListener(this, "Pause");
}
private void addCar()
@ -424,10 +414,8 @@ public class RoadTrip extends SimpleApplication implements ActionListener {
rootNode.attachChild(compassNode);
}
@Override
public void simpleUpdate(float tpf) {
public void updateGame(float tpf) {
Vector3f playerLocation = player.node.getWorldTranslation();
Vector3f newLocation = new Vector3f(playerLocation).add(new Vector3f(-1f, 1.5f, 2.4f).mult(20f));
@ -622,6 +610,8 @@ public class RoadTrip extends SimpleApplication implements ActionListener {
}
if (binding.equals("Esc")) {
stop();
} else if (binding.equals("Pause")) {
setGamePaused(value);
}
}
}