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.

IEntitiesDB.cs 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. using Svelto.DataStructures;
  2. namespace Svelto.ECS
  3. {
  4. public interface IEntitiesDB
  5. {
  6. /// <summary>
  7. /// All the EntityView related methods are left for back compatibility, but
  8. /// shouldn't be used anymore. Always pick EntityViewStruct or EntityStruct
  9. /// over EntityView
  10. /// </summary>
  11. ReadOnlyCollectionStruct<T> QueryEntityViews<T>(int group) where T : class, IEntityStruct;
  12. /// <summary>
  13. /// All the EntityView related methods are left for back compatibility, but
  14. /// shouldn't be used anymore. Always pick EntityViewStruct or EntityStruct
  15. /// over EntityView
  16. /// </summary>
  17. bool TryQueryEntityView<T>(EGID egid, out T entityView) where T : class, IEntityStruct;
  18. /// <summary>
  19. /// All the EntityView related methods are left for back compatibility, but
  20. /// shouldn't be used anymore. Always pick EntityViewStruct or EntityStruct
  21. /// over EntityView
  22. /// </summary>
  23. T QueryEntityView<T>(EGID egid) where T : class, IEntityStruct;
  24. /// <summary>
  25. /// Fast and raw (therefore not safe) return of entities buffer
  26. /// Modifying a buffer would compromise the integrity of the whole DB
  27. /// so they are meant to be used only in performance critical path
  28. /// </summary>
  29. /// <param name="count"></param>
  30. /// <typeparam name="T"></typeparam>
  31. /// <returns></returns>
  32. T[] QueryEntities<T>(int group, out int count) where T : IEntityStruct;
  33. T[] QueryEntities<T>(ExclusiveGroup @group, out int targetsCount) where T : IEntityStruct;
  34. /// <summary>
  35. /// this version returns a mapped version of the entity array so that is possible to find the
  36. /// index of the entity inside the returned buffer through it's EGID
  37. /// However mapping can be slow so it must be used for not performance critical paths
  38. /// </summary>
  39. /// <param name="groupID"></param>
  40. /// <param name="mapper"></param>
  41. /// <typeparam name="T"></typeparam>
  42. /// <returns></returns>
  43. EGIDMapper<T> QueryMappedEntities<T>(int groupID) where T : IEntityStruct;
  44. EGIDMapper<T> QueryMappedEntities<T>(ExclusiveGroup groupID) where T : IEntityStruct;
  45. /// <summary>
  46. /// Execute an action on entities. Be sure that the action is not capturing variables
  47. /// otherwise you will allocate memory which will have a great impact on the execution performance.
  48. /// ExecuteOnEntities can be used to iterate safely over entities, several checks are in place
  49. /// to be sure that everything will be done correctly.
  50. /// Cache friendliness is guaranteed if only Entity Structs are used, but
  51. /// </summary>
  52. /// <param name="egid"></param>
  53. /// <param name="action"></param>
  54. /// <typeparam name="T"></typeparam>
  55. void ExecuteOnEntities<T>(int groupID, EntitiesAction<T> action) where T : IEntityStruct;
  56. void ExecuteOnEntities<T>(ExclusiveGroup groupID, EntitiesAction<T> action) where T : IEntityStruct;
  57. void ExecuteOnEntities<T, W>(int groupID, ref W value, EntitiesAction<T, W> action) where T : IEntityStruct;
  58. void ExecuteOnEntities<T, W>(ExclusiveGroup groupID, ref W value, EntitiesAction<T, W> action) where T : IEntityStruct;
  59. /// <summary>
  60. /// Execute an action on ALL the entities regardless the group. This function doesn't guarantee cache
  61. /// friendliness even if just EntityStructs are used.
  62. /// Safety checks are in place
  63. /// </summary>
  64. /// <param name="damageableGroups"></param>
  65. /// <param name="action"></param>
  66. /// <typeparam name="T"></typeparam>
  67. void ExecuteOnAllEntities<T>(ExclusiveGroup[] damageableGroups, AllEntitiesAction<T> action) where T : IEntityStruct;
  68. void ExecuteOnAllEntities<T, W>(ref W value, AllEntitiesAction<T, W> action) where T : IEntityStruct;
  69. void ExecuteOnAllEntities<T>(ExclusiveGroup[] groups, EntitiesAction<T> action) where T : IEntityStruct;
  70. void ExecuteOnAllEntities<T, W>(ExclusiveGroup[] groups, ref W value, EntitiesAction<T, W> action) where T : IEntityStruct;
  71. /// <summary>
  72. /// ECS is meant to work on a set of Entities. Working on a single entity is sometime necessary, but using
  73. /// the following functions inside a loop would be a mistake as performance can be significantly impacted
  74. /// return the buffer and the index of the entity inside the buffer using the input EGID
  75. /// </summary>
  76. /// <param name="entityGid"></param>
  77. /// <param name="index"></param>
  78. /// <typeparam name="T"></typeparam>
  79. /// <returns></returns>
  80. T[] QueryEntitiesAndIndex<T>(EGID entityGid, out uint index) where T : IEntityStruct;
  81. bool TryQueryEntitiesAndIndex<T>(EGID entityGid, out uint index, out T[] array) where T : IEntityStruct;
  82. /// <summary>
  83. /// ECS is meant to work on a set of Entities. Working on a single entity is sometime necessary, but using
  84. /// the following functions inside a loop would be a mistake as performance can be significantly impacted
  85. /// Execute an action on a specific Entity. Be sure that the action is not capturing variables
  86. /// otherwise you will allocate memory which will have a great impact on the execution performance
  87. /// </summary>
  88. /// <param name="egid"></param>
  89. /// <param name="action"></param>
  90. /// <typeparam name="T"></typeparam>
  91. void ExecuteOnEntity<T>(EGID egid, EntityAction<T> action) where T : IEntityStruct;
  92. void ExecuteOnEntity<T>(int id, int groupid, EntityAction<T> action) where T : IEntityStruct;
  93. void ExecuteOnEntity<T>(int id, ExclusiveGroup groupid, EntityAction<T> action) where T : IEntityStruct;
  94. void ExecuteOnEntity<T, W>(EGID egid, ref W value, EntityAction<T, W> action) where T : IEntityStruct;
  95. void ExecuteOnEntity<T, W>(int id, int groupid, ref W value, EntityAction<T, W> action) where T : IEntityStruct;
  96. void ExecuteOnEntity<T, W>(int id, ExclusiveGroup groupid, ref W value, EntityAction<T, W> action) where T : IEntityStruct;
  97. bool Exists<T>(EGID egid) where T : IEntityStruct;
  98. bool HasAny<T>(int group) where T:IEntityStruct;
  99. bool HasAny<T>(ExclusiveGroup group) where T:IEntityStruct;
  100. }
  101. public delegate void EntityAction<T, W>(ref T target, ref W value);
  102. public delegate void EntityAction<T>(ref T target);
  103. public delegate void AllEntitiesAction<T, W>(ref T target, ref W value, IEntitiesDB entitiesDb);
  104. public delegate void AllEntitiesAction<T>(ref T target, IEntitiesDB entitiesDb);
  105. public delegate void EntitiesAction<T, W>(ref T target, ref W value, IEntitiesDB entitiesDb, int index);
  106. public delegate void EntitiesAction<T>(ref T target, IEntitiesDB entitiesDb, int index);
  107. }