Skip to content

Commit

Permalink
added projectile effects component
Browse files Browse the repository at this point in the history
  • Loading branch information
gregchan550 committed Oct 13, 2023
1 parent de0dcbd commit 5d80561
Showing 1 changed file with 57 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,24 +75,18 @@ private void onCollisionEnd(Fixture me, Fixture other) {
}

// Apply effect
if (effect == ProjectileEffects.FIREBALL) {
if (aoe) {
applyAoeEffect(ProjectileEffects.FIREBALL);
}
if (aoe) {
applyAoeEffect(effect);
} else {
if (aoe) {
applyAoeEffect(effect);
} else {
applySingleEffect(effect, otherCombatStats, otherEntity);
}
applySingleEffect(effect, otherEntity);
}
}

/**
* Used for singe targeting projectiles to apply effects entity it collides with.
* @param effect effect to be applied to entity
*/
public void applySingleEffect(ProjectileEffects effect, CombatStatsComponent targetCombatStats, Entity targetEntity) {
public void applySingleEffect(ProjectileEffects effect, Entity targetEntity) {
Entity hostEntity = getEntity();
CombatStatsComponent hostCombatStats = hostEntity.getComponent(CombatStatsComponent.class);

Expand All @@ -101,15 +95,12 @@ public void applySingleEffect(ProjectileEffects effect, CombatStatsComponent tar
return;
}

// Apply effect
switch (effect) {
case FIREBALL -> {}
case BURN -> {
burnEffect(targetCombatStats, hostCombatStats);
}
case SLOW -> {slowEffect(targetEntity);}
case STUN -> {stunEffect(targetEntity);}
// apply effect
EffectComponent effectComponent = targetEntity.getComponent(EffectComponent.class);
if (effectComponent == null) {
return;
}
effectComponent.applyEffect(effect, hostEntity, targetEntity);
}
/**
* Used for aoe projectiles to apply effects to all entities within the area of effect (radius).
Expand Down Expand Up @@ -137,19 +128,12 @@ public void applyAoeEffect(ProjectileEffects effect) {
return;
}

CombatStatsComponent targetCombatStats = targetEntity.getComponent(CombatStatsComponent.class);
if (targetCombatStats != null) {
switch (effect) {
case FIREBALL -> {fireballEffect(targetCombatStats, hostCombatStats);}
case BURN -> {burnEffect(targetCombatStats, hostCombatStats);}
case SLOW -> {slowEffect(targetEntity);}
case STUN -> {
stunEffect(targetEntity);
}
}
} else {
// apply effect
EffectComponent effectComponent = targetEntity.getComponent(EffectComponent.class);
if (effectComponent == null) {
return;
}
effectComponent.applyEffect(effect, hostEntity, targetEntity);
}
}

Expand All @@ -162,35 +146,35 @@ private void fireballEffect(CombatStatsComponent target, CombatStatsComponent ho
target.hit(host);
}

/**
* Applies 5 ticks of damage from hosts' CombatStatsComponent over 5 seconds
* @param target CombatStatsComponent of entity hit by projectile
* @param host CombatStatsComponent of projectile
*/
private void burnEffect(CombatStatsComponent target, CombatStatsComponent host) {
// Ensure burn effects aren't applied multiple times by same projectile
if (burnEntities.contains(target, false)) {
return;
}
burnEntities.add(target);
// Create a timer task to apply the effect repeatedly
int numberOfTicks = 5;
long delay = 1;
Timer.schedule(new Timer.Task() {
private int count = 0;

@Override
public void run() {
if (count < numberOfTicks) {
target.hit(host);
count++;
} else {
// Ensure to cancel the task when it's done
this.cancel();
}
}
}, delay, delay);
}
// /**
// * Applies 5 ticks of damage from hosts' CombatStatsComponent over 5 seconds
// * @param target CombatStatsComponent of entity hit by projectile
// * @param host CombatStatsComponent of projectile
// */
// private void burnEffect(CombatStatsComponent target, CombatStatsComponent host) {
// // Ensure burn effects aren't applied multiple times by same projectile
// if (burnEntities.contains(target, false)) {
// return;
// }
// burnEntities.add(target);
// // Create a timer task to apply the effect repeatedly
// int numberOfTicks = 5;
// long delay = 1;
// Timer.schedule(new Timer.Task() {
// private int count = 0;
//
// @Override
// public void run() {
// if (count < numberOfTicks) {
// target.hit(host);
// count++;
// } else {
// // Ensure to cancel the task when it's done
// this.cancel();
// }
// }
// }, delay, delay);
// }

/**
* Applies slow effect to targetEntity. If entity is a mob, speed
Expand Down Expand Up @@ -262,22 +246,22 @@ private void stunEffect(Entity targetEntity) {
if (stunnedEntities.contains(targetEntity)) {
return;
}

taskComponent.disposeAll();
stunnedEntities.add(targetEntity);

new java.util.Timer().schedule(
new java.util.TimerTask() {
@Override
public void run() {
taskComponent.restore();
for (int i = 0; i < stunnedEntities.size(); i++) {
if (stunnedEntities.get(i).equals(targetEntity)) {
stunnedEntities.remove(stunnedEntities.get(i));

new java.util.Timer().schedule(
new java.util.TimerTask() {
@Override
public void run() {
taskComponent.restore();
for (int i = 0; i < stunnedEntities.size(); i++) {
if (stunnedEntities.get(i).equals(targetEntity)) {
stunnedEntities.remove(stunnedEntities.get(i));
}
}
this.cancel();
}
}
this.cancel();
}
}, 5000);
}, 5000);
}
}
}

0 comments on commit 5d80561

Please sign in to comment.