From c2ff71d780934a7f726a7862293c26e34981fa9a Mon Sep 17 00:00:00 2001 From: sebas77 Date: Tue, 3 Apr 2018 00:03:10 +0100 Subject: [PATCH] EntityStructs cannot be put in or queried from groups anymore EnttiyStructs can now be built with an initializer --- .../DataStructures/TypeSafeDictionary.cs | 49 ----- .../TypeSafeFasterListForECS.cs | 2 +- Svelto.ECS/EnginesRootEntities.cs | 36 ++-- Svelto.ECS/EnginesRootSubmission.cs | 32 +-- Svelto.ECS/EntityDescriptor.cs | 8 +- Svelto.ECS/EntityFactory.cs | 1 - Svelto.ECS/EntityViewBuilder.cs | 29 ++- Svelto.ECS/EntityViewsDB.cs | 2 +- .../Experimental/StructNodeCollections.cs | 183 ------------------ Svelto.ECS/IEntityViewsDB.cs | 2 +- 10 files changed, 71 insertions(+), 273 deletions(-) delete mode 100644 Svelto.ECS/Experimental/StructNodeCollections.cs diff --git a/Svelto.ECS/DataStructures/TypeSafeDictionary.cs b/Svelto.ECS/DataStructures/TypeSafeDictionary.cs index 164017e..9bb386f 100644 --- a/Svelto.ECS/DataStructures/TypeSafeDictionary.cs +++ b/Svelto.ECS/DataStructures/TypeSafeDictionary.cs @@ -16,7 +16,6 @@ namespace Svelto.ECS.Internal void FillWithIndexedEntityViews(ITypeSafeList entityViews); bool Remove(EGID entityId); IEntityView GetIndexedEntityView(EGID entityID); - bool isQueryiableEntityView { get; } } class TypeSafeDictionaryForClass : Dictionary, ITypeSafeDictionary where TValue : EntityView @@ -55,53 +54,5 @@ namespace Svelto.ECS.Internal { return this[entityID.GID]; } - - public bool isQueryiableEntityView - { - get { return true; } - } - } - - class TypeSafeDictionaryForStruct : Dictionary, ITypeSafeDictionary where TValue : struct, IEntityStruct - { - internal static readonly ReadOnlyDictionary Default = - new ReadOnlyDictionary(new Dictionary()); - - public void FillWithIndexedEntityViews(ITypeSafeList entityViews) - { - int count; - var buffer = FasterList.NoVirt.ToArrayFast((FasterList) entityViews, out count); - - try - { - for (var i = 0; i < count; i++) - { - var entityView = buffer[i]; - - Add(entityView.ID.GID, entityView); - } - } - catch (Exception e) - { - throw new TypeSafeDictionaryException(e); - } - } - - public bool Remove(EGID entityId) - { - base.Remove(entityId.GID); - - return Count > 0; - } - - public IEntityView GetIndexedEntityView(EGID entityID) - { - return this[entityID.GID]; - } - - public bool isQueryiableEntityView - { - get { return false; } - } } } \ No newline at end of file diff --git a/Svelto.ECS/DataStructures/TypeSafeFasterListForECS.cs b/Svelto.ECS/DataStructures/TypeSafeFasterListForECS.cs index 4e54840..7fde5b9 100644 --- a/Svelto.ECS/DataStructures/TypeSafeFasterListForECS.cs +++ b/Svelto.ECS/DataStructures/TypeSafeFasterListForECS.cs @@ -118,7 +118,7 @@ namespace Svelto.ECS.Internal public ITypeSafeDictionary CreateIndexedDictionary() { - return new TypeSafeDictionaryForStruct(); + throw new NotSupportedException(); } public IEntityView[] ToArrayFast(out int count) diff --git a/Svelto.ECS/EnginesRootEntities.cs b/Svelto.ECS/EnginesRootEntities.cs index 620a29c..c5c4383 100644 --- a/Svelto.ECS/EnginesRootEntities.cs +++ b/Svelto.ECS/EnginesRootEntities.cs @@ -131,18 +131,20 @@ namespace Svelto.ECS void RemoveEntity(EGID entityGID) { var entityViewBuilders = _entityInfos[entityGID.GID]; - var entityViewBuildersCount = entityViewBuilders.Length; - var group = _groupEntityViewsDB[entityGID.group]; - + //for each entity view generated by the entity descriptor for (var i = 0; i < entityViewBuildersCount; i++) { var entityViewType = entityViewBuilders[i].GetEntityViewType(); - - InternalRemoveEntityViewFromDBDicAndEngines(entityViewType, entityGID); - - RemoveEntityViewFromDB(@group, entityViewType, entityGID); + + if (entityViewBuilders[i].isQueryiableEntityView) + { + var group = _groupEntityViewsDB[entityGID.group]; + InternalRemoveEntityViewFromDBDicAndEngines(entityViewType, entityGID); + RemoveEntityViewFromDB(@group, entityViewType, entityGID); + } + RemoveEntityViewFromDB(@_globalEntityViewsDB, entityViewType, entityGID); } @@ -161,18 +163,21 @@ namespace Svelto.ECS { foreach (var group in _groupEntityViewsDB[groupID]) { - var entityViewType = group.Key; + { + var entityViewType = group.Key; - int count; - var entities = group.Value.ToArrayFast(out count); + int count; + var entities = group.Value.ToArrayFast(out count); - for (var i = 0; i < count; i++) - { - var entityID = entities[i].ID; + for (var i = 0; i < count; i++) + { + var entityID = entities[i].ID; - RemoveEntityViewFromDB(@_globalEntityViewsDB, entityViewType, entityID); + RemoveEntityViewFromDB(@_globalEntityViewsDB, entityViewType, entityID); - InternalRemoveEntityViewFromDBDicAndEngines(entityViewType, entityID); + if (group.Value.isQueryiableEntityView) + InternalRemoveEntityViewFromDBDicAndEngines(entityViewType, entityID); + } } } @@ -182,7 +187,6 @@ namespace Svelto.ECS void InternalRemoveEntityViewFromDBDicAndEngines(Type entityViewType, EGID id) { var typeSafeDictionary = _globalEntityViewsDBDic[entityViewType]; - if (typeSafeDictionary.isQueryiableEntityView) { var entityView = typeSafeDictionary.GetIndexedEntityView(id); diff --git a/Svelto.ECS/EnginesRootSubmission.cs b/Svelto.ECS/EnginesRootSubmission.cs index 3934072..bc1e1f8 100644 --- a/Svelto.ECS/EnginesRootSubmission.cs +++ b/Svelto.ECS/EnginesRootSubmission.cs @@ -55,7 +55,8 @@ namespace Svelto.ECS foreach (var entityViewsPerType in groupToSubmit.Value) { //add the entity View in the group - AddEntityViewToDB(groupDB, entityViewsPerType); + if (entityViewsPerType.Value.isQueryiableEntityView == true) + AddEntityViewToDB(groupDB, entityViewsPerType); //add the entity view in the gloal pool AddEntityViewToDB(_globalEntityViewsDB, entityViewsPerType); //and it's not a struct, add in the indexable DB too @@ -63,7 +64,8 @@ namespace Svelto.ECS } } - //then submit everything in the engines + //then submit everything in the engines, so that the DB is up to date + //with all the entity views and struct created by the entity built foreach (var group in groupsToSubmit) { foreach (var entityViewList in group.Value) @@ -81,23 +83,29 @@ namespace Svelto.ECS static void AddEntityViewToDB( Dictionary entityViewsDB, KeyValuePair entityViewList) { - ITypeSafeList dbList; + + { + ITypeSafeList dbList; - if (entityViewsDB.TryGetValue(entityViewList.Key, out dbList) == false) - dbList = entityViewsDB[entityViewList.Key] = entityViewList.Value.Create(); + if (entityViewsDB.TryGetValue(entityViewList.Key, out dbList) == false) + dbList = entityViewsDB[entityViewList.Key] = entityViewList.Value.Create(); - dbList.AddRange(entityViewList.Value); + dbList.AddRange(entityViewList.Value); + } } static void AddEntityViewToEntityViewsDictionary(Dictionary entityViewsDBdic, ITypeSafeList entityViews, Type entityViewType) { - ITypeSafeDictionary entityViewsDic; - - if (entityViewsDBdic.TryGetValue(entityViewType, out entityViewsDic) == false) - entityViewsDic = entityViewsDBdic[entityViewType] = entityViews.CreateIndexedDictionary(); - - entityViewsDic.FillWithIndexedEntityViews(entityViews); + if (entityViews.isQueryiableEntityView == true) + { + ITypeSafeDictionary entityViewsDic; + + if (entityViewsDBdic.TryGetValue(entityViewType, out entityViewsDic) == false) + entityViewsDic = entityViewsDBdic[entityViewType] = entityViews.CreateIndexedDictionary(); + + entityViewsDic.FillWithIndexedEntityViews(entityViews); + } } static void AddEntityViewToTheSuitableEngines(Dictionary> entityViewEngines, ITypeSafeList entityViewsList, Type entityViewType) diff --git a/Svelto.ECS/EntityDescriptor.cs b/Svelto.ECS/EntityDescriptor.cs index 9958c7e..e75b01b 100644 --- a/Svelto.ECS/EntityDescriptor.cs +++ b/Svelto.ECS/EntityDescriptor.cs @@ -31,15 +31,15 @@ namespace Svelto.ECS Check.Require(extraEntityViews.Count > 0, "don't use a DynamicEntityDescriptorInfo if you don't need to use extra EntityViews"); - var descriptor = new TType(); - var length = descriptor.entityViewsToBuild.Length; + var defaultEntityViewsToBuild = EntityDescriptorTemplate.Default.entityViewsToBuild; + var length = defaultEntityViewsToBuild.Length; entityViewsToBuild = new IEntityViewBuilder[length + extraEntityViews.Count]; - Array.Copy(descriptor.entityViewsToBuild, 0, entityViewsToBuild, 0, length); + Array.Copy(defaultEntityViewsToBuild, 0, entityViewsToBuild, 0, length); Array.Copy(extraEntityViews.ToArrayFast(), 0, entityViewsToBuild, length, extraEntityViews.Count); - name = descriptor.ToString(); + name = EntityDescriptorTemplate.Default.name; } } diff --git a/Svelto.ECS/EntityFactory.cs b/Svelto.ECS/EntityFactory.cs index a7e1a19..ae600fe 100644 --- a/Svelto.ECS/EntityFactory.cs +++ b/Svelto.ECS/EntityFactory.cs @@ -1,6 +1,5 @@ using System; using System.Collections.Generic; -using System.Xml; using Svelto.DataStructures; using Svelto.Utilities; using Console = Utility.Console; diff --git a/Svelto.ECS/EntityViewBuilder.cs b/Svelto.ECS/EntityViewBuilder.cs index f709038..3d6e24e 100644 --- a/Svelto.ECS/EntityViewBuilder.cs +++ b/Svelto.ECS/EntityViewBuilder.cs @@ -11,6 +11,7 @@ namespace Svelto.ECS Type GetEntityViewType(); void MoveEntityView(EGID entityID, ITypeSafeList fromSafeList, ITypeSafeList toSafeList); bool mustBeFilled { get; } + bool isQueryiableEntityView { get; } } public class EntityViewBuilder : IEntityViewBuilder where EntityViewType : EntityView, new() @@ -56,23 +57,35 @@ namespace Svelto.ECS { get { return true; } } + + public bool isQueryiableEntityView + { + get { return true; } + } public static readonly Type ENTITY_VIEW_TYPE = typeof(EntityViewType); } public class EntityViewStructBuilder : IEntityViewBuilder where EntityViewType : struct, IEntityStruct { + public EntityViewStructBuilder() + {} + + public EntityViewStructBuilder(ref EntityViewType initializer) + { + _initializer = initializer; + } + public void BuildEntityViewAndAddToList(ref ITypeSafeList list, EGID entityID, out IEntityView entityView) { - var structEntityView = default(EntityViewType); - structEntityView.ID = entityID; + _initializer.ID = entityID; if (list == null) list = new TypeSafeFasterListForECSForStructs(); var castedList = list as TypeSafeFasterListForECSForStructs; - - castedList.Add(structEntityView); + + castedList.Add(_initializer); entityView = null; } @@ -105,6 +118,12 @@ namespace Svelto.ECS get { return false; } } + public bool isQueryiableEntityView + { + get { return false; } + } + public static readonly Type ENTITY_VIEW_TYPE = typeof(EntityViewType); - } + EntityViewType _initializer; + } } \ No newline at end of file diff --git a/Svelto.ECS/EntityViewsDB.cs b/Svelto.ECS/EntityViewsDB.cs index 0e69652..1258a0b 100644 --- a/Svelto.ECS/EntityViewsDB.cs +++ b/Svelto.ECS/EntityViewsDB.cs @@ -54,7 +54,7 @@ namespace Svelto.ECS.Internal return FasterList.NoVirt.ToArrayFast((FasterList)entityViews, out count); } - public T[] QueryGroupedEntityViewsAsArray(int @group, out int count) where T : IEntityView + public T[] QueryGroupedEntityViewsAsArray(int @group, out int count) where T : EntityView { var type = typeof(T); count = 0; diff --git a/Svelto.ECS/Experimental/StructNodeCollections.cs b/Svelto.ECS/Experimental/StructNodeCollections.cs deleted file mode 100644 index f09b605..0000000 --- a/Svelto.ECS/Experimental/StructNodeCollections.cs +++ /dev/null @@ -1,183 +0,0 @@ -#if EXPERIMENTAL -using System; -using System.Collections.Generic; -using Svelto.DataStructures; -using Svelto.ECS.Experimental.Internal; - -namespace Svelto.ECS.Experimental.Internal -{ - public interface IStructEntityViewEngine : IEngine - { - void CreateStructEntityViews(SharedStructEntityViewLists sharedStructEntityViewLists); - } - - public interface IGroupedStructEntityViewsEngine : IEngine - { - void CreateStructEntityViews(SharedGroupedStructEntityViewsLists sharedStructEntityViewLists); - } -} - -namespace Svelto.ECS.Experimental -{ - public interface IGroupedEntityView - { - int groupID { get; set; } - } - - /// - /// The engines can receive and store IEntityViews structs - /// Unboxing will happen during the Add, but the - /// data will then be stored and processed as stucts - /// - public interface IStructEntityViewEngine : IStructEntityViewEngine where T:struct, IEntityStruct - { } - - /// - /// same as above, but the entityViews are grouped by ID - /// usually the ID is the owner of the entityViews of that - /// group - /// - public interface IGroupedStructEntityViewsEngine : IGroupedStructEntityViewsEngine where T : struct, IGroupedEntityView - { - void Add(ref T entityView); - void Remove(ref T entityView); - } - - public sealed class StructEntityViews where T:struct, IEntityStruct - { - public T[] GetList(out int numberOfItems) - { - numberOfItems = _internalList.Count; - return _internalList.ToArrayFast(); - } - - public StructEntityViews(SharedStructEntityViewLists container) - { - _internalList = SharedStructEntityViewLists.NoVirt.GetList(container); - } - - public void Add(T entityView) - { - T convert = (T)entityView; - - _internalList.Add(convert); - } - - readonly FasterList _internalList; - } - - public struct StructGroupEntityViews - where T : struct, IEntityView - { - public StructGroupEntityViews(SharedGroupedStructEntityViewsLists container) - { - _container = container; - indices = new Dictionary(); - } - - public void Add(int groupID, T entityView) - { - T convert = (T)entityView; - - var fasterList = - (SharedGroupedStructEntityViewsLists.NoVirt.GetList(_container, groupID) as FasterList); - indices[entityView.ID] = fasterList.Count; - - fasterList.Add(convert); - } - - public void Remove(int groupID, T entityView) - { - var fasterList = - (SharedGroupedStructEntityViewsLists.NoVirt.GetList(_container, groupID) as FasterList); - var index = indices[entityView.ID]; - indices.Remove(entityView.ID); - - if (fasterList.UnorderedRemoveAt(index)) - indices[fasterList[index].ID] = index; - } - - public T[] GetList(int groupID, out int numberOfItems) - { - var fasterList = - (SharedGroupedStructEntityViewsLists.NoVirt.GetList(_container, groupID) as FasterList); - - return FasterList.NoVirt.ToArrayFast(fasterList, out numberOfItems); - } - - readonly SharedGroupedStructEntityViewsLists _container; - readonly Dictionary indices; - } - - public class SharedStructEntityViewLists - { - internal SharedStructEntityViewLists() - { - _collection = new Dictionary(); - } - - internal static class NoVirt - { - internal static FasterList GetList(SharedStructEntityViewLists obj) where T : struct - { - IFasterList list; - if (obj._collection.TryGetValue(typeof(T), out list)) - { - return list as FasterList; - } - - list = new FasterList(); - - obj._collection.Add(typeof(T), list); - - return (FasterList)list; - } - } - - readonly Dictionary _collection; - } - - public class SharedGroupedStructEntityViewsLists - { - internal SharedGroupedStructEntityViewsLists() - { - _collection = new Dictionary>(); - } - - internal static class NoVirt - { - internal static IFasterList GetList(SharedGroupedStructEntityViewsLists list, int groupID) where T : struct - { - Dictionary dic = GetGroup(list); - IFasterList localList; - - if (dic.TryGetValue(groupID, out localList)) - return localList; - - localList = new FasterList(); - dic.Add(groupID, localList); - - return localList; - } - - internal static Dictionary GetGroup(SharedGroupedStructEntityViewsLists list) where T : struct - { - Dictionary dic; - - if (list._collection.TryGetValue(typeof(T), out dic)) - { - return dic; - } - - dic = new Dictionary(); - - list._collection.Add(typeof(T), dic); - - return dic; - } - } - - readonly Dictionary> _collection; - } -} -#endif \ No newline at end of file diff --git a/Svelto.ECS/IEntityViewsDB.cs b/Svelto.ECS/IEntityViewsDB.cs index 239a9e6..ae71152 100644 --- a/Svelto.ECS/IEntityViewsDB.cs +++ b/Svelto.ECS/IEntityViewsDB.cs @@ -8,7 +8,7 @@ namespace Svelto.ECS FasterReadOnlyList QueryGroupedEntityViews(int group) where T : EntityView; T[] QueryEntityViewsAsArray(out int count) where T : IEntityView; - T[] QueryGroupedEntityViewsAsArray(int group, out int count) where T : IEntityView; + T[] QueryGroupedEntityViewsAsArray(int group, out int count) where T : EntityView; bool TryQueryEntityView(EGID ID, out T entityView) where T : EntityView; T QueryEntityView(EGID entityGID) where T : EntityView;