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.

75 lines
2.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. ///
  9. /// Usage Example:
  10. ///
  11. /// class SpecialisedDescriptor : ExtendibleEntityDescriptor<BaseDescriptor>
  12. /// {
  13. /// public SpecialisedDescriptor() : base (new IComponentBuilder[]
  14. /// {
  15. /// new ComponentBuilder<ObjectParentComponent>() //add more components to the base descriptor
  16. /// })
  17. /// {
  18. /// ExtendWith<ContractDescriptor>(); //add extra components from descriptors that act as contract
  19. /// }
  20. /// }
  21. /// </summary>
  22. /// <typeparam name="TType"></typeparam>
  23. public abstract class ExtendibleEntityDescriptor<TType> : IDynamicEntityDescriptor where TType : IEntityDescriptor, new()
  24. {
  25. static ExtendibleEntityDescriptor()
  26. {
  27. if (typeof(ISerializableEntityDescriptor).IsAssignableFrom(typeof(TType)))
  28. throw new Exception(
  29. $"SerializableEntityDescriptors cannot be used as base entity descriptor: {typeof(TType)}");
  30. }
  31. protected ExtendibleEntityDescriptor(IComponentBuilder[] extraEntities)
  32. {
  33. _dynamicDescriptor = new DynamicEntityDescriptor<TType>(extraEntities);
  34. }
  35. protected ExtendibleEntityDescriptor()
  36. {
  37. _dynamicDescriptor = new DynamicEntityDescriptor<TType>(true);
  38. }
  39. protected ExtendibleEntityDescriptor<TType> ExtendWith<T>() where T : IEntityDescriptor, new()
  40. {
  41. _dynamicDescriptor.ExtendWith<T>();
  42. return this;
  43. }
  44. protected ExtendibleEntityDescriptor<TType> ExtendWith(IComponentBuilder[] extraEntities)
  45. {
  46. _dynamicDescriptor.ExtendWith(extraEntities);
  47. return this;
  48. }
  49. protected void Add<T>() where T : struct, IEntityComponent
  50. {
  51. _dynamicDescriptor.Add<T>();
  52. }
  53. protected void Add<T, U>() where T : struct, IEntityComponent where U : struct, IEntityComponent
  54. {
  55. _dynamicDescriptor.Add<T, U>();
  56. }
  57. protected void Add<T, U, V>() where T : struct, IEntityComponent where U : struct, IEntityComponent where V : struct, IEntityComponent
  58. {
  59. _dynamicDescriptor.Add<T, U, V>();
  60. }
  61. public IComponentBuilder[] componentsToBuild => _dynamicDescriptor.componentsToBuild;
  62. DynamicEntityDescriptor<TType> _dynamicDescriptor;
  63. }
  64. }