Minestom/src/main/java/fr/themode/minestom/entity/EntityCreature.java

158 lines
5.8 KiB
Java
Raw Normal View History

2019-08-10 08:44:35 +02:00
package fr.themode.minestom.entity;
2020-04-09 14:25:42 +02:00
import fr.themode.minestom.entity.pathfinding.EntityPathFinder;
import fr.themode.minestom.entity.property.Attribute;
2019-08-30 01:17:46 +02:00
import fr.themode.minestom.net.packet.server.play.*;
2019-08-10 08:44:35 +02:00
import fr.themode.minestom.net.player.PlayerConnection;
2020-04-09 14:25:42 +02:00
import fr.themode.minestom.utils.BlockPosition;
2019-08-27 20:49:11 +02:00
import fr.themode.minestom.utils.ChunkUtils;
2019-08-21 16:50:52 +02:00
import fr.themode.minestom.utils.Position;
2020-04-09 14:25:42 +02:00
import fr.themode.minestom.utils.Vector;
2019-08-10 08:44:35 +02:00
2020-04-09 14:25:42 +02:00
import java.util.LinkedList;
2019-08-10 08:44:35 +02:00
2020-04-09 14:25:42 +02:00
public abstract class EntityCreature extends LivingEntity {
2020-03-29 20:58:30 +02:00
2020-04-09 14:25:42 +02:00
private EntityPathFinder pathFinder = new EntityPathFinder(this);
private LinkedList<BlockPosition> blockPositions;
private Position targetPosition;
2020-03-29 20:58:30 +02:00
2020-04-09 14:25:42 +02:00
public EntityCreature(EntityType entityType, Position spawnPosition) {
super(entityType.getId(), spawnPosition);
2019-08-10 08:44:35 +02:00
}
2019-08-24 20:34:01 +02:00
@Override
public void update() {
super.update();
2020-04-09 14:25:42 +02:00
// Path finding
if (blockPositions != null) {
if (targetPosition != null) {
float distance = getPosition().getDistance(targetPosition);
if (distance < 0.2f) {
setNextPathPosition();
//System.out.println("END TARGET");
} else {
moveTowards(targetPosition, getAttributeValue(Attribute.MOVEMENT_SPEED));
//System.out.println("MOVE TOWARD " + targetPosition);
}
}
}
2019-08-24 20:34:01 +02:00
}
2019-08-30 01:17:46 +02:00
public void move(float x, float y, float z, boolean updateView) {
2019-08-21 16:50:52 +02:00
Position position = getPosition();
float newX = position.getX() + x;
float newY = position.getY() + y;
float newZ = position.getZ() + z;
2019-08-11 13:57:23 +02:00
2020-02-11 16:48:06 +01:00
// Creatures cannot move in unload chunk
2019-08-27 20:49:11 +02:00
if (ChunkUtils.isChunkUnloaded(getInstance(), newX, newZ))
2019-08-11 13:57:23 +02:00
return;
2019-08-29 02:15:52 +02:00
float lastYaw = position.getYaw();
float radians = (float) Math.atan2(newZ - position.getZ(), newX - position.getX());
float yaw = (float) (radians * (180.0 / Math.PI)) - 90;
float pitch = position.getPitch(); // TODO
2020-02-11 16:48:06 +01:00
short deltaX = (short) ((newX * 32 - position.getX() * 32) * 128);
short deltaY = (short) ((newY * 32 - position.getY() * 32) * 128);
short deltaZ = (short) ((newZ * 32 - position.getZ() * 32) * 128);
2019-08-30 01:17:46 +02:00
if (updateView) {
2020-02-11 16:48:06 +01:00
EntityPositionAndRotationPacket entityPositionAndRotationPacket = new EntityPositionAndRotationPacket();
entityPositionAndRotationPacket.entityId = getEntityId();
entityPositionAndRotationPacket.deltaX = deltaX;
entityPositionAndRotationPacket.deltaY = deltaY;
entityPositionAndRotationPacket.deltaZ = deltaZ;
entityPositionAndRotationPacket.yaw = yaw;
entityPositionAndRotationPacket.pitch = pitch;
entityPositionAndRotationPacket.onGround = isOnGround();
sendPacketToViewers(entityPositionAndRotationPacket);
2019-08-30 01:17:46 +02:00
} else {
2020-02-11 16:48:06 +01:00
EntityPositionPacket entityPositionPacket = new EntityPositionPacket();
entityPositionPacket.entityId = getEntityId();
entityPositionPacket.deltaX = deltaX;
entityPositionPacket.deltaY = deltaY;
entityPositionPacket.deltaZ = deltaZ;
entityPositionPacket.onGround = isOnGround();
sendPacketToViewers(entityPositionPacket);
2019-08-30 01:17:46 +02:00
}
2019-08-29 02:15:52 +02:00
if (lastYaw != yaw) {
EntityHeadLookPacket entityHeadLookPacket = new EntityHeadLookPacket();
entityHeadLookPacket.entityId = getEntityId();
entityHeadLookPacket.yaw = yaw;
sendPacketToViewers(entityHeadLookPacket);
2019-08-30 01:17:46 +02:00
refreshView(yaw, pitch);
2019-08-29 02:15:52 +02:00
}
2019-08-11 13:57:23 +02:00
refreshPosition(newX, newY, newZ);
2019-08-29 02:15:52 +02:00
}
2019-08-24 20:34:01 +02:00
@Override
2019-08-21 16:50:52 +02:00
public void kill() {
2020-04-05 10:15:21 +02:00
super.kill();
// Needed for proper death animation (wait for it to finish before destroying the entity)
scheduleRemove(1000);
2019-08-11 13:57:23 +02:00
}
2019-08-19 17:04:19 +02:00
@Override
2019-08-10 08:44:35 +02:00
public void addViewer(Player player) {
super.addViewer(player);
2019-08-10 08:44:35 +02:00
PlayerConnection playerConnection = player.getPlayerConnection();
EntityPacket entityPacket = new EntityPacket();
entityPacket.entityId = getEntityId();
2020-02-09 15:34:09 +01:00
2020-03-29 20:58:30 +02:00
SpawnLivingEntityPacket spawnLivingEntityPacket = new SpawnLivingEntityPacket();
spawnLivingEntityPacket.entityId = getEntityId();
spawnLivingEntityPacket.entityUuid = getUuid();
spawnLivingEntityPacket.entityType = getEntityType();
spawnLivingEntityPacket.position = getPosition();
spawnLivingEntityPacket.headPitch = 0;
2020-02-09 15:34:09 +01:00
2019-08-10 08:44:35 +02:00
playerConnection.sendPacket(entityPacket);
2020-03-29 20:58:30 +02:00
playerConnection.sendPacket(spawnLivingEntityPacket);
2020-02-11 16:48:06 +01:00
playerConnection.sendPacket(getMetadataPacket());
2019-08-10 08:44:35 +02:00
}
2020-04-09 14:25:42 +02:00
public void jump(float height) {
// FIXME magic value
2020-04-22 02:42:58 +02:00
Vector velocity = new Vector(0, height * 8, 0);
setVelocity(velocity, 350);
2020-04-09 14:25:42 +02:00
}
public void moveTo(Position position) {
pathFinder.getPath(position, blockPositions -> {
this.blockPositions = blockPositions;
setNextPathPosition();
});
}
public void moveTowards(Position direction, float speed) {
float radians = (float) Math.atan2(direction.getZ() - position.getZ(), direction.getX() - position.getX());
float speedX = (float) (Math.cos(radians) * speed);
float speedZ = (float) (Math.sin(radians) * speed);
move(speedX, 0, speedZ, true);
}
private void setNextPathPosition() {
BlockPosition blockPosition = blockPositions.pollFirst();
if (blockPosition == null) {
this.blockPositions = null;
this.targetPosition = null;
return;
}
this.targetPosition = blockPosition.toPosition().subtract(0.5f, 0, 0.5f);
2020-04-22 02:42:58 +02:00
// FIXME: jump support
//if(blockPosition.getY() > getPosition().getY())
// jump(1);
2020-04-09 14:25:42 +02:00
}
2019-08-10 08:44:35 +02:00
}