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.

79 lines
2.5KB

  1. using System;
  2. using System.Runtime.CompilerServices;
  3. using Svelto.Common;
  4. using Svelto.ECS.Internal;
  5. namespace Svelto.ECS
  6. {
  7. /// <summary>
  8. /// </summary>
  9. /// <typeparam name="T"></typeparam>
  10. public readonly struct EGIDMapper<T>: IEGIDMapper where T : struct, IEntityComponent
  11. {
  12. public uint length => _map.count;
  13. public ExclusiveGroupStruct groupID { get; }
  14. public Type entityType => TypeCache<T>.type;
  15. internal EGIDMapper(ExclusiveGroupStruct groupStructId, ITypeSafeDictionary<T> dic) : this()
  16. {
  17. groupID = groupStructId;
  18. _map = dic;
  19. }
  20. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  21. public ref T Entity(uint entityID)
  22. {
  23. #if DEBUG && !PROFILE_SVELTO
  24. if (_map == null)
  25. throw new System.Exception("Not initialized EGIDMapper in this group ".FastConcat(typeof(T).ToString()));
  26. if (_map.TryFindIndex(entityID, out var findIndex) == false)
  27. throw new System.Exception("Entity not found in this group ".FastConcat(typeof(T).ToString()));
  28. #else
  29. _map.TryFindIndex(entityID, out var findIndex);
  30. #endif
  31. return ref _map.GetDirectValueByRef(findIndex);
  32. }
  33. public bool TryGetEntity(uint entityID, out T value)
  34. {
  35. if (_map != null && _map.TryFindIndex(entityID, out var index))
  36. {
  37. value = _map.GetDirectValueByRef(index);
  38. return true;
  39. }
  40. value = default;
  41. return false;
  42. }
  43. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  44. public bool Exists(uint idEntityId)
  45. {
  46. return _map.count > 0 && _map.TryFindIndex(idEntityId, out _);
  47. }
  48. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  49. public uint GetIndex(uint entityID)
  50. {
  51. return _map.GetIndex(entityID);
  52. }
  53. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  54. public bool FindIndex(uint valueKey, out uint index)
  55. {
  56. return _map.TryFindIndex(valueKey, out index);
  57. }
  58. internal readonly ITypeSafeDictionary<T> _map;
  59. }
  60. public interface IEGIDMapper
  61. {
  62. bool FindIndex(uint valueKey, out uint index);
  63. uint GetIndex(uint entityID);
  64. bool Exists(uint idEntityId);
  65. ExclusiveGroupStruct groupID { get; }
  66. Type entityType { get; }
  67. }
  68. }