Skip to content

Commit

Permalink
Merge branch 'main' into Team-4---Waves
Browse files Browse the repository at this point in the history
  • Loading branch information
samsully committed Oct 3, 2023
2 parents 456c8db + 88cfdaa commit 16c2aa2
Show file tree
Hide file tree
Showing 9 changed files with 358 additions and 168 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
}
}
}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.ScreenAdapter;
import com.badlogic.gdx.audio.Music;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
Expand All @@ -23,6 +24,7 @@
import com.csse3200.game.screens.text.AnimatedText;
import com.csse3200.game.screens.Planets;
import com.csse3200.game.services.GameEndService;
import com.csse3200.game.services.ResourceService;
import com.csse3200.game.services.ServiceLocator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -45,6 +47,10 @@ public class LevelSelectScreen extends ScreenAdapter {
private BitmapFont font;

private Sprite background;
private String[] bgm = {
"sounds/background/pre_game/Sci-Fi8Loop_story.ogg"
};
private Music music;

// Stores a time to determine the frame of the planet
// TODO: Account for integer overflow
Expand Down Expand Up @@ -77,6 +83,12 @@ public void clicked(com.badlogic.gdx.scenes.scene2d.InputEvent event, float x, f
table1.pad(20); // Add padding to the top-right corner
table1.add(buttonTable).row(); // Add button table and move to the next row
stage.addActor(table1);

ServiceLocator.registerResourceService(new ResourceService());
ServiceLocator.getResourceService().loadMusic(bgm);
ServiceLocator.getResourceService().loadAll();
music = ServiceLocator.getResourceService().getAsset(bgm[0], Music.class);

}

@Override
Expand All @@ -85,6 +97,10 @@ public void show() {
background = new Sprite(new Texture(BG_PATH));
ServiceLocator.registerGameEndService(new GameEndService());
Gdx.input.setInputProcessor(stage);

music.setVolume(0.4f);
music.setLooping(true);
music.play();
}

/**
Expand Down Expand Up @@ -174,5 +190,6 @@ public void resize(int width, int height) {
public void dispose() {
stage.dispose();
batch.dispose();
music.dispose();
}
}
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;
}
}
Loading

0 comments on commit 16c2aa2

Please sign in to comment.