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 c8f844eac..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) { @@ -197,7 +199,30 @@ public void updateTimerButton() { 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()) { + if (!findActor(timerButton)) { + createTimerButton(); + } + timerButton.setText("Next wave in: " + finalTime); + } else { + buttonTable.removeActor(timerButton); + stage.act(); + stage.draw(); + } + } + + /** + * 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