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 df2621c1a..a2d1015fa 100644 --- a/source/core/src/main/com/csse3200/game/components/EffectComponent.java +++ b/source/core/src/main/com/csse3200/game/components/EffectComponent.java @@ -30,7 +30,7 @@ public class EffectComponent extends Component { private long burnTime; private long slowTime; private long stunTime; - private Vector2 initialSpeed; + private Vector2 defaultTargetSpeed; private static final Vector2 STUN_SPEED = new Vector2(0f,0f); private static final long BURN_TICK = 1000; @@ -59,33 +59,42 @@ public void update() { // apply slow effect if (mob) { + if (target == null) { + return; + } if (slowFlag) { - Vector2 halfSpeed = new Vector2(target.getComponent(PhysicsMovementComponent.class).getSpeed()); - changeSpeed(new Vector2(halfSpeed.x / 2, halfSpeed.y / 2)); + isSlowed = true; + Vector2 half_speed = new Vector2(defaultTargetSpeed.x / 2, defaultTargetSpeed.y / 2); + target.getComponent(PhysicsMovementComponent.class).setSpeed(half_speed); } else if (isSlowed) { - changeSpeed(initialSpeed); isSlowed = false; + target.getComponent(PhysicsMovementComponent.class).setSpeed(defaultTargetSpeed); + } + } else { + if (slowFlag && !isSlowed) { + isSlowed = true; + target.getEvents().trigger("upgradeTower", + TowerUpgraderComponent.UPGRADE.FIRERATE, 2); + } else if (!slowFlag && isSlowed) { + isSlowed = false; + target.getEvents().trigger("upgradeTower", + TowerUpgraderComponent.UPGRADE.FIRERATE, 5); } - } - if (slowFlag && !isSlowed) { - slowEffect(2); - } else if (!slowFlag && isSlowed) { - isSlowed = false; - slowEffect(5); } // apply stun effect if (mob) { if (stunFlag) { - if (initialSpeed == null) { + if (defaultTargetSpeed == null) { return; } target.getComponent(PhysicsMovementComponent.class).setSpeed(STUN_SPEED); - } else { + } else if (isStunned) { if (target == null) { return; } - target.getComponent(PhysicsMovementComponent.class).setSpeed(initialSpeed); + isStunned = false; + target.getComponent(PhysicsMovementComponent.class).setSpeed(defaultTargetSpeed); } } else { if (stunFlag && !isStunned) { @@ -98,6 +107,10 @@ public void update() { public void applyEffect(ProjectileEffects effect, Entity host, Entity target) { this.host = host; this.target = target; + defaultTargetSpeed = entity.getComponent(PhysicsMovementComponent.class).getNormalSpeed(); + if (defaultTargetSpeed == null) { + defaultTargetSpeed = new Vector2(1f,1f); + } switch (effect) { case BURN -> { burnFlag = true; @@ -107,12 +120,10 @@ public void applyEffect(ProjectileEffects effect, Entity host, Entity target) { case SLOW -> { slowFlag = true; slowTime = gameTime.getTime() + EFFECT_DURATION; - initialSpeed = entity.getComponent(PhysicsMovementComponent.class).getSpeed(); } case STUN -> { stunFlag = true; stunTime = gameTime.getTime() + EFFECT_DURATION; - initialSpeed = entity.getComponent(PhysicsMovementComponent.class).getSpeed(); } } } @@ -124,28 +135,6 @@ private void burnEffect() { lastTimeBurned = gameTime.getTime(); } - private void slowEffect(int amount) { - isSlowed = true; - if (PhysicsLayer.contains(PhysicsLayer.HUMANS, - target.getComponent(HitboxComponent.class).getLayer())) { - // if slowing human - target.getEvents().trigger("upgradeTower", - TowerUpgraderComponent.UPGRADE.FIRERATE, amount); - } else if (PhysicsLayer.contains(PhysicsLayer.NPC, - target.getComponent(HitboxComponent.class).getLayer())) { - // if slowing npc - PhysicsMovementComponent targetPhysics = target.getComponent( - PhysicsMovementComponent.class); - if (targetPhysics == null) { - return; - } - - // Halve the mob speed - targetPhysics.setSpeed(new Vector2(targetPhysics.getSpeed().x/2, - targetPhysics.getSpeed().y/2)); - } - } - private void changeSpeed(Vector2 speed) { PhysicsMovementComponent targetPhysics = target.getComponent( PhysicsMovementComponent.class); diff --git a/source/core/src/main/com/csse3200/game/components/tasks/MobTask/MobTask.java b/source/core/src/main/com/csse3200/game/components/tasks/MobTask/MobTask.java index 02eb957e0..4d8d4d058 100644 --- a/source/core/src/main/com/csse3200/game/components/tasks/MobTask/MobTask.java +++ b/source/core/src/main/com/csse3200/game/components/tasks/MobTask/MobTask.java @@ -88,8 +88,10 @@ public void start() { if (melee) { mob.getComponent(PhysicsMovementComponent.class).setSpeed(MELEE_MOB_SPEED); + mob.getComponent(PhysicsMovementComponent.class).setNormalSpeed(MELEE_MOB_SPEED); } else { mob.getComponent(PhysicsMovementComponent.class).setSpeed(MELEE_RANGE_SPEED); + mob.getComponent(PhysicsMovementComponent.class).setNormalSpeed(MELEE_RANGE_SPEED); } } @@ -98,6 +100,7 @@ public void start() { */ @Override public void update() { +// System.out.println(mob.getComponent(PhysicsMovementComponent.class).getSpeed()); // death check if (mob.getComponent(CombatStatsComponent.class).getHealth() <= 0 && !deathFlag) { changeState(State.DEATH); diff --git a/source/core/src/main/com/csse3200/game/components/tasks/bosstask/DemonBossTask.java b/source/core/src/main/com/csse3200/game/components/tasks/bosstask/DemonBossTask.java index a96794a2a..2df2112ab 100644 --- a/source/core/src/main/com/csse3200/game/components/tasks/bosstask/DemonBossTask.java +++ b/source/core/src/main/com/csse3200/game/components/tasks/bosstask/DemonBossTask.java @@ -98,6 +98,7 @@ public void start() { animation = demon.getComponent(AnimationRenderComponent.class); // get animation currentPos = demon.getPosition(); // get current position demon.getComponent(PhysicsMovementComponent.class).setSpeed(DEMON_SPEED); // set speed + demon.getComponent(PhysicsMovementComponent.class).setNormalSpeed(DEMON_SPEED); Timer.schedule(new Timer.Task() { @Override diff --git a/source/core/src/main/com/csse3200/game/components/tasks/bosstask/IceBabyTask.java b/source/core/src/main/com/csse3200/game/components/tasks/bosstask/IceBabyTask.java index f89f12ed0..e708879f8 100644 --- a/source/core/src/main/com/csse3200/game/components/tasks/bosstask/IceBabyTask.java +++ b/source/core/src/main/com/csse3200/game/components/tasks/bosstask/IceBabyTask.java @@ -84,6 +84,7 @@ public void start() { animation = iceBaby.getComponent(AnimationRenderComponent.class); currentPos = iceBaby.getPosition(); iceBaby.getComponent(PhysicsMovementComponent.class).setSpeed(ICEBABY_SPEED); + iceBaby.getComponent(PhysicsMovementComponent.class).setNormalSpeed(ICEBABY_SPEED); Timer.schedule(new Timer.Task() { @Override public void run() { diff --git a/source/core/src/main/com/csse3200/game/components/tasks/bosstask/PatrickTask.java b/source/core/src/main/com/csse3200/game/components/tasks/bosstask/PatrickTask.java index ee06a9d44..1f116e932 100644 --- a/source/core/src/main/com/csse3200/game/components/tasks/bosstask/PatrickTask.java +++ b/source/core/src/main/com/csse3200/game/components/tasks/bosstask/PatrickTask.java @@ -79,6 +79,7 @@ public void start() { animation = owner.getEntity().getComponent(AnimationRenderComponent.class); // get animation currentPos = owner.getEntity().getPosition(); // get current position patrick.getComponent(PhysicsMovementComponent.class).setSpeed(PATRICK_SPEED); // set speed + patrick.getComponent(PhysicsMovementComponent.class).setNormalSpeed(PATRICK_SPEED); // give game time to load Timer.schedule(new Timer.Task() { diff --git a/source/core/src/main/com/csse3200/game/components/tasks/bosstask/SlimeyBoyTask.java b/source/core/src/main/com/csse3200/game/components/tasks/bosstask/SlimeyBoyTask.java index bef8386d8..ecd82008b 100644 --- a/source/core/src/main/com/csse3200/game/components/tasks/bosstask/SlimeyBoyTask.java +++ b/source/core/src/main/com/csse3200/game/components/tasks/bosstask/SlimeyBoyTask.java @@ -60,6 +60,7 @@ public void start() { animation = owner.getEntity().getComponent(AnimationRenderComponent.class); // get animation currentPos = owner.getEntity().getPosition(); // get current position slimey.getComponent(PhysicsMovementComponent.class).setSpeed(SLIMEY_SPEED); // set speed + slimey.getComponent(PhysicsMovementComponent.class).setNormalSpeed(SLIMEY_SPEED); changeState(SlimeState.TRANSFORM); slimey.getEvents().trigger("demon_death_sound"); Timer.schedule(new Timer.Task() { diff --git a/source/core/src/main/com/csse3200/game/physics/components/PhysicsMovementComponent.java b/source/core/src/main/com/csse3200/game/physics/components/PhysicsMovementComponent.java index e1f88cb1d..296460656 100644 --- a/source/core/src/main/com/csse3200/game/physics/components/PhysicsMovementComponent.java +++ b/source/core/src/main/com/csse3200/game/physics/components/PhysicsMovementComponent.java @@ -12,6 +12,7 @@ public class PhysicsMovementComponent extends Component implements MovementController { private static final Logger logger = LoggerFactory.getLogger(PhysicsMovementComponent.class); private Vector2 maxSpeed = Vector2Utils.ONE; + private Vector2 normalSpeed; private float skipMovementTime = 0f; // in seconds, for knockback private PhysicsComponent physicsComponent; @@ -104,4 +105,13 @@ public void setSpeed(Vector2 speed) { public Vector2 getSpeed() { return this.maxSpeed; } + public void setNormalSpeed(Vector2 normalSpeed) { + this.normalSpeed = normalSpeed; + } + public Vector2 getNormalSpeed() { + if (normalSpeed == null) { + return maxSpeed; + } + return normalSpeed; + } }