From 61dbfde36a8aa5c583b4a417831e73804e869e58 Mon Sep 17 00:00:00 2001 From: BlairCannon97 Date: Tue, 12 Sep 2023 09:06:25 +1000 Subject: [PATCH 1/4] Added test class XenoAnimationTest to test animation triggers. --- .../npc/XenoAnimationController.java | 5 +- .../xenos/XenoAnimationControllerTest.java | 78 +++++++++++++++++++ 2 files changed, 79 insertions(+), 4 deletions(-) create mode 100644 source/core/src/test/com/csse3200/game/components/xenos/XenoAnimationControllerTest.java diff --git a/source/core/src/main/com/csse3200/game/components/npc/XenoAnimationController.java b/source/core/src/main/com/csse3200/game/components/npc/XenoAnimationController.java index 270f5afa8..d4acfd8fe 100644 --- a/source/core/src/main/com/csse3200/game/components/npc/XenoAnimationController.java +++ b/source/core/src/main/com/csse3200/game/components/npc/XenoAnimationController.java @@ -1,11 +1,8 @@ package com.csse3200.game.components.npc; -import com.badlogic.gdx.graphics.g2d.Animation; import com.csse3200.game.components.Component; import com.csse3200.game.rendering.AnimationRenderComponent; -import java.util.Objects; - /** * This class listens to events relevant to a ghost entity's state and plays the animation when one * of the events is triggered. @@ -20,7 +17,7 @@ public void create() { entity.getEvents().addListener("wanderStart", this::animateRun); entity.getEvents().addListener("runHurt", this::animateHurt); entity.getEvents().addListener("meleeStart", this::animateMelee1); - entity.getEvents().addListener("meleeStart", this::animateMelee2); +// entity.getEvents().addListener("meleeStart", this::animateMelee2); entity.getEvents().addListener("shootStart", this::animateShoot); entity.getEvents().addListener("dieStart", this::animateDie); entity.getEvents().addListener("stop", this::stopAnimation); diff --git a/source/core/src/test/com/csse3200/game/components/xenos/XenoAnimationControllerTest.java b/source/core/src/test/com/csse3200/game/components/xenos/XenoAnimationControllerTest.java new file mode 100644 index 000000000..9da376447 --- /dev/null +++ b/source/core/src/test/com/csse3200/game/components/xenos/XenoAnimationControllerTest.java @@ -0,0 +1,78 @@ +package com.csse3200.game.components.xenos; + + +import com.csse3200.game.entities.Entity; +import com.csse3200.game.entities.factories.NPCFactory; +import com.csse3200.game.extensions.GameExtension; +import com.csse3200.game.physics.PhysicsService; +import com.csse3200.game.rendering.AnimationRenderComponent; +import com.csse3200.game.rendering.DebugRenderer; +import com.csse3200.game.rendering.RenderService; +import com.csse3200.game.services.ResourceService; +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 static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.Mockito.mock; + + +@ExtendWith(GameExtension.class) +public class XenoAnimationControllerTest { + + private Entity xenoGrunt; + private Entity target; + private final String[] atlas = {"images/mobs/xenoGrunt.atlas"}; + + @BeforeEach + public void setUp() { + ServiceLocator.registerPhysicsService(new PhysicsService()); + RenderService render = new RenderService(); + render.setDebug(mock(DebugRenderer.class)); + ServiceLocator.registerRenderService(render); + ResourceService resourceService = new ResourceService(); + ServiceLocator.registerResourceService(resourceService); + resourceService.loadTextureAtlases(atlas); + resourceService.loadAll(); + + xenoGrunt = NPCFactory.createXenoGrunt(target); // Replace with actual Droid Tower creation logic + xenoGrunt.create(); + } + + @Test + public void testAnimateWander() { + xenoGrunt.getEvents().trigger("wanderStart"); + assertEquals("xeno_run", xenoGrunt.getComponent(AnimationRenderComponent.class).getCurrentAnimation()); + } + + @Test + public void testAnimateHurt() { + xenoGrunt.getEvents().trigger("runHurt"); + assertEquals("xeno_hurt", xenoGrunt.getComponent(AnimationRenderComponent.class).getCurrentAnimation()); + } + + @Test + public void testAnimateMelee1() { + xenoGrunt.getEvents().trigger("meleeStart"); + assertEquals("xeno_melee_1", xenoGrunt.getComponent(AnimationRenderComponent.class).getCurrentAnimation()); + } + +// @Test +// public void testAnimateMelee2() { +// xenoGrunt.getEvents().trigger("meleeStart"); +// assertEquals("xeno_melee_2", xenoGrunt.getComponent(AnimationRenderComponent.class).getCurrentAnimation()); +// } + + @Test + public void testAnimateDie() { + xenoGrunt.getEvents().trigger("dieStart"); + assertEquals("xeno_die", xenoGrunt.getComponent(AnimationRenderComponent.class).getCurrentAnimation()); + } + +// @Test +// public void testAnimateStop() { +// xenoGrunt.getEvents().trigger("stop"); +// assertEquals("default", xenoGrunt.getComponent(AnimationRenderComponent.class).getCurrentAnimation()); +// } +} From ba0396f353c70d4691d19c3e797a3301c96b0757 Mon Sep 17 00:00:00 2001 From: BlairCannon97 Date: Tue, 12 Sep 2023 10:13:44 +1000 Subject: [PATCH 2/4] Cleaned up redundant tests in XenoAnimationController --- .../components/xenos/XenoAnimationControllerTest.java | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/source/core/src/test/com/csse3200/game/components/xenos/XenoAnimationControllerTest.java b/source/core/src/test/com/csse3200/game/components/xenos/XenoAnimationControllerTest.java index 9da376447..0c8b147b1 100644 --- a/source/core/src/test/com/csse3200/game/components/xenos/XenoAnimationControllerTest.java +++ b/source/core/src/test/com/csse3200/game/components/xenos/XenoAnimationControllerTest.java @@ -58,21 +58,10 @@ public void testAnimateMelee1() { assertEquals("xeno_melee_1", xenoGrunt.getComponent(AnimationRenderComponent.class).getCurrentAnimation()); } -// @Test -// public void testAnimateMelee2() { -// xenoGrunt.getEvents().trigger("meleeStart"); -// assertEquals("xeno_melee_2", xenoGrunt.getComponent(AnimationRenderComponent.class).getCurrentAnimation()); -// } - @Test public void testAnimateDie() { xenoGrunt.getEvents().trigger("dieStart"); assertEquals("xeno_die", xenoGrunt.getComponent(AnimationRenderComponent.class).getCurrentAnimation()); } -// @Test -// public void testAnimateStop() { -// xenoGrunt.getEvents().trigger("stop"); -// assertEquals("default", xenoGrunt.getComponent(AnimationRenderComponent.class).getCurrentAnimation()); -// } } From 5f7c4f495b09dcc3177db41bc7dde4426e9c3525 Mon Sep 17 00:00:00 2001 From: BlairCannon97 Date: Tue, 12 Sep 2023 10:24:57 +1000 Subject: [PATCH 3/4] Added an additional test testCreateXenoGruntHasPhysicsMovementComponent() to NPCFactoryTest. --- .../game/entities/factories/NPCFactoryTest.java | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/source/core/src/test/com/csse3200/game/entities/factories/NPCFactoryTest.java b/source/core/src/test/com/csse3200/game/entities/factories/NPCFactoryTest.java index 9c02ba0c4..9673617c1 100644 --- a/source/core/src/test/com/csse3200/game/entities/factories/NPCFactoryTest.java +++ b/source/core/src/test/com/csse3200/game/entities/factories/NPCFactoryTest.java @@ -8,6 +8,7 @@ import com.csse3200.game.physics.components.ColliderComponent; import com.csse3200.game.physics.components.HitboxComponent; import com.csse3200.game.physics.components.PhysicsComponent; +import com.csse3200.game.physics.components.PhysicsMovementComponent; import com.csse3200.game.rendering.AnimationRenderComponent; import com.csse3200.game.rendering.DebugRenderer; import com.csse3200.game.rendering.RenderService; @@ -30,12 +31,12 @@ public class NPCFactoryTest { private Entity engineerTarget; private Entity playerTarget; - private String[] texture = { + private final String[] texture = { "images/towers/turret_deployed.png", "images/towers/turret01.png", "images/towers/wallTower.png" }; - private String[] atlas = {"images/towers/turret01.atlas", + private final String[] atlas = {"images/towers/turret01.atlas", "images/mobs/xenoGrunt.atlas"}; private static final String[] sounds = { "sounds/towers/gun_shot_trimmed.mp3", @@ -62,7 +63,7 @@ public void setUp() { ServiceLocator.getResourceService() .getAsset("images/mobs/xenoGrunt.atlas", TextureAtlas.class); //playerTarget = PlayerFactory.createPlayer(); - towerTarget = TowerFactory.createBaseTower(); + //towerTarget = TowerFactory.createBaseTower(); //engineerTarget = EngineerFactory.createEngineer(); xenoGrunt = NPCFactory.createXenoGrunt(playerTarget); } @@ -100,7 +101,14 @@ public void testXenoGruntCombatStatsComponent() { @Test public void xenoGruntHasAnimationComponent() { - assertNotNull(xenoGrunt.getComponent(AnimationRenderComponent.class)); + assertNotNull(xenoGrunt.getComponent(AnimationRenderComponent.class), + "Xeno Grunt should have AnimationRenderComponent"); + } + + @Test + public void testCreateXenoGruntHasPhysicsMovementComponent() { + assertNotNull(xenoGrunt.getComponent(PhysicsMovementComponent.class), + "Xeno Grunt should have PhysicsMovementComponent"); } } From fe7dc16c3ff2a7a7564b53eeb720a36da5328a22 Mon Sep 17 00:00:00 2001 From: BlairCannon97 Date: Tue, 12 Sep 2023 10:33:02 +1000 Subject: [PATCH 4/4] Added random melee animation triggers in XenoAnimationController. --- .../npc/XenoAnimationController.java | 19 +++++++++++-------- .../xenos/XenoAnimationControllerTest.java | 5 ++++- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/source/core/src/main/com/csse3200/game/components/npc/XenoAnimationController.java b/source/core/src/main/com/csse3200/game/components/npc/XenoAnimationController.java index d4acfd8fe..2edb7ec83 100644 --- a/source/core/src/main/com/csse3200/game/components/npc/XenoAnimationController.java +++ b/source/core/src/main/com/csse3200/game/components/npc/XenoAnimationController.java @@ -3,12 +3,15 @@ import com.csse3200.game.components.Component; import com.csse3200.game.rendering.AnimationRenderComponent; +import java.util.Random; + /** * This class listens to events relevant to a ghost entity's state and plays the animation when one * of the events is triggered. */ public class XenoAnimationController extends Component { AnimationRenderComponent animator; + Random rand = new Random(); @Override public void create() { @@ -16,8 +19,7 @@ public void create() { animator = this.entity.getComponent(AnimationRenderComponent.class); entity.getEvents().addListener("wanderStart", this::animateRun); entity.getEvents().addListener("runHurt", this::animateHurt); - entity.getEvents().addListener("meleeStart", this::animateMelee1); -// entity.getEvents().addListener("meleeStart", this::animateMelee2); + entity.getEvents().addListener("meleeStart", this::animateMelee); entity.getEvents().addListener("shootStart", this::animateShoot); entity.getEvents().addListener("dieStart", this::animateDie); entity.getEvents().addListener("stop", this::stopAnimation); @@ -35,12 +37,13 @@ void animateShoot() { animator.startAnimation("xeno_shoot"); } - void animateMelee1() { - animator.startAnimation("xeno_melee_1"); - } - - void animateMelee2() { - animator.startAnimation("xeno_melee_2"); + void animateMelee() { + int randAnim = rand.nextInt(2); + if (randAnim == 1) { + animator.startAnimation("xeno_melee_1"); + } else { + animator.startAnimation("xeno_melee_2"); + } } void animateDie() { diff --git a/source/core/src/test/com/csse3200/game/components/xenos/XenoAnimationControllerTest.java b/source/core/src/test/com/csse3200/game/components/xenos/XenoAnimationControllerTest.java index 0c8b147b1..b8317ff2f 100644 --- a/source/core/src/test/com/csse3200/game/components/xenos/XenoAnimationControllerTest.java +++ b/source/core/src/test/com/csse3200/game/components/xenos/XenoAnimationControllerTest.java @@ -14,6 +14,8 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; +import java.util.Objects; + import static org.junit.jupiter.api.Assertions.assertEquals; import static org.mockito.Mockito.mock; @@ -55,7 +57,8 @@ public void testAnimateHurt() { @Test public void testAnimateMelee1() { xenoGrunt.getEvents().trigger("meleeStart"); - assertEquals("xeno_melee_1", xenoGrunt.getComponent(AnimationRenderComponent.class).getCurrentAnimation()); + assert(Objects.equals(xenoGrunt.getComponent(AnimationRenderComponent.class).getCurrentAnimation(), "xeno_melee_1") + || Objects.equals(xenoGrunt.getComponent(AnimationRenderComponent.class).getCurrentAnimation(), "xeno_melee_2")); } @Test