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 b7a244dbf..15c2fe69f 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 @@ -36,7 +36,6 @@ public class UIElementsDisplay extends UIComponent { 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 short toBuild = 0; // bitmask for the tower selected for building @Override public void create() { @@ -57,32 +56,31 @@ private void addActors() { towerTable.setDebug(true); towerTable.padTop(50f); + TowerType[] defaultTowers = { + TowerType.TNT, + TowerType.DROID, + TowerType.INCOME, + TowerType.WALL, + TowerType.WEAPON + }; + + // Fetch the selected tower types if set Array towers = new Array<>(); for (TowerType tower : ServiceLocator.getTowerTypes()) { towers.add(tower); } - TextButton tower1; - TextButton tower2; - TextButton tower3; - TextButton tower4; - TextButton tower5; - - if (!towers.isEmpty()) { - tower1 = new TextButton(towers.get(0).getTowerName(), skin); - tower2 = new TextButton(towers.get(1).getTowerName(), skin); - tower3 = new TextButton(towers.get(2).getTowerName(), skin); - tower4 = new TextButton(towers.get(3).getTowerName(), skin); - tower5 = new TextButton(towers.get(4).getTowerName(), skin); - } else { - // no selected towers, set default towers - tower1 = new TextButton(TowerType.TNT.getTowerName(), skin); - tower2 = new TextButton(TowerType.DROID.getTowerName(), skin); - tower3 = new TextButton(TowerType.WEAPON.getTowerName(), skin); - tower4 = new TextButton(TowerType.INCOME.getTowerName(), skin); - tower5 = new TextButton(TowerType.WALL.getTowerName(), skin); + // If no towers set, populate with default towers + if (towers.isEmpty()) { + towers.addAll(defaultTowers); } + TextButton tower1 = new TextButton(towers.get(0).getTowerName(), skin); + TextButton tower2 = new TextButton(towers.get(1).getTowerName(), skin); + TextButton tower3 = new TextButton(towers.get(2).getTowerName(), skin); + TextButton tower4 = new TextButton(towers.get(3).getTowerName(), skin); + TextButton tower5 = new TextButton(towers.get(4).getTowerName(), skin); + // Triggers an event when the button is pressed. tower1.addListener( new ChangeListener() { @@ -90,7 +88,6 @@ private void addActors() { public void changed(ChangeEvent changeEvent, Actor actor) { logger.debug("Tower 1 build button clicked"); ServiceLocator.getCurrencyService().setTowerType(towers.get(0)); -// entity.getEvents().trigger("exit"); } }); @@ -101,7 +98,6 @@ public void changed(ChangeEvent changeEvent, Actor actor) { public void changed(ChangeEvent changeEvent, Actor actor) { logger.debug("Tower 2 build button clicked"); ServiceLocator.getCurrencyService().setTowerType(towers.get(1)); -// entity.getEvents().trigger("exit"); } }); @@ -111,7 +107,6 @@ public void changed(ChangeEvent changeEvent, Actor actor) { public void changed(ChangeEvent changeEvent, Actor actor) { logger.debug("Tower 3 build button clicked"); ServiceLocator.getCurrencyService().setTowerType(towers.get(2)); -// entity.getEvents().trigger("exit"); } }); @@ -121,7 +116,6 @@ public void changed(ChangeEvent changeEvent, Actor actor) { public void changed(ChangeEvent changeEvent, Actor actor) { logger.debug("Tower 4 build button clicked"); ServiceLocator.getCurrencyService().setTowerType(towers.get(3)); -// entity.getEvents().trigger("exit"); } }); @@ -131,7 +125,6 @@ public void changed(ChangeEvent changeEvent, Actor actor) { public void changed(ChangeEvent changeEvent, Actor actor) { logger.debug("Tower 5 build button clicked"); ServiceLocator.getCurrencyService().setTowerType(towers.get(4)); -// entity.getEvents().trigger("exit"); } }); 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 dd7041cbc..e702a20a5 100644 --- a/source/core/src/main/com/csse3200/game/input/BuildInputComponent.java +++ b/source/core/src/main/com/csse3200/game/input/BuildInputComponent.java @@ -10,6 +10,7 @@ import com.csse3200.game.entities.factories.TowerFactory; import com.csse3200.game.screens.TowerType; import com.csse3200.game.services.ServiceLocator; +import net.dermetfan.gdx.math.MathUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -85,11 +86,10 @@ public boolean touchDown(int screenX, int screenY, int pointer, int button) { public void buildTower(int x, int y) { // fetch the currently set TowerType in the currency service, and its associated build cost. TowerType tower = ServiceLocator.getCurrencyService().getTower(); - int cost = Integer.parseInt(ServiceLocator.getCurrencyService().getTower().getPrice()) * -1; - Entity newTower = null; - - // build the selected tower if (tower != null) { + int cost = Integer.parseInt(ServiceLocator.getCurrencyService().getTower().getPrice()); + Entity newTower = null; + // build the selected tower switch (tower) { case WEAPON: newTower = TowerFactory.createWeaponTower(); @@ -113,11 +113,19 @@ public void buildTower(int x, int y) { newTower = TowerFactory.createStunTower(); } if (newTower != null) { - newTower.setPosition(x, y); - ServiceLocator.getEntityService().register(newTower); - // show a popup that reflects the cost of the build - ServiceLocator.getCurrencyService().getDisplay().currencyPopUp(x, y, cost, 10); + if (cost <= ServiceLocator.getCurrencyService().getScrap().getAmount()) { + newTower.setPosition(x, y); + ServiceLocator.getEntityService().register(newTower); + // Decrement currency and show a popup that reflects the cost of the build + ServiceLocator.getCurrencyService().getScrap().modify(-cost); + ServiceLocator.getCurrencyService().getDisplay().updateScrapsStats(); + ServiceLocator.getCurrencyService().getDisplay().currencyPopUp(x, y, cost, 10); + } else { + // maybe dispose of the tower here? + } } } + + } } diff --git a/source/core/src/main/com/csse3200/game/services/ServiceLocator.java b/source/core/src/main/com/csse3200/game/services/ServiceLocator.java index 3db0896a8..8f3c72154 100644 --- a/source/core/src/main/com/csse3200/game/services/ServiceLocator.java +++ b/source/core/src/main/com/csse3200/game/services/ServiceLocator.java @@ -123,7 +123,6 @@ public static void registerMapService(MapService source) { public static void setTowerTypes(Set selectedTowers) { towerTypes.clear(); towerTypes.addAll(selectedTowers); - logger.info("selected towers are " + towerTypes); } public static Set getTowerTypes() {