A stable modding interface between Techblox and mods https://mod.exmods.org/
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

210 lignes
6.8KB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Reflection;
  4. using System.Runtime.CompilerServices;
  5. using RobocraftX.Character;
  6. using RobocraftX.Character.Movement;
  7. using RobocraftX.Common.Players;
  8. using RobocraftX.Common.Input;
  9. using RobocraftX.CR.MachineEditing.BoxSelect;
  10. using RobocraftX.Physics;
  11. using RobocraftX.Blocks.Ghost;
  12. using Gamecraft.GUI.HUDFeedbackBlocks;
  13. using Svelto.ECS;
  14. using Techblox.Camera;
  15. using Techblox.FlyCam;
  16. using Unity.Mathematics;
  17. using Unity.Physics;
  18. using UnityEngine;
  19. using HarmonyLib;
  20. using RobocraftX.Common;
  21. using Svelto.ECS.DataStructures;
  22. using TechbloxModdingAPI.Engines;
  23. using TechbloxModdingAPI.Utility;
  24. namespace TechbloxModdingAPI.Players
  25. {
  26. internal class PlayerEngine : IApiEngine, IFactoryEngine
  27. {
  28. public string Name { get; } = "TechbloxModdingAPIPlayerGameEngine";
  29. public EntitiesDB entitiesDB { set; private get; }
  30. public bool isRemovable => false;
  31. public IEntityFactory Factory { set; private get; }
  32. private bool isReady = false;
  33. public void Dispose()
  34. {
  35. isReady = false;
  36. }
  37. public void Ready()
  38. {
  39. isReady = true;
  40. }
  41. public uint GetLocalPlayer()
  42. {
  43. if (!isReady) return uint.MaxValue;
  44. var localPlayers = entitiesDB.QueryEntities<PlayerIDStruct>(PlayersExclusiveGroups.LocalPlayers).ToBuffer();
  45. if (localPlayers.count > 0)
  46. {
  47. return localPlayers.buffer[0].ID.entityID;
  48. }
  49. return uint.MaxValue;
  50. }
  51. public uint GetRemotePlayer()
  52. {
  53. if (!isReady) return uint.MaxValue;
  54. var localPlayers = entitiesDB.QueryEntities<PlayerIDStruct>(PlayersExclusiveGroups.RemotePlayers).ToBuffer();
  55. if (localPlayers.count > 0)
  56. {
  57. return localPlayers.buffer[0].ID.entityID;
  58. }
  59. return uint.MaxValue;
  60. }
  61. public long GetAllPlayerCount()
  62. {
  63. if (entitiesDB == null) return 0;
  64. long count = 0;
  65. foreach (ExclusiveGroupStruct eg in PlayersExclusiveGroups.AllPlayers)
  66. {
  67. count += entitiesDB.Count<PlayerIDStruct>(eg);
  68. }
  69. return count;
  70. }
  71. public long GetLocalPlayerCount()
  72. {
  73. if (entitiesDB == null) return 0;
  74. return entitiesDB.Count<PlayerIDStruct>(PlayersExclusiveGroups.LocalPlayers);
  75. }
  76. public long GetRemotePlayerCount()
  77. {
  78. if (entitiesDB == null) return 0;
  79. return entitiesDB.Count<PlayerIDStruct>(PlayersExclusiveGroups.RemotePlayers);
  80. }
  81. public bool ExistsById(uint playerId)
  82. {
  83. if (entitiesDB == null) return false;
  84. return entitiesDB.Exists<PlayerIDStruct>(playerId, PlayersExclusiveGroups.LocalPlayers)
  85. || entitiesDB.Exists<PlayerIDStruct>(playerId, PlayersExclusiveGroups.RemotePlayers);
  86. }
  87. public bool SetLocation(uint playerId, float3 location, bool exitSeat = true)
  88. {
  89. if (entitiesDB == null) return false;
  90. var rbesOpt = GetCharacterStruct<RigidBodyEntityStruct>(playerId, out var group);
  91. if (!rbesOpt)
  92. return false;
  93. if (group == CharacterExclusiveGroups.InPilotSeatGroup && exitSeat)
  94. {
  95. EGID egid = new EGID(playerId, group);
  96. entitiesDB.QueryEntity<CharacterPilotSeatEntityStruct>(egid).instantExit = true;
  97. entitiesDB.PublishEntityChange<CharacterPilotSeatEntityStruct>(egid);
  98. }
  99. rbesOpt.Get().position = location;
  100. return true;
  101. }
  102. public bool GetGameOverScreen(uint playerId)
  103. {
  104. if (entitiesDB == null) return false;
  105. ref HudActivatedBlocksEntityStruct habes = ref entitiesDB.QueryEntity<HudActivatedBlocksEntityStruct>(HUDFeedbackBlocksGUIExclusiveGroups.GameOverHudEgid);
  106. NativeDynamicArrayCast<EGID> nativeDynamicArrayCast = new NativeDynamicArrayCast<EGID>(habes.activatedBlocksOrdered);
  107. return nativeDynamicArrayCast.count > 0;
  108. }
  109. public bool IsDead(uint playerId)
  110. {
  111. if (entitiesDB == null) return true;
  112. return entitiesDB.Exists<RigidBodyEntityStruct>(playerId, CharacterExclusiveGroups.DeadCharacters);
  113. }
  114. // reusable methods
  115. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  116. public OptionalRef<T> GetCharacterStruct<T>(uint playerId) where T : unmanaged, IEntityComponent =>
  117. GetCharacterStruct<T>(playerId, out _);
  118. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  119. public OptionalRef<T> GetCharacterStruct<T>(uint playerId, out ExclusiveGroupStruct group) where T : unmanaged, IEntityComponent
  120. {
  121. group = default;
  122. if (GameState.IsBuildMode())
  123. return entitiesDB.QueryEntityOptional<T>(new EGID(playerId, Techblox.FlyCam.FlyCam.Group));
  124. var characterGroups = CharacterExclusiveGroups.AllCharacters;
  125. for (int i = 0; i < characterGroups.count; i++)
  126. {
  127. EGID egid = new EGID(playerId, characterGroups[i]);
  128. var opt = entitiesDB.QueryEntityOptional<T>(egid);
  129. if (opt.Exists)
  130. {
  131. group = characterGroups[i];
  132. return opt;
  133. }
  134. }
  135. return new OptionalRef<T>();
  136. }
  137. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  138. public OptionalRef<T> GetPlayerStruct<T>(uint playerId, PlayerType type) where T : unmanaged, IEntityComponent
  139. {
  140. var playerGroup = type == PlayerType.Local ? PlayersExclusiveGroups.LocalPlayers : PlayersExclusiveGroups.RemotePlayers;
  141. EGID egid = new EGID(playerId, playerGroup);
  142. return entitiesDB.QueryEntityOptional<T>(egid);
  143. }
  144. public OptionalRef<T> GetCameraStruct<T>(uint playerId) where T : unmanaged, IEntityComponent
  145. {
  146. return entitiesDB.QueryEntityOptional<T>(new EGID(playerId, CameraExclusiveGroups.CameraGroup));
  147. }
  148. public EGID GetThingLookedAt(uint playerId, float maxDistance = -1f)
  149. {
  150. var opt = GetCameraStruct<CharacterCameraRayCastEntityStruct>(playerId);
  151. if (!opt) return EGID.Empty;
  152. CharacterCameraRayCastEntityStruct rayCast = opt;
  153. float distance = maxDistance < 0
  154. ? GhostBlockUtils.GetBuildInteractionDistance(entitiesDB, rayCast,
  155. GhostBlockUtils.GhostCastMethod.GhostCastProportionalToBlockSize)
  156. : maxDistance;
  157. if (rayCast.hit && rayCast.distance <= distance)
  158. return rayCast.hitEgid; //May be EGID.Empty
  159. return EGID.Empty;
  160. }
  161. public unsafe Block[] GetSelectedBlocks(uint playerid)
  162. {
  163. if (!entitiesDB.Exists<BoxSelectStateEntityStruct>(playerid,
  164. BoxSelectExclusiveGroups.BoxSelectVolumeExclusiveGroup))
  165. return new Block[0];
  166. var state = entitiesDB.QueryEntity<BoxSelectStateEntityStruct>(playerid,
  167. BoxSelectExclusiveGroups.BoxSelectVolumeExclusiveGroup);
  168. var blocks = entitiesDB.QueryEntity<SelectedBlocksStruct>(playerid,
  169. BoxSelectExclusiveGroups.BoxSelectVolumeExclusiveGroup);
  170. if (!state.active) return new Block[0];
  171. var pointer = (EGID*) blocks.selectedBlocks.ToPointer();
  172. var ret = new Block[blocks.count];
  173. for (int j = 0; j < blocks.count; j++)
  174. {
  175. var egid = pointer[j];
  176. ret[j] = Block.New(egid);
  177. }
  178. return ret;
  179. }
  180. }
  181. }