From 53bb03f471a2c4a670d9541b5ec39a24ddda2352 Mon Sep 17 00:00:00 2001 From: Shivam Date: Mon, 2 Oct 2023 20:01:41 +1000 Subject: [PATCH 1/7] Tidying up code for better readability --- .../maingame/UIElementsDisplay.java | 31 ++++++++++--------- 1 file changed, 16 insertions(+), 15 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 bbcc06b35..32c995a97 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 @@ -13,7 +13,6 @@ import com.csse3200.game.ui.UIComponent; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.w3c.dom.Text; /** * Displays a button to represent the remaining mobs left in the current wave and a button to skip to the next wave. @@ -24,8 +23,8 @@ public class UIElementsDisplay extends UIComponent { private final Table buttonTable = new Table(); private final Table towerTable = new Table(); Skin skin = new Skin(Gdx.files.internal("flat-earth/skin/flat-earth-ui.json")); - private TextButton remainingMobsButton = new ButtonFactory().createButton("Mobs left:"); - private final TextButton timerButton = new ButtonFactory().createButton("Next wave:"); + private TextButton remainingMobsButton; + private TextButton timerButton; @Override public void create() { @@ -37,6 +36,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(); @@ -52,17 +54,6 @@ private void addActors() { TextButton tower4 = new TextButton("Tower 4", skin); TextButton tower5 = new TextButton("Tower 5", skin); - //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); @@ -81,7 +72,17 @@ private void addActors() { * 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()); + if (ServiceLocator.getTimeSource().getTime() < ServiceLocator.getWaveService().getNextWaveTime()) { + createTimerButton(); + } + } + + public void createTimerButton() { + timerButton = new ButtonFactory().createButton("Next wave in:" + + (ServiceLocator.getWaveService().getNextWaveTime() / 1000)); + buttonTable.row(); + buttonTable.add(timerButton).padRight(10f); } @Override From 8c0be53329d9bf8f08eb366a7941e701bb8cacc0 Mon Sep 17 00:00:00 2001 From: Shivam Date: Mon, 2 Oct 2023 21:45:10 +1000 Subject: [PATCH 2/7] added the file for UIElementsDisplayTest --- .../maingame/UIElementsDisplay.java | 8 +++ .../maingame/UIElementsDisplayTest.java | 55 +++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 source/core/src/test/com/csse3200/game/components/maingame/UIElementsDisplayTest.java 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 32c995a97..29af8c768 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 @@ -85,6 +85,14 @@ public void createTimerButton() { buttonTable.add(timerButton).padRight(10f); } + public TextButton getRemainingMobsButton() { + return remainingMobsButton; + } + + public TextButton getTimerButton() { + return timerButton; + } + @Override public void draw(SpriteBatch batch) { // drawing is handled by the stage 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..eb5d606b0 --- /dev/null +++ b/source/core/src/test/com/csse3200/game/components/maingame/UIElementsDisplayTest.java @@ -0,0 +1,55 @@ +//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.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() { +// ServiceLocator.registerRenderService(new RenderService()); +// WaveService waveService = new WaveService(); +// ServiceLocator.registerWaveService(waveService); +// +// when(ServiceLocator.getWaveService().getEnemyCount()).thenReturn(10); +// when(ServiceLocator.getWaveService().getNextWaveTime()).thenReturn(10000L); // Example time +// +// display = new UIElementsDisplay(); +// display.create(); +// } +// +// @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()); +// } +//} From 5c1be2dfd1045e91cbce2e6d9a6de0b5bb956567 Mon Sep 17 00:00:00 2001 From: Shivam Date: Mon, 2 Oct 2023 23:07:46 +1000 Subject: [PATCH 3/7] fixing errors in UIElementsDisplayTest --- .../components/maingame/UIElementsDisplayTest.java | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) 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 index eb5d606b0..68b309552 100644 --- a/source/core/src/test/com/csse3200/game/components/maingame/UIElementsDisplayTest.java +++ b/source/core/src/test/com/csse3200/game/components/maingame/UIElementsDisplayTest.java @@ -7,6 +7,7 @@ //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; @@ -26,14 +27,18 @@ // // @Before // public void setUp() { -// ServiceLocator.registerRenderService(new RenderService()); -// WaveService waveService = new WaveService(); +// RenderService renderService = mock(RenderService.class); +// ResourceService resourceService = mock(ResourceService.class); +// WaveService waveService = mock(WaveService.class); +// +// ServiceLocator.registerRenderService(renderService); +// ServiceLocator.registerResourceService(resourceService); // ServiceLocator.registerWaveService(waveService); // // when(ServiceLocator.getWaveService().getEnemyCount()).thenReturn(10); // when(ServiceLocator.getWaveService().getNextWaveTime()).thenReturn(10000L); // Example time // -// display = new UIElementsDisplay(); +// display = waveService.getDisplay(); // display.create(); // } // From fbb22f0352093b427b8b93e2b5dd24a9694f1316 Mon Sep 17 00:00:00 2001 From: Shivam Date: Tue, 3 Oct 2023 00:51:57 +1000 Subject: [PATCH 4/7] Created a standard timer for wave timer button --- .../maingame/UIElementsDisplay.java | 23 ++++++++++++++++--- .../csse3200/game/screens/MainGameScreen.java | 1 + .../maingame/UIElementsDisplayTest.java | 5 ++-- 3 files changed, 24 insertions(+), 5 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 29af8c768..47a837d79 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 @@ -25,6 +25,7 @@ public class UIElementsDisplay extends UIComponent { Skin skin = new Skin(Gdx.files.internal("flat-earth/skin/flat-earth-ui.json")); private TextButton remainingMobsButton; private TextButton timerButton; + private final int timer = 110; @Override public void create() { @@ -66,6 +67,8 @@ private void addActors() { stage.addActor(buttonTable); stage.addActor(towerTable); + + createTimerButton(); } /** @@ -73,18 +76,32 @@ private void addActors() { */ public void updateMobCount() { remainingMobsButton.setText("Mobs:" + ServiceLocator.getWaveService().getEnemyCount()); - if (ServiceLocator.getTimeSource().getTime() < ServiceLocator.getWaveService().getNextWaveTime()) { - createTimerButton(); - } + updateTimerButton(); } public void createTimerButton() { + timerButton = new ButtonFactory().createButton("Next wave in:" + (ServiceLocator.getWaveService().getNextWaveTime() / 1000)); buttonTable.row(); buttonTable.add(timerButton).padRight(10f); } +// @Override +// public void render(SpriteBatch batch) { +// draw(batch); +// logger.info("HAHAHHAHAHAHHAHAHAHAHHAHAHAHAHHAHAHAHHAHAHAHHAHAHAHAHHAHAHHA"); +// updateTimerButton(); +// } + + 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); + } + public TextButton getRemainingMobsButton() { return remainingMobsButton; } 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 529748428..d1b1ca53a 100644 --- a/source/core/src/main/com/csse3200/game/screens/MainGameScreen.java +++ b/source/core/src/main/com/csse3200/game/screens/MainGameScreen.java @@ -168,6 +168,7 @@ public void render(float delta) { ui.getEvents().trigger("lose"); } + ServiceLocator.getWaveService().getDisplay().updateTimerButton(); renderer.render(); } 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 index 68b309552..aa6aac7b8 100644 --- a/source/core/src/test/com/csse3200/game/components/maingame/UIElementsDisplayTest.java +++ b/source/core/src/test/com/csse3200/game/components/maingame/UIElementsDisplayTest.java @@ -31,15 +31,16 @@ // 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); // -// display = waveService.getDisplay(); -// display.create(); // } // // @Test From b9f8f95a3fc32814d27c4e307a74991d0ce72fb2 Mon Sep 17 00:00:00 2001 From: Shivam Date: Tue, 3 Oct 2023 01:04:55 +1000 Subject: [PATCH 5/7] Added javadocs for UIElementsDisplay --- .../maingame/UIElementsDisplay.java | 21 ++++++------------- 1 file changed, 6 insertions(+), 15 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 47a837d79..c356961f9 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 @@ -79,6 +79,9 @@ public void updateMobCount() { updateTimerButton(); } + /** + * This method creates the timer button. + */ public void createTimerButton() { timerButton = new ButtonFactory().createButton("Next wave in:" @@ -87,13 +90,9 @@ public void createTimerButton() { buttonTable.add(timerButton).padRight(10f); } -// @Override -// public void render(SpriteBatch batch) { -// draw(batch); -// logger.info("HAHAHHAHAHAHHAHAHAHAHHAHAHAHAHHAHAHAHHAHAHAHHAHAHAHAHHAHAHHA"); -// updateTimerButton(); -// } - + /** + * This method updates the text for timer button. + */ public void updateTimerButton() { int totalSecs = (int) (timer - (ServiceLocator.getTimeSource().getTime() / 1000)); int seconds = totalSecs % 60; @@ -102,14 +101,6 @@ public void updateTimerButton() { timerButton.setText("Next wave in:" + finalTime); } - public TextButton getRemainingMobsButton() { - return remainingMobsButton; - } - - public TextButton getTimerButton() { - return timerButton; - } - @Override public void draw(SpriteBatch batch) { // drawing is handled by the stage From 5c018bfeb45f48c11d7e46a0c268a6502b4f140f Mon Sep 17 00:00:00 2001 From: Shivam Date: Tue, 3 Oct 2023 01:40:15 +1000 Subject: [PATCH 6/7] Fixed failing tests --- .../game/components/maingame/UIElementsDisplay.java | 9 ++++++++- .../main/com/csse3200/game/screens/MainGameScreen.java | 1 + .../src/main/com/csse3200/game/services/WaveService.java | 1 - 3 files 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 644bedcb8..471739f95 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 @@ -152,7 +152,6 @@ public void changed(ChangeEvent changeEvent, Actor actor) { */ public void updateMobCount() { remainingMobsButton.setText("Mobs:" + ServiceLocator.getWaveService().getEnemyCount()); - updateTimerButton(); } /** @@ -199,4 +198,12 @@ public void dispose() { buttonTable.clear(); towerTable.clear(); } + + public TextButton getRemainingMobsButton() { + return getRemainingMobsButton(); + } + + public TextButton getTimerButton() { + return timerButton; + } } 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 0ff767a61..332146d13 100644 --- a/source/core/src/main/com/csse3200/game/screens/MainGameScreen.java +++ b/source/core/src/main/com/csse3200/game/screens/MainGameScreen.java @@ -213,6 +213,7 @@ public void render(float delta) { } 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()); } From 882dab13284af85d7f2356ee40183b0a8de526a1 Mon Sep 17 00:00:00 2001 From: Shivam Date: Tue, 3 Oct 2023 02:32:26 +1000 Subject: [PATCH 7/7] removed useless getter methods --- .../game/components/maingame/UIElementsDisplay.java | 8 -------- 1 file changed, 8 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 471739f95..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 @@ -198,12 +198,4 @@ public void dispose() { buttonTable.clear(); towerTable.clear(); } - - public TextButton getRemainingMobsButton() { - return getRemainingMobsButton(); - } - - public TextButton getTimerButton() { - return timerButton; - } }