diff --git a/source/core/src/main/com/csse3200/game/components/EffectComponent.java b/source/core/src/main/com/csse3200/game/components/EffectComponent.java index 5d6830016..df2621c1a 100644 --- a/source/core/src/main/com/csse3200/game/components/EffectComponent.java +++ b/source/core/src/main/com/csse3200/game/components/EffectComponent.java @@ -16,7 +16,7 @@ public class EffectComponent extends Component { private static final long EFFECT_DURATION = 5000; - private GameTime gameTime; + private final GameTime gameTime; // effect flags private boolean burnFlag; private boolean slowFlag; @@ -31,7 +31,8 @@ public class EffectComponent extends Component { private long slowTime; private long stunTime; private Vector2 initialSpeed; - private static long BURN_TICK = 1000; + private static final Vector2 STUN_SPEED = new Vector2(0f,0f); + private static final long BURN_TICK = 1000; public EffectComponent(boolean mob) { this.mob = mob; @@ -74,10 +75,24 @@ public void update() { } // apply stun effect - if (stunFlag && !isStunned) { - stunEffect(true); - } else if (!stunFlag && isStunned) { - stunEffect(false); + if (mob) { + if (stunFlag) { + if (initialSpeed == null) { + return; + } + target.getComponent(PhysicsMovementComponent.class).setSpeed(STUN_SPEED); + } else { + if (target == null) { + return; + } + target.getComponent(PhysicsMovementComponent.class).setSpeed(initialSpeed); + } + } else { + if (stunFlag && !isStunned) { + stunEffect(true); + } else if (!stunFlag && isStunned) { + stunEffect(false); + } } } public void applyEffect(ProjectileEffects effect, Entity host, Entity target) { @@ -97,6 +112,7 @@ public void applyEffect(ProjectileEffects effect, Entity host, Entity target) { case STUN -> { stunFlag = true; stunTime = gameTime.getTime() + EFFECT_DURATION; + initialSpeed = entity.getComponent(PhysicsMovementComponent.class).getSpeed(); } } } @@ -146,23 +162,10 @@ private void stunEffect(boolean stunned) { if (targetAI == null) { return; } - Vector2 targetInitialSpeed = target.getComponent(PhysicsMovementComponent.class).getSpeed(); - if (targetInitialSpeed == null) { - return; - } - - if (mob) { - if (stunned) { - target.getComponent(PhysicsMovementComponent.class).setSpeed(new Vector2(0f,0f)); - } else { - target.getComponent(PhysicsMovementComponent.class).setSpeed(targetInitialSpeed); - } + if (stunned) { + targetAI.disposeAll(); } else { - if (stunned) { - targetAI.disposeAll(); - } else { - targetAI.restore(); - } + targetAI.restore(); } } }