diff --git a/Svelto.ECS/DataStructures/TypeSafeDictionary.cs b/Svelto.ECS/DataStructures/TypeSafeDictionary.cs index 7f18c92..c739607 100644 --- a/Svelto.ECS/DataStructures/TypeSafeDictionary.cs +++ b/Svelto.ECS/DataStructures/TypeSafeDictionary.cs @@ -13,21 +13,24 @@ namespace Svelto.ECS.Internal /// public interface ITypeSafeDictionary { - void RemoveEntityFromDicAndEngines(EGID entityGid, + void RemoveEntitiesFromEngines(Dictionary> + entityViewEnginesDB); + + void RemoveEntityFromEngines(EGID entityGid, Dictionary> entityViewEnginesDB); - void RemoveEntityViewsFromEngines( - Dictionary> entityViewEngines); void AddCapacity(int size); - bool Remove(long idGid); + bool Remove(int idGid); ITypeSafeDictionary Create(); int Count { get; } void FillWithIndexedEntityViews(ITypeSafeDictionary entityViews); void AddEntityViewsToEngines(Dictionary> entityViewEnginesDB); } - class TypeSafeDictionary : FasterDictionary, ITypeSafeDictionary where TValue : IEntityData + class TypeSafeDictionary : FasterDictionary, ITypeSafeDictionary where TValue : IEntityData { + static Type _type = typeof(TValue); + public TypeSafeDictionary(int size):base(size) {} @@ -45,7 +48,7 @@ namespace Svelto.ECS.Internal { var entityView = buffer[i]; - Add(entityView.ID.GID, entityView); + Add(entityView.ID.entityID, entityView); } } catch (Exception e) @@ -70,42 +73,36 @@ namespace Svelto.ECS.Internal void AddEntityViewFromEngines(Dictionary> entityViewEnginesDB, ref TValue entity) { FasterList entityViewsEngines; - if (entityViewEnginesDB.TryGetValue(typeof(TValue), out entityViewsEngines)) + //get all the engines linked to TValue + if (entityViewEnginesDB.TryGetValue(_type, out entityViewsEngines)) for (int i = 0; i < entityViewsEngines.Count; i++) (entityViewsEngines[i] as IHandleEntityStructEngine).AddInternal(ref entity); } - public void RemoveEntityFromDicAndEngines(EGID entityGid, + public void RemoveEntityFromEngines(EGID entityGid, Dictionary> entityViewEnginesDB) { - - - TValue entity = this[entityGid.GID]; - - RemoveEntityViewFromEngines(entityViewEnginesDB, ref entity); - - Remove(entityGid.GID); + RemoveEntityViewFromEngines(entityViewEnginesDB, ref GetFasterValuesBuffer()[GetValueIndex(entityGid.entityID)]); } - static void RemoveEntityViewFromEngines(Dictionary> entityViewEnginesDB, ref TValue entity) + static void RemoveEntityViewFromEngines + (Dictionary> entityViewEnginesDB, ref TValue entity) { FasterList entityViewsEngines; - if (entityViewEnginesDB.TryGetValue(typeof(TValue), out entityViewsEngines)) + if (entityViewEnginesDB.TryGetValue(_type, out entityViewsEngines)) for (int i = 0; i < entityViewsEngines.Count; i++) (entityViewsEngines[i] as IHandleEntityStructEngine).RemoveInternal(ref entity); } - public void RemoveEntityViewsFromEngines(Dictionary> entityViewEnginesDB) + public void RemoveEntitiesFromEngines(Dictionary> entityViewEnginesDB) { int count; TValue[] values = GetFasterValuesBuffer(out count); for (int i = 0; i < count; i++) { - TValue entity = values[i]; - - RemoveEntityViewFromEngines(entityViewEnginesDB, ref entity); + RemoveEntityViewFromEngines(entityViewEnginesDB, ref values[i]); } } diff --git a/Svelto.ECS/EnginesRootEngines.cs b/Svelto.ECS/EnginesRootEngines.cs index cef158c..96905c4 100644 --- a/Svelto.ECS/EnginesRootEngines.cs +++ b/Svelto.ECS/EnginesRootEngines.cs @@ -34,15 +34,15 @@ namespace Svelto.ECS /// public EnginesRoot(EntitySubmissionScheduler entityViewScheduler) { - _entityViewEngines = new Dictionary>(); + _entityEngines = new Dictionary>(); _otherEngines = new FasterList(); - _groupEntityViewsDB = new Dictionary>(); - _groupEntityViewsDB[ExclusiveGroups.StandardEntity] = new Dictionary(); + _groupEntityDB = new Dictionary>(); + _groupEntityDB[ExclusiveGroups.StandardEntity] = new Dictionary(); - _groupedEntityViewsToAdd = new DoubleBufferedEntityViews>>(); + _groupedEntityToAdd = new DoubleBufferedEntityViews>>(); - _DB = new EntityViewsDB(_groupEntityViewsDB); + _DB = new EntityViewsDB(_groupEntityDB); _scheduler = entityViewScheduler; _scheduler.Schedule(new WeakAction(SubmitEntityViews)); @@ -78,7 +78,7 @@ namespace Svelto.ECS { var genericArguments = baseType.GetGenericArgumentsEx(); - AddEngine(engine as IHandleEntityViewEngineAbstracted, genericArguments, _entityViewEngines); + AddEngine(engine as IHandleEntityViewEngineAbstracted, genericArguments, _entityEngines); return; } @@ -115,11 +115,9 @@ namespace Svelto.ECS list.Add(engine); } - readonly Dictionary> _entityViewEngines; + readonly Dictionary> _entityEngines; readonly FasterList _otherEngines; - static readonly Type _entityViewType= typeof(IEntityData); static readonly Type _objectType = typeof(object); - static readonly Type _valueType = typeof(ValueType); } } \ No newline at end of file diff --git a/Svelto.ECS/EnginesRootEntities.cs b/Svelto.ECS/EnginesRootEntities.cs index 0c90b61..a905daf 100644 --- a/Svelto.ECS/EnginesRootEntities.cs +++ b/Svelto.ECS/EnginesRootEntities.cs @@ -18,9 +18,9 @@ namespace Svelto.ECS /// public void Dispose() { - foreach (var groups in _groupEntityViewsDB) + foreach (var groups in _groupEntityDB) foreach (var entityList in groups.Value) - entityList.Value.RemoveEntityViewsFromEngines(_entityViewEngines); + entityList.Value.RemoveEntitiesFromEngines(_entityEngines); } ///-------------------------------------------- @@ -42,7 +42,7 @@ namespace Svelto.ECS where T : IEntityDescriptor, new() { EntityFactory.BuildGroupedEntityViews(entityID, - _groupedEntityViewsToAdd.current, + _groupedEntityToAdd.current, EntityDescriptorTemplate.Info, implementors); } @@ -51,7 +51,7 @@ namespace Svelto.ECS object[] implementors) { EntityFactory.BuildGroupedEntityViews(entityID, - _groupedEntityViewsToAdd.current, + _groupedEntityToAdd.current, entityDescriptorInfo, implementors); } @@ -72,23 +72,23 @@ namespace Svelto.ECS for (var index = 0; index < count; index++) { var entityViewBuilder = entityViewsToBuild[index]; - var entityViewType = entityViewBuilder.GetEntityViewType(); + var entityViewType = entityViewBuilder.GetEntityType(); //reserve space for the global pool ITypeSafeDictionary dbList; //reserve space for the single group Dictionary @group; - if (_groupEntityViewsDB.TryGetValue(groupID, out group) == false) - group = _groupEntityViewsDB[groupID] = new Dictionary(); + if (_groupEntityDB.TryGetValue(groupID, out group) == false) + group = _groupEntityDB[groupID] = new Dictionary(); if (group.TryGetValue(entityViewType, out dbList) == false) group[entityViewType] = entityViewBuilder.Preallocate(ref dbList, size); else dbList.AddCapacity(size); - if (_groupedEntityViewsToAdd.current.TryGetValue(groupID, out group) == false) - group = _groupEntityViewsDB[groupID] = new Dictionary(); + if (_groupedEntityToAdd.current.TryGetValue(groupID, out group) == false) + group = _groupEntityDB[groupID] = new Dictionary(); //reserve space to the temporary buffer if (group.TryGetValue(entityViewType, out dbList) == false) @@ -102,36 +102,39 @@ namespace Svelto.ECS /// void RemoveEntity(EGID entityGID) { - var typeSafeDictionary = _groupEntityViewsDB[entityGID.groupID][typeof(EntityInfoView)] as TypeSafeDictionary; - var entityInfoView = typeSafeDictionary[entityGID.GID]; - var entityViewBuilders = entityInfoView.entityViewsToBuild; - var entityViewBuildersCount = entityViewBuilders.Length; + var entityViewInfoDictionary = _groupEntityDB[entityGID.groupID][_typeEntityInfoView] as TypeSafeDictionary; + var entityInfoView = entityViewInfoDictionary[entityGID.entityID]; + var entityBuilders = entityInfoView.entityToBuild; + var entityBuildersCount = entityBuilders.Length; //for each entity view generated by the entity descriptor - for (var i = 0; i < entityViewBuildersCount; i++) + for (var i = 0; i < entityBuildersCount; i++) { - var entityViewType = entityViewBuilders[i].GetEntityViewType(); - var group = _groupEntityViewsDB[entityGID.groupID]; + var entityType = entityBuilders[i].GetEntityType(); + var group = _groupEntityDB[entityGID.groupID]; + + var groupedEntities = _groupEntityDB[entityGID.groupID]; + var typeSafeDictionary = groupedEntities[entityType]; + + typeSafeDictionary.RemoveEntityFromEngines(entityGID, _entityEngines); - _groupEntityViewsDB[entityGID.groupID][entityViewType].RemoveEntityFromDicAndEngines(entityGID, _entityViewEngines); - RemoveEntityViewFromGroup(group, entityViewType, entityGID); + RemoveEntityFromGroup(group, entityType, entityGID); } } - static void RemoveEntityViewFromGroup(Dictionary @group, Type entityViewType, EGID id) + static void RemoveEntityFromGroup(Dictionary @group, Type entityType, EGID id) { - //remove it from entity views group DB - var typeSafeList = @group[entityViewType]; - if (typeSafeList.Remove(id.GID) == false) //clean up - @group.Remove(entityViewType); + var typeSafeList = @group[entityType]; + if (typeSafeList.Remove(id.entityID) == false) //clean up + @group.Remove(entityType); } void RemoveGroupAndEntitiesFromDB(int groupID) { - foreach (var entiTypeSafeList in _groupEntityViewsDB[groupID]) - entiTypeSafeList.Value.RemoveEntityViewsFromEngines(_entityViewEngines); + foreach (var entiTypeSafeList in _groupEntityDB[groupID]) + entiTypeSafeList.Value.RemoveEntitiesFromEngines(_entityEngines); - _groupEntityViewsDB.Remove(groupID); + _groupEntityDB.Remove(groupID); } ///-------------------------------------------- @@ -143,24 +146,24 @@ namespace Svelto.ECS var entityegid = new EGID(entityID, fromGroupID); var entityViewBuilders = - ((TypeSafeDictionary) _groupEntityViewsDB[fromGroupID][typeof(EntityInfoView)]) - [entityegid.GID].entityViewsToBuild; + ((TypeSafeDictionary) _groupEntityDB[fromGroupID][_typeEntityInfoView]) + [entityegid.entityID].entityToBuild; var entityViewBuildersCount = entityViewBuilders.Length; - var groupedEntities = _groupEntityViewsDB[fromGroupID]; + var groupedEntities = _groupEntityDB[fromGroupID]; Dictionary groupedEntityViewsTyped; - if (_groupEntityViewsDB.TryGetValue(toGroupID, out groupedEntityViewsTyped) == false) + if (_groupEntityDB.TryGetValue(toGroupID, out groupedEntityViewsTyped) == false) { groupedEntityViewsTyped = new Dictionary(); - _groupEntityViewsDB.Add(toGroupID, groupedEntityViewsTyped); + _groupEntityDB.Add(toGroupID, groupedEntityViewsTyped); } for (var i = 0; i < entityViewBuildersCount; i++) { var entityViewBuilder = entityViewBuilders[i]; - var entityViewType = entityViewBuilder.GetEntityViewType(); + var entityViewType = entityViewBuilder.GetEntityType(); var fromSafeList = groupedEntities[entityViewType]; ITypeSafeDictionary toSafeList; @@ -169,13 +172,14 @@ namespace Svelto.ECS groupedEntityViewsTyped[entityViewType] = toSafeList = fromSafeList.Create(); entityViewBuilder.MoveEntityView(entityegid, fromSafeList, toSafeList); - fromSafeList.Remove(entityegid.GID); + fromSafeList.Remove(entityegid.entityID); } } readonly EntityViewsDB _DB; //grouped set of entity views, this is the standard way to handle entity views - readonly Dictionary> _groupEntityViewsDB; + readonly Dictionary> _groupEntityDB; + static readonly Type _typeEntityInfoView = typeof(EntityInfoView); } } \ No newline at end of file diff --git a/Svelto.ECS/EnginesRootSubmission.cs b/Svelto.ECS/EnginesRootSubmission.cs index f98dd21..3b6ce38 100644 --- a/Svelto.ECS/EnginesRootSubmission.cs +++ b/Svelto.ECS/EnginesRootSubmission.cs @@ -13,7 +13,7 @@ namespace Svelto.ECS { void SubmitEntityViews() { - bool newEntityViewsHaveBeenAddedWhileIterating = _groupedEntityViewsToAdd.current.Count > 0; + bool newEntityViewsHaveBeenAddedWhileIterating = _groupedEntityToAdd.current.Count > 0; int numberOfReenteringLoops = 0; @@ -21,16 +21,16 @@ namespace Svelto.ECS { //use other as source from now on //current will be use to write new entityViews - _groupedEntityViewsToAdd.Swap(); + _groupedEntityToAdd.Swap(); - if (_groupedEntityViewsToAdd.other.Count > 0) - AddEntityViewsToTheDBAndSuitableEngines(_groupedEntityViewsToAdd.other); + if (_groupedEntityToAdd.other.Count > 0) + AddEntityViewsToTheDBAndSuitableEngines(_groupedEntityToAdd.other); //other can be cleared now - _groupedEntityViewsToAdd.other.Clear(); + _groupedEntityToAdd.other.Clear(); //has current new entityViews? - newEntityViewsHaveBeenAddedWhileIterating = _groupedEntityViewsToAdd.current.Count > 0; + newEntityViewsHaveBeenAddedWhileIterating = _groupedEntityToAdd.current.Count > 0; if (numberOfReenteringLoops > 5) throw new Exception("possible infinite loop found creating Entities inside IEntityViewsEngine Add method, please consider building entities outside IEntityViewsEngine Add method"); @@ -40,26 +40,27 @@ namespace Svelto.ECS } //todo: groupsToSubmit can be semplified as data structure? - void AddEntityViewsToTheDBAndSuitableEngines(ITypeSafeDictionary> groupsOfEntitiesToSubmit) + void AddEntityViewsToTheDBAndSuitableEngines(Dictionary> groupsOfEntitiesToSubmit) { - //for each groups there is a dictionary of built lists of EntityView grouped by type + //each group is indexed by entity view type. for each type there is a dictionary indexed by entityID foreach (var groupOfEntitiesToSubmit in groupsOfEntitiesToSubmit) { Dictionary groupDB; int groupID = groupOfEntitiesToSubmit.Key; //if the group doesn't exist in the current DB let's create it first - if (_groupEntityViewsDB.TryGetValue(groupID, out groupDB) == false) - groupDB = _groupEntityViewsDB[groupID] = new Dictionary(); + if (_groupEntityDB.TryGetValue(groupID, out groupDB) == false) + groupDB = _groupEntityDB[groupID] = new Dictionary(); - //add the entity View in the group - foreach (var entityViewList in groupOfEntitiesToSubmit.Value) + //add the entityViews in the group + foreach (var entityViewTypeSafeDictionary in groupOfEntitiesToSubmit.Value) { - ITypeSafeDictionary dbList; - if (groupDB.TryGetValue(entityViewList.Key, out dbList) == false) - dbList = groupDB[entityViewList.Key] = entityViewList.Value.Create(); + ITypeSafeDictionary dbDic; + if (groupDB.TryGetValue(entityViewTypeSafeDictionary.Key, out dbDic) == false) + dbDic = groupDB[entityViewTypeSafeDictionary.Key] = entityViewTypeSafeDictionary.Value.Create(); - dbList.FillWithIndexedEntityViews(entityViewList.Value); + //type safe copy + dbDic.FillWithIndexedEntityViews(entityViewTypeSafeDictionary.Value); } } @@ -69,16 +70,17 @@ namespace Svelto.ECS { foreach (var entityViewsPerType in groupToSubmit.Value) { - var type = entityViewsPerType.Key; - for (var current = type; - current != _entityViewType && current != _objectType && current != _valueType; - current = current.BaseType) - entityViewsPerType.Value.AddEntityViewsToEngines(_entityViewEngines); + entityViewsPerType.Value.AddEntityViewsToEngines(_entityEngines); } } } - readonly DoubleBufferedEntityViews>> _groupedEntityViewsToAdd; + //one datastructure rule them all: + //split by group + //split by type per group. It's possible to get all the entities of a give type T per group thanks + //to the FasterDictionary capabilitiies OR it's possible to get a specific entityView indexed by + //ID. This ID doesn't need to be the EGID, it can be just the entityID + readonly DoubleBufferedEntityViews>> _groupedEntityToAdd; readonly EntitySubmissionScheduler _scheduler; } } \ No newline at end of file diff --git a/Svelto.ECS/EntityFactory.cs b/Svelto.ECS/EntityFactory.cs index df5cc70..343b287 100644 --- a/Svelto.ECS/EntityFactory.cs +++ b/Svelto.ECS/EntityFactory.cs @@ -39,12 +39,12 @@ namespace Svelto.ECS.Internal for (var index = 0; index < count; ++index) { var entityViewBuilder = entityViewsToBuild[index]; - var entityViewType = entityViewBuilder.GetEntityViewType(); + var entityViewType = entityViewBuilder.GetEntityType(); BuildEntityView(entityID, entityViewsByType, entityViewType, entityViewBuilder, implementors); } - _viewBuilder._initializer = new EntityInfoView {entityViewsToBuild = entityViewsToBuild}; + _viewBuilder._initializer = new EntityInfoView {entityToBuild = entityViewsToBuild}; BuildEntityView(entityID, entityViewsByType, _viewType, _viewBuilder, null); } @@ -66,6 +66,6 @@ namespace Svelto.ECS.Internal } static readonly EntityViewStructBuilder _viewBuilder = new EntityViewStructBuilder(); - static readonly Type _viewType = typeof(EntityInfoView); + static readonly Type _viewType = typeof(EntityInfoView); } } \ No newline at end of file diff --git a/Svelto.ECS/EntityViewBuilder.cs b/Svelto.ECS/EntityViewBuilder.cs index 6c67709..10c4e2c 100644 --- a/Svelto.ECS/EntityViewBuilder.cs +++ b/Svelto.ECS/EntityViewBuilder.cs @@ -13,7 +13,7 @@ namespace Svelto.ECS if (list == null) list = new TypeSafeDictionary(); - var castedList = list as TypeSafeDictionary; + var castedDic = list as TypeSafeDictionary; DBC.Check.Require(implementors != null, "Implementors not found while building an EntityView"); { @@ -26,7 +26,7 @@ namespace Svelto.ECS , implementors , DESCRIPTOR_NAME); - castedList.Add(entityID.GID, ref lentityView); + castedDic.Add(entityID.entityID, ref lentityView); } } @@ -40,18 +40,18 @@ namespace Svelto.ECS return list; } - public Type GetEntityViewType() + public Type GetEntityType() { return ENTITY_VIEW_TYPE; } public void MoveEntityView(EGID entityID, ITypeSafeDictionary fromSafeList, ITypeSafeDictionary toSafeList) { - var fromCastedList = fromSafeList as TypeSafeDictionary; - var toCastedList = toSafeList as TypeSafeDictionary; + var fromCastedDic = fromSafeList as TypeSafeDictionary; + var toCastedDic = toSafeList as TypeSafeDictionary; - toCastedList.Add(entityID.GID, fromCastedList[entityID.GID]); - fromCastedList.Remove(entityID.GID); + toCastedDic.Add(entityID.entityID, fromCastedDic[entityID.entityID]); + fromCastedDic.Remove(entityID.entityID); } FasterList>> entityViewBlazingFastReflection diff --git a/Svelto.ECS/EntityViewStructBuilder.cs b/Svelto.ECS/EntityViewStructBuilder.cs index e3f0c38..143cd46 100644 --- a/Svelto.ECS/EntityViewStructBuilder.cs +++ b/Svelto.ECS/EntityViewStructBuilder.cs @@ -22,9 +22,9 @@ namespace Svelto.ECS if (list == null) list = new TypeSafeDictionary(); - var castedList = list as TypeSafeDictionary; + var castedDic = list as TypeSafeDictionary; - castedList.Add(entityID.GID, _initializer); + castedDic.Add(entityID.entityID, _initializer); } public ITypeSafeDictionary Preallocate(ref ITypeSafeDictionary list, int size) @@ -37,18 +37,18 @@ namespace Svelto.ECS return list; } - public Type GetEntityViewType() + public Type GetEntityType() { return ENTITY_VIEW_TYPE; } public void MoveEntityView(EGID entityID, ITypeSafeDictionary fromSafeList, ITypeSafeDictionary toSafeList) { - var fromCastedList = fromSafeList as TypeSafeDictionary; - var toCastedList = toSafeList as TypeSafeDictionary; + var fromCastedDic = fromSafeList as TypeSafeDictionary; + var toCastedDic = toSafeList as TypeSafeDictionary; - toCastedList.Add(entityID.GID, fromCastedList[entityID.GID]); - fromCastedList.Remove(entityID.GID); + toCastedDic.Add(entityID.entityID, fromCastedDic[entityID.entityID]); + fromCastedDic.Remove(entityID.entityID); } static readonly Type ENTITY_VIEW_TYPE = typeof(EntityViewType); diff --git a/Svelto.ECS/EntityViewUtility.cs b/Svelto.ECS/EntityViewUtility.cs index a2d789c..5c45e93 100644 --- a/Svelto.ECS/EntityViewUtility.cs +++ b/Svelto.ECS/EntityViewUtility.cs @@ -21,7 +21,7 @@ static class EntityViewUtility .NoVirt.ToArrayFast(entityViewBlazingFastReflection, out count); #if DEBUG && !PROFILER if (count == 0) - throw new Exception(NO_COMPONENTS_EXCEPTION.FastConcat("Type ", entityDescriptorName, " entityView ", entityViewBuilder.GetEntityViewType().ToString())); + throw new Exception(NO_COMPONENTS_EXCEPTION.FastConcat("Type ", entityDescriptorName, " entityView ", entityViewBuilder.GetEntityType().ToString())); #endif for (var index = 0; index < implementors.Length; index++) { @@ -53,7 +53,7 @@ static class EntityViewUtility #if DEBUG && !PROFILER else { - Console.LogError(NULL_IMPLEMENTOR_ERROR.FastConcat("Type ", entityDescriptorName, " entityView ", entityViewBuilder.GetEntityViewType().ToString())); + Console.LogError(NULL_IMPLEMENTOR_ERROR.FastConcat("Type ", entityDescriptorName, " entityView ", entityViewBuilder.GetEntityType().ToString())); } #endif } @@ -73,7 +73,7 @@ static class EntityViewUtility { var e = new Exception(NOT_FOUND_EXCEPTION + " Component Type: " + fieldType.Name + " - EntityView: " + - entityViewBuilder.GetEntityViewType().Name + " - EntityDescriptor " + entityDescriptorName); + entityViewBuilder.GetEntityType().Name + " - EntityDescriptor " + entityDescriptorName); throw e; } @@ -84,7 +84,7 @@ static class EntityViewUtility " implementor: ", component.implementorType.ToString()) + " - EntityView: " + - entityViewBuilder.GetEntityViewType().Name + " - EntityDescriptor " + entityDescriptorName); + entityViewBuilder.GetEntityType().Name + " - EntityDescriptor " + entityDescriptorName); #endif #if DEBUG && !PROFILER fieldSetter.Value(ref entityView, component.implementorType); diff --git a/Svelto.ECS/EntityViewsDB.cs b/Svelto.ECS/EntityViewsDB.cs index 02d5b8f..cca5551 100644 --- a/Svelto.ECS/EntityViewsDB.cs +++ b/Svelto.ECS/EntityViewsDB.cs @@ -11,12 +11,12 @@ namespace Svelto.ECS.Internal _groupEntityViewsDB = groupEntityViewsDB; } - public ReadOnlyCollectionStruct QueryEntityViews() where T:IEntityData + public ReadOnlyCollectionStruct QueryEntities() where T:IEntityData { - return QueryEntityViews(ExclusiveGroups.StandardEntity); + return QueryEntities(ExclusiveGroups.StandardEntity); } - public ReadOnlyCollectionStruct QueryEntityViews(int @group) where T:IEntityData + public ReadOnlyCollectionStruct QueryEntities(int @group) where T:IEntityData { Dictionary entitiesInGroupPerType; @@ -30,14 +30,13 @@ namespace Svelto.ECS.Internal return (outList as TypeSafeDictionary).FasterValues; } - public T[] QueryEntityViewsCacheFriendly(out int count) where T : struct, IEntityData + public T[] QueryEntitiesCacheFriendly(out int count) where T : struct, IEntityData { - return QueryEntityViewsCacheFriendly(ExclusiveGroups.StandardEntity, out count); + return QueryEntitiesCacheFriendly(ExclusiveGroups.StandardEntity, out count); } - public T[] QueryEntityViewsCacheFriendly(int @group, out int count) where T : struct, IEntityData + public T[] QueryEntitiesCacheFriendly(int @group, out int count) where T : struct, IEntityData { - var type = typeof(T); count = 0; Dictionary entitiesInGroupPerType; @@ -45,12 +44,11 @@ namespace Svelto.ECS.Internal if (_groupEntityViewsDB.TryGetValue(group, out entitiesInGroupPerType) == false) return RetrieveEmptyEntityViewArray(); - ITypeSafeDictionary outList; - if (entitiesInGroupPerType.TryGetValue(typeof(T), out outList) == false) + ITypeSafeDictionary typeSafeDictionary; + if (entitiesInGroupPerType.TryGetValue(typeof(T), out typeSafeDictionary) == false) return RetrieveEmptyEntityViewArray(); - var typeSafeDictionary = entitiesInGroupPerType[type]; - return ((TypeSafeDictionary) typeSafeDictionary).GetFasterValuesBuffer(out count); + return ((TypeSafeDictionary)typeSafeDictionary).GetFasterValuesBuffer(out count); } public T QueryEntityView(EGID entityGID) where T : class, IEntityData @@ -62,16 +60,39 @@ namespace Svelto.ECS.Internal return entityView; } - public bool TryQueryEntityView(EGID entityegid, out T entityView) where T : IEntityData + public bool EntityExists(EGID entityGID) where T : IEntityData + { + var type = typeof(T); + + ITypeSafeDictionary entityViews; + + Dictionary entitiesInGroupPerType; + if (_groupEntityViewsDB.TryGetValue(entityGID.groupID, out entitiesInGroupPerType) == false) + { + return false; + } + + entitiesInGroupPerType.TryGetValue(type, out entityViews); + var casted = entityViews as TypeSafeDictionary; + + if (casted != null && + casted.ContainsKey(entityGID.entityID)) + { + return true; + } + + return false; + } + + public bool TryQueryEntityView(EGID entityegid, out T entityView) where T : class, IEntityData { return TryQueryEntityViewInGroup(entityegid, out entityView); } - bool TryQueryEntityViewInGroup(EGID entityGID, out T entityView) where T:IEntityData + bool TryQueryEntityViewInGroup(EGID entityGID, out T entityView) where T:class, IEntityData { var type = typeof(T); - T internalEntityView; ITypeSafeDictionary entityViews; Dictionary entitiesInGroupPerType; @@ -85,10 +106,8 @@ namespace Svelto.ECS.Internal var casted = entityViews as TypeSafeDictionary; if (casted != null && - casted.TryGetValue(entityGID.GID, out internalEntityView)) + casted.TryGetValue(entityGID.entityID, out entityView)) { - entityView = internalEntityView; - return true; } diff --git a/Svelto.ECS/IEntityView.cs b/Svelto.ECS/IEntityView.cs index f40c20b..74773ae 100644 --- a/Svelto.ECS/IEntityView.cs +++ b/Svelto.ECS/IEntityView.cs @@ -27,7 +27,7 @@ namespace Svelto.ECS { public EGID ID { get; set; } - public IEntityViewBuilder[] entityViewsToBuild; + public IEntityViewBuilder[] entityToBuild; } public static class EntityView where T: IEntityData, new() diff --git a/Svelto.ECS/IEntityViewBuilder.cs b/Svelto.ECS/IEntityViewBuilder.cs index fcd6412..0295df0 100644 --- a/Svelto.ECS/IEntityViewBuilder.cs +++ b/Svelto.ECS/IEntityViewBuilder.cs @@ -8,7 +8,7 @@ namespace Svelto.ECS void BuildEntityViewAndAddToList(ref ITypeSafeDictionary list, EGID entityID, object[] implementors); ITypeSafeDictionary Preallocate(ref ITypeSafeDictionary list, int size); - Type GetEntityViewType(); + Type GetEntityType(); void MoveEntityView(EGID entityID, ITypeSafeDictionary fromSafeList, ITypeSafeDictionary toSafeList); } } \ No newline at end of file diff --git a/Svelto.ECS/IEntityViewsDB.cs b/Svelto.ECS/IEntityViewsDB.cs index 208756f..0711e1e 100644 --- a/Svelto.ECS/IEntityViewsDB.cs +++ b/Svelto.ECS/IEntityViewsDB.cs @@ -4,13 +4,18 @@ namespace Svelto.ECS { public interface IEntityViewsDB { - ReadOnlyCollectionStruct QueryEntityViews() where T : IEntityData; - ReadOnlyCollectionStruct QueryEntityViews(int group) where T : IEntityData; + //to use with EntityViews, EntityStructs and EntityViewStructs + ReadOnlyCollectionStruct QueryEntities() where T : IEntityData; + ReadOnlyCollectionStruct QueryEntities(int group) where T : IEntityData; - T[] QueryEntityViewsCacheFriendly(out int count) where T : struct, IEntityData; - T[] QueryEntityViewsCacheFriendly(int group, out int count) where T : struct, IEntityData; + //to use with EntityStructs and EntityViewStructs + T[] QueryEntitiesCacheFriendly(out int count) where T : struct, IEntityData; + T[] QueryEntitiesCacheFriendly(int group, out int count) where T : struct, IEntityData; - bool TryQueryEntityView(EGID ID, out T entityView) where T : IEntityData; + //to use with EntityViews + bool TryQueryEntityView(EGID ID, out T entityView) where T : class, IEntityData; T QueryEntityView(EGID entityGID) where T : class, IEntityData; + + bool EntityExists(EGID ID) where T : IEntityData; } } \ No newline at end of file diff --git a/Svelto.ECS/MultiEntitiesEngine.cs b/Svelto.ECS/MultiEntitiesEngine.cs new file mode 100644 index 0000000..9805cfa --- /dev/null +++ b/Svelto.ECS/MultiEntitiesEngine.cs @@ -0,0 +1,43 @@ +namespace Svelto.ECS +{ + public abstract class MultiEntitiesEngine : SingleEntitiesEngine, IHandleEntityStructEngine + where U : struct, IEntityData where T : struct, IEntityData + { + public void AddInternal(ref U entityView) + { Add(ref entityView); } + public void RemoveInternal(ref U entityView) + { Remove(ref entityView); } + + protected abstract void Add(ref U entityView); + protected abstract void Remove(ref U entityView); + } + + public abstract class MultiEntitiesEngine : MultiEntitiesEngine, IHandleEntityStructEngine + where V : struct, IEntityData where U : struct, IEntityData where T : struct, IEntityData + { + public void AddInternal(ref V entityView) + { Add(ref entityView); } + public void RemoveInternal(ref V entityView) + { Remove(ref entityView); } + + protected abstract void Add(ref V entityView); + protected abstract void Remove(ref V entityView); + } + + /// + /// Please do not add more MultiEntityViewsEngine + /// if you use more than 4 nodes, your engine has + /// already too many responsabilities. + /// + public abstract class MultiEntitiesEngine : MultiEntitiesEngine, IHandleEntityStructEngine + where W : struct, IEntityData where V : struct, IEntityData where U : struct, IEntityData where T : struct, IEntityData + { + public void AddInternal(ref W entityView) + { Add(ref entityView); } + public void RemoveInternal(ref W entityView) + { Remove(ref entityView); } + + protected abstract void Add(ref W entityView); + protected abstract void Remove(ref W entityView); + } +} \ No newline at end of file diff --git a/Svelto.ECS/MultiEntityEngine.cs b/Svelto.ECS/MultiEntityEngine.cs new file mode 100644 index 0000000..062568b --- /dev/null +++ b/Svelto.ECS/MultiEntityEngine.cs @@ -0,0 +1,43 @@ +namespace Svelto.ECS +{ + public abstract class MultiEntityStructsEngine : SingleEntityStructEngine, IHandleEntityStructEngine + where U : struct, IEntityData where T : struct, IEntityData + { + public void AddInternal(ref U entityView) + { Add(ref entityView); } + public void RemoveInternal(ref U entityView) + { Remove(ref entityView); } + + protected abstract void Add(ref U entityView); + protected abstract void Remove(ref U entityView); + } + + public abstract class MultiEntityStructsEngine : MultiEntityViewsEngine, IHandleEntityStructEngine + where V : struct, IEntityData where U : struct, IEntityData where T : struct, IEntityData + { + public void AddInternal(ref V entityView) + { Add(ref entityView); } + public void RemoveInternal(ref V entityView) + { Remove(ref entityView); } + + protected abstract void Add(ref V entityView); + protected abstract void Remove(ref V entityView); + } + + /// + /// Please do not add more MultiEntityViewsEngine + /// if you use more than 4 nodes, your engine has + /// already too many responsabilities. + /// + public abstract class MultiEntityStructsEngine : MultiEntityViewsEngine, IHandleEntityStructEngine + where W : struct, IEntityData where V : struct, IEntityData where U : struct, IEntityData where T : struct, IEntityData + { + public void AddInternal(ref W entityView) + { Add(ref entityView); } + public void RemoveInternal(ref W entityView) + { Remove(ref entityView); } + + protected abstract void Add(ref W entityView); + protected abstract void Remove(ref W entityView); + } +} \ No newline at end of file diff --git a/Svelto.ECS/MultiEntityViewsEngine.cs b/Svelto.ECS/MultiEntityViewsEngine.cs index beaf78f..2b1b95b 100644 --- a/Svelto.ECS/MultiEntityViewsEngine.cs +++ b/Svelto.ECS/MultiEntityViewsEngine.cs @@ -1,7 +1,7 @@ namespace Svelto.ECS { public abstract class MultiEntityViewsEngine : SingleEntityViewEngine, IHandleEntityStructEngine - where U : IEntityData where T : IEntityData + where U : class, IEntityData where T : class, IEntityData { public void AddInternal(ref U entityView) { Add(entityView); } @@ -13,7 +13,7 @@ namespace Svelto.ECS } public abstract class MultiEntityViewsEngine : MultiEntityViewsEngine, IHandleEntityStructEngine - where V : IEntityData where U : IEntityData where T : IEntityData + where V : class, IEntityData where U : class, IEntityData where T : class, IEntityData { public void AddInternal(ref V entityView) { Add(entityView); } @@ -30,7 +30,7 @@ namespace Svelto.ECS /// already too many responsabilities. /// public abstract class MultiEntityViewsEngine : MultiEntityViewsEngine, IHandleEntityStructEngine - where W : IEntityData where V : IEntityData where U : IEntityData where T : IEntityData + where W : class, IEntityData where V : class, IEntityData where U : class, IEntityData where T : class, IEntityData { public void AddInternal(ref W entityView) { Add(entityView); } diff --git a/Svelto.ECS/SingleEntitiesEngine.cs b/Svelto.ECS/SingleEntitiesEngine.cs new file mode 100644 index 0000000..5924f2c --- /dev/null +++ b/Svelto.ECS/SingleEntitiesEngine.cs @@ -0,0 +1,14 @@ +namespace Svelto.ECS +{ + public abstract class SingleEntitiesEngine : IHandleEntityStructEngine where T : struct, IEntityData + { + public void AddInternal(ref T entityView) + { Add(ref entityView); } + + public void RemoveInternal(ref T entityView) + { Remove(ref entityView); } + + protected abstract void Add(ref T entityView); + protected abstract void Remove(ref T entityView); + } +} \ No newline at end of file diff --git a/Svelto.ECS/SingleEntityViewEngine.cs b/Svelto.ECS/SingleEntityViewEngine.cs index f59eb26..e3f1994 100644 --- a/Svelto.ECS/SingleEntityViewEngine.cs +++ b/Svelto.ECS/SingleEntityViewEngine.cs @@ -1,6 +1,6 @@ namespace Svelto.ECS { - public abstract class SingleEntityViewEngine : IHandleEntityStructEngine where T : IEntityData + public abstract class SingleEntityViewEngine : IHandleEntityStructEngine where T : class, IEntityData { public void AddInternal(ref T entityView) { Add(entityView); } @@ -11,16 +11,4 @@ namespace Svelto.ECS protected abstract void Add(T entityView); protected abstract void Remove(T entityView); } - - public abstract class SingleEntityStructEngine : IHandleEntityStructEngine where T : IEntityData - { - public void AddInternal(ref T entityView) - { Add(ref entityView); } - - public void RemoveInternal(ref T entityView) - { Remove(ref entityView); } - - protected abstract void Add(ref T entityView); - protected abstract void Remove(ref T entityView); - } } \ No newline at end of file