Skip to content

Commit

Permalink
Add test cases for dodge invoke event
Browse files Browse the repository at this point in the history
  • Loading branch information
freshc0w committed Sep 29, 2023
1 parent 3b78d63 commit fced1be
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ public void start() {
super.start();
owner.getEntity().getEvents().trigger("wanderStart");

endTime = timeSource.getTime() + (1 * DELAY_INTERVAL);
// endTime = timeSource.getTime() + (1 * DELAY_INTERVAL);
endTime = timeSource.getTime();
}

/**
Expand All @@ -63,7 +64,6 @@ public void update() {
owner.getEntity().getEvents().trigger("dodgeIncomingEntity", owner.getEntity().getCenterPosition());
endTime = timeSource.getTime() + (1 * DELAY_INTERVAL);
}

}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.csse3200.game.components;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.mockito.Mockito.atLeastOnce;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
Expand All @@ -15,6 +13,7 @@
import com.csse3200.game.ai.tasks.AITaskComponent;
import com.csse3200.game.components.npc.DodgingComponent;
import com.csse3200.game.components.tasks.MobDodgeTask;
import com.csse3200.game.components.tasks.MobWanderTask;
import com.csse3200.game.entities.Entity;
import com.csse3200.game.entities.EntityService;
import com.csse3200.game.entities.factories.NPCFactory;
Expand All @@ -35,13 +34,14 @@ public class DodgingComponentTest {
Entity baseMob, baseProjectile;
private static final float VALID_POSITION_Y = 4;
private static final float VALID_POSITION_X = 7;
private static final float DEFAULT_RANGE_DETECTION = 1f;
MobWanderTask task;

@BeforeEach
public void setUp() {
GameTime gameTime = mock(GameTime.class);
when(gameTime.getDeltaTime()).thenReturn(0.02f);
ServiceLocator.registerTimeSource(gameTime);

ServiceLocator.registerPhysicsService(new PhysicsService());

ServiceLocator.registerEntityService(new EntityService());
Expand All @@ -53,8 +53,8 @@ public void setUp() {
ResourceService resourceService = new ResourceService();
ServiceLocator.registerResourceService(resourceService);

baseMob = createDodgeMob(VALID_POSITION_X, VALID_POSITION_Y);
baseProjectile = createProjectile(VALID_POSITION_X - 0.2f, VALID_POSITION_Y);
baseMob = createDodgeMob(VALID_POSITION_X, VALID_POSITION_Y, DEFAULT_RANGE_DETECTION, 1.75f);
task = new MobDodgeTask(new Vector2(2f, 2f), 2f, 5);
}

@Test
Expand All @@ -67,19 +67,34 @@ public void shouldNotBeNullTask() {
assertNotNull("Mob dodging tasks should not be null", baseMob.getComponent(AITaskComponent.class));
}

@Test
public void shouldInvokeDodgeEvent() {
EventListener1<Vector2> dodgeProj = mock(EventListener1.class);
baseMob.getComponent(AITaskComponent.class).addTask(task);
baseMob.getEvents().addListener(DodgingComponent.DODGE_EVENT, dodgeProj);
Vector2 mobPos = baseMob.getCenterPosition();
task.start();
Entity projectile = createProjectile(VALID_POSITION_X - (DEFAULT_RANGE_DETECTION / 2), VALID_POSITION_Y);

ServiceLocator.getPhysicsService().getPhysics().update();
ServiceLocator.getEntityService().update();
task.update();

verify(dodgeProj).handle(mobPos);
}

Entity createDodgeMob(float posX, float posY) {
Entity mob = NPCFactory.createRangedBaseNPC();
mob.getComponent(AITaskComponent.class).addTask(new MobDodgeTask(new Vector2(2f, 2f), 2f, 5));
mob.addComponent(new CombatStatsComponent(10, 10));
mob.addComponent(new DodgingComponent(PhysicsLayer.PROJECTILE));
mob.setPosition(posX, posY);

ServiceLocator.getEntityService().register(mob);
mob.setPosition(posX, posY);
return mob;
}

Entity createDodgeMob(float posX, float posY, float rangeDetection, float dodgeSpeed) {
Entity mob = NPCFactory.createRangedBaseNPC();
mob.getComponent(AITaskComponent.class).addTask(new MobDodgeTask(new Vector2(2f, 2f), 2f, 5));
mob.addComponent(new CombatStatsComponent(10, 10));
mob.addComponent(new DodgingComponent(PhysicsLayer.PROJECTILE, rangeDetection, dodgeSpeed));

Expand Down

0 comments on commit fced1be

Please sign in to comment.