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.

NativeEGIDMultiMapper.cs 2.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #if UNITY_NATIVE
  2. using System;
  3. using Svelto.DataStructures;
  4. namespace Svelto.ECS.Native
  5. {
  6. public struct NativeEGIDMultiMapper<T> : IDisposable where T : unmanaged, IEntityComponent
  7. {
  8. public NativeEGIDMultiMapper
  9. (SveltoDictionary<ExclusiveGroupStruct,
  10. SveltoDictionary<uint, T, NativeStrategy<SveltoDictionaryNode<uint>>, NativeStrategy<T>,
  11. NativeStrategy<int>>, NativeStrategy<SveltoDictionaryNode<ExclusiveGroupStruct>>,
  12. NativeStrategy<SveltoDictionary<uint, T, NativeStrategy<SveltoDictionaryNode<uint>>, NativeStrategy<T>,
  13. NativeStrategy<int>>>, NativeStrategy<int>> dictionary)
  14. {
  15. _dic = dictionary;
  16. }
  17. public int count => (int) _dic.count;
  18. public void Dispose()
  19. {
  20. _dic.Dispose();
  21. }
  22. public ref T Entity(EGID entity)
  23. {
  24. #if DEBUG && !PROFILE_SVELTO
  25. if (Exists(entity) == false)
  26. throw new Exception("NativeEGIDMultiMapper: Entity not found");
  27. #endif
  28. ref var sveltoDictionary = ref _dic.GetValueByRef(entity.groupID);
  29. return ref sveltoDictionary.GetValueByRef(entity.entityID);
  30. }
  31. public bool Exists(EGID entity)
  32. {
  33. return _dic.TryFindIndex(entity.groupID, out var index)
  34. && _dic.GetDirectValueByRef(index).ContainsKey(entity.entityID);
  35. }
  36. public bool TryGetEntity(EGID entity, out T component)
  37. {
  38. component = default;
  39. return _dic.TryFindIndex(entity.groupID, out var index)
  40. && _dic.GetDirectValueByRef(index).TryGetValue(entity.entityID, out component);
  41. }
  42. SveltoDictionary<ExclusiveGroupStruct,
  43. SveltoDictionary<uint, T, NativeStrategy<SveltoDictionaryNode<uint>>, NativeStrategy<T>,
  44. NativeStrategy<int>>, NativeStrategy<SveltoDictionaryNode<ExclusiveGroupStruct>>,
  45. NativeStrategy<SveltoDictionary<uint, T, NativeStrategy<SveltoDictionaryNode<uint>>, NativeStrategy<T>,
  46. NativeStrategy<int>>>, NativeStrategy<int>> _dic;
  47. }
  48. }
  49. #endif