Browse Source

There were some references to EntityInfoView still in place, they are all removed now

Split ExecuteOnEntities functions to another class
Added the index element in ExecuteOnEntities so that is possible to know the current index of the iteration
tags/2.6a
sebas77 6 years ago
parent
commit
a2281628a0
6 changed files with 246 additions and 223 deletions
  1. +2
    -2
      Svelto.ECS/DataStructures/TypeSafeDictionary.cs
  2. +9
    -192
      Svelto.ECS/EntitiesDB.cs
  3. +1
    -1
      Svelto.ECS/EntityBuilder.cs
  4. +0
    -9
      Svelto.ECS/EntityInfoView.cs
  5. +212
    -0
      Svelto.ECS/ExecuteOnEntitiesDB.cs
  6. +22
    -19
      Svelto.ECS/IEntitiesDB.cs

+ 2
- 2
Svelto.ECS/DataStructures/TypeSafeDictionary.cs View File

@@ -144,7 +144,7 @@ namespace Svelto.ECS.Internal
return new TypeSafeDictionary<TValue>();
}
public bool ExecuteOnEntityView<W>(int entityGidEntityId, ref W value, ActionRef<TValue, W> action)
public bool ExecuteOnEntityView<W>(int entityGidEntityId, ref W value, EntityAction<TValue, W> action)
{
uint findIndex;
if (FindIndex(entityGidEntityId, out findIndex))
@@ -157,7 +157,7 @@ namespace Svelto.ECS.Internal
return false;
}
public bool ExecuteOnEntityView(int entityGidEntityId, ActionRef<TValue> action)
public bool ExecuteOnEntityView(int entityGidEntityId, EntityAction<TValue> action)
{
uint findIndex;
if (FindIndex(entityGidEntityId, out findIndex))


+ 9
- 192
Svelto.ECS/EntitiesDB.cs View File

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

namespace Svelto.ECS.Internal
{
class entitiesDB : IEntitiesDB
partial class entitiesDB : IEntitiesDB
{
internal entitiesDB(Dictionary<int, Dictionary<Type, ITypeSafeDictionary>> groupEntityViewsDB,
Dictionary<Type, FasterDictionary<int, ITypeSafeDictionary>> groupedGroups)
@@ -90,197 +90,6 @@ namespace Svelto.ECS.Internal
return entityView;
}

public void ExecuteOnEntity<T, W>(EGID entityGID, ref W value, ActionRef<T, W> action) where T : IEntityStruct
{
TypeSafeDictionary<T> casted;
if (QueryEntitySafeDictionary(entityGID.groupID, out casted))
{
if (casted != null)
if (casted.ExecuteOnEntityView(entityGID.entityID, ref value, action) == true)
return;
}

throw new EntitiesDBException("Entity not found id: ".FastConcat(entityGID.entityID).FastConcat(" groupID: ").FastConcat(entityGID.groupID));
}
public void ExecuteOnEntity<T>(EGID entityGID, ActionRef<T> action) where T : IEntityStruct
{
TypeSafeDictionary<T> casted;
if (QueryEntitySafeDictionary(entityGID.groupID, out casted))
{
if (casted != null)
if (casted.ExecuteOnEntityView(entityGID.entityID, action) == true)
return;
}

throw new EntitiesDBException("Entity not found id: ".FastConcat(entityGID.entityID).FastConcat(" groupID: ").FastConcat(entityGID.groupID));
}

public void ExecuteOnEntity<T>(int id, ActionRef<T> action) where T : IEntityStruct
{
ExecuteOnEntity(new EGID(id, ExclusiveGroup.StandardEntitiesGroup), action);
}

public void ExecuteOnEntity<T>(int id, int groupid, ActionRef<T> action) where T : IEntityStruct
{
ExecuteOnEntity(new EGID(id, groupid), action);
}

public void ExecuteOnEntity<T, W>(int id, ref W value, ActionRef<T, W> action) where T : IEntityStruct
{
ExecuteOnEntity(new EGID(id, ExclusiveGroup.StandardEntitiesGroup), ref value, action);
}

public void ExecuteOnEntity<T, W>(int id, int groupid, ref W value, ActionRef<T, W> action) where T : IEntityStruct
{
ExecuteOnEntity(new EGID(id, groupid), ref value, action);
}

public void ExecuteOnEntities<T>(int groupID, ActionRef<T> action) where T : IEntityStruct
{
int count;
TypeSafeDictionary<T> typeSafeDictionary;
if (QueryEntitySafeDictionary(@groupID, out typeSafeDictionary) == false) return;
var entities = typeSafeDictionary.GetFasterValuesBuffer(out count);
for (var i = 0; i < count; i++)
action(ref entities[i]);
SafetyChecks(typeSafeDictionary, count);
}

static void SafetyChecks<T>(TypeSafeDictionary<T> typeSafeDictionary, int count) where T : IEntityStruct
{
if (typeSafeDictionary.Count != count)
throw new EntitiesDBException("Entities cannot be swapped or removed during an iteration");
}

public void ExecuteOnEntities<T>(ActionRef<T> action) where T : IEntityStruct
{
ExecuteOnEntities(ExclusiveGroup.StandardEntitiesGroup, action);
}

public void ExecuteOnEntities<T, W>(int groupID, ref W value, ActionRef<T, W> action) where T : IEntityStruct
{
int count;
TypeSafeDictionary<T> typeSafeDictionary;
if (QueryEntitySafeDictionary(@groupID, out typeSafeDictionary) == false) return;
var entities = typeSafeDictionary.GetFasterValuesBuffer(out count);
for (var i = 0; i < count; i++)
action(ref entities[i], ref value);
SafetyChecks(typeSafeDictionary, count);
}

public void ExecuteOnEntities<T, W>(ref W value, ActionRef<T, W> action) where T : IEntityStruct
{
ExecuteOnEntities(ExclusiveGroup.StandardEntitiesGroup, ref value, action);
}
public void ExecuteOnEntities<T, T1, W>(W value, ActionRef<T, T1, W> action) where T : IEntityStruct where T1 : IEntityStruct
{
ExecuteOnEntities(ExclusiveGroup.StandardEntitiesGroup, ref value, action);
}

public void ExecuteOnAllEntities<T>(ActionRef<T> action) where T : IEntityStruct
{
var type = typeof(T);
FasterDictionary<int, ITypeSafeDictionary> dic;
if (_groupedGroups.TryGetValue(type, out dic))
{
int count;
var typeSafeDictionaries = dic.GetFasterValuesBuffer(out count);
for (int j = 0; j < count; j++)
{
int innerCount;
var typeSafeDictionary = typeSafeDictionaries[j];
var casted = typeSafeDictionary as TypeSafeDictionary<T>;
var entities = casted.GetFasterValuesBuffer(out innerCount);

for (int i = 0; i < innerCount; i++)
action(ref entities[i]);
SafetyChecks(casted, count);
}
}
}

public void ExecuteOnAllEntities<T, W>(ref W value, ActionRef<T, W> action) where T : IEntityStruct
{
var type = typeof(T);
FasterDictionary<int, ITypeSafeDictionary> dic;
if (_groupedGroups.TryGetValue(type, out dic))
{
int count;
var typeSafeDictionaries = dic.GetFasterValuesBuffer(out count);
for (int j = 0; j < count; j++)
{
int innerCount;
var typeSafeDictionary = typeSafeDictionaries[j];
var casted = typeSafeDictionary as TypeSafeDictionary<T>;
var entities = casted.GetFasterValuesBuffer(out innerCount);

for (int i = 0; i < innerCount; i++)
action(ref entities[i], ref value);
SafetyChecks(casted, count);
}
}
}

public void ExecuteOnEntities<T, T1>(int group, ActionRef<T, T1> action) where T : IEntityStruct where T1 : IEntityStruct
{
int count;
TypeSafeDictionary<T> typeSafeDictionary;
if (QueryEntitySafeDictionary(@group, out typeSafeDictionary) == false) return;
var entities = typeSafeDictionary.GetFasterValuesBuffer(out count);

EGIDMapper<T1> map = QueryMappedEntities<T1>(group);
for (var i = 0; i < count; i++)
{
uint index;
action(ref entities[i], ref map.entities(entities[i].ID, out index)[index]);
}

SafetyChecks(typeSafeDictionary, count);
}
public void ExecuteOnEntities<T, T1, W>(int group, ref W value, ActionRef<T, T1, W> action) where T : IEntityStruct where T1 : IEntityStruct
{
int count;
TypeSafeDictionary<T> typeSafeDictionary;
if (QueryEntitySafeDictionary(@group, out typeSafeDictionary) == false) return;
var entities = typeSafeDictionary.GetFasterValuesBuffer(out count);

EGIDMapper<T1> map = QueryMappedEntities<T1>(group);
for (var i = 0; i < count; i++)
{
uint index;
action(ref entities[i], ref map.entities(entities[i].ID, out index)[index], ref value);
}

SafetyChecks(typeSafeDictionary, count);
}

public void ExecuteOnEntities<T, T1>(ActionRef<T, T1> action) where T : IEntityStruct where T1 : IEntityStruct
{
ExecuteOnEntities(ExclusiveGroup.StandardEntitiesGroup, action);
}
public void ExecuteOnEntities<T, T1, W>(ref W value, ActionRef<T, T1, W> action) where T : IEntityStruct where T1 : IEntityStruct
{
ExecuteOnEntities(ExclusiveGroup.StandardEntitiesGroup, ref value, action);
}

public bool Exists<T>(EGID entityGID) where T : IEntityStruct
{
TypeSafeDictionary<T> casted;
@@ -347,6 +156,14 @@ namespace Svelto.ECS.Internal
return true;
}
static void SafetyChecks<T>(TypeSafeDictionary<T> typeSafeDictionary, int count) where T : IEntityStruct
{
#if DEBUG
if (typeSafeDictionary.Count != count)
throw new EntitiesDBException("Entities cannot be swapped or removed during an iteration");
#endif
}

static ReadOnlyCollectionStruct<T> RetrieveEmptyEntityViewList<T>()
{


Svelto.ECS/EntityViewBuilder.cs → Svelto.ECS/EntityBuilder.cs View File

@@ -16,7 +16,7 @@ namespace Svelto.ECS
_initializer = default(T);

#if DEBUG && !PROFILER
if (needsReflection == false && typeof(T) != typeof(EntityInfoView))
if (needsReflection == false)
{
CheckFields(typeof(T));
}

+ 0
- 9
Svelto.ECS/EntityInfoView.cs View File

@@ -1,9 +0,0 @@
namespace Svelto.ECS
{
public struct EntityInfoView : IEntityStruct
{
public EGID ID { get; set; }
public IEntityBuilder[] entityToBuild;
}
}

+ 212
- 0
Svelto.ECS/ExecuteOnEntitiesDB.cs View File

@@ -0,0 +1,212 @@
using Svelto.DataStructures.Experimental;
using Svelto.Utilities;

namespace Svelto.ECS.Internal
{
partial class entitiesDB
{
public void ExecuteOnEntity<T, W>(EGID entityGID, ref W value, EntityAction<T, W> action) where T : IEntityStruct
{
TypeSafeDictionary<T> casted;
if (QueryEntitySafeDictionary(entityGID.groupID, out casted))
{
if (casted != null)
if (casted.ExecuteOnEntityView(entityGID.entityID, ref value, action) == true)
return;
}

throw new EntitiesDBException("Entity not found id: "
.FastConcat(entityGID.entityID).FastConcat(" groupID: ")
.FastConcat(entityGID.groupID));
}

public void ExecuteOnEntity<T>(EGID entityGID, EntityAction<T> action) where T : IEntityStruct
{
TypeSafeDictionary<T> casted;
if (QueryEntitySafeDictionary(entityGID.groupID, out casted))
{
if (casted != null)
if (casted.ExecuteOnEntityView(entityGID.entityID, action) == true)
return;
}

throw new EntitiesDBException("Entity not found id: "
.FastConcat(entityGID.entityID).FastConcat(" groupID: ")
.FastConcat(entityGID.groupID));
}

public void ExecuteOnEntity<T>(int id, EntityAction<T> action) where T : IEntityStruct
{
ExecuteOnEntity(new EGID(id, ExclusiveGroup.StandardEntitiesGroup), action);
}

public void ExecuteOnEntity<T>(int id, int groupid, EntityAction<T> action) where T : IEntityStruct
{
ExecuteOnEntity(new EGID(id, groupid), action);
}

public void ExecuteOnEntity<T, W>(int id, ref W value, EntityAction<T, W> action) where T : IEntityStruct
{
ExecuteOnEntity(new EGID(id, ExclusiveGroup.StandardEntitiesGroup), ref value, action);
}

public void ExecuteOnEntity<T, W>(int id, int groupid, ref W value, EntityAction<T, W> action)
where T : IEntityStruct
{
ExecuteOnEntity(new EGID(id, groupid), ref value, action);
}
//----------------------------------------------------------------------------------------------------------

public void ExecuteOnEntities<T>(int groupID, EntitiesAction<T> action) where T : IEntityStruct
{
int count;
TypeSafeDictionary<T> typeSafeDictionary;
if (QueryEntitySafeDictionary(@groupID, out typeSafeDictionary) == false) return;

var entities = typeSafeDictionary.GetFasterValuesBuffer(out count);

for (var i = 0; i < count; i++)
action(ref entities[i], i);

SafetyChecks(typeSafeDictionary, count);
}

public void ExecuteOnEntities<T>(EntitiesAction<T> action) where T : IEntityStruct
{
ExecuteOnEntities(ExclusiveGroup.StandardEntitiesGroup, action);
}

public void ExecuteOnEntities<T, W>(int groupID, ref W value, EntitiesAction<T, W> action) where T : IEntityStruct
{
int count;
TypeSafeDictionary<T> typeSafeDictionary;
if (QueryEntitySafeDictionary(@groupID, out typeSafeDictionary) == false) return;

var entities = typeSafeDictionary.GetFasterValuesBuffer(out count);

for (var i = 0; i < count; i++)
action(ref entities[i], ref value, i);

SafetyChecks(typeSafeDictionary, count);
}

public void ExecuteOnEntities<T, W>(ref W value, EntitiesAction<T, W> action) where T : IEntityStruct
{
ExecuteOnEntities(ExclusiveGroup.StandardEntitiesGroup, ref value, action);
}

public void ExecuteOnEntities<T, T1, W>(W value, EntitiesAction<T, T1, W> action)
where T : IEntityStruct where T1 : IEntityStruct
{
ExecuteOnEntities(ExclusiveGroup.StandardEntitiesGroup, ref value, action);
}

public void ExecuteOnEntities<T, T1>(int group, EntitiesAction<T, T1> action)
where T : IEntityStruct where T1 : IEntityStruct
{
int count;
TypeSafeDictionary<T> typeSafeDictionary;
if (QueryEntitySafeDictionary(@group, out typeSafeDictionary) == false) return;

var entities = typeSafeDictionary.GetFasterValuesBuffer(out count);

EGIDMapper<T1> map = QueryMappedEntities<T1>(@group);

for (var i = 0; i < count; i++)
{
uint index;
action(ref entities[i], ref map.entities(entities[i].ID, out index)[index], i);
}

SafetyChecks(typeSafeDictionary, count);
}

public void ExecuteOnEntities<T, T1, W>(int group, ref W value, EntitiesAction<T, T1, W> action)
where T : IEntityStruct where T1 : IEntityStruct
{
int count;
TypeSafeDictionary<T> typeSafeDictionary;
if (QueryEntitySafeDictionary(@group, out typeSafeDictionary) == false) return;

var entities = typeSafeDictionary.GetFasterValuesBuffer(out count);

EGIDMapper<T1> map = QueryMappedEntities<T1>(@group);

for (var i = 0; i < count; i++)
{
uint index;
action(ref entities[i], ref map.entities(entities[i].ID, out index)[index], ref value, i);
}

SafetyChecks(typeSafeDictionary, count);
}

public void ExecuteOnEntities<T, T1>(EntitiesAction<T, T1> action) where T : IEntityStruct where T1 : IEntityStruct
{
ExecuteOnEntities(ExclusiveGroup.StandardEntitiesGroup, action);
}

public void ExecuteOnEntities<T, T1, W>(ref W value, EntitiesAction<T, T1, W> action)
where T : IEntityStruct where T1 : IEntityStruct
{
ExecuteOnEntities(ExclusiveGroup.StandardEntitiesGroup, ref value, action);
}
//-----------------------------------------------------------------------------------------------------------
public void ExecuteOnAllEntities<T>(EntityAction<T> action) where T : IEntityStruct
{
var type = typeof(T);
FasterDictionary<int, ITypeSafeDictionary> dic;

if (_groupedGroups.TryGetValue(type, out dic))
{
int count;
var typeSafeDictionaries = dic.GetFasterValuesBuffer(out count);

for (int j = 0; j < count; j++)
{
int innerCount;
var typeSafeDictionary = typeSafeDictionaries[j];
var casted = typeSafeDictionary as TypeSafeDictionary<T>;

var entities = casted.GetFasterValuesBuffer(out innerCount);

for (int i = 0; i < innerCount; i++)
action(ref entities[i]);

SafetyChecks(casted, count);
}
}
}

public void ExecuteOnAllEntities<T, W>(ref W value, EntityAction<T, W> action) where T : IEntityStruct
{
var type = typeof(T);
FasterDictionary<int, ITypeSafeDictionary> dic;

if (_groupedGroups.TryGetValue(type, out dic))
{
int count;
var typeSafeDictionaries = dic.GetFasterValuesBuffer(out count);

for (int j = 0; j < count; j++)
{
int innerCount;
var typeSafeDictionary = typeSafeDictionaries[j];
var casted = typeSafeDictionary as TypeSafeDictionary<T>;

var entities = casted.GetFasterValuesBuffer(out innerCount);

for (int i = 0; i < innerCount; i++)
action(ref entities[i], ref value);

SafetyChecks(casted, count);
}
}
}


}
}

+ 22
- 19
Svelto.ECS/IEntitiesDB.cs View File

@@ -1,5 +1,4 @@
using Svelto.DataStructures;
using Svelto.Utilities;

namespace Svelto.ECS
{
@@ -60,10 +59,10 @@ namespace Svelto.ECS
/// <param name="egid"></param>
/// <param name="action"></param>
/// <typeparam name="T"></typeparam>
void ExecuteOnEntities<T>(int groupID, ActionRef<T> action) where T : IEntityStruct;
void ExecuteOnEntities<T>(ActionRef<T> action) where T : IEntityStruct;
void ExecuteOnEntities<T, W>(int groupID, ref W value, ActionRef<T, W> action) where T : IEntityStruct;
void ExecuteOnEntities<T, W>(ref W value, ActionRef<T, W> action) where T : IEntityStruct;
void ExecuteOnEntities<T>(int groupID, EntitiesAction<T> action) where T : IEntityStruct;
void ExecuteOnEntities<T>(EntitiesAction<T> action) where T : IEntityStruct;
void ExecuteOnEntities<T, W>(int groupID, ref W value, EntitiesAction<T, W> action) where T : IEntityStruct;
void ExecuteOnEntities<T, W>(ref W value, EntitiesAction<T, W> action) where T : IEntityStruct;
/// <summary>
/// This specialized version allows to execute actions on multiple entity views or entity structs
/// Safety checks are in place. This function doesn't guarantee cache
@@ -73,12 +72,10 @@ namespace Svelto.ECS
/// <param name="action"></param>
/// <typeparam name="T"></typeparam>
/// <typeparam name="T1"></typeparam>
void ExecuteOnEntities<T, T1>(int groupID, ActionRef<T, T1> action) where T : IEntityStruct where T1 : IEntityStruct;
void ExecuteOnEntities<T, T1>(ActionRef<T, T1> action) where T : IEntityStruct where T1 : IEntityStruct;
void ExecuteOnEntities<T, T1, W>(int groupID, ref W value, ActionRef<T, T1, W> action) where T : IEntityStruct where T1 : IEntityStruct;
void ExecuteOnEntities<T, T1, W>(ref W value, ActionRef<T, T1, W> action) where T : IEntityStruct where T1 : IEntityStruct;
void ExecuteOnEntities<T, T1, W>(W value, ActionRef<T, T1, W> action) where T : IEntityStruct where T1 : IEntityStruct;
void ExecuteOnEntities<T, T1>(int groupID, EntitiesAction<T, T1> action) where T : IEntityStruct where T1 : IEntityStruct;
void ExecuteOnEntities<T, T1>(EntitiesAction<T, T1> action) where T : IEntityStruct where T1 : IEntityStruct;
void ExecuteOnEntities<T, T1, W>(int groupID, ref W value, EntitiesAction<T, T1, W> action) where T : IEntityStruct where T1 : IEntityStruct;
void ExecuteOnEntities<T, T1, W>(ref W value, EntitiesAction<T, T1, W> action) where T : IEntityStruct where T1 : IEntityStruct;
/// <summary>
/// Execute an action on ALL the entities regardless the group. This function doesn't guarantee cache
/// friendliness even if just EntityStructs are used.
@@ -86,8 +83,8 @@ namespace Svelto.ECS
/// </summary>
/// <param name="action"></param>
/// <typeparam name="T"></typeparam>
void ExecuteOnAllEntities<T>(ActionRef<T> action) where T : IEntityStruct;
void ExecuteOnAllEntities<T, W>(ref W value, ActionRef<T, W> action) where T : IEntityStruct;
void ExecuteOnAllEntities<T>(EntityAction<T> action) where T : IEntityStruct;
void ExecuteOnAllEntities<T, W>(ref W value, EntityAction<T, W> action) where T : IEntityStruct;
/// <summary>
/// ECS is meant to work on a set of Entities. Working on a single entity is sometime necessary, but using
/// the following functions inside a loop would be a mistake as performance can be significantly impacted
@@ -108,16 +105,22 @@ namespace Svelto.ECS
/// <param name="egid"></param>
/// <param name="action"></param>
/// <typeparam name="T"></typeparam>
void ExecuteOnEntity<T>(EGID egid, ActionRef<T> action) where T : IEntityStruct;
void ExecuteOnEntity<T>(int id, ActionRef<T> action) where T : IEntityStruct;
void ExecuteOnEntity<T>(int id, int groupid, ActionRef<T> action) where T : IEntityStruct;
void ExecuteOnEntity<T, W>(EGID egid, ref W value, ActionRef<T, W> action) where T : IEntityStruct;
void ExecuteOnEntity<T, W>(int id, ref W value, ActionRef<T, W> action) where T : IEntityStruct;
void ExecuteOnEntity<T, W>(int id, int groupid, ref W value, ActionRef<T, W> action) where T : IEntityStruct;
void ExecuteOnEntity<T>(EGID egid, EntityAction<T> action) where T : IEntityStruct;
void ExecuteOnEntity<T>(int id, EntityAction<T> action) where T : IEntityStruct;
void ExecuteOnEntity<T>(int id, int groupid, EntityAction<T> action) where T : IEntityStruct;
void ExecuteOnEntity<T, W>(EGID egid, ref W value, EntityAction<T, W> action) where T : IEntityStruct;
void ExecuteOnEntity<T, W>(int id, ref W value, EntityAction<T, W> action) where T : IEntityStruct;
void ExecuteOnEntity<T, W>(int id, int groupid, ref W value, EntityAction<T, W> action) where T : IEntityStruct;

bool Exists<T>(EGID egid) where T : IEntityStruct;
bool HasAny<T>() where T:IEntityStruct;
bool HasAny<T>(int group) where T:IEntityStruct;
}

public delegate void EntityAction<T, W>(ref T target, ref W value);
public delegate void EntityAction<T>(ref T target);
public delegate void EntitiesAction<T, T1, W>(ref T target, ref T1 target1, ref W value, int index);
public delegate void EntitiesAction<T, W>(ref T target, ref W value, int index);
public delegate void EntitiesAction<T>(ref T target, int index);
}

Loading…
Cancel
Save