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.

EngineEntityViewDB.cs 5.6KB

8 years ago
8 years ago
8 years ago
8 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. using System;
  2. using System.Collections.Generic;
  3. using Svelto.DataStructures;
  4. using Svelto.ECS.Internal;
  5. namespace Svelto.ECS
  6. {
  7. public class EngineEntityViewDB : IEngineEntityViewDB
  8. {
  9. internal EngineEntityViewDB( Dictionary<Type, ITypeSafeList> entityViewsDB,
  10. Dictionary<Type, ITypeSafeDictionary> entityViewsDBdic,
  11. Dictionary<Type, ITypeSafeList> metaEntityViewsDB,
  12. Dictionary<int, Dictionary<Type, ITypeSafeList>> groupEntityViewsDB)
  13. {
  14. _entityViewsDB = entityViewsDB;
  15. _entityViewsDBdic = entityViewsDBdic;
  16. _metaEntityViewsDB = metaEntityViewsDB;
  17. _groupEntityViewsDB = groupEntityViewsDB;
  18. }
  19. public FasterReadOnlyList<T> QueryEntityViews<T>() where T:EntityView, new()
  20. {
  21. var type = typeof(T);
  22. ITypeSafeList entityViews;
  23. if (_entityViewsDB.TryGetValue(type, out entityViews) == false)
  24. return RetrieveEmptyEntityViewList<T>();
  25. return new FasterReadOnlyList<T>((FasterList<T>)entityViews);
  26. }
  27. public FasterReadOnlyList<T> QueryGroupedEntityViews<T>(int @group) where T:EntityView, new()
  28. {
  29. Dictionary<Type, ITypeSafeList> entityViews;
  30. if (_groupEntityViewsDB.TryGetValue(group, out entityViews) == false)
  31. return RetrieveEmptyEntityViewList<T>();
  32. return new FasterReadOnlyList<T>(entityViews as FasterList<T>);
  33. }
  34. public T[] QueryEntityViewsAsArray<T>(out int count) where T : IEntityView
  35. {
  36. var type = typeof(T);
  37. count = 0;
  38. ITypeSafeList entityViews;
  39. if (_entityViewsDB.TryGetValue(type, out entityViews) == false)
  40. return RetrieveEmptyEntityViewArray<T>();
  41. var castedEntityViews = (FasterList<T>)entityViews;
  42. count = castedEntityViews.Count;
  43. return castedEntityViews.ToArrayFast();
  44. }
  45. public T[] QueryGroupedEntityViewsAsArray<T>(int @group, out int count) where T : IEntityView
  46. {
  47. var type = typeof(T);
  48. count = 0;
  49. Dictionary<Type, ITypeSafeList> entityViews;
  50. if (_groupEntityViewsDB.TryGetValue(group, out entityViews) == false)
  51. return RetrieveEmptyEntityViewArray<T>();
  52. var castedEntityViews = (FasterList<T>)entityViews[type];
  53. count = castedEntityViews.Count;
  54. return castedEntityViews.ToArrayFast();
  55. }
  56. public ReadOnlyDictionary<int, T> QueryIndexableEntityViews<T>() where T:IEntityView
  57. {
  58. var type = typeof(T);
  59. ITypeSafeDictionary entityViews;
  60. if (_entityViewsDBdic.TryGetValue(type, out entityViews) == false)
  61. return TypeSafeDictionary<T>.Default;
  62. return new ReadOnlyDictionary<int, T>(entityViews as Dictionary<int, T>);
  63. }
  64. public bool TryQueryEntityView<T>(int ID, out T entityView) where T : IEntityView
  65. {
  66. var type = typeof(T);
  67. T internalEntityView;
  68. ITypeSafeDictionary entityViews;
  69. TypeSafeDictionary<T> casted;
  70. _entityViewsDBdic.TryGetValue(type, out entityViews);
  71. casted = entityViews as TypeSafeDictionary<T>;
  72. if (casted != null &&
  73. casted.TryGetValue(ID, out internalEntityView))
  74. {
  75. entityView = (T)internalEntityView;
  76. return true;
  77. }
  78. entityView = default(T);
  79. return false;
  80. }
  81. public T QueryEntityView<T>(int ID) where T : IEntityView
  82. {
  83. var type = typeof(T);
  84. T internalEntityView; ITypeSafeDictionary entityViews;
  85. TypeSafeDictionary<T> casted;
  86. _entityViewsDBdic.TryGetValue(type, out entityViews);
  87. casted = entityViews as TypeSafeDictionary<T>;
  88. if (casted != null &&
  89. casted.TryGetValue(ID, out internalEntityView))
  90. return (T)internalEntityView;
  91. throw new Exception("EntityView Not Found");
  92. }
  93. public T QueryMetaEntityView<T>(int metaEntityID) where T:EntityView, new()
  94. {
  95. return QueryEntityView<T>(metaEntityID);
  96. }
  97. public bool TryQueryMetaEntityView<T>(int metaEntityID, out T entityView) where T:EntityView, new()
  98. {
  99. return TryQueryEntityView(metaEntityID, out entityView);
  100. }
  101. public FasterReadOnlyList<T> QueryMetaEntityViews<T>() where T:EntityView, new()
  102. {
  103. var type = typeof(T);
  104. ITypeSafeList entityViews;
  105. if (_metaEntityViewsDB.TryGetValue(type, out entityViews) == false)
  106. return RetrieveEmptyEntityViewList<T>();
  107. return new FasterReadOnlyList<T>((FasterList<T>)entityViews);
  108. }
  109. static FasterReadOnlyList<T> RetrieveEmptyEntityViewList<T>()
  110. {
  111. return FasterReadOnlyList<T>.DefaultList;
  112. }
  113. static T[] RetrieveEmptyEntityViewArray<T>()
  114. {
  115. return FasterList<T>.DefaultList.ToArrayFast();
  116. }
  117. readonly Dictionary<Type, ITypeSafeList> _entityViewsDB;
  118. readonly Dictionary<Type, ITypeSafeDictionary> _entityViewsDBdic;
  119. readonly Dictionary<Type, ITypeSafeList> _metaEntityViewsDB;
  120. readonly Dictionary<int, Dictionary<Type, ITypeSafeList>> _groupEntityViewsDB;
  121. }
  122. }