Skip to content

Commit

Permalink
Implemented the upgrading of a tower's max health, including changes …
Browse files Browse the repository at this point in the history
…to CombatStatsComponent to alter the fullHealth variable.
  • Loading branch information
AlasdairS4698737 committed Sep 3, 2023
1 parent 98c3dc5 commit c75f782
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class CombatStatsComponent extends Component {
private static final Logger logger = LoggerFactory.getLogger(CombatStatsComponent.class);
private int health;
private int baseAttack;
private final int fullHealth;
private int fullHealth;
private String state;
private ArrayList<Integer> drops;
private ArrayList<Melee> closeRangeAbilities;
Expand Down Expand Up @@ -99,6 +99,25 @@ public void addHealth(int health) {
changeState();
}

/**
* Returns the entity's fullHealth value (note that this does not influence the ability to set its actual health)
*
* @return The entity's fullHealth variable
*/
public int getMaxHealth() {
return fullHealth;
}

/**
* Sets the entity's fullHealth variable.
* Intended for when the entity's maximum health must be changed after creation, like upgrading a turret's HP.
*
* @param newMaxHealth The new value fullHealth should be set to
*/
public void setMaxHealth(int newMaxHealth) {
fullHealth = newMaxHealth;
}

/**
* Returns the entity's base attack damage.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public void create() {
void upgradeTower(UPGRADE upgradeType, int value) {
switch (upgradeType) {
case ATTACK -> {upgradeTowerAttack(value);}
case MAXHP -> {/*Not implemented yet either*/}
case MAXHP -> {upgradeTowerMaxHealth(value);}
case FIRERATE -> {/*Not implemented at the present moment*/}
}
}
Expand All @@ -41,4 +41,15 @@ void upgradeTowerAttack(int increase) {
int oldAttack = getEntity().getComponent(CombatStatsComponent.class).getBaseAttack();
getEntity().getComponent(CombatStatsComponent.class).setBaseAttack(oldAttack + increase);
}

/**
* Increases the tower's maximum health, and restores the tower's health to the new maximum.
*
* @param increase The amount that the max health stat should increase by.
*/
void upgradeTowerMaxHealth(int increase) {
int oldMaxHealth = getEntity().getComponent(CombatStatsComponent.class).getMaxHealth();
getEntity().getComponent(CombatStatsComponent.class).setMaxHealth(oldMaxHealth + increase);
getEntity().getComponent(CombatStatsComponent.class).setHealth(oldMaxHealth + increase);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import static org.mockito.Mockito.*;

@ExtendWith(GameExtension.class)
class TowerUpgradeComponentTest {
class TowerUpgraderComponentTest {
Entity entity;

@BeforeEach
Expand All @@ -27,4 +27,16 @@ void increaseAttackStat() {
verify(towerUpgraderComponent).upgradeTower(TowerUpgraderComponent.UPGRADE.ATTACK, 10);
assertEquals(20, combatStatsComponent.getBaseAttack());
}

@Test
void increaseMaxHealthStat() {
TowerUpgraderComponent towerUpgraderComponent = spy(TowerUpgraderComponent.class);
CombatStatsComponent combatStatsComponent = new CombatStatsComponent(100,10);
entity.addComponent(towerUpgraderComponent);
entity.addComponent(combatStatsComponent);
entity.create();
entity.getEvents().trigger("upgradeTower", TowerUpgraderComponent.UPGRADE.MAXHP, 50);
verify(towerUpgraderComponent).upgradeTower(TowerUpgraderComponent.UPGRADE.MAXHP, 50);
assertEquals(150, combatStatsComponent.getMaxHealth());
}
}

0 comments on commit c75f782

Please sign in to comment.