Skip to content

Commit

Permalink
feat: ✨ Health of Village is now successfully decremented when an ene…
Browse files Browse the repository at this point in the history
…my reaches the end of the map.

Game also doesnt crash now when an enemy reached the end of the path.

#17
  • Loading branch information
jmnorheim committed Apr 17, 2024
1 parent 2486264 commit 95d85ee
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 19 deletions.
60 changes: 47 additions & 13 deletions core/src/com/softwarearchitecture/ecs/systems/EnemySystem.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,18 @@
import java.util.Optional;
import java.util.Set;

import com.google.type.Money;
import com.softwarearchitecture.ecs.ComponentManager;
import com.softwarearchitecture.ecs.ECSManager;
import com.softwarearchitecture.ecs.Entity;
import com.softwarearchitecture.ecs.System;
import com.softwarearchitecture.ecs.components.HealthComponent;
import com.softwarearchitecture.ecs.components.MoneyComponent;
import com.softwarearchitecture.ecs.components.PathfindingComponent;
import com.softwarearchitecture.ecs.components.PlayerComponent;
import com.softwarearchitecture.ecs.components.PositionComponent;
import com.softwarearchitecture.ecs.components.SpriteComponent;
import com.softwarearchitecture.ecs.components.TextComponent;
import com.softwarearchitecture.ecs.components.TileComponent;
import com.softwarearchitecture.ecs.components.VelocityComponent;
import com.softwarearchitecture.game_server.EnemyFactory;
Expand All @@ -33,6 +36,8 @@ public class EnemySystem implements System {
private ComponentManager<SpriteComponent> drawableManager;
private ComponentManager<TileComponent> tileManager;
private ComponentManager<HealthComponent> healthManager;
private ComponentManager<TextComponent> textManager;
private ComponentManager<MoneyComponent> moneyManager;
private ComponentManager<PlayerComponent> playerManager;
private int waveSize;
private int monsterCounter;
Expand All @@ -53,6 +58,8 @@ public EnemySystem() {
this.pathfindingManager = ECSManager.getInstance().getOrDefaultComponentManager(PathfindingComponent.class);
this.tileManager = ECSManager.getInstance().getOrDefaultComponentManager(TileComponent.class);
this.healthManager = ECSManager.getInstance().getOrDefaultComponentManager(HealthComponent.class);
this.moneyManager = ECSManager.getInstance().getOrDefaultComponentManager(MoneyComponent.class);
this.textManager = ECSManager.getInstance().getOrDefaultComponentManager(TextComponent.class);
this.playerManager = ECSManager.getInstance().getOrDefaultComponentManager(PlayerComponent.class);
this.waveSize = 10;
this.monsterCounter = 0;
Expand Down Expand Up @@ -89,6 +96,7 @@ public void update(Set<Entity> entities, float deltaTime) {
return;
}

// Check if any enemies have reached the end of the path
for (Entity entity : entities) {
Optional<PositionComponent> position = positionManager.getComponent(entity);
Optional<VelocityComponent> velocity = velocityManager.getComponent(entity);
Expand All @@ -98,23 +106,27 @@ 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;
int hp = health.get().getHealth();
float nextTilePos_x = nextTile.getX() * tileSize.x;
float nextTilePos_y = nextTile.getY() * tileSize.y;
Vector2 nextTilePos = new Vector2(nextTilePos_x, nextTilePos_y);

if (nextTile.getType() == TileType.END && nextTilePos.sub(pos).len() < 0.001f) {

// 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;
float startPosition_y = find.get(0).getY() * tileSize.y;
position.get().position = new Vector2(startPosition_x, startPosition_y);
health.get().setHealth(health.get().getMaxHealth());
monsterCounter++;
villageDamage++;
int remainingEnemyHealth = health.get().getHealth();
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);
Expand All @@ -125,28 +137,37 @@ else if (hp <= 0) {

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);
ECSManager.getInstance().addEntity(mob);
monsterCounter++;
liveMonsterCounter++;
spawnTimer = 200f;
} else {
}
// 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()) {
continue;
}

// If the enemy is dead, reset its position and velocity
List<Tile> find = pathfinding.get().path;
if (velocity.get().velocity.len() == 0) {
position.get().position = new Vector2(find.get(0).getX()*tileSize.x,find.get(0).getY()*tileSize.y);
float startPosition_x = find.get(0).getX() * tileSize.x;
float startPosition_y = find.get(0).getY() * tileSize.y;
position.get().position = new Vector2(startPosition_x, startPosition_y);
pathfinding.get().targetTile = find.get(0);
velocity.get().velocity = velocity.get().baseVelocity;
health.get().setHealth(health.get().getMaxHealth());
Expand All @@ -155,20 +176,33 @@ else if (hp <= 0) {
}
}
}
// 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) {
Optional<PlayerComponent> player = playerManager.getComponent(entity);
Optional<HealthComponent> health = healthManager.getComponent(entity);
if (!player.isPresent() || !health.isPresent()) {
continue;
// 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 villageHealth = healthManager.getComponent(entity).get().getHealth();
villageHealth -= villageDamage;
healthManager.getComponent(entity).get().setHealth(villageHealth);

// Get the text-component of the village and update the health
Optional<TextComponent> textComponent = textManager.getComponent(entity);
Optional<MoneyComponent> moneyComponent = moneyManager.getComponent(entity);
if (textComponent.isPresent() && moneyComponent.isPresent()) {
int money = moneyComponent.get().amount;
String textToDisplay = "Health: " + villageHealth + "\n Money: " + money;
textComponent.get().text = textToDisplay;
}
villageDamage = 0;
}
health.get().setHealth(health.get().getHealth() - villageDamage);
villageDamage = 0;
}
}
// Start the next wave
if (monsterCounter >= waveSize && waveTimer<=0) {
waveNumber++;
monsterCounter = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ public static Entity createEnemy(EnemyType enemyType, List<Tile> enemyPath, Vect
int damage = 1;
int maxHealth = 0;
String sound = "soundPath";
int money = 0;

switch (enemyType) {
case NORDIC_ANT:
Expand All @@ -48,18 +47,16 @@ public static Entity createEnemy(EnemyType enemyType, List<Tile> enemyPath, Vect
velocity.set(0.01f, 0.01f);
maxHealth = 10;
sound = "soundPath"; // TODO: Add the correct sound path
money = 1;

break;
case WOLF:
textures.add(TexturePack.ENEMY_FENRIR1);
textures.add(TexturePack.ENEMY_FENRIR2);
damage = 2;
size.set(0.075f, 0.075f);
velocity.set(0.02f, 0.02f);
velocity.set(0.2f, 0.2f);
maxHealth = 100;
sound = "soundPath"; // TODO: Add the correct sound path
money = 5;

break;

Expand All @@ -73,7 +70,6 @@ public static Entity createEnemy(EnemyType enemyType, List<Tile> enemyPath, Vect
HealthComponent healthComponent = new HealthComponent(maxHealth);
SoundComponent soundComponent = new SoundComponent(sound);
VelocityComponent velocityComponent = new VelocityComponent(velocity.x, velocity.y);
CostComponent costComponent = new CostComponent(money);
PathfindingComponent PathfindingComponent = new PathfindingComponent(enemyPath);
// TODO: Add target component if necessary

Expand All @@ -85,7 +81,6 @@ public static Entity createEnemy(EnemyType enemyType, List<Tile> enemyPath, Vect
enemyEntity.addComponent(HealthComponent.class, healthComponent);
enemyEntity.addComponent(SoundComponent.class, soundComponent);
enemyEntity.addComponent(VelocityComponent.class, velocityComponent);
enemyEntity.addComponent(CostComponent.class, costComponent);
enemyEntity.addComponent(PathfindingComponent.class, PathfindingComponent);

return enemyEntity;
Expand Down

0 comments on commit 95d85ee

Please sign in to comment.