using Svelto.Common; using Svelto.DataStructures; using Svelto.ECS.Internal; 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, _IInternalEntityComponent { if (_group.TryGetValue( new RefWrapperType(TypeCache.type), out var typeSafeDictionary) == false) return; var dictionary = (ITypeSafeDictionary)typeSafeDictionary; #if SLOW_SVELTO_SUBMISSION if (ComponentBuilder.HAS_EGID) SetEGIDWithoutBoxing.SetIDWithoutBoxing(ref initializer, _ID); #endif if (dictionary.TryFindIndex(_ID.entityID, out var findElementIndex)) dictionary.GetDirectValueByRef(findElementIndex) = initializer; } internal ref T GetOrAdd() where T : unmanaged, IEntityComponent { ref var entityDictionary = ref _group.GetOrAdd( new RefWrapperType(ComponentBuilder.ENTITY_COMPONENT_TYPE), () => new UnmanagedTypeSafeDictionary(1)); var dictionary = (ITypeSafeDictionary)entityDictionary; return ref dictionary.GetOrAdd(_ID.entityID); } public ref T Get() where T : struct, _IInternalEntityComponent { return ref (_group[new RefWrapperType(TypeCache.type)] as ITypeSafeDictionary) .GetValueByRef(_ID.entityID); } public bool Has() where T : struct, _IInternalEntityComponent { if (_group.TryGetValue( new RefWrapperType(TypeCache.type), out var typeSafeDictionary)) { var dictionary = (ITypeSafeDictionary)typeSafeDictionary; if (dictionary.ContainsKey(_ID.entityID)) return true; } return false; } readonly EGID _ID; readonly FasterDictionary _group; } }