|
- using System;
-
- using Unity.Mathematics;
-
- using GamecraftModdingAPI.Players;
- using GamecraftModdingAPI.Utility;
-
- namespace GamecraftModdingAPI
- {
- public class Player
- {
- // static functionality
- private static PlayerEngine playerEngine = new PlayerEngine();
-
- 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;
- }
-
- public static bool Exists(uint player)
- {
- return playerEngine.ExistsById(player);
- }
-
- 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;
- }
-
- 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
-
- public PlayerType Type { get; }
-
- public uint Id { get; private set; }
-
- public float3 Position
- {
- get
- {
- return playerEngine.GetLocation(Id);
- }
-
- set
- {
- playerEngine.SetLocation(Id, value, false);
- }
- }
-
- public quaternion Rotation
- {
- get
- {
- return playerEngine.GetRotation(Id);
- }
-
- set
- {
- playerEngine.SetRotation(Id, value);
- }
- }
-
- public float3 Velocity
- {
- get
- {
- return playerEngine.GetLinearVelocity(Id);
- }
-
- set
- {
- playerEngine.SetLinearVelocity(Id, value);
- }
- }
-
- public float3 AngularVelocity
- {
- get
- {
- return playerEngine.GetAngularVelocity(Id);
- }
-
- set
- {
- playerEngine.SetAngularVelocity(Id, value);
- }
- }
-
- public float Mass
- {
- get
- {
- return 1f / playerEngine.GetMass(Id).InverseMass;
- }
-
- set
- {
- playerEngine.SetInverseMass(Id, 1f / value);
- }
- }
-
- private float _ping = -1f;
-
- public float Ping
- {
- get
- {
- float? temp = playerEngine.GetLastPingTime(Id, Type);
- if (temp.HasValue)
- {
- _ping = temp.Value;
- }
- return _ping;
- }
- }
-
- // object methods
-
- 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);
- }
-
- // internal methods
-
- public static void Init()
- {
- Utility.GameEngineManager.AddGameEngine(playerEngine);
- }
- }
- }
|