A stable modding interface between Techblox and mods https://mod.exmods.org/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

167 lines
3.3KB

  1. using System;
  2. using Unity.Mathematics;
  3. using GamecraftModdingAPI.Players;
  4. using GamecraftModdingAPI.Utility;
  5. namespace GamecraftModdingAPI
  6. {
  7. public class Player
  8. {
  9. // static functionality
  10. private static PlayerEngine playerEngine = new PlayerEngine();
  11. public static bool Exists(PlayerType player)
  12. {
  13. switch (player)
  14. {
  15. case PlayerType.Remote:
  16. return playerEngine.GetRemotePlayer() != uint.MaxValue;
  17. case PlayerType.Local:
  18. return playerEngine.GetLocalPlayer() != uint.MaxValue;
  19. }
  20. return false;
  21. }
  22. public static bool Exists(uint player)
  23. {
  24. return playerEngine.ExistsById(player);
  25. }
  26. public Player(uint id)
  27. {
  28. this.Id = id;
  29. if (!Exists(id))
  30. {
  31. throw new InvalidOperationException($"No player with id {id} exists");
  32. }
  33. this.Type = playerEngine.GetLocalPlayer() == id ? PlayerType.Local : PlayerType.Remote;
  34. }
  35. public Player(PlayerType player)
  36. {
  37. uint localId = playerEngine.GetLocalPlayer();
  38. switch (player)
  39. {
  40. case PlayerType.Local:
  41. this.Id = playerEngine.GetLocalPlayer();
  42. break;
  43. case PlayerType.Remote:
  44. this.Id = playerEngine.GetRemotePlayer();
  45. break;
  46. }
  47. if (this.Id == uint.MaxValue)
  48. {
  49. throw new InvalidOperationException($"No player of {player} type exists");
  50. }
  51. this.Type = player;
  52. }
  53. // object fields & properties
  54. public PlayerType Type { get; }
  55. public uint Id { get; private set; }
  56. public float3 Position
  57. {
  58. get
  59. {
  60. return playerEngine.GetLocation(Id);
  61. }
  62. set
  63. {
  64. playerEngine.SetLocation(Id, value, false);
  65. }
  66. }
  67. public quaternion Rotation
  68. {
  69. get
  70. {
  71. return playerEngine.GetRotation(Id);
  72. }
  73. set
  74. {
  75. playerEngine.SetRotation(Id, value);
  76. }
  77. }
  78. public float3 Velocity
  79. {
  80. get
  81. {
  82. return playerEngine.GetLinearVelocity(Id);
  83. }
  84. set
  85. {
  86. playerEngine.SetLinearVelocity(Id, value);
  87. }
  88. }
  89. public float3 AngularVelocity
  90. {
  91. get
  92. {
  93. return playerEngine.GetAngularVelocity(Id);
  94. }
  95. set
  96. {
  97. playerEngine.SetAngularVelocity(Id, value);
  98. }
  99. }
  100. public float Mass
  101. {
  102. get
  103. {
  104. return 1f / playerEngine.GetMass(Id).InverseMass;
  105. }
  106. set
  107. {
  108. playerEngine.SetInverseMass(Id, 1f / value);
  109. }
  110. }
  111. private float _ping = -1f;
  112. public float Ping
  113. {
  114. get
  115. {
  116. float? temp = playerEngine.GetLastPingTime(Id, Type);
  117. if (temp.HasValue)
  118. {
  119. _ping = temp.Value;
  120. }
  121. return _ping;
  122. }
  123. }
  124. // object methods
  125. public void Teleport(float x, float y, float z, bool relative = true, bool exitSeat = true)
  126. {
  127. float3 location = new float3(x, y, z);
  128. if (relative)
  129. {
  130. location += playerEngine.GetLocation(Id);
  131. }
  132. playerEngine.SetLocation(Id, location, exitSeat: exitSeat);
  133. }
  134. // internal methods
  135. public static void Init()
  136. {
  137. Utility.GameEngineManager.AddGameEngine(playerEngine);
  138. }
  139. }
  140. }