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.

64 lines
2.2KB

  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<ComponentID, 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(ComponentTypeID<T>.id, out var typeSafeDictionary) == false)
  20. return;
  21. var dictionary = (ITypeSafeDictionary<T>)typeSafeDictionary;
  22. #if SLOW_SVELTO_SUBMISSION
  23. if (ComponentBuilder<T>.HAS_EGID)
  24. SetEGIDWithoutBoxing<T>.SetIDWithoutBoxing(ref initializer, _ID);
  25. #endif
  26. if (dictionary.TryFindIndex(_ID.entityID, out var findElementIndex))
  27. dictionary.GetDirectValueByRef(findElementIndex) = initializer;
  28. }
  29. internal ref T GetOrAdd<T>() where T : unmanaged, _IInternalEntityComponent
  30. {
  31. ref var entityDictionary = ref _group.GetOrAdd(ComponentTypeID<T>.id, () => new UnmanagedTypeSafeDictionary<T>(1));
  32. var dictionary = (ITypeSafeDictionary<T>)entityDictionary;
  33. return ref dictionary.GetOrAdd(_ID.entityID);
  34. }
  35. public ref T Get<T>() where T : struct, _IInternalEntityComponent
  36. {
  37. return ref (_group[ComponentTypeID<T>.id] as ITypeSafeDictionary<T>).GetValueByRef(_ID.entityID);
  38. }
  39. public bool Has<T>() where T : struct, _IInternalEntityComponent
  40. {
  41. if (_group.TryGetValue(ComponentTypeID<T>.id, out var typeSafeDictionary))
  42. {
  43. var dictionary = (ITypeSafeDictionary<T>)typeSafeDictionary;
  44. if (dictionary.ContainsKey(_ID.entityID))
  45. return true;
  46. }
  47. return false;
  48. }
  49. readonly EGID _ID;
  50. readonly FasterDictionary<ComponentID, ITypeSafeDictionary> _group;
  51. }
  52. }