using System; using Svelto.DataStructures; using Svelto.ECS.Internal; namespace Svelto.ECS { public readonly ref struct EntityComponentInitializer { public EntityComponentInitializer(EGID id, FasterDictionary group) { _group = group; _ID = id; } public EGID EGID => _ID; public void Init(T initializer) where T : struct, IEntityComponent { if (_group.TryGetValue(new RefWrapperType(ComponentBuilder.ENTITY_COMPONENT_TYPE), out var typeSafeDictionary) == false) return; var dictionary = (ITypeSafeDictionary) typeSafeDictionary; if (ComponentBuilder.HAS_EGID) SetEGIDWithoutBoxing.SetIDWithoutBoxing(ref initializer, _ID); if (dictionary.TryFindIndex(_ID.entityID, out var findElementIndex)) dictionary.GetDirectValueByRef(findElementIndex) = initializer; } public ref T GetOrCreate() where T : struct, IEntityComponent { ref var entityDictionary = ref _group.GetOrCreate(new RefWrapperType(ComponentBuilder.ENTITY_COMPONENT_TYPE) , TypeSafeDictionaryFactory.Create); var dictionary = (ITypeSafeDictionary) entityDictionary; return ref dictionary.GetOrCreate(_ID.entityID); } public ref T Get() where T : struct, IEntityComponent { return ref (_group[new RefWrapperType(ComponentBuilder.ENTITY_COMPONENT_TYPE)] as ITypeSafeDictionary)[ _ID.entityID]; } public bool Has() where T : struct, IEntityComponent { if (_group.TryGetValue(new RefWrapperType(ComponentBuilder.ENTITY_COMPONENT_TYPE), out var typeSafeDictionary)) { var dictionary = (ITypeSafeDictionary) typeSafeDictionary; if (dictionary.ContainsKey(_ID.entityID)) return true; } return false; } readonly EGID _ID; readonly FasterDictionary _group; } }