Mirror of Svelto.ECS because we're a fan of it
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

75 lines
2.8KB

  1. using Svelto.ECS.Internal;
  2. namespace Svelto.ECS
  3. {
  4. /// <summary>
  5. /// Inherit from an ExtendibleEntityDescriptor to extend a base entity descriptor that can be used
  6. /// to swap and remove specialized entities from abstract engines
  7. ///
  8. /// Usage Example:
  9. ///
  10. /// class SpecialisedDescriptor : ExtendibleEntityDescriptor<BaseDescriptor>
  11. /// {
  12. /// public SpecialisedDescriptor() : base (new IComponentBuilder[]
  13. /// {
  14. /// new ComponentBuilder<ObjectParentComponent>() //add more components to the base descriptor
  15. /// })
  16. /// {
  17. /// ExtendWith<ContractDescriptor>(); //add extra components from descriptors that act as contract
  18. /// }
  19. /// }
  20. /// </summary>
  21. /// <typeparam name="TType"></typeparam>
  22. public abstract class ExtendibleEntityDescriptor<TType> : IDynamicEntityDescriptor where TType : IEntityDescriptor, new()
  23. {
  24. static ExtendibleEntityDescriptor()
  25. {
  26. //I am removing this check because in reality there is not a strong reason to forbid it and
  27. //furthermore it's already possible to extend a SerializableEntityDescriptor through DynamicEntityDescriptor
  28. // if (typeof(ISerializableEntityDescriptor).IsAssignableFrom(typeof(TType)))
  29. // throw new Exception(
  30. // $"SerializableEntityDescriptors cannot be used as base entity descriptor: {typeof(TType)}");
  31. }
  32. protected ExtendibleEntityDescriptor(IComponentBuilder[] extraEntities)
  33. {
  34. _dynamicDescriptor = new DynamicEntityDescriptor<TType>(extraEntities);
  35. }
  36. protected ExtendibleEntityDescriptor()
  37. {
  38. _dynamicDescriptor = DynamicEntityDescriptor<TType>.CreateDynamicEntityDescriptor();
  39. }
  40. protected ExtendibleEntityDescriptor<TType> ExtendWith<T>() where T : IEntityDescriptor, new()
  41. {
  42. _dynamicDescriptor.ExtendWith<T>();
  43. return this;
  44. }
  45. protected ExtendibleEntityDescriptor<TType> ExtendWith(IComponentBuilder[] extraEntities)
  46. {
  47. _dynamicDescriptor.ExtendWith(extraEntities);
  48. return this;
  49. }
  50. protected void Add<T>() where T : struct, _IInternalEntityComponent
  51. {
  52. _dynamicDescriptor.Add<T>();
  53. }
  54. protected void Add<T, U>() where T : struct, _IInternalEntityComponent where U : struct, _IInternalEntityComponent
  55. {
  56. _dynamicDescriptor.Add<T, U>();
  57. }
  58. protected void Add<T, U, V>() where T : struct, _IInternalEntityComponent where U : struct, _IInternalEntityComponent where V : struct, _IInternalEntityComponent
  59. {
  60. _dynamicDescriptor.Add<T, U, V>();
  61. }
  62. public IComponentBuilder[] componentsToBuild => _dynamicDescriptor.componentsToBuild;
  63. DynamicEntityDescriptor<TType> _dynamicDescriptor;
  64. }
  65. }