Switched to Cooldown object in attacking goals

This commit is contained in:
Konstantin Shandurenko 2021-04-04 03:13:56 +03:00
parent 3f184aad41
commit e295ffea51
4 changed files with 19 additions and 18 deletions

View File

@ -20,8 +20,7 @@ import java.util.function.Function;
*/ */
public class CombinedAttackGoal extends GoalSelector { public class CombinedAttackGoal extends GoalSelector {
private final UpdateOption pathUpdateOptions = new UpdateOption(5, TimeUnit.TICK); private final Cooldown cooldown = new Cooldown(new UpdateOption(5, TimeUnit.TICK));
private long lastPathUpdate;
private final int meleeRangeSquared; private final int meleeRangeSquared;
private final int meleeDelay; private final int meleeDelay;
@ -94,8 +93,8 @@ public class CombinedAttackGoal extends GoalSelector {
Check.argCondition(desirableRange > rangedRange, "Desirable range can not exceed ranged range!"); Check.argCondition(desirableRange > rangedRange, "Desirable range can not exceed ranged range!");
} }
public UpdateOption getPathUpdateOptions() { public Cooldown getCooldown() {
return this.pathUpdateOptions; return this.cooldown;
} }
public void setProjectileGenerator(Function<Entity, EntityProjectile> projectileGenerator) { public void setProjectileGenerator(Function<Entity, EntityProjectile> projectileGenerator) {
@ -167,8 +166,8 @@ public class CombinedAttackGoal extends GoalSelector {
// Otherwise going to the target. // Otherwise going to the target.
Position targetPosition = target.getPosition(); Position targetPosition = target.getPosition();
if (pathPosition == null || !pathPosition.isSimilar(targetPosition)) { if (pathPosition == null || !pathPosition.isSimilar(targetPosition)) {
if (!Cooldown.hasCooldown(time, this.lastPathUpdate, getPathUpdateOptions())) { if (this.cooldown.isReady(time)) {
this.lastPathUpdate = time; this.cooldown.refreshLastUpdate(time);
navigator.setPathTo(targetPosition); navigator.setPathTo(targetPosition);
} }
} }

View File

@ -17,8 +17,7 @@ import org.jetbrains.annotations.NotNull;
*/ */
public class MeleeAttackGoal extends GoalSelector { public class MeleeAttackGoal extends GoalSelector {
private final UpdateOption pathUpdateOptions = new UpdateOption(5, TimeUnit.TICK); private final Cooldown cooldown = new Cooldown(new UpdateOption(5, TimeUnit.TICK));
private long lastPathUpdate;
private long lastHit; private long lastHit;
private final int delay; private final int delay;
@ -41,8 +40,8 @@ public class MeleeAttackGoal extends GoalSelector {
this.timeUnit = timeUnit; this.timeUnit = timeUnit;
} }
public UpdateOption getPathUpdateOptions() { public Cooldown getCooldown() {
return this.pathUpdateOptions; return this.cooldown;
} }
@Override @Override
@ -85,8 +84,8 @@ public class MeleeAttackGoal extends GoalSelector {
final Position pathPosition = navigator.getPathPosition(); final Position pathPosition = navigator.getPathPosition();
final Position targetPosition = target.getPosition(); final Position targetPosition = target.getPosition();
if (pathPosition == null || !pathPosition.isSimilar(targetPosition)) { if (pathPosition == null || !pathPosition.isSimilar(targetPosition)) {
if (!Cooldown.hasCooldown(time, this.lastPathUpdate, getPathUpdateOptions())) { if (this.cooldown.isReady(time)) {
this.lastPathUpdate = time; this.cooldown.refreshLastUpdate(time);
navigator.setPathTo(targetPosition); navigator.setPathTo(targetPosition);
} }
} }

View File

@ -17,8 +17,7 @@ import java.util.function.Function;
public class RangedAttackGoal extends GoalSelector { public class RangedAttackGoal extends GoalSelector {
private final UpdateOption pathUpdateOptions = new UpdateOption(5, TimeUnit.TICK); private final Cooldown cooldown = new Cooldown(new UpdateOption(5, TimeUnit.TICK));
private long lastPathUpdate;
private long lastShot; private long lastShot;
private final int delay; private final int delay;
@ -56,8 +55,8 @@ public class RangedAttackGoal extends GoalSelector {
Check.argCondition(desirableRange > attackRange, "Desirable range can not exceed attack range!"); Check.argCondition(desirableRange > attackRange, "Desirable range can not exceed attack range!");
} }
public UpdateOption getPathUpdateOptions() { public Cooldown getCooldown() {
return this.pathUpdateOptions; return this.cooldown;
} }
public void setProjectileGenerator(Function<Entity, EntityProjectile> projectileGenerator) { public void setProjectileGenerator(Function<Entity, EntityProjectile> projectileGenerator) {
@ -119,8 +118,8 @@ public class RangedAttackGoal extends GoalSelector {
} }
Position targetPosition = target.getPosition(); Position targetPosition = target.getPosition();
if (pathPosition == null || !pathPosition.isSimilar(targetPosition)) { if (pathPosition == null || !pathPosition.isSimilar(targetPosition)) {
if (!Cooldown.hasCooldown(time, this.lastPathUpdate, getPathUpdateOptions())) { if (this.cooldown.isReady(time)) {
this.lastPathUpdate = time; this.cooldown.refreshLastUpdate(time);
navigator.setPathTo(targetPosition); navigator.setPathTo(targetPosition);
} }
} }

View File

@ -12,6 +12,10 @@ public final class Cooldown {
this.lastUpdate = System.currentTimeMillis(); this.lastUpdate = System.currentTimeMillis();
} }
public UpdateOption getUpdateOption() {
return this.updateOption;
}
public void refreshLastUpdate(long lastUpdate) { public void refreshLastUpdate(long lastUpdate) {
this.lastUpdate = lastUpdate; this.lastUpdate = lastUpdate;
} }