From 2b8b59c8181ba5f1b13bad39810f2ce7f59516ea Mon Sep 17 00:00:00 2001 From: gregchan550 Date: Tue, 10 Oct 2023 21:22:57 +1000 Subject: [PATCH] commit where slow doesn't crash the game --- .../csse3200/game/areas/ForestGameArea.java | 2 +- .../game/components/EffectComponent.java | 21 +++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/source/core/src/main/com/csse3200/game/areas/ForestGameArea.java b/source/core/src/main/com/csse3200/game/areas/ForestGameArea.java index 7a4a7f81a..7794d9e00 100644 --- a/source/core/src/main/com/csse3200/game/areas/ForestGameArea.java +++ b/source/core/src/main/com/csse3200/game/areas/ForestGameArea.java @@ -326,7 +326,7 @@ public void create() { // waves.getEvents().addListener("spawnWave", this::spawnMob); spawnGregMob(18, 2); // spawnDemonBoss(); - spawnEffectProjectile(new Vector2(5f,2f), PhysicsLayer.HUMANS, 20, new Vector2(1f,1f), ProjectileEffects.STUN, false); + spawnEffectProjectile(new Vector2(10f,2f), PhysicsLayer.NPC, 20, new Vector2(2f,2f), ProjectileEffects.SLOW, false); spawnScrap(); spawnGapScanners(); 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 367be90dd..5d6830016 100644 --- a/source/core/src/main/com/csse3200/game/components/EffectComponent.java +++ b/source/core/src/main/com/csse3200/game/components/EffectComponent.java @@ -30,6 +30,7 @@ public class EffectComponent extends Component { private long burnTime; private long slowTime; private long stunTime; + private Vector2 initialSpeed; private static long BURN_TICK = 1000; public EffectComponent(boolean mob) { @@ -56,6 +57,15 @@ public void update() { } // apply slow effect + if (mob) { + if (slowFlag) { + Vector2 halfSpeed = new Vector2(target.getComponent(PhysicsMovementComponent.class).getSpeed()); + changeSpeed(new Vector2(halfSpeed.x / 2, halfSpeed.y / 2)); + } else if (isSlowed) { + changeSpeed(initialSpeed); + isSlowed = false; + } + } if (slowFlag && !isSlowed) { slowEffect(2); } else if (!slowFlag && isSlowed) { @@ -82,6 +92,7 @@ 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; @@ -119,6 +130,16 @@ private void slowEffect(int amount) { } } + private void changeSpeed(Vector2 speed) { + PhysicsMovementComponent targetPhysics = target.getComponent( + PhysicsMovementComponent.class); + if (targetPhysics == null) { + return; + } + // Set mob speed + targetPhysics.setSpeed(speed); + } + private void stunEffect(boolean stunned) { isStunned = true; AITaskComponent targetAI = target.getComponent(AITaskComponent.class);