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 b25b91e00..42c04ce19 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,13 +1,11 @@ package com.csse3200.game.components.npc; - import com.badlogic.gdx.audio.Sound; - import com.csse3200.game.components.Component; import com.csse3200.game.rendering.AnimationRenderComponent; import com.csse3200.game.services.ServiceLocator; -import java.util.Objects; +import java.util.Random; /** * This class listens to events relevant to a ghost entity's state and plays the animation when one @@ -19,6 +17,7 @@ public class XenoAnimationController extends Component { // Sound onCollisionSound = ServiceLocator.getResourceService().getAsset( // COLLISION_SFX, Sound.class); AnimationRenderComponent animator; + Random rand = new Random(); @Override public void create() { @@ -26,8 +25,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); @@ -45,12 +43,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 new file mode 100644 index 000000000..b8317ff2f --- /dev/null +++ b/source/core/src/test/com/csse3200/game/components/xenos/XenoAnimationControllerTest.java @@ -0,0 +1,70 @@ +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 java.util.Objects; + +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"); + assert(Objects.equals(xenoGrunt.getComponent(AnimationRenderComponent.class).getCurrentAnimation(), "xeno_melee_1") + || Objects.equals(xenoGrunt.getComponent(AnimationRenderComponent.class).getCurrentAnimation(), "xeno_melee_2")); + } + + @Test + public void testAnimateDie() { + xenoGrunt.getEvents().trigger("dieStart"); + assertEquals("xeno_die", xenoGrunt.getComponent(AnimationRenderComponent.class).getCurrentAnimation()); + } + +} 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"); } }