@@ -13,21 +13,24 @@ namespace Svelto.ECS.Internal | |||
/// </summary> | |||
public interface ITypeSafeDictionary | |||
{ | |||
void RemoveEntityFromDicAndEngines(EGID entityGid, | |||
void RemoveEntitiesFromEngines(Dictionary<Type, FasterList<IHandleEntityViewEngineAbstracted>> | |||
entityViewEnginesDB); | |||
void RemoveEntityFromEngines(EGID entityGid, | |||
Dictionary<Type, FasterList<IHandleEntityViewEngineAbstracted>> | |||
entityViewEnginesDB); | |||
void RemoveEntityViewsFromEngines( | |||
Dictionary<Type, FasterList<IHandleEntityViewEngineAbstracted>> entityViewEngines); | |||
void AddCapacity(int size); | |||
bool Remove(long idGid); | |||
bool Remove(int idGid); | |||
ITypeSafeDictionary Create(); | |||
int Count { get; } | |||
void FillWithIndexedEntityViews(ITypeSafeDictionary entityViews); | |||
void AddEntityViewsToEngines(Dictionary<Type, FasterList<IHandleEntityViewEngineAbstracted>> entityViewEnginesDB); | |||
} | |||
class TypeSafeDictionary<TValue> : FasterDictionary<long, TValue>, ITypeSafeDictionary where TValue : IEntityData | |||
class TypeSafeDictionary<TValue> : FasterDictionary<int, TValue>, ITypeSafeDictionary where TValue : IEntityData | |||
{ | |||
static Type _type = typeof(TValue); | |||
public TypeSafeDictionary(int size):base(size) | |||
{} | |||
@@ -45,7 +48,7 @@ namespace Svelto.ECS.Internal | |||
{ | |||
var entityView = buffer[i]; | |||
Add(entityView.ID.GID, entityView); | |||
Add(entityView.ID.entityID, entityView); | |||
} | |||
} | |||
catch (Exception e) | |||
@@ -70,42 +73,36 @@ namespace Svelto.ECS.Internal | |||
void AddEntityViewFromEngines(Dictionary<Type, FasterList<IHandleEntityViewEngineAbstracted>> entityViewEnginesDB, ref TValue entity) | |||
{ | |||
FasterList<IHandleEntityViewEngineAbstracted> entityViewsEngines; | |||
if (entityViewEnginesDB.TryGetValue(typeof(TValue), out entityViewsEngines)) | |||
//get all the engines linked to TValue | |||
if (entityViewEnginesDB.TryGetValue(_type, out entityViewsEngines)) | |||
for (int i = 0; i < entityViewsEngines.Count; i++) | |||
(entityViewsEngines[i] as IHandleEntityStructEngine<TValue>).AddInternal(ref entity); | |||
} | |||
public void RemoveEntityFromDicAndEngines(EGID entityGid, | |||
public void RemoveEntityFromEngines(EGID entityGid, | |||
Dictionary<Type, FasterList<IHandleEntityViewEngineAbstracted>> | |||
entityViewEnginesDB) | |||
{ | |||
TValue entity = this[entityGid.GID]; | |||
RemoveEntityViewFromEngines(entityViewEnginesDB, ref entity); | |||
Remove(entityGid.GID); | |||
RemoveEntityViewFromEngines(entityViewEnginesDB, ref GetFasterValuesBuffer()[GetValueIndex(entityGid.entityID)]); | |||
} | |||
static void RemoveEntityViewFromEngines(Dictionary<Type, FasterList<IHandleEntityViewEngineAbstracted>> entityViewEnginesDB, ref TValue entity) | |||
static void RemoveEntityViewFromEngines | |||
(Dictionary<Type, FasterList<IHandleEntityViewEngineAbstracted>> entityViewEnginesDB, ref TValue entity) | |||
{ | |||
FasterList<IHandleEntityViewEngineAbstracted> entityViewsEngines; | |||
if (entityViewEnginesDB.TryGetValue(typeof(TValue), out entityViewsEngines)) | |||
if (entityViewEnginesDB.TryGetValue(_type, out entityViewsEngines)) | |||
for (int i = 0; i < entityViewsEngines.Count; i++) | |||
(entityViewsEngines[i] as IHandleEntityStructEngine<TValue>).RemoveInternal(ref entity); | |||
} | |||
public void RemoveEntityViewsFromEngines(Dictionary<Type, FasterList<IHandleEntityViewEngineAbstracted>> entityViewEnginesDB) | |||
public void RemoveEntitiesFromEngines(Dictionary<Type, FasterList<IHandleEntityViewEngineAbstracted>> entityViewEnginesDB) | |||
{ | |||
int count; | |||
TValue[] values = GetFasterValuesBuffer(out count); | |||
for (int i = 0; i < count; i++) | |||
{ | |||
TValue entity = values[i]; | |||
RemoveEntityViewFromEngines(entityViewEnginesDB, ref entity); | |||
RemoveEntityViewFromEngines(entityViewEnginesDB, ref values[i]); | |||
} | |||
} | |||
@@ -34,15 +34,15 @@ namespace Svelto.ECS | |||
/// </summary> | |||
public EnginesRoot(EntitySubmissionScheduler entityViewScheduler) | |||
{ | |||
_entityViewEngines = new Dictionary<Type, FasterList<IHandleEntityViewEngineAbstracted>>(); | |||
_entityEngines = new Dictionary<Type, FasterList<IHandleEntityViewEngineAbstracted>>(); | |||
_otherEngines = new FasterList<IEngine>(); | |||
_groupEntityViewsDB = new Dictionary<int, Dictionary<Type, ITypeSafeDictionary>>(); | |||
_groupEntityViewsDB[ExclusiveGroups.StandardEntity] = new Dictionary<Type, ITypeSafeDictionary>(); | |||
_groupEntityDB = new Dictionary<int, Dictionary<Type, ITypeSafeDictionary>>(); | |||
_groupEntityDB[ExclusiveGroups.StandardEntity] = new Dictionary<Type, ITypeSafeDictionary>(); | |||
_groupedEntityViewsToAdd = new DoubleBufferedEntityViews<Dictionary<int, Dictionary<Type, ITypeSafeDictionary>>>(); | |||
_groupedEntityToAdd = new DoubleBufferedEntityViews<Dictionary<int, Dictionary<Type, ITypeSafeDictionary>>>(); | |||
_DB = new EntityViewsDB(_groupEntityViewsDB); | |||
_DB = new EntityViewsDB(_groupEntityDB); | |||
_scheduler = entityViewScheduler; | |||
_scheduler.Schedule(new WeakAction(SubmitEntityViews)); | |||
@@ -78,7 +78,7 @@ namespace Svelto.ECS | |||
{ | |||
var genericArguments = baseType.GetGenericArgumentsEx(); | |||
AddEngine(engine as IHandleEntityViewEngineAbstracted, genericArguments, _entityViewEngines); | |||
AddEngine(engine as IHandleEntityViewEngineAbstracted, genericArguments, _entityEngines); | |||
return; | |||
} | |||
@@ -115,11 +115,9 @@ namespace Svelto.ECS | |||
list.Add(engine); | |||
} | |||
readonly Dictionary<Type, FasterList<IHandleEntityViewEngineAbstracted>> _entityViewEngines; | |||
readonly Dictionary<Type, FasterList<IHandleEntityViewEngineAbstracted>> _entityEngines; | |||
readonly FasterList<IEngine> _otherEngines; | |||
static readonly Type _entityViewType= typeof(IEntityData); | |||
static readonly Type _objectType = typeof(object); | |||
static readonly Type _valueType = typeof(ValueType); | |||
} | |||
} |
@@ -18,9 +18,9 @@ namespace Svelto.ECS | |||
/// </summary> | |||
public void Dispose() | |||
{ | |||
foreach (var groups in _groupEntityViewsDB) | |||
foreach (var groups in _groupEntityDB) | |||
foreach (var entityList in groups.Value) | |||
entityList.Value.RemoveEntityViewsFromEngines(_entityViewEngines); | |||
entityList.Value.RemoveEntitiesFromEngines(_entityEngines); | |||
} | |||
///-------------------------------------------- | |||
@@ -42,7 +42,7 @@ namespace Svelto.ECS | |||
where T : IEntityDescriptor, new() | |||
{ | |||
EntityFactory.BuildGroupedEntityViews(entityID, | |||
_groupedEntityViewsToAdd.current, | |||
_groupedEntityToAdd.current, | |||
EntityDescriptorTemplate<T>.Info, | |||
implementors); | |||
} | |||
@@ -51,7 +51,7 @@ namespace Svelto.ECS | |||
object[] implementors) | |||
{ | |||
EntityFactory.BuildGroupedEntityViews(entityID, | |||
_groupedEntityViewsToAdd.current, | |||
_groupedEntityToAdd.current, | |||
entityDescriptorInfo, | |||
implementors); | |||
} | |||
@@ -72,23 +72,23 @@ namespace Svelto.ECS | |||
for (var index = 0; index < count; index++) | |||
{ | |||
var entityViewBuilder = entityViewsToBuild[index]; | |||
var entityViewType = entityViewBuilder.GetEntityViewType(); | |||
var entityViewType = entityViewBuilder.GetEntityType(); | |||
//reserve space for the global pool | |||
ITypeSafeDictionary dbList; | |||
//reserve space for the single group | |||
Dictionary<Type, ITypeSafeDictionary> @group; | |||
if (_groupEntityViewsDB.TryGetValue(groupID, out group) == false) | |||
group = _groupEntityViewsDB[groupID] = new Dictionary<Type, ITypeSafeDictionary>(); | |||
if (_groupEntityDB.TryGetValue(groupID, out group) == false) | |||
group = _groupEntityDB[groupID] = new Dictionary<Type, ITypeSafeDictionary>(); | |||
if (group.TryGetValue(entityViewType, out dbList) == false) | |||
group[entityViewType] = entityViewBuilder.Preallocate(ref dbList, size); | |||
else | |||
dbList.AddCapacity(size); | |||
if (_groupedEntityViewsToAdd.current.TryGetValue(groupID, out group) == false) | |||
group = _groupEntityViewsDB[groupID] = new Dictionary<Type, ITypeSafeDictionary>(); | |||
if (_groupedEntityToAdd.current.TryGetValue(groupID, out group) == false) | |||
group = _groupEntityDB[groupID] = new Dictionary<Type, ITypeSafeDictionary>(); | |||
//reserve space to the temporary buffer | |||
if (group.TryGetValue(entityViewType, out dbList) == false) | |||
@@ -102,36 +102,39 @@ namespace Svelto.ECS | |||
/// | |||
void RemoveEntity(EGID entityGID) | |||
{ | |||
var typeSafeDictionary = _groupEntityViewsDB[entityGID.groupID][typeof(EntityInfoView)] as TypeSafeDictionary<EntityInfoView>; | |||
var entityInfoView = typeSafeDictionary[entityGID.GID]; | |||
var entityViewBuilders = entityInfoView.entityViewsToBuild; | |||
var entityViewBuildersCount = entityViewBuilders.Length; | |||
var entityViewInfoDictionary = _groupEntityDB[entityGID.groupID][_typeEntityInfoView] as TypeSafeDictionary<EntityInfoView>; | |||
var entityInfoView = entityViewInfoDictionary[entityGID.entityID]; | |||
var entityBuilders = entityInfoView.entityToBuild; | |||
var entityBuildersCount = entityBuilders.Length; | |||
//for each entity view generated by the entity descriptor | |||
for (var i = 0; i < entityViewBuildersCount; i++) | |||
for (var i = 0; i < entityBuildersCount; i++) | |||
{ | |||
var entityViewType = entityViewBuilders[i].GetEntityViewType(); | |||
var group = _groupEntityViewsDB[entityGID.groupID]; | |||
var entityType = entityBuilders[i].GetEntityType(); | |||
var group = _groupEntityDB[entityGID.groupID]; | |||
var groupedEntities = _groupEntityDB[entityGID.groupID]; | |||
var typeSafeDictionary = groupedEntities[entityType]; | |||
typeSafeDictionary.RemoveEntityFromEngines(entityGID, _entityEngines); | |||
_groupEntityViewsDB[entityGID.groupID][entityViewType].RemoveEntityFromDicAndEngines(entityGID, _entityViewEngines); | |||
RemoveEntityViewFromGroup(group, entityViewType, entityGID); | |||
RemoveEntityFromGroup(group, entityType, entityGID); | |||
} | |||
} | |||
static void RemoveEntityViewFromGroup(Dictionary<Type, ITypeSafeDictionary> @group, Type entityViewType, EGID id) | |||
static void RemoveEntityFromGroup(Dictionary<Type, ITypeSafeDictionary> @group, Type entityType, EGID id) | |||
{ | |||
//remove it from entity views group DB | |||
var typeSafeList = @group[entityViewType]; | |||
if (typeSafeList.Remove(id.GID) == false) //clean up | |||
@group.Remove(entityViewType); | |||
var typeSafeList = @group[entityType]; | |||
if (typeSafeList.Remove(id.entityID) == false) //clean up | |||
@group.Remove(entityType); | |||
} | |||
void RemoveGroupAndEntitiesFromDB(int groupID) | |||
{ | |||
foreach (var entiTypeSafeList in _groupEntityViewsDB[groupID]) | |||
entiTypeSafeList.Value.RemoveEntityViewsFromEngines(_entityViewEngines); | |||
foreach (var entiTypeSafeList in _groupEntityDB[groupID]) | |||
entiTypeSafeList.Value.RemoveEntitiesFromEngines(_entityEngines); | |||
_groupEntityViewsDB.Remove(groupID); | |||
_groupEntityDB.Remove(groupID); | |||
} | |||
///-------------------------------------------- | |||
@@ -143,24 +146,24 @@ namespace Svelto.ECS | |||
var entityegid = new EGID(entityID, fromGroupID); | |||
var entityViewBuilders = | |||
((TypeSafeDictionary<EntityInfoView>) _groupEntityViewsDB[fromGroupID][typeof(EntityInfoView)]) | |||
[entityegid.GID].entityViewsToBuild; | |||
((TypeSafeDictionary<EntityInfoView>) _groupEntityDB[fromGroupID][_typeEntityInfoView]) | |||
[entityegid.entityID].entityToBuild; | |||
var entityViewBuildersCount = entityViewBuilders.Length; | |||
var groupedEntities = _groupEntityViewsDB[fromGroupID]; | |||
var groupedEntities = _groupEntityDB[fromGroupID]; | |||
Dictionary<Type, ITypeSafeDictionary> groupedEntityViewsTyped; | |||
if (_groupEntityViewsDB.TryGetValue(toGroupID, out groupedEntityViewsTyped) == false) | |||
if (_groupEntityDB.TryGetValue(toGroupID, out groupedEntityViewsTyped) == false) | |||
{ | |||
groupedEntityViewsTyped = new Dictionary<Type, ITypeSafeDictionary>(); | |||
_groupEntityViewsDB.Add(toGroupID, groupedEntityViewsTyped); | |||
_groupEntityDB.Add(toGroupID, groupedEntityViewsTyped); | |||
} | |||
for (var i = 0; i < entityViewBuildersCount; i++) | |||
{ | |||
var entityViewBuilder = entityViewBuilders[i]; | |||
var entityViewType = entityViewBuilder.GetEntityViewType(); | |||
var entityViewType = entityViewBuilder.GetEntityType(); | |||
var fromSafeList = groupedEntities[entityViewType]; | |||
ITypeSafeDictionary toSafeList; | |||
@@ -169,13 +172,14 @@ namespace Svelto.ECS | |||
groupedEntityViewsTyped[entityViewType] = toSafeList = fromSafeList.Create(); | |||
entityViewBuilder.MoveEntityView(entityegid, fromSafeList, toSafeList); | |||
fromSafeList.Remove(entityegid.GID); | |||
fromSafeList.Remove(entityegid.entityID); | |||
} | |||
} | |||
readonly EntityViewsDB _DB; | |||
//grouped set of entity views, this is the standard way to handle entity views | |||
readonly Dictionary<int, Dictionary<Type, ITypeSafeDictionary>> _groupEntityViewsDB; | |||
readonly Dictionary<int, Dictionary<Type, ITypeSafeDictionary>> _groupEntityDB; | |||
static readonly Type _typeEntityInfoView = typeof(EntityInfoView); | |||
} | |||
} |
@@ -13,7 +13,7 @@ namespace Svelto.ECS | |||
{ | |||
void SubmitEntityViews() | |||
{ | |||
bool newEntityViewsHaveBeenAddedWhileIterating = _groupedEntityViewsToAdd.current.Count > 0; | |||
bool newEntityViewsHaveBeenAddedWhileIterating = _groupedEntityToAdd.current.Count > 0; | |||
int numberOfReenteringLoops = 0; | |||
@@ -21,16 +21,16 @@ namespace Svelto.ECS | |||
{ | |||
//use other as source from now on | |||
//current will be use to write new entityViews | |||
_groupedEntityViewsToAdd.Swap(); | |||
_groupedEntityToAdd.Swap(); | |||
if (_groupedEntityViewsToAdd.other.Count > 0) | |||
AddEntityViewsToTheDBAndSuitableEngines(_groupedEntityViewsToAdd.other); | |||
if (_groupedEntityToAdd.other.Count > 0) | |||
AddEntityViewsToTheDBAndSuitableEngines(_groupedEntityToAdd.other); | |||
//other can be cleared now | |||
_groupedEntityViewsToAdd.other.Clear(); | |||
_groupedEntityToAdd.other.Clear(); | |||
//has current new entityViews? | |||
newEntityViewsHaveBeenAddedWhileIterating = _groupedEntityViewsToAdd.current.Count > 0; | |||
newEntityViewsHaveBeenAddedWhileIterating = _groupedEntityToAdd.current.Count > 0; | |||
if (numberOfReenteringLoops > 5) | |||
throw new Exception("possible infinite loop found creating Entities inside IEntityViewsEngine Add method, please consider building entities outside IEntityViewsEngine Add method"); | |||
@@ -40,26 +40,27 @@ namespace Svelto.ECS | |||
} | |||
//todo: groupsToSubmit can be semplified as data structure? | |||
void AddEntityViewsToTheDBAndSuitableEngines(ITypeSafeDictionary<int, Dictionary<Type, ITypeSafeDictionary>> groupsOfEntitiesToSubmit) | |||
void AddEntityViewsToTheDBAndSuitableEngines(Dictionary<int, Dictionary<Type, ITypeSafeDictionary>> groupsOfEntitiesToSubmit) | |||
{ | |||
//for each groups there is a dictionary of built lists of EntityView grouped by type | |||
//each group is indexed by entity view type. for each type there is a dictionary indexed by entityID | |||
foreach (var groupOfEntitiesToSubmit in groupsOfEntitiesToSubmit) | |||
{ | |||
Dictionary<Type, ITypeSafeDictionary> groupDB; | |||
int groupID = groupOfEntitiesToSubmit.Key; | |||
//if the group doesn't exist in the current DB let's create it first | |||
if (_groupEntityViewsDB.TryGetValue(groupID, out groupDB) == false) | |||
groupDB = _groupEntityViewsDB[groupID] = new Dictionary<Type, ITypeSafeDictionary>(); | |||
if (_groupEntityDB.TryGetValue(groupID, out groupDB) == false) | |||
groupDB = _groupEntityDB[groupID] = new Dictionary<Type, ITypeSafeDictionary>(); | |||
//add the entity View in the group | |||
foreach (var entityViewList in groupOfEntitiesToSubmit.Value) | |||
//add the entityViews in the group | |||
foreach (var entityViewTypeSafeDictionary in groupOfEntitiesToSubmit.Value) | |||
{ | |||
ITypeSafeDictionary dbList; | |||
if (groupDB.TryGetValue(entityViewList.Key, out dbList) == false) | |||
dbList = groupDB[entityViewList.Key] = entityViewList.Value.Create(); | |||
ITypeSafeDictionary dbDic; | |||
if (groupDB.TryGetValue(entityViewTypeSafeDictionary.Key, out dbDic) == false) | |||
dbDic = groupDB[entityViewTypeSafeDictionary.Key] = entityViewTypeSafeDictionary.Value.Create(); | |||
dbList.FillWithIndexedEntityViews(entityViewList.Value); | |||
//type safe copy | |||
dbDic.FillWithIndexedEntityViews(entityViewTypeSafeDictionary.Value); | |||
} | |||
} | |||
@@ -69,16 +70,17 @@ namespace Svelto.ECS | |||
{ | |||
foreach (var entityViewsPerType in groupToSubmit.Value) | |||
{ | |||
var type = entityViewsPerType.Key; | |||
for (var current = type; | |||
current != _entityViewType && current != _objectType && current != _valueType; | |||
current = current.BaseType) | |||
entityViewsPerType.Value.AddEntityViewsToEngines(_entityViewEngines); | |||
entityViewsPerType.Value.AddEntityViewsToEngines(_entityEngines); | |||
} | |||
} | |||
} | |||
readonly DoubleBufferedEntityViews<Dictionary<int, Dictionary<Type, ITypeSafeDictionary>>> _groupedEntityViewsToAdd; | |||
//one datastructure rule them all: | |||
//split by group | |||
//split by type per group. It's possible to get all the entities of a give type T per group thanks | |||
//to the FasterDictionary capabilitiies OR it's possible to get a specific entityView indexed by | |||
//ID. This ID doesn't need to be the EGID, it can be just the entityID | |||
readonly DoubleBufferedEntityViews<Dictionary<int, Dictionary<Type, ITypeSafeDictionary>>> _groupedEntityToAdd; | |||
readonly EntitySubmissionScheduler _scheduler; | |||
} | |||
} |
@@ -39,12 +39,12 @@ namespace Svelto.ECS.Internal | |||
for (var index = 0; index < count; ++index) | |||
{ | |||
var entityViewBuilder = entityViewsToBuild[index]; | |||
var entityViewType = entityViewBuilder.GetEntityViewType(); | |||
var entityViewType = entityViewBuilder.GetEntityType(); | |||
BuildEntityView(entityID, entityViewsByType, entityViewType, entityViewBuilder, implementors); | |||
} | |||
_viewBuilder._initializer = new EntityInfoView {entityViewsToBuild = entityViewsToBuild}; | |||
_viewBuilder._initializer = new EntityInfoView {entityToBuild = entityViewsToBuild}; | |||
BuildEntityView(entityID, entityViewsByType, _viewType, _viewBuilder, null); | |||
} | |||
@@ -66,6 +66,6 @@ namespace Svelto.ECS.Internal | |||
} | |||
static readonly EntityViewStructBuilder<EntityInfoView> _viewBuilder = new EntityViewStructBuilder<EntityInfoView>(); | |||
static readonly Type _viewType = typeof(EntityInfoView); | |||
static readonly Type _viewType = typeof(EntityInfoView); | |||
} | |||
} |
@@ -13,7 +13,7 @@ namespace Svelto.ECS | |||
if (list == null) | |||
list = new TypeSafeDictionary<EntityViewType>(); | |||
var castedList = list as TypeSafeDictionary<EntityViewType>; | |||
var castedDic = list as TypeSafeDictionary<EntityViewType>; | |||
DBC.Check.Require(implementors != null, "Implementors not found while building an EntityView"); | |||
{ | |||
@@ -26,7 +26,7 @@ namespace Svelto.ECS | |||
, implementors | |||
, DESCRIPTOR_NAME); | |||
castedList.Add(entityID.GID, ref lentityView); | |||
castedDic.Add(entityID.entityID, ref lentityView); | |||
} | |||
} | |||
@@ -40,18 +40,18 @@ namespace Svelto.ECS | |||
return list; | |||
} | |||
public Type GetEntityViewType() | |||
public Type GetEntityType() | |||
{ | |||
return ENTITY_VIEW_TYPE; | |||
} | |||
public void MoveEntityView(EGID entityID, ITypeSafeDictionary fromSafeList, ITypeSafeDictionary toSafeList) | |||
{ | |||
var fromCastedList = fromSafeList as TypeSafeDictionary<EntityViewType>; | |||
var toCastedList = toSafeList as TypeSafeDictionary<EntityViewType>; | |||
var fromCastedDic = fromSafeList as TypeSafeDictionary<EntityViewType>; | |||
var toCastedDic = toSafeList as TypeSafeDictionary<EntityViewType>; | |||
toCastedList.Add(entityID.GID, fromCastedList[entityID.GID]); | |||
fromCastedList.Remove(entityID.GID); | |||
toCastedDic.Add(entityID.entityID, fromCastedDic[entityID.entityID]); | |||
fromCastedDic.Remove(entityID.entityID); | |||
} | |||
FasterList<KeyValuePair<Type, ActionRef<EntityViewType>>> entityViewBlazingFastReflection | |||
@@ -22,9 +22,9 @@ namespace Svelto.ECS | |||
if (list == null) | |||
list = new TypeSafeDictionary<EntityViewType>(); | |||
var castedList = list as TypeSafeDictionary<EntityViewType>; | |||
var castedDic = list as TypeSafeDictionary<EntityViewType>; | |||
castedList.Add(entityID.GID, _initializer); | |||
castedDic.Add(entityID.entityID, _initializer); | |||
} | |||
public ITypeSafeDictionary Preallocate(ref ITypeSafeDictionary list, int size) | |||
@@ -37,18 +37,18 @@ namespace Svelto.ECS | |||
return list; | |||
} | |||
public Type GetEntityViewType() | |||
public Type GetEntityType() | |||
{ | |||
return ENTITY_VIEW_TYPE; | |||
} | |||
public void MoveEntityView(EGID entityID, ITypeSafeDictionary fromSafeList, ITypeSafeDictionary toSafeList) | |||
{ | |||
var fromCastedList = fromSafeList as TypeSafeDictionary<EntityViewType>; | |||
var toCastedList = toSafeList as TypeSafeDictionary<EntityViewType>; | |||
var fromCastedDic = fromSafeList as TypeSafeDictionary<EntityViewType>; | |||
var toCastedDic = toSafeList as TypeSafeDictionary<EntityViewType>; | |||
toCastedList.Add(entityID.GID, fromCastedList[entityID.GID]); | |||
fromCastedList.Remove(entityID.GID); | |||
toCastedDic.Add(entityID.entityID, fromCastedDic[entityID.entityID]); | |||
fromCastedDic.Remove(entityID.entityID); | |||
} | |||
static readonly Type ENTITY_VIEW_TYPE = typeof(EntityViewType); | |||
@@ -21,7 +21,7 @@ static class EntityViewUtility | |||
.NoVirt.ToArrayFast(entityViewBlazingFastReflection, out count); | |||
#if DEBUG && !PROFILER | |||
if (count == 0) | |||
throw new Exception(NO_COMPONENTS_EXCEPTION.FastConcat("Type ", entityDescriptorName, " entityView ", entityViewBuilder.GetEntityViewType().ToString())); | |||
throw new Exception(NO_COMPONENTS_EXCEPTION.FastConcat("Type ", entityDescriptorName, " entityView ", entityViewBuilder.GetEntityType().ToString())); | |||
#endif | |||
for (var index = 0; index < implementors.Length; index++) | |||
{ | |||
@@ -53,7 +53,7 @@ static class EntityViewUtility | |||
#if DEBUG && !PROFILER | |||
else | |||
{ | |||
Console.LogError(NULL_IMPLEMENTOR_ERROR.FastConcat("Type ", entityDescriptorName, " entityView ", entityViewBuilder.GetEntityViewType().ToString())); | |||
Console.LogError(NULL_IMPLEMENTOR_ERROR.FastConcat("Type ", entityDescriptorName, " entityView ", entityViewBuilder.GetEntityType().ToString())); | |||
} | |||
#endif | |||
} | |||
@@ -73,7 +73,7 @@ static class EntityViewUtility | |||
{ | |||
var e = new Exception(NOT_FOUND_EXCEPTION + " Component Type: " + fieldType.Name + | |||
" - EntityView: " + | |||
entityViewBuilder.GetEntityViewType().Name + " - EntityDescriptor " + entityDescriptorName); | |||
entityViewBuilder.GetEntityType().Name + " - EntityDescriptor " + entityDescriptorName); | |||
throw e; | |||
} | |||
@@ -84,7 +84,7 @@ static class EntityViewUtility | |||
" implementor: ", | |||
component.implementorType.ToString()) + | |||
" - EntityView: " + | |||
entityViewBuilder.GetEntityViewType().Name + " - EntityDescriptor " + entityDescriptorName); | |||
entityViewBuilder.GetEntityType().Name + " - EntityDescriptor " + entityDescriptorName); | |||
#endif | |||
#if DEBUG && !PROFILER | |||
fieldSetter.Value(ref entityView, component.implementorType); | |||
@@ -11,12 +11,12 @@ namespace Svelto.ECS.Internal | |||
_groupEntityViewsDB = groupEntityViewsDB; | |||
} | |||
public ReadOnlyCollectionStruct<T> QueryEntityViews<T>() where T:IEntityData | |||
public ReadOnlyCollectionStruct<T> QueryEntities<T>() where T:IEntityData | |||
{ | |||
return QueryEntityViews<T>(ExclusiveGroups.StandardEntity); | |||
return QueryEntities<T>(ExclusiveGroups.StandardEntity); | |||
} | |||
public ReadOnlyCollectionStruct<T> QueryEntityViews<T>(int @group) where T:IEntityData | |||
public ReadOnlyCollectionStruct<T> QueryEntities<T>(int @group) where T:IEntityData | |||
{ | |||
Dictionary<Type, ITypeSafeDictionary> entitiesInGroupPerType; | |||
@@ -30,14 +30,13 @@ namespace Svelto.ECS.Internal | |||
return (outList as TypeSafeDictionary<T>).FasterValues; | |||
} | |||
public T[] QueryEntityViewsCacheFriendly<T>(out int count) where T : struct, IEntityData | |||
public T[] QueryEntitiesCacheFriendly<T>(out int count) where T : struct, IEntityData | |||
{ | |||
return QueryEntityViewsCacheFriendly<T>(ExclusiveGroups.StandardEntity, out count); | |||
return QueryEntitiesCacheFriendly<T>(ExclusiveGroups.StandardEntity, out count); | |||
} | |||
public T[] QueryEntityViewsCacheFriendly<T>(int @group, out int count) where T : struct, IEntityData | |||
public T[] QueryEntitiesCacheFriendly<T>(int @group, out int count) where T : struct, IEntityData | |||
{ | |||
var type = typeof(T); | |||
count = 0; | |||
Dictionary<Type, ITypeSafeDictionary> entitiesInGroupPerType; | |||
@@ -45,12 +44,11 @@ namespace Svelto.ECS.Internal | |||
if (_groupEntityViewsDB.TryGetValue(group, out entitiesInGroupPerType) == false) | |||
return RetrieveEmptyEntityViewArray<T>(); | |||
ITypeSafeDictionary outList; | |||
if (entitiesInGroupPerType.TryGetValue(typeof(T), out outList) == false) | |||
ITypeSafeDictionary typeSafeDictionary; | |||
if (entitiesInGroupPerType.TryGetValue(typeof(T), out typeSafeDictionary) == false) | |||
return RetrieveEmptyEntityViewArray<T>(); | |||
var typeSafeDictionary = entitiesInGroupPerType[type]; | |||
return ((TypeSafeDictionary<T>) typeSafeDictionary).GetFasterValuesBuffer(out count); | |||
return ((TypeSafeDictionary<T>)typeSafeDictionary).GetFasterValuesBuffer(out count); | |||
} | |||
public T QueryEntityView<T>(EGID entityGID) where T : class, IEntityData | |||
@@ -62,16 +60,39 @@ namespace Svelto.ECS.Internal | |||
return entityView; | |||
} | |||
public bool TryQueryEntityView<T>(EGID entityegid, out T entityView) where T : IEntityData | |||
public bool EntityExists<T>(EGID entityGID) where T : IEntityData | |||
{ | |||
var type = typeof(T); | |||
ITypeSafeDictionary entityViews; | |||
Dictionary<Type, ITypeSafeDictionary> entitiesInGroupPerType; | |||
if (_groupEntityViewsDB.TryGetValue(entityGID.groupID, out entitiesInGroupPerType) == false) | |||
{ | |||
return false; | |||
} | |||
entitiesInGroupPerType.TryGetValue(type, out entityViews); | |||
var casted = entityViews as TypeSafeDictionary<T>; | |||
if (casted != null && | |||
casted.ContainsKey(entityGID.entityID)) | |||
{ | |||
return true; | |||
} | |||
return false; | |||
} | |||
public bool TryQueryEntityView<T>(EGID entityegid, out T entityView) where T : class, IEntityData | |||
{ | |||
return TryQueryEntityViewInGroup(entityegid, out entityView); | |||
} | |||
bool TryQueryEntityViewInGroup<T>(EGID entityGID, out T entityView) where T:IEntityData | |||
bool TryQueryEntityViewInGroup<T>(EGID entityGID, out T entityView) where T:class, IEntityData | |||
{ | |||
var type = typeof(T); | |||
T internalEntityView; | |||
ITypeSafeDictionary entityViews; | |||
Dictionary<Type, ITypeSafeDictionary> entitiesInGroupPerType; | |||
@@ -85,10 +106,8 @@ namespace Svelto.ECS.Internal | |||
var casted = entityViews as TypeSafeDictionary<T>; | |||
if (casted != null && | |||
casted.TryGetValue(entityGID.GID, out internalEntityView)) | |||
casted.TryGetValue(entityGID.entityID, out entityView)) | |||
{ | |||
entityView = internalEntityView; | |||
return true; | |||
} | |||
@@ -27,7 +27,7 @@ namespace Svelto.ECS | |||
{ | |||
public EGID ID { get; set; } | |||
public IEntityViewBuilder[] entityViewsToBuild; | |||
public IEntityViewBuilder[] entityToBuild; | |||
} | |||
public static class EntityView<T> where T: IEntityData, new() | |||
@@ -8,7 +8,7 @@ namespace Svelto.ECS | |||
void BuildEntityViewAndAddToList(ref ITypeSafeDictionary list, EGID entityID, object[] implementors); | |||
ITypeSafeDictionary Preallocate(ref ITypeSafeDictionary list, int size); | |||
Type GetEntityViewType(); | |||
Type GetEntityType(); | |||
void MoveEntityView(EGID entityID, ITypeSafeDictionary fromSafeList, ITypeSafeDictionary toSafeList); | |||
} | |||
} |
@@ -4,13 +4,18 @@ namespace Svelto.ECS | |||
{ | |||
public interface IEntityViewsDB | |||
{ | |||
ReadOnlyCollectionStruct<T> QueryEntityViews<T>() where T : IEntityData; | |||
ReadOnlyCollectionStruct<T> QueryEntityViews<T>(int group) where T : IEntityData; | |||
//to use with EntityViews, EntityStructs and EntityViewStructs | |||
ReadOnlyCollectionStruct<T> QueryEntities<T>() where T : IEntityData; | |||
ReadOnlyCollectionStruct<T> QueryEntities<T>(int group) where T : IEntityData; | |||
T[] QueryEntityViewsCacheFriendly<T>(out int count) where T : struct, IEntityData; | |||
T[] QueryEntityViewsCacheFriendly<T>(int group, out int count) where T : struct, IEntityData; | |||
//to use with EntityStructs and EntityViewStructs | |||
T[] QueryEntitiesCacheFriendly<T>(out int count) where T : struct, IEntityData; | |||
T[] QueryEntitiesCacheFriendly<T>(int group, out int count) where T : struct, IEntityData; | |||
bool TryQueryEntityView<T>(EGID ID, out T entityView) where T : IEntityData; | |||
//to use with EntityViews | |||
bool TryQueryEntityView<T>(EGID ID, out T entityView) where T : class, IEntityData; | |||
T QueryEntityView<T>(EGID entityGID) where T : class, IEntityData; | |||
bool EntityExists<T>(EGID ID) where T : IEntityData; | |||
} | |||
} |
@@ -0,0 +1,43 @@ | |||
namespace Svelto.ECS | |||
{ | |||
public abstract class MultiEntitiesEngine<T, U> : SingleEntitiesEngine<T>, IHandleEntityStructEngine<U> | |||
where U : struct, IEntityData where T : struct, IEntityData | |||
{ | |||
public void AddInternal(ref U entityView) | |||
{ Add(ref entityView); } | |||
public void RemoveInternal(ref U entityView) | |||
{ Remove(ref entityView); } | |||
protected abstract void Add(ref U entityView); | |||
protected abstract void Remove(ref U entityView); | |||
} | |||
public abstract class MultiEntitiesEngine<T, U, V> : MultiEntitiesEngine<T, U>, IHandleEntityStructEngine<V> | |||
where V : struct, IEntityData where U : struct, IEntityData where T : struct, IEntityData | |||
{ | |||
public void AddInternal(ref V entityView) | |||
{ Add(ref entityView); } | |||
public void RemoveInternal(ref V entityView) | |||
{ Remove(ref entityView); } | |||
protected abstract void Add(ref V entityView); | |||
protected abstract void Remove(ref V entityView); | |||
} | |||
/// <summary> | |||
/// Please do not add more MultiEntityViewsEngine | |||
/// if you use more than 4 nodes, your engine has | |||
/// already too many responsabilities. | |||
/// </summary> | |||
public abstract class MultiEntitiesEngine<T, U, V, W> : MultiEntitiesEngine<T, U, V>, IHandleEntityStructEngine<W> | |||
where W : struct, IEntityData where V : struct, IEntityData where U : struct, IEntityData where T : struct, IEntityData | |||
{ | |||
public void AddInternal(ref W entityView) | |||
{ Add(ref entityView); } | |||
public void RemoveInternal(ref W entityView) | |||
{ Remove(ref entityView); } | |||
protected abstract void Add(ref W entityView); | |||
protected abstract void Remove(ref W entityView); | |||
} | |||
} |
@@ -0,0 +1,43 @@ | |||
namespace Svelto.ECS | |||
{ | |||
public abstract class MultiEntityStructsEngine<T, U> : SingleEntityStructEngine<T>, IHandleEntityStructEngine<U> | |||
where U : struct, IEntityData where T : struct, IEntityData | |||
{ | |||
public void AddInternal(ref U entityView) | |||
{ Add(ref entityView); } | |||
public void RemoveInternal(ref U entityView) | |||
{ Remove(ref entityView); } | |||
protected abstract void Add(ref U entityView); | |||
protected abstract void Remove(ref U entityView); | |||
} | |||
public abstract class MultiEntityStructsEngine<T, U, V> : MultiEntityViewsEngine<T, U>, IHandleEntityStructEngine<V> | |||
where V : struct, IEntityData where U : struct, IEntityData where T : struct, IEntityData | |||
{ | |||
public void AddInternal(ref V entityView) | |||
{ Add(ref entityView); } | |||
public void RemoveInternal(ref V entityView) | |||
{ Remove(ref entityView); } | |||
protected abstract void Add(ref V entityView); | |||
protected abstract void Remove(ref V entityView); | |||
} | |||
/// <summary> | |||
/// Please do not add more MultiEntityViewsEngine | |||
/// if you use more than 4 nodes, your engine has | |||
/// already too many responsabilities. | |||
/// </summary> | |||
public abstract class MultiEntityStructsEngine<T, U, V, W> : MultiEntityViewsEngine<T, U, V>, IHandleEntityStructEngine<W> | |||
where W : struct, IEntityData where V : struct, IEntityData where U : struct, IEntityData where T : struct, IEntityData | |||
{ | |||
public void AddInternal(ref W entityView) | |||
{ Add(ref entityView); } | |||
public void RemoveInternal(ref W entityView) | |||
{ Remove(ref entityView); } | |||
protected abstract void Add(ref W entityView); | |||
protected abstract void Remove(ref W entityView); | |||
} | |||
} |
@@ -1,7 +1,7 @@ | |||
namespace Svelto.ECS | |||
{ | |||
public abstract class MultiEntityViewsEngine<T, U> : SingleEntityViewEngine<T>, IHandleEntityStructEngine<U> | |||
where U : IEntityData where T : IEntityData | |||
where U : class, IEntityData where T : class, IEntityData | |||
{ | |||
public void AddInternal(ref U entityView) | |||
{ Add(entityView); } | |||
@@ -13,7 +13,7 @@ namespace Svelto.ECS | |||
} | |||
public abstract class MultiEntityViewsEngine<T, U, V> : MultiEntityViewsEngine<T, U>, IHandleEntityStructEngine<V> | |||
where V : IEntityData where U : IEntityData where T : IEntityData | |||
where V : class, IEntityData where U : class, IEntityData where T : class, IEntityData | |||
{ | |||
public void AddInternal(ref V entityView) | |||
{ Add(entityView); } | |||
@@ -30,7 +30,7 @@ namespace Svelto.ECS | |||
/// already too many responsabilities. | |||
/// </summary> | |||
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 | |||
where W : class, IEntityData where V : class, IEntityData where U : class, IEntityData where T : class, IEntityData | |||
{ | |||
public void AddInternal(ref W entityView) | |||
{ Add(entityView); } | |||
@@ -0,0 +1,14 @@ | |||
namespace Svelto.ECS | |||
{ | |||
public abstract class SingleEntitiesEngine<T> : IHandleEntityStructEngine<T> where T : struct, IEntityData | |||
{ | |||
public void AddInternal(ref T entityView) | |||
{ Add(ref entityView); } | |||
public void RemoveInternal(ref T entityView) | |||
{ Remove(ref entityView); } | |||
protected abstract void Add(ref T entityView); | |||
protected abstract void Remove(ref T entityView); | |||
} | |||
} |
@@ -1,6 +1,6 @@ | |||
namespace Svelto.ECS | |||
{ | |||
public abstract class SingleEntityViewEngine<T> : IHandleEntityStructEngine<T> where T : IEntityData | |||
public abstract class SingleEntityViewEngine<T> : IHandleEntityStructEngine<T> where T : class, IEntityData | |||
{ | |||
public void AddInternal(ref T entityView) | |||
{ Add(entityView); } | |||
@@ -11,16 +11,4 @@ namespace Svelto.ECS | |||
protected abstract void Add(T entityView); | |||
protected abstract void Remove(T entityView); | |||
} | |||
public abstract class SingleEntityStructEngine<T> : IHandleEntityStructEngine<T> where T : IEntityData | |||
{ | |||
public void AddInternal(ref T entityView) | |||
{ Add(ref entityView); } | |||
public void RemoveInternal(ref T entityView) | |||
{ Remove(ref entityView); } | |||
protected abstract void Add(ref T entityView); | |||
protected abstract void Remove(ref T entityView); | |||
} | |||
} |