diff options
author | Nao Pross <naopross@thearcway.org> | 2018-11-20 17:07:35 +0100 |
---|---|---|
committer | Nao Pross <naopross@thearcway.org> | 2018-11-20 17:07:35 +0100 |
commit | 235262919e0863215c2f58a4ab3b977f59442d05 (patch) | |
tree | 792b383ce57bf1e191d7cc21f2eb85d371ee5530 /src/subconscious/graphics/GameWindow.java | |
parent | Update gitignore (diff) | |
download | Subconscious-java-235262919e0863215c2f58a4ab3b977f59442d05.tar.gz Subconscious-java-235262919e0863215c2f58a4ab3b977f59442d05.zip |
Add Game class, fix GameWindow's scene loading / management
The new Game class contains the state of the game and the main
logic to manage maps, actors and scores.
Diffstat (limited to 'src/subconscious/graphics/GameWindow.java')
-rw-r--r-- | src/subconscious/graphics/GameWindow.java | 125 |
1 files changed, 88 insertions, 37 deletions
diff --git a/src/subconscious/graphics/GameWindow.java b/src/subconscious/graphics/GameWindow.java index f7459fd..ce99df5 100644 --- a/src/subconscious/graphics/GameWindow.java +++ b/src/subconscious/graphics/GameWindow.java @@ -1,5 +1,7 @@ package subconscious.graphics; +import subconscious.Game; + import java.awt.Dimension; import java.awt.GridLayout; import java.awt.CardLayout; @@ -12,6 +14,11 @@ import javax.swing.JButton; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; + +/* 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 static final Dimension WINDOW_SIZE = new Dimension(600, 400); @@ -24,26 +31,36 @@ public class GameWindow extends JFrame implements ActionListener { private Scene scene = null; private Thread sceneThread = null; + private Game game; + // TODO: remove map editor, start directly on Battle mode - public GameWindow() { + public GameWindow(Game g) { super("Subconscious"); + this.game = g; + + // set up JFrame this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setSize(WINDOW_SIZE); this.setPreferredSize(WINDOW_SIZE); this.setLocationRelativeTo(null); - this.initComponents(); + // set up components + this.buildUi(); this.add(this.root, BorderLayout.CENTER); this.pack(); this.setVisible(true); + + // start Window Loop + this.loop(); } - private void initComponents() { + 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)); @@ -68,43 +85,82 @@ public class GameWindow extends JFrame implements ActionListener { this.root.add(menu, MENU_CARD); } - public void showScene(Scene scene) { - // the current scene is already open - if (scene == this.scene) { - ((CardLayout)this.root.getLayout()).show(this.root, SCENE_CARD); - scene.updateCanvasSize(); - scene.resume(); + // ovserver of this.game + private void loop() { + while (this.game.isRunning()) { + this.game.waitStateChange(); - return; - } + switch (this.game.getState()) { + case MENU: + this.loadMenu(); + break; - // if there is an old scene - if (this.scene != null) { - // close old scene - this.scene.stop(); - try { - this.sceneThread.join(); - } catch (InterruptedException ex) { - ex.printStackTrace(); + case DREAM: + this.loadScene(this.scene); + break; } + + } + } + + 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 (this.scene != null){ + this.unloadScene(); } - // build new thread + // build thread this.scene = scene; this.sceneThread = new Thread(this.scene); + // for debugging + this.sceneThread.setName("Scene rendering Thread"); - // add to layout - this.root.add(scene, SCENE_CARD); - ((CardLayout)this.root.getLayout()).show(this.root, SCENE_CARD); + // add to UI + this.root.add(this.scene, SCENE_CARD); + ((CardLayout) this.root.getLayout()).show(this.root, SCENE_CARD); + // start scene this.sceneThread.start(); - scene.updateCanvasSize(); - scene.resume(); + this.scene.updateCanvasSize(this.getSize()); } - public void showMenu() { - this.scene.pause(); - ((CardLayout)this.root.getLayout()).show(this, MENU_CARD); + private void unloadScene() { + // close old scene + this.scene.stop(); + try { + // wait for thread to die + this.sceneThread.join(); + } catch (InterruptedException ex) { + ex.printStackTrace(); + } + + // remove card from UI + this.root.remove(scene); + + this.scene = null; + this.sceneThread = null; + } + + // TODO: menu should become a Scene + private void loadMenu() { + if (this.scene != null) + this.scene.pause(); + + ((CardLayout) this.root.getLayout()).show(this.root, MENU_CARD); + } + + public void quit() { + this.setVisible(false); + + if (this.scene != null) { + this.unloadScene(); + } + + this.dispose(); } // Action Listener for menu @@ -112,22 +168,17 @@ public class GameWindow extends JFrame implements ActionListener { public void actionPerformed(ActionEvent e) { if (e.getActionCommand().startsWith("btn")) { if (e.getActionCommand().equals("btn-exit")) { - this.setVisible(false); - this.dispose(); + this.quit(); return; } - Scene scene = null; - if (e.getActionCommand().equals("btn-editor")) { - scene = new MapEditorScene(); + this.loadScene(new MapEditorScene()); } else if (e.getActionCommand().equals("btn-battle")) { - scene = new BattleScene(); + this.loadScene(new BattleScene(this.game)); + this.game.start(); } - if (scene != null) { - showScene(scene); - } } } } |