Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- internal class ComponentManager
- {
- private int _wordsPerEntity;
- private int[] _componentHashCodes;
- private ComponentStore<IComponent>[] _componentStores;
- private OpenBitSet _componentBits;
- private int _componentTypeCount;
- internal ComponentManager(Type[] componentTypes)
- {
- _componentTypeCount = componentTypes.Length;
- _componentStores = new ComponentStore<IComponent>[_componentTypeCount];
- _componentHashCodes = new int[_componentTypeCount];
- _wordsPerEntity = Math.Max(1, _componentTypeCount / 64);
- _componentBits = new OpenBitSet((_wordsPerEntity * 1024) * 64);
- Array.Sort(componentTypes, (x, y) => x.GetHashCode().CompareTo(y.GetHashCode()));
- for (int i = 0; i < _componentTypeCount; i++)
- {
- Type currentType = componentTypes[i];
- Type storeType = typeof(ComponentStore<>).MakeGenericType(currentType);
- _componentHashCodes[i] = currentType.GetHashCode();
- var currentStore = Activator.CreateInstance(storeType, new object[] { _componentBits, _wordsPerEntity, i });
- _componentStores[i] = currentStore as ComponentStore<IComponent>;
- }
- }
- internal ComponentStore<T> GetStoreFor<T>() where T : IComponent
- {
- int index = GetTypeIndex(typeof(T));
- return _componentStores[index] as ComponentStore<T>;
- }
- private int GetTypeIndex(Type componentType)
- {
- return Array.BinarySearch(_componentStores, componentType.GetHashCode());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement