Skip to content

Commit

Permalink
Merge pull request #218 from UQcsse3200/Team-5--Turret-Selection
Browse files Browse the repository at this point in the history
[Team 5] Comments, Tests, Reimplementation of Economy Upgrade
  • Loading branch information
Hasakev authored Oct 3, 2023
2 parents e38db31 + 4507c49 commit 9de3757
Show file tree
Hide file tree
Showing 6 changed files with 151 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,8 @@ public void changeInterval(int newInterval) {
public void setInterval(int interval) {
this.interval = interval;
}

public int getInterval() {
return interval;
}
}
14 changes: 14 additions & 0 deletions source/core/src/main/com/csse3200/game/entities/EntityService.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,20 @@ public void dispose() {
}
}

/**
* Find an entity by its ID, if it exists return true, else return false
* @param id id of entity to find
* @return boolean true if entity exists, false if not
*/
public boolean findEntityExistence(int id) {
for (Entity entity : entities) {
if (entity.getId() == id) {
return true;
}
}
return false;
}

/**
* Get all entities
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class UpgradeUIComponent extends InputComponent {
Expand Down Expand Up @@ -62,15 +63,23 @@ public Camera getCamera() {
return camera;
}

/**
* Getter for the stage
* @return the stage
*/
public Stage getStage() {
return stage;
}

/**
* Method to handle the touch down event
* @param screenX the x coordinate of the touch
* @param screenY the y coordinate of the touch
* @param pointer the pointer
* @param button the button
*/
@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
// Clear all existing upgrade tables


Vector3 worldCoordinates = new Vector3((float) screenX, (float) screenY, 0);
getCamera().unproject(worldCoordinates);
Vector2 cursorPosition = new Vector2(worldCoordinates.x, worldCoordinates.y);
Expand All @@ -84,9 +93,13 @@ public boolean touchDown(int screenX, int screenY, int pointer, int button) {
}
//

// If the clicked position contains a turret, and the turret is upgradable and not a TNT tower
if (clickedEntity != null && clickedEntity.getComponent(TowerUpgraderComponent.class) != null
&& clickedEntity.getComponent(TNTDamageComponent.class) == null) {
// TNT TowerUpgraderComponent can be removed later, but possibly useful for future sprint.
// Clear all existing upgrade tables
logger.info("clickedEntity: " + clickedEntity);

if (clickedEntity != null && clickedEntity.getComponent(TowerUpgraderComponent.class) != null && clickedEntity.getComponent(TNTDamageComponent.class) == null) {
// logger.info("clicked a turret that is upgradable!");
clearUpgradeTables();
// Check if there is an existing upgrade table for this turret entity
Table existingUpgradeTable = upgradeTables.get(clickedEntity);
Expand All @@ -103,6 +116,7 @@ public boolean touchDown(int screenX, int screenY, int pointer, int button) {

// Store the new upgrade table in the map
upgradeTables.put(clickedEntity, newUpgradeTable);

}

return true;
Expand All @@ -118,28 +132,43 @@ private void clearUpgradeTables() {
upgradeTables.clear();
}

/**
* Creates the upgrade table for the associated turret entity
* <p>
* Each button has a listener that will upgrade the turret entity when clicked
* if the player has enough scrap. Additionally when the player hovers over a button
* the cost of the upgrade will be displayed in the cost display
* </p>
*
* Currently, the cost of each upgrade is hardcoded to 10 scrap, this can be changed to global
* variables to make balancing easier (contact @Hasakev (Kevin) if confused)
*
* @param turretEntity the turret entity to create the upgrade table for (the entity that was clicked)
* @return the upgrade table for the turret entity
*/
private Table createUpgradeTable(Entity turretEntity) {
// This is the overarching table that contains the close button, the inner table, and the cost display
Table upgradeTable = new Table();
upgradeTable.top();
upgradeTable.defaults().pad(0).space(0);
upgradeTable.setSize(60, 60);

// The inner table contains the upgrade buttons and the stats display
Table innerUpgradeTable = new Table();
innerUpgradeTable.top();
innerUpgradeTable.defaults().pad(10).space(0).padBottom(1);
innerUpgradeTable.setSize(60, 60);
// set table background
String imageFilePath = "images/ui/Sprites/UI_Glass_Frame_Standard_01a.png";
String upgradeButtonFilePath = "images/economy/scrapBanner.png";
Drawable drawableBackground = new TextureRegionDrawable(new TextureRegion(new Texture(imageFilePath)));
innerUpgradeTable.setBackground(drawableBackground);

// Stying for all the buttons
Drawable drawable = new TextureRegionDrawable(new TextureRegion(new Texture("images/ui/Sprites/UI_Glass_Button_Small_Lock_01a2.png")));
Drawable econDrawable = new TextureRegionDrawable(new TextureRegion(new Texture(upgradeButtonFilePath)));
TextButton.TextButtonStyle style = new TextButton.TextButtonStyle(
drawable, drawable, drawable, new BitmapFont());
TextButton.TextButtonStyle econStyle = new TextButton.TextButtonStyle(
econDrawable, econDrawable, econDrawable, new BitmapFont());
// create button

// Default values for the stats
int maxHealth = turretEntity.getComponent(CombatStatsComponent.class).getMaxHealth();
int currentHealth = turretEntity.getComponent(CombatStatsComponent.class).getHealth();
turretEntity.getComponent(CombatStatsComponent.class).setHealth(5); // for testing
Expand Down Expand Up @@ -179,9 +208,12 @@ public void clicked(InputEvent event, float x, float y) {
Drawable fireRateDrawable = new TextureRegionDrawable(new TextureRegion(new Texture("images/hourglass.png")));
Image fireRateImage = new Image(fireRateDrawable);


Drawable healthStyle = new TextureRegionDrawable(new TextureRegion(new Texture("images/heart_upgrade.png")));
ImageButton upgradeHealth = new ImageButton(healthStyle);

//// UPGRADE BUTTONS ////

// Health upgrade button
upgradeHealth.setScale(0.8f);
upgradeHealth.addListener(new ClickListener() {
@Override
Expand Down Expand Up @@ -210,6 +242,7 @@ public void exit(InputEvent event, float x, float y, int pointer, Actor toActor)
}
});

// Attack upgrade button
Drawable attackStyle = new TextureRegionDrawable(new TextureRegion(new Texture("images/damage_upgrade.png")));
ImageButton upgradeAttack = new ImageButton(attackStyle);
upgradeAttack.addListener(new ClickListener() {
Expand Down Expand Up @@ -238,7 +271,7 @@ public void exit(InputEvent event, float x, float y, int pointer, Actor toActor)
});



// Fire rate upgrade button
Drawable asStyle = new TextureRegionDrawable(new TextureRegion(new Texture("images/hourglass_upgrade.png")));
ImageButton upgradeFireRate = new ImageButton(asStyle);
upgradeFireRate.addListener(new ClickListener() {
Expand Down Expand Up @@ -271,6 +304,7 @@ public void exit(InputEvent event, float x, float y, int pointer, Actor toActor)
}
});

// Repair button
Drawable repair = new TextureRegionDrawable(new TextureRegion(new Texture("images/hammer.png")));
ImageButton repairButton = new ImageButton(repair);
repairButton.addListener(new ClickListener() {
Expand Down Expand Up @@ -304,8 +338,11 @@ public void exit(InputEvent event, float x, float y, int pointer, Actor toActor)
innerUpgradeTable.add(healthIconImage).padRight(5).width(32).height(32); // Add health icon
innerUpgradeTable.add(healthLabel).expandX().left();
innerUpgradeTable.row();
TextButton upgradeIncome = null;
ImageButton upgradeIncome = null;
// if the turret has an income upgrade component, add the income upgrade button
if (turretEntity.getComponent(IncomeUpgradeComponent.class) != null) {

// Income label and upgrade button
Drawable incomeDrawable = new TextureRegionDrawable(new TextureRegion(new Texture("images/economy/scrap.png")));
Image incomeImage = new Image(incomeDrawable);
Label incomeLabel = new Label(String.format("%.2f", turretEntity.getComponent(IncomeUpgradeComponent.class).getIncomeRate()), createLabelStyle());
Expand All @@ -314,8 +351,8 @@ public void exit(InputEvent event, float x, float y, int pointer, Actor toActor)
innerUpgradeTable.row();

Drawable income = new TextureRegionDrawable(new TextureRegion(new Texture("images/scrap_upgrade.png")));
ImageButton attackStyleButton = new ImageButton(asStyle);
attackStyleButton.addListener(new ClickListener() {
upgradeIncome = new ImageButton(income);
upgradeIncome.addListener(new ClickListener() {
@Override
public void clicked(InputEvent event, float x, float y) {
value = ServiceLocator.getCurrencyService().getScrap().getAmount();
Expand Down Expand Up @@ -371,10 +408,39 @@ public void exit(InputEvent event, float x, float y, int pointer, Actor toActor)
return upgradeTable;
}

/**
* Creates a label style for the upgrade table
* @return the label style
*/
private LabelStyle createLabelStyle() {
LabelStyle style = new LabelStyle();
style.font = new BitmapFont();
style.fontColor = Color.WHITE;
return style;
}


/**
* Update method for the UpgradeUIComponent, checks if the entity is disposed and removes the upgrade table
*/
public void checkForDispose() {
if (!upgradeTables.isEmpty()) {
// Iterate over the entries in the upgradeTables map
Iterator<Map.Entry<Entity, Table>> iterator = upgradeTables.entrySet().iterator();
// logger.info("upgradeTables size: " + upgradeTables.size());
while (iterator.hasNext()) {
Map.Entry<Entity, Table> entry = iterator.next();
Entity entity = entry.getKey();
// logger.info("entity: " + entity);
// Check if the entity is disposed (use your own disposal condition)
if (!ServiceLocator.getEntityService().findEntityExistence(entity.getId())) {
Table upgradeTable = entry.getValue();
upgradeTable.remove(); // Remove the upgrade table
iterator.remove(); // Remove the entry from the map
}
}
}
}


}
15 changes: 10 additions & 5 deletions source/core/src/main/com/csse3200/game/screens/MainGameScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ public class MainGameScreen extends ScreenAdapter {
private final Renderer renderer;
private final PhysicsEngine physicsEngine;

private InputComponent upgradedInputHandler;
private final Stage stage;
static int screenWidth = Gdx.graphics.getWidth();
static int screenHeight = Gdx.graphics.getHeight();
Expand Down Expand Up @@ -134,17 +135,14 @@ public MainGameScreen(GdxGame game) {
renderer = RenderFactory.createRenderer();
renderer.getCamera().getEntity().setPosition(CAMERA_POSITION);
renderer.getDebug().renderPhysicsWorld(physicsEngine.getWorld());

InputComponent inputHandler = new DropInputComponent(renderer.getCamera().getCamera());
InputComponent buildHandler = new BuildInputComponent(renderer.getCamera().getCamera());
InputComponent UpgradedInputHandler = new UpgradeUIComponent(renderer.getCamera().getCamera(), renderer.getStage());
upgradedInputHandler = new UpgradeUIComponent(renderer.getCamera().getCamera(), renderer.getStage());
InputComponent engineerInputHandler = new EngineerInputComponent(game, renderer.getCamera().getCamera());

ServiceLocator.getInputService().register(inputHandler);
ServiceLocator.getInputService().register(buildHandler);
ServiceLocator.getInputService().register(engineerInputHandler);
ServiceLocator.getInputService().register(UpgradedInputHandler);

ServiceLocator.getInputService().register(upgradedInputHandler);
ServiceLocator.getCurrencyService().getDisplay().setCamera(renderer.getCamera().getCamera());

loadAssets();
Expand Down Expand Up @@ -214,6 +212,9 @@ public void render(float delta) {
physicsEngine.update();
ServiceLocator.getEntityService().update();

// Checks if tower selected is dead
this.getUpgradedInputHandler().checkForDispose();

// Check if the game has ended
if (ServiceLocator.getGameEndService().hasGameEnded()) {
ui.getEvents().trigger("lose");
Expand Down Expand Up @@ -317,4 +318,8 @@ private void playAmbientSound() {

ServiceLocator.getResourceService().getAsset(ambientSounds.random(), Sound.class).play(0.2f);
}

private UpgradeUIComponent getUpgradedInputHandler() {
return (UpgradeUIComponent) upgradedInputHandler;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ public class TurretSelectionScreen extends ScreenAdapter {
private TextButton backButton;
private static final Logger logger = LoggerFactory.getLogger(MainMenuScreen.class);

/**
* Constructor for the TurretSelectionScreen
* @param game The game object
*/
public TurretSelectionScreen(GdxGame game) {
this.game = game;
stage = new Stage(new ScreenViewport());
Expand Down Expand Up @@ -232,10 +236,23 @@ public void render(float delta) {
stage.draw(); // Draw the stage
}

/**
* Returns the list of selected turrets
* @return The list of selected turrets
*/
public List<TowerType> getTurretList() {
return turretList;
}

/**
* Creates a button with the specified images and text
* @param defaultImageFilePath The file path to the default image
* @param alternateImageFilePath The file path to the alternate image
* @param cost The cost of the turret
* @param towerName The name of the turret
* @param turretDesc The description of the turret
* @return The created button
*/
private TextButton createButton(String defaultImageFilePath, String alternateImageFilePath, String cost,
String towerName, String turretDesc) {
Drawable defaultDrawable = new TextureRegionDrawable(new TextureRegion(new Texture(defaultImageFilePath)));
Expand Down Expand Up @@ -293,14 +310,23 @@ public void exit(InputEvent event, float x, float y, int pointer, com.badlogic.g
return tb;
}

/**
* Updates the description label
*/
private void updateDescriptionLabel() {
descriptionLabel.setText("Description: " + turretDescription);
}

/**
* Updates the description text
*/
private void updateDescriptionText() {
descText.setText(turretDescriptionText);
}

/**
* Disposes of the stage
*/
@Override
public void dispose() {
stage.dispose();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.csse3200.game.ai.tasks.AITaskComponent;
import com.csse3200.game.components.CombatStatsComponent;
import com.csse3200.game.components.tasks.CurrencyTask;
import com.csse3200.game.components.tasks.TowerCombatTask;
import com.csse3200.game.entities.Entity;
import com.csse3200.game.extensions.GameExtension;
Expand Down Expand Up @@ -78,4 +79,19 @@ void divideByZeroDefaultToIgnore() {
verify(towerUpgraderComponent).upgradeTower(TowerUpgraderComponent.UPGRADE.FIRERATE, 60);
assertEquals((1/12f), towerCombatTask.getFireRateInterval());
}

@Test
void incomeRate() {
entity.addComponent(towerUpgraderComponent);
AITaskComponent aiTaskComponent = new AITaskComponent();
ServiceLocator.registerTimeSource(mock(GameTime.class));
CurrencyTask currencyTask = new CurrencyTask(10, 10);
aiTaskComponent.addTask(currencyTask);
entity.addComponent(aiTaskComponent);
currencyTask.start();
entity.create();
entity.getEvents().trigger("upgradeTower", TowerUpgraderComponent.UPGRADE.INCOME, 60);
verify(towerUpgraderComponent).upgradeTower(TowerUpgraderComponent.UPGRADE.INCOME, 60);
assertEquals(60, currencyTask.getInterval());
}
}

0 comments on commit 9de3757

Please sign in to comment.