From de0dcbdf4e617e4ba270018cc36c032130193152 Mon Sep 17 00:00:00 2001 From: gregchan550 Date: Fri, 13 Oct 2023 16:20:36 +1000 Subject: [PATCH] created effect component and added it to all factories --- .../game/components/EffectComponent.java | 166 ++++++++++++++++++ .../entities/factories/EngineerFactory.java | 2 + .../entities/factories/MobBossFactory.java | 1 + .../game/entities/factories/NPCFactory.java | 2 + .../entities/factories/PlayerFactory.java | 3 + .../game/entities/factories/TowerFactory.java | 3 + 6 files changed, 177 insertions(+) create mode 100644 source/core/src/main/com/csse3200/game/components/EffectComponent.java diff --git a/source/core/src/main/com/csse3200/game/components/EffectComponent.java b/source/core/src/main/com/csse3200/game/components/EffectComponent.java new file mode 100644 index 000000000..4e13ba09a --- /dev/null +++ b/source/core/src/main/com/csse3200/game/components/EffectComponent.java @@ -0,0 +1,166 @@ +package com.csse3200.game.components; + +import com.badlogic.gdx.math.Vector2; +import com.csse3200.game.ai.tasks.AITaskComponent; +import com.csse3200.game.components.CombatStatsComponent; +import com.csse3200.game.components.Component; +import com.csse3200.game.components.ProjectileEffects; +import com.csse3200.game.components.tasks.MovementTask; +import com.csse3200.game.components.tower.TowerUpgraderComponent; +import com.csse3200.game.entities.Entity; +import com.csse3200.game.physics.PhysicsLayer; +import com.csse3200.game.physics.components.HitboxComponent; +import com.csse3200.game.physics.components.PhysicsMovementComponent; +import com.csse3200.game.services.GameTime; +import com.csse3200.game.services.ServiceLocator; + +public class EffectComponent extends Component { + private static final long EFFECT_DURATION = 5000; + private final GameTime gameTime; + // effect flags + private boolean burnFlag; + private boolean slowFlag; + private boolean isSlowed; + private boolean stunFlag; + private boolean isStunned; + private boolean mob; + private Entity host; + private Entity target; + private long lastTimeBurned; + private long burnTime; + private long slowTime; + private long stunTime; + private Vector2 defaultTargetSpeed; + private PhysicsMovementComponent targetPhysics; + private static final Vector2 STUN_SPEED = new Vector2(0f,0f); + private static final long BURN_TICK = 1000; + + public EffectComponent(boolean mob) { + this.mob = mob; + this.gameTime = ServiceLocator.getTimeSource(); + } + + public void start() { + burnTime = 0; + slowTime = 0; + stunTime = 0; + } + + @Override + public void update() { + // update effect flags + burnFlag = burnTime > gameTime.getTime(); + slowFlag = slowTime > gameTime.getTime(); + stunFlag = stunTime > gameTime.getTime(); + + // apply burn effect + if (burnFlag && gameTime.getTime() > lastTimeBurned + BURN_TICK) { + burnEffect(); + } + + // apply slow effect + if (mob) { + if (target == null) { + return; + } + if (slowFlag) { + isSlowed = true; + Vector2 half_speed = new Vector2(defaultTargetSpeed.x / 2, defaultTargetSpeed.y / 2); + targetPhysics.setSpeed(half_speed); + } else if (isSlowed) { + isSlowed = false; + targetPhysics.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); + } + } + + // apply stun effect + if (mob) { + if (stunFlag) { + if (defaultTargetSpeed == null) { + return; + } + targetPhysics.setSpeed(STUN_SPEED); + } else if (isStunned) { + if (target == null) { + return; + } + isStunned = false; + targetPhysics.setSpeed(defaultTargetSpeed); + } + } else { + if (stunFlag && !isStunned) { + stunEffect(true); + } else if (!stunFlag && isStunned) { + stunEffect(false); + } + } + } + public void applyEffect(ProjectileEffects effect, Entity host, Entity target) { + this.host = host; + this.target = target; + + targetPhysics = entity.getComponent(PhysicsMovementComponent.class); + if (targetPhysics == null) { + return; + } + defaultTargetSpeed = targetPhysics.getNormalSpeed(); + if (defaultTargetSpeed == null) { + defaultTargetSpeed = new Vector2(1f,1f); + } + switch (effect) { + case BURN -> { + burnFlag = true; + burnTime = gameTime.getTime() + EFFECT_DURATION; + lastTimeBurned = gameTime.getTime(); + } + case SLOW -> { + slowFlag = true; + slowTime = gameTime.getTime() + EFFECT_DURATION; + } + case STUN -> { + stunFlag = true; + stunTime = gameTime.getTime() + EFFECT_DURATION; + } + } + } + + private void burnEffect() { + CombatStatsComponent hostCombat = this.host.getComponent(CombatStatsComponent.class); + CombatStatsComponent targetCombat = this.target.getComponent(CombatStatsComponent.class); + targetCombat.hit(hostCombat); + lastTimeBurned = gameTime.getTime(); + } + + 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); + if (targetAI == null) { + return; + } + if (stunned) { + targetAI.disposeAll(); + } else { + targetAI.restore(); + } + } +} \ No newline at end of file diff --git a/source/core/src/main/com/csse3200/game/entities/factories/EngineerFactory.java b/source/core/src/main/com/csse3200/game/entities/factories/EngineerFactory.java index e9feaf2aa..593c12de4 100644 --- a/source/core/src/main/com/csse3200/game/entities/factories/EngineerFactory.java +++ b/source/core/src/main/com/csse3200/game/entities/factories/EngineerFactory.java @@ -4,6 +4,7 @@ import com.badlogic.gdx.graphics.g2d.TextureAtlas; import com.csse3200.game.ai.tasks.AITaskComponent; import com.csse3200.game.components.CombatStatsComponent; +import com.csse3200.game.components.EffectComponent; import com.csse3200.game.components.TouchAttackComponent; import com.csse3200.game.components.npc.EngineerMenuComponent; import com.csse3200.game.components.player.HumanAnimationController; @@ -107,6 +108,7 @@ public static Entity createBaseHumanNPC() { .addComponent(new PhysicsComponent()) .addComponent(new PhysicsMovementComponent()) .addComponent(new ColliderComponent()) + .addComponent(new EffectComponent(false)) .addComponent(new HitboxComponent().setLayer(PhysicsLayer.ENGINEER)) .addComponent(new TouchAttackComponent(PhysicsLayer.NPC, 1.5f)); diff --git a/source/core/src/main/com/csse3200/game/entities/factories/MobBossFactory.java b/source/core/src/main/com/csse3200/game/entities/factories/MobBossFactory.java index 1e16adaa4..20480b0b8 100644 --- a/source/core/src/main/com/csse3200/game/entities/factories/MobBossFactory.java +++ b/source/core/src/main/com/csse3200/game/entities/factories/MobBossFactory.java @@ -219,6 +219,7 @@ public static Entity createBaseBoss() { Entity boss = new Entity() .addComponent(new PhysicsComponent()) // .addComponent(new ColliderComponent()) + .addComponent(new EffectComponent(false)) .addComponent(new PhysicsMovementComponent()) .addComponent(new HitboxComponent().setLayer(PhysicsLayer.NPC)) .addComponent(new TouchAttackComponent(PhysicsLayer.HUMANS, 1.5f)); diff --git a/source/core/src/main/com/csse3200/game/entities/factories/NPCFactory.java b/source/core/src/main/com/csse3200/game/entities/factories/NPCFactory.java index 72f464de3..fbb423b1d 100644 --- a/source/core/src/main/com/csse3200/game/entities/factories/NPCFactory.java +++ b/source/core/src/main/com/csse3200/game/entities/factories/NPCFactory.java @@ -5,6 +5,7 @@ import com.badlogic.gdx.math.Vector2; import com.csse3200.game.ai.tasks.AITaskComponent; import com.csse3200.game.components.CombatStatsComponent; +import com.csse3200.game.components.EffectComponent; import com.csse3200.game.components.TouchAttackComponent; import com.csse3200.game.components.npc.ArcaneArcherAnimationController; import com.csse3200.game.components.npc.CoatAnimationController; @@ -449,6 +450,7 @@ public static Entity createBaseNPC() { .addComponent(new PhysicsComponent()) .addComponent(new PhysicsMovementComponent()) .addComponent(new ColliderComponent()) + .addComponent(new EffectComponent(true)) .addComponent(new HitboxComponent().setLayer(PhysicsLayer.NPC)) .addComponent(new TouchAttackComponent(PhysicsLayer.HUMANS)); PhysicsUtils.setScaledCollider(npc, 0.3f, 0.5f); diff --git a/source/core/src/main/com/csse3200/game/entities/factories/PlayerFactory.java b/source/core/src/main/com/csse3200/game/entities/factories/PlayerFactory.java index d07b91497..f6cce1e6f 100644 --- a/source/core/src/main/com/csse3200/game/entities/factories/PlayerFactory.java +++ b/source/core/src/main/com/csse3200/game/entities/factories/PlayerFactory.java @@ -2,6 +2,8 @@ import com.csse3200.game.ai.tasks.AITaskComponent; import com.csse3200.game.components.CombatStatsComponent; +import com.csse3200.game.components.EffectComponent; +import com.csse3200.game.components.EffectsComponent; import com.csse3200.game.components.TouchAttackComponent; import com.csse3200.game.components.player.InventoryComponent; import com.csse3200.game.components.player.PlayerActions; @@ -47,6 +49,7 @@ public static Entity createPlayer() { .addComponent(new CombatStatsComponent(1000, 0)) .addComponent(new InventoryComponent(stats.gold)) .addComponent(inputComponent) + .addComponent(new EffectComponent(false)) .addComponent(new PlayerStatsDisplay()); PhysicsUtils.setScaledCollider(player, 0.6f, 0.3f); diff --git a/source/core/src/main/com/csse3200/game/entities/factories/TowerFactory.java b/source/core/src/main/com/csse3200/game/entities/factories/TowerFactory.java index a92dbb234..10cf2c131 100644 --- a/source/core/src/main/com/csse3200/game/entities/factories/TowerFactory.java +++ b/source/core/src/main/com/csse3200/game/entities/factories/TowerFactory.java @@ -1,6 +1,8 @@ package com.csse3200.game.entities.factories; import com.badlogic.gdx.physics.box2d.Filter; +import com.csse3200.game.components.EffectComponent; +import com.csse3200.game.components.EffectsComponent; import com.csse3200.game.components.tasks.DroidCombatTask; import com.csse3200.game.components.tasks.TNTTowerCombatTask; import com.csse3200.game.components.tasks.*; @@ -469,6 +471,7 @@ public static Entity createBaseTower() { // we're going to add more components later on Entity tower = new Entity() .addComponent(new ColliderComponent()) + .addComponent(new EffectComponent(false)) .addComponent(new HitboxComponent().setLayer(PhysicsLayer.TOWER)) // TODO: we might have to change the names of the layers .addComponent(new PhysicsComponent().setBodyType(BodyType.StaticBody)) .addComponent(new TowerUpgraderComponent());