Mirror of Svelto.ECS because we're a fan of it
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

65 linhas
2.4KB

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