mirror of
https://github.com/Minestom/Minestom.git
synced 2025-03-09 13:19:05 +01:00
Improve follow target goal
This commit is contained in:
parent
f8840cf7a9
commit
012e65337d
@ -80,7 +80,8 @@ public final class UpdateManager {
|
||||
try {
|
||||
Thread.sleep(sleepTime);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
if (!stopRequested)
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -3,50 +3,79 @@ package net.minestom.server.entity.ai.goal;
|
||||
import net.minestom.server.entity.Entity;
|
||||
import net.minestom.server.entity.EntityCreature;
|
||||
import net.minestom.server.entity.ai.GoalSelector;
|
||||
import net.minestom.server.utils.MathUtils;
|
||||
import net.minestom.server.utils.Position;
|
||||
import net.minestom.server.utils.time.UpdateOption;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class FollowTargetGoal extends GoalSelector {
|
||||
|
||||
public FollowTargetGoal(@NotNull EntityCreature entityCreature) {
|
||||
private final UpdateOption updateOption;
|
||||
private long lastUpdateTime = 0;
|
||||
private boolean forceEnd = false;
|
||||
private Position lastTargetPos;
|
||||
|
||||
public FollowTargetGoal(@NotNull EntityCreature entityCreature, @NotNull UpdateOption updateOption) {
|
||||
super(entityCreature);
|
||||
this.updateOption = updateOption;
|
||||
}
|
||||
|
||||
private float getDistance(Position a, Position b) {
|
||||
if (a == null || b == null)
|
||||
throw new NullPointerException();
|
||||
return MathUtils.square(a.getX() - b.getX()) +
|
||||
MathUtils.square(a.getZ() - b.getZ());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean shouldStart() {
|
||||
return entityCreature.getTarget() != null;
|
||||
return entityCreature.getTarget() != null && getDistance(entityCreature.getTarget().getPosition(), entityCreature.getPosition()) >= 2;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void start() {
|
||||
lastUpdateTime = 0;
|
||||
forceEnd = false;
|
||||
lastTargetPos = null;
|
||||
final Entity target = entityCreature.getTarget();
|
||||
|
||||
if (target != null) {
|
||||
final Position targetPosition = target.getPosition();
|
||||
if (targetPosition.getDistance(entityCreature.getPosition()) < 1) {
|
||||
lastTargetPos = target.getPosition().copy();
|
||||
if (getDistance(lastTargetPos, entityCreature.getPosition()) < 2) {
|
||||
forceEnd = true;
|
||||
entityCreature.setPathTo(null);
|
||||
return;
|
||||
}
|
||||
|
||||
if (entityCreature.getPathPosition() == null ||
|
||||
(!entityCreature.getPathPosition().isSimilar(targetPosition))) {
|
||||
entityCreature.setPathTo(targetPosition);
|
||||
}
|
||||
}
|
||||
(!entityCreature.getPathPosition().isSimilar(lastTargetPos))) {
|
||||
entityCreature.setPathTo(lastTargetPos);
|
||||
} else
|
||||
forceEnd = true;
|
||||
} else
|
||||
forceEnd = true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void tick(long time) {
|
||||
|
||||
if (forceEnd || updateOption.getValue() == 0 || updateOption.getTimeUnit().toMilliseconds(updateOption.getValue()) + lastUpdateTime > time) {
|
||||
return;
|
||||
}
|
||||
Position targetPos = entityCreature.getTarget() != null ? entityCreature.getTarget().getPosition() : null;
|
||||
if (targetPos != null && !targetPos.equals(lastTargetPos)) {
|
||||
lastUpdateTime = time;
|
||||
lastTargetPos.copy(lastTargetPos);
|
||||
entityCreature.setPathTo(targetPos);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean shouldEnd() {
|
||||
return true;
|
||||
return forceEnd || entityCreature.getTarget() == null || getDistance(entityCreature.getTarget().getPosition(), entityCreature.getPosition()) < 2;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void end() {
|
||||
|
||||
entityCreature.setPathTo(null);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user