Skip to content

Commit

Permalink
refactor: Resolved code smells.
Browse files Browse the repository at this point in the history
  • Loading branch information
JSLLW committed Oct 15, 2023
1 parent 2a4e2b4 commit 1856c3c
Show file tree
Hide file tree
Showing 31 changed files with 231 additions and 318 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ public void applyEffect(ProjectileEffects effect, Entity host, Entity target) {
stunFlag = true;
stunTime = gameTime.getTime() + EFFECT_DURATION;
}
case FIREBALL -> {}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ public void update() {
runFlag = true;
}
}
case DEATH, DEFAULT -> {}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,9 @@
import com.csse3200.game.entities.Entity;
import com.csse3200.game.entities.factories.MobBossFactory;
import com.csse3200.game.entities.factories.ProjectileFactory;
import com.csse3200.game.physics.PhysicsEngine;
import com.csse3200.game.physics.PhysicsLayer;
import com.csse3200.game.physics.components.PhysicsMovementComponent;
import com.csse3200.game.rendering.AnimationRenderComponent;
import com.csse3200.game.services.GameTime;
import com.csse3200.game.services.ServiceLocator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -31,7 +29,6 @@ public class DemonBossTask extends DefaultTask implements PriorityTask {
// Constants
private static final int PRIORITY = 3;
private static final Vector2 DEMON_SPEED = new Vector2(1f, 1f);
private static final float STOP_DISTANCE = 0.1f;
private static final float JUMP_DISTANCE = 3.0f;
private static final double Y_TOP_BOUNDARY = 5.5;
private static final double Y_BOT_BOUNDARY = 0.5;
Expand All @@ -50,9 +47,6 @@ public class DemonBossTask extends DefaultTask implements PriorityTask {
// Private variables
private static final Logger logger = LoggerFactory.getLogger(DemonBossTask.class);
private Vector2 currentPos;
private final PhysicsEngine physics;
private final GameTime gameTime;
private Vector2 jumpPos;
private MovementTask jumpTask;
private DemonState state = DemonState.IDLE;
private DemonState prevState;
Expand All @@ -79,14 +73,6 @@ private enum DemonState {
TRANSFORM, IDLE, CAST, CLEAVE, DEATH, BREATH, SMASH, TAKE_HIT, WALK
}

/**
* The demon boss task constructor. Initialises the physics and time.
*/
public DemonBossTask() {
physics = ServiceLocator.getPhysicsService().getPhysics();
gameTime = ServiceLocator.getTimeSource();
}

/**
* Starts transform animation, triggers idle animation which starts
* sequence, and dynamically shifts the demons boundary to the left.
Expand Down Expand Up @@ -187,10 +173,8 @@ public void update() {
}
}
case TRANSFORM -> {
if (health <= 0) {
if (animation.isFinished()) {
changeState(DemonState.DEATH);
}
if (health <= 0 && animation.isFinished()) {
changeState(DemonState.DEATH);
}
}
case TAKE_HIT -> {
Expand All @@ -199,6 +183,7 @@ public void update() {
halfHealthFlag = true;
}
}
case DEATH, WALK -> {}
}
}

Expand Down Expand Up @@ -308,6 +293,7 @@ public void run() {
*/
private Vector2 getJumpPos() {
// check if boundary has shifted causing demon to be out of bounds
Vector2 jumpPos;
if (currentPos.x > xRightBoundary) {
jumpPos = new Vector2(currentPos.x - JUMP_DISTANCE, currentPos.y); //jump back into boundary
return jumpPos;
Expand All @@ -317,6 +303,7 @@ private Vector2 getJumpPos() {
if (currentPos.dst(ServiceLocator.getEntityService().getClosestEntityOfLayer(
demon, PhysicsLayer.HUMANS).getPosition()) < 2f) {
jumpPos = new Vector2(currentPos.x + JUMP_DISTANCE, currentPos.y);
//TODO: Check whether jumpPos needs to be returned
}

float randomAngle = MathUtils.random(0, 2 * MathUtils.PI);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public void update() {
if (towerAhead()) {
Entity newProjectile = ProjectileFactory.createEffectProjectile(PhysicsLayer.TOWER, new Vector2(0,currentPos.y + 0.75f), new Vector2(2f,2f), ProjectileEffects.BURN, false);
newProjectile.scaleHeight(-0.4f);
newProjectile.setPosition((float) (currentPos.x), (float) (currentPos.y+0.75f));
newProjectile.setPosition(currentPos.x, currentPos.y + 0.75f);
ServiceLocator.getEntityService().register(newProjectile);
}
startWaiting();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,10 @@
import com.csse3200.game.components.tasks.MovementTask;
import com.csse3200.game.entities.Entity;
import com.csse3200.game.entities.factories.NPCFactory;
import com.csse3200.game.physics.PhysicsEngine;
import com.csse3200.game.physics.PhysicsLayer;
import com.csse3200.game.physics.components.HitboxComponent;
import com.csse3200.game.physics.components.PhysicsMovementComponent;
import com.csse3200.game.rendering.AnimationRenderComponent;
import com.csse3200.game.services.GameTime;
import com.csse3200.game.services.ServiceLocator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -34,8 +32,6 @@ public class IceBabyTask extends DefaultTask implements PriorityTask {
private static final int Y_BOT_BOUNDARY = 1;
private static final Logger logger = LoggerFactory.getLogger(IceBabyTask.class);
/** Variable names */
private PhysicsEngine physics;
private GameTime gameTime;
private STATE prevState;
private AnimationRenderComponent animation;
private Entity iceBaby;
Expand All @@ -44,7 +40,6 @@ public class IceBabyTask extends DefaultTask implements PriorityTask {
private MovementTask walkTask;
private static int xRightBoundary = 17;
private static int xLeftBoundary = 12;
private boolean aoe = true;
private boolean startFlag = false;
private boolean isWalking;
/** Animation constants */
Expand All @@ -62,14 +57,6 @@ private enum STATE {
}
private STATE iceBabyState = STATE.IDLE;

/**
* Constructor for IceBabyTask
*/
public IceBabyTask() {
physics = ServiceLocator.getPhysicsService().getPhysics();
gameTime = ServiceLocator.getTimeSource();
}

//ice baby should be able to poop out little mobs - spawn new
//ice baby can also do aoe attack based on the animation
//ice baby does punches to towers once it is close
Expand Down Expand Up @@ -141,14 +128,15 @@ public void update() {
}
case ATK1, ATK2 -> {
if (animation.isFinished()) {
ATK3();
atk3();
}
}
case ATK3 -> {
if (animation.isFinished()) {
changeState(STATE.IDLE);
}
}
case DEATH, INTRO, STAGGER, TAKEHIT -> {}
}
}

Expand Down Expand Up @@ -253,7 +241,7 @@ private boolean walkComplete() {
/**
* Changes the state of the animation and deals damage to nearby humans
*/
private void ATK3() {
private void atk3() {
changeState(STATE.ATK3);
animate();
Entity target = ServiceLocator.getEntityService().getClosestEntityOfLayer(iceBaby,
Expand Down Expand Up @@ -309,16 +297,11 @@ private Array<Entity> getNearbyHumans(int radius) {
for (int i = 0; i < nearbyEntities.size; i++) {
Entity targetEntity = nearbyEntities.get(i);
HitboxComponent targetHitbox = targetEntity.getComponent(HitboxComponent.class);
if (targetHitbox == null) {
// check target hitbox and layer
if (targetHitbox == null || (!PhysicsLayer.contains(PhysicsLayer.HUMANS, targetHitbox.
getLayer()))) {
break;
}

// check target layer
if (!PhysicsLayer.contains(PhysicsLayer.HUMANS, targetHitbox.
getLayer())) {
break;
}

nearbyHumans.add(targetEntity);
}
return nearbyHumans;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,8 @@
import com.csse3200.game.components.CombatStatsComponent;
import com.csse3200.game.entities.Entity;
import com.csse3200.game.entities.factories.DropFactory;
import com.csse3200.game.physics.PhysicsEngine;
import com.csse3200.game.physics.raycast.RaycastHit;
import com.csse3200.game.services.GameTime;
import com.csse3200.game.services.ServiceLocator;
//import com.csse3200.game.rendering.DebugRenderer;


/**
* Task that prints a message to the terminal whenever it is called.
Expand All @@ -21,10 +17,8 @@ public class MobBossDeathTask extends DefaultTask implements PriorityTask {

private final int priority;
private Vector2 bossPosition = new Vector2(10f,10f);
private final PhysicsEngine physics;
private GameTime timeSource;
private long endTime;
private final RaycastHit hit = new RaycastHit();

private int bossHealth;

Expand All @@ -33,9 +27,6 @@ public class MobBossDeathTask extends DefaultTask implements PriorityTask {
*/
public MobBossDeathTask(int priority) {
this.priority = priority;

physics = ServiceLocator.getPhysicsService().getPhysics();

timeSource = ServiceLocator.getTimeSource();
}

Expand Down Expand Up @@ -70,11 +61,6 @@ public void updateMobBossState() {

}

@Override
public void stop() {
super.stop();
}

@Override
public int getPriority() {
if (status == Status.ACTIVE) {
Expand All @@ -98,11 +84,7 @@ private int getInactivePriority() {
return -1;
}
private boolean bossIsDead(int bosshealth) {

if (bosshealth <= 0) {
return true;
}
return false;
return bosshealth <= 0;
}

private void killboss() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,9 @@
import com.csse3200.game.entities.Entity;
import com.csse3200.game.entities.factories.MobBossFactory;
import com.csse3200.game.entities.factories.ProjectileFactory;
import com.csse3200.game.physics.PhysicsEngine;
import com.csse3200.game.physics.PhysicsLayer;
import com.csse3200.game.physics.components.PhysicsMovementComponent;
import com.csse3200.game.rendering.AnimationRenderComponent;
import com.csse3200.game.services.GameTime;
import com.csse3200.game.services.ServiceLocator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -28,7 +26,6 @@ public class PatrickTask extends DefaultTask implements PriorityTask {
// Constants
private static final int PRIORITY = 3;
private static final Vector2 PATRICK_SPEED = new Vector2(1f, 1f);
private static final float MAX_RADIUS = 20f;
private static final int ATTACK_DAMAGE = 100;
private static final float RANGE_MIN_X = 10f;
private static final float RANGE_MAX_X = 18f;
Expand All @@ -38,9 +35,6 @@ public class PatrickTask extends DefaultTask implements PriorityTask {

// Private variables
private static final Logger logger = LoggerFactory.getLogger(PatrickTask.class);
private Vector2 currentPos;
private PhysicsEngine physics;
private GameTime gameTime;
private PatrickState state = PatrickState.IDLE;
private PatrickState prevState;
private AnimationRenderComponent animation;
Expand All @@ -61,14 +55,6 @@ private enum PatrickState {
IDLE, WALK, ATTACK, HURT, DEATH, SPELL, APPEAR
}

/**
* Constructor for PatrickTask
*/
public PatrickTask() {
physics = ServiceLocator.getPhysicsService().getPhysics();
gameTime = ServiceLocator.getTimeSource();
}

/**
* What is called when the patrick task is assigned
*/
Expand All @@ -77,7 +63,6 @@ public void start() {
super.start();
patrick = owner.getEntity();
animation = owner.getEntity().getComponent(AnimationRenderComponent.class); // get animation
currentPos = owner.getEntity().getPosition(); // get current position
patrick.getComponent(PhysicsMovementComponent.class).setSpeed(PATRICK_SPEED); // set speed

// give game time to load
Expand All @@ -101,10 +86,7 @@ public void update() {
// give game time to load
if (!startFlag) {
return;
}

// update teleport task while teleporting
if (teleportFlag) {
} else if (teleportFlag) { // update teleport task while teleporting
teleportTask.update();
if (teleportTask.getStatus().equals(Status.FINISHED)) {
teleportFlag = false;
Expand Down Expand Up @@ -151,15 +133,12 @@ public void run() {
}, 1f);
changeState(PatrickState.ATTACK);
meleeFlag = false;
} else if (rangeFlag && shotsFired > 2) {
rangeFlag = false;
meleeAttack();
shotsFired = 0; // reset shots fired
} else if (rangeFlag) {
// shoot 3 projectiles
if (shotsFired > 2) {
rangeFlag = false;
meleeAttack();
shotsFired = 0; // reset shots fired
} else {
changeState(PatrickState.IDLE);
}
changeState(PatrickState.IDLE);
}
}
case IDLE -> {
Expand All @@ -174,6 +153,7 @@ public void run() {
rangeFlag = true;
}
}
case WALK, HURT, DEATH, SPELL -> {}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,18 @@
import com.badlogic.gdx.utils.Timer;
import com.csse3200.game.ai.tasks.DefaultTask;
import com.csse3200.game.components.CombatStatsComponent;
import com.csse3200.game.components.tasks.MovementTask;
import com.csse3200.game.entities.Entity;
import com.csse3200.game.rendering.AnimationRenderComponent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class PatrickTeleportTask extends DefaultTask {
private static final Logger logger = LoggerFactory.getLogger(MovementTask.class);
private static final Logger logger = LoggerFactory.getLogger(PatrickTeleportTask.class);
private Entity patrick;
private final Vector2 location;
private PatrickState state = PatrickState.IDLE;
private PatrickState prevState;
private AnimationRenderComponent animation;
private Status status = Status.INACTIVE;
private CombatStatsComponent combatStats;
private int health;
private enum PatrickState {
Expand All @@ -32,6 +30,7 @@ public PatrickTeleportTask(Entity patrick, Vector2 location) {
@Override
public void start() {
super.start();
status = Status.INACTIVE;
animation = owner.getEntity().getComponent(AnimationRenderComponent.class);
combatStats = owner.getEntity().getComponent(CombatStatsComponent.class);
health = combatStats.getHealth();
Expand Down Expand Up @@ -70,6 +69,7 @@ public void update() {
status = Status.FINISHED;
}
}
case IDLE -> {}
}
}

Expand Down
Loading

0 comments on commit 1856c3c

Please sign in to comment.