Skip to content

Commit

Permalink
added currency decrementing and cost linked spawning of towers
Browse files Browse the repository at this point in the history
  • Loading branch information
The-AhmadAA committed Oct 2, 2023
1 parent 483b51d commit 9f7f167
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -57,40 +56,38 @@ 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<TowerType> 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() {
@Override
public void changed(ChangeEvent changeEvent, Actor actor) {
logger.debug("Tower 1 build button clicked");
ServiceLocator.getCurrencyService().setTowerType(towers.get(0));
// entity.getEvents().trigger("exit");
}
});

Expand All @@ -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");
}
});

Expand All @@ -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");
}
});

Expand All @@ -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");
}
});

Expand All @@ -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");
}
});

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

Expand Down Expand Up @@ -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();
Expand All @@ -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?
}
}
}


}
}
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@ public static void registerMapService(MapService source) {
public static void setTowerTypes(Set<TowerType> selectedTowers) {
towerTypes.clear();
towerTypes.addAll(selectedTowers);
logger.info("selected towers are " + towerTypes);
}

public static Set<TowerType> getTowerTypes() {
Expand Down

0 comments on commit 9f7f167

Please sign in to comment.