Skip to content

Commit

Permalink
fixed key shortcut bug that set towers to default
Browse files Browse the repository at this point in the history
  • Loading branch information
The-AhmadAA committed Oct 11, 2023
1 parent 03c0905 commit 6215af1
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.graphics.Camera;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
Expand All @@ -15,6 +16,7 @@
import com.badlogic.gdx.scenes.scene2d.utils.Drawable;
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;
import com.badlogic.gdx.utils.Align;
import com.csse3200.game.services.GameTime;
import com.csse3200.game.services.ServiceLocator;
import com.csse3200.game.ui.UIComponent;
import com.badlogic.gdx.scenes.scene2d.actions.Actions;
Expand Down Expand Up @@ -89,6 +91,15 @@ public void updateScrapsStats() {
scrapsTb.getLabel().setText(text);
}

/**
* Displays a warning animation of the scraps display if the player tries to
* build something that costs more than the balance
*/
public void scrapBalanceFlash() {
// TODO: IMPLEMENT THIS
scrapsTb.setText("Insufficient!");
}

/**
* Updates the currency (Crystals) value on the UI component
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
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 @@ -33,6 +32,7 @@ public class BuildInputComponent extends InputComponent {
private Sound buildSound;
private Sound errorSound;
private Array<TowerType> towers = new Array<>();
private Array<TowerType> defaultTowers = new Array<>();

/**
* Constructor for the BuildInputComponent
Expand All @@ -43,6 +43,21 @@ public BuildInputComponent(Camera camera) {
this.camera = camera;
loadSounds();
towers.addAll(ServiceLocator.getTowerTypes());

// logger.info("selected towers in buildInputComponent are " + towers);
TowerType[] defaultTowerTypes = {
TowerType.TNT,
TowerType.DROID,
TowerType.INCOME,
TowerType.WALL,
TowerType.WEAPON
};
defaultTowers.addAll(defaultTowerTypes);

if (towers.isEmpty()) {
ServiceLocator.setTowerTypes(defaultTowers);
towers = defaultTowers;
}
}

/**
Expand Down Expand Up @@ -74,12 +89,12 @@ public boolean touchDown(int screenX, int screenY, int pointer, int button) {

// determine if the tile is unoccupied
boolean tileOccupied = entityService.entitiesInTile((int)cursorPosition.x, (int)cursorPosition.y);
logger.debug("Tile is occupied: " + tileOccupied );
// logger.debug("Tile is occupied: " + tileOccupied );

// check that no entities are occupying the tile
if (!tileOccupied) {
buildTower((int)cursorPosition.x, (int)cursorPosition.y);
logger.debug("spawning a tower at {}, {}", cursorPosition.x, cursorPosition.y);
// logger.debug("spawning a tower at {}, {}", cursorPosition.x, cursorPosition.y);
return true;
} else {
// TODO: Create a tile indication of invalid placement here??
Expand All @@ -95,7 +110,7 @@ public boolean touchDown(int screenX, int screenY, int pointer, int button) {
* @see InputProcessor#keyDown(int)
*/
@Override
public boolean keyDown(int keycode) {
public boolean keyUp(int keycode) {
switch (keycode) {
case Input.Keys.NUM_1:
ServiceLocator.getCurrencyService().setTowerType(towers.get(0));
Expand Down Expand Up @@ -126,6 +141,7 @@ public boolean keyDown(int keycode) {
*/
public void buildTower(int x, int y) {
// fetch the currently set TowerType in the currency service, and its associated build cost.

TowerType tower = ServiceLocator.getCurrencyService().getTower();
if (tower != null) {
// fetch the price of the selected tower and attempt to instantiate
Expand Down Expand Up @@ -159,6 +175,7 @@ public void buildTower(int x, int y) {
// play a sound to indicate an invalid action
long soundId = errorSound.play();
errorSound.setVolume(soundId, 1f);
ServiceLocator.getCurrencyService().getDisplay().scrapBalanceFlash();
// TODO: add a visual indication of the build fail, through
// currency display flash
}
Expand All @@ -169,9 +186,14 @@ public void buildTower(int x, int y) {
* Load the sound assets related to in-game tower building activity
*/
private void loadSounds() {
ServiceLocator.getResourceService().loadSounds(sounds);
ServiceLocator.getResourceService().loadAll();
buildSound = ServiceLocator.getResourceService().getAsset("sounds/economy/buildSound.ogg", Sound.class);
errorSound = ServiceLocator.getResourceService().getAsset("sounds/ui/Switch/NA_SFUI_Vol1_switch_01.ogg", Sound.class);
try {
ServiceLocator.getResourceService().loadSounds(sounds);
ServiceLocator.getResourceService().loadAll();
buildSound = ServiceLocator.getResourceService().getAsset("sounds/economy/buildSound.ogg", Sound.class);
errorSound = ServiceLocator.getResourceService().getAsset("sounds/ui/Switch/NA_SFUI_Vol1_switch_01.ogg", Sound.class);

} catch (NullPointerException e) {
logger.error("BuildInputComponent line 173: Couldn't load sounds for build menu");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -121,20 +121,9 @@ public static void registerMapService(MapService source) {
}

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);
}

towerTypes.clear();
towerTypes.addAll(selectedTowers);
}

public static Array<TowerType> getTowerTypes() {
Expand All @@ -151,6 +140,7 @@ public static void clear() {
gameEndService = null;
waveService = null;
mapService = null;
towerTypes.clear();
}

private ServiceLocator() {
Expand Down
4 changes: 4 additions & 0 deletions source/core/src/main/com/csse3200/game/ui/UIComponent.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,8 @@ public int getLayer() {
public float getZIndex() {
return 1f;
}

public static Skin getSkin() {
return skin;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,14 @@
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Graphics;
import com.badlogic.gdx.graphics.Camera;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.math.Vector2;
import com.csse3200.game.areas.terrain.TerrainComponent;
import com.csse3200.game.components.CameraComponent;
import com.csse3200.game.entities.Entity;
import com.csse3200.game.entities.EntityService;
import com.csse3200.game.entities.factories.TowerFactory;
import com.csse3200.game.extensions.GameExtension;
import com.csse3200.game.physics.PhysicsService;
import com.csse3200.game.rendering.DebugRenderer;
import com.csse3200.game.rendering.RenderService;
import com.csse3200.game.screens.TowerType;
import com.csse3200.game.services.*;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -98,21 +95,47 @@ void shouldRejectOccupiedOrInvalidTile() {

@Test
void shouldHandleMissingMapService() {

when(ServiceLocator.getMapService()).thenReturn(null);
assertFalse(buildInputComponent.touchDown(5,5,7,8));
}

@Test
void shouldHandleMissingCurrencyService() {
when(ServiceLocator.getCurrencyService()).thenReturn(null);
assertFalse(buildInputComponent.touchDown(5,5,7,8));
}

@Test
void shouldHandleNullTowerName() {
TowerType towerType = mock(TowerType.class);
when(towerType.getTowerName()).thenReturn(null);
ServiceLocator.getCurrencyService().setTowerType(towerType);
}

@Test
void shouldHandleInvalidTower() {
void shouldHandleNullTowerDesc() {
TowerType towerType = mock(TowerType.class);
when(towerType.getDescription()).thenReturn(null);
ServiceLocator.getCurrencyService().setTowerType(towerType);
}

@Test
void shouldHandleNullTowerCost() {
TowerType towerType = mock(TowerType.class);
when(towerType.getPrice()).thenReturn(null);
ServiceLocator.getCurrencyService().setTowerType(towerType);
}

@Test
void shouldHandleMissingEntityService() {
void shouldHandleInvalidTowerName() {
TowerType towerType = mock(TowerType.class);
when(towerType.getTowerName()).thenReturn(null);
ServiceLocator.getCurrencyService().setTowerType(towerType);
}

@Test
void shouldHandleMissingEntityService() {
when(ServiceLocator.getEntityService()).thenReturn(null);
assertFalse(buildInputComponent.touchDown(5,5,7,8));
}
}

0 comments on commit 6215af1

Please sign in to comment.