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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. using System;
  2. using Unity.Mathematics;
  3. using RobocraftX.Common;
  4. using GamecraftModdingAPI.Players;
  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 PlayerNotFoundException($"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 PlayerNotFoundException($"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. /// <summary>
  198. /// Returns the block the player is currently looking at.
  199. /// </summary>
  200. /// <param name="maxDistance">The maximum distance from the player (default is the player's building reach)</param>
  201. /// <returns>The block or null if not found</returns>
  202. public Block GetBlockLookedAt(float maxDistance = -1f)
  203. {
  204. var egid = playerEngine.GetThingLookedAt(Id, maxDistance);
  205. return egid.HasValue && egid.Value.groupID == CommonExclusiveGroups.OWNED_BLOCKS_GROUP
  206. ? new Block(egid.Value)
  207. : null;
  208. }
  209. /// <summary>
  210. /// Returns the rigid body the player is currently looking at during simulation.
  211. /// </summary>
  212. /// <param name="maxDistance">The maximum distance from the player (default is the player's building reach)</param>
  213. /// <returns>The block or null if not found</returns>
  214. public SimBody GetSimBodyLookedAt(float maxDistance = -1f)
  215. {
  216. var egid = playerEngine.GetThingLookedAt(Id, maxDistance);
  217. return egid.HasValue && egid.Value.groupID == CommonExclusiveGroups.SIMULATION_BODIES_GROUP
  218. ? new SimBody(egid.Value)
  219. : null;
  220. }
  221. // internal methods
  222. public static void Init()
  223. {
  224. Utility.GameEngineManager.AddGameEngine(playerEngine);
  225. }
  226. }
  227. }