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.

80 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, _IInternalEntityComponent
  11. {
  12. public int count => _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 Exception(
  26. "Not initialized EGIDMapper in this group ".FastConcat(typeof(T).ToString()));
  27. if (_map.TryFindIndex(entityID, out var findIndex) == false)
  28. throw new Exception("Entity not found in this group ".FastConcat(typeof(T).ToString()));
  29. #else
  30. _map.TryFindIndex(entityID, out var findIndex);
  31. #endif
  32. return ref _map.GetDirectValueByRef(findIndex);
  33. }
  34. public bool TryGetEntity(uint entityID, out T value)
  35. {
  36. if (_map != null && _map.TryFindIndex(entityID, out var index))
  37. {
  38. value = _map.GetDirectValueByRef(index);
  39. return true;
  40. }
  41. value = default;
  42. return false;
  43. }
  44. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  45. public bool Exists(uint idEntityId)
  46. {
  47. return _map.count > 0 && _map.TryFindIndex(idEntityId, out _);
  48. }
  49. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  50. public uint GetIndex(uint entityID)
  51. {
  52. return _map.GetIndex(entityID);
  53. }
  54. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  55. public bool FindIndex(uint valueKey, out uint index)
  56. {
  57. return _map.TryFindIndex(valueKey, out index);
  58. }
  59. internal readonly ITypeSafeDictionary<T> _map;
  60. }
  61. public interface IEGIDMapper
  62. {
  63. bool FindIndex(uint valueKey, out uint index);
  64. uint GetIndex(uint entityID);
  65. bool Exists(uint idEntityId);
  66. ExclusiveGroupStruct groupID { get; }
  67. Type entityType { get; }
  68. }
  69. }