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.

42 lines
2.0KB

  1. #if UNITY_NATIVE
  2. using Svelto.ECS.DataStructures;
  3. namespace Svelto.ECS.Native
  4. {
  5. public readonly struct NativeEntityFactory
  6. {
  7. internal NativeEntityFactory(AtomicNativeBags addOperationQueue, int index, EnginesRoot.LocatorMap entityLocator)
  8. {
  9. _index = index;
  10. _addOperationQueue = addOperationQueue;
  11. _entityLocator = entityLocator;
  12. }
  13. public NativeEntityInitializer BuildEntity
  14. (uint eindex, ExclusiveBuildGroup exclusiveBuildGroup, int threadIndex)
  15. {
  16. EntityReference reference = _entityLocator.ClaimReference();
  17. NativeBag unsafeBuffer = _addOperationQueue.GetBuffer(threadIndex + 1);
  18. unsafeBuffer.Enqueue(_index); //each native ECS native operation is stored in an array, each request to perform a native operation in a queue. _index is the index of the operation in the array that will be dequeued later
  19. unsafeBuffer.Enqueue(new EGID(eindex, exclusiveBuildGroup));
  20. unsafeBuffer.Enqueue(reference);
  21. //NativeEntityInitializer is quite a complex beast. It holds the starting values of the component set by the user. These components must be later dequeued and in order to know how many components
  22. //must be dequeued, a count must be used. The space to hold the count is then reserved in the queue and index will be used access the count later on through NativeEntityInitializer so it can increment it.
  23. unsafeBuffer.ReserveEnqueue<uint>(out var index) = 0;
  24. return new NativeEntityInitializer(unsafeBuffer, index, reference);
  25. }
  26. public NativeEntityInitializer BuildEntity(EGID egid, int threadIndex)
  27. {
  28. return BuildEntity(egid.entityID, egid.groupID, threadIndex);
  29. }
  30. readonly EnginesRoot.LocatorMap _entityLocator;
  31. readonly AtomicNativeBags _addOperationQueue;
  32. readonly int _index;
  33. }
  34. }
  35. #endif