diff --git a/source/core/src/main/com/csse3200/game/components/tasks/MobTask/MobTask.java b/source/core/src/main/com/csse3200/game/components/tasks/MobTask/MobTask.java index 648600fe..1d4555ba 100644 --- a/source/core/src/main/com/csse3200/game/components/tasks/MobTask/MobTask.java +++ b/source/core/src/main/com/csse3200/game/components/tasks/MobTask/MobTask.java @@ -237,22 +237,25 @@ private void changeState(State state) { */ private boolean enemyDetected() { // if there's an entity within x of - 1 of mob - Entity targetInFront = ServiceLocator.getEntityService().getEntityAtPosition( - mob.getPosition().x - MELEE_ATTACK_RANGE, mob.getPosition().y); + Entity targetInFront = ServiceLocator.getEntityService().getEntityAtPositionLayer( + mob.getPosition().x, mob.getPosition().y, PhysicsLayer.HUMANS); if (targetInFront == null) { - return false; + targetInFront = ServiceLocator.getEntityService().getEntityAtPositionLayer( + mob.getPosition().x + 0.2f, mob.getPosition().y, PhysicsLayer.HUMANS); } - - // layer checking - HitboxComponent targetHitbox = targetInFront.getComponent(HitboxComponent.class); - if (targetHitbox == null) { - return false; + if (targetInFront == null) { + targetInFront = ServiceLocator.getEntityService().getEntityAtPositionLayer( + mob.getPosition().x, mob.getPosition().y + 0.2f, PhysicsLayer.HUMANS); } - if (PhysicsLayer.contains(PhysicsLayer.HUMANS, targetHitbox.getLayer())) { - this.target = targetInFront; - return true; + if (targetInFront == null) { + targetInFront = ServiceLocator.getEntityService().getEntityAtPositionLayer( + mob.getPosition().x, mob.getPosition().y - 0.2f, PhysicsLayer.HUMANS); + } + if (targetInFront == null) { + return false; } - return false; + target = targetInFront; + return true; } /** diff --git a/source/core/src/main/com/csse3200/game/components/tasks/bosstask/DemonBossTask.java b/source/core/src/main/com/csse3200/game/components/tasks/bosstask/DemonBossTask.java index a861a3a5..88d8a0c5 100644 --- a/source/core/src/main/com/csse3200/game/components/tasks/bosstask/DemonBossTask.java +++ b/source/core/src/main/com/csse3200/game/components/tasks/bosstask/DemonBossTask.java @@ -335,7 +335,7 @@ private Vector2 getJumpPos() { */ private boolean jumpComplete() { if (animation.isFinished() && isJumping) { - nearbyEntities = ServiceLocator.getEntityService().getEntitiesInLayer( + nearbyEntities = ServiceLocator.getEntityService().getEntitiesInRadiusOfLayer( demon, SMASH_RADIUS, PhysicsLayer.HUMANS); applyAoeDamage(nearbyEntities, SMASH_DAMAGE); // do damage upon landing isJumping = false; diff --git a/source/core/src/main/com/csse3200/game/components/tasks/bosstask/SlimeyBoyTask.java b/source/core/src/main/com/csse3200/game/components/tasks/bosstask/SlimeyBoyTask.java index bf607a12..0d099294 100644 --- a/source/core/src/main/com/csse3200/game/components/tasks/bosstask/SlimeyBoyTask.java +++ b/source/core/src/main/com/csse3200/game/components/tasks/bosstask/SlimeyBoyTask.java @@ -90,7 +90,7 @@ public void update() { case MOVE -> { if (targetFound()) { // do aoe damage based on how much health slime has left - applyAoeDamage(ServiceLocator.getEntityService().getEntitiesInLayer( + applyAoeDamage(ServiceLocator.getEntityService().getEntitiesInRadiusOfLayer( slimey, MAX_RADIUS, PhysicsLayer.HUMANS), health); changeState(SlimeState.TAKE_HIT); } diff --git a/source/core/src/main/com/csse3200/game/entities/EntityService.java b/source/core/src/main/com/csse3200/game/entities/EntityService.java index 35e7a9ee..f1de166c 100644 --- a/source/core/src/main/com/csse3200/game/entities/EntityService.java +++ b/source/core/src/main/com/csse3200/game/entities/EntityService.java @@ -115,6 +115,23 @@ public Array getNearbyEntities(Entity source, float radius) { return nearbyEntities; } + public Array getEntitiesInLayer(short layer) { + Array entitiesInLayer = new Array<>(); + Array allEntities = getEntities(); + + for (int i = 0; i < allEntities.size; i++) { + Entity targetEntity = allEntities.get(i); + + // check targets layer + HitboxComponent targetHitbox = targetEntity.getComponent(HitboxComponent.class); + if (targetHitbox == null || (!PhysicsLayer.contains(layer, targetHitbox.getLayer()))) { + continue; + } + entitiesInLayer.add(targetEntity); + } + return entitiesInLayer; + } + /** * Get entities within a certain radius of a given entity. * @@ -123,7 +140,7 @@ public Array getNearbyEntities(Entity source, float radius) { * @param layer Desired layer for entities to be in * @return An array containing entities within the given radius. */ - public Array getEntitiesInLayer(Entity source, float radius, short layer) { + public Array getEntitiesInRadiusOfLayer(Entity source, float radius, short layer) { Array entitiesInLayer = new Array<>(); Array allEntities = getNearbyEntities(source, radius); @@ -150,7 +167,7 @@ public Entity getClosestEntityOfLayer(Entity source, short layer) { Entity closestHuman = null; Vector2 sourcePos = source.getPosition(); float closestDistance = MAX_RADIUS; - Array entitiesInLayer = getEntitiesInLayer(source, MAX_RADIUS, layer); + Array entitiesInLayer = getEntitiesInRadiusOfLayer(source, MAX_RADIUS, layer); for (int i = 0; i < entitiesInLayer.size; i++) { Entity targetEntity = entitiesInLayer.get(i); diff --git a/source/core/src/main/com/csse3200/game/services/WaveService.java b/source/core/src/main/com/csse3200/game/services/WaveService.java index 5b95d376..24baea31 100644 --- a/source/core/src/main/com/csse3200/game/services/WaveService.java +++ b/source/core/src/main/com/csse3200/game/services/WaveService.java @@ -1,6 +1,9 @@ package com.csse3200.game.services; +import com.badlogic.gdx.utils.Array; import com.csse3200.game.components.maingame.UIElementsDisplay; +import com.csse3200.game.entities.Entity; +import com.csse3200.game.physics.PhysicsLayer; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -60,6 +63,7 @@ public int getEnemyCount() { * If enemy count is 0, the game is over. */ public void updateEnemyCount() { +// Array entities = ServiceLocator.getEntityService().getEntitiesInLayer(PhysicsLayer.NPC); enemyCount -= 1; remainingLevelEnemyCount -= 1; logger.info("{} enemies remaining in wave", getEnemyCount());