Skip to content

Commit

Permalink
Added tower click detection
Browse files Browse the repository at this point in the history
  • Loading branch information
Hasakev committed Sep 30, 2023
1 parent 2b68ed9 commit 6b93621
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,10 @@ public void create() {
// spawnWeaponTower();
// spawnGapScanners();
// spawnDroidTower();
spawnScrap();
spawnDroidTower();
spawnTNTTower();
spawnIncome();

}

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


}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.security.Provider;

/**
* The game screen containing the main game.
*
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 6b93621

Please sign in to comment.