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.

178 lines
5.4KB

  1. using System;
  2. using System.Collections.Generic;
  3. using Svelto.DataStructures;
  4. using Svelto.ECS.Experimental.Internal;
  5. namespace Svelto.ECS.Experimental.Internal
  6. {
  7. public interface IStructEntityViewEngine : IEngine
  8. {
  9. void CreateStructEntityViews(SharedStructEntityViewLists sharedStructEntityViewLists);
  10. }
  11. public interface IGroupedStructEntityViewsEngine : IEngine
  12. {
  13. void CreateStructEntityViews(SharedGroupedStructEntityViewsLists sharedStructEntityViewLists);
  14. }
  15. }
  16. namespace Svelto.ECS.Experimental
  17. {
  18. public interface IGroupedEntityView
  19. {
  20. int groupID { get; set; }
  21. }
  22. /// <summary>
  23. /// The engines can receive and store IEntityViews structs
  24. /// Unboxing will happen during the Add, but the
  25. /// data will then be stored and processed as stucts
  26. /// </summary>
  27. public interface IStructEntityViewEngine<T> : IStructEntityViewEngine where T:struct, IEntityStruct
  28. { }
  29. /// <summary>
  30. /// same as above, but the entityViews are grouped by ID
  31. /// usually the ID is the owner of the entityViews of that
  32. /// group
  33. /// </summary>
  34. public interface IGroupedStructEntityViewsEngine<T> : IGroupedStructEntityViewsEngine where T : struct, IGroupedEntityView
  35. {
  36. void Add(ref T entityView);
  37. void Remove(ref T entityView);
  38. }
  39. public sealed class StructEntityViews<T> where T:struct, IEntityStruct
  40. {
  41. public T[] GetList(out int numberOfItems)
  42. {
  43. numberOfItems = _internalList.Count;
  44. return _internalList.ToArrayFast();
  45. }
  46. public StructEntityViews(SharedStructEntityViewLists container)
  47. {
  48. _internalList = SharedStructEntityViewLists.NoVirt.GetList<T>(container);
  49. }
  50. public void Add(T entityView)
  51. {
  52. T convert = (T)entityView;
  53. _internalList.Add(convert);
  54. }
  55. readonly FasterList<T> _internalList;
  56. }
  57. public struct StructGroupEntityViews<T>
  58. where T : struct, IEntityView
  59. {
  60. public StructGroupEntityViews(SharedGroupedStructEntityViewsLists container)
  61. {
  62. _container = container;
  63. indices = new Dictionary<int, int>();
  64. }
  65. public void Add(int groupID, T entityView)
  66. {
  67. T convert = (T)entityView;
  68. var fasterList = (SharedGroupedStructEntityViewsLists.NoVirt.GetList<T>(_container, groupID) as FasterList<T>);
  69. indices[entityView.ID] = fasterList.Count;
  70. fasterList.Add(convert);
  71. }
  72. public void Remove(int groupID, T entityView)
  73. {
  74. var fasterList = (SharedGroupedStructEntityViewsLists.NoVirt.GetList<T>(_container, groupID) as FasterList<T>);
  75. var index = indices[entityView.ID];
  76. indices.Remove(entityView.ID);
  77. if (fasterList.UnorderedRemoveAt(index))
  78. indices[fasterList[index].ID] = index;
  79. }
  80. public T[] GetList(int groupID, out int numberOfItems)
  81. {
  82. var fasterList = (SharedGroupedStructEntityViewsLists.NoVirt.GetList<T>(_container, groupID) as FasterList<T>);
  83. return FasterList<T>.NoVirt.ToArrayFast(fasterList, out numberOfItems);
  84. }
  85. readonly SharedGroupedStructEntityViewsLists _container;
  86. readonly Dictionary<int, int> indices;
  87. }
  88. public class SharedStructEntityViewLists
  89. {
  90. internal SharedStructEntityViewLists()
  91. {
  92. _collection = new Dictionary<Type, IFasterList>();
  93. }
  94. internal static class NoVirt
  95. {
  96. internal static FasterList<T> GetList<T>(SharedStructEntityViewLists obj) where T : struct
  97. {
  98. IFasterList list;
  99. if (obj._collection.TryGetValue(typeof(T), out list))
  100. {
  101. return list as FasterList<T>;
  102. }
  103. list = new FasterList<T>();
  104. obj._collection.Add(typeof(T), list);
  105. return (FasterList<T>)list;
  106. }
  107. }
  108. readonly Dictionary<Type, IFasterList> _collection;
  109. }
  110. public class SharedGroupedStructEntityViewsLists
  111. {
  112. internal SharedGroupedStructEntityViewsLists()
  113. {
  114. _collection = new Dictionary<Type, Dictionary<int, IFasterList>>();
  115. }
  116. internal static class NoVirt
  117. {
  118. internal static IFasterList GetList<T>(SharedGroupedStructEntityViewsLists list, int groupID) where T : struct
  119. {
  120. Dictionary<int, IFasterList> dic = GetGroup<T>(list);
  121. IFasterList localList;
  122. if (dic.TryGetValue(groupID, out localList))
  123. return localList;
  124. localList = new FasterList<T>();
  125. dic.Add(groupID, localList);
  126. return localList;
  127. }
  128. internal static Dictionary<int, IFasterList> GetGroup<T>(SharedGroupedStructEntityViewsLists list) where T : struct
  129. {
  130. Dictionary<int, IFasterList> dic;
  131. if (list._collection.TryGetValue(typeof(T), out dic))
  132. {
  133. return dic;
  134. }
  135. dic = new Dictionary<int, IFasterList>();
  136. list._collection.Add(typeof(T), dic);
  137. return dic;
  138. }
  139. }
  140. readonly Dictionary<Type, Dictionary<int, IFasterList>> _collection;
  141. }
  142. }