diff --git a/Svelto.ECS/DataStructures/TypeSafeDictionary.cs b/Svelto.ECS/DataStructures/TypeSafeDictionary.cs
index 0ffde6b..d1e7dcc 100644
--- a/Svelto.ECS/DataStructures/TypeSafeDictionary.cs
+++ b/Svelto.ECS/DataStructures/TypeSafeDictionary.cs
@@ -1,29 +1,28 @@
using System;
-using Svelto.DataStructures;
using System.Collections.Generic;
+using Svelto.DataStructures;
namespace Svelto.ECS.Internal
{
///
- /// This is just a place holder at the moment
- /// I always wanted to create my own Dictionary
- /// data structure as excercise, but never had the
- /// time to. At the moment I need the custom interface
- /// wrapped though.
+ /// This is just a place holder at the moment
+ /// I always wanted to create my own Dictionary
+ /// data structure as excercise, but never had the
+ /// time to. At the moment I need the custom interface
+ /// wrapped though.
///
-
public interface ITypeSafeDictionary
{
- void FillWithIndexedEntityViews(ITypeSafeList entityViews);
- bool Remove(int entityId);
- IEntityView GetIndexedEntityView(int entityID);
+ void FillWithIndexedEntityViews(ITypeSafeList entityViews);
+ bool Remove(int entityId);
+ IEntityView GetIndexedEntityView(int entityID);
}
- class TypeSafeDictionary : Dictionary, ITypeSafeDictionary where TValue:IEntityView
+ class TypeSafeDictionary : Dictionary, ITypeSafeDictionary where TValue : IEntityView
{
- internal static readonly ReadOnlyDictionary Default =
+ internal static readonly ReadOnlyDictionary Default =
new ReadOnlyDictionary(new Dictionary());
-
+
public void FillWithIndexedEntityViews(ITypeSafeList entityViews)
{
int count;
@@ -31,7 +30,7 @@ namespace Svelto.ECS.Internal
try
{
- for (int i = 0; i < count; i++)
+ for (var i = 0; i < count; i++)
{
var entityView = buffer[i];
@@ -48,7 +47,7 @@ namespace Svelto.ECS.Internal
{
base.Remove(entityId);
- return this.Count > 0;
+ return Count > 0;
}
public IEntityView GetIndexedEntityView(int entityID)
@@ -56,4 +55,4 @@ namespace Svelto.ECS.Internal
return this[entityID];
}
}
-}
+}
\ No newline at end of file
diff --git a/Svelto.ECS/DataStructures/TypeSafeDictionaryException.cs b/Svelto.ECS/DataStructures/TypeSafeDictionaryException.cs
index 1db957d..4f2a321 100644
--- a/Svelto.ECS/DataStructures/TypeSafeDictionaryException.cs
+++ b/Svelto.ECS/DataStructures/TypeSafeDictionaryException.cs
@@ -4,7 +4,8 @@ namespace Svelto.ECS
{
public class TypeSafeDictionaryException : Exception
{
- public TypeSafeDictionaryException(Exception exception):base(exception.Message, exception)
- {}
+ public TypeSafeDictionaryException(Exception exception) : base(exception.Message, exception)
+ {
+ }
}
}
\ No newline at end of file
diff --git a/Svelto.ECS/DataStructures/TypeSafeFasterListForECS.cs b/Svelto.ECS/DataStructures/TypeSafeFasterListForECS.cs
index 17235f2..8175857 100644
--- a/Svelto.ECS/DataStructures/TypeSafeFasterListForECS.cs
+++ b/Svelto.ECS/DataStructures/TypeSafeFasterListForECS.cs
@@ -1,61 +1,65 @@
using System;
using System.Collections;
using System.Collections.Generic;
+using DesignByContract;
using Svelto.DataStructures;
namespace Svelto.ECS.Internal
{
- public interface ITypeSafeList: IEnumerable
+ public interface ITypeSafeList : IEnumerable
{
+ bool isQueryiableEntityView { get; }
void AddRange(ITypeSafeList entityViewListValue);
- ITypeSafeList Create();
- bool isQueryiableEntityView { get; }
- bool UnorderedRemove(int entityID);
+ ITypeSafeList Create();
+ bool MappedRemove(int entityID);
ITypeSafeDictionary CreateIndexedDictionary();
- IEntityView[] ToArrayFast(out int count);
- void ReserveCapacity(int capacity);
+ IEntityView[] ToArrayFast(out int count);
+ void ReserveCapacity(int capacity);
}
- class TypeSafeFasterListForECS: FasterList where T:IEntityView
+ class TypeSafeFasterListForECS : FasterList where T : IEntityView
{
+ readonly Dictionary _mappedIndices;
+
protected TypeSafeFasterListForECS()
{
_mappedIndices = new Dictionary();
}
- protected TypeSafeFasterListForECS(int size):base(size)
+ protected TypeSafeFasterListForECS(int size) : base(size)
{
_mappedIndices = new Dictionary();
}
-
- public bool UnorderedRemove(int entityID)
+
+ public bool MappedRemove(int entityID)
{
var index = _mappedIndices[entityID];
- DesignByContract.Check.Assert(entityID == this[index].ID, "Something went wrong with the Svelto.ECS code, please contact the author");
+ Check.Assert(entityID == this[index].ID,
+ "Something went wrong with the Svelto.ECS code, please contact the author");
_mappedIndices.Remove(entityID);
if (UnorderedRemoveAt(index))
_mappedIndices[this[index].ID] = index;
- return this.Count > 0;
+ return Count > 0;
}
-
+
public void AddRange(ITypeSafeList entityViewListValue)
{
- var index = this.Count;
-
+ var index = Count;
+
base.AddRange(entityViewListValue as FasterList);
-
- for (int i = index; i < Count; ++i)
+
+ for (var i = index; i < Count; ++i)
_mappedIndices[this[i].ID] = i;
}
- new public void Add(T entityView)
+ public new void Add(T entityView)
{
- var index = this.Count;
+ var index = Count;
base.Add(entityView);
@@ -64,7 +68,7 @@ namespace Svelto.ECS.Internal
public void ReserveCapacity(int capacity)
{
- if (this.ToArrayFast().Length < capacity)
+ if (ToArrayFast().Length < capacity)
Resize(capacity);
}
@@ -72,17 +76,18 @@ namespace Svelto.ECS.Internal
{
return _mappedIndices[entityID];
}
-
- readonly Dictionary _mappedIndices;
}
- class TypeSafeFasterListForECSForStructs : TypeSafeFasterListForECS, ITypeSafeList where T:struct, IEntityStruct
+ class TypeSafeFasterListForECSForStructs : TypeSafeFasterListForECS, ITypeSafeList
+ where T : struct, IEntityStruct
{
- public TypeSafeFasterListForECSForStructs(int size):base(size)
- {}
+ public TypeSafeFasterListForECSForStructs(int size) : base(size)
+ {
+ }
public TypeSafeFasterListForECSForStructs()
- {}
+ {
+ }
public ITypeSafeList Create()
{
@@ -109,14 +114,16 @@ namespace Svelto.ECS.Internal
return new TypeSafeFasterListForECSForStructs(size);
}
}
-
- class TypeSafeFasterListForECSForClasses : TypeSafeFasterListForECS, ITypeSafeList where T:EntityView, new()
+
+ class TypeSafeFasterListForECSForClasses : TypeSafeFasterListForECS, ITypeSafeList where T : EntityView, new()
{
- public TypeSafeFasterListForECSForClasses(int size):base(size)
- {}
+ public TypeSafeFasterListForECSForClasses(int size) : base(size)
+ {
+ }
public TypeSafeFasterListForECSForClasses()
- {}
+ {
+ }
public ITypeSafeList Create()
{
@@ -135,9 +142,9 @@ namespace Svelto.ECS.Internal
public IEntityView[] ToArrayFast(out int count)
{
- count = this.Count;
-
- return this.ToArrayFast();
+ count = Count;
+
+ return ToArrayFast();
}
public ITypeSafeList Create(int size)
@@ -145,4 +152,4 @@ namespace Svelto.ECS.Internal
return new TypeSafeFasterListForECSForClasses(size);
}
}
-}
+}
\ No newline at end of file
diff --git a/Svelto.ECS/EnginesRootEntities.cs b/Svelto.ECS/EnginesRootEntities.cs
index 6a3adbb..0a50abf 100644
--- a/Svelto.ECS/EnginesRootEntities.cs
+++ b/Svelto.ECS/EnginesRootEntities.cs
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
+using DesignByContract;
using Svelto.DataStructures;
using Svelto.ECS.Internal;
@@ -11,10 +12,24 @@ namespace Svelto.ECS
{
public partial class EnginesRoot : IDisposable
{
+ public void Dispose()
+ {
+ foreach (var entity in _entityViewsDB)
+ if (entity.Value.isQueryiableEntityView)
+ foreach (var entityView in entity.Value)
+ RemoveEntityViewFromEngines(_entityViewEngines, entityView as EntityView, entity.Key);
+
+ foreach (var entity in _metaEntityViewsDB)
+ {
+ foreach (var entityView in entity.Value)
+ RemoveEntityViewFromEngines(_entityViewEngines, entityView as EntityView, entity.Key);
+ }
+ }
+
///
- /// an EnginesRoot reference cannot be held by anything else than the Composition Root
- /// where it has been created. IEntityFactory and IEntityFunctions allow a weakreference
- /// of the EnginesRoot to be passed around.
+ /// an EnginesRoot reference cannot be held by anything else than the Composition Root
+ /// where it has been created. IEntityFactory and IEntityFunctions allow a weakreference
+ /// of the EnginesRoot to be passed around.
///
///
public IEntityFactory GenerateEntityFactory()
@@ -28,10 +43,10 @@ namespace Svelto.ECS
}
///
- /// The EntityDescriptor doesn't need to be ever instantiated. It just describes the Entity
- /// itself in terms of EntityViews to build. The Implementors are passed to fill the
- /// references of the EntityViews components. Please read the articles on my blog
- /// to understand better the terminologies
+ /// The EntityDescriptor doesn't need to be ever instantiated. It just describes the Entity
+ /// itself in terms of EntityViews to build. The Implementors are passed to fill the
+ /// references of the EntityViews components. Please read the articles on my blog
+ /// to understand better the terminologies
///
///
///
@@ -43,8 +58,8 @@ namespace Svelto.ECS
}
///
- /// When the type of the entity is not known (this is a special case!) an EntityDescriptorInfo
- /// can be built in place of the generic parameter T.
+ /// When the type of the entity is not known (this is a special case!) an EntityDescriptorInfo
+ /// can be built in place of the generic parameter T.
///
///
///
@@ -56,26 +71,26 @@ namespace Svelto.ECS
}
///
- /// A meta entity is a way to manage a set of entitites that are not easily
- /// queriable otherwise. For example you may want to group existing entities
- /// by size and type and then use the meta entity entityView to manage the data
- /// shared among the single entities of the same type and size. This will
- /// prevent the scenario where the coder is forced to parse all the entities to
- /// find the ones of the same size and type.
- /// Since the entities are managed through the shared entityView, the same
- /// shared entityView must be found on the single entities of the same type and size.
- /// The shared entityView of the meta entity is then used by engines that are meant
- /// to manage a group of entities through a single entityView.
- /// The same engine can manage several meta entities entityViews too.
- /// The Engine manages the logic of the Meta EntityView data and other engines
- /// can read back this data through the normal entity as the shared entityView
- /// will be present in their descriptor too.
- /// It's a way to control a group of Entities through a entityView only.
- /// This set of entities can share exactly the same entityView reference if
- /// built through this function. In this way, if you need to set a variable
- /// on a group of entities, instead to inject N entityViews and iterate over
- /// them to set the same value, you can inject just one entityView, set the value
- /// and be sure that the value is shared between entities.
+ /// A meta entity is a way to manage a set of entitites that are not easily
+ /// queriable otherwise. For example you may want to group existing entities
+ /// by size and type and then use the meta entity entityView to manage the data
+ /// shared among the single entities of the same type and size. This will
+ /// prevent the scenario where the coder is forced to parse all the entities to
+ /// find the ones of the same size and type.
+ /// Since the entities are managed through the shared entityView, the same
+ /// shared entityView must be found on the single entities of the same type and size.
+ /// The shared entityView of the meta entity is then used by engines that are meant
+ /// to manage a group of entities through a single entityView.
+ /// The same engine can manage several meta entities entityViews too.
+ /// The Engine manages the logic of the Meta EntityView data and other engines
+ /// can read back this data through the normal entity as the shared entityView
+ /// will be present in their descriptor too.
+ /// It's a way to control a group of Entities through a entityView only.
+ /// This set of entities can share exactly the same entityView reference if
+ /// built through this function. In this way, if you need to set a variable
+ /// on a group of entities, instead to inject N entityViews and iterate over
+ /// them to set the same value, you can inject just one entityView, set the value
+ /// and be sure that the value is shared between entities.
///
///
///
@@ -87,39 +102,43 @@ namespace Svelto.ECS
}
///
- /// Using this function is like building a normal entity, but the entityViews
- /// are grouped by groupID to be more efficently processed inside engines and
- /// improve cache locality. Either class entityViews and struct entityViews can be
- /// grouped.
+ /// Using this function is like building a normal entity, but the entityViews
+ /// are grouped by groupID to be more efficently processed inside engines and
+ /// improve cache locality. Either class entityViews and struct entityViews can be
+ /// grouped.
///
///
///
///
///
- void BuildEntityInGroup(int entityID, int groupID, object[] implementors = null) where T : IEntityDescriptor, new()
+ void BuildEntityInGroup(int entityID, int groupID, object[] implementors = null)
+ where T : IEntityDescriptor, new()
{
EntityFactory.BuildGroupedEntityViews(entityID, groupID,
_groupedEntityViewsToAdd.current,
+ _entityViewsToAdd.current,
EntityDescriptorTemplate.Default,
implementors);
}
- void BuildEntityInGroup(int entityID, int groupID, IEntityDescriptorInfo entityDescriptor, object[] implementors = null)
+ void BuildEntityInGroup(int entityID, int groupID, IEntityDescriptorInfo entityDescriptor,
+ object[] implementors = null)
{
EntityFactory.BuildGroupedEntityViews(entityID, groupID,
_groupedEntityViewsToAdd.current,
+ _entityViewsToAdd.current,
entityDescriptor, implementors);
}
void Preallocate(int size) where T : IEntityDescriptor, new()
{
var entityViewsToBuild = ((EntityDescriptorInfo) EntityDescriptorTemplate.Default).entityViewsToBuild;
- int count = entityViewsToBuild.Length;
+ var count = entityViewsToBuild.Length;
- for (int index = 0; index < count; index++)
+ for (var index = 0; index < count; index++)
{
var entityViewBuilder = entityViewsToBuild[index];
- var entityViewType = entityViewBuilder.GetEntityViewType();
+ var entityViewType = entityViewBuilder.GetEntityViewType();
ITypeSafeList dbList;
if (_entityViewsDB.TryGetValue(entityViewType, out dbList) == false)
@@ -134,28 +153,31 @@ namespace Svelto.ECS
}
}
- void RemoveEntity(ref EntityInfoView entityInfoView, Dictionary viewsDB, Dictionary entityViewsDBDic)
+ void RemoveEntity(ref EntityInfoView entityInfoView, Dictionary viewsDB,
+ Dictionary entityViewsDBDic)
{
if (entityInfoView.isInAGroup)
- InternalRemoveFromGroupAndDBAndEngines(entityInfoView.entityViews, entityInfoView.ID, entityInfoView.groupID, viewsDB, entityViewsDBDic);
+ InternalRemoveFromGroupAndDBAndEngines(entityInfoView.entityViews, entityInfoView.ID,
+ entityInfoView.groupID, viewsDB, entityViewsDBDic);
else
- InternalRemoveFromDBAndEngines(entityInfoView.entityViews, entityInfoView.ID, viewsDB, entityViewsDBDic);
+ InternalRemoveFromDBAndEngines(entityInfoView.entityViews, entityInfoView.ID, viewsDB,
+ entityViewsDBDic);
}
-
+
void RemoveEntity(int entityID)
{
var entityInfoView = _DB.QueryEntityView(entityID);
-
+
RemoveEntity(ref entityInfoView, _entityViewsDB, _entityViewsDBDic);
}
void RemoveMetaEntity(int metaEntityID)
{
var entityInfoView = _DB.QueryMetaEntityView(metaEntityID);
-
+
RemoveEntity(ref entityInfoView, _metaEntityViewsDB, _metaEntityViewsDBDic);
}
-
+
void RemoveGroupAndEntitiesFromDB(int groupID)
{
foreach (var group in _groupEntityViewsDB[groupID])
@@ -165,29 +187,31 @@ namespace Svelto.ECS
int count;
var entities = group.Value.ToArrayFast(out count);
- for (int i = 0; i < count; i++)
+ for (var i = 0; i < count; i++)
{
var entityID = entities[i].ID;
- InternalRemoveEntityViewFromDBAndEngines(_entityViewsDB, _entityViewsDBDic, entityViewType, entityID);
+ InternalRemoveEntityViewFromDBAndEngines(_entityViewsDB, _entityViewsDBDic, entityViewType,
+ entityID);
}
}
-
+
_groupEntityViewsDB.Remove(groupID);
}
-
- void InternalRemoveEntityViewFromDBAndEngines(Dictionary entityViewsDB,
+
+ void InternalRemoveEntityViewFromDBAndEngines(Dictionary entityViewsDB,
Dictionary entityViewsDBDic,
- Type entityViewType, int entityID)
+ Type entityViewType,
+ int entityID)
{
var entityViews = entityViewsDB[entityViewType];
- if (entityViews.UnorderedRemove(entityID) == false)
+ if (entityViews.MappedRemove(entityID) == false)
entityViewsDB.Remove(entityViewType);
if (entityViews.isQueryiableEntityView)
{
var typeSafeDictionary = entityViewsDBDic[entityViewType];
- var entityView = typeSafeDictionary.GetIndexedEntityView(entityID);
+ var entityView = typeSafeDictionary.GetIndexedEntityView(entityID);
if (typeSafeDictionary.Remove(entityID) == false)
entityViewsDBDic.Remove(entityViewType);
@@ -199,12 +223,13 @@ namespace Svelto.ECS
void SwapEntityGroup(int entityID, int fromGroupID, int toGroupID)
{
- DesignByContract.Check.Require(fromGroupID != toGroupID, "can't move an entity to the same group where it already belongs to");
+ Check.Require(fromGroupID != toGroupID,
+ "can't move an entity to the same group where it already belongs to");
- var entityViewBuilders = _DB.QueryEntityView(entityID).entityViews;
- int entityViewBuildersCount = entityViewBuilders.Length;
+ var entityViewBuilders = _DB.QueryEntityView(entityID).entityViews;
+ var entityViewBuildersCount = entityViewBuilders.Length;
- var dictionary = _groupEntityViewsDB[fromGroupID];
+ var groupedEntities = _groupEntityViewsDB[fromGroupID];
Dictionary groupedEntityViewsTyped;
if (_groupEntityViewsDB.TryGetValue(toGroupID, out groupedEntityViewsTyped) == false)
@@ -214,43 +239,46 @@ namespace Svelto.ECS
_groupEntityViewsDB.Add(toGroupID, groupedEntityViewsTyped);
}
- for (int i = 0; i < entityViewBuildersCount; i++)
+ for (var i = 0; i < entityViewBuildersCount; i++)
{
- IEntityViewBuilder entityViewBuilder = entityViewBuilders[i];
- Type entityViewType = entityViewBuilder.GetEntityViewType();
+ var entityViewBuilder = entityViewBuilders[i];
+ var entityViewType = entityViewBuilder.GetEntityViewType();
- ITypeSafeList fromSafeList = dictionary[entityViewType];
+ var fromSafeList = groupedEntities[entityViewType];
ITypeSafeList toSafeList;
if (groupedEntityViewsTyped.TryGetValue(entityViewType, out toSafeList) == false)
- {
- toSafeList = fromSafeList.Create();
- }
+ groupedEntityViewsTyped[entityViewType] = toSafeList = fromSafeList.Create();
entityViewBuilder.MoveEntityView(entityID, fromSafeList, toSafeList);
- if (fromSafeList.UnorderedRemove(entityID) == false)
- dictionary.Remove(entityViewType);
+ fromSafeList.MappedRemove(entityID);
}
- if (dictionary.Count == 0) _groupEntityViewsDB.Remove(fromGroupID);
+ var entityInfoView = _DB.QueryEntityView(entityID);
+ entityInfoView.groupID = toGroupID;
}
- void InternalRemoveFromDBAndEngines(IEntityViewBuilder[] entityViewBuilders, int entityID,
- Dictionary entityViewsDB, Dictionary entityViewsDBDic)
+ void InternalRemoveFromDBAndEngines(IEntityViewBuilder[] entityViewBuilders, int entityID,
+ Dictionary entityViewsDB,
+ Dictionary entityViewsDBDic)
{
- int entityViewBuildersCount = entityViewBuilders.Length;
+ var entityViewBuildersCount = entityViewBuilders.Length;
- for (int i = 0; i < entityViewBuildersCount; i++)
+ for (var i = 0; i < entityViewBuildersCount; i++)
{
- Type entityViewType = entityViewBuilders[i].GetEntityViewType();
+ var entityViewType = entityViewBuilders[i].GetEntityViewType();
InternalRemoveEntityViewFromDBAndEngines(entityViewsDB, entityViewsDBDic, entityViewType, entityID);
}
+
+ InternalRemoveEntityViewFromDBAndEngines(entityViewsDB, entityViewsDBDic, typeof(EntityInfoView), entityID);
}
- void InternalRemoveFromGroupAndDBAndEngines(IEntityViewBuilder[] entityViewBuilders, int entityID, int groupID,
- Dictionary entityViewsDB, Dictionary entityViewsDBDic)
+ void InternalRemoveFromGroupAndDBAndEngines(IEntityViewBuilder[] entityViewBuilders,
+ int entityID, int groupID,
+ Dictionary entityViewsDB,
+ Dictionary entityViewsDBDic)
{
InternalRemoveFromGroupDB(entityViewBuilders, entityID, groupID);
@@ -259,20 +287,22 @@ namespace Svelto.ECS
void InternalRemoveFromGroupDB(IEntityViewBuilder[] entityViewBuilders, int entityID, int groupID)
{
- int entityViewBuildersCount = entityViewBuilders.Length;
+ var entityViewBuildersCount = entityViewBuilders.Length;
- Dictionary dictionary = _groupEntityViewsDB[groupID];
+ var dictionary = _groupEntityViewsDB[groupID];
- for (int i = 0; i < entityViewBuildersCount; i++)
+ for (var i = 0; i < entityViewBuildersCount; i++)
{
- Type entityViewType = entityViewBuilders[i].GetEntityViewType();
+ var entityViewType = entityViewBuilders[i].GetEntityViewType();
- dictionary[entityViewType].UnorderedRemove(entityID);
+ var typeSafeList = dictionary[entityViewType];
+ typeSafeList.MappedRemove(entityID);
}
}
static void RemoveEntityViewFromEngines(Dictionary> entityViewEngines,
- IEntityView entityView, Type entityViewType)
+ IEntityView entityView,
+ Type entityViewType)
{
FasterList enginesForEntityView;
@@ -281,7 +311,7 @@ namespace Svelto.ECS
int count;
var fastList = FasterList.NoVirt.ToArrayFast(enginesForEntityView, out count);
- for (int j = 0; j < count; j++)
+ for (var j = 0; j < count; j++)
{
#if ENGINE_PROFILER_ENABLED && UNITY_EDITOR
EngineProfiler.MonitorRemoveDuration(fastList[j], entityView);
@@ -294,7 +324,7 @@ namespace Svelto.ECS
class GenericEntityFactory : IEntityFactory
{
- DataStructures.WeakReference _weakEngine;
+ readonly DataStructures.WeakReference _weakEngine;
public GenericEntityFactory(DataStructures.WeakReference weakReference)
{
@@ -316,12 +346,14 @@ namespace Svelto.ECS
_weakEngine.Target.BuildMetaEntity(metaEntityID, implementors);
}
- public void BuildEntityInGroup(int entityID, int groupID, object[] implementors) where T : IEntityDescriptor, new()
+ public void BuildEntityInGroup(int entityID, int groupID, object[] implementors)
+ where T : IEntityDescriptor, new()
{
_weakEngine.Target.BuildEntityInGroup(entityID, groupID, implementors);
}
- public void BuildEntityInGroup(int entityID, int groupID, IEntityDescriptorInfo entityDescriptor, object[] implementors)
+ public void BuildEntityInGroup(int entityID, int groupID, IEntityDescriptorInfo entityDescriptor,
+ object[] implementors)
{
_weakEngine.Target.BuildEntityInGroup(entityID, groupID, entityDescriptor, implementors);
}
@@ -334,6 +366,8 @@ namespace Svelto.ECS
class GenericEntityFunctions : IEntityFunctions
{
+ readonly DataStructures.WeakReference _weakReference;
+
public GenericEntityFunctions(DataStructures.WeakReference weakReference)
{
_weakReference = weakReference;
@@ -358,39 +392,16 @@ namespace Svelto.ECS
{
_weakReference.Target.SwapEntityGroup(entityID, fromGroupID, toGroupID);
}
-
- readonly DataStructures.WeakReference _weakReference;
}
- public void Dispose()
- {
- foreach (var entity in _entityViewsDB)
- {
- if (entity.Value.isQueryiableEntityView == true)
- {
- foreach (var entityView in entity.Value)
- {
- RemoveEntityViewFromEngines(_entityViewEngines, entityView as EntityView, entity.Key);
- }
- }
- }
-
- foreach (var entity in _metaEntityViewsDB)
- {
- foreach (var entityView in entity.Value)
- {
- RemoveEntityViewFromEngines(_entityViewEngines, entityView as EntityView, entity.Key);
- }
- }
- }
-
readonly EntityViewsDB _DB;
-
+
readonly Dictionary _entityViewsDB;
- readonly Dictionary _metaEntityViewsDB;
+
+ readonly Dictionary _entityViewsDBDic;
readonly Dictionary> _groupEntityViewsDB;
-
- readonly Dictionary _entityViewsDBDic;
- readonly Dictionary _metaEntityViewsDBDic;
+ readonly Dictionary _metaEntityViewsDB;
+ readonly Dictionary _metaEntityViewsDBDic;
+
}
}
\ No newline at end of file
diff --git a/Svelto.ECS/EntityDescriptor.cs b/Svelto.ECS/EntityDescriptor.cs
index 6b1bc21..9ea0385 100644
--- a/Svelto.ECS/EntityDescriptor.cs
+++ b/Svelto.ECS/EntityDescriptor.cs
@@ -1,6 +1,7 @@
+using System;
+using DesignByContract;
using Svelto.DataStructures;
using Svelto.ECS.Internal;
-using System;
namespace Svelto.ECS
{
@@ -8,20 +9,21 @@ namespace Svelto.ECS
{
IEntityViewBuilder[] entityViewsToBuild { get; }
}
-
- public class EntityDescriptor:IEntityDescriptor
+
+ public class EntityDescriptor : IEntityDescriptor
{
protected EntityDescriptor(IEntityViewBuilder[] entityViewsToBuild)
{
this.entityViewsToBuild = entityViewsToBuild;
}
- public IEntityViewBuilder[] entityViewsToBuild { get; private set; }
+ public IEntityViewBuilder[] entityViewsToBuild { get; }
}
public interface IEntityDescriptorInfo
- {}
-
+ {
+ }
+
public static class EntityDescriptorTemplate where TType : IEntityDescriptor, new()
{
public static readonly IEntityDescriptorInfo Default = new EntityDescriptorInfo(new TType());
@@ -31,16 +33,17 @@ namespace Svelto.ECS
{
public DynamicEntityDescriptorInfo(FasterList extraEntityViews)
{
- DesignByContract.Check.Require(extraEntityViews.Count > 0, "don't use a DynamicEntityDescriptorInfo if you don't need to use extra EntityViews");
-
+ Check.Require(extraEntityViews.Count > 0,
+ "don't use a DynamicEntityDescriptorInfo if you don't need to use extra EntityViews");
+
var descriptor = new TType();
- int length = descriptor.entityViewsToBuild.Length;
-
+ var length = descriptor.entityViewsToBuild.Length;
+
entityViewsToBuild = new IEntityViewBuilder[length + extraEntityViews.Count];
-
+
Array.Copy(descriptor.entityViewsToBuild, 0, entityViewsToBuild, 0, length);
Array.Copy(extraEntityViews.ToArrayFast(), 0, entityViewsToBuild, length, extraEntityViews.Count);
-
+
name = descriptor.ToString();
}
}
@@ -48,19 +51,19 @@ namespace Svelto.ECS
namespace Svelto.ECS.Internal
{
- public class EntityDescriptorInfo:IEntityDescriptorInfo
+ public class EntityDescriptorInfo : IEntityDescriptorInfo
{
internal IEntityViewBuilder[] entityViewsToBuild;
- internal string name;
+ internal string name;
internal EntityDescriptorInfo(IEntityDescriptor descriptor)
{
- name = descriptor.ToString();
+ name = descriptor.ToString();
entityViewsToBuild = descriptor.entityViewsToBuild;
}
protected EntityDescriptorInfo()
- {}
+ {
+ }
}
-}
-
+}
\ No newline at end of file
diff --git a/Svelto.ECS/EntityFactory.cs b/Svelto.ECS/EntityFactory.cs
index 78cba8a..5ce8640 100644
--- a/Svelto.ECS/EntityFactory.cs
+++ b/Svelto.ECS/EntityFactory.cs
@@ -2,17 +2,20 @@ using System;
using System.Collections.Generic;
using Svelto.DataStructures;
using Svelto.Utilities;
+using Console = Utility.Console;
namespace Svelto.ECS.Internal
{
static class EntityFactory
{
- internal static void BuildGroupedEntityViews(int entityID, int groupID,
+ internal static void BuildGroupedEntityViews(int entityID, int groupID,
Dictionary> groupEntityViewsByType,
- IEntityDescriptorInfo eentityViewsToBuildDescriptor,
- object[] implementors)
+ Dictionary entityViewsByType,
+ IEntityDescriptorInfo eentityViewsToBuildDescriptor,
+ object[] implementors)
{
- var entityViewsToBuildDescriptor = eentityViewsToBuildDescriptor as EntityDescriptorInfo;
+ var entityViewsToBuildDescriptor =
+ eentityViewsToBuildDescriptor as EntityDescriptorInfo;
Dictionary groupedEntityViewsTyped;
if (groupEntityViewsByType.TryGetValue(groupID, out groupedEntityViewsTyped) == false)
@@ -22,51 +25,53 @@ namespace Svelto.ECS.Internal
}
InternalBuildEntityViews(entityID, groupedEntityViewsTyped, entityViewsToBuildDescriptor, implementors);
-
- EntityInfoView removeEntityView = new EntityInfoView();
- removeEntityView.groupID = groupID;
- removeEntityView.isInAGroup = true;
+ var removeEntityView = EntityView.BuildEntityView(entityID);
+
+ removeEntityView.groupID = groupID;
+ removeEntityView.isInAGroup = true;
removeEntityView.entityViews = entityViewsToBuildDescriptor.entityViewsToBuild;
-
- AddEntityInfoView(groupEntityViewsByType[groupID], removeEntityView);
+
+ AddEntityInfoView(entityViewsByType, removeEntityView);
}
- internal static void BuildEntityViews(int entityID,
+ internal static void BuildEntityViews(int entityID,
Dictionary entityViewsByType,
IEntityDescriptorInfo eentityViewsToBuildDescriptor,
object[] implementors)
{
var entityViewsToBuildDescriptor = eentityViewsToBuildDescriptor as EntityDescriptorInfo;
-
+
InternalBuildEntityViews(entityID, entityViewsByType, entityViewsToBuildDescriptor, implementors);
-
- EntityInfoView removeEntityView = new EntityInfoView();
+
+ var removeEntityView = EntityView.BuildEntityView(entityID);
removeEntityView.entityViews = entityViewsToBuildDescriptor.entityViewsToBuild;
-
+
AddEntityInfoView(entityViewsByType, removeEntityView);
}
-
- static void AddEntityInfoView(Dictionary entityViewsByType, EntityInfoView removeEntityView)
+
+ static void AddEntityInfoView(Dictionary entityViewsByType,
+ EntityInfoView removeEntityView)
{
ITypeSafeList list;
-
+
if (entityViewsByType.TryGetValue(typeof(EntityInfoView), out list) == false)
- list = entityViewsByType[typeof(EntityInfoView)] = new TypeSafeFasterListForECSForClasses();
-
+ list = entityViewsByType[typeof(EntityInfoView)] =
+ new TypeSafeFasterListForECSForClasses();
+
(list as TypeSafeFasterListForECSForClasses).Add(removeEntityView);
}
- static void InternalBuildEntityViews(int entityID,
- Dictionary entityViewsByType,
- IEntityDescriptorInfo eentityViewsToBuildDescriptor,
+ static void InternalBuildEntityViews(int entityID,
+ Dictionary entityViewsByType,
+ IEntityDescriptorInfo eentityViewsToBuildDescriptor,
object[] implementors)
{
var entityViewsToBuildDescriptor = eentityViewsToBuildDescriptor as EntityDescriptorInfo;
var entityViewsToBuild = entityViewsToBuildDescriptor.entityViewsToBuild;
- int count = entityViewsToBuild.Length;
+ var count = entityViewsToBuild.Length;
- for (int index = 0; index < count; index++)
+ for (var index = 0; index < count; index++)
{
var entityViewBuilder = entityViewsToBuild[index];
var entityViewType = entityViewBuilder.GetEntityViewType();
@@ -74,12 +79,10 @@ namespace Svelto.ECS.Internal
var entityViewObjectToFill =
BuildEntityView(entityID, entityViewsByType, entityViewType, entityViewBuilder);
- if (entityViewBuilder.mustBeFilled == true)
- {
+ if (entityViewBuilder.mustBeFilled)
FillEntityView(entityViewObjectToFill as EntityView
- ,implementors
- ,entityViewsToBuildDescriptor.name);
- }
+ , implementors
+ , entityViewsToBuildDescriptor.name);
}
}
@@ -106,24 +109,26 @@ namespace Svelto.ECS.Internal
//this is used to avoid newing a dictionary every time, but it's used locally only and it's clearead for each use
#if DEBUG && !PROFILER
- static readonly Dictionary> implementorsByType = new Dictionary>();
+ static readonly Dictionary> implementorsByType =
+ new Dictionary>();
#else
static readonly Dictionary implementorsByType = new Dictionary();
#endif
static void FillEntityView(EntityView entityView
- , object[] implementors
- , string entityDescriptorName)
+ , object[] implementors
+ , string entityDescriptorName)
{
int count;
//Very efficent way to collect the fields of every EntityViewType
- KeyValuePair>[] setters =
- FasterList>>.NoVirt.ToArrayFast(entityView.entityViewBlazingFastReflection, out count);
+ var setters =
+ FasterList>>
+ .NoVirt.ToArrayFast(entityView.entityViewBlazingFastReflection, out count);
if (count == 0) return;
-
- for (int index = 0; index < implementors.Length; index++)
+
+ for (var index = 0; index < implementors.Length; index++)
{
var implementor = implementors[index];
@@ -133,15 +138,15 @@ namespace Svelto.ECS.Internal
Type[] interfaces;
if (_cachedTypes.TryGetValue(type, out interfaces) == false)
- interfaces = _cachedTypes[type] = type.GetInterfacesEx();
+ interfaces = _cachedTypes[type] = type.GetInterfacesEx();
- for (int iindex = 0; iindex < interfaces.Length; iindex++)
+ for (var iindex = 0; iindex < interfaces.Length; iindex++)
{
var componentType = interfaces[iindex];
#if DEBUG && !PROFILER
Tuple