diff options
author | Nao Pross <naopross@thearcway.org> | 2018-11-24 19:25:07 +0100 |
---|---|---|
committer | Nao Pross <naopross@thearcway.org> | 2018-11-24 19:25:07 +0100 |
commit | dab4fe776ccd52a5251115060ac8b18b5a167df8 (patch) | |
tree | 4434f4686fa040793d2f9508e01d064f0a627d5c /src | |
parent | Enable use of assertions (diff) | |
download | Subconscious-java-dab4fe776ccd52a5251115060ac8b18b5a167df8.tar.gz Subconscious-java-dab4fe776ccd52a5251115060ac8b18b5a167df8.zip |
Add dynamic widgets (update on every gameloop tick)
Diffstat (limited to 'src')
-rw-r--r-- | src/subconscious/graphics/Scene.java | 25 | ||||
-rw-r--r-- | src/subconscious/graphics/widget/Dynamic.java | 5 |
2 files changed, 27 insertions, 3 deletions
diff --git a/src/subconscious/graphics/Scene.java b/src/subconscious/graphics/Scene.java index 7e67490..f1a947d 100644 --- a/src/subconscious/graphics/Scene.java +++ b/src/subconscious/graphics/Scene.java @@ -4,6 +4,7 @@ import subconscious.Game; import subconscious.graphics.widget.Widget; import subconscious.graphics.widget.Clickable; +import subconscious.graphics.widget.Dynamic; import java.util.ArrayList; import java.util.Map; @@ -57,6 +58,8 @@ public abstract class Scene extends Panel ); protected ArrayList<Widget> widgets = new ArrayList<>(); + protected ArrayList<Widget> dynamicWidgetsCache = new ArrayList<>(); + // protected ArrayList<Widget> clickableWidgetsCache = new ArrayList<>(); // Game is never cached in the local thread protected volatile Game game; @@ -99,6 +102,12 @@ public abstract class Scene extends Panel // TODO: solve canvas // this.add(this.canvas, BorderLayout.CENTER); this.build(); + + for (Widget w : this.widgets) { + if (w instanceof Dynamic) { + this.dynamicWidgetsCache.add(w); + } + } } public Scene(Game game) { @@ -114,6 +123,12 @@ public abstract class Scene extends Panel protected abstract void update(long deltaNanoTime); protected abstract void render(Graphics2D g); + protected void updateWidgets(long deltaNanoTime) { + for (Widget w : this.dynamicWidgetsCache) { + ((Dynamic) w).update(deltaNanoTime); + } + } + protected void renderWidgets(Graphics2D g) { for (Widget w : this.widgets) { Graphics2D widgetGraphics = (Graphics2D) g.create( @@ -161,6 +176,7 @@ public abstract class Scene extends Panel long currentUpdateTime = System.nanoTime(); long lastUpdateTime; long deltaLoop; + long gameDeltaTime; this.running = true; this.threadPaused = false; @@ -207,9 +223,12 @@ public abstract class Scene extends Panel lastUpdateTime = currentUpdateTime; currentUpdateTime = System.nanoTime(); - // TODO: change to this.game.update(...) - this.update(currentUpdateTime - lastUpdateTime); - this.game.update(currentUpdateTime - lastUpdateTime); + gameDeltaTime = currentUpdateTime - lastUpdateTime; + + // TODO: remove Scene.update? + this.update(gameDeltaTime); + this.updateWidgets(gameDeltaTime); + this.game.update(gameDeltaTime); endLoopTime = System.nanoTime(); deltaLoop = endLoopTime - beginLoopTime; diff --git a/src/subconscious/graphics/widget/Dynamic.java b/src/subconscious/graphics/widget/Dynamic.java new file mode 100644 index 0000000..ca0029a --- /dev/null +++ b/src/subconscious/graphics/widget/Dynamic.java @@ -0,0 +1,5 @@ +package subconscious.graphics.widget; + +public interface Dynamic { + public void update(long deltaNanoTime); +}
\ No newline at end of file |