diff options
author | Nao Pross <naopross@thearcway.org> | 2018-11-21 00:46:06 +0100 |
---|---|---|
committer | Nao Pross <naopross@thearcway.org> | 2018-11-21 00:57:40 +0100 |
commit | 6423b1c0b1034e881fdab0b0f62df8019078a76e (patch) | |
tree | c006d68f2dcad47473e0d9f9aa8e50e432177e16 /src/subconscious/graphics/GameWindow.java | |
parent | Add res folder for resources (diff) | |
download | Subconscious-java-6423b1c0b1034e881fdab0b0f62df8019078a76e.tar.gz Subconscious-java-6423b1c0b1034e881fdab0b0f62df8019078a76e.zip |
Generalize to use only Scenes, attempt to implement pause
Compiles, but its totally broken
Diffstat (limited to 'src/subconscious/graphics/GameWindow.java')
-rw-r--r-- | src/subconscious/graphics/GameWindow.java | 147 |
1 files changed, 73 insertions, 74 deletions
diff --git a/src/subconscious/graphics/GameWindow.java b/src/subconscious/graphics/GameWindow.java index b0f48fd..da69d40 100644 --- a/src/subconscious/graphics/GameWindow.java +++ b/src/subconscious/graphics/GameWindow.java @@ -2,8 +2,10 @@ package subconscious.graphics; import subconscious.Game; +import java.util.Stack; +import java.util.AbstractMap.SimpleEntry; + import java.awt.Dimension; -import java.awt.GridLayout; import java.awt.CardLayout; import java.awt.BorderLayout; @@ -11,24 +13,24 @@ import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JButton; -import java.awt.event.ActionEvent; -import java.awt.event.ActionListener; +import java.awt.event.WindowListener; +import java.awt.event.WindowEvent; /* GameWindow * This class manages the graphical part of the game, such as loading and * unloading scenes, and the window itself */ @SuppressWarnings("serial") -public class GameWindow extends JFrame implements ActionListener { +public class GameWindow extends JFrame implements WindowListener { public static final Dimension WINDOW_SIZE = new Dimension(600, 400); - public final String MENU_CARD = "menu"; - public final String SCENE_CARD = "scene"; - private JPanel root; + // reference to the current scene which is NOT on the stack private Scene scene = null; private Thread sceneThread = null; + // stack of loaded scenes in the background that are paused + private Stack<SimpleEntry<Scene, Thread>> loadedScenes = new Stack<>(); private Game game; @@ -44,8 +46,11 @@ public class GameWindow extends JFrame implements ActionListener { this.setPreferredSize(WINDOW_SIZE); this.setLocationRelativeTo(null); + this.addWindowListener(this); + // set up components - this.buildUi(); + // set up a cardlayout + this.root = new JPanel(new CardLayout()); this.add(this.root, BorderLayout.CENTER); this.pack(); @@ -55,47 +60,33 @@ public class GameWindow extends JFrame implements ActionListener { this.loop(); } - private void buildUi() { - // set up a cardlayout - this.root = new JPanel(new CardLayout()); - - // TODO: menu should be a Scene - // build Menu card - JPanel menu = new JPanel(); - menu.setLayout(new GridLayout(3, 1)); - - JButton editor = new JButton("Editor"); - editor.setActionCommand("btn-editor"); - editor.addActionListener(this); - - JButton battle = new JButton("Battle"); - battle.setActionCommand("btn-battle"); - battle.addActionListener(this); - - JButton exit = new JButton("Exit"); - exit.setActionCommand("btn-exit"); - exit.addActionListener(this); - - menu.add(editor); - menu.add(battle); - menu.add(exit); - - // add cards - this.root.add(menu, MENU_CARD); - } - // ovserver of this.game private void loop() { + // load the first scene + this.loadScene(new MenuScene(this.game, "mainmenu")); + while (this.game.isRunning()) { Game.State newState = this.game.waitStateChange(); switch (newState) { case MENU: - this.loadMenu(); + // unload all scenes to get the first one => menu + this.unloadAllScenes(); break; - case DREAM: - this.loadScene(this.scene); + // 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; } @@ -106,29 +97,40 @@ public class GameWindow extends JFrame implements ActionListener { private void loadScene(Scene scene) { // check to not reload the same scene - // TODO: this may not be necessary, ex reaload scene? if (this.scene == scene) return; + // if a scene is loaded push it onto the loadedScenes stack if (this.scene != null){ - this.unloadScene(); + this.scene.pauseThread(); + this.loadedScenes.push(new SimpleEntry<Scene, Thread>(scene, sceneThread)); } // build thread this.scene = scene; // add to UI - this.root.add(this.scene, SCENE_CARD); - ((CardLayout) this.root.getLayout()).show(this.root, SCENE_CARD); + this.root.add(this.scene, this.scene.UNIQUE_NAME); + ((CardLayout) this.root.getLayout()).show(this.root, this.scene.UNIQUE_NAME); // start scene this.sceneThread = new Thread(this.scene); // for debugging - this.sceneThread.setName("Scene rendering Thread"); + this.sceneThread.setName("Scene [" + scene.UNIQUE_NAME + "]"); this.sceneThread.start(); this.scene.updateCanvasSize(this.getSize()); } + private void unloadAllScenes() { + unloadScenes(this.loadedScenes.size() -1); + } + + private void unloadScenes(int count) { + for (int i = 0; i < count; i++) { + this.unloadScene(); + } + } + private void unloadScene() { // close old scene this.scene.stop(); @@ -142,44 +144,41 @@ public class GameWindow extends JFrame implements ActionListener { // remove card from UI this.root.remove(scene); - this.scene = null; - this.sceneThread = null; - } + // TODO: this should probably be an error + if (this.loadedScenes.empty()) { + this.scene = null; + this.sceneThread = null; + return; + } - // TODO: menu should become a Scene - private void loadMenu() { - if (this.scene != null) - this.scene.pause(); + // load the last scene + SimpleEntry<Scene, Thread> sceneEntry = this.loadedScenes.pop(); + this.scene = sceneEntry.getKey(); + this.sceneThread = sceneEntry.getValue(); - ((CardLayout) this.root.getLayout()).show(this.root, MENU_CARD); + ((CardLayout) this.root.getLayout()).show(this.root, this.scene.UNIQUE_NAME); + this.scene.resumeThread(); } public void quit() { + this.unloadAllScenes(); this.setVisible(false); - - if (this.scene != null) { - this.unloadScene(); - } - this.dispose(); } - // Action Listener for menu - @Override - public void actionPerformed(ActionEvent e) { - if (e.getActionCommand().startsWith("btn")) { - if (e.getActionCommand().equals("btn-exit")) { - this.game.quit(); - return; - } - - if (e.getActionCommand().equals("btn-editor")) { - this.loadScene(new MapEditorScene()); - } else if (e.getActionCommand().equals("btn-battle")) { - this.loadScene(new BattleScene(this.game)); - this.game.start(); - } + /* window listener */ + @Override public void windowActivated(WindowEvent e) {} + @Override public void windowClosed(WindowEvent e) {} + @Override public void windowClosing(WindowEvent e) { + this.quit(); + } - } + @Override + public void windowDeactivated(WindowEvent e) { + this.game.pause(); } + + @Override public void windowDeiconified(WindowEvent e) {} + @Override public void windowIconified(WindowEvent e) {} + @Override public void windowOpened(WindowEvent e) {} } |