Browse Source

Merge remote-tracking branch 'remotes/origin/entitystructrework' into EntityViewAsStruct

# Conflicts:
#	Svelto.ECS/DataStructures/TypeSafeFasterListForECS.cs
#	Svelto.ECS/EnginesRootEngines.cs
#	Svelto.ECS/EnginesRootEntities.cs
#	Svelto.ECS/EnginesRootSubmission.cs
#	Svelto.ECS/Extensions/Unity/GenericEntityDescriptorHolder.cs
#	Svelto.ECS/IEngine.cs
#	Svelto.ECS/IEntityDescriptorHolder.cs
#	Svelto.ECS/MultiEntityViewsEngine.cs
#	Svelto.ECS/Profiler/EngineProfiler.cs
#	Svelto.ECS/SingleEntityViewEngine.cs
tags/Rel25a
sebas77 6 years ago
parent
commit
36eaa4fe47
17 changed files with 215 additions and 180 deletions
  1. +70
    -15
      Svelto.ECS/DataStructures/TypeSafeFasterListForECS.cs
  2. +5
    -0
      Svelto.ECS/EGID.cs
  3. +5
    -5
      Svelto.ECS/EnginesRoot.GenericEntityFactory.cs
  4. +10
    -8
      Svelto.ECS/EnginesRootEngines.cs
  5. +21
    -36
      Svelto.ECS/EnginesRootEntities.cs
  6. +13
    -30
      Svelto.ECS/EnginesRootSubmission.cs
  7. +0
    -5
      Svelto.ECS/EntityFactory.cs
  8. +1
    -1
      Svelto.ECS/EntityViewBuilder.cs
  9. +3
    -8
      Svelto.ECS/EntityViewsDB.cs
  10. +2
    -2
      Svelto.ECS/Extensions/Unity/UnitySumbmissionEntityViewScheduler .cs
  11. +21
    -21
      Svelto.ECS/GenericEntityDescriptor.cs
  12. +10
    -2
      Svelto.ECS/IEngine.cs
  13. +3
    -3
      Svelto.ECS/IEntityFactory.cs
  14. +11
    -1
      Svelto.ECS/IEntityView.cs
  15. +26
    -34
      Svelto.ECS/MultiEntityViewsEngine.cs
  16. +13
    -8
      Svelto.ECS/Profiler/EngineProfiler.cs
  17. +1
    -1
      Svelto.ECS/SingleEntityViewEngine.cs

+ 70
- 15
Svelto.ECS/DataStructures/TypeSafeFasterListForECS.cs View File

@@ -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<IHandleEntityViewEngineAbstracted> enginesForEntityView);
}

class TypeSafeFasterListForECS<T> : FasterList<T> where T : IEntityData
public struct EGIDEnumerator:IEnumerable, IEnumerator
{
readonly Dictionary<long, int> _mappedIndices;
Dictionary<long, int>.Enumerator _keysEnumerator;

public EGIDEnumerator(Dictionary<long, int> 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<T> : FasterList<T> where T : IEntityData
{
protected TypeSafeFasterListForECS()
{
_mappedIndices = new Dictionary<long, int>();
@@ -55,7 +81,6 @@ namespace Svelto.ECS.Internal

base.AddRange(entityViewListValue as FasterList<T>);


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<long, int> _mappedIndices;
}

class TypeSafeFasterListForECSForStructs<T> : TypeSafeFasterListForECS<T>, ITypeSafeList
where T : struct, IEntityData
class TypeSafeFasterListForECSForStructs<T> : TypeSafeFasterListForECS<T>, 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<IHandleEntityViewEngineAbstracted> enginesForEntityView)
{
throw new Exception("Not Allowed");
var thisfastList = NoVirt.ToArrayFast(this);
for (int i = 0; i < Count; i++)
{
int count;
var fastList = FasterList<IHandleEntityViewEngineAbstracted>.NoVirt.ToArrayFast(enginesForEntityView, out count);
for (int j = 0; j < count; j++)
{
#if ENGINE_PROFILER_ENABLED
EngineProfiler.MonitorAddDuration<T>(fastList[j], entityView);
#else
(fastList[j] as IHandleEntityStructEngine<T>).Add(ref thisfastList[j]);
#endif
}
}
}

public ITypeSafeList Create(int size)
@@ -131,8 +175,8 @@ namespace Svelto.ECS.Internal
return new TypeSafeFasterListForECSForStructs<T>(size);
}
}
class TypeSafeFasterListForECSForClasses<T> : TypeSafeFasterListForECS<T>, ITypeSafeList where T : IEntityData, new()
class TypeSafeFasterListForECSForClasses<T> : TypeSafeFasterListForECS<T>, ITypeSafeList where T:IEntityData, new()
{
public TypeSafeFasterListForECSForClasses(int size) : base(size)
{}
@@ -155,11 +199,22 @@ namespace Svelto.ECS.Internal
return new TypeSafeDictionaryForClass<T>();
}

public IEntityData[] ToArrayFast(out int count)
public void Fill(FasterList<IHandleEntityViewEngineAbstracted> enginesForEntityView)
{
count = Count;

return ToArrayFast();
var thisfastList = NoVirt.ToArrayFast(this);
for (int i = 0; i < Count; i++)
{
int count;
var fastList = FasterList<IHandleEntityViewEngineAbstracted>.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<T>).Add(ref thisfastList[j]);
#endif
}
}
}

public ITypeSafeList Create(int size)


+ 5
- 0
Svelto.ECS/EGID.cs View File

@@ -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;


+ 5
- 5
Svelto.ECS/EnginesRoot.GenericEntityFactory.cs View File

@@ -27,16 +27,16 @@ namespace Svelto.ECS
_weakEngine.Target.BuildEntity(entityID, entityDescriptor, implementors);
}

public void BuildEntityInGroup<T>(int entityID, int groupID, object[] implementors)
public void BuildEntity<T>(int entityID, int groupID, object[] implementors)
where T : IEntityDescriptor, new()
{
_weakEngine.Target.BuildEntityInGroup<T>(entityID, groupID, implementors);
_weakEngine.Target.BuildEntity<T>(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<T>(int size) where T : IEntityDescriptor, new()
@@ -44,7 +44,7 @@ namespace Svelto.ECS
_weakEngine.Target.Preallocate<T>(ExclusiveGroups.StandardEntity, size);
}
public void PreallocateEntitySpaceInGroup<T>(int groupID, int size) where T : IEntityDescriptor, new()
public void PreallocateEntitySpace<T>(int groupID, int size) where T : IEntityDescriptor, new()
{
_weakEngine.Target.Preallocate<T>(groupID, size);
}


+ 10
- 8
Svelto.ECS/EnginesRootEngines.cs View File

@@ -34,18 +34,18 @@ namespace Svelto.ECS
/// </summary>
public EnginesRoot(EntitySubmissionScheduler entityViewScheduler)
{
_entityViewEngines = new Dictionary<Type, FasterList<IHandleEntityViewEngine>>();
_entityViewEngines = new Dictionary<Type, FasterList<IHandleEntityViewEngineAbstracted>>();
_otherEngines = new FasterList<IEngine>();

_globalEntityViewsDB = new Dictionary<Type, ITypeSafeList>();
_groupEntityViewsDB = new Dictionary<int, Dictionary<Type, ITypeSafeList>>();
_groupEntityViewsDB[ExclusiveGroups.StandardEntity] = new Dictionary<Type, ITypeSafeList>();
_globalEntityViewsDBDic = new Dictionary<Type, ITypeSafeDictionary>();
_entityInfos = new Dictionary<long, IEntityViewBuilder[]>();
_groupedEntityViewsToAdd = new DoubleBufferedEntityViews<Dictionary<int, Dictionary<Type, ITypeSafeList>>>();

_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<Type, FasterList<IHandleEntityViewEngine>> _entityViewEngines;
readonly Dictionary<Type, FasterList<IHandleEntityViewEngineAbstracted>> _entityViewEngines;
readonly FasterList<IEngine> _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);
}
}

+ 21
- 36
Svelto.ECS/EnginesRootEntities.cs View File

@@ -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<T>(int entityID, object[] implementors = null) where T : IEntityDescriptor, new()
{
BuildEntityInGroup<T>
BuildEntity<T>
(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
/// <param name="entityID"></param>
/// <param name="groupID"></param>
/// <param name="implementors"></param>
void BuildEntityInGroup<T>(int entityID, int groupID, object[] implementors = null)
void BuildEntity<T>(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<Type, ITypeSafeList> @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<Type, FasterList<IHandleEntityViewEngine>> entityViewEngines,
IEntityData entityView,
Type entityViewType)
static void RemoveEntityViewFromEngines(Dictionary<Type, FasterList<IHandleEntityViewEngineAbstracted>> entityViewEngines,
IEntityData entityView, Type entityViewType)
{
FasterList<IHandleEntityViewEngine> enginesForEntityView;
FasterList<IHandleEntityViewEngineAbstracted> enginesForEntityView;

if (entityViewEngines.TryGetValue(entityViewType, out enginesForEntityView))
{
int count;
var fastList = FasterList<IHandleEntityViewEngine>.NoVirt.ToArrayFast(enginesForEntityView, out count);
var fastList = FasterList<IHandleEntityViewEngineAbstracted>.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<Type, ITypeSafeList> _globalEntityViewsDB;
//indexable entity views when the entity ID is known. Usually useful to handle
//event based logic.
readonly Dictionary<Type, ITypeSafeDictionary> _globalEntityViewsDBDic;


+ 13
- 30
Svelto.ECS/EnginesRootSubmission.cs View File

@@ -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<Type, FasterList<IHandleEntityViewEngine>> entityViewEngines, ITypeSafeList entityViewsList, Type entityViewType)
static void AddEntityViewToTheSuitableEngines(Dictionary<Type, FasterList<IHandleEntityViewEngineAbstracted>> entityViewEngines,
ITypeSafeList entityViewsList,
Type entityViewType)
{
FasterList<IHandleEntityViewEngine> enginesForEntityView;
FasterList<IHandleEntityViewEngineAbstracted> 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<IHandleEntityViewEngine>.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);
}
}


+ 0
- 5
Svelto.ECS/EntityFactory.cs View File

@@ -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.


+ 1
- 1
Svelto.ECS/EntityViewBuilder.cs View File

@@ -6,7 +6,7 @@ using Svelto.Utilities;

namespace Svelto.ECS
{
public class EntityViewBuilder<EntityViewType> : IEntityViewBuilder where EntityViewType : IEntityData
public class EntityViewBuilder<EntityViewType> : IEntityViewBuilder where EntityViewType : IEntityData, new()
{
public void BuildEntityViewAndAddToList(ref ITypeSafeList list, EGID entityID, object[] implementors)
{


+ 3
- 8
Svelto.ECS/EntityViewsDB.cs View File

@@ -6,11 +6,9 @@ namespace Svelto.ECS.Internal
{
class EntityViewsDB : IEntityViewsDB
{
internal EntityViewsDB( Dictionary<Type, ITypeSafeList> entityViewsDB,
Dictionary<Type, ITypeSafeDictionary> entityViewsDBdic,
internal EntityViewsDB( Dictionary<Type, ITypeSafeDictionary> entityViewsDBdic,
Dictionary<int, Dictionary<Type, ITypeSafeList>> 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<T>();

return new FasterReadOnlyList<T>((FasterList<T>)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<T>();
return FasterList<T>.NoVirt.ToArrayFast((FasterList<T>)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<int, Dictionary<Type, ITypeSafeList>> _groupEntityViewsDB;
//Global pool of entity views when engines want to manage entityViews regardless
//the group
readonly Dictionary<Type, ITypeSafeList> _globalEntityViewsDB;
//indexable entity views when the entity ID is known. Usually useful to handle
//event based logic.
readonly Dictionary<Type, ITypeSafeDictionary> _groupedEntityViewsDBDic;


+ 2
- 2
Svelto.ECS/Extensions/Unity/UnitySumbmissionEntityViewScheduler .cs View File

@@ -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

+ 21
- 21
Svelto.ECS/GenericEntityDescriptor.cs View File

@@ -1,6 +1,6 @@
namespace Svelto.ECS
{
public abstract class GenericEntityDescriptor<T>:IEntityDescriptor where T : struct, IEntityData
public abstract class GenericEntityDescriptor<T>:IEntityDescriptor where T : IEntityData, new()
{
static GenericEntityDescriptor()
{
@@ -16,8 +16,8 @@
}

public abstract class GenericEntityDescriptor<T, U> : IEntityDescriptor where T : struct, IEntityData
where U : struct, IEntityData
public abstract class GenericEntityDescriptor<T, U> : IEntityDescriptor where T : IEntityData, new()
where U : IEntityData, new()
{
static GenericEntityDescriptor()
{
@@ -32,9 +32,9 @@
static readonly IEntityViewBuilder[] entityViewBuilders;
}

public abstract class GenericEntityDescriptor<T, U, V> : IEntityDescriptor where T : struct, IEntityData
where U : struct, IEntityData
where V : struct, IEntityData
public abstract class GenericEntityDescriptor<T, U, V> : 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<T, U, V, W> : IEntityDescriptor where T : struct, IEntityData
where U : struct, IEntityData
where V : struct, IEntityData
where W : struct, IEntityData
public abstract class GenericEntityDescriptor<T, U, V, W> : 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<T, U, V, W, X> : 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<T, U, V, W, X> : 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<T, U, V, W, X, Y> : 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<T, U, V, W, X, Y> : 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()
{


+ 10
- 2
Svelto.ECS/IEngine.cs View File

@@ -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<T> : IHandleEntityViewEngineAbstracted
{
void Add(ref T entityView);
}
}

+ 3
- 3
Svelto.ECS/IEntityFactory.cs View File

@@ -17,7 +17,7 @@ namespace Svelto.ECS
/// <typeparam name="T"></typeparam>
/// <param name="size"></param>
void PreallocateEntitySpace<T>(int size) where T : IEntityDescriptor, new();
void PreallocateEntitySpaceInGroup<T>(int groupID, int size) where T : IEntityDescriptor, new();
void PreallocateEntitySpace<T>(int groupID, int size) where T : IEntityDescriptor, new();

/// <summary>
/// Using this function is like building a normal entity, but the entityViews
@@ -29,8 +29,8 @@ namespace Svelto.ECS
/// <param name="groupID"></param>
/// <param name="ed"></param>
/// <param name="implementors"></param>
void BuildEntityInGroup<T>(int entityID, int groupID, object[] implementors) where T:IEntityDescriptor, new();
void BuildEntityInGroup(int entityID, int groupID, EntityDescriptorInfo entityDescriptor, object[] implementors);
void BuildEntity<T>(int entityID, int groupID, object[] implementors) where T:IEntityDescriptor, new();
void BuildEntity(int entityID, int groupID, EntityDescriptorInfo entityDescriptor, object[] implementors);

/// <summary>
/// The EntityDescriptor doesn't need to be ever instantiated. It just describes the Entity


+ 11
- 1
Svelto.ECS/IEntityView.cs View File

@@ -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<T> where T: IEntityData, new()
{
internal static T BuildEntityView(EGID ID)


+ 26
- 34
Svelto.ECS/MultiEntityViewsEngine.cs View File

@@ -2,11 +2,12 @@ using Svelto.ECS.Internal;

namespace Svelto.ECS.Internal
{
public abstract class MultiEntityViewsEngine<T> : IHandleEntityViewEngine where T : class, IEntityData
public abstract class MultiEntityViewsEngine<T>:IHandleEntityStructEngine<T>,
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<T, U> : MultiEntityViewsEngine<T>
where U : class, IEntityData where T : class, IEntityData
public abstract class MultiEntityViewsEngine<T, U> : MultiEntityViewsEngine<T>, IHandleEntityStructEngine<U>
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<T, U, V> : MultiEntityViewsEngine<T, U>
where V : class, IEntityData where U : class, IEntityData where T : class, IEntityData
public abstract class MultiEntityViewsEngine<T, U, V> : MultiEntityViewsEngine<T, U>, IHandleEntityStructEngine<V>
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);
}
}

/// <summary>
@@ -72,20 +67,12 @@ namespace Svelto.ECS
/// if you use more than 4 nodes, your engine has
/// already too many responsabilities.
/// </summary>
public abstract class MultiEntityViewsEngine<T, U, V, W> : MultiEntityViewsEngine<T, U, V>
where W : class, IEntityData where V : class, IEntityData where U : class, IEntityData where T : class, IEntityData
public abstract class MultiEntityViewsEngine<T, U, V, W> : MultiEntityViewsEngine<T, U, V>, IHandleEntityStructEngine<W>
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);
}
}
}

+ 13
- 8
Svelto.ECS/Profiler/EngineProfiler.cs View File

@@ -12,28 +12,26 @@ namespace Svelto.ECS.Profiler
{
static readonly Stopwatch _stopwatch = new Stopwatch();

public static readonly Dictionary<Type, EngineInfo> engineInfos = new Dictionary<Type, EngineInfo>();

public static void MonitorAddDuration(IHandleEntityViewEngine engine, IEntityData entityView)
public static void MonitorAddDuration<T>(IHandleEntityViewEngineAbstracted engine, T entityView)
{
EngineInfo info;
if (engineInfos.TryGetValue(engine.GetType(), out info))
{
_stopwatch.Start();
engine.Add(entityView);
(engine as IHandleEntityStructEngine<T>).Add(ref entityView);
_stopwatch.Stop();
info.AddAddDuration(_stopwatch.Elapsed.TotalMilliseconds);
_stopwatch.Reset();
}
}

public static void MonitorRemoveDuration(IHandleEntityViewEngine engine, IEntityData entityView)
public static void MonitorRemoveDuration<T>(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<Type, EngineInfo> engineInfos = new Dictionary<Type, EngineInfo>();
}
}
}

+ 1
- 1
Svelto.ECS/SingleEntityViewEngine.cs View File

@@ -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)


Loading…
Cancel
Save