From fced1beecaac0bed8fc82886ec7336fcededd504 Mon Sep 17 00:00:00 2001 From: freshc0w <121275444+freshc0w@users.noreply.github.com> Date: Fri, 29 Sep 2023 16:21:50 +1000 Subject: [PATCH] Add test cases for dodge invoke event --- .../game/components/tasks/MobDodgeTask.java | 4 +-- .../game/components/DodgingComponentTest.java | 31 ++++++++++++++----- 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/source/core/src/main/com/csse3200/game/components/tasks/MobDodgeTask.java b/source/core/src/main/com/csse3200/game/components/tasks/MobDodgeTask.java index d7c1f546f..0f20e32dc 100644 --- a/source/core/src/main/com/csse3200/game/components/tasks/MobDodgeTask.java +++ b/source/core/src/main/com/csse3200/game/components/tasks/MobDodgeTask.java @@ -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(); } /** @@ -63,7 +64,6 @@ public void update() { owner.getEntity().getEvents().trigger("dodgeIncomingEntity", owner.getEntity().getCenterPosition()); endTime = timeSource.getTime() + (1 * DELAY_INTERVAL); } - } /** diff --git a/source/core/src/test/com/csse3200/game/components/DodgingComponentTest.java b/source/core/src/test/com/csse3200/game/components/DodgingComponentTest.java index 63d813191..e50ff1257 100644 --- a/source/core/src/test/com/csse3200/game/components/DodgingComponentTest.java +++ b/source/core/src/test/com/csse3200/game/components/DodgingComponentTest.java @@ -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; @@ -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; @@ -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()); @@ -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 @@ -67,19 +67,34 @@ public void shouldNotBeNullTask() { assertNotNull("Mob dodging tasks should not be null", baseMob.getComponent(AITaskComponent.class)); } + @Test + public void shouldInvokeDodgeEvent() { + EventListener1 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));