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.

Player.cs 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. using System;
  2. using Unity.Mathematics;
  3. using GamecraftModdingAPI.Players;
  4. using GamecraftModdingAPI.Utility;
  5. namespace GamecraftModdingAPI
  6. {
  7. /// <summary>
  8. /// An in-game player character. Any Leo you see is a player.
  9. /// </summary>
  10. public class Player
  11. {
  12. // static functionality
  13. private static PlayerEngine playerEngine = new PlayerEngine();
  14. /// <summary>
  15. /// Checks if the specified player exists.
  16. /// </summary>
  17. /// <returns>Whether the player exists.</returns>
  18. /// <param name="player">Player type.</param>
  19. public static bool Exists(PlayerType player)
  20. {
  21. switch (player)
  22. {
  23. case PlayerType.Remote:
  24. return playerEngine.GetRemotePlayer() != uint.MaxValue;
  25. case PlayerType.Local:
  26. return playerEngine.GetLocalPlayer() != uint.MaxValue;
  27. }
  28. return false;
  29. }
  30. /// <summary>
  31. /// Checks if the specified player exists.
  32. /// </summary>
  33. /// <returns>Whether the player exists.</returns>
  34. /// <param name="player">The player's unique identifier.</param>
  35. public static bool Exists(uint player)
  36. {
  37. return playerEngine.ExistsById(player);
  38. }
  39. /// <summary>
  40. /// Initializes a new instance of the <see cref="T:GamecraftModdingAPI.Player"/> class.
  41. /// </summary>
  42. /// <param name="id">The player's unique identifier.</param>
  43. public Player(uint id)
  44. {
  45. this.Id = id;
  46. if (!Exists(id))
  47. {
  48. throw new InvalidOperationException($"No player with id {id} exists");
  49. }
  50. this.Type = playerEngine.GetLocalPlayer() == id ? PlayerType.Local : PlayerType.Remote;
  51. }
  52. /// <summary>
  53. /// Initializes a new instance of the <see cref="T:GamecraftModdingAPI.Player"/> class.
  54. /// </summary>
  55. /// <param name="player">The player type. Chooses the first available player matching the criteria.</param>
  56. public Player(PlayerType player)
  57. {
  58. uint localId = playerEngine.GetLocalPlayer();
  59. switch (player)
  60. {
  61. case PlayerType.Local:
  62. this.Id = playerEngine.GetLocalPlayer();
  63. break;
  64. case PlayerType.Remote:
  65. this.Id = playerEngine.GetRemotePlayer();
  66. break;
  67. }
  68. if (this.Id == uint.MaxValue)
  69. {
  70. throw new InvalidOperationException($"No player of {player} type exists");
  71. }
  72. this.Type = player;
  73. }
  74. // object fields & properties
  75. /// <summary>
  76. /// The player's type.
  77. /// The player type is always relative to the current client, not the game host.
  78. /// </summary>
  79. /// <value>The enumerated player type.</value>
  80. public PlayerType Type { get; }
  81. /// <summary>
  82. /// The player's unique identifier.
  83. /// </summary>
  84. /// <value>The identifier.</value>
  85. public uint Id { get; private set; }
  86. /// <summary>
  87. /// The player's current position.
  88. /// </summary>
  89. /// <value>The position.</value>
  90. public float3 Position
  91. {
  92. get
  93. {
  94. return playerEngine.GetLocation(Id);
  95. }
  96. set
  97. {
  98. playerEngine.SetLocation(Id, value, false);
  99. }
  100. }
  101. /// <summary>
  102. /// The player's current rotation.
  103. /// </summary>
  104. /// <value>The rotation.</value>
  105. public quaternion Rotation
  106. {
  107. get
  108. {
  109. return playerEngine.GetRotation(Id);
  110. }
  111. set
  112. {
  113. playerEngine.SetRotation(Id, value);
  114. }
  115. }
  116. /// <summary>
  117. /// The player's current velocity.
  118. /// </summary>
  119. /// <value>The velocity.</value>
  120. public float3 Velocity
  121. {
  122. get
  123. {
  124. return playerEngine.GetLinearVelocity(Id);
  125. }
  126. set
  127. {
  128. playerEngine.SetLinearVelocity(Id, value);
  129. }
  130. }
  131. /// <summary>
  132. /// The player's current angular velocity.
  133. /// </summary>
  134. /// <value>The angular velocity.</value>
  135. public float3 AngularVelocity
  136. {
  137. get
  138. {
  139. return playerEngine.GetAngularVelocity(Id);
  140. }
  141. set
  142. {
  143. playerEngine.SetAngularVelocity(Id, value);
  144. }
  145. }
  146. /// <summary>
  147. /// The player's mass.
  148. /// </summary>
  149. /// <value>The mass.</value>
  150. public float Mass
  151. {
  152. get
  153. {
  154. return 1f / playerEngine.GetMass(Id).InverseMass;
  155. }
  156. // FIXME: Setting mass doesn't do anything
  157. /*set
  158. {
  159. playerEngine.SetInverseMass(Id, 1f / value);
  160. }*/
  161. }
  162. private float _ping = -1f;
  163. /// <summary>
  164. /// The player's latest network ping time.
  165. /// </summary>
  166. /// <value>The ping (s).</value>
  167. public float Ping
  168. {
  169. get
  170. {
  171. float? temp = playerEngine.GetLastPingTime(Id, Type);
  172. if (temp.HasValue)
  173. {
  174. _ping = temp.Value;
  175. }
  176. return _ping;
  177. }
  178. }
  179. // object methods
  180. /// <summary>
  181. /// Teleport the player to the specified coordinates.
  182. /// </summary>
  183. /// <param name="x">The x coordinate.</param>
  184. /// <param name="y">The y coordinate.</param>
  185. /// <param name="z">The z coordinate.</param>
  186. /// <param name="relative">If set to <c>true</c> teleport relative to the player's current position.</param>
  187. /// <param name="exitSeat">If set to <c>true</c> exit any seat the player is in.</param>
  188. public void Teleport(float x, float y, float z, bool relative = true, bool exitSeat = true)
  189. {
  190. float3 location = new float3(x, y, z);
  191. if (relative)
  192. {
  193. location += playerEngine.GetLocation(Id);
  194. }
  195. playerEngine.SetLocation(Id, location, exitSeat: exitSeat);
  196. }
  197. // internal methods
  198. public static void Init()
  199. {
  200. Utility.GameEngineManager.AddGameEngine(playerEngine);
  201. }
  202. }
  203. }