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.

EntitiesStreams.cs 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. using System;
  2. using Svelto.DataStructures;
  3. namespace Svelto.ECS
  4. {
  5. /// <summary>
  6. /// I eventually realised that, with the ECS design, no form of communication other than polling entity components can
  7. /// exist.
  8. /// Using groups, you can have always an optimal set of entity components to poll. However EntityStreams
  9. /// can be useful if:
  10. /// - you need to react on seldom entity changes, usually due to user events
  11. /// - you want engines to be able to track entity changes
  12. /// - you want a thread-safe way to read entity states, which includes all the state changes and not the last
  13. /// one only
  14. /// - you want to communicate between EnginesRoots
  15. /// </summary>
  16. internal struct EntitiesStreams : IDisposable
  17. {
  18. internal Consumer<T> GenerateConsumer<T>(string name, uint capacity)
  19. where T : unmanaged, IEntityComponent
  20. {
  21. if (_streams.ContainsKey(TypeRefWrapper<T>.wrapper) == false)
  22. _streams[TypeRefWrapper<T>.wrapper] = new EntityStream<T>();
  23. return (_streams[TypeRefWrapper<T>.wrapper] as EntityStream<T>).GenerateConsumer(name, capacity);
  24. }
  25. public Consumer<T> GenerateConsumer<T>(ExclusiveGroupStruct group, string name, uint capacity)
  26. where T : unmanaged, IEntityComponent
  27. {
  28. if (_streams.ContainsKey(TypeRefWrapper<T>.wrapper) == false)
  29. _streams[TypeRefWrapper<T>.wrapper] = new EntityStream<T>();
  30. var typeSafeStream = (EntityStream<T>) _streams[TypeRefWrapper<T>.wrapper];
  31. return typeSafeStream.GenerateConsumer(group, name, capacity);
  32. }
  33. #if later
  34. public ThreadSafeNativeEntityStream<T> GenerateThreadSafePublisher<T>(EntitiesDB entitiesDB) where T: unmanaged, IEntityComponent
  35. {
  36. var threadSafeNativeEntityStream = new ThreadSafeNativeEntityStream<T>(entitiesDB);
  37. _streams[TypeRefWrapper<T>.wrapper] = threadSafeNativeEntityStream;
  38. return threadSafeNativeEntityStream;
  39. }
  40. #endif
  41. internal void PublishEntity<T>(ref T entity, EGID egid) where T : unmanaged, IEntityComponent
  42. {
  43. if (_streams.TryGetValue(TypeRefWrapper<T>.wrapper, out var typeSafeStream))
  44. (typeSafeStream as EntityStream<T>).PublishEntity(ref entity, egid);
  45. else
  46. Console.LogDebug("No Consumers are waiting for this entity to change ", typeof(T));
  47. }
  48. public void Dispose()
  49. {
  50. foreach (var stream in _streams)
  51. stream.Value.Dispose();
  52. }
  53. public static EntitiesStreams Create()
  54. {
  55. var stream = new EntitiesStreams();
  56. stream._streams = ManagedSveltoDictionary<RefWrapperType, ITypeSafeStream>.Create();
  57. return stream;
  58. }
  59. ManagedSveltoDictionary<RefWrapperType, ITypeSafeStream> _streams;
  60. }
  61. }