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.

71 lines
1.9KB

  1. using Svelto.DataStructures;
  2. namespace Svelto.ECS
  3. {
  4. internal interface ITypeSafeStream
  5. {
  6. void Dispose();
  7. }
  8. public class EntityStream<T> : ITypeSafeStream where T : unmanaged, IEntityComponent
  9. {
  10. readonly ThreadSafeFasterList<Consumer<T>> _consumers;
  11. internal EntityStream()
  12. {
  13. _consumers = new ThreadSafeFasterList<Consumer<T>>();
  14. }
  15. ~EntityStream()
  16. {
  17. for (var i = 0; i < _consumers.count; i++)
  18. _consumers[i].Free();
  19. }
  20. public void Dispose()
  21. { }
  22. internal void PublishEntity(ref T entity, EGID egid)
  23. {
  24. for (var i = 0; i < _consumers.count; i++)
  25. unsafe
  26. {
  27. if (*(bool*) _consumers[i].mustBeDisposed)
  28. {
  29. _consumers[i].Free();
  30. _consumers.UnorderedRemoveAt((uint) i);
  31. --i;
  32. continue;
  33. }
  34. if (_consumers[i].hasGroup)
  35. {
  36. if (egid.groupID == _consumers[i].@group)
  37. _consumers[i].Enqueue(entity, egid);
  38. }
  39. else
  40. {
  41. _consumers[i].Enqueue(entity, egid);
  42. }
  43. }
  44. }
  45. internal Consumer<T> GenerateConsumer(string name, uint capacity)
  46. {
  47. var consumer = new Consumer<T>(name, capacity);
  48. _consumers.Add(consumer);
  49. return consumer;
  50. }
  51. internal Consumer<T> GenerateConsumer(ExclusiveGroupStruct group, string name, uint capacity)
  52. {
  53. var consumer = new Consumer<T>(group, name, capacity);
  54. _consumers.Add(consumer);
  55. return consumer;
  56. }
  57. }
  58. }