Skip to content

Commit

Permalink
Refactor EnemySystem to improve code readability and formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
tobiasfremming committed Apr 17, 2024
1 parent 50b158e commit 8d35232
Showing 1 changed file with 29 additions and 24 deletions.
53 changes: 29 additions & 24 deletions core/src/com/softwarearchitecture/ecs/systems/EnemySystem.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.softwarearchitecture.ecs.systems;


import java.util.List;
import java.util.Optional;
import java.util.Set;
Expand All @@ -26,7 +25,8 @@
import com.softwarearchitecture.math.Vector2;

/**
* This class is supposed to check if enemies are at the end of the map, and if so despawns them
* This class is supposed to check if enemies are at the end of the map, and if
* so despawns them
*
*/
public class EnemySystem implements System {
Expand All @@ -50,7 +50,6 @@ public class EnemySystem implements System {
private int maxLiveMonsters;
private int villageDamage;


public EnemySystem() {
this.positionManager = ECSManager.getInstance().getOrDefaultComponentManager(PositionComponent.class);
this.velocityManager = ECSManager.getInstance().getOrDefaultComponentManager(VelocityComponent.class);
Expand All @@ -71,11 +70,10 @@ public EnemySystem() {
this.villageDamage = 0;
}


@Override
public void update(Set<Entity> entities, float deltaTime) {
//Get tile size
Vector2 tileSize = new Vector2(0,0);
// Get tile size
Vector2 tileSize = new Vector2(0, 0);
for (Entity entity : entities) {
if (path == null) {
Optional<PathfindingComponent> possiblePath = pathfindingManager.getComponent(entity);
Expand All @@ -89,9 +87,9 @@ public void update(Set<Entity> entities, float deltaTime) {
if (sprite.isPresent() && tile.isPresent()) {
tileSize = sprite.get().size_uv;
continue;
}
}
}
/*For path not yet initialized escape early */
/* For path not yet initialized escape early */
if (path == null) {
return;
}
Expand All @@ -106,7 +104,7 @@ public void update(Set<Entity> entities, float deltaTime) {
if (!position.isPresent() || !velocity.isPresent() || !pathfinding.isPresent() || !health.isPresent()) {
continue;
}

Vector2 pos = position.get().position;
List<Tile> find = pathfinding.get().path;
Tile nextTile = pathfinding.get().targetTile;
Expand All @@ -115,7 +113,8 @@ public void update(Set<Entity> entities, float deltaTime) {
float nextTilePos_y = nextTile.getY() * tileSize.y;
Vector2 nextTilePos = new Vector2(nextTilePos_x, nextTilePos_y);

// If the enemy has reached the end of the path, move it to the start to be spawned again
// If the enemy has reached the end of the path, move it to the start to be
// spawned again
if (nextTile.getType() == TileType.END) {
pathfinding.get().targetTile = find.get(0);
float startPosition_x = find.get(0).getX() * tileSize.x;
Expand All @@ -127,38 +126,42 @@ public void update(Set<Entity> entities, float deltaTime) {
java.lang.System.out.println("RemainingEnemyHealth = " + remainingEnemyHealth);
this.villageDamage += remainingEnemyHealth;
java.lang.System.out.println("VillageDamage = " + villageDamage);
}
else if (hp <= 0) {
position.get().position = new Vector2(-1,-1);
} else if (hp <= 0) {
position.get().position = new Vector2(-1, -1);
velocity.get().velocity = new Vector2(0, 0);
liveMonsterCounter--;
}
}

spawnTimer -= deltaTime;


if (spawnTimer <= 0 && monsterCounter < waveSize) {

// Keep creating enemies under max-limit is met
if (liveMonsterCounter < maxLiveMonsters) {
mob = EnemyFactory.createEnemy(EnemyType.WOLF, path, tileSize);

// random enemy
EnemyType[] enemyTypes = EnemyType.values();
EnemyType randomEnemy = enemyTypes[(int) (Math.random() * enemyTypes.length)];

mob = EnemyFactory.createEnemy(randomEnemy, path, tileSize);
ECSManager.getInstance().addEntity(mob);
monsterCounter++;
liveMonsterCounter++;
spawnTimer = 200f;
}
}
// If the max number of enemies has been met, check if any of them are dead
else {

for (Entity entity : entities) {
Optional<PositionComponent> position = positionManager.getComponent(entity);
Optional<VelocityComponent> velocity = velocityManager.getComponent(entity);
Optional<PathfindingComponent> pathfinding = pathfindingManager.getComponent(entity);
Optional<HealthComponent> health = healthManager.getComponent(entity);
// Optional<MoneyComponent> money = moneyManager.getComponent(entity);

if (!position.isPresent() || !velocity.isPresent() || !pathfinding.isPresent() || !health.isPresent()) {

if (!position.isPresent() || !velocity.isPresent() || !pathfinding.isPresent()
|| !health.isPresent()) {
continue;
}

Expand All @@ -176,16 +179,18 @@ else if (hp <= 0) {
}
}
}
// Decrement the wave timer
// Decrement the wave timer
if (waveTimer > 0) {
waveTimer -= deltaTime;
}
// If any enemies have gotten through, damage the village
if (villageDamage > 0) {
for (Entity entity : entities) {
// If an entity has both a player and health component, it is the village and should take damage
// If an entity has both a player and health component, it is the village and
// should take damage
if (playerManager.getComponent(entity).isPresent() && healthManager.getComponent(entity).isPresent()) {
// int remainingEnemyHealth = healthManager.getComponent(entity).get().getHealth();
// int remainingEnemyHealth =
// healthManager.getComponent(entity).get().getHealth();
int villageHealth = healthManager.getComponent(entity).get().getHealth();
villageHealth -= villageDamage;
healthManager.getComponent(entity).get().setHealth(villageHealth);
Expand All @@ -203,10 +208,10 @@ else if (hp <= 0) {
}
}
// Start the next wave
if (monsterCounter >= waveSize && waveTimer<=0) {
if (monsterCounter >= waveSize && waveTimer <= 0) {
waveNumber++;
monsterCounter = 0;
waveSize+=waveNumber * 2 - 2;
waveSize += waveNumber * 2 - 2;
waveTimer = 60f;
spawnTimer = 0f;
maxLiveMonsters++;
Expand Down

0 comments on commit 8d35232

Please sign in to comment.