Advertisement
Guest User

Untitled

a guest
Jul 19th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.67 KB | None | 0 0
  1.     internal class ComponentManager
  2.     {
  3.         private int _wordsPerEntity;
  4.         private int[] _componentHashCodes;
  5.         private ComponentStore<IComponent>[] _componentStores;
  6.         private OpenBitSet _componentBits;
  7.         private int _componentTypeCount;
  8.  
  9.         internal ComponentManager(Type[] componentTypes)
  10.         {
  11.             _componentTypeCount = componentTypes.Length;
  12.             _componentStores = new ComponentStore<IComponent>[_componentTypeCount];
  13.             _componentHashCodes = new int[_componentTypeCount];
  14.             _wordsPerEntity = Math.Max(1, _componentTypeCount / 64);
  15.             _componentBits = new OpenBitSet((_wordsPerEntity * 1024) * 64);
  16.  
  17.             Array.Sort(componentTypes, (x, y) => x.GetHashCode().CompareTo(y.GetHashCode()));
  18.  
  19.             for (int i = 0; i < _componentTypeCount; i++)
  20.             {
  21.                 Type currentType = componentTypes[i];
  22.                 Type storeType = typeof(ComponentStore<>).MakeGenericType(currentType);
  23.  
  24.                 _componentHashCodes[i] = currentType.GetHashCode();
  25.  
  26.                 var currentStore = Activator.CreateInstance(storeType, new object[] { _componentBits, _wordsPerEntity, i });
  27.                 _componentStores[i] = currentStore as ComponentStore<IComponent>;
  28.             }
  29.         }
  30.  
  31.         internal ComponentStore<T> GetStoreFor<T>() where T : IComponent
  32.         {
  33.             int index = GetTypeIndex(typeof(T));
  34.             return _componentStores[index] as ComponentStore<T>;
  35.         }
  36.  
  37.         private int GetTypeIndex(Type componentType)
  38.         {
  39.             return Array.BinarySearch(_componentStores, componentType.GetHashCode());
  40.         }
  41.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement