Entity#lookAt and updating view in melee/ranged/combined attack goals when within attackable range (#494)

This commit is contained in:
Konstantin Shandurenko 2021-10-17 22:07:59 +03:00 committed by GitHub
parent 68ff09df8a
commit ba76e746a2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 28 additions and 2 deletions

View File

@ -40,6 +40,7 @@ import net.minestom.server.utils.block.BlockIterator;
import net.minestom.server.utils.chunk.ChunkUtils;
import net.minestom.server.utils.entity.EntityUtils;
import net.minestom.server.utils.player.PlayerUtils;
import net.minestom.server.utils.position.PositionUtils;
import net.minestom.server.utils.time.Cooldown;
import net.minestom.server.utils.time.TimeUnit;
import net.minestom.server.utils.validate.Check;
@ -280,6 +281,29 @@ public class Entity implements Viewable, Tickable, TagHandler, PermissionHandler
sendPacketToViewersAndSelf(new EntityRotationPacket(getEntityId(), yaw, pitch, onGround));
}
/**
* Changes the view of the entity so that it looks in a direction to the given position.
*
* @param position the position to look at.
*/
public void lookAt(@NotNull Pos position) {
Vec delta = position.sub(getPosition()).asVec().normalize();
setView(
PositionUtils.getLookYaw(delta.x(), delta.z()),
PositionUtils.getLookPitch(delta.x(), delta.y(), delta.z())
);
}
/**
* Changes the view of the entity so that it looks in a direction to the given entity.
*
* @param entity the entity to look at.
*/
public void lookAt(@NotNull Entity entity) {
Check.argCondition(entity.instance != instance, "Entity can look at another entity that is within it's own instance");
lookAt(entity.position);
}
/**
* When set to true, the entity will automatically get new viewers when they come too close.
* This can be use to have complete control over which player can see it, without having to deal with

View File

@ -198,6 +198,7 @@ public class CombinedAttackGoal extends GoalSelector {
if (pathPosition != null) {
navigator.setPathTo(null);
}
this.entityCreature.lookAt(target);
return;
}
// Otherwise going to the target.

View File

@ -60,8 +60,7 @@ public class FollowTargetGoal extends GoalSelector {
navigator.setPathTo(null);
return;
}
if (navigator.getPathPosition() == null ||
(!navigator.getPathPosition().samePoint(lastTargetPos))) {
if (navigator.getPathPosition() == null || !navigator.getPathPosition().samePoint(lastTargetPos)) {
navigator.setPathTo(lastTargetPos);
} else {
forceEnd = true;

View File

@ -81,6 +81,7 @@ public class MeleeAttackGoal extends GoalSelector {
// Attack the target entity
if (entityCreature.getDistance(target) <= range) {
entityCreature.lookAt(target);
if (!Cooldown.hasCooldown(time, lastHit, delay)) {
entityCreature.attack(target, true);
this.lastHit = time;

View File

@ -124,6 +124,7 @@ public class RangedAttackGoal extends GoalSelector {
if (pathPosition != null) {
navigator.setPathTo(null);
}
this.entityCreature.lookAt(target);
return;
}
final var targetPosition = target.getPosition();