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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. using System;
  2. using Unity.Mathematics;
  3. using RobocraftX.Common;
  4. using RobocraftX.Common.Players;
  5. using Svelto.ECS;
  6. using GamecraftModdingAPI.Players;
  7. using GamecraftModdingAPI.Blocks;
  8. namespace GamecraftModdingAPI
  9. {
  10. /// <summary>
  11. /// An in-game player character. Any Leo you see is a player.
  12. /// </summary>
  13. public class Player : IEquatable<Player>, IEquatable<EGID>
  14. {
  15. // static functionality
  16. private static PlayerEngine playerEngine = new PlayerEngine();
  17. /// <summary>
  18. /// Checks if the specified player exists.
  19. /// </summary>
  20. /// <returns>Whether the player exists.</returns>
  21. /// <param name="player">Player type.</param>
  22. public static bool Exists(PlayerType player)
  23. {
  24. switch (player)
  25. {
  26. case PlayerType.Remote:
  27. return playerEngine.GetRemotePlayer() != uint.MaxValue;
  28. case PlayerType.Local:
  29. return playerEngine.GetLocalPlayer() != uint.MaxValue;
  30. }
  31. return false;
  32. }
  33. /// <summary>
  34. /// Checks if the specified player exists.
  35. /// </summary>
  36. /// <returns>Whether the player exists.</returns>
  37. /// <param name="player">The player's unique identifier.</param>
  38. public static bool Exists(uint player)
  39. {
  40. return playerEngine.ExistsById(player);
  41. }
  42. /// <summary>
  43. /// The amount of Players in the current game.
  44. /// </summary>
  45. /// <returns>The count.</returns>
  46. public static uint Count()
  47. {
  48. return (uint) playerEngine.GetAllPlayerCount();
  49. }
  50. /// <summary>
  51. /// Initializes a new instance of the <see cref="T:GamecraftModdingAPI.Player"/> class.
  52. /// </summary>
  53. /// <param name="id">The player's unique identifier.</param>
  54. public Player(uint id)
  55. {
  56. this.Id = id;
  57. if (!Exists(id))
  58. {
  59. throw new PlayerNotFoundException($"No player with id {id} exists");
  60. }
  61. this.Type = playerEngine.GetLocalPlayer() == id ? PlayerType.Local : PlayerType.Remote;
  62. }
  63. /// <summary>
  64. /// Initializes a new instance of the <see cref="T:GamecraftModdingAPI.Player"/> class.
  65. /// </summary>
  66. /// <param name="player">The player type. Chooses the first available player matching the criteria.</param>
  67. public Player(PlayerType player)
  68. {
  69. switch (player)
  70. {
  71. case PlayerType.Local:
  72. this.Id = playerEngine.GetLocalPlayer();
  73. break;
  74. case PlayerType.Remote:
  75. this.Id = playerEngine.GetRemotePlayer();
  76. break;
  77. }
  78. if (this.Id == uint.MaxValue)
  79. {
  80. throw new PlayerNotFoundException($"No player of {player} type exists");
  81. }
  82. this.Type = player;
  83. }
  84. // object fields & properties
  85. /// <summary>
  86. /// The player's type.
  87. /// The player type is always relative to the current client, not the game host.
  88. /// </summary>
  89. /// <value>The enumerated player type.</value>
  90. public PlayerType Type { get; }
  91. /// <summary>
  92. /// The player's unique identifier.
  93. /// </summary>
  94. /// <value>The identifier.</value>
  95. public uint Id { get; }
  96. /// <summary>
  97. /// The player's current position.
  98. /// </summary>
  99. /// <value>The position.</value>
  100. public float3 Position
  101. {
  102. get
  103. {
  104. return playerEngine.GetLocation(Id);
  105. }
  106. set
  107. {
  108. playerEngine.SetLocation(Id, value, false);
  109. }
  110. }
  111. /// <summary>
  112. /// The player's current rotation.
  113. /// </summary>
  114. /// <value>The rotation.</value>
  115. public float3 Rotation
  116. {
  117. get
  118. {
  119. return playerEngine.GetRotation(Id);
  120. }
  121. set
  122. {
  123. playerEngine.SetRotation(Id, value);
  124. }
  125. }
  126. /// <summary>
  127. /// The player's current velocity.
  128. /// </summary>
  129. /// <value>The velocity.</value>
  130. public float3 Velocity
  131. {
  132. get
  133. {
  134. return playerEngine.GetLinearVelocity(Id);
  135. }
  136. set
  137. {
  138. playerEngine.SetLinearVelocity(Id, value);
  139. }
  140. }
  141. /// <summary>
  142. /// The player's current angular velocity.
  143. /// </summary>
  144. /// <value>The angular velocity.</value>
  145. public float3 AngularVelocity
  146. {
  147. get
  148. {
  149. return playerEngine.GetAngularVelocity(Id);
  150. }
  151. set
  152. {
  153. playerEngine.SetAngularVelocity(Id, value);
  154. }
  155. }
  156. /// <summary>
  157. /// The player's mass.
  158. /// </summary>
  159. /// <value>The mass.</value>
  160. public float Mass
  161. {
  162. get
  163. {
  164. return 1f / playerEngine.GetMass(Id).InverseMass;
  165. }
  166. // FIXME: Setting mass doesn't do anything
  167. /*set
  168. {
  169. playerEngine.SetInverseMass(Id, 1f / value);
  170. }*/
  171. }
  172. private float _ping = -1f;
  173. /// <summary>
  174. /// The player's latest network ping time.
  175. /// </summary>
  176. /// <value>The ping (s).</value>
  177. public float Ping
  178. {
  179. get
  180. {
  181. float? temp = playerEngine.GetLastPingTime(Id, Type);
  182. if (temp.HasValue)
  183. {
  184. _ping = temp.Value;
  185. }
  186. return _ping;
  187. }
  188. }
  189. /// <summary>
  190. /// The player's initial health when entering Simulation (aka Time Running) mode.
  191. /// </summary>
  192. /// <value>The initial health.</value>
  193. public float InitialHealth
  194. {
  195. get => playerEngine.GetInitialHealth(Id);
  196. set
  197. {
  198. playerEngine.SetInitialHealth(Id, value);
  199. }
  200. }
  201. /// <summary>
  202. /// The player's current health in Simulation (aka Time Running) mode.
  203. /// </summary>
  204. /// <value>The current health.</value>
  205. public float CurrentHealth
  206. {
  207. get => playerEngine.GetCurrentHealth(Id);
  208. set
  209. {
  210. playerEngine.DamagePlayer(Id, CurrentHealth - value);
  211. }
  212. }
  213. /// <summary>
  214. /// Whether this <see cref="T:GamecraftModdingAPI.Player"/> is damageable.
  215. /// </summary>
  216. /// <value><c>true</c> if damageable; otherwise, <c>false</c>.</value>
  217. public bool Damageable
  218. {
  219. get => playerEngine.GetDamageable(Id);
  220. set
  221. {
  222. playerEngine.SetDamageable(Id, value);
  223. }
  224. }
  225. /// <summary>
  226. /// The player's lives when initially entering Simulation (aka Time Running) mode.
  227. /// </summary>
  228. /// <value>The initial lives.</value>
  229. public uint InitialLives
  230. {
  231. get => playerEngine.GetInitialLives(Id);
  232. set => playerEngine.SetInitialLives(Id, value);
  233. }
  234. /// <summary>
  235. /// The player's current lives in Simulation (aka Time Running) mode.
  236. /// </summary>
  237. /// <value>The current lives.</value>
  238. public uint CurrentLives
  239. {
  240. get => playerEngine.GetCurrentLives(Id);
  241. set => playerEngine.SetCurrentLives(Id, value);
  242. }
  243. /// <summary>
  244. /// Whether the Game Over screen is displayed for the player.
  245. /// </summary>
  246. /// <value><c>true</c> if game over; otherwise, <c>false</c>.</value>
  247. public bool GameOver
  248. {
  249. get => playerEngine.GetGameOverScreen(Id);
  250. }
  251. /// <summary>
  252. /// Whether the player is dead.
  253. /// If <c>true</c>, hopefully it was quick.
  254. /// </summary>
  255. /// <value><c>true</c> if dead; otherwise, <c>false</c>.</value>
  256. public bool Dead
  257. {
  258. get => playerEngine.IsDead(Id);
  259. }
  260. /// <summary>
  261. /// The player's selected block ID in their hand.
  262. /// </summary>
  263. /// <value>The selected block.</value>
  264. public BlockIDs SelectedBlock
  265. {
  266. get
  267. {
  268. return (BlockIDs)playerEngine.GetSelectedBlock(Id);
  269. }
  270. }
  271. /// <summary>
  272. /// The player's selected block color in their hand.
  273. /// </summary>
  274. /// <value>The selected block's color.</value>
  275. public BlockColor SelectedColor
  276. {
  277. get
  278. {
  279. return new BlockColor(playerEngine.GetSelectedColor(Id));
  280. }
  281. }
  282. /// <summary>
  283. /// The player's selected block colour in their hand.
  284. /// </summary>
  285. /// <value>The selected block's colour.</value>
  286. public BlockColor SelectedColour
  287. {
  288. get
  289. {
  290. return new BlockColor(playerEngine.GetSelectedColor(Id));
  291. }
  292. }
  293. // object methods
  294. /// <summary>
  295. /// Teleport the player to the specified coordinates.
  296. /// </summary>
  297. /// <param name="x">The x coordinate.</param>
  298. /// <param name="y">The y coordinate.</param>
  299. /// <param name="z">The z coordinate.</param>
  300. /// <param name="relative">If set to <c>true</c> teleport relative to the player's current position.</param>
  301. /// <param name="exitSeat">If set to <c>true</c> exit any seat the player is in.</param>
  302. public void Teleport(float x, float y, float z, bool relative = true, bool exitSeat = true)
  303. {
  304. float3 location = new float3(x, y, z);
  305. if (relative)
  306. {
  307. location += playerEngine.GetLocation(Id);
  308. }
  309. playerEngine.SetLocation(Id, location, exitSeat: exitSeat);
  310. }
  311. /// <summary>
  312. /// Returns the block the player is currently looking at in build mode.
  313. /// </summary>
  314. /// <param name="maxDistance">The maximum distance from the player (default is the player's building reach)</param>
  315. /// <returns>The block or null if not found</returns>
  316. public Block GetBlockLookedAt(float maxDistance = -1f)
  317. {
  318. var egid = playerEngine.GetThingLookedAt(Id, maxDistance);
  319. return egid.HasValue && egid.Value.groupID != CommonExclusiveGroups.SIMULATION_BODIES_GROUP
  320. ? new Block(egid.Value)
  321. : null;
  322. }
  323. /// <summary>
  324. /// Returns the rigid body the player is currently looking at during simulation.
  325. /// </summary>
  326. /// <param name="maxDistance">The maximum distance from the player (default is the player's building reach)</param>
  327. /// <returns>The block or null if not found</returns>
  328. public SimBody GetSimBodyLookedAt(float maxDistance = -1f)
  329. {
  330. var egid = playerEngine.GetThingLookedAt(Id, maxDistance);
  331. return egid.HasValue && egid.Value.groupID == CommonExclusiveGroups.SIMULATION_BODIES_GROUP
  332. ? new SimBody(egid.Value)
  333. : null;
  334. }
  335. /// <summary>
  336. /// Returns the blocks that are in the player's current selection.
  337. /// </summary>
  338. /// <returns>An array of blocks or an empty array</returns>
  339. public Block[] GetSelectedBlocks()
  340. {
  341. return playerEngine.GetSelectedBlocks(Id);
  342. }
  343. public bool Equals(Player other)
  344. {
  345. if (ReferenceEquals(null, other)) return false;
  346. if (ReferenceEquals(this, other)) return true;
  347. return Id == other.Id;
  348. }
  349. public bool Equals(EGID other)
  350. {
  351. return Id == other.entityID && other.groupID == (Type == PlayerType.Local
  352. ? PlayersExclusiveGroups.LocalPlayers
  353. : PlayersExclusiveGroups.RemotePlayers);
  354. }
  355. public override bool Equals(object obj)
  356. {
  357. if (ReferenceEquals(null, obj)) return false;
  358. if (ReferenceEquals(this, obj)) return true;
  359. if (obj.GetType() != this.GetType()) return false;
  360. return Equals((Player) obj);
  361. }
  362. public override int GetHashCode()
  363. {
  364. return (int) Id;
  365. }
  366. // internal methods
  367. internal static void Init()
  368. {
  369. Utility.GameEngineManager.AddGameEngine(playerEngine);
  370. }
  371. }
  372. }