Mirror of Svelto.ECS because we're a fan of it
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

67 lignes
2.4KB

  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. in EntityReference reference)
  9. {
  10. _group = group;
  11. _ID = id;
  12. this.reference = reference;
  13. }
  14. public EGID EGID => _ID;
  15. public readonly EntityReference reference;
  16. public void Init<T>(T initializer) where T : struct, IEntityComponent
  17. {
  18. if (_group.TryGetValue(new RefWrapperType(ComponentBuilder<T>.ENTITY_COMPONENT_TYPE),
  19. 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. public ref T GetOrAdd<T>() where T : struct, IEntityComponent
  30. {
  31. ref var entityDictionary = ref _group.GetOrAdd(
  32. new RefWrapperType(ComponentBuilder<T>.ENTITY_COMPONENT_TYPE), TypeSafeDictionaryFactory<T>.Create);
  33. var dictionary = (ITypeSafeDictionary<T>)entityDictionary;
  34. return ref dictionary.GetOrAdd(_ID.entityID);
  35. }
  36. public ref T Get<T>() where T : struct, IEntityComponent
  37. {
  38. return ref (_group[new RefWrapperType(ComponentBuilder<T>.ENTITY_COMPONENT_TYPE)] as ITypeSafeDictionary<T>)
  39. .GetValueByRef(_ID.entityID);
  40. }
  41. public bool Has<T>() where T : struct, IEntityComponent
  42. {
  43. if (_group.TryGetValue(new RefWrapperType(ComponentBuilder<T>.ENTITY_COMPONENT_TYPE),
  44. out var typeSafeDictionary))
  45. {
  46. var dictionary = (ITypeSafeDictionary<T>)typeSafeDictionary;
  47. if (dictionary.ContainsKey(_ID.entityID))
  48. return true;
  49. }
  50. return false;
  51. }
  52. readonly EGID _ID;
  53. readonly FasterDictionary<RefWrapperType, ITypeSafeDictionary> _group;
  54. }
  55. }