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 6.5KB

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