2019-08-10 08:44:35 +02:00
|
|
|
package fr.themode.minestom.entity;
|
|
|
|
|
2019-08-25 20:03:43 +02:00
|
|
|
import fr.themode.minestom.event.DeathEvent;
|
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;
|
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;
|
2019-08-10 08:44:35 +02:00
|
|
|
|
2019-08-29 02:15:52 +02:00
|
|
|
// TODO pathfinding
|
2019-08-20 17:41:07 +02:00
|
|
|
public abstract class EntityCreature extends LivingEntity {
|
2019-08-10 08:44:35 +02:00
|
|
|
|
2020-03-29 20:58:30 +02:00
|
|
|
|
|
|
|
public EntityCreature(EntityType entityType, Position defaultPosition) {
|
|
|
|
super(entityType.getId(), defaultPosition);
|
|
|
|
}
|
|
|
|
|
2019-08-27 20:49:11 +02:00
|
|
|
public EntityCreature(EntityType entityType) {
|
2020-03-29 20:58:30 +02:00
|
|
|
this(entityType, new Position());
|
2019-08-10 08:44:35 +02:00
|
|
|
}
|
|
|
|
|
2019-08-24 20:34:01 +02:00
|
|
|
@Override
|
|
|
|
public void update() {
|
|
|
|
super.update();
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2019-08-30 01:17:46 +02:00
|
|
|
move(speedX, 0, speedZ, true);
|
2019-08-11 13:57:23 +02:00
|
|
|
}
|
|
|
|
|
2019-08-24 20:34:01 +02:00
|
|
|
@Override
|
2019-08-21 16:50:52 +02:00
|
|
|
public void kill() {
|
2020-03-29 20:58:30 +02:00
|
|
|
this.isDead = true; // So the entity isn't killed over and over again
|
|
|
|
triggerStatus((byte) 3); // Start death animation status
|
|
|
|
scheduleRemove(1000); // Needed for proper death animation (wait for it to finish before destroying the entity)
|
2019-08-25 20:03:43 +02:00
|
|
|
DeathEvent deathEvent = new DeathEvent();
|
|
|
|
callEvent(DeathEvent.class, deathEvent);
|
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) {
|
2019-08-20 17:41:07 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|