Skip to content

Commit

Permalink
Fix 4 code smells in PierceTowerCombatTask (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 ef9a838 commit b61f658
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public class PierceTowerCombatTask 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 @@ -75,6 +75,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 Down Expand Up @@ -113,14 +114,14 @@ public void updateTowerState() {
Entity newProjectile = ProjectileFactory.createPierceFireBall(PhysicsLayer.NPC,
new Vector2(100, owner.getEntity().getPosition().y), new Vector2(2f, 2f));
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);
}
Expand All @@ -139,6 +140,7 @@ public STATE getState() {
/**
* 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 All @@ -159,4 +161,21 @@ public int getPriority() {
public boolean isTargetVisible() {
return physics.raycast(towerPosition, maxRangePosition, TARGET, hit);
}

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

/**
* Function for setting the tower's state
* @param newState The new state of this tower
*/
public void setTowerState(STATE newState) {
this.towerState = newState;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public void testUpdateTowerStateWithTargetInRange() {
entity.getEvents().addListener(PierceTowerCombatTask.ATTACK, attack);
//Jump to IDLE state
pierceTowerCombatTask.start();
pierceTowerCombatTask.towerState = PierceTowerCombatTask.STATE.IDLE;
pierceTowerCombatTask.setTowerState(PierceTowerCombatTask.STATE.IDLE);

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

pierceTowerCombatTask.towerState = PierceTowerCombatTask.STATE.IDLE;
pierceTowerCombatTask.setTowerState(PierceTowerCombatTask.STATE.IDLE);

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

0 comments on commit b61f658

Please sign in to comment.