From ec25e7e4bf66ea60c28538beaaf0c0ff3175633f Mon Sep 17 00:00:00 2001 From: Ahmad Abu-Aysha <111224176+The-AhmadAA@users.noreply.github.com> Date: Wed, 11 Oct 2023 13:35:39 +1000 Subject: [PATCH] updated buildInputComponent JUnit tests --- .../game/input/BuildInputComponent.java | 36 ++++++++++++++----- .../game/input/BuildInputComponentTest.java | 2 +- 2 files changed, 29 insertions(+), 9 deletions(-) diff --git a/source/core/src/main/com/csse3200/game/input/BuildInputComponent.java b/source/core/src/main/com/csse3200/game/input/BuildInputComponent.java index 11fed6a5e..8af5618c8 100644 --- a/source/core/src/main/com/csse3200/game/input/BuildInputComponent.java +++ b/source/core/src/main/com/csse3200/game/input/BuildInputComponent.java @@ -12,6 +12,7 @@ import com.csse3200.game.entities.EntityService; import com.csse3200.game.entities.factories.TowerFactory; import com.csse3200.game.screens.TowerType; +import com.csse3200.game.services.CurrencyService; import com.csse3200.game.services.ServiceLocator; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -93,9 +94,8 @@ public boolean touchDown(int screenX, int screenY, int pointer, int button) { // check that no entities are occupying the tile if (!tileOccupied) { - buildTower((int)cursorPosition.x, (int)cursorPosition.y); // logger.debug("spawning a tower at {}, {}", cursorPosition.x, cursorPosition.y); - return true; + return buildTower((int)cursorPosition.x, (int)cursorPosition.y); } else { // TODO: Create a tile indication of invalid placement here?? return false; @@ -139,15 +139,24 @@ public boolean keyUp(int keycode) { * @param x x-coordinate int value * @param y y-coordinate int value */ - public void buildTower(int x, int y) { + public boolean buildTower(int x, int y) { + TowerType tower; + CurrencyService currencyService; // fetch the currently set TowerType in the currency service, and its associated build cost. - - TowerType tower = ServiceLocator.getCurrencyService().getTower(); + try { + currencyService = ServiceLocator.getCurrencyService(); + } catch (NullPointerException e) { + // if the currency service fails or is not running + logger.error("BuildInputComponent line 148: Failed to fetch currency service"); + // Set to default weaponTower + return false; + } + tower = currencyService.getTower(); if (tower != null) { // fetch the price of the selected tower and attempt to instantiate - int cost = Integer.parseInt(ServiceLocator.getCurrencyService().getTower().getPrice()); + int cost = Integer.parseInt(currencyService.getTower().getPrice()); - if (cost <= ServiceLocator.getCurrencyService().getScrap().getAmount()) { + if (cost <= currencyService.getScrap().getAmount()) { Entity newTower = switch (tower) { case WEAPON -> TowerFactory.createWeaponTower(); case INCOME -> TowerFactory.createIncomeTower(); @@ -159,7 +168,15 @@ public void buildTower(int x, int y) { }; // build the selected tower newTower.setPosition(x, y); - ServiceLocator.getEntityService().register(newTower); + EntityService entityService; + try { + entityService = ServiceLocator.getEntityService(); + } catch (NullPointerException e) { + // failed to fetch entityService + logger.error("BuildInputComponent line 173: Failed to fetch EntityService"); + return false; + } + entityService.register(newTower); // Decrement currency and show a popup that reflects the cost of the build ServiceLocator.getCurrencyService().getScrap().modify(-cost); @@ -171,6 +188,7 @@ public void buildTower(int x, int y) { // deselect the tower after building ServiceLocator.getCurrencyService().setTowerType(null); + return true; } else { // play a sound to indicate an invalid action long soundId = errorSound.play(); @@ -178,8 +196,10 @@ public void buildTower(int x, int y) { ServiceLocator.getCurrencyService().getDisplay().scrapBalanceFlash(); // TODO: add a visual indication of the build fail, through // currency display flash + } } + return false; } /** diff --git a/source/core/src/test/com/csse3200/game/input/BuildInputComponentTest.java b/source/core/src/test/com/csse3200/game/input/BuildInputComponentTest.java index 61f2d1935..b67900aa8 100644 --- a/source/core/src/test/com/csse3200/game/input/BuildInputComponentTest.java +++ b/source/core/src/test/com/csse3200/game/input/BuildInputComponentTest.java @@ -82,7 +82,7 @@ void shouldRegisterOnCreate() { @Test void shouldHandleTouchDown() { when(entityService.entitiesInTile(5, 5)).thenReturn(false); - assert(buildInputComponent.touchDown( 5, 5, 7, 8)); + assertFalse(buildInputComponent.touchDown( 5, 5, 7, 8)); } @Test