Skip to content

Commit

Permalink
cleanup of code, added button updating for shortcut keys
Browse files Browse the repository at this point in the history
  • Loading branch information
The-AhmadAA committed Oct 15, 2023
1 parent 04b86aa commit 31ca8f4
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ public class MainGameDisplay extends UIComponent {
};
private Sound click;
private Sound openSound;
private GdxGame game;
private final GdxGame game;
private Array<TowerType> towers = new Array<>();
private final Array<ImageButton> towerButtons = new Array<>();
private ImageButton tower1;
private ImageButton tower2;
private ImageButton tower3;
Expand Down Expand Up @@ -107,10 +108,16 @@ private void addActors() {
ServiceLocator.setTowerTypes(towers);

tower1 = new ImageButton(skin, towers.get(0).getSkinName());
towerButtons.add(tower1);
tower2 = new ImageButton(skin, towers.get(1).getSkinName());
towerButtons.add(tower2);
tower3 = new ImageButton(skin, towers.get(2).getSkinName());
towerButtons.add(tower3);
tower4 = new ImageButton(skin, towers.get(3).getSkinName());
towerButtons.add(tower4);
tower5 = new ImageButton(skin, towers.get(4).getSkinName());
towerButtons.add(tower5);

TextButton pauseBtn = ButtonFactory.createButton("Pause");

// Spawns a pause menu when the button is pressed.
Expand All @@ -133,15 +140,14 @@ public void clicked(InputEvent event, float x, float y) {
TowerType selected = ServiceLocator.getCurrencyService().getTower();
if (selected == towers.get(0) ) {
ServiceLocator.getCurrencyService().setTowerType(null);
tower1.setChecked(false);

towerToggle(null, false);

} else {
ServiceLocator.getCurrencyService().setTowerType(towers.get(0));
tower1.setChecked(true);
tower2.setChecked(false);
tower3.setChecked(false);
tower4.setChecked(false);
tower5.setChecked(false);

towerToggle(tower1, false);

}
click.play(0.4f);
}
Expand All @@ -158,14 +164,14 @@ public void clicked(InputEvent event, float x, float y) {
TowerType selected = ServiceLocator.getCurrencyService().getTower();
if (selected == towers.get(1) ) {
ServiceLocator.getCurrencyService().setTowerType(null);
tower2.setChecked(false);

towerToggle(null, false);

} else {
ServiceLocator.getCurrencyService().setTowerType(towers.get(1));
tower1.setChecked(false);
tower2.setChecked(true);
tower3.setChecked(false);
tower4.setChecked(false);
tower5.setChecked(false);

towerToggle(tower2, false);

}
click.play(0.4f);
}
Expand All @@ -181,17 +187,16 @@ public void clicked(InputEvent event, float x, float y) {
TowerType selected = ServiceLocator.getCurrencyService().getTower();
if (selected == towers.get(2)) {
ServiceLocator.getCurrencyService().setTowerType(null);
tower3.setChecked(false);

towerToggle(null, false);

} else {
ServiceLocator.getCurrencyService().setTowerType(towers.get(2));
if (ServiceLocator.getCurrencyService().getScrap().getAmount()
>= Integer.parseInt(towers.get(2).getPrice())) {
tower3.setDisabled(false);
tower1.setChecked(false);
tower2.setChecked(false);
tower3.setChecked(true);
tower4.setChecked(false);
tower5.setChecked(false);

towerToggle(tower3, false);

} else {
tower3.setDisabled(true);
}
Expand All @@ -211,14 +216,14 @@ public void clicked(InputEvent event, float x, float y) {
TowerType selected = ServiceLocator.getCurrencyService().getTower();
if (selected == towers.get(3)) {
ServiceLocator.getCurrencyService().setTowerType(null);
tower4.setChecked(false);

towerToggle(null, false);

} else {
ServiceLocator.getCurrencyService().setTowerType(towers.get(3));
tower1.setChecked(false);
tower2.setChecked(false);
tower3.setChecked(false);
tower4.setChecked(true);
tower5.setChecked(false);

towerToggle(tower4, false);

}
click.play(0.4f);
}
Expand All @@ -234,14 +239,14 @@ public void clicked(InputEvent event, float x, float y) {
TowerType selected = ServiceLocator.getCurrencyService().getTower();
if (selected == towers.get(4)) {
ServiceLocator.getCurrencyService().setTowerType(null);
tower5.setChecked(false);

towerToggle(tower5, false);

} else {
ServiceLocator.getCurrencyService().setTowerType(towers.get(4));
tower1.setChecked(false);
tower2.setChecked(false);
tower3.setChecked(false);
tower4.setChecked(false);
tower5.setChecked(true);

towerToggle(tower5, false);

}
click.play(0.4f);
}
Expand Down Expand Up @@ -270,43 +275,59 @@ public void clicked(InputEvent event, float x, float y) {
@Override
public void draw(SpriteBatch batch) {
// draw is handled by the stage
if (ServiceLocator.getCurrencyService().getTower() == null) {
towerUpdate();
}

tower1.setChecked(false);
tower2.setChecked(false);
tower3.setChecked(false);
tower4.setChecked(false);
tower5.setChecked(false);
}
int balance = ServiceLocator.getCurrencyService().getScrap().getAmount();
if (Integer.parseInt(towers.get(0).getPrice()) > balance) {
tower1.setDisabled(true);
} else {
tower1.setDisabled(false);
}
if (Integer.parseInt(towers.get(1).getPrice()) > balance) {
tower2.setDisabled(true);
} else {
tower2.setDisabled(false);
}
if (Integer.parseInt(towers.get(2).getPrice()) > balance) {
tower3.setDisabled(true);
/**
* Update function for the tower build menu buttons that is called with every draw, updates button states
* depending on button selection and currency balance
*/
private void towerUpdate() {
// no tower selected, set all to off
if (ServiceLocator.getCurrencyService().getTower() == null) {
// toggle all buttons to off
towerToggle(null, false);
} else {
tower3.setDisabled(false);
// for handling shortcut key selection of tower build buttons
for (int i = 0; i < towerButtons.size; i++) {
if (ServiceLocator.getCurrencyService().getTower() == towers.get(i)) {
towerToggle(towerButtons.get(i), false);
}
}
}
if (Integer.parseInt(towers.get(3).getPrice()) > balance) {
tower4.setDisabled(true);
} else {
tower4.setDisabled(false);
// update button state based on currency balance
int balance = ServiceLocator.getCurrencyService().getScrap().getAmount();
for (int i = 0; i < towerButtons.size; i++) {
towerButtons.get(i).setDisabled(Integer.parseInt(towers.get(i).getPrice()) > balance);
}
if (Integer.parseInt(towers.get(4).getPrice()) > balance) {
tower5.setDisabled(true);
}

/**
* Helper method for toggling tower build buttons. The expected behaviour is that if one button is pressed,
* all other buttons should be set to off. If a null button value is passed in then all buttons are set to off.
* Passing in isDisabled == true sets the button to 'disabled'. If null button and isDisabled == true, then all
* buttons will be set to 'disabled'.
* <p></p>
* @param towerButton the ImageButton which is being set to on, all others will be toggled off. If null, all buttons
* will be set to off
* @param isDisabled sets whether the button should be set to disabled, if used with null towerButton, all buttons
* will be disabled
*/
private void towerToggle(ImageButton towerButton, boolean isDisabled) {
if (towerButton == null) {
// set all buttons to off, disable if isDisabled
for (ImageButton button : towerButtons) {
button.setChecked(false);
button.setDisabled(isDisabled);
}
} else {
tower5.setDisabled(false);
// set the button corresponding to towerButton to on, all others to off
for (ImageButton button : towerButtons) {
button.setChecked(button == towerButton);
button.setDisabled(isDisabled && button == towerButton);
}

}
}

@Override
public float getZIndex() {
return Z_INDEX;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,12 @@
package com.csse3200.game.components.maingame;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.InputListener;
import com.badlogic.gdx.scenes.scene2d.actions.Actions;
import com.badlogic.gdx.scenes.scene2d.actions.SequenceAction;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.badlogic.gdx.scenes.scene2d.utils.Drawable;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.Null;
import com.csse3200.game.screens.TowerType;
import com.csse3200.game.services.ServiceLocator;
import com.csse3200.game.ui.ButtonFactory;
import com.csse3200.game.ui.UIComponent;
Expand All @@ -34,7 +24,6 @@ public class UIElementsDisplay extends UIComponent {
private TextButton remainingMobsButton;
private TextButton timerButton;
private LevelProgressBar progressbar;
private final int timer = 110;

@Override
public void create() {
Expand Down Expand Up @@ -67,7 +56,7 @@ private void addActors() {
}

public void updateLevelProgressBar() {
float totalSecs = (ServiceLocator.getTimeSource().getTime() / 1000);
float totalSecs = ((float) ServiceLocator.getTimeSource().getTime() / 1000);
progressbar.setValue(totalSecs);
}

Expand Down

0 comments on commit 31ca8f4

Please sign in to comment.