Skip to content

Commit

Permalink
created effect component and added it to all factories
Browse files Browse the repository at this point in the history
  • Loading branch information
gregchan550 committed Oct 13, 2023
1 parent 639b93c commit de0dcbd
Show file tree
Hide file tree
Showing 6 changed files with 177 additions and 0 deletions.
166 changes: 166 additions & 0 deletions source/core/src/main/com/csse3200/game/components/EffectComponent.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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.*;
Expand Down Expand Up @@ -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());
Expand Down

0 comments on commit de0dcbd

Please sign in to comment.