TechbloxModdingAPI/GamecraftModdingAPI/Player.cs

242 lines
6.5 KiB
C#
Raw Normal View History

2020-05-12 00:30:16 +00:00
using System;
using Unity.Mathematics;
using GamecraftModdingAPI.Players;
using GamecraftModdingAPI.Utility;
namespace GamecraftModdingAPI
{
2020-05-12 21:08:30 +00:00
/// <summary>
/// An in-game player character. Any Leo you see is a player.
/// </summary>
2020-05-12 00:30:16 +00:00
public class Player
{
// static functionality
private static PlayerEngine playerEngine = new PlayerEngine();
2020-05-12 21:08:30 +00:00
/// <summary>
/// Checks if the specified player exists.
/// </summary>
/// <returns>Whether the player exists.</returns>
/// <param name="player">Player type.</param>
2020-05-12 00:30:16 +00:00
public static bool Exists(PlayerType player)
{
switch (player)
{
case PlayerType.Remote:
return playerEngine.GetRemotePlayer() != uint.MaxValue;
case PlayerType.Local:
return playerEngine.GetLocalPlayer() != uint.MaxValue;
}
return false;
}
2020-05-12 21:08:30 +00:00
/// <summary>
/// Checks if the specified player exists.
/// </summary>
/// <returns>Whether the player exists.</returns>
/// <param name="player">The player's unique identifier.</param>
2020-05-12 00:30:16 +00:00
public static bool Exists(uint player)
{
return playerEngine.ExistsById(player);
}
2020-05-12 21:08:30 +00:00
/// <summary>
/// Initializes a new instance of the <see cref="T:GamecraftModdingAPI.Player"/> class.
/// </summary>
/// <param name="id">The player's unique identifier.</param>
2020-05-12 00:30:16 +00:00
public Player(uint id)
{
this.Id = id;
if (!Exists(id))
{
throw new InvalidOperationException($"No player with id {id} exists");
}
this.Type = playerEngine.GetLocalPlayer() == id ? PlayerType.Local : PlayerType.Remote;
}
2020-05-12 21:08:30 +00:00
/// <summary>
/// Initializes a new instance of the <see cref="T:GamecraftModdingAPI.Player"/> class.
/// </summary>
/// <param name="player">The player type. Chooses the first available player matching the criteria.</param>
2020-05-12 00:30:16 +00:00
public Player(PlayerType player)
{
uint localId = playerEngine.GetLocalPlayer();
switch (player)
{
case PlayerType.Local:
this.Id = playerEngine.GetLocalPlayer();
break;
case PlayerType.Remote:
this.Id = playerEngine.GetRemotePlayer();
break;
}
if (this.Id == uint.MaxValue)
{
throw new InvalidOperationException($"No player of {player} type exists");
}
this.Type = player;
}
// object fields & properties
2020-05-12 21:08:30 +00:00
/// <summary>
/// The player's type.
/// The player type is always relative to the current client, not the game host.
/// </summary>
/// <value>The enumerated player type.</value>
2020-05-12 00:30:16 +00:00
public PlayerType Type { get; }
2020-05-12 21:08:30 +00:00
/// <summary>
/// The player's unique identifier.
/// </summary>
/// <value>The identifier.</value>
2020-05-12 00:30:16 +00:00
public uint Id { get; private set; }
2020-05-12 21:08:30 +00:00
/// <summary>
/// The player's current position.
/// </summary>
/// <value>The position.</value>
2020-05-12 00:30:16 +00:00
public float3 Position
{
get
{
return playerEngine.GetLocation(Id);
}
set
{
playerEngine.SetLocation(Id, value, false);
}
}
2020-05-12 21:08:30 +00:00
/// <summary>
/// The player's current rotation.
/// </summary>
/// <value>The rotation.</value>
2020-05-12 00:30:16 +00:00
public quaternion Rotation
{
get
{
return playerEngine.GetRotation(Id);
}
set
{
playerEngine.SetRotation(Id, value);
}
}
2020-05-12 21:08:30 +00:00
/// <summary>
/// The player's current velocity.
/// </summary>
/// <value>The velocity.</value>
2020-05-12 00:30:16 +00:00
public float3 Velocity
{
get
{
return playerEngine.GetLinearVelocity(Id);
}
set
{
playerEngine.SetLinearVelocity(Id, value);
}
}
2020-05-12 21:08:30 +00:00
/// <summary>
/// The player's current angular velocity.
/// </summary>
/// <value>The angular velocity.</value>
2020-05-12 00:30:16 +00:00
public float3 AngularVelocity
{
get
{
return playerEngine.GetAngularVelocity(Id);
}
set
{
playerEngine.SetAngularVelocity(Id, value);
}
}
2020-05-12 21:08:30 +00:00
/// <summary>
/// The player's mass.
/// </summary>
/// <value>The mass.</value>
2020-05-12 00:30:16 +00:00
public float Mass
{
get
{
return 1f / playerEngine.GetMass(Id).InverseMass;
}
2020-05-12 21:08:30 +00:00
// FIXME: Setting mass doesn't do anything
/*set
2020-05-12 00:30:16 +00:00
{
playerEngine.SetInverseMass(Id, 1f / value);
2020-05-12 21:08:30 +00:00
}*/
2020-05-12 00:30:16 +00:00
}
private float _ping = -1f;
2020-05-12 21:08:30 +00:00
/// <summary>
/// The player's latest network ping time.
/// </summary>
/// <value>The ping (s).</value>
2020-05-12 00:30:16 +00:00
public float Ping
{
get
{
float? temp = playerEngine.GetLastPingTime(Id, Type);
if (temp.HasValue)
{
_ping = temp.Value;
}
return _ping;
}
}
// object methods
2020-05-12 21:08:30 +00:00
/// <summary>
/// Teleport the player to the specified coordinates.
/// </summary>
/// <param name="x">The x coordinate.</param>
/// <param name="y">The y coordinate.</param>
/// <param name="z">The z coordinate.</param>
/// <param name="relative">If set to <c>true</c> teleport relative to the player's current position.</param>
/// <param name="exitSeat">If set to <c>true</c> exit any seat the player is in.</param>
2020-05-12 00:30:16 +00:00
public void Teleport(float x, float y, float z, bool relative = true, bool exitSeat = true)
{
float3 location = new float3(x, y, z);
if (relative)
{
location += playerEngine.GetLocation(Id);
}
playerEngine.SetLocation(Id, location, exitSeat: exitSeat);
}
/// <summary>
/// Returns the block the player is currently looking at.
/// </summary>
/// <param name="playerId">The player's ID</param>
/// <param name="entitiesDB">The entities DB</param>
/// <param name="maxDistance">The maximum distance from the player (default is the player's building reach)</param>
/// <returns>The block's EGID or null if not found</returns>
public Block GetBlockLookedAt(float maxDistance = -1f)
{
return playerEngine.GetBlockLookedAt(Id, maxDistance);
}
2020-05-12 00:30:16 +00:00
// internal methods
public static void Init()
{
Utility.GameEngineManager.AddGameEngine(playerEngine);
}
}
}