From dab4fe776ccd52a5251115060ac8b18b5a167df8 Mon Sep 17 00:00:00 2001 From: Nao Pross Date: Sat, 24 Nov 2018 19:25:07 +0100 Subject: Add dynamic widgets (update on every gameloop tick) --- src/subconscious/graphics/Scene.java | 25 ++++++++++++++++++++++--- src/subconscious/graphics/widget/Dynamic.java | 5 +++++ 2 files changed, 27 insertions(+), 3 deletions(-) create mode 100644 src/subconscious/graphics/widget/Dynamic.java 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 widgets = new ArrayList<>(); + protected ArrayList dynamicWidgetsCache = new ArrayList<>(); + // protected ArrayList 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 -- cgit v1.2.1