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.

513 lines
18KB

  1. using System;
  2. using Gamecraft.Wires;
  3. using RobocraftX.Character;
  4. using RobocraftX.Character.Movement;
  5. using Unity.Mathematics;
  6. using RobocraftX.Common;
  7. using RobocraftX.Common.Players;
  8. using RobocraftX.GUI.Wires;
  9. using RobocraftX.Physics;
  10. using Svelto.ECS;
  11. using Techblox.BuildingDrone;
  12. using Techblox.Camera;
  13. using TechbloxModdingAPI.Blocks;
  14. using TechbloxModdingAPI.Players;
  15. using TechbloxModdingAPI.Utility;
  16. using UnityEngine;
  17. namespace TechbloxModdingAPI
  18. {
  19. /// <summary>
  20. /// An in-game player character. Any Leo you see is a player.
  21. /// </summary>
  22. public class Player : IEquatable<Player>, IEquatable<EGID>
  23. { //TODO: Inherit EcsObjectBase and make Id an EGID, useful for caching
  24. // static functionality
  25. private static PlayerEngine playerEngine = new PlayerEngine();
  26. private static Player localPlayer;
  27. /// <summary>
  28. /// Checks if the specified player exists.
  29. /// </summary>
  30. /// <returns>Whether the player exists.</returns>
  31. /// <param name="player">Player type.</param>
  32. public static bool Exists(PlayerType player)
  33. {
  34. switch (player)
  35. {
  36. case PlayerType.Remote:
  37. return playerEngine.GetRemotePlayer() != uint.MaxValue;
  38. case PlayerType.Local:
  39. return playerEngine.GetLocalPlayer() != uint.MaxValue;
  40. }
  41. return false;
  42. }
  43. /// <summary>
  44. /// Checks if the specified player exists.
  45. /// </summary>
  46. /// <returns>Whether the player exists.</returns>
  47. /// <param name="player">The player's unique identifier.</param>
  48. public static bool Exists(uint player)
  49. {
  50. return playerEngine.ExistsById(player);
  51. }
  52. /// <summary>
  53. /// The amount of Players in the current game.
  54. /// </summary>
  55. /// <returns>The count.</returns>
  56. public static uint Count()
  57. {
  58. return (uint) playerEngine.GetAllPlayerCount();
  59. }
  60. /// <summary>
  61. /// Returns the current player belonging to this client.
  62. /// </summary>
  63. public static Player LocalPlayer
  64. {
  65. get
  66. {
  67. if (localPlayer == null || localPlayer.Id != playerEngine.GetLocalPlayer())
  68. localPlayer = new Player(PlayerType.Local);
  69. return localPlayer;
  70. }
  71. }
  72. /// <summary>
  73. /// Initializes a new instance of the <see cref="T:TechbloxModdingAPI.Player"/> class.
  74. /// </summary>
  75. /// <param name="id">The player's unique identifier.</param>
  76. public Player(uint id)
  77. {
  78. this.Id = id;
  79. if (!Exists(id))
  80. {
  81. throw new PlayerNotFoundException($"No player with id {id} exists");
  82. }
  83. this.Type = playerEngine.GetLocalPlayer() == id ? PlayerType.Local : PlayerType.Remote;
  84. }
  85. /// <summary>
  86. /// Initializes a new instance of the <see cref="T:TechbloxModdingAPI.Player"/> class.
  87. /// </summary>
  88. /// <param name="player">The player type. Chooses the first available player matching the criteria.</param>
  89. public Player(PlayerType player)
  90. {
  91. switch (player)
  92. {
  93. case PlayerType.Local:
  94. this.Id = playerEngine.GetLocalPlayer();
  95. break;
  96. case PlayerType.Remote:
  97. this.Id = playerEngine.GetRemotePlayer();
  98. break;
  99. }
  100. if (this.Id == uint.MaxValue)
  101. {
  102. throw new PlayerNotFoundException($"No player of {player} type exists");
  103. }
  104. this.Type = player;
  105. }
  106. // object fields & properties
  107. /// <summary>
  108. /// The player's type.
  109. /// The player type is always relative to the current client, not the game host.
  110. /// </summary>
  111. /// <value>The enumerated player type.</value>
  112. public PlayerType Type { get; }
  113. /// <summary>
  114. /// The player's unique identifier.
  115. /// </summary>
  116. /// <value>The identifier.</value>
  117. public uint Id { get; }
  118. /// <summary>
  119. /// The player's current position.
  120. /// </summary>
  121. /// <value>The position.</value>
  122. public float3 Position
  123. {
  124. get => playerEngine.GetCharacterStruct<RigidBodyEntityStruct>(Id).Get().position;
  125. set => playerEngine.SetLocation(Id, value, false);
  126. }
  127. /// <summary>
  128. /// The player's current rotation.
  129. /// </summary>
  130. /// <value>The rotation.</value>
  131. public float3 Rotation
  132. {
  133. get => ((Quaternion) (GameState.IsBuildMode()
  134. ? playerEngine.GetCameraStruct<CameraEntityStruct>(Id).Get().rotation
  135. : playerEngine.GetCharacterStruct<RigidBodyEntityStruct>(Id).Get().rotation)).eulerAngles;
  136. set => _ = GameState.IsBuildMode()
  137. ? playerEngine.GetCameraStruct<CameraEntityStruct>(Id).Get().rotation = quaternion.Euler(value)
  138. : playerEngine.GetCharacterStruct<RigidBodyEntityStruct>(Id).Get().rotation = quaternion.Euler(value);
  139. }
  140. /// <summary>
  141. /// The player's current velocity.
  142. /// </summary>
  143. /// <value>The velocity.</value>
  144. public float3 Velocity
  145. {
  146. get => playerEngine.GetCharacterStruct<RigidBodyEntityStruct>(Id).Get().velocity;
  147. set => playerEngine.GetCharacterStruct<RigidBodyEntityStruct>(Id).Get().velocity = value;
  148. }
  149. /// <summary>
  150. /// The player's current angular velocity.
  151. /// </summary>
  152. /// <value>The angular velocity.</value>
  153. public float3 AngularVelocity
  154. {
  155. get => playerEngine.GetCharacterStruct<RigidBodyEntityStruct>(Id).Get().angularVelocity;
  156. set => playerEngine.GetCharacterStruct<RigidBodyEntityStruct>(Id).Get().angularVelocity = value;
  157. }
  158. /// <summary>
  159. /// The player's mass.
  160. /// </summary>
  161. /// <value>The mass.</value>
  162. public float Mass =>
  163. 1f / playerEngine.GetCharacterStruct<RigidBodyEntityStruct>(Id).Get().physicsMass.InverseMass;
  164. private float _ping = -1f;
  165. /// <summary>
  166. /// The player's latest network ping time.
  167. /// </summary>
  168. /// <value>The ping (s).</value>
  169. public float Ping
  170. {
  171. get
  172. {
  173. var opt = playerEngine.GetPlayerStruct<PlayerNetworkStatsEntityStruct>(Id, Type);
  174. if (opt)
  175. {
  176. _ping = opt.Get().lastPingTimeSinceLevelLoad ?? _ping;
  177. }
  178. return _ping;
  179. }
  180. }
  181. /// <summary>
  182. /// The player's initial health when entering Simulation (aka Time Running) mode.
  183. /// </summary>
  184. /// <value>The initial health.</value>
  185. public float InitialHealth
  186. {
  187. get
  188. {
  189. var opt = playerEngine.GetCharacterStruct<CharacterHealthEntityStruct>(Id);
  190. return opt ? opt.Get().initialHealth : -1f;
  191. }
  192. set => playerEngine.GetCharacterStruct<CharacterHealthEntityStruct>(Id).Get().initialHealth = value;
  193. }
  194. /// <summary>
  195. /// The player's current health in Simulation (aka Time Running) mode.
  196. /// </summary>
  197. /// <value>The current health.</value>
  198. public float CurrentHealth
  199. {
  200. get
  201. {
  202. var opt = playerEngine.GetCharacterStruct<CharacterHealthEntityStruct>(Id);
  203. return opt ? opt.Get().currentHealth : -1f;
  204. }
  205. set => playerEngine.GetCharacterStruct<CharacterHealthEntityStruct>(Id).Get().currentHealth = value;
  206. }
  207. /// <summary>
  208. /// Whether this <see cref="T:TechbloxModdingAPI.Player"/> is damageable.
  209. /// </summary>
  210. /// <value><c>true</c> if damageable; otherwise, <c>false</c>.</value>
  211. public bool Damageable
  212. {
  213. get
  214. {
  215. var opt = playerEngine.GetCharacterStruct<CharacterHealthEntityStruct>(Id);
  216. return opt.Get().canTakeDamageStat;
  217. }
  218. set
  219. {
  220. ref var healthStruct = ref playerEngine.GetCharacterStruct<CharacterHealthEntityStruct>(Id).Get();
  221. healthStruct.canTakeDamage = value;
  222. healthStruct.canTakeDamageStat = 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
  232. {
  233. var opt = playerEngine.GetCharacterStruct<CharacterLivesEntityComponent>(Id);
  234. return opt ? opt.Get().initialLives : uint.MaxValue;
  235. }
  236. set => playerEngine.GetCharacterStruct<CharacterLivesEntityComponent>(Id).Get().initialLives = value;
  237. }
  238. /// <summary>
  239. /// The player's current lives in Simulation (aka Time Running) mode.
  240. /// </summary>
  241. /// <value>The current lives.</value>
  242. public uint CurrentLives
  243. {
  244. get
  245. {
  246. var opt = playerEngine.GetCharacterStruct<CharacterLivesEntityComponent>(Id);
  247. return opt ? opt.Get().currentLives : uint.MaxValue;
  248. }
  249. set => playerEngine.GetCharacterStruct<CharacterLivesEntityComponent>(Id).Get().currentLives = value;
  250. }
  251. /*/// <summary>
  252. /// Whether the Game Over screen is displayed for the player.
  253. /// </summary>
  254. /// <value><c>true</c> if game over; otherwise, <c>false</c>.</value>
  255. public bool GameOver
  256. {
  257. get => playerEngine.GetGameOverScreen(Id);
  258. }*/
  259. /// <summary>
  260. /// Whether the player is dead.
  261. /// If <c>true</c>, hopefully it was quick.
  262. /// </summary>
  263. /// <value><c>true</c> if dead; otherwise, <c>false</c>.</value>
  264. public bool Dead
  265. {
  266. get => playerEngine.IsDead(Id);
  267. }
  268. /// <summary>
  269. /// The player's selected block ID in their hand.
  270. /// </summary>
  271. /// <value>The selected block.</value>
  272. public BlockIDs SelectedBlock
  273. {
  274. get
  275. {
  276. var optstruct = playerEngine.GetCharacterStruct<EquippedPartStruct>(Id);
  277. return optstruct ? (BlockIDs) optstruct.Get().SelectedDBPartID : BlockIDs.Invalid;
  278. }
  279. }
  280. /// <summary>
  281. /// The player's selected block color in their hand.
  282. /// </summary>
  283. /// <value>The selected block's color.</value>
  284. public BlockColor SelectedColor
  285. {
  286. get
  287. {
  288. var optstruct = playerEngine.GetCharacterStruct<EquippedColourStruct>(Id);
  289. return optstruct ? new BlockColor(optstruct.Get().indexInPalette) : BlockColors.Default;
  290. }
  291. }
  292. /// <summary>
  293. /// The player's selected block colour in their hand.
  294. /// </summary>
  295. /// <value>The selected block's colour.</value>
  296. public BlockColor SelectedColour
  297. {
  298. get
  299. {
  300. var optstruct = playerEngine.GetCharacterStruct<EquippedColourStruct>(Id);
  301. return optstruct ? new BlockColor(optstruct.Get().indexInPalette) : BlockColors.Default;
  302. }
  303. }
  304. /// <summary>
  305. /// The player's selected blueprint in their hand. Set to null to clear. Dispose after usage.
  306. /// </summary>
  307. public Blueprint SelectedBlueprint
  308. {
  309. get
  310. {
  311. var lbiso = playerEngine.GetPlayerStruct<LocalBlueprintInputStruct>(Id, Type);
  312. return lbiso ? new Blueprint(lbiso.Get().selectedBlueprintId) : null;
  313. }
  314. set => BlockGroup._engine.SelectBlueprint(value?.Id ?? uint.MaxValue);
  315. }
  316. /// <summary>
  317. /// The player's mode in time stopped mode, determining what they place.
  318. /// </summary>
  319. public PlayerBuildingMode BuildingMode => (PlayerBuildingMode) playerEngine
  320. .GetCharacterStruct<TimeStoppedModeComponent>(Id).Get().timeStoppedContext;
  321. /// <summary>
  322. /// Whether the player is sprinting.
  323. /// </summary>
  324. public bool Sprinting
  325. {
  326. get => GameState.IsBuildMode()
  327. ? playerEngine.GetCharacterStruct<BuildingDroneMovementComponent>(Id).Get().sprinting
  328. : playerEngine.GetCharacterStruct<CharacterMovementEntityStruct>(Id).Get().isSprinting;
  329. set => _ = GameState.IsBuildMode()
  330. ? playerEngine.GetCharacterStruct<BuildingDroneMovementComponent>(Id).Get().sprinting = value
  331. : playerEngine.GetCharacterStruct<CharacterMovementEntityStruct>(Id).Get().isSprinting = value;
  332. }
  333. /// <summary>
  334. /// Movement speed setting. Build mode (camera) and simulation mode settings are separate.
  335. /// </summary>
  336. public float SpeedSetting
  337. {
  338. get => GameState.IsBuildMode()
  339. ? playerEngine.GetCharacterStruct<BuildingDroneMovementSettingsComponent>(Id).Get().speed
  340. : playerEngine.GetCharacterStruct<CharacterMovementSettingsEntityStruct>(Id).Get().moveSpeed;
  341. set => _ = GameState.IsBuildMode()
  342. ? playerEngine.GetCharacterStruct<BuildingDroneMovementSettingsComponent>(Id).Get().speed = value
  343. : playerEngine.GetCharacterStruct<CharacterMovementSettingsEntityStruct>(Id).Get().moveSpeed = value;
  344. }
  345. /// <summary>
  346. /// The multiplier setting to use when sprinting. Build mode (camera) and simulation mode settings are separate.
  347. /// </summary>
  348. public float SpeedSprintMultiplierSetting
  349. {
  350. get => GameState.IsBuildMode()
  351. ? playerEngine.GetCharacterStruct<BuildingDroneMovementSettingsComponent>(Id).Get().speedSprintMultiplier
  352. : playerEngine.GetCharacterStruct<CharacterMovementSettingsEntityStruct>(Id).Get().sprintSpeedMultiplier;
  353. set => _ = GameState.IsBuildMode()
  354. ? playerEngine.GetCharacterStruct<BuildingDroneMovementSettingsComponent>(Id).Get().speedSprintMultiplier = value
  355. : playerEngine.GetCharacterStruct<CharacterMovementSettingsEntityStruct>(Id).Get().sprintSpeedMultiplier = value;
  356. }
  357. /// <summary>
  358. /// The acceleration setting of the player. Build mode (camera) and simulation mode settings are separate.
  359. /// </summary>
  360. public float AccelerationSetting
  361. {
  362. get => GameState.IsBuildMode()
  363. ? playerEngine.GetCharacterStruct<BuildingDroneMovementSettingsComponent>(Id).Get().acceleration
  364. : playerEngine.GetCharacterStruct<CharacterMovementSettingsEntityStruct>(Id).Get().acceleration;
  365. set => _ = GameState.IsBuildMode()
  366. ? playerEngine.GetCharacterStruct<BuildingDroneMovementSettingsComponent>(Id).Get().acceleration = value
  367. : playerEngine.GetCharacterStruct<CharacterMovementSettingsEntityStruct>(Id).Get().acceleration = value;
  368. }
  369. // object methods
  370. /// <summary>
  371. /// Teleport the player to the specified coordinates.
  372. /// </summary>
  373. /// <param name="x">The x coordinate.</param>
  374. /// <param name="y">The y coordinate.</param>
  375. /// <param name="z">The z coordinate.</param>
  376. /// <param name="relative">If set to <c>true</c> teleport relative to the player's current position.</param>
  377. /// <param name="exitSeat">If set to <c>true</c> exit any seat the player is in.</param>
  378. public void Teleport(float x, float y, float z, bool relative = true, bool exitSeat = true)
  379. {
  380. float3 location = new float3(x, y, z);
  381. if (relative)
  382. {
  383. location += Position;
  384. }
  385. playerEngine.SetLocation(Id, location, exitSeat: exitSeat);
  386. }
  387. /// <summary>
  388. /// Returns the block the player is currently looking at in build mode.
  389. /// </summary>
  390. /// <param name="maxDistance">The maximum distance from the player (default is the player's building reach)</param>
  391. /// <returns>The block or null if not found</returns>
  392. public Block GetBlockLookedAt(float maxDistance = -1f)
  393. {
  394. var egid = playerEngine.GetThingLookedAt(Id, maxDistance);
  395. return egid != default && egid.groupID != CommonExclusiveGroups.SIMULATION_BODIES_GROUP
  396. && egid.groupID != WiresGUIExclusiveGroups.WireGroup
  397. ? Block.New(egid)
  398. : null;
  399. }
  400. /// <summary>
  401. /// Returns the rigid body the player is currently looking at during simulation.
  402. /// </summary>
  403. /// <param name="maxDistance">The maximum distance from the player (default is the player's building reach)</param>
  404. /// <returns>The body or null if not found</returns>
  405. public SimBody GetSimBodyLookedAt(float maxDistance = -1f)
  406. {
  407. var egid = playerEngine.GetThingLookedAt(Id, maxDistance);
  408. return egid != default && egid.groupID == CommonExclusiveGroups.SIMULATION_BODIES_GROUP
  409. ? EcsObjectBase.GetInstance(egid, e => new SimBody(e))
  410. : null;
  411. }
  412. /// <summary>
  413. /// Returns the wire the player is currently looking at in build mode.
  414. /// </summary>
  415. /// <param name="maxDistance">The maximum distance from the player (default is the player's building reach)</param>
  416. /// <returns>The wire or null if not found</returns>
  417. public Wire GetWireLookedAt(float maxDistance = -1f)
  418. {
  419. var egid = playerEngine.GetThingLookedAt(Id, maxDistance);
  420. return egid != default && egid.groupID == WiresGUIExclusiveGroups.WireGroup
  421. ? EcsObjectBase.GetInstance(new EGID(egid.entityID, NamedExclusiveGroup<WiresGroup>.Group),
  422. e => new Wire(e))
  423. : null;
  424. }
  425. /// <summary>
  426. /// Returns the blocks that are in the player's current selection.
  427. /// </summary>
  428. /// <returns>An array of blocks or an empty array</returns>
  429. public Block[] GetSelectedBlocks()
  430. {
  431. return playerEngine.GetSelectedBlocks(Id);
  432. }
  433. public bool Equals(Player other)
  434. {
  435. if (ReferenceEquals(null, other)) return false;
  436. if (ReferenceEquals(this, other)) return true;
  437. return Id == other.Id;
  438. }
  439. public bool Equals(EGID other)
  440. {
  441. return Id == other.entityID && other.groupID == (Type == PlayerType.Local
  442. ? PlayersExclusiveGroups.LocalPlayers
  443. : PlayersExclusiveGroups.RemotePlayers);
  444. }
  445. public override bool Equals(object obj)
  446. {
  447. if (ReferenceEquals(null, obj)) return false;
  448. if (ReferenceEquals(this, obj)) return true;
  449. if (obj.GetType() != this.GetType()) return false;
  450. return Equals((Player) obj);
  451. }
  452. public override int GetHashCode()
  453. {
  454. return (int) Id;
  455. }
  456. // internal methods
  457. internal static void Init()
  458. {
  459. Utility.GameEngineManager.AddGameEngine(playerEngine);
  460. }
  461. }
  462. }