From 6b936212f74d7321c4d74c3b9c9027b671a0025d Mon Sep 17 00:00:00 2001 From: Kevin <104761532+Hasakev@users.noreply.github.com> Date: Sat, 30 Sep 2023 23:22:31 +1000 Subject: [PATCH] Added tower click detection --- .../csse3200/game/areas/ForestGameArea.java | 4 ++ .../game/entities/factories/TowerFactory.java | 4 +- .../game/input/UpgradeUIComponent.java | 62 +++++++++++++++++++ .../csse3200/game/screens/MainGameScreen.java | 4 ++ 4 files changed, 72 insertions(+), 2 deletions(-) create mode 100644 source/core/src/main/com/csse3200/game/input/UpgradeUIComponent.java diff --git a/source/core/src/main/com/csse3200/game/areas/ForestGameArea.java b/source/core/src/main/com/csse3200/game/areas/ForestGameArea.java index 86fb45744..603a00b61 100644 --- a/source/core/src/main/com/csse3200/game/areas/ForestGameArea.java +++ b/source/core/src/main/com/csse3200/game/areas/ForestGameArea.java @@ -271,6 +271,10 @@ public void create() { // spawnWeaponTower(); // spawnGapScanners(); // spawnDroidTower(); + spawnScrap(); + spawnDroidTower(); + spawnTNTTower(); + spawnIncome(); } diff --git a/source/core/src/main/com/csse3200/game/entities/factories/TowerFactory.java b/source/core/src/main/com/csse3200/game/entities/factories/TowerFactory.java index 0e750647f..f3880c68c 100644 --- a/source/core/src/main/com/csse3200/game/entities/factories/TowerFactory.java +++ b/source/core/src/main/com/csse3200/game/entities/factories/TowerFactory.java @@ -24,7 +24,7 @@ import com.csse3200.game.rendering.AnimationRenderComponent; import com.csse3200.game.rendering.TextureRenderComponent; import com.csse3200.game.services.ServiceLocator; - +import com.csse3200.game.input.UpgradeUIComponent; /** * Factory to create a tower entity. * @@ -315,7 +315,7 @@ public static Entity createBaseTower() { .addComponent(new HitboxComponent().setLayer(PhysicsLayer.TOWER)) // TODO: we might have to change the names of the layers .addComponent(new PhysicsComponent().setBodyType(BodyType.StaticBody)) .addComponent(new TowerUpgraderComponent()); - + tower.setLayer(1); // Set priority to 1, which is 1 below scrap (which is 0) return tower; } } \ No newline at end of file diff --git a/source/core/src/main/com/csse3200/game/input/UpgradeUIComponent.java b/source/core/src/main/com/csse3200/game/input/UpgradeUIComponent.java new file mode 100644 index 000000000..52a06442c --- /dev/null +++ b/source/core/src/main/com/csse3200/game/input/UpgradeUIComponent.java @@ -0,0 +1,62 @@ +package com.csse3200.game.input; + +import com.badlogic.gdx.graphics.Camera; +import com.badlogic.gdx.math.Vector2; +import com.badlogic.gdx.math.Vector3; +import com.csse3200.game.areas.ForestGameArea; +import com.csse3200.game.components.tower.TowerUpgraderComponent; +import com.csse3200.game.entities.Entity; +import com.csse3200.game.entities.EntityService; +import com.csse3200.game.services.ServiceLocator; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class UpgradeUIComponent extends InputComponent { + private static final Logger logger = LoggerFactory.getLogger(ForestGameArea.class); + private final EntityService entityService; + private final Camera camera; + int value; + + /** + * Constructor for the UpgradeUIComponent + * @param camera the camera to be used, this is the camera that the game is rendered with + */ + public UpgradeUIComponent(Camera camera) { + this.value = ServiceLocator.getCurrencyService().getScrap().getAmount(); + this.entityService = ServiceLocator.getEntityService(); + this.camera = camera; + } + + /** + * Getter for the camera + * @return the camera + */ + public Camera getCamera() { + return camera; + } + + /** + * When the mouse is clicked, this method is called. + * + * @param screenX The x coordinate, origin is in the upper left corner + * @param screenY The y coordinate, origin is in the upper left corner + * @param pointer the pointer for the event. + * @param button the button + * @return + */ + @Override + public boolean touchDown(int screenX, int screenY, int pointer, int button) { + Vector3 worldCoordinates = new Vector3((float) screenX , (float) screenY, 0); + getCamera().unproject(worldCoordinates); // translate from screen to world coordinates + Vector2 cursorPosition = new Vector2(worldCoordinates.x, worldCoordinates.y); + Entity clickedEntity = entityService.getEntityAtPosition(cursorPosition.x, cursorPosition.y); + + if (clickedEntity != null && clickedEntity.getComponent(TowerUpgraderComponent.class) != null) { + logger.info("clicked a turret that is upgradable!"); + return true; + } + return false; + } + + +} diff --git a/source/core/src/main/com/csse3200/game/screens/MainGameScreen.java b/source/core/src/main/com/csse3200/game/screens/MainGameScreen.java index 13ad9eb18..42afa287f 100644 --- a/source/core/src/main/com/csse3200/game/screens/MainGameScreen.java +++ b/source/core/src/main/com/csse3200/game/screens/MainGameScreen.java @@ -32,6 +32,8 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.security.Provider; + /** * The game screen containing the main game. * @@ -95,7 +97,9 @@ public MainGameScreen(GdxGame game) { /* Input components */ InputComponent dropInputHandler = new DropInputComponent(renderer.getCamera().getCamera()); + InputComponent upgradeInputHandler = new UpgradeUIComponent(renderer.getCamera().getCamera()); ServiceLocator.getInputService().register(dropInputHandler); + ServiceLocator.getInputService().register(upgradeInputHandler); InputComponent engineerInputHandler = new EngineerInputComponent(game, renderer.getCamera().getCamera()); ServiceLocator.getInputService().register(engineerInputHandler);