Skip to content

Commit

Permalink
Fixed issue with income tower being invulnerable
Browse files Browse the repository at this point in the history
  • Loading branch information
ThivanW committed Oct 11, 2023
1 parent 7d8efa9 commit 16b587a
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
Expand All @@ -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);
}

Expand All @@ -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.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -25,6 +26,7 @@
import com.csse3200.game.input.UpgradeUIComponent;import java.util.HashSet;
import java.util.Set;


/**
* Factory to create a tower entity.
*
Expand All @@ -34,7 +36,6 @@
public class TowerFactory {
// Define a set to keep track of occupied lanes
private static final Set<Integer> 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;
Expand Down Expand Up @@ -467,14 +468,18 @@ 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
.addComponent(new PhysicsComponent().setBodyType(BodyType.StaticBody))
.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) {
Expand Down

0 comments on commit 16b587a

Please sign in to comment.