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.

71 lines
2.4KB

  1. using Svelto.Common;
  2. using Svelto.DataStructures;
  3. using Svelto.ECS.Internal;
  4. namespace Svelto.ECS
  5. {
  6. public readonly ref struct EntityInitializer
  7. {
  8. public EntityInitializer(EGID id, FasterDictionary<RefWrapperType, ITypeSafeDictionary> group,
  9. in EntityReference reference)
  10. {
  11. _group = group;
  12. _ID = id;
  13. this.reference = reference;
  14. }
  15. public EGID EGID => _ID;
  16. public readonly EntityReference reference;
  17. public void Init<T>(T initializer) where T : struct, _IInternalEntityComponent
  18. {
  19. if (_group.TryGetValue(
  20. new RefWrapperType(TypeCache<T>.type),
  21. out var typeSafeDictionary) == false)
  22. return;
  23. var dictionary = (ITypeSafeDictionary<T>)typeSafeDictionary;
  24. #if SLOW_SVELTO_SUBMISSION
  25. if (ComponentBuilder<T>.HAS_EGID)
  26. SetEGIDWithoutBoxing<T>.SetIDWithoutBoxing(ref initializer, _ID);
  27. #endif
  28. if (dictionary.TryFindIndex(_ID.entityID, out var findElementIndex))
  29. dictionary.GetDirectValueByRef(findElementIndex) = initializer;
  30. }
  31. internal ref T GetOrAdd<T>() where T : unmanaged, IEntityComponent
  32. {
  33. ref var entityDictionary = ref _group.GetOrAdd(
  34. new RefWrapperType(ComponentBuilder<T>.ENTITY_COMPONENT_TYPE),
  35. () => new UnmanagedTypeSafeDictionary<T>(1));
  36. var dictionary = (ITypeSafeDictionary<T>)entityDictionary;
  37. return ref dictionary.GetOrAdd(_ID.entityID);
  38. }
  39. public ref T Get<T>() where T : struct, _IInternalEntityComponent
  40. {
  41. return ref (_group[new RefWrapperType(TypeCache<T>.type)] as ITypeSafeDictionary<T>)
  42. .GetValueByRef(_ID.entityID);
  43. }
  44. public bool Has<T>() where T : struct, _IInternalEntityComponent
  45. {
  46. if (_group.TryGetValue(
  47. new RefWrapperType(TypeCache<T>.type),
  48. out var typeSafeDictionary))
  49. {
  50. var dictionary = (ITypeSafeDictionary<T>)typeSafeDictionary;
  51. if (dictionary.ContainsKey(_ID.entityID))
  52. return true;
  53. }
  54. return false;
  55. }
  56. readonly EGID _ID;
  57. readonly FasterDictionary<RefWrapperType, ITypeSafeDictionary> _group;
  58. }
  59. }