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