Mirror of Svelto.ECS because we're a fan of it
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

73 rindas
2.7KB

  1. namespace Svelto.ECS
  2. {
  3. /// <summary>
  4. /// Inherit from an ExtendibleEntityDescriptor to extend a base entity descriptor that can be used
  5. /// to swap and remove specialized entities from abstract engines
  6. ///
  7. /// Usage Example:
  8. ///
  9. /// class SpecialisedDescriptor : ExtendibleEntityDescriptor<BaseDescriptor>
  10. /// {
  11. /// public SpecialisedDescriptor() : base (new IComponentBuilder[]
  12. /// {
  13. /// new ComponentBuilder<ObjectParentComponent>() //add more components to the base descriptor
  14. /// })
  15. /// {
  16. /// ExtendWith<ContractDescriptor>(); //add extra components from descriptors that act as contract
  17. /// }
  18. /// }
  19. /// </summary>
  20. /// <typeparam name="TType"></typeparam>
  21. public abstract class ExtendibleEntityDescriptor<TType> : IDynamicEntityDescriptor where TType : IEntityDescriptor, new()
  22. {
  23. static ExtendibleEntityDescriptor()
  24. {
  25. //I am removing this check because in reality there is not a strong reason to forbid it and
  26. //furthermore it's already possible to extend a SerializableEntityDescriptor through DynamicEntityDescriptor
  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. }