Path update cooldown for attacking entity goals

This commit is contained in:
Konstantin Shandurenko 2021-04-04 02:56:02 +03:00
parent 4cf66fde08
commit 3f184aad41
3 changed files with 36 additions and 3 deletions

View File

@ -9,6 +9,7 @@ import net.minestom.server.entity.type.projectile.EntityProjectile;
import net.minestom.server.utils.Position;
import net.minestom.server.utils.time.Cooldown;
import net.minestom.server.utils.time.TimeUnit;
import net.minestom.server.utils.time.UpdateOption;
import net.minestom.server.utils.validate.Check;
import org.jetbrains.annotations.NotNull;
@ -19,6 +20,9 @@ import java.util.function.Function;
*/
public class CombinedAttackGoal extends GoalSelector {
private final UpdateOption pathUpdateOptions = new UpdateOption(5, TimeUnit.TICK);
private long lastPathUpdate;
private final int meleeRangeSquared;
private final int meleeDelay;
private final TimeUnit meleeTimeUnit;
@ -90,6 +94,10 @@ public class CombinedAttackGoal extends GoalSelector {
Check.argCondition(desirableRange > rangedRange, "Desirable range can not exceed ranged range!");
}
public UpdateOption getPathUpdateOptions() {
return this.pathUpdateOptions;
}
public void setProjectileGenerator(Function<Entity, EntityProjectile> projectileGenerator) {
this.projectileGenerator = projectileGenerator;
}
@ -159,7 +167,10 @@ public class CombinedAttackGoal extends GoalSelector {
// Otherwise going to the target.
Position targetPosition = target.getPosition();
if (pathPosition == null || !pathPosition.isSimilar(targetPosition)) {
navigator.setPathTo(targetPosition);
if (!Cooldown.hasCooldown(time, this.lastPathUpdate, getPathUpdateOptions())) {
this.lastPathUpdate = time;
navigator.setPathTo(targetPosition);
}
}
}

View File

@ -8,6 +8,7 @@ import net.minestom.server.entity.pathfinding.Navigator;
import net.minestom.server.utils.Position;
import net.minestom.server.utils.time.Cooldown;
import net.minestom.server.utils.time.TimeUnit;
import net.minestom.server.utils.time.UpdateOption;
import org.jetbrains.annotations.NotNull;
/**
@ -16,6 +17,9 @@ import org.jetbrains.annotations.NotNull;
*/
public class MeleeAttackGoal extends GoalSelector {
private final UpdateOption pathUpdateOptions = new UpdateOption(5, TimeUnit.TICK);
private long lastPathUpdate;
private long lastHit;
private final int delay;
private final TimeUnit timeUnit;
@ -37,6 +41,10 @@ public class MeleeAttackGoal extends GoalSelector {
this.timeUnit = timeUnit;
}
public UpdateOption getPathUpdateOptions() {
return this.pathUpdateOptions;
}
@Override
public boolean shouldStart() {
this.cachedTarget = findTarget();
@ -77,7 +85,10 @@ public class MeleeAttackGoal extends GoalSelector {
final Position pathPosition = navigator.getPathPosition();
final Position targetPosition = target.getPosition();
if (pathPosition == null || !pathPosition.isSimilar(targetPosition)) {
navigator.setPathTo(targetPosition);
if (!Cooldown.hasCooldown(time, this.lastPathUpdate, getPathUpdateOptions())) {
this.lastPathUpdate = time;
navigator.setPathTo(targetPosition);
}
}
}
}

View File

@ -9,6 +9,7 @@ import net.minestom.server.entity.type.projectile.EntityProjectile;
import net.minestom.server.utils.Position;
import net.minestom.server.utils.time.Cooldown;
import net.minestom.server.utils.time.TimeUnit;
import net.minestom.server.utils.time.UpdateOption;
import net.minestom.server.utils.validate.Check;
import org.jetbrains.annotations.NotNull;
@ -16,6 +17,9 @@ import java.util.function.Function;
public class RangedAttackGoal extends GoalSelector {
private final UpdateOption pathUpdateOptions = new UpdateOption(5, TimeUnit.TICK);
private long lastPathUpdate;
private long lastShot;
private final int delay;
private final TimeUnit timeUnit;
@ -52,6 +56,10 @@ public class RangedAttackGoal extends GoalSelector {
Check.argCondition(desirableRange > attackRange, "Desirable range can not exceed attack range!");
}
public UpdateOption getPathUpdateOptions() {
return this.pathUpdateOptions;
}
public void setProjectileGenerator(Function<Entity, EntityProjectile> projectileGenerator) {
this.projectileGenerator = projectileGenerator;
}
@ -111,7 +119,10 @@ public class RangedAttackGoal extends GoalSelector {
}
Position targetPosition = target.getPosition();
if (pathPosition == null || !pathPosition.isSimilar(targetPosition)) {
navigator.setPathTo(targetPosition);
if (!Cooldown.hasCooldown(time, this.lastPathUpdate, getPathUpdateOptions())) {
this.lastPathUpdate = time;
navigator.setPathTo(targetPosition);
}
}
}