Skip to content

Commit

Permalink
added shortcut key functionality for build menu
Browse files Browse the repository at this point in the history
  • Loading branch information
The-AhmadAA committed Oct 10, 2023
1 parent 8a75052 commit c9ad35f
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ private void addActors() {
}
}

// Update the centrally located towerTypes list -
logger.info("In UIElementsDisplay, the towers being sent to ServiceLocator are " + towers);
ServiceLocator.setTowerTypes(towers);

// Create the buttons - TODO This needs overhauling to pretty buttons
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);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
package com.csse3200.game.input;

import com.badlogic.gdx.Input;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.graphics.Camera;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.utils.Array;
import com.csse3200.game.areas.ForestGameArea;
import com.csse3200.game.entities.Entity;
import com.csse3200.game.entities.EntityService;
import com.csse3200.game.entities.factories.TowerFactory;
import com.csse3200.game.screens.TowerType;
import com.csse3200.game.services.ServiceLocator;
import com.csse3200.game.utils.math.Vector2Utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -28,6 +32,7 @@ public class BuildInputComponent extends InputComponent {
};
private Sound buildSound;
private Sound errorSound;
private Array<TowerType> towers = new Array<>();

/**
* Constructor for the BuildInputComponent
Expand All @@ -37,6 +42,7 @@ public BuildInputComponent(Camera camera) {
this.entityService = ServiceLocator.getEntityService();
this.camera = camera;
loadSounds();
towers.addAll(ServiceLocator.getTowerTypes());
}

/**
Expand Down Expand Up @@ -79,6 +85,35 @@ public boolean touchDown(int screenX, int screenY, int pointer, int button) {
return false;
}

/**
* Triggers player events on specific keycodes.
*
* @return whether the input was processed
* @see InputProcessor#keyDown(int)
*/
@Override
public boolean keyDown(int keycode) {
switch (keycode) {
case Input.Keys.NUM_1:
ServiceLocator.getCurrencyService().setTowerType(towers.get(0));
return true;
case Input.Keys.NUM_2:
ServiceLocator.getCurrencyService().setTowerType(towers.get(1));
return true;
case Input.Keys.NUM_3:
ServiceLocator.getCurrencyService().setTowerType(towers.get(2));
return true;
case Input.Keys.NUM_4:
ServiceLocator.getCurrencyService().setTowerType(towers.get(3));
return true;
case Input.Keys.NUM_5:
ServiceLocator.getCurrencyService().setTowerType(towers.get(4));
return true;
default:
return false;
}
}

/**
* Instantiates and spawns the selected tower at the given x y coordinates on the tile map. Assumes that the given
* x and y coordinate is valid and that the TowerType exists in the CurrencyService.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.badlogic.gdx.scenes.scene2d.utils.Drawable;
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.viewport.ScreenViewport;
import com.csse3200.game.GdxGame;
import com.csse3200.game.services.ResourceService;
Expand Down Expand Up @@ -115,7 +116,12 @@ public void clicked(InputEvent event, float x, float y) {
@Override
public void clicked(InputEvent event, float x, float y) {
// Store the selected towers in the ServiceLocator for transferring across screens
ServiceLocator.setTowerTypes(selectedTurrets);;
// (as an Array)
Array<TowerType> towers = new Array<>();
for (TowerType t : selectedTurrets) {
towers.add(t);
}
ServiceLocator.setTowerTypes(towers);;
game.setScreen(GdxGame.ScreenType.MAIN_GAME);
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class ServiceLocator {
private static WaveService waveService;
private static MapService mapService;

private static Set<TowerType> towerTypes = new HashSet<>();
private static Array<TowerType> towerTypes = new Array<>();

public static CurrencyService getCurrencyService() {
return currencyService;
Expand Down Expand Up @@ -120,12 +120,24 @@ public static void registerMapService(MapService source) {
mapService = source;
}

public static void setTowerTypes(Set<TowerType> selectedTowers) {
towerTypes.clear();
towerTypes.addAll(selectedTowers);
}

public static Set<TowerType> getTowerTypes() {
public static void setTowerTypes(Array<TowerType> selectedTowers) {
if (towerTypes.isEmpty()) {
// set default towers
TowerType[] defaultTowers = {
TowerType.TNT,
TowerType.DROID,
TowerType.INCOME,
TowerType.WALL,
TowerType.WEAPON
};
towerTypes.addAll(defaultTowers);
} else{
towerTypes.clear();
towerTypes.addAll(selectedTowers);
}
}

public static Array<TowerType> getTowerTypes() {
return towerTypes;
}

Expand Down

0 comments on commit c9ad35f

Please sign in to comment.