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 15c2fe69f..e8a9c020e 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 @@ -20,7 +20,6 @@ import com.csse3200.game.ui.UIComponent; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.w3c.dom.Text; import java.util.HashSet; import java.util.Set; @@ -34,8 +33,9 @@ public class UIElementsDisplay extends UIComponent { private final Table buttonTable = new Table(); private final Table towerTable = new Table(); Skin skin = new Skin(Gdx.files.internal("images/ui/buttons/glass.json")); - private TextButton remainingMobsButton = new ButtonFactory().createButton("Mobs left:"); - private final TextButton timerButton = new ButtonFactory().createButton("Next wave:"); + private TextButton remainingMobsButton; + private TextButton timerButton; + private final int timer = 110; @Override public void create() { @@ -47,6 +47,9 @@ public void create() { * This method creates the buttons, adds them to the respective tables and draws them on the screen. */ private void addActors() { + remainingMobsButton = new ButtonFactory().createButton("Mobs:" + + ServiceLocator.getWaveService().getEnemyCount()); + buttonTable.top().right(); towerTable.top(); @@ -128,17 +131,6 @@ public void changed(ChangeEvent changeEvent, Actor actor) { } }); - //Not sure if we need a listened for a label -// // Triggers an event when the button is pressed. -// remainingMobsButton.addListener( -// new ChangeListener() { -// @Override -// public void changed(ChangeEvent changeEvent, Actor actor) { -// logger.debug("Wave counter button clicked"); -// entity.getEvents().trigger("wave counter"); -// } -// }); - buttonTable.add(remainingMobsButton).padTop(10f).padRight(10f); buttonTable.row(); buttonTable.add(timerButton).padRight(10f); @@ -151,13 +143,37 @@ public void changed(ChangeEvent changeEvent, Actor actor) { stage.addActor(buttonTable); stage.addActor(towerTable); + + createTimerButton(); } /** * This method updates the mob count button as mobs die in the game */ public void updateMobCount() { - remainingMobsButton.getLabel().setText("Mobs:" + ServiceLocator.getWaveService().getEnemyCount()); + remainingMobsButton.setText("Mobs:" + ServiceLocator.getWaveService().getEnemyCount()); + } + + /** + * This method creates the timer button. + */ + public void createTimerButton() { + + timerButton = new ButtonFactory().createButton("Next wave in:" + + (ServiceLocator.getWaveService().getNextWaveTime() / 1000)); + buttonTable.row(); + buttonTable.add(timerButton).padRight(10f); + } + + /** + * This method updates the text for timer button. + */ + public void updateTimerButton() { + int totalSecs = (int) (timer - (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); } @Override diff --git a/source/core/src/main/com/csse3200/game/screens/MainGameScreen.java b/source/core/src/main/com/csse3200/game/screens/MainGameScreen.java index bd9b3eb62..332146d13 100644 --- a/source/core/src/main/com/csse3200/game/screens/MainGameScreen.java +++ b/source/core/src/main/com/csse3200/game/screens/MainGameScreen.java @@ -212,6 +212,8 @@ public void render(float delta) { ui.getEvents().trigger("lose"); } + ServiceLocator.getWaveService().getDisplay().updateTimerButton(); + ServiceLocator.getWaveService().getDisplay().updateMobCount(); renderer.render(); } diff --git a/source/core/src/main/com/csse3200/game/services/WaveService.java b/source/core/src/main/com/csse3200/game/services/WaveService.java index a8416b11f..0e50a7fc1 100644 --- a/source/core/src/main/com/csse3200/game/services/WaveService.java +++ b/source/core/src/main/com/csse3200/game/services/WaveService.java @@ -52,7 +52,6 @@ public int getEnemyCount() { */ public void updateEnemyCount() { enemyCount -= 1; - display.updateMobCount(); logger.info("{} enemies remaining in wave", getEnemyCount()); } diff --git a/source/core/src/test/com/csse3200/game/components/maingame/UIElementsDisplayTest.java b/source/core/src/test/com/csse3200/game/components/maingame/UIElementsDisplayTest.java new file mode 100644 index 000000000..aa6aac7b8 --- /dev/null +++ b/source/core/src/test/com/csse3200/game/components/maingame/UIElementsDisplayTest.java @@ -0,0 +1,61 @@ +//package com.csse3200.game.components.maingame; +// +//import com.badlogic.gdx.scenes.scene2d.Stage; +//import com.badlogic.gdx.scenes.scene2d.ui.Skin; +//import com.badlogic.gdx.scenes.scene2d.ui.TextButton; +//import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener; +//import com.csse3200.game.components.maingame.UIElementsDisplay; +//import com.csse3200.game.rendering.RenderService; +//import com.csse3200.game.screens.MainGameScreen; +//import com.csse3200.game.services.ResourceService; +//import com.csse3200.game.services.ServiceLocator; +//import com.csse3200.game.services.WaveService; +//import com.csse3200.game.ui.ButtonFactory; +//import org.junit.Before; +//import org.junit.Test; +//import org.mockito.Mockito; +// +//import java.security.Provider; +// +//import static org.junit.Assert.assertEquals; +//import static org.junit.Assert.assertNull; +//import static org.mockito.Mockito.*; +// +//public class UIElementsDisplayTest { +// +// private UIElementsDisplay display; +// +// @Before +// public void setUp() { +// RenderService renderService = mock(RenderService.class); +// ResourceService resourceService = mock(ResourceService.class); +// WaveService waveService = mock(WaveService.class); +// +// display = new UIElementsDisplay(); +// +// ServiceLocator.registerRenderService(renderService); +// ServiceLocator.registerResourceService(resourceService); +// ServiceLocator.registerWaveService(waveService); +// +// when(ServiceLocator.getWaveService().getEnemyCount()).thenReturn(10); +// when(ServiceLocator.getWaveService().getNextWaveTime()).thenReturn(10000L); // Example time +// when(ServiceLocator.getWaveService().getDisplay()).thenReturn(display); +// +// } +// +// @Test +// public void testUpdateMobCount() { +// TextButton mobButton = display.getRemainingMobsButton(); +// assertEquals("Mobs:10", mobButton.getText()); +// +// display.updateMobCount(); +// assertEquals("Mobs:9", mobButton.getText()); +// } +// +// @Test +// public void testCreateTimerButton() { +// assertNull(display.getTimerButton()); +// display.createTimerButton(); +// assertEquals("Next wave in:10", display.getTimerButton().getText()); +// } +//}