Browse Source

Improved the sequencer to be more readable, added comments and use example

int cannot be used anymore to change a group, only ExclusiveGroups from now on
tags/2.7
sebas77 5 years ago
parent
commit
741995f7da
12 changed files with 248 additions and 89 deletions
  1. +1
    -1
      Svelto.ECS/DynamicEntityDescriptorInfo.cs
  2. +2
    -2
      Svelto.ECS/EGID.cs
  3. +4
    -6
      Svelto.ECS/EnginesRoot.Entities.cs
  4. +6
    -6
      Svelto.ECS/EnginesRoot.GenericEntityFactory.cs
  5. +6
    -12
      Svelto.ECS/EnginesRoot.GenericEntityFunctions.cs
  6. +12
    -6
      Svelto.ECS/EntitiesDB.cs
  7. +98
    -16
      Svelto.ECS/ExclusiveGroups.cs
  8. +8
    -8
      Svelto.ECS/ExecuteOnEntitiesDB.cs
  9. +8
    -7
      Svelto.ECS/IEntitiesDB.cs
  10. +4
    -4
      Svelto.ECS/IEntityFactory.cs
  11. +5
    -6
      Svelto.ECS/IEntityFunctions.cs
  12. +94
    -15
      Svelto.ECS/Sequencer.cs

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

@@ -11,7 +11,7 @@ namespace Svelto.ECS
"don't use a DynamicEntityDescriptorInfo if you don't need to use extra EntityViews");

var defaultEntities = EntityDescriptorTemplate<TType>.descriptor.entitiesToBuild;
var length = defaultEntities.Length;
var length = defaultEntities.Length;

entitiesToBuild = new IEntityBuilder[length + extraEntities.Count + 1];



+ 2
- 2
Svelto.ECS/EGID.cs View File

@@ -22,12 +22,12 @@ namespace Svelto.ECS
get { return (int) (_GID >> 32); }
}

public EGID(int entityID, int groupID) : this()
internal EGID(int entityID, int groupID) : this()
{
_GID = MAKE_GLOBAL_ID(entityID, groupID);
}
public EGID(int entityID, ExclusiveGroup groupID) : this()
public EGID(int entityID, ExclusiveGroup.ExclusiveGroupStruct groupID) : this()
{
_GID = MAKE_GLOBAL_ID(entityID, (int) groupID);
}


+ 4
- 6
Svelto.ECS/EnginesRoot.Entities.cs View File

@@ -64,7 +64,7 @@ namespace Svelto.ECS
return new EntityStructInitializer(entityID, dic);
}
#if DEBUG && !PROFILER
#if DEBUG && !PROFILER
void CheckEntityID(EGID entityID, IEntityDescriptor descriptorEntity)
{
Dictionary<Type, ITypeSafeDictionary> @group;
@@ -79,16 +79,14 @@ namespace Svelto.ECS
}
}

static void CheckEntityID(EGID entityID, Type entityType, Dictionary<Type, ITypeSafeDictionary> @group,
string name)
static void CheckEntityID(EGID entityID, Type entityType, Dictionary<Type, ITypeSafeDictionary> @group, string name)
{
ITypeSafeDictionary entities;
if (@group.TryGetValue(entityType, out entities))
{
if (entities.Has(entityID.entityID) == true)
{
Svelto.Utilities.Console.LogError("Entity ".FastConcat
(name, " with used ID is about to be built: ")
Svelto.Utilities.Console.LogError("Entity ".FastConcat(name, " with used ID is about to be built: ")
.FastConcat(entityType)
.FastConcat(" id: ")
.FastConcat(entityID.entityID)
@@ -97,7 +95,7 @@ namespace Svelto.ECS
}
}
}
#endif
#endif
///--------------------------------------------

void Preallocate<T>(int groupID, int size) where T : IEntityDescriptor, new()


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

@@ -15,9 +15,9 @@ namespace Svelto.ECS
_weakEngine = weakReference;
}

public EntityStructInitializer BuildEntity<T>(int entityID, ExclusiveGroup groupID, object[] implementors) where T : IEntityDescriptor, new()
public EntityStructInitializer BuildEntity<T>(int entityID, ExclusiveGroup.ExclusiveGroupStruct groupStructId, object[] implementors) where T : IEntityDescriptor, new()
{
return _weakEngine.Target.BuildEntity<T>(new EGID(entityID, (int)groupID), implementors);
return _weakEngine.Target.BuildEntity<T>(new EGID(entityID, (int)groupStructId), implementors);
}

public EntityStructInitializer BuildEntity<T>(EGID egid, object[] implementors) where T : IEntityDescriptor, new()
@@ -30,14 +30,14 @@ namespace Svelto.ECS
return _weakEngine.Target.BuildEntity(egid, entityDescriptor, implementors);
}

public EntityStructInitializer BuildEntity<T>(int entityID, ExclusiveGroup groupID, T descriptorEntity, object[] implementors) where T:IEntityDescriptor
public EntityStructInitializer BuildEntity<T>(int entityID, ExclusiveGroup.ExclusiveGroupStruct groupStructId, T descriptorEntity, object[] implementors) where T:IEntityDescriptor
{
return _weakEngine.Target.BuildEntity(new EGID(entityID, (int)groupID), descriptorEntity, implementors);
return _weakEngine.Target.BuildEntity(new EGID(entityID, (int)groupStructId), descriptorEntity, implementors);
}
public void PreallocateEntitySpace<T>(ExclusiveGroup groupID, int size) where T : IEntityDescriptor, new()
public void PreallocateEntitySpace<T>(ExclusiveGroup.ExclusiveGroupStruct groupStructId, int size) where T : IEntityDescriptor, new()
{
_weakEngine.Target.Preallocate<T>((int)groupID, size);
_weakEngine.Target.Preallocate<T>((int)groupStructId, size);
}
}
}

+ 6
- 12
Svelto.ECS/EnginesRoot.GenericEntityFunctions.cs View File

@@ -22,7 +22,7 @@ namespace Svelto.ECS
_weakReference.Target.QueueEntitySubmitOperation(new EntitySubmitOperation(EntitySubmitOperationType.Remove, entityID, groupID, -1, EntityDescriptorTemplate<T>.descriptor.entitiesToBuild));
}

public void RemoveEntity<T>(int entityID, ExclusiveGroup groupID) where T : IEntityDescriptor, new()
public void RemoveEntity<T>(int entityID, ExclusiveGroup.ExclusiveGroupStruct groupID) where T : IEntityDescriptor, new()
{
_weakReference.Target.QueueEntitySubmitOperation(
new EntitySubmitOperation(EntitySubmitOperationType.Remove, entityID, (int)groupID, -1, EntityDescriptorTemplate<T>.descriptor.entitiesToBuild));
@@ -40,31 +40,25 @@ namespace Svelto.ECS
new EntitySubmitOperation(EntitySubmitOperationType.RemoveGroup, -1, groupID, -1, null));
}

public void RemoveGroupAndEntities(ExclusiveGroup groupID)
public void RemoveGroupAndEntities(ExclusiveGroup.ExclusiveGroupStruct groupID)
{
_weakReference.Target.QueueEntitySubmitOperation(
new EntitySubmitOperation(EntitySubmitOperationType.RemoveGroup, -1, (int)groupID, -1, null));
}

public void SwapEntityGroup<T>(int entityID, int fromGroupID, int toGroupID) where T : IEntityDescriptor, new()
public void SwapEntityGroup<T>(int entityID, int fromGroupID, ExclusiveGroup.ExclusiveGroupStruct toGroupID) where T : IEntityDescriptor, new()
{
_weakReference.Target.QueueEntitySubmitOperation(
new EntitySubmitOperation(EntitySubmitOperationType.Swap, entityID, fromGroupID, toGroupID, EntityDescriptorTemplate<T>.descriptor.entitiesToBuild));
new EntitySubmitOperation(EntitySubmitOperationType.Swap, entityID, fromGroupID, (int)toGroupID, EntityDescriptorTemplate<T>.descriptor.entitiesToBuild));
}

public void SwapEntityGroup<T>(int entityID, ExclusiveGroup fromGroupID, ExclusiveGroup toGroupID) where T : IEntityDescriptor, new()
public void SwapEntityGroup<T>(int entityID, ExclusiveGroup.ExclusiveGroupStruct fromGroupID, ExclusiveGroup.ExclusiveGroupStruct toGroupID) where T : IEntityDescriptor, new()
{
_weakReference.Target.QueueEntitySubmitOperation(
new EntitySubmitOperation(EntitySubmitOperationType.Swap, entityID, (int) fromGroupID, (int) toGroupID, EntityDescriptorTemplate<T>.descriptor.entitiesToBuild));
}

public void SwapEntityGroup<T>(EGID id, int toGroupID) where T : IEntityDescriptor, new()
{
_weakReference.Target.QueueEntitySubmitOperation(
new EntitySubmitOperation(EntitySubmitOperationType.Swap, id.entityID, id.groupID, toGroupID, EntityDescriptorTemplate<T>.descriptor.entitiesToBuild));
}

public void SwapEntityGroup<T>(EGID id, ExclusiveGroup toGroupID) where T : IEntityDescriptor, new()
public void SwapEntityGroup<T>(EGID id, ExclusiveGroup.ExclusiveGroupStruct toGroupID) where T : IEntityDescriptor, new()
{
_weakReference.Target.QueueEntitySubmitOperation(
new EntitySubmitOperation(EntitySubmitOperationType.Swap, id.entityID, id.groupID, (int)toGroupID, EntityDescriptorTemplate<T>.descriptor.entitiesToBuild));


+ 12
- 6
Svelto.ECS/EntitiesDB.cs View File

@@ -31,9 +31,9 @@ namespace Svelto.ECS.Internal
return typeSafeDictionary.GetValuesArray(out count);
}

public T[] QueryEntities<T>(ExclusiveGroup @group, out int targetsCount) where T : IEntityStruct
public T[] QueryEntities<T>(ExclusiveGroup.ExclusiveGroupStruct groupStruct, out int targetsCount) where T : IEntityStruct
{
return QueryEntities<T>((int) @group, out targetsCount);
return QueryEntities<T>((int) groupStruct, out targetsCount);
}

public EGIDMapper<T> QueryMappedEntities<T>(int groupID) where T : IEntityStruct
@@ -52,9 +52,9 @@ namespace Svelto.ECS.Internal
return mapper;
}

public EGIDMapper<T> QueryMappedEntities<T>(ExclusiveGroup groupID) where T : IEntityStruct
public EGIDMapper<T> QueryMappedEntities<T>(ExclusiveGroup.ExclusiveGroupStruct groupStructId) where T : IEntityStruct
{
return QueryMappedEntities<T>((int) groupID);
return QueryMappedEntities<T>((int) groupStructId);
}

public T[] QueryEntitiesAndIndex<T>(EGID entityGID, out uint index) where T : IEntityStruct
@@ -92,6 +92,12 @@ namespace Svelto.ECS.Internal
return casted != null && casted.ContainsKey(entityGID.entityID);
}

public bool Exists (ExclusiveGroup.ExclusiveGroupStruct gid)
{
//search for the group
return _groupEntityViewsDB.ContainsKey((int) @gid);
}

public bool HasAny<T>(int @group) where T : IEntityStruct
{
int count;
@@ -99,9 +105,9 @@ namespace Svelto.ECS.Internal
return count > 0;
}

public bool HasAny<T>(ExclusiveGroup @group) where T : IEntityStruct
public bool HasAny<T>(ExclusiveGroup.ExclusiveGroupStruct groupStruct) where T : IEntityStruct
{
return HasAny<T>((int) group);
return HasAny<T>((int) groupStruct);
}

public bool TryQueryEntityView<T>(EGID entityegid, out T entityView) where T : class, IEntityStruct


+ 98
- 16
Svelto.ECS/ExclusiveGroups.cs View File

@@ -1,4 +1,8 @@
namespace Svelto.ECS
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices.WindowsRuntime;

namespace Svelto.ECS
{
/// <summary>
/// Exclusive Groups guarantee that the GroupID is unique.
@@ -7,33 +11,111 @@
///
/// public static class MyExclusiveGroups //(can be as many as you want)
/// {
/// public static MyExclusiveGroup1 = new ExclusiveGroup();
/// public static ExclusiveGroup MyExclusiveGroup1 = new ExclusiveGroup();
///
/// public static ExclusiveGroup[] GroupOfGroups = { MyExclusiveGroup1, ...}; //for each on this!
/// }
/// </summary>
///
public class ExclusiveGroup
{
public ExclusiveGroup()
{
_id = _globalId;
_globalId += 1;
_group = ExclusiveGroupStruct.Generate();
}

/// <summary>
/// Use this constructor to reserve N groups
/// </summary>
public ExclusiveGroup(int range)
public ExclusiveGroup(ushort range)
{
_group = new ExclusiveGroupStruct(range);
}
public static implicit operator ExclusiveGroupStruct (ExclusiveGroup group) // explicit byte to digit conversion operator
{
_id = _globalId;
_globalId += range;
return group._group;
}
public static explicit operator int(ExclusiveGroup group) // explicit byte to digit conversion operator
public static explicit operator int (ExclusiveGroup group) // explicit byte to digit conversion operator
{
return (int)group._group;
}

public static ExclusiveGroupStruct operator + (ExclusiveGroup a, int b)
{
return group._id;
return a._group + b;
}

readonly int _id;
static int _globalId;
readonly ExclusiveGroupStruct _group;
public struct ExclusiveGroupStruct : IEquatable<ExclusiveGroupStruct>, IComparable<ExclusiveGroupStruct>,
IEqualityComparer<ExclusiveGroupStruct>
{
public static bool operator == (ExclusiveGroupStruct c1, ExclusiveGroupStruct c2)
{
return c1.Equals(c2);
}

public static bool operator != (ExclusiveGroupStruct c1, ExclusiveGroupStruct c2)
{
return c1.Equals(c2) == false;
}

public bool Equals (ExclusiveGroupStruct other)
{
return other._id == _id;
}

public int CompareTo (ExclusiveGroupStruct other)
{
return other._id.CompareTo(_id);
}

public bool Equals (ExclusiveGroupStruct x, ExclusiveGroupStruct y)
{
return x._id == y._id;
}

public int GetHashCode (ExclusiveGroupStruct obj)
{
return _id.GetHashCode();
}

internal static ExclusiveGroupStruct Generate()
{
ExclusiveGroupStruct groupStruct;

groupStruct._id = (int) _globalId;
DBC.ECS.Check.Require(_globalId + 1 < ushort.MaxValue, "too many exclusive groups created");
_globalId++;

return groupStruct;
}

/// <summary>
/// Use this constructor to reserve N groups
/// </summary>
public ExclusiveGroupStruct(ushort range)
{
_id = (int) _globalId;
DBC.ECS.Check.Require(_globalId + range < ushort.MaxValue, "too many exclusive groups created");
_globalId += range;
}

public static explicit operator int (ExclusiveGroupStruct groupStruct) // explicit byte to digit conversion operator
{
return groupStruct._id;
}

public static ExclusiveGroupStruct operator + (ExclusiveGroupStruct a, int b)
{
var group = new ExclusiveGroupStruct();

group._id = a._id + b;

return group;
}

int _id;
static uint _globalId;
}
}
}

+ 8
- 8
Svelto.ECS/ExecuteOnEntitiesDB.cs View File

@@ -4,7 +4,7 @@ namespace Svelto.ECS.Internal
{
partial class EntitiesDB
{
public void ExecuteOnEntity<T>(int id, ExclusiveGroup groupid, EntityAction<T> action) where T : IEntityStruct
public void ExecuteOnEntity<T>(int id, ExclusiveGroup.ExclusiveGroupStruct groupid, EntityAction<T> action) where T : IEntityStruct
{
ExecuteOnEntity(id, (int)groupid, action);
}
@@ -50,7 +50,7 @@ namespace Svelto.ECS.Internal
ExecuteOnEntity(new EGID(id, groupid), ref value, action);
}

public void ExecuteOnEntity<T, W>(int id, ExclusiveGroup groupid, ref W value, EntityAction<T, W> action) where T : IEntityStruct
public void ExecuteOnEntity<T, W>(int id, ExclusiveGroup.ExclusiveGroupStruct groupid, ref W value, EntityAction<T, W> action) where T : IEntityStruct
{
ExecuteOnEntity(id, (int)groupid, ref value, action);
}
@@ -71,9 +71,9 @@ namespace Svelto.ECS.Internal
SafetyChecks(typeSafeDictionary, count);
}

public void ExecuteOnEntities<T>(ExclusiveGroup groupID, EntitiesAction<T> action) where T : IEntityStruct
public void ExecuteOnEntities<T>(ExclusiveGroup.ExclusiveGroupStruct groupStructId, EntitiesAction<T> action) where T : IEntityStruct
{
ExecuteOnEntities((int)groupID, action);
ExecuteOnEntities((int)groupStructId, action);
}

public void ExecuteOnEntities<T, W>(int groupID, ref W value, EntitiesAction<T, W> action) where T : IEntityStruct
@@ -90,9 +90,9 @@ namespace Svelto.ECS.Internal
SafetyChecks(typeSafeDictionary, count);
}

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

//-----------------------------------------------------------------------------------------------------------
@@ -149,7 +149,7 @@ namespace Svelto.ECS.Internal
}
}

public void ExecuteOnAllEntities<T>(ExclusiveGroup[] groups, EntitiesAction<T> action) where T : IEntityStruct
public void ExecuteOnAllEntities<T>(Svelto.ECS.ExclusiveGroup [] groups, EntitiesAction<T> action) where T : IEntityStruct
{
foreach (var group in groups)
{
@@ -157,7 +157,7 @@ namespace Svelto.ECS.Internal
}
}

public void ExecuteOnAllEntities<T, W>(ExclusiveGroup[] groups, ref W value, EntitiesAction<T, W> action) where T : IEntityStruct
public void ExecuteOnAllEntities<T, W>(Svelto.ECS.ExclusiveGroup [] groups, ref W value, EntitiesAction<T, W> action) where T : IEntityStruct
{
foreach (var group in groups)
{


+ 8
- 7
Svelto.ECS/IEntitiesDB.cs View File

@@ -31,7 +31,7 @@ namespace Svelto.ECS
/// <typeparam name="T"></typeparam>
/// <returns></returns>
T[] QueryEntities<T>(int group, out int count) where T : IEntityStruct;
T[] QueryEntities<T>(ExclusiveGroup @group, out int targetsCount) where T : IEntityStruct;
T[] QueryEntities<T>(ExclusiveGroup.ExclusiveGroupStruct groupStruct, out int targetsCount) where T : IEntityStruct;
/// <summary>
/// this version returns a mapped version of the entity array so that is possible to find the
/// index of the entity inside the returned buffer through it's EGID
@@ -42,7 +42,7 @@ namespace Svelto.ECS
/// <typeparam name="T"></typeparam>
/// <returns></returns>
EGIDMapper<T> QueryMappedEntities<T>(int groupID) where T : IEntityStruct;
EGIDMapper<T> QueryMappedEntities<T>(ExclusiveGroup groupID) where T : IEntityStruct;
EGIDMapper<T> QueryMappedEntities<T>(ExclusiveGroup.ExclusiveGroupStruct groupStructId) where T : IEntityStruct;
/// <summary>
/// Execute an action on entities. Be sure that the action is not capturing variables
/// otherwise you will allocate memory which will have a great impact on the execution performance.
@@ -54,9 +54,9 @@ namespace Svelto.ECS
/// <param name="action"></param>
/// <typeparam name="T"></typeparam>
void ExecuteOnEntities<T>(int groupID, EntitiesAction<T> action) where T : IEntityStruct;
void ExecuteOnEntities<T>(ExclusiveGroup groupID, EntitiesAction<T> action) where T : IEntityStruct;
void ExecuteOnEntities<T>(ExclusiveGroup.ExclusiveGroupStruct groupStructId, 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>(ExclusiveGroup groupID, ref W value, EntitiesAction<T, W> action) where T : IEntityStruct;
void ExecuteOnEntities<T, W>(ExclusiveGroup.ExclusiveGroupStruct groupStructId, ref W value, EntitiesAction<T, W> action) where T : 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.
@@ -91,15 +91,16 @@ namespace Svelto.ECS
/// <typeparam name="T"></typeparam>
void ExecuteOnEntity<T>(EGID egid, EntityAction<T> action) where T : IEntityStruct;
void ExecuteOnEntity<T>(int id, int groupid, EntityAction<T> action) where T : IEntityStruct;
void ExecuteOnEntity<T>(int id, ExclusiveGroup groupid, EntityAction<T> action) where T : IEntityStruct;
void ExecuteOnEntity<T>(int id, ExclusiveGroup.ExclusiveGroupStruct 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, int groupid, ref W value, EntityAction<T, W> action) where T : IEntityStruct;
void ExecuteOnEntity<T, W>(int id, ExclusiveGroup groupid, ref W value, EntityAction<T, W> action) where T : IEntityStruct;
void ExecuteOnEntity<T, W>(int id, ExclusiveGroup.ExclusiveGroupStruct groupid, ref W value, EntityAction<T, W> action) where T : IEntityStruct;

bool Exists<T>(EGID egid) where T : IEntityStruct;
bool Exists (ExclusiveGroup.ExclusiveGroupStruct gid);
bool HasAny<T>(int group) where T:IEntityStruct;
bool HasAny<T>(ExclusiveGroup group) where T:IEntityStruct;
bool HasAny<T>(ExclusiveGroup.ExclusiveGroupStruct groupStruct) where T:IEntityStruct;
}

public delegate void EntityAction<T, W>(ref T target, ref W value);


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

@@ -22,7 +22,7 @@ namespace Svelto.ECS
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="size"></param>
void PreallocateEntitySpace<T>(ExclusiveGroup groupID, int size) where T : IEntityDescriptor, new();
void PreallocateEntitySpace<T>(ExclusiveGroup.ExclusiveGroupStruct groupStructId, int size) where T : IEntityDescriptor, new();
/// <summary>
/// The EntityDescriptor doesn't need to be ever instantiated. It just describes the Entity
@@ -35,10 +35,10 @@ namespace Svelto.ECS
/// grouped.
/// </summary>
/// <param name="entityID"></param>
/// <param name="groupID"></param>
/// <param name="groupStructId"></param>
/// <param name="ed"></param>
/// <param name="implementors"></param>
EntityStructInitializer BuildEntity<T>(int entityID, ExclusiveGroup groupID, object[] implementors) where T:IEntityDescriptor, new();
EntityStructInitializer BuildEntity<T>(int entityID, ExclusiveGroup.ExclusiveGroupStruct groupStructId, object[] implementors) where T:IEntityDescriptor, new();
EntityStructInitializer BuildEntity<T>(EGID egid, object[] implementors) where T:IEntityDescriptor, new();
/// <summary>
/// When the type of the entity is not known (this is a special case!) an EntityDescriptorInfo
@@ -48,7 +48,7 @@ namespace Svelto.ECS
/// <param name="entityDescriptor"></param>
/// <param name="implementors"></param>
///
EntityStructInitializer BuildEntity<T>(int entityID, ExclusiveGroup groupID, T descriptorEntity, object[] implementors) where T:IEntityDescriptor;
EntityStructInitializer BuildEntity<T>(int entityID, ExclusiveGroup.ExclusiveGroupStruct groupStructId, T descriptorEntity, object[] implementors) where T:IEntityDescriptor;
EntityStructInitializer BuildEntity<T>(EGID egid, T entityDescriptor, object[] implementors) where T:IEntityDescriptor;
}
}

+ 5
- 6
Svelto.ECS/IEntityFunctions.cs View File

@@ -8,15 +8,14 @@ namespace Svelto.ECS
//an entity is removed. Not specifying the group will attempt to remove
//the entity from the special standard group.
void RemoveEntity<T>(int entityID, int groupID) where T : IEntityDescriptor, new();
void RemoveEntity<T>(int entityID, ExclusiveGroup groupID) where T : IEntityDescriptor, new();
void RemoveEntity<T>(int entityID, ExclusiveGroup.ExclusiveGroupStruct groupID) where T : IEntityDescriptor, new();
void RemoveEntity<T>(EGID entityegid) where T : IEntityDescriptor, new();

void RemoveGroupAndEntities(int groupID);
void RemoveGroupAndEntities(ExclusiveGroup groupID);
void RemoveGroupAndEntities(ExclusiveGroup.ExclusiveGroupStruct groupID);
void SwapEntityGroup<T>(int entityID, int fromGroupID, int toGroupID) where T : IEntityDescriptor, new();
void SwapEntityGroup<T>(int entityID, ExclusiveGroup fromGroupID, ExclusiveGroup toGroupID) where T : IEntityDescriptor, new();
void SwapEntityGroup<T>(EGID id, int toGroupID) where T : IEntityDescriptor, new();
void SwapEntityGroup<T>(EGID id, ExclusiveGroup toGroupID) where T : IEntityDescriptor, new();
void SwapEntityGroup<T>(int entityID, int fromGroupID, ExclusiveGroup.ExclusiveGroupStruct toGroupID) where T : IEntityDescriptor, new();
void SwapEntityGroup<T>(int entityID, ExclusiveGroup.ExclusiveGroupStruct fromGroupID, ExclusiveGroup.ExclusiveGroupStruct toGroupID) where T : IEntityDescriptor, new();
void SwapEntityGroup<T>(EGID id, ExclusiveGroup.ExclusiveGroupStruct toGroupID) where T : IEntityDescriptor, new();
}
}

+ 94
- 15
Svelto.ECS/Sequencer.cs View File

@@ -4,32 +4,100 @@ using System.Collections.Generic;

namespace Svelto.ECS
{
public class Steps : Dictionary<IEngine, IDictionary>
public class Steps
{
public new void Add(IEngine engine, IDictionary dictionary)
internal readonly Dictionary<IEngine, To> _steps;

public Steps(params Step[] values)
{
_steps = new Dictionary<IEngine, To>();

for (int i = 0; i < values.Length; i++)
_steps.Add(values[i].from, values[i].to);
}
}

public class To
{
public To(IStep engine)
{
this.engine = engine;
}
public To(params IStep[] engine)
{
if (ContainsKey(engine))
{
Svelto.Utilities.Console.LogError("can't hold multiple steps with the same engine as origin in a Sequencer");
}
base.Add(engine, dictionary);
this.engines = engine;
}

public IStep engine { get; set; }
public IStep[] engines { get; set; }
}

public class To<C> : Dictionary<C, IStep<C>[]> where C : struct, IConvertible
public class To<C>:To where C : struct, IConvertible
{
public new void Add(C condition, params IStep<C>[] engines)
internal readonly Dictionary<C, IStep<C>[]> _tos = new Dictionary<C, IStep<C>[]>();

public To(C condition, params IStep<C>[] steps)
{
_tos[condition] = steps;
}
public To(C condition, IStep<C> step)
{
base.Add(condition, engines);
_tos[condition] = new[] { step };
}
}

public interface IStep
{
void Step(EGID id);
}
public interface IStep<in C> where C:struct,IConvertible
{
void Step(C condition, EGID id);
}

public abstract class Sequencer
public struct Step
{
public IEngine from { get; set; }
public To to { get; set; }
}
/// <summary>
/// The sequencer has just one goal: define a complex sequence of engines to call. The sequence is not
/// just "sequential", but can create branches and loops.
/// With the time, I figure out that the majority of the times this class is useful in the rare cases where
/// order execution of the engine is necessary/
/// Using branching is even rarer, but still useful sometimes.
/// I used loops only once.
/// There is the chance that this class will become obsolete over the time, as by design engines should
/// never rely on the order of execution
/// Using this class to let engines from different EnginesRoot communicate will also become obsolete, as
/// better solution will be implemented once I have the time
/// Trying to work out how to initialize this structure can be painful. This is by design as this class must
/// be initialized using the following pattern:
/// instancedSequence.SetSequence(
/// new Steps //list of steps
/// (
/// new Step // first step
/// {
/// from = menuOptionEnablerEngine, //starting engine
/// to = new To<ItemActionsPanelEnableCondition> //targets
/// {
/// {
/// ItemActionsPanelEnableCondition.Enable, //condition 1
/// menuPanelEnablerEngine //targets for condition 1
/// },
/// {
/// ItemActionsPanelEnableCondition.Disable,//condition 2
/// menuPanelEnablerEngine //targets for condition 2
/// }
/// }
/// })
/// );
/// </summary>
public class Sequencer<S> where S: Sequencer<S>, new()
{
public void SetSequence(Steps steps)
{
@@ -39,14 +107,25 @@ namespace Svelto.ECS
public void Next<C>(IEngine engine, C condition, EGID id) where C:struct,IConvertible
{
C branch = condition;
var steps = (_steps[engine] as Dictionary<C, IStep<C>[]>)[branch];

if (steps == null)
Svelto.Utilities.Console.LogError("selected steps not found in sequencer ".FastConcat(this.ToString()));
var to = (_steps._steps[engine] as To<C>);
var steps = to._tos[branch];

for (var i = 0; i < steps.Length; i++)
steps[i].Step(condition, id);
}
public void Next(IEngine engine, EGID id)
{
var to = _steps._steps[engine];
var steps = to.engines;
if (steps != null)
for (var i = 0; i < steps.Length; i++)
steps[i].Step(id);
else
to.engine.Step(id);
}

Steps _steps;
}

Loading…
Cancel
Save