diff --git a/Svelto.ECS/DataStructures/TypeSafeFasterListForECS.cs b/Svelto.ECS/DataStructures/TypeSafeFasterListForECS.cs index 4e8d9a6..e478ebd 100644 --- a/Svelto.ECS/DataStructures/TypeSafeFasterListForECS.cs +++ b/Svelto.ECS/DataStructures/TypeSafeFasterListForECS.cs @@ -14,14 +14,40 @@ namespace Svelto.ECS.Internal ITypeSafeList Create(); bool MappedRemove(EGID entityID); ITypeSafeDictionary CreateIndexedDictionary(); - IEntityData[] ToArrayFast(out int count); + EGIDEnumerator EntityIDS(); void AddCapacity(int capacity); + + void Fill(FasterList enginesForEntityView); } - class TypeSafeFasterListForECS : FasterList where T : IEntityData + public struct EGIDEnumerator:IEnumerable, IEnumerator { - readonly Dictionary _mappedIndices; + Dictionary.Enumerator _keysEnumerator; + + public EGIDEnumerator(Dictionary mappedIndices) + { + _keysEnumerator = mappedIndices.GetEnumerator(); + } + + public bool MoveNext() + { + return _keysEnumerator.MoveNext(); + } + public void Reset() + { + throw new NotImplementedException(); + } + + public object Current { get { return new EGID(_keysEnumerator.Current.Key);} } + public IEnumerator GetEnumerator() + { + return this; + } + } + + class TypeSafeFasterListForECS : FasterList where T : IEntityData + { protected TypeSafeFasterListForECS() { _mappedIndices = new Dictionary(); @@ -55,7 +81,6 @@ namespace Svelto.ECS.Internal base.AddRange(entityViewListValue as FasterList); - for (var i = index; i < Count; ++i) { try @@ -95,10 +120,16 @@ namespace Svelto.ECS.Internal { return _mappedIndices[entityID.GID]; } + + public EGIDEnumerator EntityIDS() + { + return new EGIDEnumerator(_mappedIndices); + } + + readonly Dictionary _mappedIndices; } - class TypeSafeFasterListForECSForStructs : TypeSafeFasterListForECS, ITypeSafeList - where T : struct, IEntityData + class TypeSafeFasterListForECSForStructs : TypeSafeFasterListForECS, ITypeSafeList where T:struct, IEntityData { public TypeSafeFasterListForECSForStructs(int size) : base(size) {} @@ -118,12 +149,25 @@ namespace Svelto.ECS.Internal public ITypeSafeDictionary CreateIndexedDictionary() { - throw new NotSupportedException(); + throw new NotImplementedException(); } - public IEntityData[] ToArrayFast(out int count) + public void Fill(FasterList enginesForEntityView) { - throw new Exception("Not Allowed"); + var thisfastList = NoVirt.ToArrayFast(this); + for (int i = 0; i < Count; i++) + { + int count; + var fastList = FasterList.NoVirt.ToArrayFast(enginesForEntityView, out count); + for (int j = 0; j < count; j++) + { +#if ENGINE_PROFILER_ENABLED + EngineProfiler.MonitorAddDuration(fastList[j], entityView); +#else + (fastList[j] as IHandleEntityStructEngine).Add(ref thisfastList[j]); +#endif + } + } } public ITypeSafeList Create(int size) @@ -131,8 +175,8 @@ namespace Svelto.ECS.Internal return new TypeSafeFasterListForECSForStructs(size); } } - - class TypeSafeFasterListForECSForClasses : TypeSafeFasterListForECS, ITypeSafeList where T : IEntityData, new() + + class TypeSafeFasterListForECSForClasses : TypeSafeFasterListForECS, ITypeSafeList where T:IEntityData, new() { public TypeSafeFasterListForECSForClasses(int size) : base(size) {} @@ -155,11 +199,22 @@ namespace Svelto.ECS.Internal return new TypeSafeDictionaryForClass(); } - public IEntityData[] ToArrayFast(out int count) + public void Fill(FasterList enginesForEntityView) { - count = Count; - - return ToArrayFast(); + var thisfastList = NoVirt.ToArrayFast(this); + for (int i = 0; i < Count; i++) + { + int count; + var fastList = FasterList.NoVirt.ToArrayFast(enginesForEntityView, out count); + for (int j = 0; j < count; j++) + { +#if ENGINE_PROFILER_ENABLED + EngineProfiler.MonitorAddDuration(fastList[j], entityView); +#else + (fastList[j] as IHandleEntityStructEngine).Add(ref thisfastList[j]); +#endif + } + } } public ITypeSafeList Create(int size) diff --git a/Svelto.ECS/EGID.cs b/Svelto.ECS/EGID.cs index 18a3901..0b42a77 100644 --- a/Svelto.ECS/EGID.cs +++ b/Svelto.ECS/EGID.cs @@ -31,6 +31,11 @@ namespace Svelto.ECS _GID = MAKE_GLOBAL_ID(entityID, ExclusiveGroups.StandardEntity); } + internal EGID(long otherID) + { + _GID = otherID; + } + static long MAKE_GLOBAL_ID(int entityId, int groupId) { return (long)groupId << 32 | (uint)entityId; diff --git a/Svelto.ECS/EnginesRoot.GenericEntityFactory.cs b/Svelto.ECS/EnginesRoot.GenericEntityFactory.cs index 0fa071f..2f53b7b 100644 --- a/Svelto.ECS/EnginesRoot.GenericEntityFactory.cs +++ b/Svelto.ECS/EnginesRoot.GenericEntityFactory.cs @@ -27,16 +27,16 @@ namespace Svelto.ECS _weakEngine.Target.BuildEntity(entityID, entityDescriptor, implementors); } - public void BuildEntityInGroup(int entityID, int groupID, object[] implementors) + public void BuildEntity(int entityID, int groupID, object[] implementors) where T : IEntityDescriptor, new() { - _weakEngine.Target.BuildEntityInGroup(entityID, groupID, implementors); + _weakEngine.Target.BuildEntity(entityID, groupID, implementors); } - public void BuildEntityInGroup(int entityID, int groupID, EntityDescriptorInfo entityDescriptor, + public void BuildEntity(int entityID, int groupID, EntityDescriptorInfo entityDescriptor, object[] implementors) { - _weakEngine.Target.BuildEntityInGroup(entityID, groupID, entityDescriptor, implementors); + _weakEngine.Target.BuildEntity(entityID, groupID, entityDescriptor, implementors); } public void PreallocateEntitySpace(int size) where T : IEntityDescriptor, new() @@ -44,7 +44,7 @@ namespace Svelto.ECS _weakEngine.Target.Preallocate(ExclusiveGroups.StandardEntity, size); } - public void PreallocateEntitySpaceInGroup(int groupID, int size) where T : IEntityDescriptor, new() + public void PreallocateEntitySpace(int groupID, int size) where T : IEntityDescriptor, new() { _weakEngine.Target.Preallocate(groupID, size); } diff --git a/Svelto.ECS/EnginesRootEngines.cs b/Svelto.ECS/EnginesRootEngines.cs index 0f249c3..7d554a7 100644 --- a/Svelto.ECS/EnginesRootEngines.cs +++ b/Svelto.ECS/EnginesRootEngines.cs @@ -34,18 +34,18 @@ namespace Svelto.ECS /// public EnginesRoot(EntitySubmissionScheduler entityViewScheduler) { - _entityViewEngines = new Dictionary>(); + _entityViewEngines = new Dictionary>(); _otherEngines = new FasterList(); - _globalEntityViewsDB = new Dictionary(); _groupEntityViewsDB = new Dictionary>(); + _groupEntityViewsDB[ExclusiveGroups.StandardEntity] = new Dictionary(); _globalEntityViewsDBDic = new Dictionary(); _entityInfos = new Dictionary(); _groupedEntityViewsToAdd = new DoubleBufferedEntityViews>>(); - _DB = new EntityViewsDB(_globalEntityViewsDB, _globalEntityViewsDBDic, _groupEntityViewsDB); + _DB = new EntityViewsDB(_globalEntityViewsDBDic, _groupEntityViewsDB); _scheduler = entityViewScheduler; _scheduler.Schedule(new WeakAction(SubmitEntityViews)); @@ -56,7 +56,7 @@ namespace Svelto.ECS #if ENGINE_PROFILER_ENABLED && UNITY_EDITOR Profiler.EngineProfiler.AddEngine(engine); #endif - var viewEngine = engine as IHandleEntityViewEngine; + var viewEngine = engine as IHandleEntityViewEngineAbstracted; if (viewEngine != null) CheckEntityViewsEngine(viewEngine); @@ -75,12 +75,13 @@ namespace Svelto.ECS { var baseType = engine.GetType().GetBaseType(); - while (baseType != _object) + while (baseType != _objectType) { if (baseType.IsGenericTypeEx()) { var genericArguments = baseType.GetGenericArgumentsEx(); - AddEngine(engine as IHandleEntityViewEngine, genericArguments, _entityViewEngines); + + AddEngine(engine as IHandleEntityViewEngineAbstracted, genericArguments, _entityViewEngines); return; } @@ -117,10 +118,11 @@ namespace Svelto.ECS list.Add(engine); } - readonly Dictionary> _entityViewEngines; + readonly Dictionary> _entityViewEngines; readonly FasterList _otherEngines; static readonly Type _entityViewType= typeof(IEntityData); - static readonly Type _object = typeof(object); + 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 520ada4..9c3500b 100644 --- a/Svelto.ECS/EnginesRootEntities.cs +++ b/Svelto.ECS/EnginesRootEntities.cs @@ -14,10 +14,11 @@ namespace Svelto.ECS { public void Dispose() { - foreach (var entity in _globalEntityViewsDB) - if (entity.Value.isQueryiableEntityView) - foreach (var entityView in entity.Value) - RemoveEntityViewFromEngines(_entityViewEngines, entityView as IEntityData, entity.Key); + foreach (var groups in _groupEntityViewsDB) + foreach (var entity in groups.Value) + if (entity.Value.isQueryiableEntityView == true) + foreach (var entityView in entity.Value) + RemoveEntityViewFromEngines(_entityViewEngines, entityView as IEntityData, entity.Key); } ///-------------------------------------------- @@ -36,13 +37,13 @@ namespace Svelto.ECS void BuildEntity(int entityID, object[] implementors = null) where T : IEntityDescriptor, new() { - BuildEntityInGroup + BuildEntity (entityID, ExclusiveGroups.StandardEntity, implementors); } void BuildEntity(int entityID, EntityDescriptorInfo entityDescriptor, object[] implementors) { - BuildEntityInGroup + BuildEntity (entityID, ExclusiveGroups.StandardEntity, entityDescriptor, implementors); } @@ -55,7 +56,7 @@ namespace Svelto.ECS /// /// /// - void BuildEntityInGroup(int entityID, int groupID, object[] implementors = null) + void BuildEntity(int entityID, int groupID, object[] implementors = null) where T : IEntityDescriptor, new() { EntityFactory.BuildGroupedEntityViews(entityID, groupID, @@ -65,7 +66,7 @@ namespace Svelto.ECS implementors); } - void BuildEntityInGroup(int entityID, int groupID, EntityDescriptorInfo entityDescriptor, + void BuildEntity(int entityID, int groupID, EntityDescriptorInfo entityDescriptor, object[] implementors = null) { EntityFactory.BuildGroupedEntityViews(entityID, groupID, @@ -95,10 +96,6 @@ namespace Svelto.ECS //reserve space for the global pool ITypeSafeList dbList; - if (_globalEntityViewsDB.TryGetValue(entityViewType, out dbList) == false) - _globalEntityViewsDB[entityViewType] = entityViewBuilder.Preallocate(ref dbList, size); - else - dbList.AddCapacity(size); //reserve space for the single group Dictionary @group; @@ -144,8 +141,6 @@ namespace Svelto.ECS InternalRemoveEntityViewFromDBDicAndEngines(entityViewType, entityGID); RemoveEntityViewFromDB(@group, entityViewType, entityGID); } - - RemoveEntityViewFromDB(@_globalEntityViewsDB, entityViewType, entityGID); } _entityInfos.Remove(entityGID.GID); @@ -163,21 +158,14 @@ 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); + var entities = group.Value.EntityIDS(); - for (var i = 0; i < count; i++) - { - var entityID = entities[i].ID; - - RemoveEntityViewFromDB(@_globalEntityViewsDB, entityViewType, entityID); - - if (group.Value.isQueryiableEntityView) - InternalRemoveEntityViewFromDBDicAndEngines(entityViewType, entityID); - } + foreach (EGID entityID in entities) + { + if (group.Value.isQueryiableEntityView) + InternalRemoveEntityViewFromDBDicAndEngines(entityViewType, entityID); } } @@ -206,23 +194,23 @@ namespace Svelto.ECS typeSafeDictionary.Remove(id); } - static void RemoveEntityViewFromEngines(Dictionary> entityViewEngines, - IEntityData entityView, - Type entityViewType) + static void RemoveEntityViewFromEngines(Dictionary> entityViewEngines, + IEntityData entityView, Type entityViewType) { - FasterList enginesForEntityView; + FasterList enginesForEntityView; if (entityViewEngines.TryGetValue(entityViewType, out enginesForEntityView)) { int count; - var fastList = FasterList.NoVirt.ToArrayFast(enginesForEntityView, out count); + var fastList = FasterList.NoVirt.ToArrayFast(enginesForEntityView, out count); for (var j = 0; j < count; j++) { #if ENGINE_PROFILER_ENABLED && UNITY_EDITOR EngineProfiler.MonitorRemoveDuration(fastList[j], entityView); #else - fastList[j].Remove(entityView); + var handleMixedEntityViewEngine = (fastList[j] as IHandleEntityViewEngine); + handleMixedEntityViewEngine.Remove(entityView); #endif } } @@ -276,9 +264,6 @@ namespace Svelto.ECS //TODO: Use faster dictionary and merge these two? - //Global pool of entity views when engines want to manage entityViews regardless - //the group - readonly Dictionary _globalEntityViewsDB; //indexable entity views when the entity ID is known. Usually useful to handle //event based logic. readonly Dictionary _globalEntityViewsDBDic; diff --git a/Svelto.ECS/EnginesRootSubmission.cs b/Svelto.ECS/EnginesRootSubmission.cs index d57c2e5..3e7de1b 100644 --- a/Svelto.ECS/EnginesRootSubmission.cs +++ b/Svelto.ECS/EnginesRootSubmission.cs @@ -57,8 +57,6 @@ namespace Svelto.ECS //add the entity View in the group 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 AddEntityViewToEntityViewsDictionary(_globalEntityViewsDBDic, entityViewsPerType.Value, entityViewsPerType.Key); } @@ -66,16 +64,16 @@ namespace Svelto.ECS //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 groupToSubmit in groupsToSubmit) { - foreach (var entityViewList in group.Value) + foreach (var entityViewsPerType in groupToSubmit.Value) { - if (entityViewList.Value.isQueryiableEntityView) - { - var type = entityViewList.Key; - for (var current = type; current != _entityViewType; current = current.BaseType) - AddEntityViewToTheSuitableEngines(_entityViewEngines, entityViewList.Value, current); - } + var type = entityViewsPerType.Key; + for (var current = type; + current != _entityViewType && current != _objectType && current != _valueType; + current = current.BaseType) + AddEntityViewToTheSuitableEngines(_entityViewEngines, entityViewsPerType.Value, + current); } } } @@ -108,30 +106,15 @@ namespace Svelto.ECS } } - static void AddEntityViewToTheSuitableEngines(Dictionary> entityViewEngines, ITypeSafeList entityViewsList, Type entityViewType) + static void AddEntityViewToTheSuitableEngines(Dictionary> entityViewEngines, + ITypeSafeList entityViewsList, + Type entityViewType) { - FasterList enginesForEntityView; + FasterList enginesForEntityView; if (entityViewEngines.TryGetValue(entityViewType, out enginesForEntityView)) { - int viewsCount; - - var entityViews = entityViewsList.ToArrayFast(out viewsCount); - - for (int i = 0; i < viewsCount; i++) - { - int count; - var fastList = FasterList.NoVirt.ToArrayFast(enginesForEntityView, out count); - IEntityData entityView = entityViews[i]; - for (int j = 0; j < count; j++) - { -#if ENGINE_PROFILER_ENABLED && UNITY_EDITOR - EngineProfiler.MonitorAddDuration(fastList[j], entityView); -#else - fastList[j].Add(entityView); -#endif - } - } + entityViewsList.Fill(enginesForEntityView); } } diff --git a/Svelto.ECS/EntityFactory.cs b/Svelto.ECS/EntityFactory.cs index f3793a4..83ad1b4 100644 --- a/Svelto.ECS/EntityFactory.cs +++ b/Svelto.ECS/EntityFactory.cs @@ -1,8 +1,5 @@ using System; using System.Collections.Generic; -using Svelto.DataStructures; -using Svelto.Utilities; -using Console = Utility.Console; namespace Svelto.ECS.Internal { @@ -59,8 +56,6 @@ namespace Svelto.ECS.Internal var entityViewsPoolWillBeCreated = entityViewsByType.TryGetValue(entityViewType, out entityViewsList) == false; - IEntityData entityViewObjectToFill; - //passing the undefined entityViewsByType inside the entityViewBuilder will allow //it to be created with the correct type and casted back to the undefined list. //that's how the list will be eventually of the target type. diff --git a/Svelto.ECS/EntityViewBuilder.cs b/Svelto.ECS/EntityViewBuilder.cs index 4200d56..0e980a8 100644 --- a/Svelto.ECS/EntityViewBuilder.cs +++ b/Svelto.ECS/EntityViewBuilder.cs @@ -6,7 +6,7 @@ using Svelto.Utilities; namespace Svelto.ECS { - public class EntityViewBuilder : IEntityViewBuilder where EntityViewType : IEntityData + public class EntityViewBuilder : IEntityViewBuilder where EntityViewType : IEntityData, new() { public void BuildEntityViewAndAddToList(ref ITypeSafeList list, EGID entityID, object[] implementors) { diff --git a/Svelto.ECS/EntityViewsDB.cs b/Svelto.ECS/EntityViewsDB.cs index 9ecd725..733f9a4 100644 --- a/Svelto.ECS/EntityViewsDB.cs +++ b/Svelto.ECS/EntityViewsDB.cs @@ -6,11 +6,9 @@ namespace Svelto.ECS.Internal { class EntityViewsDB : IEntityViewsDB { - internal EntityViewsDB( Dictionary entityViewsDB, - Dictionary entityViewsDBdic, + internal EntityViewsDB( Dictionary entityViewsDBdic, Dictionary> groupEntityViewsDB) { - _globalEntityViewsDB = entityViewsDB; _groupedEntityViewsDBDic = entityViewsDBdic; _groupEntityViewsDB = groupEntityViewsDB; } @@ -21,7 +19,7 @@ namespace Svelto.ECS.Internal ITypeSafeList entityViews; - if (_globalEntityViewsDB.TryGetValue(type, out entityViews) == false) + if (_groupEntityViewsDB[ExclusiveGroups.StandardEntity].TryGetValue(type, out entityViews) == false) return RetrieveEmptyEntityViewList(); return new FasterReadOnlyList((FasterList)entityViews); @@ -48,7 +46,7 @@ namespace Svelto.ECS.Internal ITypeSafeList entityViews; - if (_globalEntityViewsDB.TryGetValue(type, out entityViews) == false) + if (_groupEntityViewsDB[ExclusiveGroups.StandardEntity].TryGetValue(type, out entityViews) == false) return RetrieveEmptyEntityViewArray(); return FasterList.NoVirt.ToArrayFast((FasterList)entityViews, out count); @@ -122,9 +120,6 @@ namespace Svelto.ECS.Internal //grouped set of entity views, this is the standard way to handle entity views readonly Dictionary> _groupEntityViewsDB; - //Global pool of entity views when engines want to manage entityViews regardless - //the group - readonly Dictionary _globalEntityViewsDB; //indexable entity views when the entity ID is known. Usually useful to handle //event based logic. readonly Dictionary _groupedEntityViewsDBDic; diff --git a/Svelto.ECS/Extensions/Unity/UnitySumbmissionEntityViewScheduler .cs b/Svelto.ECS/Extensions/Unity/UnitySumbmissionEntityViewScheduler .cs index 4eab8cd..9d66549 100644 --- a/Svelto.ECS/Extensions/Unity/UnitySumbmissionEntityViewScheduler .cs +++ b/Svelto.ECS/Extensions/Unity/UnitySumbmissionEntityViewScheduler .cs @@ -44,10 +44,10 @@ namespace Svelto.ECS.Schedulers.Unity internal WeakAction OnTick; - WaitForEndOfFrame _wait = new WaitForEndOfFrame(); + readonly WaitForEndOfFrame _wait = new WaitForEndOfFrame(); } - Scheduler _scheduler; + readonly Scheduler _scheduler; } } #endif \ No newline at end of file diff --git a/Svelto.ECS/GenericEntityDescriptor.cs b/Svelto.ECS/GenericEntityDescriptor.cs index e59c152..093b08c 100644 --- a/Svelto.ECS/GenericEntityDescriptor.cs +++ b/Svelto.ECS/GenericEntityDescriptor.cs @@ -1,6 +1,6 @@ namespace Svelto.ECS { - public abstract class GenericEntityDescriptor:IEntityDescriptor where T : struct, IEntityData + public abstract class GenericEntityDescriptor:IEntityDescriptor where T : IEntityData, new() { static GenericEntityDescriptor() { @@ -16,8 +16,8 @@ } - public abstract class GenericEntityDescriptor : IEntityDescriptor where T : struct, IEntityData - where U : struct, IEntityData + public abstract class GenericEntityDescriptor : IEntityDescriptor where T : IEntityData, new() + where U : IEntityData, new() { static GenericEntityDescriptor() { @@ -32,9 +32,9 @@ static readonly IEntityViewBuilder[] entityViewBuilders; } - public abstract class GenericEntityDescriptor : IEntityDescriptor where T : struct, IEntityData - where U : struct, IEntityData - where V : struct, IEntityData + public abstract class GenericEntityDescriptor : IEntityDescriptor where T : IEntityData, new() + where U : IEntityData, new() + where V : IEntityData, new() { static GenericEntityDescriptor() { @@ -49,10 +49,10 @@ static readonly IEntityViewBuilder[] entityViewBuilders; } - public abstract class GenericEntityDescriptor : IEntityDescriptor where T : struct, IEntityData - where U : struct, IEntityData - where V : struct, IEntityData - where W : struct, IEntityData + public abstract class GenericEntityDescriptor : IEntityDescriptor where T : IEntityData, new() + where U : IEntityData, new() + where V : IEntityData, new() + where W : IEntityData, new() { static GenericEntityDescriptor() { @@ -67,11 +67,11 @@ static readonly IEntityViewBuilder[] entityViewBuilders; } - public abstract class GenericEntityDescriptor : IEntityDescriptor where T : struct, IEntityData - where U : struct, IEntityData - where V : struct, IEntityData - where W : struct, IEntityData - where X : struct, IEntityData + public abstract class GenericEntityDescriptor : IEntityDescriptor where T : IEntityData, new() + where U : IEntityData, new() + where V : IEntityData, new() + where W : IEntityData, new() + where X : IEntityData, new() { static GenericEntityDescriptor() { @@ -86,12 +86,12 @@ static readonly IEntityViewBuilder[] entityViewBuilders; } - public abstract class GenericEntityDescriptor : IEntityDescriptor where T : struct, IEntityData - where U : struct, IEntityData - where V : struct, IEntityData - where W : struct, IEntityData - where X : struct, IEntityData - where Y : struct, IEntityData + public abstract class GenericEntityDescriptor : IEntityDescriptor where T : IEntityData, new() + where U : IEntityData, new() + where V : IEntityData, new() + where W : IEntityData, new() + where X : IEntityData, new() + where Y : IEntityData, new() { static GenericEntityDescriptor() { diff --git a/Svelto.ECS/IEngine.cs b/Svelto.ECS/IEngine.cs index bd73d70..a06bd82 100644 --- a/Svelto.ECS/IEngine.cs +++ b/Svelto.ECS/IEngine.cs @@ -1,8 +1,12 @@ +using Svelto.ECS.Internal; + namespace Svelto.ECS.Internal { - public interface IHandleEntityViewEngine : IEngine + public interface IHandleEntityViewEngineAbstracted : IEngine + {} + + public interface IHandleEntityViewEngine : IHandleEntityViewEngineAbstracted { - void Add(IEntityData entityView); void Remove(IEntityData entityView); } } @@ -10,6 +14,10 @@ namespace Svelto.ECS.Internal namespace Svelto.ECS { public interface IEngine + {} + + public interface IHandleEntityStructEngine : IHandleEntityViewEngineAbstracted { + void Add(ref T entityView); } } \ No newline at end of file diff --git a/Svelto.ECS/IEntityFactory.cs b/Svelto.ECS/IEntityFactory.cs index cc63776..34a076c 100644 --- a/Svelto.ECS/IEntityFactory.cs +++ b/Svelto.ECS/IEntityFactory.cs @@ -17,7 +17,7 @@ namespace Svelto.ECS /// /// void PreallocateEntitySpace(int size) where T : IEntityDescriptor, new(); - void PreallocateEntitySpaceInGroup(int groupID, int size) where T : IEntityDescriptor, new(); + void PreallocateEntitySpace(int groupID, int size) where T : IEntityDescriptor, new(); /// /// Using this function is like building a normal entity, but the entityViews @@ -29,8 +29,8 @@ namespace Svelto.ECS /// /// /// - void BuildEntityInGroup(int entityID, int groupID, object[] implementors) where T:IEntityDescriptor, new(); - void BuildEntityInGroup(int entityID, int groupID, EntityDescriptorInfo entityDescriptor, object[] implementors); + void BuildEntity(int entityID, int groupID, object[] implementors) where T:IEntityDescriptor, new(); + void BuildEntity(int entityID, int groupID, EntityDescriptorInfo entityDescriptor, object[] implementors); /// /// The EntityDescriptor doesn't need to be ever instantiated. It just describes the Entity diff --git a/Svelto.ECS/IEntityView.cs b/Svelto.ECS/IEntityView.cs index 0cf96a2..7109aff 100644 --- a/Svelto.ECS/IEntityView.cs +++ b/Svelto.ECS/IEntityView.cs @@ -7,12 +7,22 @@ using Svelto.Utilities; namespace Svelto.ECS { //todo: can I remove the ID from the struct? - public interface IEntityData { EGID ID { get; set; } } + public class EntityView : IEntityData + { + public EGID ID + { + get { return _ID; } + set { _ID = value; } + } + + EGID _ID; + } + static class EntityView where T: IEntityData, new() { internal static T BuildEntityView(EGID ID) diff --git a/Svelto.ECS/MultiEntityViewsEngine.cs b/Svelto.ECS/MultiEntityViewsEngine.cs index f497953..05f18d3 100644 --- a/Svelto.ECS/MultiEntityViewsEngine.cs +++ b/Svelto.ECS/MultiEntityViewsEngine.cs @@ -2,11 +2,12 @@ using Svelto.ECS.Internal; namespace Svelto.ECS.Internal { - public abstract class MultiEntityViewsEngine : IHandleEntityViewEngine where T : class, IEntityData + public abstract class MultiEntityViewsEngine:IHandleEntityStructEngine, + IHandleEntityViewEngine where T:IEntityData { - public virtual void Add(IEntityData entityView) + public void Add(ref T entityView) { - Add((T) entityView); + Add(entityView); } public virtual void Remove(IEntityData entityView) @@ -14,27 +15,19 @@ namespace Svelto.ECS.Internal Remove((T) entityView); } - protected abstract void Add(T entityView); + protected abstract void Add(T entityView); protected abstract void Remove(T entityView); } } namespace Svelto.ECS { - public abstract class MultiEntityViewsEngine : MultiEntityViewsEngine - where U : class, IEntityData where T : class, IEntityData + public abstract class MultiEntityViewsEngine : MultiEntityViewsEngine, IHandleEntityStructEngine + where U : IEntityData where T : IEntityData { protected abstract void Add(U entityView); protected abstract void Remove(U entityView); - public override void Add(IEntityData entityView) - { - if (entityView is U) - Add((U) entityView); - else - base.Add(entityView); - } - public override void Remove(IEntityData entityView) { if (entityView is U) @@ -42,22 +35,19 @@ namespace Svelto.ECS else base.Remove(entityView); } + + public void Add(ref U entityView) + { + Add(entityView); + } } - public abstract class MultiEntityViewsEngine : MultiEntityViewsEngine - where V : class, IEntityData where U : class, IEntityData where T : class, IEntityData + public abstract class MultiEntityViewsEngine : MultiEntityViewsEngine, IHandleEntityStructEngine + where V : IEntityData where U : IEntityData where T : IEntityData { protected abstract void Add(V entityView); protected abstract void Remove(V entityView); - public override void Add(IEntityData entityView) - { - if (entityView is V) - Add((V) entityView); - else - base.Add(entityView); - } - public override void Remove(IEntityData entityView) { if (entityView is V) @@ -65,6 +55,11 @@ namespace Svelto.ECS else base.Remove(entityView); } + + public void Add(ref V entityView) + { + Add(entityView); + } } /// @@ -72,20 +67,12 @@ namespace Svelto.ECS /// if you use more than 4 nodes, your engine has /// already too many responsabilities. /// - public abstract class MultiEntityViewsEngine : MultiEntityViewsEngine - where W : class, IEntityData where V : class, IEntityData where U : class, IEntityData where T : class, IEntityData + public abstract class MultiEntityViewsEngine : MultiEntityViewsEngine, IHandleEntityStructEngine + where W : IEntityData where V : IEntityData where U : IEntityData where T : IEntityData { protected abstract void Add(W entityView); protected abstract void Remove(W entityView); - public override void Add(IEntityData entityView) - { - if (entityView is W) - Add((W) entityView); - else - base.Add(entityView); - } - public override void Remove(IEntityData entityView) { if (entityView is W) @@ -93,5 +80,10 @@ namespace Svelto.ECS else base.Remove(entityView); } + + public void Add(ref W entityView) + { + Add(entityView); + } } } \ No newline at end of file diff --git a/Svelto.ECS/Profiler/EngineProfiler.cs b/Svelto.ECS/Profiler/EngineProfiler.cs index e5b2163..78f027d 100644 --- a/Svelto.ECS/Profiler/EngineProfiler.cs +++ b/Svelto.ECS/Profiler/EngineProfiler.cs @@ -12,28 +12,26 @@ namespace Svelto.ECS.Profiler { static readonly Stopwatch _stopwatch = new Stopwatch(); - public static readonly Dictionary engineInfos = new Dictionary(); - - public static void MonitorAddDuration(IHandleEntityViewEngine engine, IEntityData entityView) + public static void MonitorAddDuration(IHandleEntityViewEngineAbstracted engine, T entityView) { EngineInfo info; if (engineInfos.TryGetValue(engine.GetType(), out info)) { _stopwatch.Start(); - engine.Add(entityView); + (engine as IHandleEntityStructEngine).Add(ref entityView); _stopwatch.Stop(); info.AddAddDuration(_stopwatch.Elapsed.TotalMilliseconds); _stopwatch.Reset(); } } - public static void MonitorRemoveDuration(IHandleEntityViewEngine engine, IEntityData entityView) + public static void MonitorRemoveDuration(IHandleEntityViewEngineAbstracted engine, IEntityData entityView) { EngineInfo info; if (engineInfos.TryGetValue(engine.GetType(), out info)) { _stopwatch.Start(); - engine.Remove(entityView); + (engine as IHandleEntityViewEngine).Remove(entityView); _stopwatch.Stop(); info.AddRemoveDuration(_stopwatch.Elapsed.TotalMilliseconds); @@ -44,12 +42,19 @@ namespace Svelto.ECS.Profiler public static void AddEngine(IEngine engine) { if (engineInfos.ContainsKey(engine.GetType()) == false) + { engineInfos.Add(engine.GetType(), new EngineInfo(engine)); + } } public static void ResetDurations() { - foreach (var engine in engineInfos) engine.Value.ResetDurations(); + foreach (var engine in engineInfos) + { + engine.Value.ResetDurations(); + } } + + public static readonly Dictionary engineInfos = new Dictionary(); } -} \ No newline at end of file +} diff --git a/Svelto.ECS/SingleEntityViewEngine.cs b/Svelto.ECS/SingleEntityViewEngine.cs index aae5e88..ee7acd0 100644 --- a/Svelto.ECS/SingleEntityViewEngine.cs +++ b/Svelto.ECS/SingleEntityViewEngine.cs @@ -6,7 +6,7 @@ namespace Svelto.ECS { public void Add(IEntityData entityView) { - Add((T) entityView); //when byref returns will be vailable, this should be passed by reference, not copy! + Add((T) entityView); } public void Remove(IEntityData entityView)