Skip to content

Commit

Permalink
Fix 4 code smells in RicochetTowerCombatTask (2 medium, 2 low)
Browse files Browse the repository at this point in the history
  • Loading branch information
lenhatminh451 committed Oct 16, 2023
1 parent b61f658 commit ecfab7b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public class RicochetTowerCombatTask extends DefaultTask implements PriorityTask
public enum STATE {
IDLE, ATTACK, DEATH
}
public STATE towerState = STATE.IDLE;
private STATE towerState = STATE.IDLE;

/**
* @param priority Task priority when targets are detected (0 when nothing is present)
Expand Down Expand Up @@ -77,6 +77,7 @@ public void start() {
* updates the current state of the tower based on the current state of the game. If enemies are detected, attack
* state is activated and otherwise idle state remains.
*/
@Override
public void update() {
if (timeSource.getTime() >= endTime) {
updateTowerState();
Expand All @@ -89,7 +90,6 @@ public void update() {
* of the game. If enemies are detected, state of the tower is changed to attack state.
*/
public void updateTowerState() {

if (owner.getEntity().getComponent(CombatStatsComponent.class).getHealth() <= 0 && towerState != STATE.DEATH) {
owner.getEntity().getEvents().trigger(DEATH);
towerState = STATE.DEATH;
Expand All @@ -114,27 +114,41 @@ public void updateTowerState() {
// NEED TO DO USER TESTING TO FIGURE OUT THE BOUNCE COUNT
new Vector2(100, owner.getEntity().getPosition().y), new Vector2(2f, 2f), 3);
newProjectile.setPosition((float) (owner.getEntity().getPosition().x + 0.25),
(float) (owner.getEntity().getPosition().y));
(owner.getEntity().getPosition().y));
ServiceLocator.getEntityService().register(newProjectile);
}
}
shoot = !shoot;
}
case DEATH -> {
default -> { // DEATH
if (owner.getEntity().getComponent(AnimationRenderComponent.class).isFinished()) {
owner.getEntity().setFlagForDelete(true);
}
}
}
}

/**
* Function for getting the tower's state
*
* @return The state of this tower
*/
public STATE getState() {
return this.towerState;
}

/**
* Function for setting the tower's state
* @param newState The new state of this tower
*/
public void setState(STATE newState) {
this.towerState = newState;
}

/**
* stops the current animation and switches back the state of the tower to IDLE.
*/
@Override
public void stop() {
super.stop();
owner.getEntity().getEvents().trigger(IDLE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public void testUpdateTowerStateWithTargetInRange() {
entity.getEvents().addListener(RicochetTowerCombatTask.ATTACK, attack);
//Jump to IDLE state
ricochetTowerCombatTask.start();
ricochetTowerCombatTask.towerState = RicochetTowerCombatTask.STATE.IDLE;
ricochetTowerCombatTask.setState(RicochetTowerCombatTask.STATE.IDLE);

ServiceLocator.getPhysicsService().getPhysics().update();
entity.update();
Expand Down Expand Up @@ -92,7 +92,7 @@ public void testUpdateTowerStateWithTargetNotInRange() {
entity.getEvents().addListener(RicochetTowerCombatTask.IDLE, idle);
entity.getEvents().addListener(RicochetTowerCombatTask.ATTACK, attack);

ricochetTowerCombatTask.towerState = RicochetTowerCombatTask.STATE.IDLE;
ricochetTowerCombatTask.setState(RicochetTowerCombatTask.STATE.IDLE);

ServiceLocator.getPhysicsService().getPhysics().update();
entity.update();
Expand Down

0 comments on commit ecfab7b

Please sign in to comment.