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.

36 lines
1.1KB

  1. using System;
  2. using System.Collections.Generic;
  3. using Svelto.ECS.Internal;
  4. namespace Svelto.ECS
  5. {
  6. public struct EntityStructInitializer
  7. {
  8. public EntityStructInitializer(EGID id, Dictionary<Type, ITypeSafeDictionary> @group)
  9. {
  10. _group = @group;
  11. ID = id;
  12. }
  13. public void Init<T>(T initializer) where T: struct, IEntityStruct
  14. {
  15. if (_group.TryGetValue(EntityBuilder<T>.ENTITY_VIEW_TYPE, out var typeSafeDictionary) == true)
  16. {
  17. var dictionary = typeSafeDictionary as TypeSafeDictionary<T>;
  18. if (EntityBuilder<T>.HAS_EGID)
  19. {
  20. var needEgid = ((INeedEGID) initializer);
  21. needEgid.ID = ID;
  22. initializer = (T) needEgid;
  23. }
  24. if (dictionary.TryFindIndex(ID.entityID, out var findElementIndex))
  25. dictionary.GetDirectValue(findElementIndex) = initializer;
  26. }
  27. }
  28. readonly EGID ID;
  29. readonly Dictionary<Type, ITypeSafeDictionary> _group;
  30. }
  31. }