From f32ad2b82249ec8232ef200c69bd4d1c3a92d36b Mon Sep 17 00:00:00 2001 From: sebas77 Date: Wed, 23 May 2018 23:17:30 +0100 Subject: [PATCH] Improve the work on the the EntityStructs --- Svelto.ECS/EntityFactory.cs | 4 +- Svelto.ECS/EntityViewBuilder.cs | 62 ++++++++++----- Svelto.ECS/EntityViewStructBuilder.cs | 57 -------------- Svelto.ECS/IEntityView.cs | 5 +- Svelto.ECS/IEntityViewBuilder.cs | 6 +- Svelto.ECS/MixedEntityDescriptor.cs | 107 -------------------------- 6 files changed, 50 insertions(+), 191 deletions(-) delete mode 100644 Svelto.ECS/EntityViewStructBuilder.cs delete mode 100644 Svelto.ECS/MixedEntityDescriptor.cs diff --git a/Svelto.ECS/EntityFactory.cs b/Svelto.ECS/EntityFactory.cs index 343b287..15ffa7a 100644 --- a/Svelto.ECS/EntityFactory.cs +++ b/Svelto.ECS/EntityFactory.cs @@ -65,7 +65,7 @@ namespace Svelto.ECS.Internal entityViewsByType.Add(entityViewType, safeDictionary); } - static readonly EntityViewStructBuilder _viewBuilder = new EntityViewStructBuilder(); - static readonly Type _viewType = typeof(EntityInfoView); + static readonly EntityViewBuilder _viewBuilder = new EntityViewBuilder(); + 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 10c4e2c..64db11a 100644 --- a/Svelto.ECS/EntityViewBuilder.cs +++ b/Svelto.ECS/EntityViewBuilder.cs @@ -6,38 +6,55 @@ using Svelto.Utilities; namespace Svelto.ECS { - public class EntityViewBuilder : IEntityViewBuilder where EntityViewType : IEntityData, new() + public class EntityViewBuilder : IEntityViewBuilder where T : IEntityData, new() { - public void BuildEntityViewAndAddToList(ref ITypeSafeDictionary list, EGID entityID, object[] implementors) + public EntityViewBuilder(ref T initializer) { - if (list == null) - list = new TypeSafeDictionary(); + _initializer = initializer; + } + + public EntityViewBuilder() + { + _initializer = default(T); + } + + public void BuildEntityViewAndAddToList(ref ITypeSafeDictionary dictionary, EGID entityID, object[] implementors) + { + if (dictionary == null) + dictionary = new TypeSafeDictionary(); - var castedDic = list as TypeSafeDictionary; + var castedDic = dictionary as TypeSafeDictionary; - DBC.Check.Require(implementors != null, "Implementors not found while building an EntityView"); + if (needsReflection == true) { - EntityViewType lentityView; - - EntityView.BuildEntityView(entityID, out lentityView); + DBC.Check.Require(implementors != null, "Implementors not found while building an EntityView"); + + T lentityView; + EntityView.BuildEntityView(entityID, out lentityView); this.FillEntityView(ref lentityView , entityViewBlazingFastReflection , implementors , DESCRIPTOR_NAME); - + castedDic.Add(entityID.entityID, ref lentityView); } + else + { + _initializer.ID = entityID; + + castedDic.Add(entityID.entityID, _initializer); + } } - public ITypeSafeDictionary Preallocate(ref ITypeSafeDictionary list, int size) + public ITypeSafeDictionary Preallocate(ref ITypeSafeDictionary dictionary, int size) { - if (list == null) - list = new TypeSafeDictionary(size); + if (dictionary == null) + dictionary = new TypeSafeDictionary(size); else - list.AddCapacity(size); + dictionary.AddCapacity(size); - return list; + return dictionary; } public Type GetEntityType() @@ -45,21 +62,24 @@ namespace Svelto.ECS return ENTITY_VIEW_TYPE; } - public void MoveEntityView(EGID entityID, ITypeSafeDictionary fromSafeList, ITypeSafeDictionary toSafeList) + public void MoveEntityView(EGID entityID, ITypeSafeDictionary fromSafeDic, ITypeSafeDictionary toSafeDic) { - var fromCastedDic = fromSafeList as TypeSafeDictionary; - var toCastedDic = toSafeList as TypeSafeDictionary; + var fromCastedDic = fromSafeDic as TypeSafeDictionary; + var toCastedDic = toSafeDic as TypeSafeDictionary; toCastedDic.Add(entityID.entityID, fromCastedDic[entityID.entityID]); fromCastedDic.Remove(entityID.entityID); } - FasterList>> entityViewBlazingFastReflection + FasterList>> entityViewBlazingFastReflection { - get { return EntityView.FieldCache.list; } + get { return EntityView.FieldCache.list; } } - static readonly Type ENTITY_VIEW_TYPE = typeof(EntityViewType); + static readonly Type ENTITY_VIEW_TYPE = typeof(T); static string DESCRIPTOR_NAME = ENTITY_VIEW_TYPE.ToString(); + + internal T _initializer; + readonly bool needsReflection = typeof(IEntityView).IsAssignableFrom(typeof(T)); } } \ No newline at end of file diff --git a/Svelto.ECS/EntityViewStructBuilder.cs b/Svelto.ECS/EntityViewStructBuilder.cs deleted file mode 100644 index 143cd46..0000000 --- a/Svelto.ECS/EntityViewStructBuilder.cs +++ /dev/null @@ -1,57 +0,0 @@ -using System; -using Svelto.ECS.Internal; - -namespace Svelto.ECS -{ - public class EntityViewStructBuilder : IEntityViewBuilder where EntityViewType : struct, IEntityData - { - public EntityViewStructBuilder(ref EntityViewType initializer) - { - _initializer = initializer; - } - - public EntityViewStructBuilder() - { - _initializer = default(EntityViewType); - } - - public void BuildEntityViewAndAddToList(ref ITypeSafeDictionary list, EGID entityID, object[] implementors = null) - { - _initializer.ID = entityID; - - if (list == null) - list = new TypeSafeDictionary(); - - var castedDic = list as TypeSafeDictionary; - - castedDic.Add(entityID.entityID, _initializer); - } - - public ITypeSafeDictionary Preallocate(ref ITypeSafeDictionary list, int size) - { - if (list == null) - list = new TypeSafeDictionary(size); - else - list.AddCapacity(size); - - return list; - } - - public Type GetEntityType() - { - return ENTITY_VIEW_TYPE; - } - - public void MoveEntityView(EGID entityID, ITypeSafeDictionary fromSafeList, ITypeSafeDictionary toSafeList) - { - var fromCastedDic = fromSafeList as TypeSafeDictionary; - var toCastedDic = toSafeList as TypeSafeDictionary; - - toCastedDic.Add(entityID.entityID, fromCastedDic[entityID.entityID]); - fromCastedDic.Remove(entityID.entityID); - } - - static readonly Type ENTITY_VIEW_TYPE = typeof(EntityViewType); - internal EntityViewType _initializer; - } -} \ No newline at end of file diff --git a/Svelto.ECS/IEntityView.cs b/Svelto.ECS/IEntityView.cs index 74773ae..0636877 100644 --- a/Svelto.ECS/IEntityView.cs +++ b/Svelto.ECS/IEntityView.cs @@ -12,7 +12,10 @@ namespace Svelto.ECS EGID ID { get; set; } } - public class EntityView : IEntityData + public interface IEntityView:IEntityData + {} + + public class EntityView : IEntityView { public EGID ID { diff --git a/Svelto.ECS/IEntityViewBuilder.cs b/Svelto.ECS/IEntityViewBuilder.cs index 0295df0..7f847b0 100644 --- a/Svelto.ECS/IEntityViewBuilder.cs +++ b/Svelto.ECS/IEntityViewBuilder.cs @@ -5,10 +5,10 @@ namespace Svelto.ECS { public interface IEntityViewBuilder { - void BuildEntityViewAndAddToList(ref ITypeSafeDictionary list, EGID entityID, object[] implementors); - ITypeSafeDictionary Preallocate(ref ITypeSafeDictionary list, int size); + void BuildEntityViewAndAddToList(ref ITypeSafeDictionary dictionary, EGID entityID, object[] implementors); + ITypeSafeDictionary Preallocate(ref ITypeSafeDictionary dictionary, int size); Type GetEntityType(); - void MoveEntityView(EGID entityID, ITypeSafeDictionary fromSafeList, ITypeSafeDictionary toSafeList); + void MoveEntityView(EGID entityID, ITypeSafeDictionary fromSafeDic, ITypeSafeDictionary toSafeDic); } } \ No newline at end of file diff --git a/Svelto.ECS/MixedEntityDescriptor.cs b/Svelto.ECS/MixedEntityDescriptor.cs deleted file mode 100644 index 3a27826..0000000 --- a/Svelto.ECS/MixedEntityDescriptor.cs +++ /dev/null @@ -1,107 +0,0 @@ -namespace Svelto.ECS -{ - public abstract class MixedEntityDescriptor:IEntityDescriptor where T : class, IEntityViewBuilder, new() - { - static MixedEntityDescriptor() - { - _entityViewsToBuild = new IEntityViewBuilder[] {new T()}; - } - - public IEntityViewBuilder[] entityViewsToBuild - { - get { return _entityViewsToBuild; } - } - - static readonly IEntityViewBuilder[] _entityViewsToBuild; - } - - public abstract class MixedEntityDescriptor : IEntityDescriptor where T : class, IEntityViewBuilder, new() - where U : class, IEntityViewBuilder, new() - { - static MixedEntityDescriptor() - { - _entityViewsToBuild = new IEntityViewBuilder[] {new T(), new U()}; - } - - public IEntityViewBuilder[] entityViewsToBuild - { - get { return _entityViewsToBuild; } - } - - static readonly IEntityViewBuilder[] _entityViewsToBuild; - } - - public abstract class MixedEntityDescriptor : IEntityDescriptor where T : class, IEntityViewBuilder, new() - where U : class, IEntityViewBuilder, new() - where V : class, IEntityViewBuilder, new() - { - static MixedEntityDescriptor() - { - _entityViewsToBuild = new IEntityViewBuilder[] {new T(), new U(), new V()}; - } - - public IEntityViewBuilder[] entityViewsToBuild - { - get { return _entityViewsToBuild; } - } - - static readonly IEntityViewBuilder[] _entityViewsToBuild; - } - - public abstract class MixedEntityDescriptor : IEntityDescriptor where T : class, IEntityViewBuilder, new() - where U : class, IEntityViewBuilder, new() - where V : class, IEntityViewBuilder, new() - where W : class, IEntityViewBuilder, new() - { - static MixedEntityDescriptor() - { - _entityViewsToBuild = new IEntityViewBuilder[] {new T(), new U(), new V(), new W()}; - } - - public IEntityViewBuilder[] entityViewsToBuild - { - get { return _entityViewsToBuild; } - } - - static readonly IEntityViewBuilder[] _entityViewsToBuild; - } - - public abstract class MixedEntityDescriptor : IEntityDescriptor where T : class, IEntityViewBuilder, new() - where U : class, IEntityViewBuilder, new() - where V : class, IEntityViewBuilder, new() - where W : class, IEntityViewBuilder, new() - where X : class, IEntityViewBuilder, new() - { - static MixedEntityDescriptor() - { - _entityViewsToBuild = new IEntityViewBuilder[] {new T(), new U(), new V(), new W(), new X()}; - } - - public IEntityViewBuilder[] entityViewsToBuild - { - get { return _entityViewsToBuild; } - } - - static readonly IEntityViewBuilder[] _entityViewsToBuild; - } - - public abstract class MixedEntityDescriptor : IEntityDescriptor where T : class, IEntityViewBuilder, new() - where U : class, IEntityViewBuilder, new() - where V : class, IEntityViewBuilder, new() - where W : class, IEntityViewBuilder, new() - where X : class, IEntityViewBuilder, new() - where Y : class, IEntityViewBuilder, new() - { - static MixedEntityDescriptor() - { - _entityViewsToBuild = new IEntityViewBuilder[] {new T(), new U(), new V(), new W(), new X(), new Y()}; - } - - public IEntityViewBuilder[] entityViewsToBuild - { - get { return _entityViewsToBuild; } - } - - static readonly IEntityViewBuilder[] _entityViewsToBuild; - } -}