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.

39 lines
1.5KB

  1. using System;
  2. namespace Svelto.ECS
  3. {
  4. /// <summary>
  5. /// DynamicEntityDescriptor can be used to add entity views to an existing EntityDescriptor that act as flags,
  6. /// at building time.
  7. /// This method allocates, so it shouldn't be abused
  8. /// </summary>
  9. /// <typeparam name="TType"></typeparam>
  10. public struct DynamicEntityDescriptor<TType>:IEntityDescriptor where TType : IEntityDescriptor, new()
  11. {
  12. public DynamicEntityDescriptor(IEntityBuilder[] extraEntities)
  13. {
  14. DBC.ECS.Check.Require(extraEntities.Length > 0,
  15. "don't use a DynamicEntityDescriptorInfo if you don't need to use extra EntityViews");
  16. var defaultEntities = EntityDescriptorTemplate<TType>.descriptor.entitiesToBuild;
  17. var length = defaultEntities.Length;
  18. entitiesToBuild = new IEntityBuilder[length + extraEntities.Length + 1];
  19. Array.Copy(defaultEntities, 0, entitiesToBuild, 0, length);
  20. Array.Copy(extraEntities, 0, entitiesToBuild, length, extraEntities.Length);
  21. var builder = new EntityBuilder<EntityStructInfoView>
  22. {
  23. _initializer = new EntityStructInfoView
  24. {
  25. entitiesToBuild = entitiesToBuild,
  26. type = typeof(TType)
  27. }
  28. };
  29. entitiesToBuild[entitiesToBuild.Length - 1] = builder;
  30. }
  31. public IEntityBuilder[] entitiesToBuild { get; }
  32. }
  33. }