From 0e146897aa0696e4eba7525c049965c5bc6057a7 Mon Sep 17 00:00:00 2001 From: Shivam Date: Sat, 14 Oct 2023 15:00:59 +1000 Subject: [PATCH 1/2] revised the basic structure for timer button --- .../game/components/maingame/UIElementsDisplay.java | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/source/core/src/main/com/csse3200/game/components/maingame/UIElementsDisplay.java b/source/core/src/main/com/csse3200/game/components/maingame/UIElementsDisplay.java index b84b24fe3..1f38a21c4 100644 --- a/source/core/src/main/com/csse3200/game/components/maingame/UIElementsDisplay.java +++ b/source/core/src/main/com/csse3200/game/components/maingame/UIElementsDisplay.java @@ -196,11 +196,18 @@ public void createTimerButton() { * This method updates the text for timer button. */ public void updateTimerButton() { - int totalSecs = (int) (timer - (ServiceLocator.getTimeSource().getTime() / 1000)); + int totalSecs = (int) ((ServiceLocator.getWaveService().getNextWaveTime() + - ServiceLocator.getTimeSource().getTime()) / 1000); int seconds = totalSecs % 60; int minutes = (totalSecs % 3600) / 60; String finalTime = String.format("%02d:%02d", minutes, seconds); - timerButton.setText("Next wave in:" + finalTime); + if (ServiceLocator.getTimeSource().getTime() < ServiceLocator.getWaveService().getNextWaveTime()) { + timerButton.setText("Next wave in: " + finalTime); + } else { + buttonTable.removeActor(timerButton); + stage.act(); + stage.draw(); + } } @Override From 862a2874ce6fcde3a1a0c4da9c959e8beb727008 Mon Sep 17 00:00:00 2001 From: Shivam Date: Sat, 14 Oct 2023 16:34:59 +1000 Subject: [PATCH 2/2] Finalised the working of timer button --- .../maingame/UIElementsDisplay.java | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/source/core/src/main/com/csse3200/game/components/maingame/UIElementsDisplay.java b/source/core/src/main/com/csse3200/game/components/maingame/UIElementsDisplay.java index 30a2538a1..0c5cbc091 100644 --- a/source/core/src/main/com/csse3200/game/components/maingame/UIElementsDisplay.java +++ b/source/core/src/main/com/csse3200/game/components/maingame/UIElementsDisplay.java @@ -9,6 +9,7 @@ import com.badlogic.gdx.scenes.scene2d.ui.TextButton; import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener; import com.badlogic.gdx.utils.Array; +import com.badlogic.gdx.utils.Null; import com.csse3200.game.screens.TowerType; import com.csse3200.game.services.ServiceLocator; import com.csse3200.game.ui.ButtonFactory; @@ -187,7 +188,8 @@ public void createTimerButton() { * This method updates the text for timer button. */ public void updateTimerButton() { - int totalSecs = (int) (timer - (ServiceLocator.getTimeSource().getTime() / 1000)); + int totalSecs = (int) ((ServiceLocator.getWaveService().getNextWaveTime() + - ServiceLocator.getTimeSource().getTime()) / 1000); // TODO : THESE SHOULD BE REMOVED AND PLACED WHEREVER THE BOSS MOB GETS SPAWNED if (totalSecs % 20 == 0) { @@ -198,6 +200,9 @@ public void updateTimerButton() { int minutes = (totalSecs % 3600) / 60; String finalTime = String.format("%02d:%02d", minutes, seconds); if (ServiceLocator.getTimeSource().getTime() < ServiceLocator.getWaveService().getNextWaveTime()) { + if (!findActor(timerButton)) { + createTimerButton(); + } timerButton.setText("Next wave in: " + finalTime); } else { buttonTable.removeActor(timerButton); @@ -206,6 +211,20 @@ public void updateTimerButton() { } } + /** + * This function checks if a certain actor is present in buttonTable. + * @param actor the actor to find in buttonTable. + * @return true if the actor is present and false otherwise. + */ + public boolean findActor(Actor actor) { + for (Actor actors: buttonTable.getChildren()) { + if (actors == actor) { + return true; + } + } + return false; + } + @Override public void draw(SpriteBatch batch) { // drawing is handled by the stage