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.

62 lines
2.2KB

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