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.

239 lines
7.0KB

  1. using System;
  2. using System.Runtime.CompilerServices;
  3. using RobocraftX.Character;
  4. using RobocraftX.Character.Movement;
  5. using RobocraftX.Common.Players;
  6. using RobocraftX.Common.Input;
  7. using RobocraftX.Physics;
  8. using Svelto.ECS;
  9. using Unity.Mathematics;
  10. using Unity.Physics;
  11. using GamecraftModdingAPI.Engines;
  12. namespace GamecraftModdingAPI.Players
  13. {
  14. internal class PlayerEngine : IApiEngine
  15. {
  16. public string Name { get; } = "GamecraftModdingAPIPlayerGameEngine";
  17. public EntitiesDB entitiesDB { set; private get; }
  18. public bool isRemovable => false;
  19. private bool isReady = false;
  20. public void Dispose()
  21. {
  22. isReady = false;
  23. }
  24. public void Ready()
  25. {
  26. isReady = true;
  27. }
  28. public uint GetLocalPlayer()
  29. {
  30. if (!isReady) return uint.MaxValue;
  31. PlayerIDStruct[] localPlayers = entitiesDB.QueryEntities<PlayerIDStruct>(PlayersExclusiveGroups.LocalPlayers).ToFastAccess(out uint count);
  32. if (count > 0)
  33. {
  34. return localPlayers[0].ID.entityID;
  35. }
  36. return uint.MaxValue;
  37. }
  38. public uint GetRemotePlayer()
  39. {
  40. if (!isReady) return uint.MaxValue;
  41. PlayerIDStruct[] localPlayers = entitiesDB.QueryEntities<PlayerIDStruct>(PlayersExclusiveGroups.RemotePlayers).ToFastAccess(out uint count);
  42. if (count > 0)
  43. {
  44. return localPlayers[0].ID.entityID;
  45. }
  46. return uint.MaxValue;
  47. }
  48. public bool ExistsById(uint playerId)
  49. {
  50. PlayerIDStruct[] players = entitiesDB.QueryEntities<PlayerIDStruct>(PlayersExclusiveGroups.LocalPlayers).ToFastAccess(out uint count);
  51. for (int i = 0; i < count; i++)
  52. {
  53. if (players[i].ID.entityID == playerId)
  54. {
  55. return true;
  56. }
  57. }
  58. players = entitiesDB.QueryEntities<PlayerIDStruct>(PlayersExclusiveGroups.RemotePlayers).ToFastAccess(out count);
  59. for (int i = 0; i < count; i++)
  60. {
  61. if (players[i].ID.entityID == playerId)
  62. {
  63. return true;
  64. }
  65. }
  66. return false;
  67. }
  68. public float3 GetLocation(uint playerId)
  69. {
  70. if (GetCharacterStruct<RigidBodyEntityStruct>(playerId, out RigidBodyEntityStruct rbes))
  71. {
  72. return rbes.position;
  73. }
  74. return float3.zero;
  75. }
  76. public bool SetLocation(uint playerId, float3 location, bool exitSeat = true)
  77. {
  78. ExclusiveGroup[] characterGroups = CharacterExclusiveGroups.AllCharacters;
  79. for (int i = 0; i < characterGroups.Length; i++)
  80. {
  81. EGID egid = new EGID(playerId, characterGroups[i]);
  82. if (entitiesDB.Exists<RigidBodyEntityStruct>(egid))
  83. {
  84. ref RigidBodyEntityStruct rbes = ref entitiesDB.QueryEntity<RigidBodyEntityStruct>(egid);
  85. if (characterGroups[i] == CharacterExclusiveGroups.InPilotSeatGroup && exitSeat)
  86. {
  87. entitiesDB.QueryEntity<CharacterPilotSeatEntityStruct>(egid).instantExit = true;
  88. entitiesDB.PublishEntityChange<CharacterPilotSeatEntityStruct>(egid);
  89. }
  90. rbes.position = location;
  91. return true;
  92. }
  93. }
  94. return false;
  95. }
  96. public quaternion GetRotation(uint playerId)
  97. {
  98. if (GetCharacterStruct<RigidBodyEntityStruct>(playerId, out RigidBodyEntityStruct rbes))
  99. {
  100. return rbes.rotation;
  101. }
  102. return quaternion.identity;
  103. }
  104. public bool SetRotation(uint playerId, quaternion value)
  105. {
  106. if (GetCharacterStruct<RigidBodyEntityStruct>(playerId, out RigidBodyEntityStruct rbes))
  107. {
  108. rbes.rotation = value;
  109. return true;
  110. }
  111. return false;
  112. }
  113. public float3 GetLinearVelocity(uint playerId)
  114. {
  115. if (GetCharacterStruct<RigidBodyEntityStruct>(playerId, out RigidBodyEntityStruct rbes))
  116. {
  117. return rbes.velocity;
  118. }
  119. return float3.zero;
  120. }
  121. public bool SetLinearVelocity(uint playerId, float3 value)
  122. {
  123. if (GetCharacterStruct<RigidBodyEntityStruct>(playerId, out RigidBodyEntityStruct rbes))
  124. {
  125. rbes.velocity = value;
  126. return true;
  127. }
  128. return false;
  129. }
  130. public float3 GetAngularVelocity(uint playerId)
  131. {
  132. if (GetCharacterStruct<RigidBodyEntityStruct>(playerId, out RigidBodyEntityStruct rbes))
  133. {
  134. return rbes.angularVelocity;
  135. }
  136. return float3.zero;
  137. }
  138. public bool SetAngularVelocity(uint playerId, float3 value)
  139. {
  140. if (GetCharacterStruct<RigidBodyEntityStruct>(playerId, out RigidBodyEntityStruct rbes))
  141. {
  142. rbes.angularVelocity = value;
  143. return true;
  144. }
  145. return false;
  146. }
  147. public PhysicsMass GetMass(uint playerId)
  148. {
  149. if (GetCharacterStruct<RigidBodyEntityStruct>(playerId, out RigidBodyEntityStruct rbes))
  150. {
  151. return rbes.physicsMass;
  152. }
  153. return default;
  154. }
  155. public bool SetInverseMass(uint playerId, float inverseMass)
  156. {
  157. if (GetCharacterStruct<RigidBodyEntityStruct>(playerId, out RigidBodyEntityStruct rbes))
  158. {
  159. rbes.physicsMass.InverseInertia = inverseMass;
  160. return true;
  161. }
  162. return false;
  163. }
  164. public float? GetLastPingTime(uint playerId, PlayerType type)
  165. {
  166. EGID egid = new EGID(playerId, GroupFromEnum(type));
  167. if (entitiesDB.Exists<PlayerNetworkStatsEntityStruct>(egid))
  168. {
  169. return entitiesDB.QueryEntity<PlayerNetworkStatsEntityStruct>(egid).lastPingTimeSinceLevelLoad;
  170. }
  171. return null;
  172. }
  173. // reusable methods
  174. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  175. private ExclusiveGroup GroupFromEnum(PlayerType type)
  176. {
  177. return type == PlayerType.Local ? PlayersExclusiveGroups.LocalPlayers : PlayersExclusiveGroups.RemotePlayers;
  178. }
  179. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  180. public bool GetCharacterStruct<T>(uint playerId, out T s) where T : unmanaged, IEntityComponent
  181. {
  182. ExclusiveGroup[] characterGroups = CharacterExclusiveGroups.AllCharacters;
  183. for (int i = 0; i < characterGroups.Length; i++)
  184. {
  185. EGID egid = new EGID(playerId, characterGroups[i]);
  186. if (entitiesDB.Exists<T>(egid))
  187. {
  188. s = entitiesDB.QueryEntity<T>(egid);
  189. return true;
  190. }
  191. }
  192. s = default;
  193. return false;
  194. }
  195. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  196. public bool GetPlayerStruct<T>(uint playerId, out T s) where T : unmanaged, IEntityComponent
  197. {
  198. ExclusiveGroup[] playerGroups = PlayersExclusiveGroups.AllPlayers;
  199. for (int i = 0; i < playerGroups.Length; i++)
  200. {
  201. EGID egid = new EGID(playerId, playerGroups[i]);
  202. if (entitiesDB.Exists<T>(egid))
  203. {
  204. s = entitiesDB.QueryEntity<T>(egid);
  205. return true;
  206. }
  207. }
  208. s = default;
  209. return false;
  210. }
  211. }
  212. }