Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Junit tests for TNTTowerCombatTask and DroidCombatTask #134

Merged
merged 7 commits into from
Sep 10, 2023
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@
import com.csse3200.game.ai.tasks.DefaultTask;
import com.csse3200.game.ai.tasks.PriorityTask;
import com.csse3200.game.components.CombatStatsComponent;
import com.csse3200.game.components.ProjectileEffects;
import com.csse3200.game.entities.Entity;
import com.csse3200.game.entities.factories.ProjectileFactory;
import com.csse3200.game.physics.PhysicsEngine;
import com.csse3200.game.physics.PhysicsLayer;
import com.csse3200.game.physics.raycast.RaycastHit;
Expand All @@ -24,13 +21,15 @@ public class DroidCombatTask extends DefaultTask implements PriorityTask {
private static final int INTERVAL = 1; // time interval to scan for enemies in seconds
private static final short TARGET = PhysicsLayer.NPC; // The type of targets that the tower will detect
// the following four constants are the event names that will be triggered in the state machine
private static final String GO_UP = "goUpStart";
private static final String GO_DOWN = "goDownStart";
private static final String ATTACK_UP = "attackUpStart";
private static final String ATTACK_DOWN = "attackDownStart";
private static final String WALK = "walkStart";
private static final String DEATH = "deathStart";
private static final String IDLE = "idleStart";
public static final String GO_UP = "goUpStart";
public static final String GO_DOWN = "goDownStart";
public static final String ATTACK_UP = "attackUpStart";
public static final String ATTACK_DOWN = "attackDownStart";
public static final String WALK = "walkStart";
public static final String DEATH = "deathStart";
public static final String IDLE = "idleStart";
public static final String SHOOT_UP = "ShootUp";
public static final String SHOOT_DOWN = "ShootDown";


// class attributes
Expand All @@ -43,10 +42,10 @@ public class DroidCombatTask extends DefaultTask implements PriorityTask {
private long endTime;
private final RaycastHit hit = new RaycastHit();

private enum STATE {
public enum STATE {
IDLE, UP, DOWN, SHOOT_UP, SHOOT_DOWN, WALK, DIE
}
private STATE towerState = STATE.WALK;
public STATE towerState = STATE.WALK;

/**
* @param priority Task priority when targets are detected (0 when nothing detected). Must be a positive integer.
Expand Down Expand Up @@ -105,6 +104,7 @@ public void updateTowerState() {
case IDLE -> {
if (isTargetVisible()) {
owner.getEntity().getEvents().trigger(ATTACK_UP);
owner.getEntity().getEvents().trigger(SHOOT_UP);
towerState = STATE.DOWN;
} else {
owner.getEntity().getEvents().trigger(IDLE);
Expand All @@ -113,12 +113,7 @@ public void updateTowerState() {
case SHOOT_DOWN -> {
if (isTargetVisible()) {
owner.getEntity().getEvents().trigger(ATTACK_DOWN);
Entity Projectile = ProjectileFactory.createEffectProjectile(PhysicsLayer.NPC, new Vector2(100,
owner.getEntity().getPosition().y), new Vector2(2,2), ProjectileEffects.SLOW, false);
Projectile.setScale(new Vector2(0.5f,0.5f));
Projectile.setPosition((float) (owner.getEntity().getPosition().x + 0.2),
(float) (owner.getEntity().getPosition().y - 0.2));
ServiceLocator.getEntityService().register(Projectile);
owner.getEntity().getEvents().trigger(SHOOT_DOWN);
towerState = STATE.UP;
} else {
owner.getEntity().getEvents().trigger(GO_UP);
Expand All @@ -129,13 +124,8 @@ public void updateTowerState() {
if (isTargetVisible()) {

owner.getEntity().getEvents().trigger(ATTACK_UP);
owner.getEntity().getEvents().trigger(SHOOT_UP);
towerState = STATE.DOWN;
Entity Projectile = ProjectileFactory.createEffectProjectile(PhysicsLayer.NPC, new Vector2(100,
owner.getEntity().getPosition().y), new Vector2(2,2), ProjectileEffects.SLOW, false);
Projectile.setScale(new Vector2(0.5f,0.5f));
Projectile.setPosition((float) (owner.getEntity().getPosition().x + 0.2),
(float) (owner.getEntity().getPosition().y + 0.5));
ServiceLocator.getEntityService().register(Projectile);
} else {
owner.getEntity().getEvents().trigger(IDLE);
towerState = STATE.IDLE;
Expand Down Expand Up @@ -173,7 +163,15 @@ public void updateTowerState() {
@Override
public void stop() {
super.stop();
// owner.getEntity().getEvents().trigger(STOW);
}

/**
* Returns the current state of the tower.
*
* @return the current state of the tower.
*/
public STATE getState() {
return this.towerState;
}

/**
Expand All @@ -189,8 +187,10 @@ public int getPriority() {
* Uses a raycast to determine whether there are any targets in detection range
* @return true if a target is visible, false otherwise
*/
private boolean isTargetVisible() {
public boolean isTargetVisible() {
// If there is an obstacle in the path to the max range point, mobs visible.
return physics.raycast(towerPosition, maxRangePosition, TARGET, hit);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
import com.badlogic.gdx.math.Vector2;
import com.csse3200.game.ai.tasks.DefaultTask;
import com.csse3200.game.ai.tasks.PriorityTask;
import com.csse3200.game.entities.Entity;
import com.csse3200.game.entities.EntityService;
import com.csse3200.game.physics.PhysicsEngine;
import com.csse3200.game.physics.PhysicsLayer;
import com.csse3200.game.physics.raycast.RaycastHit;
Expand All @@ -22,10 +20,10 @@ public class TNTTowerCombatTask extends DefaultTask implements PriorityTask {
private static final int INTERVAL = 1; // time interval to scan for enemies in seconds
private static final short TARGET = PhysicsLayer.NPC; // The type of targets that the tower will detect
// the following four constants are the event names that will be triggered in the state machine
private static final String DIG = "digStart";
private static final String EXPLOSION = "explodeStart";
private static final String DEFAULT = "defaultStart";
private static final String DAMAGE = "TNTDamageStart";
public static final String DIG = "digStart";
public static final String EXPLOSION = "explodeStart";
public static final String DEFAULT = "defaultStart";
public static final String DAMAGE = "TNTDamageStart";


// class attributes
Expand All @@ -37,9 +35,9 @@ public class TNTTowerCombatTask extends DefaultTask implements PriorityTask {
private final GameTime timeSource;
private long endTime;
private final RaycastHit hit = new RaycastHit();
private boolean readToDelete = false;
public boolean readToDelete = false;

private enum STATE {
public enum STATE {
IDLE, EXPLODE, REMOVE
}
private STATE towerState = STATE.IDLE;
Expand Down Expand Up @@ -136,36 +134,29 @@ public int getPriority() {
}

/**
* Fetches the active priority of the Task if a target is visible.
* @return (int) active priority if a target is visible, -1 otherwise
* Returns the current state of the tower.
*
* @return the current state of the tower.
*/
private int getActivePriority() {

return !isTargetVisible() ? 0 : priority;
}

/**
* Fetches the inactive priority of the Task if a target is not visible.
* @return (int) -1 if a target is not visible, active priority otherwise
*/
private int getInactivePriority() {

return isTargetVisible() ? priority : 0;
public STATE getState() {
return this.towerState;
}

/**
* Uses a raycast to determine whether there are any targets in detection range
* @return true if a target is visible, false otherwise
*/
private boolean isTargetVisible() {
public boolean isTargetVisible() {
// If there is an obstacle in the path to the max range point, mobs visible.
return physics.raycast(towerPosition, maxRangePosition, TARGET, hit);
}

private boolean isReadyToDelete() {
public boolean isReadyToDelete() {

return readToDelete;
}


}


Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
package com.csse3200.game.components.tower;

import com.badlogic.gdx.math.Vector2;
import com.csse3200.game.components.Component;
import com.csse3200.game.components.ProjectileEffects;
import com.csse3200.game.entities.Entity;
import com.csse3200.game.entities.factories.ProjectileFactory;
import com.csse3200.game.physics.PhysicsLayer;
import com.csse3200.game.rendering.AnimationRenderComponent;
import com.csse3200.game.services.ServiceLocator;

/**
* This class listens to events relevant to DroidTower entity's state and plays the animation when one
Expand All @@ -25,6 +31,8 @@ public void create() {
entity.getEvents().addListener("attackUpStart",this::animateAttackUp);
entity.getEvents().addListener("attackDownStart",this::animateAttackDown);
entity.getEvents().addListener("deathStart",this::animateDeath);
entity.getEvents().addListener("ShootUp",this::shootUp);
entity.getEvents().addListener("ShootDown",this::shootDown);

}

Expand Down Expand Up @@ -83,4 +91,32 @@ void animateDeath() {
*/
void animateDefault() { animator.startAnimation("idle");}


//TODO: For the time being, these items will be positioned here. Next, we should create a component that enables an entity to fire projectiles.

/**
* Fires a projectile upwards from the entity's current position.
*/
void shootUp() {
Entity Projectile = ProjectileFactory.createEffectProjectile(PhysicsLayer.NPC, new Vector2(100,
entity.getPosition().y), new Vector2(2,2), ProjectileEffects.SLOW, false);
Projectile.setScale(new Vector2(0.5f,0.5f));
Projectile.setPosition((float) (entity.getPosition().x + 0.2),
(float) (entity.getPosition().y + 0.5));
ServiceLocator.getEntityService().register(Projectile);
}

/**
* Fires a projectile downwards from the entity's current position.
*/
void shootDown() {
Entity Projectile = ProjectileFactory.createEffectProjectile(PhysicsLayer.NPC, new Vector2(100,
entity.getPosition().y), new Vector2(2,2), ProjectileEffects.SLOW, false);
Projectile.setScale(new Vector2(0.5f,0.5f));
Projectile.setPosition((float) (entity.getPosition().x + 0.2),
(float) (entity.getPosition().y - 0.2));
ServiceLocator.getEntityService().register(Projectile);

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
* Utilizes HitboxComponent and CombatStatsComponent for functionality.
*/
public class TNTDamageComponent extends Component {
private static final Logger logger = LoggerFactory.getLogger(TNTDamageComponent.class);
private short targetLayer;
private float knockbackForce = 0f;
private float radius;
Expand Down Expand Up @@ -84,12 +83,7 @@ private void applyTNTDamage() {

// Check for null components and log specifics
if (sourceHitbox == null || otherHitbox == null) {
if (sourceHitbox == null) {
logger.debug("Warning: Source Entity without HitboxComponent. Source Entity: " + entity);
}
if (otherHitbox == null) {
logger.debug("Warning: Other Entity without HitboxComponent. Other Entity: " + otherEntity);
}

continue;
}

Expand Down
Loading
Loading