No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

207 líneas
6.4 KiB

  1. package roadtrip;
  2. import com.jme3.app.Application;
  3. import com.jme3.app.DebugKeysAppState;
  4. import com.jme3.app.StatsAppState;
  5. import com.jme3.app.state.AppState;
  6. import com.jme3.font.BitmapFont;
  7. import com.jme3.font.BitmapText;
  8. import com.jme3.input.controls.ActionListener;
  9. import com.jme3.input.controls.KeyTrigger;
  10. import com.jme3.input.controls.Trigger;
  11. import com.jme3.renderer.RenderManager;
  12. import com.jme3.renderer.queue.RenderQueue;
  13. import com.jme3.scene.Node;
  14. import com.jme3.scene.Spatial;
  15. import com.jme3.system.AppSettings;
  16. import com.jme3.system.JmeContext;
  17. import com.jme3.system.JmeSystem;
  18. /**
  19. * Cloned from SimpleApplication.
  20. *
  21. * Created by dejvino on 15.01.2017.
  22. */
  23. public abstract class NotSoSimpleApplication extends Application
  24. {
  25. protected Node rootNode;
  26. protected Node guiNode;
  27. protected BitmapText fpsText;
  28. protected BitmapFont guiFont;
  29. protected boolean showSettings;
  30. private AppActionListener actionListener;
  31. public NotSoSimpleApplication(AppState... initialStates) {
  32. this.rootNode = new Node("Root Node");
  33. this.guiNode = new Node("Gui Node");
  34. this.showSettings = true;
  35. this.actionListener = new AppActionListener();
  36. attachStates(initialStates);
  37. }
  38. public final void attachStates(AppState... states)
  39. {
  40. if(states != null) {
  41. AppState[] arr$ = states;
  42. int len$ = states.length;
  43. for(int i$ = 0; i$ < len$; ++i$) {
  44. AppState a = arr$[i$];
  45. if(a != null) {
  46. this.stateManager.attach(a);
  47. }
  48. }
  49. }
  50. }
  51. public void attachDebugStates()
  52. {
  53. attachStates(new StatsAppState(guiNode, guiFont), new DebugKeysAppState());
  54. }
  55. @Override
  56. public void start() {
  57. boolean loadSettings = false;
  58. if(this.settings == null) {
  59. this.setSettings(new AppSettings(true));
  60. loadSettings = true;
  61. }
  62. if(!this.showSettings || JmeSystem.showSettingsDialog(this.settings, loadSettings)) {
  63. this.setSettings(this.settings);
  64. super.start();
  65. }
  66. }
  67. public Node getGuiNode() {
  68. return this.guiNode;
  69. }
  70. public Node getRootNode() {
  71. return this.rootNode;
  72. }
  73. public boolean isShowSettings() {
  74. return this.showSettings;
  75. }
  76. public void setShowSettings(boolean showSettings) {
  77. this.showSettings = showSettings;
  78. }
  79. protected BitmapFont loadGuiFont() {
  80. return this.assetManager.loadFont("Interface/Fonts/Default.fnt");
  81. }
  82. @Override
  83. public void initialize() {
  84. super.initialize();
  85. this.guiFont = this.loadGuiFont();
  86. this.guiNode.setQueueBucket(RenderQueue.Bucket.Gui);
  87. this.guiNode.setCullHint(Spatial.CullHint.Never);
  88. this.viewPort.attachScene(this.rootNode);
  89. this.guiViewPort.attachScene(this.guiNode);
  90. if(this.inputManager != null) {
  91. if(this.context.getType() == JmeContext.Type.Display) {
  92. this.inputManager.addMapping("SIMPLEAPP_Exit", new Trigger[]{new KeyTrigger(1)});
  93. }
  94. if(this.stateManager.getState(StatsAppState.class) != null) {
  95. this.inputManager.addMapping("SIMPLEAPP_HideStats", new Trigger[]{new KeyTrigger(63)});
  96. this.inputManager.addListener(this.actionListener, new String[]{"SIMPLEAPP_HideStats"});
  97. }
  98. this.inputManager.addListener(this.actionListener, new String[]{"SIMPLEAPP_Exit"});
  99. }
  100. if(this.stateManager.getState(StatsAppState.class) != null) {
  101. StatsAppState sas = (StatsAppState)this.stateManager.getState(StatsAppState.class);
  102. sas.setFont(this.guiFont);
  103. this.fpsText = sas.getFpsText();
  104. }
  105. this.initializeGame();
  106. }
  107. @Override
  108. public void update() {
  109. super.update();
  110. if (this.speed > 0.0f && !this.paused) {
  111. float tpf = this.timer.getTimePerFrame() * this.speed;
  112. updateStates(tpf);
  113. updateGame(tpf);
  114. updateLogicalState(tpf);
  115. updateGeometricState();
  116. updatePreRender(tpf);
  117. updateRender(tpf);
  118. updatePostRender(this.renderManager);
  119. updateStatesPostRender();
  120. }
  121. }
  122. protected void updateStates(float tpf) {
  123. this.stateManager.update(tpf);
  124. }
  125. public void updateGame(float tpf) {
  126. }
  127. protected void updateLogicalState(float tpf) {
  128. this.rootNode.updateLogicalState(tpf);
  129. this.guiNode.updateLogicalState(tpf);
  130. }
  131. protected void updateGeometricState() {
  132. this.rootNode.updateGeometricState();
  133. this.guiNode.updateGeometricState();
  134. }
  135. protected void updatePreRender(float tpf) {
  136. }
  137. protected void updateRender(float tpf) {
  138. this.stateManager.render(this.renderManager);
  139. this.renderManager.render(tpf, this.context.isRenderable());
  140. }
  141. public void updatePostRender(RenderManager rm) {
  142. }
  143. protected void updateStatesPostRender() {
  144. this.stateManager.postRender();
  145. }
  146. public void setDisplayFps(boolean show) {
  147. if(this.stateManager.getState(StatsAppState.class) != null) {
  148. ((StatsAppState)this.stateManager.getState(StatsAppState.class)).setDisplayFps(show);
  149. }
  150. }
  151. public void setDisplayStatView(boolean show) {
  152. if(this.stateManager.getState(StatsAppState.class) != null) {
  153. ((StatsAppState)this.stateManager.getState(StatsAppState.class)).setDisplayStatView(show);
  154. }
  155. }
  156. public abstract void initializeGame();
  157. private class AppActionListener implements ActionListener {
  158. private AppActionListener() {
  159. }
  160. public void onAction(String name, boolean value, float tpf) {
  161. if(value) {
  162. if(name.equals("SIMPLEAPP_Exit")) {
  163. NotSoSimpleApplication.this.stop();
  164. } else if(name.equals("SIMPLEAPP_HideStats") && NotSoSimpleApplication.this.stateManager.getState(StatsAppState.class) != null) {
  165. ((StatsAppState) NotSoSimpleApplication.this.stateManager.getState(StatsAppState.class)).toggleStats();
  166. }
  167. }
  168. }
  169. }
  170. }