Skip to content

Commit

Permalink
Fix code smells in DroidCombatTask
Browse files Browse the repository at this point in the history
  • Loading branch information
lenhatminh451 committed Oct 16, 2023
1 parent cfcd12a commit 36e9a9d
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public class DroidCombatTask extends DefaultTask implements PriorityTask {
public enum STATE {
IDLE, UP, DOWN, SHOOT_UP, SHOOT_DOWN, WALK, DIE
}
public STATE towerState = STATE.WALK;
private STATE towerState = STATE.WALK;

/**
* @param priority Task priority when targets are detected (0 when nothing detected). Must be a positive integer.
Expand Down Expand Up @@ -90,7 +90,7 @@ public void update() {
} else {
endTime = timeSource.getTime() + (INTERVAL * 1000);
}
}
}
}

/**
Expand All @@ -104,75 +104,31 @@ public void updateTowerState() {
towerState = STATE.DIE;
return;
}

switch (towerState) {
case WALK -> {
owner.getEntity().getEvents().trigger(WALK);
towerState = STATE.IDLE;
handleWalkState();
}
case IDLE -> {
if (isTargetVisible()) {
owner.getEntity().getEvents().trigger(ATTACK_UP);
owner.getEntity().getEvents().trigger(SHOOT_UP);
towerState = STATE.DOWN;
} else {
owner.getEntity().getEvents().trigger(IDLE);
}
handleIdleState();
}
case SHOOT_DOWN -> {
if (isTargetVisible()) {
owner.getEntity().getEvents().trigger(ATTACK_DOWN);
owner.getEntity().getEvents().trigger(SHOOT_DOWN);
towerState = STATE.UP;
} else {
owner.getEntity().getEvents().trigger(GO_UP);
towerState = STATE.UP;
}
handleShootDownState();
}
case SHOOT_UP -> {
if (isTargetVisible()) {

owner.getEntity().getEvents().trigger(ATTACK_UP);
owner.getEntity().getEvents().trigger(SHOOT_UP);
towerState = STATE.DOWN;
} else {
owner.getEntity().getEvents().trigger(IDLE);
towerState = STATE.IDLE;
}
handleShootUpState();
}
case DOWN -> {
if (isTargetVisible()) {
owner.getEntity().getEvents().trigger(GO_DOWN);
towerState = STATE.SHOOT_DOWN;
} else {
owner.getEntity().getEvents().trigger(IDLE);
towerState = STATE.IDLE;
}
handleDownState();
}
case UP -> {
if (isTargetVisible()) {
owner.getEntity().getEvents().trigger(GO_UP);
towerState = STATE.SHOOT_UP;
} else {
owner.getEntity().getEvents().trigger(GO_UP);
towerState = STATE.IDLE;


}
handleUpState();
}
case DIE -> {
if (owner.getEntity().getComponent(AnimationRenderComponent.class).isFinished()) {
owner.getEntity().setFlagForDelete(true);
}
default -> { // DIE
handleDieState();
}
}
}
/**
* For stopping the running task
*/
@Override
public void stop() {
super.stop();
}

/**
* Returns the current state of the tower.
Expand Down Expand Up @@ -214,4 +170,104 @@ public float getFireRateInterval() {
return fireRateInterval;
}

}
/**
* Function for setting the tower state
* @param newState New state of the tower
*/
public void setTowerState(STATE newState) {
this.towerState = newState;
}

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

/**
* Function triggers walk and changes state from WALK to IDLE
*/
private void handleWalkState() {
owner.getEntity().getEvents().trigger(WALK);
towerState = STATE.IDLE;
}

/**
* Function triggers actions at IDLE state
*/
private void handleIdleState() {
if (isTargetVisible()) {
owner.getEntity().getEvents().trigger(ATTACK_UP);
owner.getEntity().getEvents().trigger(SHOOT_UP);
towerState = STATE.DOWN;
} else {
owner.getEntity().getEvents().trigger(IDLE);
}
}

/**
* Function triggers actions at SHOOT_DOWN state, then switch to UP
*/
private void handleShootDownState() {
if (isTargetVisible()) {
owner.getEntity().getEvents().trigger(ATTACK_DOWN);
owner.getEntity().getEvents().trigger(SHOOT_DOWN);
} else {
owner.getEntity().getEvents().trigger(GO_UP);
}

towerState = STATE.UP;
}

/**
* Function triggers actions at SHOOT_UP state, then switch to DOWN or IDLE
*/
private void handleShootUpState() {
if (isTargetVisible()) {
owner.getEntity().getEvents().trigger(ATTACK_UP);
owner.getEntity().getEvents().trigger(SHOOT_UP);
towerState = STATE.DOWN;
} else {
owner.getEntity().getEvents().trigger(IDLE);
towerState = STATE.IDLE;
}
}

/**
* Function triggers actions at DOWN state, then switch to SHOOT_DOWN or IDLE
*/
private void handleDownState() {
if (isTargetVisible()) {
owner.getEntity().getEvents().trigger(GO_DOWN);
towerState = STATE.SHOOT_DOWN;
} else {
owner.getEntity().getEvents().trigger(IDLE);
towerState = STATE.IDLE;
}
}

/**
* Function triggers actions at UP state, then switch to SHOOT_UP or IDLE
*/
private void handleUpState() {
if (isTargetVisible()) {
owner.getEntity().getEvents().trigger(GO_UP);
towerState = STATE.SHOOT_UP;
} else {
owner.getEntity().getEvents().trigger(GO_UP);
towerState = STATE.IDLE;
}
}

/**
* Function handles DIE state
*/
private void handleDieState() {
if (owner.getEntity().getComponent(AnimationRenderComponent.class).isFinished()) {
owner.getEntity().setFlagForDelete(true);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public void testUpdateTowerStateWithTargetInRange() {
entity.getEvents().addListener(DroidCombatTask.SHOOT_DOWN,shootDown);
//Jump to IDLE state
droidCombatTask.start();
droidCombatTask.towerState = DroidCombatTask.STATE.IDLE;
droidCombatTask.setTowerState(DroidCombatTask.STATE.IDLE);

ServiceLocator.getPhysicsService().getPhysics().update();
entity.update();
Expand Down Expand Up @@ -110,7 +110,7 @@ public void testUpdateTowerStateWithTargetNotInRange() {
entity.getEvents().addListener(DroidCombatTask.IDLE, idle);
entity.getEvents().addListener(DroidCombatTask.ATTACK_UP,attackUp);
//Jump to IDLE state
droidCombatTask.towerState = DroidCombatTask.STATE.IDLE;
droidCombatTask.setTowerState(DroidCombatTask.STATE.IDLE);

ServiceLocator.getPhysicsService().getPhysics().update();
entity.update();
Expand All @@ -122,7 +122,6 @@ public void testUpdateTowerStateWithTargetNotInRange() {
verify(idle).handle();
verifyNoInteractions(attackUp);
assertEquals(DroidCombatTask.STATE.IDLE, droidCombatTask.getState());

}


Expand Down

0 comments on commit 36e9a9d

Please sign in to comment.