Skip to content

Commit

Permalink
stun effect fully functional for mobs: constantly sets speed to 0 whi…
Browse files Browse the repository at this point in the history
…le its stunned
  • Loading branch information
gregchan550 committed Oct 10, 2023
1 parent 73fe55d commit 9a0ffde
Showing 1 changed file with 26 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -30,7 +30,9 @@ public class EffectComponent extends Component {
private long burnTime;
private long slowTime;
private long stunTime;
private static long BURN_TICK = 1000;
private Vector2 initialSpeed;
private static final Vector2 STUN_SPEED = new Vector2(0f,0f);
private static final long BURN_TICK = 1000;

public EffectComponent(boolean mob) {
this.mob = mob;
Expand Down Expand Up @@ -64,10 +66,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) {
Expand All @@ -86,6 +102,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();
}
}
}
Expand Down Expand Up @@ -125,23 +142,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();
}
}
}

0 comments on commit 9a0ffde

Please sign in to comment.