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.

81 lines
2.6KB

  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. /// Note: does mono devirtualize sealed classes? If so it could be worth to use TypeSafeDictionary instead of
  9. /// the interface
  10. /// </summary>
  11. /// <typeparam name="T"></typeparam>
  12. public readonly struct EGIDMapper<T>: IEGIDMapper where T : struct, IEntityComponent
  13. {
  14. public uint length => _map.count;
  15. public ExclusiveGroupStruct groupID { get; }
  16. public Type entityType => TypeCache<T>.type;
  17. internal EGIDMapper(ExclusiveGroupStruct groupStructId, ITypeSafeDictionary<T> dic) : this()
  18. {
  19. groupID = groupStructId;
  20. _map = dic;
  21. }
  22. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  23. public ref T Entity(uint entityID)
  24. {
  25. #if DEBUG && !PROFILE_SVELTO
  26. if (_map == null)
  27. throw new System.Exception("Not initialized EGIDMapper in this group ".FastConcat(typeof(T).ToString()));
  28. if (_map.TryFindIndex(entityID, out var findIndex) == false)
  29. throw new System.Exception("Entity not found in this group ".FastConcat(typeof(T).ToString()));
  30. #else
  31. _map.TryFindIndex(entityID, out var findIndex);
  32. #endif
  33. return ref _map.GetDirectValueByRef(findIndex);
  34. }
  35. public bool TryGetEntity(uint entityID, out T value)
  36. {
  37. if (_map != null && _map.TryFindIndex(entityID, out var index))
  38. {
  39. value = _map.GetDirectValueByRef(index);
  40. return true;
  41. }
  42. value = default;
  43. return false;
  44. }
  45. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  46. public bool Exists(uint idEntityId)
  47. {
  48. return _map.count > 0 && _map.TryFindIndex(idEntityId, out _);
  49. }
  50. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  51. public uint GetIndex(uint entityID)
  52. {
  53. return _map.GetIndex(entityID);
  54. }
  55. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  56. public bool FindIndex(uint valueKey, out uint index)
  57. {
  58. return _map.TryFindIndex(valueKey, out index);
  59. }
  60. internal readonly ITypeSafeDictionary<T> _map;
  61. }
  62. public interface IEGIDMapper
  63. {
  64. bool FindIndex(uint valueKey, out uint index);
  65. uint GetIndex(uint entityID);
  66. bool Exists(uint idEntityId);
  67. ExclusiveGroupStruct groupID { get; }
  68. Type entityType { get; }
  69. }
  70. }