Skip to content

Commit

Permalink
Added comments to MobAttackTask and created MobAttackTaskTest
Browse files Browse the repository at this point in the history
Tests still need to be implemented.
  • Loading branch information
meganroxburgh committed Sep 5, 2023
1 parent df15f8f commit 81802a1
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@
import com.csse3200.game.services.ServiceLocator;
import com.csse3200.game.services.GameTime;
import com.csse3200.game.entities.factories.ProjectileFactory;
//import com.csse3200.game.rendering.DebugRenderer;


/**
* Task that prints a message to the terminal whenever it is called.
* Task that allows mobs to shoot projectiles or melee attack towers
*/
public class MobAttackTask extends DefaultTask implements PriorityTask {
private static final int INTERVAL = 1; // time interval to scan for towers in
Expand All @@ -34,7 +33,6 @@ public class MobAttackTask extends DefaultTask implements PriorityTask {
private GameTime timeSource;
private long endTime;
private final RaycastHit hit = new RaycastHit();
// private final DebugRenderer debugRenderer;

private final long delay = 700; // delay between shots
private long startTime;
Expand All @@ -46,21 +44,21 @@ private enum STATE {
private STATE mobState = STATE.IDLE;

/**
* @param target The entity to shoot at.
* @param priority Task priority when shooting (0 when not chasing).
* @param viewDistance Maximum distance from the entity at which shooting can start.
* @param maxChaseDistance Maximum distance from the entity while shooting before giving up.
* @param priority Task priority when targets are detected (0 when nothing detected). Must be a positive integer.
* @param maxRange Maximum effective range of the weapon mob. This determines the detection distance of targets
*/
public MobAttackTask(int priority, float maxRange) {
this.priority = priority;
this.maxRange = maxRange;
startTime = 0;

physics = ServiceLocator.getPhysicsService().getPhysics();
// debugRenderer = ServiceLocator.getRenderService().getDebug();
timeSource = ServiceLocator.getTimeSource();
}

/**
* Starts the task running, triggers the initial "idleStart" event.
*/
@Override
public void start() {
super.start();
Expand All @@ -71,6 +69,10 @@ public void start() {
endTime = timeSource.getTime() + (INTERVAL * 500);
}

/**
* The update method is what is run every time the TaskRunner in the AiTaskComponent calls update().
* Triggers events depending on the presence or otherwise of targets in the detection range
*/
@Override
public void update() {
updateMobState();
Expand All @@ -80,17 +82,23 @@ public void update() {
}
}

/**
* Mob state machine. Updates mob state by scanning for towers, and
* triggers the appropriate events corresponding to the STATE enum.
*/
public void updateMobState() {
switch (mobState) {

case IDLE -> {
if (isTargetVisible()) {
// targets detected in idle mode - start deployment
owner.getEntity().getEvents().trigger(DEPLOY);
mobState = STATE.DEPLOY;
}
}

case DEPLOY -> {
// currently deploying,
if (isTargetVisible()) {
owner.getEntity().getEvents().trigger(FIRING);
mobState = STATE.FIRING;
Expand All @@ -101,6 +109,7 @@ public void updateMobState() {
}

case FIRING -> {
// targets gone - stop firing
if (!isTargetVisible()) {
owner.getEntity().getEvents().trigger(STOW);
mobState = STATE.STOW;
Expand All @@ -114,6 +123,7 @@ public void updateMobState() {
}

case STOW -> {
// currently stowing
if (isTargetVisible()) {
owner.getEntity().getEvents().trigger(DEPLOY);
mobState = STATE.DEPLOY;
Expand All @@ -125,12 +135,19 @@ public void updateMobState() {
}
}

/**
* For stopping the running task
*/
@Override
public void stop() {
super.stop();
owner.getEntity().getEvents().trigger(STOW);
}

/**
* Returns the current priority of the task.
* @return active priority value if targets detected, inactive priority otherwise
*/
@Override
public int getPriority() {
// return -1;
Expand All @@ -141,10 +158,10 @@ public int getPriority() {
// return isTargetVisible() ? getActivePriority() : getInactivePriority();
}

// private float getDistanceToTarget() {
// return owner.getEntity().getPosition().dst(target.getPosition());
// }

/**
* Fetches the active priority of the Task if a target is visible.
* @return (int) active priority if a target is visible, -1 otherwise
*/
private int getActivePriority() {
if ((startTime + delay) < timeSource.getTime()) {
// if (isTargetVisible() && (startTime + delay) > timeSource.getTime()) {
Expand All @@ -156,6 +173,10 @@ private int getActivePriority() {
return -1;
}

/**
* Fetches the inactive priority of the Task if a target is not visible.
* @return (int) -1 if a target is not visible, active priority otherwise
*/
private int getInactivePriority() {
// return isTargetVisible() ? priority : 0;
if ((startTime + delay) < timeSource.getTime()) {
Expand All @@ -168,18 +189,11 @@ private int getInactivePriority() {
// return isTargetVisible() ? priority : -1;
}

/**
* Uses a raycast to determine whether there are any targets in detection range
* @return true if a target is visible, false otherwise
*/
private boolean isTargetVisible() {
return physics.raycast(mobPosition, maxRangePosition, TARGET, hit);
}
// Vector2 from = owner.getEntity().getCenterPosition();
// Vector2 to = target.getCenterPosition();
//
// // If there is an obstacle in the path to the player, not visible.
// if (physics.raycast(from, to, PhysicsLayer.OBSTACLE, hit)) {
// debugRenderer.drawLine(from, hit.point);
// return false;
// }
// debugRenderer.drawLine(from, to);
// return true;
// }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.csse3200.game.components.tasks;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;

import com.csse3200.game.ai.tasks.AITaskComponent;
import com.csse3200.game.entities.Entity;
import com.csse3200.game.events.listeners.EventListener0;
import com.csse3200.game.extensions.GameExtension;
import com.csse3200.game.utils.math.Vector2Utils;
import com.csse3200.game.physics.components.PhysicsMovementComponent;
import com.csse3200.game.services.GameTime;
import com.csse3200.game.services.ServiceLocator;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

@ExtendWith(GameExtension.class)
@ExtendWith(MockitoExtension.class)
class MobAttackTaskTest {
@Mock
GameTime gameTime;

// @BeforeEach
// void beforeEach() {
// ServiceLocator.registerTimeSource(gameTime);
// // To Do
// }
//
// @Test
// void shouldShootProjectile() {
// Entity target = new Entity();
// target.setPosition(2f, 2f);
//
// AITaskComponent ai = new AITaskComponent().addTask(new MobAttackTask(target, 10, 5, 10));
// Entity entity = makePhysicsEntity().addComponent(ai);
// entity.create();
// entity.setPosition(0f, 0f);
//
// // To Do
// }
}

0 comments on commit 81802a1

Please sign in to comment.