diff --git a/source/core/src/main/com/csse3200/game/components/tasks/CurrencyTask.java b/source/core/src/main/com/csse3200/game/components/tasks/CurrencyTask.java index 1793115c6..dac8bbae3 100644 --- a/source/core/src/main/com/csse3200/game/components/tasks/CurrencyTask.java +++ b/source/core/src/main/com/csse3200/game/components/tasks/CurrencyTask.java @@ -3,7 +3,9 @@ import com.badlogic.gdx.math.Vector2; import com.csse3200.game.ai.tasks.DefaultTask; import com.csse3200.game.ai.tasks.PriorityTask; +import com.csse3200.game.components.CombatStatsComponent; import com.csse3200.game.currency.Scrap; +import com.csse3200.game.rendering.AnimationRenderComponent; import com.csse3200.game.services.GameTime; import com.csse3200.game.services.ServiceLocator; import org.slf4j.Logger; @@ -22,6 +24,13 @@ public class CurrencyTask extends DefaultTask implements PriorityTask { private final int currencyAmount = scrap.getAmount(); // amount of currency to update private static final String IDLE = "idleStartEco"; private static final String MOVE = "moveStartEco"; + private static final String DEATH = "deathStartEco"; + + public enum STATE { + IDLE, DEATH + } + public STATE towerState = STATE.IDLE; + /** * @param priority Task priority for currency updates. Must be a positive integer. @@ -40,7 +49,8 @@ public CurrencyTask(int priority, int interval) { public void start() { super.start(); owner.getEntity().getEvents().addListener("addIncome",this::changeInterval); - endTime = timeSource.getTime() + (30 * 1000L); + // TODO: GOT RID OF THE 30 TIMES MULTIPLIER + endTime = timeSource.getTime() + (1000L); owner.getEntity().getEvents().trigger(IDLE); } @@ -52,14 +62,38 @@ public void start() { @Override public void update() { if (timeSource.getTime() >= endTime) { - owner.getEntity().getEvents().trigger(MOVE); - updateCurrency(); // update currency + updateTowerState(); logger.info(String.format("Interval: %d", interval)); endTime = timeSource.getTime() + (interval * 1000L); // reset end time } } + /** + * This method acts is the state machine for IncomeTower. Relevant animations are triggered based on relevant state + * of the game. If the tower runs out of health it dies. + */ + public void updateTowerState() { + if (owner.getEntity().getComponent(CombatStatsComponent.class).getHealth() <= 0 && towerState != STATE.DEATH) { + owner.getEntity().getEvents().trigger(DEATH); + towerState = STATE.DEATH; + } + + switch (towerState) { + case IDLE -> { + owner.getEntity().getEvents().trigger(MOVE); + updateCurrency(); // update currency + towerState = STATE.IDLE; + } + case DEATH -> { + if (owner.getEntity().getComponent(AnimationRenderComponent.class).isFinished()) { + owner.getEntity().setFlagForDelete(true); + } + } + } + } + + /** * Updates the currency based on time intervals. */ diff --git a/source/core/src/main/com/csse3200/game/components/tasks/WallTowerDestructionTask.java b/source/core/src/main/com/csse3200/game/components/tasks/WallTowerDestructionTask.java index 988ff0071..390c3359d 100644 --- a/source/core/src/main/com/csse3200/game/components/tasks/WallTowerDestructionTask.java +++ b/source/core/src/main/com/csse3200/game/components/tasks/WallTowerDestructionTask.java @@ -41,7 +41,7 @@ public class WallTowerDestructionTask extends DefaultTask implements PriorityTas private final RaycastHit hit = new RaycastHit(); public enum STATE { - IDLE, ATTACK, DEATH + IDLE, DEATH } public STATE towerState = STATE.IDLE; @@ -97,7 +97,7 @@ public void updateTowerState() { switch (towerState) { case IDLE -> { owner.getEntity().getEvents().trigger(IDLE); - towerState = STATE.ATTACK; + towerState = STATE.IDLE; } case DEATH -> { if (owner.getEntity().getComponent(AnimationRenderComponent.class).isFinished()) { diff --git a/source/core/src/main/com/csse3200/game/entities/factories/TowerFactory.java b/source/core/src/main/com/csse3200/game/entities/factories/TowerFactory.java index a92dbb234..ca8f28b98 100644 --- a/source/core/src/main/com/csse3200/game/entities/factories/TowerFactory.java +++ b/source/core/src/main/com/csse3200/game/entities/factories/TowerFactory.java @@ -1,5 +1,6 @@ package com.csse3200.game.entities.factories; +import com.badlogic.gdx.math.Vector2; import com.badlogic.gdx.physics.box2d.Filter; import com.csse3200.game.components.tasks.DroidCombatTask; import com.csse3200.game.components.tasks.TNTTowerCombatTask; @@ -25,6 +26,7 @@ import com.csse3200.game.input.UpgradeUIComponent;import java.util.HashSet; import java.util.Set; + /** * Factory to create a tower entity. * @@ -34,7 +36,6 @@ public class TowerFactory { // Define a set to keep track of occupied lanes private static final Set occupiedLanes = new HashSet<>(); - private static final int COMBAT_TASK_PRIORITY = 2; private static final int WEAPON_TOWER_MAX_RANGE = 40; private static final int TNT_TOWER_MAX_RANGE = 6; @@ -467,6 +468,8 @@ public static Entity createHealTower() { */ public static Entity createBaseTower() { // we're going to add more components later on + + Entity tower = new Entity() .addComponent(new ColliderComponent()) .addComponent(new HitboxComponent().setLayer(PhysicsLayer.TOWER)) // TODO: we might have to change the names of the layers @@ -474,7 +477,9 @@ public static Entity createBaseTower() { .addComponent(new TowerUpgraderComponent()); tower.setLayer(1); // Set priority to 1, which is 1 below scrap (which is 0) - + // Set hitboxes and colliders to one tile + tower.getComponent(HitboxComponent.class).setAsBoxAligned(new Vector2(1f, 1f), PhysicsComponent.AlignX.CENTER, PhysicsComponent.AlignY.CENTER); + tower.getComponent(ColliderComponent.class).setAsBoxAligned(new Vector2(1f, 1f), PhysicsComponent.AlignX.CENTER, PhysicsComponent.AlignY.CENTER); return tower; } public static Entity createAndPlaceTower(int lane) {