Skip to content

Commit

Permalink
Ranged attacks are only made when there is a tower ahead
Browse files Browse the repository at this point in the history
  • Loading branch information
ThivanW committed Sep 9, 2023
1 parent a673006 commit 4daece4
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import com.csse3200.game.ai.tasks.Task;
import com.csse3200.game.entities.Entity;
import com.csse3200.game.entities.factories.ProjectileFactory;
import com.csse3200.game.physics.PhysicsEngine;
import com.csse3200.game.physics.raycast.RaycastHit;
import com.csse3200.game.services.ServiceLocator;
import com.csse3200.game.physics.PhysicsLayer;
import org.slf4j.Logger;
Expand All @@ -23,13 +25,17 @@ public class RangeBossMovementTask extends DefaultTask implements PriorityTask {
private MovementTask movementTask;
private WaitTask waitTask;
private Task currentTask;
private PhysicsEngine physics;
private static final short TARGET = PhysicsLayer.TOWER;
private final RaycastHit hit = new RaycastHit();

/**
* @param waitTime How long in seconds to wait between wandering.
*/
public RangeBossMovementTask(float waitTime) {

this.waitTime = waitTime;
physics = ServiceLocator.getPhysicsService().getPhysics();
}

@Override
Expand Down Expand Up @@ -58,11 +64,12 @@ public void start() {
public void update() {
if (currentTask.getStatus() != Status.ACTIVE) {
if (currentTask == movementTask) {
Entity newProjectile = ProjectileFactory.createFireBall(PhysicsLayer.OBSTACLE, new Vector2(0, currentPos.y + 0.75f), new Vector2(2f,2f));

newProjectile.scaleHeight(-0.4f);
newProjectile.setPosition((float) (currentPos.x), (float) (currentPos.y+0.75f));
ServiceLocator.getEntityService().register(newProjectile);
if (towerAhead()) {
Entity newProjectile = ProjectileFactory.createFireBall(TARGET, new Vector2(0, currentPos.y + 0.75f), new Vector2(2f,2f));
newProjectile.scaleHeight(-0.4f);
newProjectile.setPosition((float) (currentPos.x), (float) (currentPos.y+0.75f));
ServiceLocator.getEntityService().register(newProjectile);
}
startWaiting();
} else {
startMoving();
Expand Down Expand Up @@ -90,4 +97,8 @@ private void swapTask(Task newTask) {
currentTask.start();
}

private boolean towerAhead() {
return physics.raycast(currentPos, new Vector2(0, currentPos.y), TARGET, hit);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.csse3200.game.entities.Entity;
import com.csse3200.game.events.listeners.EventListener0;
import com.csse3200.game.extensions.GameExtension;
import com.csse3200.game.physics.PhysicsService;
import com.csse3200.game.utils.math.Vector2Utils;
import com.csse3200.game.physics.components.PhysicsMovementComponent;
import com.csse3200.game.services.GameTime;
Expand All @@ -26,14 +27,17 @@ class RangeBossMovementTaskTest {
@BeforeEach
void beforeEach() {
ServiceLocator.registerTimeSource(gameTime);
ServiceLocator.registerPhysicsService(new PhysicsService());
}

@Test
void shouldTriggerEvent() {
RangeBossMovementTask RBMTask = new RangeBossMovementTask(1f);

AITaskComponent aiTaskComponent = new AITaskComponent().addTask(RBMTask);
Entity entity = new Entity().addComponent(aiTaskComponent).addComponent(new PhysicsMovementComponent());
Entity entity = new Entity()
.addComponent(aiTaskComponent)
.addComponent(new PhysicsMovementComponent());
entity.create();

// Register callbacks
Expand Down

0 comments on commit 4daece4

Please sign in to comment.