Skip to content

Commit

Permalink
Fix 4 code smells in FireworksTowerCombatTask (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 6703cb4 commit ef9a838
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public class FireworksTowerCombatTask extends DefaultTask implements PriorityTas
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 Down Expand Up @@ -111,7 +112,7 @@ public void updateTowerState() {
Entity newProjectile = ProjectileFactory.createSplitFireWorksFireball(PhysicsLayer.NPC,
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);
} else {
owner.getEntity().getEvents().trigger(IDLE);
Expand Down Expand Up @@ -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(FireworksTowerCombatTask.ATTACK, attack);
//Jump to IDLE state
fireworksTowerCombatTask.start();
fireworksTowerCombatTask.towerState = FireworksTowerCombatTask.STATE.IDLE;
fireworksTowerCombatTask.setTowerState(FireworksTowerCombatTask.STATE.IDLE);

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

fireworksTowerCombatTask.towerState = FireworksTowerCombatTask.STATE.IDLE;
fireworksTowerCombatTask.setTowerState(FireworksTowerCombatTask.STATE.IDLE);

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

0 comments on commit ef9a838

Please sign in to comment.