MinecraftConsoles/Minecraft.Server.FourKit/Entity/Entity.cs

128 lines
4.3 KiB
C#

namespace Minecraft.Server.FourKit.Entity;
using Minecraft.Server.FourKit.Util;
/// <summary>
/// Represents a base entity in the world
/// </summary>
public abstract class Entity
{
private Location _location = new();
private Guid _uniqueId = Guid.NewGuid();
private float _fallDistance;
private int _dimensionId;
private int _entityId;
private EntityType _entityType = EntityType.UNKNOWN;
private bool _onGround;
private double _velocityX, _velocityY, _velocityZ;
/// <summary>
/// Gets the entity's current position.
/// </summary>
/// <returns>a new copy of <see cref="Location"/> containing the position of this entity</returns>
public Location getLocation() => _location;
/// <summary>
/// Returns a unique id for this entity
/// </summary>
/// <returns>Entity id</returns>
public virtual int getEntityId() => _entityId;
/// <summary>
/// Get the type of the entity.
/// </summary>
/// <returns>The <see cref="EntityType"/> of this entity.</returns>
public new virtual EntityType getType() => _entityType;
public new virtual EntityType GetType() => _entityType;
/// <summary>
/// Returns a unique and persistent id for this entity. Note that this is not the standard UUID for players.
/// </summary>
/// <returns>A <see cref="Guid"/> unique to this entity.</returns>
public Guid getUniqueId() => _uniqueId;
/// <summary>
/// Teleports this entity to the given location.
/// This calls into the native server to perform the actual teleport.
/// </summary>
/// <param name="location">The destination location.</param>
/// <returns><c>true</c> if the teleport was successful.</returns>
public virtual bool teleport(Location location)
{
NativeBridge.TeleportEntity?.Invoke(getEntityId(), _dimensionId, location.getX(), location.getY(), location.getZ());
SetLocation(location);
return true;
}
/// <summary>
/// Sets the fall distance for this entity.
/// </summary>
/// <param name="distance">The fall distance value.</param>
public void setFallDistance(float distance)
{
_fallDistance = distance;
NativeBridge.SetFallDistance?.Invoke(getEntityId(), distance);
}
/// <summary>
/// Returns the distance this entity has fallen.
/// </summary>
/// <returns>The current fall distance.</returns>
public float getFallDistance() => _fallDistance;
/// <summary>
/// Gets the current world this entity resides in.
/// </summary>
/// <returns>World containing this entity.</returns>
public World getWorld() => FourKit.getWorld(_dimensionId);
/// <summary>
/// Returns true if the entity is supported by a block. This value is a
/// state updated by the server and is not recalculated unless the entity moves.
/// </summary>
/// <returns>True if entity is on ground.</returns>
public bool isOnGround() => _onGround;
/// <summary>
/// Gets this entity's current velocity.
/// </summary>
/// <returns>Current travelling velocity of this entity.</returns>
public Vector getVelocity() => new Vector(_velocityX, _velocityY, _velocityZ);
/// <summary>
/// Sets this entity's velocity.
/// </summary>
/// <param name="velocity">New velocity to travel with.</param>
public void setVelocity(Vector velocity)
{
_velocityX = velocity.getX();
_velocityY = velocity.getY();
_velocityZ = velocity.getZ();
NativeBridge.SetVelocity?.Invoke(getEntityId(), velocity.getX(), velocity.getY(), velocity.getZ());
}
// INTERNAL
internal void SetLocation(Location location)
{
_location = location;
}
internal void SetFallDistanceInternal(float distance) => _fallDistance = distance;
internal void SetUniqueId(Guid id)
{
_uniqueId = id;
}
internal void SetDimensionInternal(int dimensionId) => _dimensionId = dimensionId;
internal void SetEntityIdInternal(int entityId) => _entityId = entityId;
internal void SetEntityTypeInternal(EntityType entityType) => _entityType = entityType;
internal void SetOnGroundInternal(bool onGround) => _onGround = onGround;
internal void SetVelocityInternal(double x, double y, double z)
{
_velocityX = x;
_velocityY = y;
_velocityZ = z;
}
}