WIP support for flying entities

This commit is contained in:
Felix Cravic 2020-11-29 16:50:57 +01:00
parent d0ad143c0f
commit bb3d57b1e5
5 changed files with 60 additions and 17 deletions

View File

@ -461,8 +461,12 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer, P
this.lastUpdate = time;
// Velocity
final boolean applyVelocity = !PlayerUtils.isNettyClient(this) ||
(PlayerUtils.isNettyClient(this) && hasVelocity());
boolean applyVelocity = false;
// Non-player entities with either velocity or gravity enabled
applyVelocity |= !PlayerUtils.isNettyClient(this) && (hasVelocity() || !hasNoGravity());
// Players with a velocity applied (client is responsible for gravity)
applyVelocity |= PlayerUtils.isNettyClient(this) && hasVelocity();
if (applyVelocity) {
final float tps = MinecraftServer.TICK_PER_SECOND;
float newX = position.getX() + velocity.getX() / tps;

View File

@ -427,21 +427,28 @@ public abstract class EntityCreature extends LivingEntity {
*/
public void moveTowards(@NotNull Position direction, float speed) {
Check.notNull(direction, "The direction cannot be null");
final float currentX = position.getX();
final float currentY = position.getY();
final float currentZ = position.getZ();
final float targetX = direction.getX();
final float targetY = direction.getY();
final float targetZ = direction.getZ();
final float dz = targetZ - currentZ;
final float dx = targetX - currentX;
final float dy = targetY - currentY;
final float dz = targetZ - currentZ;
// the purpose of these few lines is to slow down entities when they reach their destination
final float distSquared = dx * dx + dz * dz;
final float distSquared = dx * dx + dy * dy + dz * dz;
if (speed > distSquared) {
speed = distSquared;
}
final float radians = (float) Math.atan2(dz, dx);
final float speedX = (float) (Math.cos(radians) * speed);
final float speedY = dy * speed;
final float speedZ = (float) (Math.sin(radians) * speed);
lookAlong(dx, direction.getY(), dz);
@ -450,11 +457,10 @@ public abstract class EntityCreature extends LivingEntity {
Vector newVelocityOut = new Vector();
// Prevent ghosting
this.onGround = CollisionUtils.handlePhysics(this, new Vector(speedX, 0, speedZ), newPosition, newVelocityOut);
this.onGround = CollisionUtils.handlePhysics(this, new Vector(speedX, speedY, speedZ), newPosition, newVelocityOut);
// Will move the entity during Entity#tick
getPosition().setX(newPosition.getX());
getPosition().setZ(newPosition.getZ());
getPosition().copyCoordinates(newPosition);
}
/**

View File

@ -2,13 +2,14 @@ package net.minestom.server.entity.ai;
import net.minestom.server.entity.Entity;
import net.minestom.server.entity.EntityCreature;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public abstract class GoalSelector {
protected final EntityCreature entityCreature;
public GoalSelector(EntityCreature entityCreature) {
public GoalSelector(@NotNull EntityCreature entityCreature) {
this.entityCreature = entityCreature;
}
@ -25,7 +26,7 @@ public abstract class GoalSelector {
public abstract void start();
/**
* Called every tick when this {@link GoalSelector} is running
* Called every tick when this {@link GoalSelector} is running.
*
* @param time the time of the update in milliseconds
*/

View File

@ -20,6 +20,8 @@ public class PFPathingEntity implements IPathingEntity {
private boolean cautious;
private boolean climber;
private boolean swimmer;
private boolean aquatic;
private boolean avian;
private boolean aquaphobic;
private boolean avoidsDoorways;
private boolean opensDoors;
@ -84,6 +86,22 @@ public class PFPathingEntity implements IPathingEntity {
this.swimmer = swimmer;
}
public boolean isAquatic() {
return aquatic;
}
public void setAquatic(boolean aquatic) {
this.aquatic = aquatic;
}
public boolean isAvian() {
return avian;
}
public void setAvian(boolean avian) {
this.avian = avian;
}
public boolean isAquaphobic() {
return aquaphobic;
}
@ -138,12 +156,12 @@ public class PFPathingEntity implements IPathingEntity {
@Override
public boolean aquatic() {
return false;
return aquatic;
}
@Override
public boolean avian() {
return false;
return avian;
}
@Override
@ -170,9 +188,12 @@ public class PFPathingEntity implements IPathingEntity {
final float z = (float) position.z;
this.targetPosition = new Position(x, y, z);
final float entityY = entity.getPosition().getY();
if (entityY < y) {
entity.jump(1);
// Jump for non-flying entities
if (!avian) {
final float entityY = entity.getPosition().getY();
if (entityY < y) {
entity.jump(1);
}
}
}

View File

@ -166,18 +166,18 @@ public class Position {
*
* @param position the vector to copy the values from
*/
public void copy(Vector position) {
public void copy(@NotNull Vector position) {
this.x = position.getX();
this.y = position.getY();
this.z = position.getZ();
}
/**
* Sets the x/y/z field of this position to the value of {@code position}.
* Sets the x/y/z/yaw/pitch fields of this position to the value of {@code position}.
*
* @param position the position to copy the values from
*/
public void copy(Position position) {
public void copy(@NotNull Position position) {
this.x = position.getX();
this.y = position.getY();
this.z = position.getZ();
@ -185,6 +185,17 @@ public class Position {
this.pitch = position.getPitch();
}
/**
* Sets the x/y/z fields of this position to the value of {@code position}.
*
* @param position the position to copy the values from
*/
public void copyCoordinates(@NotNull Position position) {
this.x = position.getX();
this.y = position.getY();
this.z = position.getZ();
}
/**
* Copies this position object with the same values.
*