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.

67 lines
2.4KB

  1. using Svelto.DataStructures;
  2. using Svelto.ECS.Internal;
  3. using Svelto.ECS.Reference;
  4. namespace Svelto.ECS
  5. {
  6. public readonly ref struct EntityInitializer
  7. {
  8. public EntityInitializer
  9. (EGID id, FasterDictionary<RefWrapperType, ITypeSafeDictionary> group, 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, IEntityComponent
  18. {
  19. if (_group.TryGetValue(new RefWrapperType(ComponentBuilder<T>.ENTITY_COMPONENT_TYPE)
  20. , out var typeSafeDictionary) == false)
  21. return;
  22. var dictionary = (ITypeSafeDictionary<T>) typeSafeDictionary;
  23. if (ComponentBuilder<T>.HAS_EGID)
  24. SetEGIDWithoutBoxing<T>.SetIDWithoutBoxing(ref initializer, _ID);
  25. if (dictionary.TryFindIndex(_ID.entityID, out var findElementIndex))
  26. dictionary.GetDirectValueByRef(findElementIndex) = initializer;
  27. }
  28. public ref T GetOrCreate<T>() where T : struct, IEntityComponent
  29. {
  30. ref var entityDictionary = ref _group.GetOrCreate(
  31. new RefWrapperType(ComponentBuilder<T>.ENTITY_COMPONENT_TYPE), TypeSafeDictionaryFactory<T>.Create);
  32. var dictionary = (ITypeSafeDictionary<T>) entityDictionary;
  33. return ref dictionary.GetOrCreate(_ID.entityID);
  34. }
  35. public ref T Get<T>() where T : struct, IEntityComponent
  36. {
  37. return ref (_group[new RefWrapperType(ComponentBuilder<T>.ENTITY_COMPONENT_TYPE)] as ITypeSafeDictionary<T>)
  38. [_ID.entityID];
  39. }
  40. public bool Has<T>() where T : struct, IEntityComponent
  41. {
  42. if (_group.TryGetValue(new RefWrapperType(ComponentBuilder<T>.ENTITY_COMPONENT_TYPE)
  43. , out var typeSafeDictionary))
  44. {
  45. var dictionary = (ITypeSafeDictionary<T>) typeSafeDictionary;
  46. if (dictionary.ContainsKey(_ID.entityID))
  47. return true;
  48. }
  49. return false;
  50. }
  51. readonly EGID _ID;
  52. readonly FasterDictionary<RefWrapperType, ITypeSafeDictionary> _group;
  53. }
  54. }