Mirror of Svelto.ECS because we're a fan of it
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

63 Zeilen
2.6KB

  1. #if UNITY_NATIVE
  2. using System;
  3. using Svelto.DataStructures;
  4. using Svelto.DataStructures.Native;
  5. namespace Svelto.ECS.Native
  6. {
  7. /// <summary>
  8. /// Note: this class should really be ref struct by design. It holds the reference of a dictionary that can become
  9. /// invalid. Unfortunately it can be a ref struct, because Jobs needs to hold if by paramater. So the deal is
  10. /// that a job can use it as long as nothing else is modifying the entities database and the NativeEGIDMultiMapper
  11. /// is disposed right after the use.
  12. /// </summary>
  13. public struct NativeEGIDMultiMapper<T> : IDisposable where T : unmanaged, IEntityComponent
  14. {
  15. public NativeEGIDMultiMapper
  16. (SveltoDictionary<ExclusiveGroupStruct,
  17. SveltoDictionary<uint, T, NativeStrategy<SveltoDictionaryNode<uint>>, NativeStrategy<T>,
  18. NativeStrategy<int>>, NativeStrategy<SveltoDictionaryNode<ExclusiveGroupStruct>>,
  19. NativeStrategy<SveltoDictionary<uint, T, NativeStrategy<SveltoDictionaryNode<uint>>, NativeStrategy<T>,
  20. NativeStrategy<int>>>, NativeStrategy<int>> dictionary)
  21. {
  22. _dic = dictionary;
  23. }
  24. public int count => (int) _dic.count;
  25. public void Dispose()
  26. {
  27. _dic.Dispose();
  28. }
  29. public ref T Entity(EGID entity)
  30. {
  31. #if DEBUG && !PROFILE_SVELTO
  32. if (Exists(entity) == false)
  33. throw new Exception("NativeEGIDMultiMapper: Entity not found");
  34. #endif
  35. ref var sveltoDictionary = ref _dic.GetValueByRef(entity.groupID);
  36. return ref sveltoDictionary.GetValueByRef(entity.entityID);
  37. }
  38. public bool Exists(EGID entity)
  39. {
  40. return _dic.TryFindIndex(entity.groupID, out var index)
  41. && _dic.GetDirectValueByRef(index).ContainsKey(entity.entityID);
  42. }
  43. public bool TryGetEntity(EGID entity, out T component)
  44. {
  45. component = default;
  46. return _dic.TryFindIndex(entity.groupID, out var index)
  47. && _dic.GetDirectValueByRef(index).TryGetValue(entity.entityID, out component);
  48. }
  49. SveltoDictionary<ExclusiveGroupStruct,
  50. SveltoDictionary<uint, T, NativeStrategy<SveltoDictionaryNode<uint>>, NativeStrategy<T>,
  51. NativeStrategy<int>>, NativeStrategy<SveltoDictionaryNode<ExclusiveGroupStruct>>,
  52. NativeStrategy<SveltoDictionary<uint, T, NativeStrategy<SveltoDictionaryNode<uint>>, NativeStrategy<T>,
  53. NativeStrategy<int>>>, NativeStrategy<int>> _dic;
  54. }
  55. }
  56. #endif