Merge pull request #216 from RinesThaix/attackingGoalsPathUpdateCooldown

Path update cooldown for attacking entity goals
This commit is contained in:
TheMode 2021-04-04 02:15:54 +02:00 committed by GitHub
commit 7aa9136449
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 37 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,8 @@ import java.util.function.Function;
*/
public class CombinedAttackGoal extends GoalSelector {
private final Cooldown cooldown = new Cooldown(new UpdateOption(5, TimeUnit.TICK));
private final int meleeRangeSquared;
private final int meleeDelay;
private final TimeUnit meleeTimeUnit;
@ -90,6 +93,10 @@ public class CombinedAttackGoal extends GoalSelector {
Check.argCondition(desirableRange > rangedRange, "Desirable range can not exceed ranged range!");
}
public Cooldown getCooldown() {
return this.cooldown;
}
public void setProjectileGenerator(Function<Entity, EntityProjectile> projectileGenerator) {
this.projectileGenerator = projectileGenerator;
}
@ -159,7 +166,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 (this.cooldown.isReady(time)) {
this.cooldown.refreshLastUpdate(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,8 @@ import org.jetbrains.annotations.NotNull;
*/
public class MeleeAttackGoal extends GoalSelector {
private final Cooldown cooldown = new Cooldown(new UpdateOption(5, TimeUnit.TICK));
private long lastHit;
private final int delay;
private final TimeUnit timeUnit;
@ -37,6 +40,10 @@ public class MeleeAttackGoal extends GoalSelector {
this.timeUnit = timeUnit;
}
public Cooldown getCooldown() {
return this.cooldown;
}
@Override
public boolean shouldStart() {
this.cachedTarget = findTarget();
@ -77,7 +84,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 (this.cooldown.isReady(time)) {
this.cooldown.refreshLastUpdate(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,8 @@ import java.util.function.Function;
public class RangedAttackGoal extends GoalSelector {
private final Cooldown cooldown = new Cooldown(new UpdateOption(5, TimeUnit.TICK));
private long lastShot;
private final int delay;
private final TimeUnit timeUnit;
@ -52,6 +55,10 @@ public class RangedAttackGoal extends GoalSelector {
Check.argCondition(desirableRange > attackRange, "Desirable range can not exceed attack range!");
}
public Cooldown getCooldown() {
return this.cooldown;
}
public void setProjectileGenerator(Function<Entity, EntityProjectile> projectileGenerator) {
this.projectileGenerator = projectileGenerator;
}
@ -111,7 +118,10 @@ public class RangedAttackGoal extends GoalSelector {
}
Position targetPosition = target.getPosition();
if (pathPosition == null || !pathPosition.isSimilar(targetPosition)) {
navigator.setPathTo(targetPosition);
if (this.cooldown.isReady(time)) {
this.cooldown.refreshLastUpdate(time);
navigator.setPathTo(targetPosition);
}
}
}

View File

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