Mirror of Svelto.ECS because we're a fan of it
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.

194 lines
9.5KB

  1. using System;
  2. namespace Svelto.ECS
  3. {
  4. public delegate void ExecuteOnAllEntitiesAction<T, W>(T[] entities, ExclusiveGroup.ExclusiveGroupStruct group,
  5. uint count, IEntitiesDB db, ref W value);
  6. public interface IEntitiesDB
  7. {
  8. ///////////////////////////////////////////////////
  9. // Query entities
  10. // ECS systems are meant to work on a set of Entities. These methods allow to iterate over entity
  11. // structs inside a given group or an array of groups
  12. ///////////////////////////////////////////////////
  13. /// <summary>
  14. /// Fast and raw return of entities buffer.
  15. /// </summary>
  16. /// <param name="groupStruct"></param>
  17. /// <param name="count"></param>
  18. /// <typeparam name="T"></typeparam>
  19. /// <returns></returns>
  20. T[] QueryEntities<T>(ExclusiveGroup.ExclusiveGroupStruct groupStruct, out uint count)
  21. where T : struct, IEntityStruct;
  22. (T1[], T2[]) QueryEntities<T1, T2>(ExclusiveGroup.ExclusiveGroupStruct groupStruct, out uint count)
  23. where T1 : struct, IEntityStruct where T2 : struct, IEntityStruct;
  24. (T1[], T2[], T3[]) QueryEntities<T1, T2, T3>(ExclusiveGroup.ExclusiveGroupStruct groupStruct, out uint count)
  25. where T1 : struct, IEntityStruct where T2 : struct, IEntityStruct where T3 : struct, IEntityStruct;
  26. /// <summary>
  27. /// return entities that can be iterated through the EntityCollection iterator
  28. /// </summary>
  29. /// <param name="groupStruct"></param>
  30. /// <typeparam name="T"></typeparam>
  31. /// <returns></returns>
  32. EntityCollection<T> QueryEntities<T>(ExclusiveGroup.ExclusiveGroupStruct groupStruct)
  33. where T : struct, IEntityStruct;
  34. EntityCollection<T1, T2> QueryEntities<T1, T2>(ExclusiveGroup.ExclusiveGroupStruct groupStruct)
  35. where T1 : struct, IEntityStruct where T2 : struct, IEntityStruct;
  36. EntityCollection<T1, T2, T3> QueryEntities<T1, T2, T3>(ExclusiveGroup.ExclusiveGroupStruct groupStruct)
  37. where T1 : struct, IEntityStruct
  38. where T2 : struct, IEntityStruct
  39. where T3 : struct, IEntityStruct;
  40. /// <summary>
  41. /// return entities found in multiple groups, that can be iterated through the EntityCollection iterator
  42. /// This method is useful to write abstracted engines
  43. /// </summary>
  44. /// <typeparam name="T"></typeparam>
  45. /// <returns></returns>
  46. EntityCollections<T> QueryEntities<T>(ExclusiveGroup[] groups) where T : struct, IEntityStruct;
  47. EntityCollections<T1, T2> QueryEntities<T1, T2>(ExclusiveGroup[] groups)
  48. where T1 : struct, IEntityStruct where T2 : struct, IEntityStruct;
  49. ///////////////////////////////////////////////////
  50. // Query entities regardless the group
  51. // these methods are necessary to create abstracted engines. Engines that can iterate over entities regardless
  52. // the group
  53. ///////////////////////////////////////////////////
  54. /// <summary>
  55. /// Execute an action on ALL the entities regardless the group. This function doesn't guarantee cache
  56. /// friendliness even if just EntityStructs are used. Safety checks are in place,
  57. /// </summary>
  58. /// <param name="action"></param>
  59. /// <typeparam name="T"></typeparam>
  60. void ExecuteOnAllEntities<T>(Action<T[], ExclusiveGroup.ExclusiveGroupStruct, uint, IEntitiesDB> action)
  61. where T : struct, IEntityStruct;
  62. /// <summary>
  63. /// same as above, but can pass some external data to avoid allocations
  64. /// </summary>
  65. /// <param name="value"></param>
  66. /// <param name="action"></param>
  67. /// <typeparam name="T"></typeparam>
  68. /// <typeparam name="W"></typeparam>
  69. void ExecuteOnAllEntities<T, W>(ref W value, ExecuteOnAllEntitiesAction<T, W> action)
  70. where T : struct, IEntityStruct;
  71. void ExecuteOnAllEntities<T, W>(W value, Action<T[], ExclusiveGroup.ExclusiveGroupStruct, uint, IEntitiesDB, W> action)
  72. where T : struct, IEntityStruct;
  73. ///////////////////////////////////////////////////
  74. // Query single entities
  75. // ECS systems are meant to work on a set of Entities. Working on a single entity is sometime necessary, hence
  76. // the following methods
  77. // However Because of the double hashing required to identify a specific entity, these function are slower than
  78. // other query methods when used multiple times!
  79. ///////////////////////////////////////////////////
  80. /// <summary>
  81. /// QueryUniqueEntity is a contract method that explicitly declare the intention to have just on entity in a
  82. /// specific group, usually used for GUI elements
  83. /// </summary>
  84. /// <param name="group"></param>
  85. /// <typeparam name="T"></typeparam>
  86. /// <returns></returns>
  87. ref T QueryUniqueEntity<T>(ExclusiveGroup.ExclusiveGroupStruct group) where T : struct, IEntityStruct;
  88. /// <summary>
  89. /// return a specific entity by reference.
  90. /// </summary>
  91. /// <param name="entityGid"></param>
  92. /// <typeparam name="T"></typeparam>
  93. /// <returns></returns>
  94. ref T QueryEntity<T>(EGID entityGid) where T : struct, IEntityStruct;
  95. ref T QueryEntity<T>(uint id, ExclusiveGroup.ExclusiveGroupStruct group) where T : struct, IEntityStruct;
  96. /// <summary>
  97. ///
  98. ///QueryEntitiesAndIndex is useful to optimize cases when multiple entity structs from the same entity must
  99. /// be queried. This is the use case:
  100. ///
  101. ///ref var ghostPosition = ref entitiesDB.QueryEntitiesAndIndex<PositionEntityStruct>
  102. /// (MockupRenderingGroups.GhostCubeID, out var index)[index];
  103. ///ref var ghostScaling = ref entitiesDB.QueryEntities<ScalingEntityStruct>
  104. /// (MockupRenderingGroups.GhostCubeID.groupID, out _)[index];
  105. ///ref var ghostRotation = ref entitiesDB.QueryEntities<RotationEntityStruct>
  106. /// (MockupRenderingGroups.GhostCubeID.groupID, out _)[index];
  107. ///ref var ghostResource = ref entitiesDB.QueryEntities<GFXPrefabEntityStruct>
  108. /// (MockupRenderingGroups.GhostCubeID.groupID, out _)[index];
  109. ///
  110. /// </summary>
  111. /// <param name="entityGid"></param>
  112. /// <param name="index"></param>
  113. /// <typeparam name="T"></typeparam>
  114. /// <returns></returns>
  115. T[] QueryEntitiesAndIndex<T>(EGID entityGid, out uint index) where T : struct, IEntityStruct;
  116. T[] QueryEntitiesAndIndex<T>(uint id, ExclusiveGroup.ExclusiveGroupStruct group, out uint index)
  117. where T : struct, IEntityStruct;
  118. /// <summary>
  119. /// Like QueryEntitiesAndIndex and only way to get an index only if exists
  120. /// </summary>
  121. /// <param name="entityGid"></param>
  122. /// <param name="index"></param>
  123. /// <typeparam name="T"></typeparam>
  124. /// <returns></returns>
  125. bool TryQueryEntitiesAndIndex<T>(EGID entityGid, out uint index, out T[] array) where T : struct, IEntityStruct;
  126. bool TryQueryEntitiesAndIndex
  127. <T>(uint id, ExclusiveGroup.ExclusiveGroupStruct group, out uint index, out T[] array)
  128. where T : struct, IEntityStruct;
  129. /// <summary>
  130. /// this method returns a mapped version of the entity array so that is possible to work on multiple entities
  131. /// inside the group through their EGID. This version skip a level of indirection so it's a bit faster than
  132. /// using QueryEntity multiple times (with different EGIDs).
  133. /// However mapping can be slow so it must be used for not performance critical paths
  134. /// </summary>
  135. /// <typeparam name="T"></typeparam>
  136. /// <returns></returns>
  137. EGIDMapper<T> QueryMappedEntities<T>(ExclusiveGroup.ExclusiveGroupStruct groupStructId)
  138. where T : struct, IEntityStruct;
  139. bool TryQueryMappedEntities<T>(ExclusiveGroup.ExclusiveGroupStruct groupStructId, out EGIDMapper<T> mapper)
  140. where T : struct, IEntityStruct;
  141. ///////////////////////////////////////////////////
  142. // Utility methods
  143. ///////////////////////////////////////////////////
  144. /// <summary>
  145. /// check if a specific entity exists
  146. /// </summary>
  147. /// <param name="egid"></param>
  148. /// <typeparam name="T"></typeparam>
  149. /// <returns></returns>
  150. bool Exists<T>(EGID egid) where T : struct, IEntityStruct;
  151. bool Exists<T>(uint id, ExclusiveGroup.ExclusiveGroupStruct group) where T : struct, IEntityStruct;
  152. bool Exists(ExclusiveGroup.ExclusiveGroupStruct gid);
  153. /// <summary>
  154. /// know if there is any entity struct in a specific group
  155. /// </summary>
  156. /// <param name="groupStruct"></param>
  157. /// <typeparam name="T"></typeparam>
  158. /// <returns></returns>
  159. bool HasAny<T>(ExclusiveGroup.ExclusiveGroupStruct groupStruct) where T : struct, IEntityStruct;
  160. /// <summary>
  161. /// Count the number of entity structs in a specific group
  162. /// </summary>
  163. /// <param name="groupStruct"></param>
  164. /// <typeparam name="T"></typeparam>
  165. /// <returns></returns>
  166. uint Count<T>(ExclusiveGroup.ExclusiveGroupStruct groupStruct) where T : struct, IEntityStruct;
  167. /// <summary>
  168. /// </summary>
  169. /// <param name="egid"></param>
  170. /// <typeparam name="T"></typeparam>
  171. void PublishEntityChange<T>(EGID egid) where T : unmanaged, IEntityStruct;
  172. }
  173. }