using Svelto.DataStructures; using Svelto.ECS.Internal; using Svelto.ECS.Reference; namespace Svelto.ECS { public readonly ref struct EntityInitializer { public EntityInitializer (EGID id, FasterDictionary group, in EntityReference reference) { _group = group; _ID = id; this.reference = reference; } public EGID EGID => _ID; public readonly EntityReference reference; 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; } }