Skip to content

Commit

Permalink
Added JUnit tests for DroidAnimationController and TNTDamageComponent
Browse files Browse the repository at this point in the history
  • Loading branch information
Mohamad11Dab committed Sep 9, 2023
1 parent 5d7113c commit fb1466a
Show file tree
Hide file tree
Showing 3 changed files with 204 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,6 @@ void animateDeath() {
* Triggers the "default" animation for the entity.
* This method should be invoked when the entity returns to its default state.
*/
void animateDefault() { animator.startAnimation("default");}
void animateDefault() { animator.startAnimation("idle");}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package com.csse3200.game.components.tower;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.*;

import com.csse3200.game.entities.Entity;
import com.csse3200.game.entities.factories.TowerFactory;
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.GameTime;
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;

@ExtendWith(GameExtension.class)
public class DroidAnimationControllerTest {

private Entity mockEntity;
private final String[] texture = {"images/towers/DroidTower.png"};
private final String[] atlas = {"images/towers/DroidTower.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.loadTextures(texture);
resourceService.loadTextureAtlases(atlas);
resourceService.loadAll();

mockEntity = TowerFactory.createDroidTower(); // Replace with actual Droid Tower creation logic
mockEntity.create();
}

@Test
public void testAnimateWalk() {
mockEntity.getEvents().trigger("walkStart");
assertEquals("walk", mockEntity.getComponent(AnimationRenderComponent.class).getCurrentAnimation());
}

@Test
public void testAnimateDefault() {
mockEntity.getEvents().trigger("idleStart");
assertEquals("idle", mockEntity.getComponent(AnimationRenderComponent.class).getCurrentAnimation());
}

@Test
public void testAnimateGoUp() {
mockEntity.getEvents().trigger("goUpStart");
assertEquals("goUp", mockEntity.getComponent(AnimationRenderComponent.class).getCurrentAnimation());
}

@Test
public void testAnimateGoDown() {
mockEntity.getEvents().trigger("goDownStart");
assertEquals("goDown", mockEntity.getComponent(AnimationRenderComponent.class).getCurrentAnimation());
}

@Test
public void testAnimateAttackUp() {
mockEntity.getEvents().trigger("attackUpStart");
assertEquals("attackUp", mockEntity.getComponent(AnimationRenderComponent.class).getCurrentAnimation());
}

@Test
public void testAnimateAttackDown() {
mockEntity.getEvents().trigger("attackDownStart");
assertEquals("attackDown", mockEntity.getComponent(AnimationRenderComponent.class).getCurrentAnimation());
}

@Test
public void testAnimateDeath() {
mockEntity.getEvents().trigger("deathStart");
assertEquals("death", mockEntity.getComponent(AnimationRenderComponent.class).getCurrentAnimation());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package com.csse3200.game.components.tower;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.*;

import com.csse3200.game.components.CombatStatsComponent;
import com.csse3200.game.entities.Entity;
import com.csse3200.game.entities.EntityService;
import com.csse3200.game.extensions.GameExtension;
import com.csse3200.game.physics.PhysicsService;
import com.csse3200.game.physics.components.HitboxComponent;
import com.csse3200.game.physics.components.PhysicsComponent;
import com.csse3200.game.rendering.DebugRenderer;
import com.csse3200.game.rendering.RenderService;
import com.csse3200.game.services.GameTime;
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;

@ExtendWith(GameExtension.class)
public class TNTDamageComponentTest {

private GameTime gameTime;
private Entity Attacker;
private Entity Target_1;
private Entity Target_2;
private Entity Entity_3;
private final String[] texture = {"images/towers/TNTTower.png"};
private final String[] atlas = {"images/towers/TNTTower.atlas"};

@BeforeEach
public void setUp() {
gameTime = mock(GameTime.class);
when(gameTime.getDeltaTime()).thenReturn(1f);
ServiceLocator.registerTimeSource(gameTime);
ServiceLocator.registerPhysicsService(new PhysicsService());
ServiceLocator.registerEntityService(new EntityService());
RenderService render = new RenderService();
render.setDebug(mock(DebugRenderer.class));
ServiceLocator.registerRenderService(render);
ResourceService resourceService = new ResourceService();
ServiceLocator.registerResourceService(resourceService);
resourceService.loadTextures(texture);
resourceService.loadTextureAtlases(atlas);
resourceService.loadAll();
Attacker = createAttacker((short) (1 << 3));
Target_1 = createTarget((short) (1 << 3));
Target_2 = createTarget((short) (1 << 3));
Entity_3 = createTarget((short) (1 << 4));
ServiceLocator.getEntityService().register(Attacker);
ServiceLocator.getEntityService().register(Target_1);
ServiceLocator.getEntityService().register(Target_2);
ServiceLocator.getEntityService().register(Entity_3);


}

@Test
public void TestTNTDamageOnTargetsThatAreInRange() {
Attacker.setPosition(10,10);
Target_1.setPosition(12,10);// Same lane and inside radius

Attacker.getEvents().trigger("TNTDamageStart");

assertEquals(80, Target_1.getComponent(CombatStatsComponent.class).getHealth());


}

@Test
public void TestTNTDamageOnTargetsThatAreNoTInRange() {
Attacker.setPosition(10,10);
Target_1.setPosition(15,10); // on the same lane but outside the radius
Target_2.setPosition(11,12); // inside the radius but outside the lane


Attacker.getEvents().trigger("TNTDamageStart");
//Nothing should happen
assertEquals(100, Target_2.getComponent(CombatStatsComponent.class).getHealth());
assertEquals(100, Target_1.getComponent(CombatStatsComponent.class).getHealth());
}

@Test
public void TestTNTDamageOnEntitiesThatAreNotTargets() {
Attacker.setPosition(10,10);
Entity_3.setPosition(12,10); // on the same lane and inside the radius but different target layer


Attacker.getEvents().trigger("TNTDamageStart");
//Nothing should happen
assertEquals(100, Entity_3.getComponent(CombatStatsComponent.class).getHealth());

}

Entity createAttacker(short targetLayer) {
Entity entity =
new Entity()
.addComponent(new CombatStatsComponent(100, 20))
.addComponent(new PhysicsComponent())
.addComponent(new HitboxComponent())
.addComponent(new TNTDamageComponent(targetLayer,0,4));

return entity;
}

Entity createTarget(short layer) {
Entity target =
new Entity()
.addComponent(new CombatStatsComponent(100, 10))
.addComponent(new PhysicsComponent())
.addComponent(new HitboxComponent().setLayer(layer));

return target;
}
}


0 comments on commit fb1466a

Please sign in to comment.