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.

48 lines
1.6KB

  1. using System;
  2. using Svelto.ECS.Serialization;
  3. namespace Svelto.ECS
  4. {
  5. /// <summary>
  6. /// Inherit from an ExtendibleEntityDescriptor to extend a base entity descriptor that can be used
  7. /// to swap and remove specialized entities from abstract engines
  8. /// </summary>
  9. /// <typeparam name="TType"></typeparam>
  10. public class ExtendibleEntityDescriptor<TType> : IEntityDescriptor where TType : IEntityDescriptor, new()
  11. {
  12. static ExtendibleEntityDescriptor()
  13. {
  14. if (typeof(ISerializableEntityDescriptor).IsAssignableFrom(typeof(TType)))
  15. throw new Exception(
  16. $"SerializableEntityDescriptors cannot be used as base entity descriptor: {typeof(TType)}");
  17. }
  18. public ExtendibleEntityDescriptor(IEntityBuilder[] extraEntities)
  19. {
  20. _dynamicDescriptor = new DynamicEntityDescriptor<TType>(extraEntities);
  21. }
  22. public ExtendibleEntityDescriptor()
  23. {
  24. _dynamicDescriptor = new DynamicEntityDescriptor<TType>(true);
  25. }
  26. public ExtendibleEntityDescriptor<TType> ExtendWith<T>() where T : IEntityDescriptor, new()
  27. {
  28. _dynamicDescriptor.ExtendWith<T>();
  29. return this;
  30. }
  31. public ExtendibleEntityDescriptor<TType> ExtendWith(IEntityBuilder[] extraEntities)
  32. {
  33. _dynamicDescriptor.ExtendWith(extraEntities);
  34. return this;
  35. }
  36. public IEntityBuilder[] entitiesToBuild => _dynamicDescriptor.entitiesToBuild;
  37. DynamicEntityDescriptor<TType> _dynamicDescriptor;
  38. }
  39. }