Skip to content

Commit

Permalink
updated buildInputComponent JUnit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
The-AhmadAA committed Oct 11, 2023
1 parent 6215af1 commit ec25e7e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand All @@ -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);
Expand All @@ -171,15 +188,18 @@ 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();
errorSound.setVolume(soundId, 1f);
ServiceLocator.getCurrencyService().getDisplay().scrapBalanceFlash();
// TODO: add a visual indication of the build fail, through
// currency display flash

}
}
return false;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit ec25e7e

Please sign in to comment.