From c074c86b0c4a0c6be75c023c2c7cd975ef5cab47 Mon Sep 17 00:00:00 2001 From: Nao Pross Date: Wed, 12 Dec 2018 03:10:38 +0100 Subject: Minor access and synchronization corrections --- src/subconscious/Game.java | 24 ++++++++++++------------ src/subconscious/graphics/BattleScene.java | 4 ++-- src/subconscious/graphics/GameWindow.java | 3 +-- src/subconscious/graphics/MapScene.java | 4 ++-- src/subconscious/graphics/Scene.java | 23 +++++++++++++++-------- src/subconscious/graphics/WorldScene.java | 4 ++-- 6 files changed, 34 insertions(+), 28 deletions(-) diff --git a/src/subconscious/Game.java b/src/subconscious/Game.java index 7c7893a..36e1702 100644 --- a/src/subconscious/Game.java +++ b/src/subconscious/Game.java @@ -118,17 +118,17 @@ public class Game { } /* thread pause controls */ - public void stop() { + public synchronized void stop() { // stop thread even if paused this.resumeThread(); this.running = false; } - public void pauseThread() { + public synchronized void pauseThread() { this.threadPaused = true; } - public void resumeThread() { + public synchronized void resumeThread() { if (!this.threadPaused) return; @@ -148,7 +148,7 @@ public class Game { } /* methods to manage the state */ - public void setState(State state) { + public synchronized void setState(State state) { if (state == this.state) return; @@ -161,16 +161,16 @@ public class Game { this.state = state; } - public State getState() { + public synchronized State getState() { this.stateChanged = false; return this.state; } - public State getLastState() { + public synchronized State getLastState() { return this.lastState; } - public boolean stateChanged() { + public synchronized boolean stateChanged() { return this.stateChanged; } @@ -203,21 +203,21 @@ public class Game { // } /* methods to manage map and map loading */ - public Map getMap() { + public synchronized Map getMap() { return currentMap; } /* accessors */ - public boolean isRunning() { + public synchronized boolean isRunning() { return running; } - public void quit() { + public synchronized void quit() { // TODO: change to MAIN_MENU? this.setState(State.CLOSING); this.running = false; } - public boolean isGameOver() { return gameOver; } - public void gameOver() { this.gameOver = true; } + public synchronized boolean isGameOver() { return gameOver; } + public synchronized void gameOver() { this.gameOver = true; } } \ No newline at end of file diff --git a/src/subconscious/graphics/BattleScene.java b/src/subconscious/graphics/BattleScene.java index c8dad2e..080e746 100644 --- a/src/subconscious/graphics/BattleScene.java +++ b/src/subconscious/graphics/BattleScene.java @@ -22,7 +22,7 @@ public class BattleScene extends MapScene { } @Override - public void build() { + protected void build() { this.actorInfoWidget = new ActorInfo("actorinfo"); this.addWidget(this.actorInfoWidget); @@ -36,7 +36,7 @@ public class BattleScene extends MapScene { } @Override - public void update(long deltaNanoTime) { + protected void update(long deltaNanoTime) { } diff --git a/src/subconscious/graphics/GameWindow.java b/src/subconscious/graphics/GameWindow.java index 66fe26e..15faeef 100644 --- a/src/subconscious/graphics/GameWindow.java +++ b/src/subconscious/graphics/GameWindow.java @@ -28,8 +28,7 @@ public class GameWindow extends Frame implements Runnable, WindowListener { private volatile Scene scene = null; private Thread sceneThread = null; // stack of loaded scenes in the background that are paused - private volatile Stack> loadedScenes = new Stack<>(); - + private Stack> loadedScenes = new Stack<>(); private volatile Game game; public GameWindow(Game g) { diff --git a/src/subconscious/graphics/MapScene.java b/src/subconscious/graphics/MapScene.java index 8e2933b..dde15c6 100644 --- a/src/subconscious/graphics/MapScene.java +++ b/src/subconscious/graphics/MapScene.java @@ -98,7 +98,7 @@ public abstract class MapScene extends Scene { return screenPos; } - protected void renderTiles(Graphics2D g) { + private void renderTiles(Graphics2D g) { for (Tile tile : this.game.getMap().getGrid()) { switch (tile.getType()) { case CLEAR: @@ -158,7 +158,7 @@ public abstract class MapScene extends Scene { g.drawRect(0, 0, maxBound, maxBound); } - protected void renderSprites(Graphics2D g) { + private void renderSprites(Graphics2D g) { for (Sprite sprite : this.sprites) { Rectangle rect = sprite.getRect(); diff --git a/src/subconscious/graphics/Scene.java b/src/subconscious/graphics/Scene.java index a544b1c..f6cd4e1 100644 --- a/src/subconscious/graphics/Scene.java +++ b/src/subconscious/graphics/Scene.java @@ -98,12 +98,12 @@ public abstract class Scene extends Panel private Scene requestedScene = null; private boolean requestedPrevScene = false; - protected volatile boolean running = true; - private volatile boolean threadPaused = false; + protected boolean running = true; + private boolean threadPaused = false; private Lock pauseLock = new ReentrantLock(); private Condition threadResumed = pauseLock.newCondition(); - protected volatile Dimension canvasSize = GameWindow.WINDOW_SIZE; + protected Dimension canvasSize = GameWindow.WINDOW_SIZE; protected Canvas canvas = new Canvas(); protected BufferStrategy buffer; @@ -196,8 +196,15 @@ public abstract class Scene extends Panel } protected void removeWidget(Widget widget) { - // TODO - throw new UnsupportedOperationException("TODO"); + this.widgets.remove(widget); + + if (widget instanceof Dynamic) { + this.dynamicWidgetsCache.remove(widget); + } + + if (widget instanceof Clickable) { + this.clickableWidgetsCache.remove(widget); + } } /* call render and updates */ @@ -307,17 +314,17 @@ public abstract class Scene extends Panel } /* thread pause controls */ - public void stop() { + public synchronized void stop() { // stop thread even if paused this.resumeThread(); this.running = false; } - public void pauseThread() { + public synchronized void pauseThread() { this.threadPaused = true; } - public void resumeThread() { + public synchronized void resumeThread() { if (!this.threadPaused) return; diff --git a/src/subconscious/graphics/WorldScene.java b/src/subconscious/graphics/WorldScene.java index e56e6c0..97c2277 100644 --- a/src/subconscious/graphics/WorldScene.java +++ b/src/subconscious/graphics/WorldScene.java @@ -22,7 +22,7 @@ public class WorldScene extends MapScene { } @Override - public void build() { + protected void build() { this.actorInfoWidget = new ActorInfo("actorinfo"); this.addWidget(this.actorInfoWidget); @@ -36,7 +36,7 @@ public class WorldScene extends MapScene { } @Override - public void update(long deltaNanoTime) { + protected void update(long deltaNanoTime) { } -- cgit v1.2.1