|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166 |
- 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);
- }
- }
- }
|