@@ -1,29 +1,28 @@ | |||
using System; | |||
using Svelto.DataStructures; | |||
using System.Collections.Generic; | |||
using Svelto.DataStructures; | |||
namespace Svelto.ECS.Internal | |||
{ | |||
/// <summary> | |||
/// This is just a place holder at the moment | |||
/// I always wanted to create my own Dictionary | |||
/// data structure as excercise, but never had the | |||
/// time to. At the moment I need the custom interface | |||
/// wrapped though. | |||
/// This is just a place holder at the moment | |||
/// I always wanted to create my own Dictionary | |||
/// data structure as excercise, but never had the | |||
/// time to. At the moment I need the custom interface | |||
/// wrapped though. | |||
/// </summary> | |||
public interface ITypeSafeDictionary | |||
{ | |||
void FillWithIndexedEntityViews(ITypeSafeList entityViews); | |||
bool Remove(int entityId); | |||
IEntityView GetIndexedEntityView(int entityID); | |||
void FillWithIndexedEntityViews(ITypeSafeList entityViews); | |||
bool Remove(int entityId); | |||
IEntityView GetIndexedEntityView(int entityID); | |||
} | |||
class TypeSafeDictionary<TValue> : Dictionary<int, TValue>, ITypeSafeDictionary where TValue:IEntityView | |||
class TypeSafeDictionary<TValue> : Dictionary<int, TValue>, ITypeSafeDictionary where TValue : IEntityView | |||
{ | |||
internal static readonly ReadOnlyDictionary<int, TValue> Default = | |||
internal static readonly ReadOnlyDictionary<int, TValue> Default = | |||
new ReadOnlyDictionary<int, TValue>(new Dictionary<int, TValue>()); | |||
public void FillWithIndexedEntityViews(ITypeSafeList entityViews) | |||
{ | |||
int count; | |||
@@ -31,7 +30,7 @@ namespace Svelto.ECS.Internal | |||
try | |||
{ | |||
for (int i = 0; i < count; i++) | |||
for (var i = 0; i < count; i++) | |||
{ | |||
var entityView = buffer[i]; | |||
@@ -48,7 +47,7 @@ namespace Svelto.ECS.Internal | |||
{ | |||
base.Remove(entityId); | |||
return this.Count > 0; | |||
return Count > 0; | |||
} | |||
public IEntityView GetIndexedEntityView(int entityID) | |||
@@ -56,4 +55,4 @@ namespace Svelto.ECS.Internal | |||
return this[entityID]; | |||
} | |||
} | |||
} | |||
} |
@@ -4,7 +4,8 @@ namespace Svelto.ECS | |||
{ | |||
public class TypeSafeDictionaryException : Exception | |||
{ | |||
public TypeSafeDictionaryException(Exception exception):base(exception.Message, exception) | |||
{} | |||
public TypeSafeDictionaryException(Exception exception) : base(exception.Message, exception) | |||
{ | |||
} | |||
} | |||
} |
@@ -1,61 +1,65 @@ | |||
using System; | |||
using System.Collections; | |||
using System.Collections.Generic; | |||
using DesignByContract; | |||
using Svelto.DataStructures; | |||
namespace Svelto.ECS.Internal | |||
{ | |||
public interface ITypeSafeList: IEnumerable | |||
public interface ITypeSafeList : IEnumerable | |||
{ | |||
bool isQueryiableEntityView { get; } | |||
void AddRange(ITypeSafeList entityViewListValue); | |||
ITypeSafeList Create(); | |||
bool isQueryiableEntityView { get; } | |||
bool UnorderedRemove(int entityID); | |||
ITypeSafeList Create(); | |||
bool MappedRemove(int entityID); | |||
ITypeSafeDictionary CreateIndexedDictionary(); | |||
IEntityView[] ToArrayFast(out int count); | |||
void ReserveCapacity(int capacity); | |||
IEntityView[] ToArrayFast(out int count); | |||
void ReserveCapacity(int capacity); | |||
} | |||
class TypeSafeFasterListForECS<T>: FasterList<T> where T:IEntityView | |||
class TypeSafeFasterListForECS<T> : FasterList<T> where T : IEntityView | |||
{ | |||
readonly Dictionary<int, int> _mappedIndices; | |||
protected TypeSafeFasterListForECS() | |||
{ | |||
_mappedIndices = new Dictionary<int, int>(); | |||
} | |||
protected TypeSafeFasterListForECS(int size):base(size) | |||
protected TypeSafeFasterListForECS(int size) : base(size) | |||
{ | |||
_mappedIndices = new Dictionary<int, int>(); | |||
} | |||
public bool UnorderedRemove(int entityID) | |||
public bool MappedRemove(int entityID) | |||
{ | |||
var index = _mappedIndices[entityID]; | |||
DesignByContract.Check.Assert(entityID == this[index].ID, "Something went wrong with the Svelto.ECS code, please contact the author"); | |||
Check.Assert(entityID == this[index].ID, | |||
"Something went wrong with the Svelto.ECS code, please contact the author"); | |||
_mappedIndices.Remove(entityID); | |||
if (UnorderedRemoveAt(index)) | |||
_mappedIndices[this[index].ID] = index; | |||
return this.Count > 0; | |||
return Count > 0; | |||
} | |||
public void AddRange(ITypeSafeList entityViewListValue) | |||
{ | |||
var index = this.Count; | |||
var index = Count; | |||
base.AddRange(entityViewListValue as FasterList<T>); | |||
for (int i = index; i < Count; ++i) | |||
for (var i = index; i < Count; ++i) | |||
_mappedIndices[this[i].ID] = i; | |||
} | |||
new public void Add(T entityView) | |||
public new void Add(T entityView) | |||
{ | |||
var index = this.Count; | |||
var index = Count; | |||
base.Add(entityView); | |||
@@ -64,7 +68,7 @@ namespace Svelto.ECS.Internal | |||
public void ReserveCapacity(int capacity) | |||
{ | |||
if (this.ToArrayFast().Length < capacity) | |||
if (ToArrayFast().Length < capacity) | |||
Resize(capacity); | |||
} | |||
@@ -72,17 +76,18 @@ namespace Svelto.ECS.Internal | |||
{ | |||
return _mappedIndices[entityID]; | |||
} | |||
readonly Dictionary<int, int> _mappedIndices; | |||
} | |||
class TypeSafeFasterListForECSForStructs<T> : TypeSafeFasterListForECS<T>, ITypeSafeList where T:struct, IEntityStruct | |||
class TypeSafeFasterListForECSForStructs<T> : TypeSafeFasterListForECS<T>, ITypeSafeList | |||
where T : struct, IEntityStruct | |||
{ | |||
public TypeSafeFasterListForECSForStructs(int size):base(size) | |||
{} | |||
public TypeSafeFasterListForECSForStructs(int size) : base(size) | |||
{ | |||
} | |||
public TypeSafeFasterListForECSForStructs() | |||
{} | |||
{ | |||
} | |||
public ITypeSafeList Create() | |||
{ | |||
@@ -109,14 +114,16 @@ namespace Svelto.ECS.Internal | |||
return new TypeSafeFasterListForECSForStructs<T>(size); | |||
} | |||
} | |||
class TypeSafeFasterListForECSForClasses<T> : TypeSafeFasterListForECS<T>, ITypeSafeList where T:EntityView, new() | |||
class TypeSafeFasterListForECSForClasses<T> : TypeSafeFasterListForECS<T>, ITypeSafeList where T : EntityView, new() | |||
{ | |||
public TypeSafeFasterListForECSForClasses(int size):base(size) | |||
{} | |||
public TypeSafeFasterListForECSForClasses(int size) : base(size) | |||
{ | |||
} | |||
public TypeSafeFasterListForECSForClasses() | |||
{} | |||
{ | |||
} | |||
public ITypeSafeList Create() | |||
{ | |||
@@ -135,9 +142,9 @@ namespace Svelto.ECS.Internal | |||
public IEntityView[] ToArrayFast(out int count) | |||
{ | |||
count = this.Count; | |||
return this.ToArrayFast(); | |||
count = Count; | |||
return ToArrayFast(); | |||
} | |||
public ITypeSafeList Create(int size) | |||
@@ -145,4 +152,4 @@ namespace Svelto.ECS.Internal | |||
return new TypeSafeFasterListForECSForClasses<T>(size); | |||
} | |||
} | |||
} | |||
} |
@@ -1,5 +1,6 @@ | |||
using System; | |||
using System.Collections.Generic; | |||
using DesignByContract; | |||
using Svelto.DataStructures; | |||
using Svelto.ECS.Internal; | |||
@@ -11,10 +12,24 @@ namespace Svelto.ECS | |||
{ | |||
public partial class EnginesRoot : IDisposable | |||
{ | |||
public void Dispose() | |||
{ | |||
foreach (var entity in _entityViewsDB) | |||
if (entity.Value.isQueryiableEntityView) | |||
foreach (var entityView in entity.Value) | |||
RemoveEntityViewFromEngines(_entityViewEngines, entityView as EntityView, entity.Key); | |||
foreach (var entity in _metaEntityViewsDB) | |||
{ | |||
foreach (var entityView in entity.Value) | |||
RemoveEntityViewFromEngines(_entityViewEngines, entityView as EntityView, entity.Key); | |||
} | |||
} | |||
/// <summary> | |||
/// an EnginesRoot reference cannot be held by anything else than the Composition Root | |||
/// where it has been created. IEntityFactory and IEntityFunctions allow a weakreference | |||
/// of the EnginesRoot to be passed around. | |||
/// an EnginesRoot reference cannot be held by anything else than the Composition Root | |||
/// where it has been created. IEntityFactory and IEntityFunctions allow a weakreference | |||
/// of the EnginesRoot to be passed around. | |||
/// </summary> | |||
/// <returns></returns> | |||
public IEntityFactory GenerateEntityFactory() | |||
@@ -28,10 +43,10 @@ namespace Svelto.ECS | |||
} | |||
/// <summary> | |||
/// The EntityDescriptor doesn't need to be ever instantiated. It just describes the Entity | |||
/// itself in terms of EntityViews to build. The Implementors are passed to fill the | |||
/// references of the EntityViews components. Please read the articles on my blog | |||
/// to understand better the terminologies | |||
/// The EntityDescriptor doesn't need to be ever instantiated. It just describes the Entity | |||
/// itself in terms of EntityViews to build. The Implementors are passed to fill the | |||
/// references of the EntityViews components. Please read the articles on my blog | |||
/// to understand better the terminologies | |||
/// </summary> | |||
/// <typeparam name="T"></typeparam> | |||
/// <param name="entityID"></param> | |||
@@ -43,8 +58,8 @@ namespace Svelto.ECS | |||
} | |||
/// <summary> | |||
/// When the type of the entity is not known (this is a special case!) an EntityDescriptorInfo | |||
/// can be built in place of the generic parameter T. | |||
/// When the type of the entity is not known (this is a special case!) an EntityDescriptorInfo | |||
/// can be built in place of the generic parameter T. | |||
/// </summary> | |||
/// <param name="entityID"></param> | |||
/// <param name="entityDescriptor"></param> | |||
@@ -56,26 +71,26 @@ namespace Svelto.ECS | |||
} | |||
/// <summary> | |||
/// A meta entity is a way to manage a set of entitites that are not easily | |||
/// queriable otherwise. For example you may want to group existing entities | |||
/// by size and type and then use the meta entity entityView to manage the data | |||
/// shared among the single entities of the same type and size. This will | |||
/// prevent the scenario where the coder is forced to parse all the entities to | |||
/// find the ones of the same size and type. | |||
/// Since the entities are managed through the shared entityView, the same | |||
/// shared entityView must be found on the single entities of the same type and size. | |||
/// The shared entityView of the meta entity is then used by engines that are meant | |||
/// to manage a group of entities through a single entityView. | |||
/// The same engine can manage several meta entities entityViews too. | |||
/// The Engine manages the logic of the Meta EntityView data and other engines | |||
/// can read back this data through the normal entity as the shared entityView | |||
/// will be present in their descriptor too. | |||
/// It's a way to control a group of Entities through a entityView only. | |||
/// This set of entities can share exactly the same entityView reference if | |||
/// built through this function. In this way, if you need to set a variable | |||
/// on a group of entities, instead to inject N entityViews and iterate over | |||
/// them to set the same value, you can inject just one entityView, set the value | |||
/// and be sure that the value is shared between entities. | |||
/// A meta entity is a way to manage a set of entitites that are not easily | |||
/// queriable otherwise. For example you may want to group existing entities | |||
/// by size and type and then use the meta entity entityView to manage the data | |||
/// shared among the single entities of the same type and size. This will | |||
/// prevent the scenario where the coder is forced to parse all the entities to | |||
/// find the ones of the same size and type. | |||
/// Since the entities are managed through the shared entityView, the same | |||
/// shared entityView must be found on the single entities of the same type and size. | |||
/// The shared entityView of the meta entity is then used by engines that are meant | |||
/// to manage a group of entities through a single entityView. | |||
/// The same engine can manage several meta entities entityViews too. | |||
/// The Engine manages the logic of the Meta EntityView data and other engines | |||
/// can read back this data through the normal entity as the shared entityView | |||
/// will be present in their descriptor too. | |||
/// It's a way to control a group of Entities through a entityView only. | |||
/// This set of entities can share exactly the same entityView reference if | |||
/// built through this function. In this way, if you need to set a variable | |||
/// on a group of entities, instead to inject N entityViews and iterate over | |||
/// them to set the same value, you can inject just one entityView, set the value | |||
/// and be sure that the value is shared between entities. | |||
/// </summary> | |||
/// <param name="metaEntityID"></param> | |||
/// <param name="ed"></param> | |||
@@ -87,39 +102,43 @@ namespace Svelto.ECS | |||
} | |||
/// <summary> | |||
/// Using this function is like building a normal entity, but the entityViews | |||
/// are grouped by groupID to be more efficently processed inside engines and | |||
/// improve cache locality. Either class entityViews and struct entityViews can be | |||
/// grouped. | |||
/// Using this function is like building a normal entity, but the entityViews | |||
/// are grouped by groupID to be more efficently processed inside engines and | |||
/// improve cache locality. Either class entityViews and struct entityViews can be | |||
/// grouped. | |||
/// </summary> | |||
/// <param name="entityID"></param> | |||
/// <param name="groupID"></param> | |||
/// <param name="ed"></param> | |||
/// <param name="implementors"></param> | |||
void BuildEntityInGroup<T>(int entityID, int groupID, object[] implementors = null) where T : IEntityDescriptor, new() | |||
void BuildEntityInGroup<T>(int entityID, int groupID, object[] implementors = null) | |||
where T : IEntityDescriptor, new() | |||
{ | |||
EntityFactory.BuildGroupedEntityViews(entityID, groupID, | |||
_groupedEntityViewsToAdd.current, | |||
_entityViewsToAdd.current, | |||
EntityDescriptorTemplate<T>.Default, | |||
implementors); | |||
} | |||
void BuildEntityInGroup(int entityID, int groupID, IEntityDescriptorInfo entityDescriptor, object[] implementors = null) | |||
void BuildEntityInGroup(int entityID, int groupID, IEntityDescriptorInfo entityDescriptor, | |||
object[] implementors = null) | |||
{ | |||
EntityFactory.BuildGroupedEntityViews(entityID, groupID, | |||
_groupedEntityViewsToAdd.current, | |||
_entityViewsToAdd.current, | |||
entityDescriptor, implementors); | |||
} | |||
void Preallocate<T>(int size) where T : IEntityDescriptor, new() | |||
{ | |||
var entityViewsToBuild = ((EntityDescriptorInfo) EntityDescriptorTemplate<T>.Default).entityViewsToBuild; | |||
int count = entityViewsToBuild.Length; | |||
var count = entityViewsToBuild.Length; | |||
for (int index = 0; index < count; index++) | |||
for (var index = 0; index < count; index++) | |||
{ | |||
var entityViewBuilder = entityViewsToBuild[index]; | |||
var entityViewType = entityViewBuilder.GetEntityViewType(); | |||
var entityViewType = entityViewBuilder.GetEntityViewType(); | |||
ITypeSafeList dbList; | |||
if (_entityViewsDB.TryGetValue(entityViewType, out dbList) == false) | |||
@@ -134,28 +153,31 @@ namespace Svelto.ECS | |||
} | |||
} | |||
void RemoveEntity(ref EntityInfoView entityInfoView, Dictionary<Type, ITypeSafeList> viewsDB, Dictionary<Type, ITypeSafeDictionary> entityViewsDBDic) | |||
void RemoveEntity(ref EntityInfoView entityInfoView, Dictionary<Type, ITypeSafeList> viewsDB, | |||
Dictionary<Type, ITypeSafeDictionary> entityViewsDBDic) | |||
{ | |||
if (entityInfoView.isInAGroup) | |||
InternalRemoveFromGroupAndDBAndEngines(entityInfoView.entityViews, entityInfoView.ID, entityInfoView.groupID, viewsDB, entityViewsDBDic); | |||
InternalRemoveFromGroupAndDBAndEngines(entityInfoView.entityViews, entityInfoView.ID, | |||
entityInfoView.groupID, viewsDB, entityViewsDBDic); | |||
else | |||
InternalRemoveFromDBAndEngines(entityInfoView.entityViews, entityInfoView.ID, viewsDB, entityViewsDBDic); | |||
InternalRemoveFromDBAndEngines(entityInfoView.entityViews, entityInfoView.ID, viewsDB, | |||
entityViewsDBDic); | |||
} | |||
void RemoveEntity(int entityID) | |||
{ | |||
var entityInfoView = _DB.QueryEntityView<EntityInfoView>(entityID); | |||
RemoveEntity(ref entityInfoView, _entityViewsDB, _entityViewsDBDic); | |||
} | |||
void RemoveMetaEntity(int metaEntityID) | |||
{ | |||
var entityInfoView = _DB.QueryMetaEntityView<EntityInfoView>(metaEntityID); | |||
RemoveEntity(ref entityInfoView, _metaEntityViewsDB, _metaEntityViewsDBDic); | |||
} | |||
void RemoveGroupAndEntitiesFromDB(int groupID) | |||
{ | |||
foreach (var group in _groupEntityViewsDB[groupID]) | |||
@@ -165,29 +187,31 @@ namespace Svelto.ECS | |||
int count; | |||
var entities = group.Value.ToArrayFast(out count); | |||
for (int i = 0; i < count; i++) | |||
for (var i = 0; i < count; i++) | |||
{ | |||
var entityID = entities[i].ID; | |||
InternalRemoveEntityViewFromDBAndEngines(_entityViewsDB, _entityViewsDBDic, entityViewType, entityID); | |||
InternalRemoveEntityViewFromDBAndEngines(_entityViewsDB, _entityViewsDBDic, entityViewType, | |||
entityID); | |||
} | |||
} | |||
_groupEntityViewsDB.Remove(groupID); | |||
} | |||
void InternalRemoveEntityViewFromDBAndEngines(Dictionary<Type, ITypeSafeList> entityViewsDB, | |||
void InternalRemoveEntityViewFromDBAndEngines(Dictionary<Type, ITypeSafeList> entityViewsDB, | |||
Dictionary<Type, ITypeSafeDictionary> entityViewsDBDic, | |||
Type entityViewType, int entityID) | |||
Type entityViewType, | |||
int entityID) | |||
{ | |||
var entityViews = entityViewsDB[entityViewType]; | |||
if (entityViews.UnorderedRemove(entityID) == false) | |||
if (entityViews.MappedRemove(entityID) == false) | |||
entityViewsDB.Remove(entityViewType); | |||
if (entityViews.isQueryiableEntityView) | |||
{ | |||
var typeSafeDictionary = entityViewsDBDic[entityViewType]; | |||
var entityView = typeSafeDictionary.GetIndexedEntityView(entityID); | |||
var entityView = typeSafeDictionary.GetIndexedEntityView(entityID); | |||
if (typeSafeDictionary.Remove(entityID) == false) | |||
entityViewsDBDic.Remove(entityViewType); | |||
@@ -199,12 +223,13 @@ namespace Svelto.ECS | |||
void SwapEntityGroup(int entityID, int fromGroupID, int toGroupID) | |||
{ | |||
DesignByContract.Check.Require(fromGroupID != toGroupID, "can't move an entity to the same group where it already belongs to"); | |||
Check.Require(fromGroupID != toGroupID, | |||
"can't move an entity to the same group where it already belongs to"); | |||
var entityViewBuilders = _DB.QueryEntityView<EntityInfoView>(entityID).entityViews; | |||
int entityViewBuildersCount = entityViewBuilders.Length; | |||
var entityViewBuilders = _DB.QueryEntityView<EntityInfoView>(entityID).entityViews; | |||
var entityViewBuildersCount = entityViewBuilders.Length; | |||
var dictionary = _groupEntityViewsDB[fromGroupID]; | |||
var groupedEntities = _groupEntityViewsDB[fromGroupID]; | |||
Dictionary<Type, ITypeSafeList> groupedEntityViewsTyped; | |||
if (_groupEntityViewsDB.TryGetValue(toGroupID, out groupedEntityViewsTyped) == false) | |||
@@ -214,43 +239,46 @@ namespace Svelto.ECS | |||
_groupEntityViewsDB.Add(toGroupID, groupedEntityViewsTyped); | |||
} | |||
for (int i = 0; i < entityViewBuildersCount; i++) | |||
for (var i = 0; i < entityViewBuildersCount; i++) | |||
{ | |||
IEntityViewBuilder entityViewBuilder = entityViewBuilders[i]; | |||
Type entityViewType = entityViewBuilder.GetEntityViewType(); | |||
var entityViewBuilder = entityViewBuilders[i]; | |||
var entityViewType = entityViewBuilder.GetEntityViewType(); | |||
ITypeSafeList fromSafeList = dictionary[entityViewType]; | |||
var fromSafeList = groupedEntities[entityViewType]; | |||
ITypeSafeList toSafeList; | |||
if (groupedEntityViewsTyped.TryGetValue(entityViewType, out toSafeList) == false) | |||
{ | |||
toSafeList = fromSafeList.Create(); | |||
} | |||
groupedEntityViewsTyped[entityViewType] = toSafeList = fromSafeList.Create(); | |||
entityViewBuilder.MoveEntityView(entityID, fromSafeList, toSafeList); | |||
if (fromSafeList.UnorderedRemove(entityID) == false) | |||
dictionary.Remove(entityViewType); | |||
fromSafeList.MappedRemove(entityID); | |||
} | |||
if (dictionary.Count == 0) _groupEntityViewsDB.Remove(fromGroupID); | |||
var entityInfoView = _DB.QueryEntityView<EntityInfoView>(entityID); | |||
entityInfoView.groupID = toGroupID; | |||
} | |||
void InternalRemoveFromDBAndEngines(IEntityViewBuilder[] entityViewBuilders, int entityID, | |||
Dictionary<Type, ITypeSafeList> entityViewsDB, Dictionary<Type, ITypeSafeDictionary> entityViewsDBDic) | |||
void InternalRemoveFromDBAndEngines(IEntityViewBuilder[] entityViewBuilders, int entityID, | |||
Dictionary<Type, ITypeSafeList> entityViewsDB, | |||
Dictionary<Type, ITypeSafeDictionary> entityViewsDBDic) | |||
{ | |||
int entityViewBuildersCount = entityViewBuilders.Length; | |||
var entityViewBuildersCount = entityViewBuilders.Length; | |||
for (int i = 0; i < entityViewBuildersCount; i++) | |||
for (var i = 0; i < entityViewBuildersCount; i++) | |||
{ | |||
Type entityViewType = entityViewBuilders[i].GetEntityViewType(); | |||
var entityViewType = entityViewBuilders[i].GetEntityViewType(); | |||
InternalRemoveEntityViewFromDBAndEngines(entityViewsDB, entityViewsDBDic, entityViewType, entityID); | |||
} | |||
InternalRemoveEntityViewFromDBAndEngines(entityViewsDB, entityViewsDBDic, typeof(EntityInfoView), entityID); | |||
} | |||
void InternalRemoveFromGroupAndDBAndEngines(IEntityViewBuilder[] entityViewBuilders, int entityID, int groupID, | |||
Dictionary<Type, ITypeSafeList> entityViewsDB, Dictionary<Type, ITypeSafeDictionary> entityViewsDBDic) | |||
void InternalRemoveFromGroupAndDBAndEngines(IEntityViewBuilder[] entityViewBuilders, | |||
int entityID, int groupID, | |||
Dictionary<Type, ITypeSafeList> entityViewsDB, | |||
Dictionary<Type, ITypeSafeDictionary> entityViewsDBDic) | |||
{ | |||
InternalRemoveFromGroupDB(entityViewBuilders, entityID, groupID); | |||
@@ -259,20 +287,22 @@ namespace Svelto.ECS | |||
void InternalRemoveFromGroupDB(IEntityViewBuilder[] entityViewBuilders, int entityID, int groupID) | |||
{ | |||
int entityViewBuildersCount = entityViewBuilders.Length; | |||
var entityViewBuildersCount = entityViewBuilders.Length; | |||
Dictionary<Type, ITypeSafeList> dictionary = _groupEntityViewsDB[groupID]; | |||
var dictionary = _groupEntityViewsDB[groupID]; | |||
for (int i = 0; i < entityViewBuildersCount; i++) | |||
for (var i = 0; i < entityViewBuildersCount; i++) | |||
{ | |||
Type entityViewType = entityViewBuilders[i].GetEntityViewType(); | |||
var entityViewType = entityViewBuilders[i].GetEntityViewType(); | |||
dictionary[entityViewType].UnorderedRemove(entityID); | |||
var typeSafeList = dictionary[entityViewType]; | |||
typeSafeList.MappedRemove(entityID); | |||
} | |||
} | |||
static void RemoveEntityViewFromEngines(Dictionary<Type, FasterList<IHandleEntityViewEngine>> entityViewEngines, | |||
IEntityView entityView, Type entityViewType) | |||
IEntityView entityView, | |||
Type entityViewType) | |||
{ | |||
FasterList<IHandleEntityViewEngine> enginesForEntityView; | |||
@@ -281,7 +311,7 @@ namespace Svelto.ECS | |||
int count; | |||
var fastList = FasterList<IHandleEntityViewEngine>.NoVirt.ToArrayFast(enginesForEntityView, out count); | |||
for (int j = 0; j < count; j++) | |||
for (var j = 0; j < count; j++) | |||
{ | |||
#if ENGINE_PROFILER_ENABLED && UNITY_EDITOR | |||
EngineProfiler.MonitorRemoveDuration(fastList[j], entityView); | |||
@@ -294,7 +324,7 @@ namespace Svelto.ECS | |||
class GenericEntityFactory : IEntityFactory | |||
{ | |||
DataStructures.WeakReference<EnginesRoot> _weakEngine; | |||
readonly DataStructures.WeakReference<EnginesRoot> _weakEngine; | |||
public GenericEntityFactory(DataStructures.WeakReference<EnginesRoot> weakReference) | |||
{ | |||
@@ -316,12 +346,14 @@ namespace Svelto.ECS | |||
_weakEngine.Target.BuildMetaEntity<T>(metaEntityID, implementors); | |||
} | |||
public void BuildEntityInGroup<T>(int entityID, int groupID, object[] implementors) where T : IEntityDescriptor, new() | |||
public void BuildEntityInGroup<T>(int entityID, int groupID, object[] implementors) | |||
where T : IEntityDescriptor, new() | |||
{ | |||
_weakEngine.Target.BuildEntityInGroup<T>(entityID, groupID, implementors); | |||
} | |||
public void BuildEntityInGroup(int entityID, int groupID, IEntityDescriptorInfo entityDescriptor, object[] implementors) | |||
public void BuildEntityInGroup(int entityID, int groupID, IEntityDescriptorInfo entityDescriptor, | |||
object[] implementors) | |||
{ | |||
_weakEngine.Target.BuildEntityInGroup(entityID, groupID, entityDescriptor, implementors); | |||
} | |||
@@ -334,6 +366,8 @@ namespace Svelto.ECS | |||
class GenericEntityFunctions : IEntityFunctions | |||
{ | |||
readonly DataStructures.WeakReference<EnginesRoot> _weakReference; | |||
public GenericEntityFunctions(DataStructures.WeakReference<EnginesRoot> weakReference) | |||
{ | |||
_weakReference = weakReference; | |||
@@ -358,39 +392,16 @@ namespace Svelto.ECS | |||
{ | |||
_weakReference.Target.SwapEntityGroup(entityID, fromGroupID, toGroupID); | |||
} | |||
readonly DataStructures.WeakReference<EnginesRoot> _weakReference; | |||
} | |||
public void Dispose() | |||
{ | |||
foreach (var entity in _entityViewsDB) | |||
{ | |||
if (entity.Value.isQueryiableEntityView == true) | |||
{ | |||
foreach (var entityView in entity.Value) | |||
{ | |||
RemoveEntityViewFromEngines(_entityViewEngines, entityView as EntityView, entity.Key); | |||
} | |||
} | |||
} | |||
foreach (var entity in _metaEntityViewsDB) | |||
{ | |||
foreach (var entityView in entity.Value) | |||
{ | |||
RemoveEntityViewFromEngines(_entityViewEngines, entityView as EntityView, entity.Key); | |||
} | |||
} | |||
} | |||
readonly EntityViewsDB _DB; | |||
readonly Dictionary<Type, ITypeSafeList> _entityViewsDB; | |||
readonly Dictionary<Type, ITypeSafeList> _metaEntityViewsDB; | |||
readonly Dictionary<Type, ITypeSafeDictionary> _entityViewsDBDic; | |||
readonly Dictionary<int, Dictionary<Type, ITypeSafeList>> _groupEntityViewsDB; | |||
readonly Dictionary<Type, ITypeSafeDictionary> _entityViewsDBDic; | |||
readonly Dictionary<Type, ITypeSafeDictionary> _metaEntityViewsDBDic; | |||
readonly Dictionary<Type, ITypeSafeList> _metaEntityViewsDB; | |||
readonly Dictionary<Type, ITypeSafeDictionary> _metaEntityViewsDBDic; | |||
} | |||
} |
@@ -1,6 +1,7 @@ | |||
using System; | |||
using DesignByContract; | |||
using Svelto.DataStructures; | |||
using Svelto.ECS.Internal; | |||
using System; | |||
namespace Svelto.ECS | |||
{ | |||
@@ -8,20 +9,21 @@ namespace Svelto.ECS | |||
{ | |||
IEntityViewBuilder[] entityViewsToBuild { get; } | |||
} | |||
public class EntityDescriptor:IEntityDescriptor | |||
public class EntityDescriptor : IEntityDescriptor | |||
{ | |||
protected EntityDescriptor(IEntityViewBuilder[] entityViewsToBuild) | |||
{ | |||
this.entityViewsToBuild = entityViewsToBuild; | |||
} | |||
public IEntityViewBuilder[] entityViewsToBuild { get; private set; } | |||
public IEntityViewBuilder[] entityViewsToBuild { get; } | |||
} | |||
public interface IEntityDescriptorInfo | |||
{} | |||
{ | |||
} | |||
public static class EntityDescriptorTemplate<TType> where TType : IEntityDescriptor, new() | |||
{ | |||
public static readonly IEntityDescriptorInfo Default = new EntityDescriptorInfo(new TType()); | |||
@@ -31,16 +33,17 @@ namespace Svelto.ECS | |||
{ | |||
public DynamicEntityDescriptorInfo(FasterList<IEntityViewBuilder> extraEntityViews) | |||
{ | |||
DesignByContract.Check.Require(extraEntityViews.Count > 0, "don't use a DynamicEntityDescriptorInfo if you don't need to use extra EntityViews"); | |||
Check.Require(extraEntityViews.Count > 0, | |||
"don't use a DynamicEntityDescriptorInfo if you don't need to use extra EntityViews"); | |||
var descriptor = new TType(); | |||
int length = descriptor.entityViewsToBuild.Length; | |||
var length = descriptor.entityViewsToBuild.Length; | |||
entityViewsToBuild = new IEntityViewBuilder[length + extraEntityViews.Count]; | |||
Array.Copy(descriptor.entityViewsToBuild, 0, entityViewsToBuild, 0, length); | |||
Array.Copy(extraEntityViews.ToArrayFast(), 0, entityViewsToBuild, length, extraEntityViews.Count); | |||
name = descriptor.ToString(); | |||
} | |||
} | |||
@@ -48,19 +51,19 @@ namespace Svelto.ECS | |||
namespace Svelto.ECS.Internal | |||
{ | |||
public class EntityDescriptorInfo:IEntityDescriptorInfo | |||
public class EntityDescriptorInfo : IEntityDescriptorInfo | |||
{ | |||
internal IEntityViewBuilder[] entityViewsToBuild; | |||
internal string name; | |||
internal string name; | |||
internal EntityDescriptorInfo(IEntityDescriptor descriptor) | |||
{ | |||
name = descriptor.ToString(); | |||
name = descriptor.ToString(); | |||
entityViewsToBuild = descriptor.entityViewsToBuild; | |||
} | |||
protected EntityDescriptorInfo() | |||
{} | |||
{ | |||
} | |||
} | |||
} | |||
} |
@@ -2,17 +2,20 @@ using System; | |||
using System.Collections.Generic; | |||
using Svelto.DataStructures; | |||
using Svelto.Utilities; | |||
using Console = Utility.Console; | |||
namespace Svelto.ECS.Internal | |||
{ | |||
static class EntityFactory | |||
{ | |||
internal static void BuildGroupedEntityViews(int entityID, int groupID, | |||
internal static void BuildGroupedEntityViews(int entityID, int groupID, | |||
Dictionary<int, Dictionary<Type, ITypeSafeList>> groupEntityViewsByType, | |||
IEntityDescriptorInfo eentityViewsToBuildDescriptor, | |||
object[] implementors) | |||
Dictionary<Type, ITypeSafeList> entityViewsByType, | |||
IEntityDescriptorInfo eentityViewsToBuildDescriptor, | |||
object[] implementors) | |||
{ | |||
var entityViewsToBuildDescriptor = eentityViewsToBuildDescriptor as EntityDescriptorInfo; | |||
var entityViewsToBuildDescriptor = | |||
eentityViewsToBuildDescriptor as EntityDescriptorInfo; | |||
Dictionary<Type, ITypeSafeList> groupedEntityViewsTyped; | |||
if (groupEntityViewsByType.TryGetValue(groupID, out groupedEntityViewsTyped) == false) | |||
@@ -22,51 +25,53 @@ namespace Svelto.ECS.Internal | |||
} | |||
InternalBuildEntityViews(entityID, groupedEntityViewsTyped, entityViewsToBuildDescriptor, implementors); | |||
EntityInfoView removeEntityView = new EntityInfoView(); | |||
removeEntityView.groupID = groupID; | |||
removeEntityView.isInAGroup = true; | |||
var removeEntityView = EntityView<EntityInfoView>.BuildEntityView(entityID); | |||
removeEntityView.groupID = groupID; | |||
removeEntityView.isInAGroup = true; | |||
removeEntityView.entityViews = entityViewsToBuildDescriptor.entityViewsToBuild; | |||
AddEntityInfoView(groupEntityViewsByType[groupID], removeEntityView); | |||
AddEntityInfoView(entityViewsByType, removeEntityView); | |||
} | |||
internal static void BuildEntityViews(int entityID, | |||
internal static void BuildEntityViews(int entityID, | |||
Dictionary<Type, ITypeSafeList> entityViewsByType, | |||
IEntityDescriptorInfo eentityViewsToBuildDescriptor, | |||
object[] implementors) | |||
{ | |||
var entityViewsToBuildDescriptor = eentityViewsToBuildDescriptor as EntityDescriptorInfo; | |||
InternalBuildEntityViews(entityID, entityViewsByType, entityViewsToBuildDescriptor, implementors); | |||
EntityInfoView removeEntityView = new EntityInfoView(); | |||
var removeEntityView = EntityView<EntityInfoView>.BuildEntityView(entityID); | |||
removeEntityView.entityViews = entityViewsToBuildDescriptor.entityViewsToBuild; | |||
AddEntityInfoView(entityViewsByType, removeEntityView); | |||
} | |||
static void AddEntityInfoView(Dictionary<Type, ITypeSafeList> entityViewsByType, EntityInfoView removeEntityView) | |||
static void AddEntityInfoView(Dictionary<Type, ITypeSafeList> entityViewsByType, | |||
EntityInfoView removeEntityView) | |||
{ | |||
ITypeSafeList list; | |||
if (entityViewsByType.TryGetValue(typeof(EntityInfoView), out list) == false) | |||
list = entityViewsByType[typeof(EntityInfoView)] = new TypeSafeFasterListForECSForClasses<EntityInfoView>(); | |||
list = entityViewsByType[typeof(EntityInfoView)] = | |||
new TypeSafeFasterListForECSForClasses<EntityInfoView>(); | |||
(list as TypeSafeFasterListForECSForClasses<EntityInfoView>).Add(removeEntityView); | |||
} | |||
static void InternalBuildEntityViews(int entityID, | |||
Dictionary<Type, ITypeSafeList> entityViewsByType, | |||
IEntityDescriptorInfo eentityViewsToBuildDescriptor, | |||
static void InternalBuildEntityViews(int entityID, | |||
Dictionary<Type, ITypeSafeList> entityViewsByType, | |||
IEntityDescriptorInfo eentityViewsToBuildDescriptor, | |||
object[] implementors) | |||
{ | |||
var entityViewsToBuildDescriptor = eentityViewsToBuildDescriptor as EntityDescriptorInfo; | |||
var entityViewsToBuild = entityViewsToBuildDescriptor.entityViewsToBuild; | |||
int count = entityViewsToBuild.Length; | |||
var count = entityViewsToBuild.Length; | |||
for (int index = 0; index < count; index++) | |||
for (var index = 0; index < count; index++) | |||
{ | |||
var entityViewBuilder = entityViewsToBuild[index]; | |||
var entityViewType = entityViewBuilder.GetEntityViewType(); | |||
@@ -74,12 +79,10 @@ namespace Svelto.ECS.Internal | |||
var entityViewObjectToFill = | |||
BuildEntityView(entityID, entityViewsByType, entityViewType, entityViewBuilder); | |||
if (entityViewBuilder.mustBeFilled == true) | |||
{ | |||
if (entityViewBuilder.mustBeFilled) | |||
FillEntityView(entityViewObjectToFill as EntityView | |||
,implementors | |||
,entityViewsToBuildDescriptor.name); | |||
} | |||
, implementors | |||
, entityViewsToBuildDescriptor.name); | |||
} | |||
} | |||
@@ -106,24 +109,26 @@ namespace Svelto.ECS.Internal | |||
//this is used to avoid newing a dictionary every time, but it's used locally only and it's clearead for each use | |||
#if DEBUG && !PROFILER | |||
static readonly Dictionary<Type, Tuple<object, int>> implementorsByType = new Dictionary<Type, Tuple<object, int>>(); | |||
static readonly Dictionary<Type, Tuple<object, int>> implementorsByType = | |||
new Dictionary<Type, Tuple<object, int>>(); | |||
#else | |||
static readonly Dictionary<Type, object> implementorsByType = new Dictionary<Type, object>(); | |||
#endif | |||
static void FillEntityView(EntityView entityView | |||
, object[] implementors | |||
, string entityDescriptorName) | |||
, object[] implementors | |||
, string entityDescriptorName) | |||
{ | |||
int count; | |||
//Very efficent way to collect the fields of every EntityViewType | |||
KeyValuePair<Type, CastedAction<EntityView>>[] setters = | |||
FasterList<KeyValuePair<Type, CastedAction<EntityView>>>.NoVirt.ToArrayFast(entityView.entityViewBlazingFastReflection, out count); | |||
var setters = | |||
FasterList<KeyValuePair<Type, CastedAction<EntityView>>> | |||
.NoVirt.ToArrayFast(entityView.entityViewBlazingFastReflection, out count); | |||
if (count == 0) return; | |||
for (int index = 0; index < implementors.Length; index++) | |||
for (var index = 0; index < implementors.Length; index++) | |||
{ | |||
var implementor = implementors[index]; | |||
@@ -133,15 +138,15 @@ namespace Svelto.ECS.Internal | |||
Type[] interfaces; | |||
if (_cachedTypes.TryGetValue(type, out interfaces) == false) | |||
interfaces = _cachedTypes[type] = type.GetInterfacesEx(); | |||
interfaces = _cachedTypes[type] = type.GetInterfacesEx(); | |||
for (int iindex = 0; iindex < interfaces.Length; iindex++) | |||
for (var iindex = 0; iindex < interfaces.Length; iindex++) | |||
{ | |||
var componentType = interfaces[iindex]; | |||
#if DEBUG && !PROFILER | |||
Tuple<object, int> implementorHolder; | |||
if (implementorsByType.TryGetValue(componentType, out implementorHolder) == true) | |||
if (implementorsByType.TryGetValue(componentType, out implementorHolder)) | |||
implementorHolder.numberOfImplementations++; | |||
else | |||
implementorsByType[componentType] = new Tuple<object, int>(implementor, 1); | |||
@@ -152,39 +157,44 @@ namespace Svelto.ECS.Internal | |||
} | |||
#if DEBUG && !PROFILER | |||
else | |||
Utility.Console.LogError(NULL_IMPLEMENTOR_ERROR.FastConcat(entityView.ToString())); | |||
{ | |||
Console.LogError(NULL_IMPLEMENTOR_ERROR.FastConcat(entityView.ToString())); | |||
} | |||
#endif | |||
} | |||
for (int i = 0; i < count; i++) | |||
for (var i = 0; i < count; i++) | |||
{ | |||
var fieldSetter = setters[i]; | |||
Type fieldType = fieldSetter.Key; | |||
var fieldSetter = setters[i]; | |||
var fieldType = fieldSetter.Key; | |||
#if DEBUG && !PROFILER | |||
Tuple<object, int> component; | |||
Tuple<object, int> component; | |||
#else | |||
object component; | |||
#endif | |||
if (implementorsByType.TryGetValue(fieldType, out component) == false) | |||
{ | |||
Exception e = new Exception(NOT_FOUND_EXCEPTION + " Component Type: " + fieldType.Name + " - EntityView: " + | |||
entityView.GetType().Name + " - EntityDescriptor " + entityDescriptorName); | |||
if (implementorsByType.TryGetValue(fieldType, out component) == false) | |||
{ | |||
var e = new Exception(NOT_FOUND_EXCEPTION + " Component Type: " + fieldType.Name + | |||
" - EntityView: " + | |||
entityView.GetType().Name + " - EntityDescriptor " + entityDescriptorName); | |||
throw e; | |||
} | |||
throw e; | |||
} | |||
#if DEBUG && !PROFILER | |||
if (component.numberOfImplementations > 1) | |||
Utility.Console.LogError(DUPLICATE_IMPLEMENTOR_ERROR.FastConcat( | |||
"Component Type: ", fieldType.Name, " implementor: ", | |||
component.implementorType.ToString()) + " - EntityView: " + | |||
entityView.GetType().Name + " - EntityDescriptor " + entityDescriptorName); | |||
if (component.numberOfImplementations > 1) | |||
Console.LogError(DUPLICATE_IMPLEMENTOR_ERROR.FastConcat( | |||
"Component Type: ", fieldType.Name, | |||
" implementor: ", | |||
component.implementorType.ToString()) + | |||
" - EntityView: " + | |||
entityView.GetType().Name + " - EntityDescriptor " + entityDescriptorName); | |||
#endif | |||
#if DEBUG && !PROFILER | |||
fieldSetter.Value.Call(entityView, component.implementorType); | |||
fieldSetter.Value.Call(entityView, component.implementorType); | |||
#else | |||
keyValuePair.Value.Call(entityView, component); | |||
fieldSetter.Value.Call(entityView, component); | |||
#endif | |||
} | |||
@@ -193,17 +203,17 @@ namespace Svelto.ECS.Internal | |||
#if DEBUG && !PROFILER | |||
struct Tuple<T1, T2> | |||
{ | |||
public T1 implementorType; | |||
public T2 numberOfImplementations; | |||
public readonly T1 implementorType; | |||
public T2 numberOfImplementations; | |||
public Tuple(T1 implementor, T2 v) | |||
{ | |||
implementorType = implementor; | |||
implementorType = implementor; | |||
numberOfImplementations = v; | |||
} | |||
} | |||
#endif | |||
static Dictionary<Type, Type[]> _cachedTypes = new Dictionary<Type, Type[]>(); | |||
#endif | |||
static readonly Dictionary<Type, Type[]> _cachedTypes = new Dictionary<Type, Type[]>(); | |||
const string DUPLICATE_IMPLEMENTOR_ERROR = | |||
"<color=orange>Svelto.ECS</color> the same component is implemented with more than one implementor. This is considered an error and MUST be fixed. "; | |||
@@ -3,8 +3,7 @@ | |||
class EntityInfoView : EntityView | |||
{ | |||
internal IEntityViewBuilder[] entityViews; | |||
internal int groupID; | |||
internal bool isInAGroup; | |||
internal int groupID; | |||
internal bool isInAGroup; | |||
} | |||
} | |||
} |
@@ -4,6 +4,6 @@ namespace Svelto.ECS.Schedulers | |||
{ | |||
public abstract class EntitySubmissionScheduler | |||
{ | |||
abstract public void Schedule(WeakAction submitEntityViews); | |||
public abstract void Schedule(WeakAction submitEntityViews); | |||
} | |||
} |
@@ -79,7 +79,8 @@ namespace Svelto.ECS.Experimental | |||
{ | |||
T convert = (T)entityView; | |||
var fasterList = (SharedGroupedStructEntityViewsLists.NoVirt.GetList<T>(_container, groupID) as FasterList<T>); | |||
var fasterList = | |||
(SharedGroupedStructEntityViewsLists.NoVirt.GetList<T>(_container, groupID) as FasterList<T>); | |||
indices[entityView.ID] = fasterList.Count; | |||
fasterList.Add(convert); | |||
@@ -87,7 +88,8 @@ namespace Svelto.ECS.Experimental | |||
public void Remove(int groupID, T entityView) | |||
{ | |||
var fasterList = (SharedGroupedStructEntityViewsLists.NoVirt.GetList<T>(_container, groupID) as FasterList<T>); | |||
var fasterList = | |||
(SharedGroupedStructEntityViewsLists.NoVirt.GetList<T>(_container, groupID) as FasterList<T>); | |||
var index = indices[entityView.ID]; | |||
indices.Remove(entityView.ID); | |||
@@ -97,7 +99,8 @@ namespace Svelto.ECS.Experimental | |||
public T[] GetList(int groupID, out int numberOfItems) | |||
{ | |||
var fasterList = (SharedGroupedStructEntityViewsLists.NoVirt.GetList<T>(_container, groupID) as FasterList<T>); | |||
var fasterList = | |||
(SharedGroupedStructEntityViewsLists.NoVirt.GetList<T>(_container, groupID) as FasterList<T>); | |||
return FasterList<T>.NoVirt.ToArrayFast(fasterList, out numberOfItems); | |||
} | |||
@@ -2,7 +2,7 @@ namespace Svelto.ECS.Internal | |||
{ | |||
public interface IHandleEntityViewEngine : IEngine | |||
{ | |||
void Add(IEntityView entityView); | |||
void Add(IEntityView entityView); | |||
void Remove(IEntityView entityView); | |||
} | |||
} | |||
@@ -10,5 +10,6 @@ namespace Svelto.ECS.Internal | |||
namespace Svelto.ECS | |||
{ | |||
public interface IEngine | |||
{} | |||
} | |||
{ | |||
} | |||
} |
@@ -4,4 +4,4 @@ namespace Svelto.ECS | |||
{ | |||
IEntityDescriptorInfo RetrieveDescriptor(); | |||
} | |||
} | |||
} |
@@ -4,21 +4,20 @@ namespace Svelto.ECS | |||
{ | |||
public interface IEntityViewsDB | |||
{ | |||
FasterReadOnlyList<T> QueryEntityViews<T>() where T:EntityView; | |||
FasterReadOnlyList<T> QueryMetaEntityViews<T>() where T: EntityView; | |||
FasterReadOnlyList<T> QueryGroupedEntityViews<T>(int group) where T: EntityView; | |||
T[] QueryEntityViewsAsArray<T>(out int count) where T: IEntityView; | |||
T[] QueryGroupedEntityViewsAsArray<T>(int @group, out int count) where T: IEntityView; | |||
ReadOnlyDictionary<int, T> QueryIndexableEntityViews<T>() where T: EntityView; | |||
ReadOnlyDictionary<int, T> QueryIndexableMetaEntityViews<T>() where T: EntityView; | |||
FasterReadOnlyList<T> QueryEntityViews<T>() where T : EntityView; | |||
FasterReadOnlyList<T> QueryMetaEntityViews<T>() where T : EntityView; | |||
FasterReadOnlyList<T> QueryGroupedEntityViews<T>(int group) where T : EntityView; | |||
T[] QueryEntityViewsAsArray<T>(out int count) where T : IEntityView; | |||
T[] QueryGroupedEntityViewsAsArray<T>(int group, out int count) where T : IEntityView; | |||
ReadOnlyDictionary<int, T> QueryIndexableEntityViews<T>() where T : EntityView; | |||
ReadOnlyDictionary<int, T> QueryIndexableMetaEntityViews<T>() where T : EntityView; | |||
bool TryQueryEntityView<T>(int ID, out T entityView) where T : EntityView; | |||
T QueryEntityView<T>(int ID) where T: EntityView; | |||
T QueryEntityView<T>(int ID) where T : EntityView; | |||
bool TryQueryMetaEntityView<T>(int metaEntityID, out T entityView) where T: EntityView; | |||
T QueryMetaEntityView<T>(int metaEntityID) where T: EntityView; | |||
bool TryQueryMetaEntityView<T>(int metaEntityID, out T entityView) where T : EntityView; | |||
T QueryMetaEntityView<T>(int metaEntityID) where T : EntityView; | |||
} | |||
} | |||
} |
@@ -2,11 +2,8 @@ using Svelto.ECS.Internal; | |||
namespace Svelto.ECS.Internal | |||
{ | |||
public abstract class MultiEntityViewsEngine<T>:IHandleEntityViewEngine where T:EntityView, new() | |||
public abstract class MultiEntityViewsEngine<T> : IHandleEntityViewEngine where T : EntityView, new() | |||
{ | |||
protected abstract void Add(T entityView); | |||
protected abstract void Remove(T entityView); | |||
public virtual void Add(IEntityView entityView) | |||
{ | |||
Add((T) entityView); | |||
@@ -16,56 +13,49 @@ namespace Svelto.ECS.Internal | |||
{ | |||
Remove((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:EntityView, new() where T : EntityView, new() | |||
where U : EntityView, new() where T : EntityView, new() | |||
{ | |||
protected abstract void Add(U entityView); | |||
protected abstract void Add(U entityView); | |||
protected abstract void Remove(U entityView); | |||
public override void Add(IEntityView entityView) | |||
{ | |||
var castedEntityView = entityView as U; | |||
if (castedEntityView != null) | |||
{ | |||
Add(castedEntityView); | |||
} | |||
else | |||
{ | |||
base.Add(entityView); | |||
} | |||
} | |||
public override void Remove(IEntityView entityView) | |||
{ | |||
if (entityView is U) | |||
{ | |||
Remove((U) entityView); | |||
} | |||
else | |||
{ | |||
base.Remove(entityView); | |||
} | |||
} | |||
} | |||
public abstract class MultiEntityViewsEngine<T, U, V> : MultiEntityViewsEngine<T, U> | |||
where V : EntityView, new() where U : EntityView, new() where T : EntityView, new() | |||
{ | |||
protected abstract void Add(V entityView); | |||
protected abstract void Add(V entityView); | |||
protected abstract void Remove(V entityView); | |||
public override void Add(IEntityView entityView) | |||
{ | |||
var castedEntityView = entityView as V; | |||
if (castedEntityView != null) | |||
{ | |||
Add(castedEntityView); | |||
} | |||
else | |||
base.Add(entityView); | |||
} | |||
@@ -74,32 +64,28 @@ namespace Svelto.ECS | |||
{ | |||
var castedEntityView = entityView as V; | |||
if (castedEntityView != null) | |||
{ | |||
Remove(castedEntityView); | |||
} | |||
else | |||
base.Remove(entityView); | |||
} | |||
} | |||
/// <summary> | |||
/// Please do not add more MultiEntityViewsEngine | |||
/// if you use more than 4 nodes, your engine has | |||
/// already too many responsabilities. | |||
/// Please do not add more MultiEntityViewsEngine | |||
/// 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 : EntityView, new() where V : EntityView, new() where U : EntityView, new() where T : EntityView, new() | |||
{ | |||
protected abstract void Add(W entityView); | |||
protected abstract void Add(W entityView); | |||
protected abstract void Remove(W entityView); | |||
public override void Add(IEntityView entityView) | |||
{ | |||
var castedEntityView = entityView as W; | |||
if (castedEntityView != null) | |||
{ | |||
Add(castedEntityView); | |||
} | |||
else | |||
base.Add(entityView); | |||
} | |||
@@ -108,9 +94,7 @@ namespace Svelto.ECS | |||
{ | |||
var castedEntityView = entityView as W; | |||
if (castedEntityView != null) | |||
{ | |||
Remove(castedEntityView); | |||
} | |||
else | |||
base.Remove(entityView); | |||
} | |||
@@ -1,5 +1,4 @@ | |||
#if UNITY_EDITOR | |||
using System; | |||
using UnityEditor; | |||
using UnityEngine; | |||
@@ -12,6 +12,8 @@ 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, IEntityView entityView) | |||
{ | |||
EngineInfo info; | |||
@@ -42,19 +44,12 @@ 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>(); | |||
} | |||
} | |||
} |
@@ -2,19 +2,19 @@ using Svelto.ECS.Internal; | |||
namespace Svelto.ECS | |||
{ | |||
public abstract class SingleEntityViewEngine<T> : IHandleEntityViewEngine where T:EntityView, new() | |||
public abstract class SingleEntityViewEngine<T> : IHandleEntityViewEngine where T : EntityView, new() | |||
{ | |||
public void Add(IEntityView entityView) | |||
{ | |||
Add((T)entityView); //when byref returns will be vailable, this should be passed by reference, not copy! | |||
Add((T) entityView); //when byref returns will be vailable, this should be passed by reference, not copy! | |||
} | |||
public void Remove(IEntityView entityView) | |||
{ | |||
Remove((T)entityView); | |||
Remove((T) entityView); | |||
} | |||
protected abstract void Add(T entityView); | |||
protected abstract void Add(T entityView); | |||
protected abstract void Remove(T entityView); | |||
} | |||
} | |||
} |