summaryrefslogtreecommitdiffstats
path: root/src/subconscious/graphics/GameWindow.java
diff options
context:
space:
mode:
authorNao Pross <naopross@thearcway.org>2018-11-21 12:16:51 +0100
committerNao Pross <naopross@thearcway.org>2018-11-21 12:16:51 +0100
commit18077719f32071e614d3b4d01134d501796f571d (patch)
tree0148efb3f4bfea3c5951e9ca564faceff8208b3b /src/subconscious/graphics/GameWindow.java
parentGeneralize to use only Scenes, attempt to implement pause (diff)
downloadSubconscious-java-18077719f32071e614d3b4d01134d501796f571d.tar.gz
Subconscious-java-18077719f32071e614d3b4d01134d501796f571d.zip
Fix InvalidStateException at scene loading, add Scene.requestScene()
The internal game state management has also been changed for the new scene management in the graphics
Diffstat (limited to 'src/subconscious/graphics/GameWindow.java')
-rw-r--r--src/subconscious/graphics/GameWindow.java79
1 files changed, 43 insertions, 36 deletions
diff --git a/src/subconscious/graphics/GameWindow.java b/src/subconscious/graphics/GameWindow.java
index da69d40..8f24dac 100644
--- a/src/subconscious/graphics/GameWindow.java
+++ b/src/subconscious/graphics/GameWindow.java
@@ -32,7 +32,7 @@ public class GameWindow extends JFrame implements WindowListener {
// stack of loaded scenes in the background that are paused
private Stack<SimpleEntry<Scene, Thread>> loadedScenes = new Stack<>();
- private Game game;
+ private volatile Game game;
// TODO: remove map editor, start directly on Battle mode
public GameWindow(Game g) {
@@ -66,33 +66,39 @@ public class GameWindow extends JFrame implements WindowListener {
this.loadScene(new MenuScene(this.game, "mainmenu"));
while (this.game.isRunning()) {
- Game.State newState = this.game.waitStateChange();
-
- switch (newState) {
- case MENU:
- // unload all scenes to get the first one => menu
- this.unloadAllScenes();
- break;
-
- // TODO: rewrite the following part
- case BATTLE:
- this.loadScene(new BattleScene(this.game, "battle1"));
- break;
-
- case PAUSE:
- this.loadScene(new PauseScene(this.game, "pause"));
- try {
- this.sceneThread.join();
- } catch (InterruptedException ex) {
- ex.printStackTrace();
- }
- this.unloadScene();
- break;
+ // check if the scene has requested a new scene
+ if (this.scene.isRequestingScene()) {
+ this.loadScene(this.scene.getRequestedScene());
}
+
+ // check for game state change
+ if (this.game.stateChanged()) {
+ // Game.State newState = this.game.waitStateChange();
+ Game.State newState = this.game.getState();
+ Game.State lastState = this.game.getLastState();
+
+ if (lastState == newState) {
+ throw new IllegalStateException();
+ }
- }
+ // if unpaused unload PauseScene
+ if (lastState == Game.State.PAUSE) {
+ this.unloadScene();
+ }
- this.quit();
+ // MAIN_MENU is always the first scene
+ if (newState == Game.State.MAIN_MENU) {
+ this.unloadAllScenes();
+ }
+ // if paused load new PauseScene
+ else if (newState == Game.State.PAUSE) {
+ this.loadScene(new PauseScene(this.game, "pause"));
+ }
+ else if (newState == Game.State.CLOSING) {
+ this.quit();
+ }
+ }
+ }
}
private void loadScene(Scene scene) {
@@ -122,15 +128,16 @@ public class GameWindow extends JFrame implements WindowListener {
}
private void unloadAllScenes() {
- unloadScenes(this.loadedScenes.size() -1);
- }
-
- private void unloadScenes(int count) {
- for (int i = 0; i < count; i++) {
+ while (!this.loadedScenes.empty())
this.unloadScene();
- }
}
+ // private void unloadScenes(int count) {
+ // for (int i = 0; i < count; i++) {
+ // this.unloadScene();
+ // }
+ // }
+
private void unloadScene() {
// close old scene
this.scene.stop();
@@ -144,11 +151,8 @@ public class GameWindow extends JFrame implements WindowListener {
// remove card from UI
this.root.remove(scene);
- // TODO: this should probably be an error
if (this.loadedScenes.empty()) {
- this.scene = null;
- this.sceneThread = null;
- return;
+ throw new IllegalStateException();
}
// load the last scene
@@ -164,6 +168,8 @@ public class GameWindow extends JFrame implements WindowListener {
this.unloadAllScenes();
this.setVisible(false);
this.dispose();
+
+ System.exit(0);
}
/* window listener */
@@ -175,7 +181,8 @@ public class GameWindow extends JFrame implements WindowListener {
@Override
public void windowDeactivated(WindowEvent e) {
- this.game.pause();
+ if (this.game.getState() != Game.State.MAIN_MENU)
+ this.game.pause();
}
@Override public void windowDeiconified(WindowEvent e) {}