diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/subconscious/Game.java | 23 | ||||
-rw-r--r-- | src/subconscious/graphics/GameWindow.java | 31 | ||||
-rw-r--r-- | src/subconscious/graphics/MapScene.java | 2 | ||||
-rw-r--r-- | src/subconscious/graphics/MenuScene.java | 7 | ||||
-rw-r--r-- | src/subconscious/graphics/PauseScene.java | 14 | ||||
-rw-r--r-- | src/subconscious/graphics/Scene.java | 47 |
6 files changed, 73 insertions, 51 deletions
diff --git a/src/subconscious/Game.java b/src/subconscious/Game.java index 2361dd0..5c3d7d0 100644 --- a/src/subconscious/Game.java +++ b/src/subconscious/Game.java @@ -97,21 +97,20 @@ public class Game { // return this.state; // } - public void pause() { - if (this.state == State.PAUSE) - return; + // public void pause() { + // if (this.state == State.PAUSE) + // return; - this.lastState = this.state; - this.setState(State.PAUSE); - } + // this.setState(State.PAUSE); + // } - public void resume() { - this.setState(lastState); - } + // public void resume() { + // this.setState(lastState); + // } - public boolean isPaused() { - return this.state == State.PAUSE; - } + // public boolean isPaused() { + // return this.state == State.PAUSE; + // } /* methods to manage map and map loading */ public Map getMap() { diff --git a/src/subconscious/graphics/GameWindow.java b/src/subconscious/graphics/GameWindow.java index 8f24dac..65af798 100644 --- a/src/subconscious/graphics/GameWindow.java +++ b/src/subconscious/graphics/GameWindow.java @@ -63,14 +63,19 @@ public class GameWindow extends JFrame implements WindowListener { // ovserver of this.game private void loop() { // load the first scene - this.loadScene(new MenuScene(this.game, "mainmenu")); + this.loadScene(new MenuScene(this.game)); while (this.game.isRunning()) { // check if the scene has requested a new scene if (this.scene.isRequestingScene()) { this.loadScene(this.scene.getRequestedScene()); } - + + // check if the scene wants to die + if (this.scene.isRequestingPrevScene()) { + this.unloadScene(); + } + // check for game state change if (this.game.stateChanged()) { // Game.State newState = this.game.waitStateChange(); @@ -81,19 +86,13 @@ public class GameWindow extends JFrame implements WindowListener { throw new IllegalStateException(); } - // if unpaused unload PauseScene - if (lastState == Game.State.PAUSE) { - this.unloadScene(); - } - // 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.PAUSE) { + // this.scene.requestScene(new PauseScene(this.game, "from GameWindow")); + // } else if (newState == Game.State.CLOSING) { this.quit(); } @@ -109,6 +108,7 @@ public class GameWindow extends JFrame implements WindowListener { // if a scene is loaded push it onto the loadedScenes stack if (this.scene != null){ this.scene.pauseThread(); + this.scene.setVisible(false); this.loadedScenes.push(new SimpleEntry<Scene, Thread>(scene, sceneThread)); } @@ -122,7 +122,7 @@ public class GameWindow extends JFrame implements WindowListener { // start scene this.sceneThread = new Thread(this.scene); // for debugging - this.sceneThread.setName("Scene [" + scene.UNIQUE_NAME + "]"); + this.sceneThread.setName(this.scene.UNIQUE_NAME); this.sceneThread.start(); this.scene.updateCanvasSize(this.getSize()); } @@ -149,7 +149,8 @@ public class GameWindow extends JFrame implements WindowListener { } // remove card from UI - this.root.remove(scene); + this.root.getLayout().removeLayoutComponent(this.scene); + this.root.remove(this.scene); if (this.loadedScenes.empty()) { throw new IllegalStateException(); @@ -160,6 +161,7 @@ public class GameWindow extends JFrame implements WindowListener { this.scene = sceneEntry.getKey(); this.sceneThread = sceneEntry.getValue(); + this.scene.setVisible(true); ((CardLayout) this.root.getLayout()).show(this.root, this.scene.UNIQUE_NAME); this.scene.resumeThread(); } @@ -181,8 +183,7 @@ public class GameWindow extends JFrame implements WindowListener { @Override public void windowDeactivated(WindowEvent e) { - if (this.game.getState() != Game.State.MAIN_MENU) - this.game.pause(); + // this.game.pause(); } @Override public void windowDeiconified(WindowEvent e) {} diff --git a/src/subconscious/graphics/MapScene.java b/src/subconscious/graphics/MapScene.java index ed58506..e17a933 100644 --- a/src/subconscious/graphics/MapScene.java +++ b/src/subconscious/graphics/MapScene.java @@ -242,7 +242,7 @@ public abstract class MapScene extends Scene { break; case KeyEvent.VK_ESCAPE: - this.game.pause(); + // this.game.pause(); break; // TODO: remove diff --git a/src/subconscious/graphics/MenuScene.java b/src/subconscious/graphics/MenuScene.java index ac38d10..1a7e7f2 100644 --- a/src/subconscious/graphics/MenuScene.java +++ b/src/subconscious/graphics/MenuScene.java @@ -17,6 +17,11 @@ import javax.swing.JButton; @SuppressWarnings("serial") public class MenuScene extends Scene implements ActionListener { + + public MenuScene(Game g) { + this(g, ""); + } + public MenuScene(Game g, String uniqueName) { super(g, uniqueName); @@ -63,7 +68,7 @@ public class MenuScene extends Scene implements ActionListener { } else if (e.getActionCommand().equals("btn-battle")) { this.game.start(); // TODO: change - this.requestScene(new BattleScene(this.game, "demobattle")); + this.requestScene(new BattleScene(this.game, "demo")); } } diff --git a/src/subconscious/graphics/PauseScene.java b/src/subconscious/graphics/PauseScene.java index 2b5f411..85afc51 100644 --- a/src/subconscious/graphics/PauseScene.java +++ b/src/subconscious/graphics/PauseScene.java @@ -10,8 +10,13 @@ import java.awt.event.KeyEvent; @SuppressWarnings("serial") public class PauseScene extends Scene { - public PauseScene(Game g, String uniqueName) { - super(g, uniqueName); + + public PauseScene(Game g) { + this(g, ""); + } + + public PauseScene(Game g, String label) { + super(g, label); // TODO: build on upper level this.add(this.canvas, BorderLayout.CENTER); @@ -27,7 +32,7 @@ public class PauseScene extends Scene { } protected void update(long deltaNanoTime) { - + } @@ -36,7 +41,8 @@ public class PauseScene extends Scene { super.keyPressed(e); if (e.getKeyCode() == KeyEvent.VK_ESCAPE) { - this.game.resume(); + this.requestPrevScene(); + // this.game.resume(); } } }
\ No newline at end of file diff --git a/src/subconscious/graphics/Scene.java b/src/subconscious/graphics/Scene.java index 7b54a1a..9d11707 100644 --- a/src/subconscious/graphics/Scene.java +++ b/src/subconscious/graphics/Scene.java @@ -38,6 +38,7 @@ public abstract class Scene extends JPanel // this is required to manage multiple scenes public final String UNIQUE_NAME; + private static int absScenesCount = 0; protected final long DESIRED_FPS = 60; protected final long DESIRED_DELTA_LOOP = (1000*1000*1000)/DESIRED_FPS; @@ -47,35 +48,31 @@ public abstract class Scene extends JPanel // TODO: this could become a Queue of scenes to load protected Scene requestedScene = null; + protected boolean requestedPrevScene = false; - protected volatile boolean running; - protected volatile boolean threadPaused; - protected Object pauseLock; + protected volatile boolean running = true; + protected volatile boolean threadPaused = false; + protected Object pauseLock = new Object(); - protected volatile Dimension canvasSize; + protected volatile Dimension canvasSize = GameWindow.WINDOW_SIZE; + protected Canvas canvas = new Canvas(); protected BufferStrategy buffer; - protected Canvas canvas; // TODO: make accessible from user settings protected int guiSize = 2; - public Scene(Game game, String uniqueName) { + public Scene(Game game, String label) { + // generate unique name + String uniqueName = this.getClass().getCanonicalName() + Integer.toString(Scene.absScenesCount); + Scene.absScenesCount++; - this.UNIQUE_NAME = uniqueName; + this.UNIQUE_NAME = uniqueName + " (" + label + ")"; this.game = game; - // thread control - this.running = false; - this.threadPaused = false; - this.pauseLock = new Object(); - // watch for resize this.addComponentListener(this); // set up canvas - this.canvasSize = GameWindow.WINDOW_SIZE; - this.canvas = new Canvas(); - this.canvas.setBounds(0, 0, this.canvasSize.width, this.canvasSize.height); this.canvas.setIgnoreRepaint(true); @@ -89,6 +86,10 @@ public abstract class Scene extends JPanel this.build(); } + public Scene(Game game) { + this(game, ""); + } + /* abstract methods */ // runs when the the scene thread starts protected abstract void build(); @@ -97,18 +98,28 @@ public abstract class Scene extends JPanel protected abstract void render(Graphics2D g); /* request scenes */ - protected void requestScene(Scene sc) { + protected synchronized void requestPrevScene() { + this.requestedPrevScene = true; + } + + protected synchronized boolean isRequestingPrevScene() { + return this.requestedPrevScene; + } + + protected synchronized void requestScene(Scene sc) { if (this.requestedScene != null) throw new UnsupportedOperationException("only one scene can be requested at once"); this.requestedScene = sc; } - public boolean isRequestingScene() { + // TODO: protected synchronized void requestScene(String uniqueName) {} + + public synchronized boolean isRequestingScene() { return this.requestedScene != null; } - public Scene getRequestedScene() { + public synchronized Scene getRequestedScene() { Scene requested = this.requestedScene; this.requestedScene = null; |