Skip to content

Commit

Permalink
slow and stun fully functional and added a set normal speed to physic…
Browse files Browse the repository at this point in the history
…smovementcomponent
  • Loading branch information
gregchan550 committed Oct 10, 2023
1 parent 68d5870 commit 2b1c77b
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

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

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

0 comments on commit 2b1c77b

Please sign in to comment.