mirror of
https://github.com/Minestom/Minestom.git
synced 2025-02-06 07:21:32 +01:00
Entity#lookAt and updating view in melee/ranged/combined attack goals when within attackable range (#494)
This commit is contained in:
parent
68ff09df8a
commit
ba76e746a2
@ -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
|
||||
|
@ -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.
|
||||
|
@ -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;
|
||||
|
@ -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;
|
||||
|
@ -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();
|
||||
|
Loading…
Reference in New Issue
Block a user