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.

82 lines
2.5KB

  1. using System;
  2. using Svelto.ECS.Internal;
  3. namespace Svelto.ECS
  4. {
  5. public interface IEntityViewBuilder
  6. {
  7. void BuildEntityViewAndAddToList(ref ITypeSafeList list, int entityID, out IEntityView entityView);
  8. ITypeSafeList Preallocate(ref ITypeSafeList list, int size);
  9. Type GetEntityViewType();
  10. }
  11. public class EntityViewBuilder<EntityViewType> : IEntityViewBuilder where EntityViewType : EntityView, new()
  12. {
  13. public void BuildEntityViewAndAddToList(ref ITypeSafeList list, int entityID, out IEntityView entityView)
  14. {
  15. if (list == null)
  16. list = new TypeSafeFasterListForECSForClasses<EntityViewType>();
  17. var castedList = list as TypeSafeFasterListForECSForClasses<EntityViewType>;
  18. var lentityView = EntityView<EntityViewType>.BuildEntityView(entityID);
  19. castedList.Add(lentityView);
  20. entityView = lentityView;
  21. }
  22. public ITypeSafeList Preallocate(ref ITypeSafeList list, int size)
  23. {
  24. if (list == null)
  25. list = new TypeSafeFasterListForECSForClasses<EntityViewType>(size);
  26. else
  27. list.ReserveCapacity(size);
  28. return list;
  29. }
  30. public Type GetEntityViewType()
  31. {
  32. return _entityViewType;
  33. }
  34. readonly Type _entityViewType = typeof(EntityViewType);
  35. }
  36. public class EntityStructBuilder<EntityViewType> : IEntityViewBuilder where EntityViewType : struct, IEntityStruct
  37. {
  38. public void BuildEntityViewAndAddToList(ref ITypeSafeList list, int entityID, out IEntityView entityView)
  39. {
  40. var lentityView = default(EntityViewType);
  41. lentityView.ID = entityID;
  42. if (list == null)
  43. list = new TypeSafeFasterListForECSForStructs<EntityViewType>();
  44. var castedList = list as TypeSafeFasterListForECSForStructs<EntityViewType>;
  45. castedList.Add(lentityView);
  46. entityView = null;
  47. }
  48. public ITypeSafeList Preallocate(ref ITypeSafeList list, int size)
  49. {
  50. if (list == null)
  51. list = new TypeSafeFasterListForECSForStructs<EntityViewType>(size);
  52. else
  53. list.ReserveCapacity(size);
  54. return list;
  55. }
  56. public Type GetEntityViewType()
  57. {
  58. return _entityViewType;
  59. }
  60. readonly Type _entityViewType = typeof(EntityViewType);
  61. }
  62. }