Skip to content

Commit

Permalink
Fixed bug (#75)
Browse files Browse the repository at this point in the history
  • Loading branch information
Hasakev committed Sep 6, 2023
1 parent 53d7484 commit 967c1e8
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 13 deletions.
7 changes: 7 additions & 0 deletions source/core/assets/images/economy/econ-tower.atlas
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ size: 256, 64
format: RGBA8888
filter: Nearest, Nearest
repeat: none
idle
rotate: false
xy: 122, 2
size: 28, 31
orig: 28, 31
offset: 0, 0
index: -1
move1
rotate: false
xy: 122, 2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,21 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.TimeUnit;

/**
* The CurrencyTask updates the in-game currency based on time intervals.
*/
public class CurrencyTask extends DefaultTask implements PriorityTask {
private static final Logger logger = LoggerFactory.getLogger(CurrencyTask.class);
private final int priority; // The active priority this task will have
private final int INTERVAL = 10; // time interval to update currency in seconds
private final GameTime timeSource;
private long endTime;
private int interval;
private final Scrap scrap = new Scrap(); // currency to update
private final int currencyAmount = scrap.getAmount(); // amount of currency to update
private static final String IDLE = "idleStart";
private static final String IDLE = "idleStartEco";
private static final String MOVE = "moveStartEco";

/**
* @param priority Task priority for currency updates. Must be a positive integer.
Expand All @@ -38,7 +40,7 @@ public CurrencyTask(int priority, int interval) {
@Override
public void start() {
super.start();
endTime = timeSource.getTime() + (INTERVAL * 1000);
endTime = timeSource.getTime();
owner.getEntity().getEvents().trigger(IDLE);
}

Expand All @@ -50,11 +52,17 @@ public void start() {
@Override
public void update() {
if (timeSource.getTime() >= endTime) {
owner.getEntity().getEvents().trigger(MOVE);
updateCurrency(); // update currency
endTime = timeSource.getTime() + (interval * 1000L); // reset end time
endTime = timeSource.getTime() + (30 * 1000); // reset end time
restartAnimation();

}
}

private void restartAnimation() {
owner.getEntity().getEvents().trigger(IDLE);
}

/**
* Updates the currency based on time intervals.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@
*/
public class EconTowerAnimationController extends Component {
// Event name constants
private static final String IDLE = "idleStart";
private static final String IDLE = "idleStartEco";
private static final String MOVE = "move1StartEco";
// Animation name constants
private static final String IDLE_ANIM = "move1";
private static final String ECO_MOVE = "move1";
private static final String ECO_IDLE = "idle";

AnimationRenderComponent animator;

Expand All @@ -26,13 +28,17 @@ public void create() {
super.create();
animator = this.entity.getComponent(AnimationRenderComponent.class);
entity.getEvents().addListener(IDLE, this::animateIdle);
entity.getEvents().addListener(MOVE, this::animateMove);
}

/**
* Starts the
*/
void animateIdle() {
animator.startAnimation(IDLE_ANIM);
animator.startAnimation(ECO_IDLE);
}

void animateMove() { animator.startAnimation(ECO_MOVE); }


}
12 changes: 12 additions & 0 deletions source/core/src/main/com/csse3200/game/entities/Entity.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ public class Entity {
private Vector2 scale = new Vector2(1, 1);
private Array<Component> createdComponents;

private int layer = 2;

// Check if the entity is flagged for deletion
private boolean isFlaggedForDelete = false;

Expand Down Expand Up @@ -298,4 +300,14 @@ public int hashCode() {
public String toString() {
return String.format("Entity{id=%d}", id);
}

public int getLayer() {
return layer;
}

public int setLayer(int layer) {
this.layer = layer;
return layer;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.slf4j.LoggerFactory;

import java.security.Provider;
import java.util.Comparator;

/**
* Provides a global access point for entities to register themselves. This allows for iterating
Expand Down Expand Up @@ -99,6 +100,7 @@ public Array<Entity> getNearbyEntities(Entity source, float radius) {
}

public Entity getEntityAtPosition(float x, float y) {
entities.sort(Comparator.comparingInt(Entity::getLayer));
for (Entity entity : entities) {
if (entityContainsPosition(entity, x, y)) {
return entity;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public static Entity createScrapDrop() {
drop.getComponent(TextureRenderComponent.class).scaleEntity();
drop.scaleHeight(0.5f);
drop.scaleWidth(0.5f);
drop.setLayer(0);
return drop;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ public class TowerFactory {


private static final String ECO_ATLAS = "images/economy/econ-tower.atlas";
private static final String ECO_IDLE = "move1";
private static final String ECO_MOVE = "move1";
private static final String ECO_IDLE = "idle";
private static final float ECO_IDLE_SPEED = 0.3f;

private static final baseTowerConfigs configs =
Expand All @@ -66,7 +67,7 @@ public static Entity createIncomeTower() {
IncomeTowerConfig config = configs.income;

// Create the CurrencyIncomeTask and add it to the AITaskComponent
CurrencyTask currencyTask = new CurrencyTask(INCOME_TASK_PRIORITY, 3);
CurrencyTask currencyTask = new CurrencyTask(INCOME_TASK_PRIORITY, 30);

int updatedInterval = 1;
currencyTask.setInterval(updatedInterval);
Expand All @@ -79,17 +80,14 @@ public static Entity createIncomeTower() {
ServiceLocator.getResourceService()
.getAsset(ECO_ATLAS, TextureAtlas.class));
animator.addAnimation(ECO_IDLE, ECO_IDLE_SPEED, Animation.PlayMode.LOOP);
animator.addAnimation(ECO_MOVE, ECO_IDLE_SPEED, Animation.PlayMode.NORMAL);

income
.addComponent(new CombatStatsComponent(config.health, config.baseAttack))
.addComponent(new CostComponent(config.cost))
.addComponent(aiTaskComponent)
.addComponent(animator)
.addComponent(new EconTowerAnimationController());




return income;
}

Expand Down

0 comments on commit 967c1e8

Please sign in to comment.