Skip to content

Commit

Permalink
fixed bug where mobs were getting stuck
Browse files Browse the repository at this point in the history
  • Loading branch information
gregchan550 committed Oct 18, 2023
1 parent d7c2f40 commit 3c69248
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
21 changes: 19 additions & 2 deletions source/core/src/main/com/csse3200/game/entities/EntityService.java
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,23 @@ public Array<Entity> getNearbyEntities(Entity source, float radius) {
return nearbyEntities;
}

public Array<Entity> getEntitiesInLayer(short layer) {
Array<Entity> entitiesInLayer = new Array<>();
Array<Entity> 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.
*
Expand All @@ -123,7 +140,7 @@ public Array<Entity> 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<Entity> getEntitiesInLayer(Entity source, float radius, short layer) {
public Array<Entity> getEntitiesInRadiusOfLayer(Entity source, float radius, short layer) {
Array<Entity> entitiesInLayer = new Array<>();
Array<Entity> allEntities = getNearbyEntities(source, radius);

Expand All @@ -150,7 +167,7 @@ public Entity getClosestEntityOfLayer(Entity source, short layer) {
Entity closestHuman = null;
Vector2 sourcePos = source.getPosition();
float closestDistance = MAX_RADIUS;
Array<Entity> entitiesInLayer = getEntitiesInLayer(source, MAX_RADIUS, layer);
Array<Entity> entitiesInLayer = getEntitiesInRadiusOfLayer(source, MAX_RADIUS, layer);

for (int i = 0; i < entitiesInLayer.size; i++) {
Entity targetEntity = entitiesInLayer.get(i);
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -60,6 +63,7 @@ public int getEnemyCount() {
* If enemy count is 0, the game is over.
*/
public void updateEnemyCount() {
// Array<Entity> entities = ServiceLocator.getEntityService().getEntitiesInLayer(PhysicsLayer.NPC);
enemyCount -= 1;
remainingLevelEnemyCount -= 1;
logger.info("{} enemies remaining in wave", getEnemyCount());
Expand Down

0 comments on commit 3c69248

Please sign in to comment.