diff --git a/source/core/src/main/com/csse3200/game/components/tasks/DroidCombatTask.java b/source/core/src/main/com/csse3200/game/components/tasks/DroidCombatTask.java index 1c597fc2e..a3c8d5822 100644 --- a/source/core/src/main/com/csse3200/game/components/tasks/DroidCombatTask.java +++ b/source/core/src/main/com/csse3200/game/components/tasks/DroidCombatTask.java @@ -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. @@ -90,7 +90,7 @@ public void update() { } else { endTime = timeSource.getTime() + (INTERVAL * 1000); } - } + } } /** @@ -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. @@ -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); + } + } +} \ No newline at end of file diff --git a/source/core/src/test/com/csse3200/game/components/tasks/DroidCombatTaskTest.java b/source/core/src/test/com/csse3200/game/components/tasks/DroidCombatTaskTest.java index 5878ea061..6e888c1c3 100644 --- a/source/core/src/test/com/csse3200/game/components/tasks/DroidCombatTaskTest.java +++ b/source/core/src/test/com/csse3200/game/components/tasks/DroidCombatTaskTest.java @@ -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(); @@ -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(); @@ -122,7 +122,6 @@ public void testUpdateTowerStateWithTargetNotInRange() { verify(idle).handle(); verifyNoInteractions(attackUp); assertEquals(DroidCombatTask.STATE.IDLE, droidCombatTask.getState()); - }