mirror of
https://github.com/DCFApixels/DragonECS.git
synced 2025-09-18 09:54:35 +08:00
update EcsWorld
This commit is contained in:
parent
cdbb33b6ab
commit
4c183bd126
@ -371,7 +371,7 @@ namespace DCFApixels.DragonECS
|
|||||||
#region Include/Exclude/Combine
|
#region Include/Exclude/Combine
|
||||||
public Builder Include<T>()
|
public Builder Include<T>()
|
||||||
{
|
{
|
||||||
int id = _world.GetComponentID<T>();
|
int id = _world.GetComponentTypeID<T>();
|
||||||
#if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS
|
#if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS
|
||||||
if (_inc.Contains(id) || _exc.Contains(id)) Throw.ConstraintIsAlreadyContainedInMask(typeof(T));
|
if (_inc.Contains(id) || _exc.Contains(id)) Throw.ConstraintIsAlreadyContainedInMask(typeof(T));
|
||||||
#endif
|
#endif
|
||||||
@ -380,7 +380,7 @@ namespace DCFApixels.DragonECS
|
|||||||
}
|
}
|
||||||
public Builder Exclude<T>()
|
public Builder Exclude<T>()
|
||||||
{
|
{
|
||||||
int id = _world.GetComponentID<T>();
|
int id = _world.GetComponentTypeID<T>();
|
||||||
#if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS
|
#if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS
|
||||||
if (_inc.Contains(id) || _exc.Contains(id)) Throw.ConstraintIsAlreadyContainedInMask(typeof(T));
|
if (_inc.Contains(id) || _exc.Contains(id)) Throw.ConstraintIsAlreadyContainedInMask(typeof(T));
|
||||||
#endif
|
#endif
|
||||||
@ -389,7 +389,7 @@ namespace DCFApixels.DragonECS
|
|||||||
}
|
}
|
||||||
public Builder Include(Type type)
|
public Builder Include(Type type)
|
||||||
{
|
{
|
||||||
int id = _world.GetComponentID(type);
|
int id = _world.GetComponentTypeID(type);
|
||||||
#if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS
|
#if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS
|
||||||
if (_inc.Contains(id) || _exc.Contains(id)) Throw.ConstraintIsAlreadyContainedInMask(type);
|
if (_inc.Contains(id) || _exc.Contains(id)) Throw.ConstraintIsAlreadyContainedInMask(type);
|
||||||
#endif
|
#endif
|
||||||
@ -398,7 +398,7 @@ namespace DCFApixels.DragonECS
|
|||||||
}
|
}
|
||||||
public Builder Exclude(Type type)
|
public Builder Exclude(Type type)
|
||||||
{
|
{
|
||||||
int id = _world.GetComponentID(type);
|
int id = _world.GetComponentTypeID(type);
|
||||||
#if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS
|
#if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS
|
||||||
if (_inc.Contains(id) || _exc.Contains(id)) Throw.ConstraintIsAlreadyContainedInMask(type);
|
if (_inc.Contains(id) || _exc.Contains(id)) Throw.ConstraintIsAlreadyContainedInMask(type);
|
||||||
#endif
|
#endif
|
||||||
|
@ -144,8 +144,8 @@ namespace DCFApixels.DragonECS
|
|||||||
ReleaseData(id);
|
ReleaseData(id);
|
||||||
_worldIdDispenser.Release(id);
|
_worldIdDispenser.Release(id);
|
||||||
_isDestroyed = true;
|
_isDestroyed = true;
|
||||||
_poolIds = null;
|
_poolTypeCode_2_CmpTypeIDs = null;
|
||||||
_componentIds = null;
|
_componentTypeCode_2_CmpTypeIDs = null;
|
||||||
}
|
}
|
||||||
//public void Clear() { }
|
//public void Clear() { }
|
||||||
#endregion
|
#endregion
|
||||||
|
@ -7,32 +7,14 @@ namespace DCFApixels.DragonECS
|
|||||||
{
|
{
|
||||||
public abstract partial class EcsWorld
|
public abstract partial class EcsWorld
|
||||||
{
|
{
|
||||||
private SparseArray<int> _poolIds = new SparseArray<int>();
|
private SparseArray<int> _poolTypeCode_2_CmpTypeIDs = new SparseArray<int>();
|
||||||
private SparseArray<int> _componentIds = new SparseArray<int>();
|
private SparseArray<int> _componentTypeCode_2_CmpTypeIDs = new SparseArray<int>();
|
||||||
private int _poolsCount;
|
private int _poolsCount;
|
||||||
internal IEcsPoolImplementation[] _pools;
|
internal IEcsPoolImplementation[] _pools;
|
||||||
//private int[] _sortedPoolIds;
|
|
||||||
//private int[] _sortedPoolIdsMapping;
|
|
||||||
internal int[] _poolComponentCounts;
|
internal int[] _poolComponentCounts;
|
||||||
|
|
||||||
private static EcsNullPool _nullPool = EcsNullPool.instance;
|
private static EcsNullPool _nullPool = EcsNullPool.instance;
|
||||||
|
|
||||||
#region ComponentInfo
|
|
||||||
public int GetComponentID<T>() => DeclareComponentType(EcsTypeCode.Get<T>());
|
|
||||||
public int GetComponentID(Type type) => DeclareComponentType(EcsTypeCode.Get(type));
|
|
||||||
public bool IsComponentTypeDeclared<T>() => _componentIds.Contains(EcsTypeCode.Get<T>());
|
|
||||||
public bool IsComponentTypeDeclared(Type type) => _componentIds.Contains(EcsTypeCode.Get(type));
|
|
||||||
public bool IsComponentTypeDeclared(int componentTypeID)
|
|
||||||
{
|
|
||||||
if (componentTypeID >= 0 && componentTypeID < _pools.Length)
|
|
||||||
{
|
|
||||||
return _pools[componentTypeID] != _nullPool;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
public Type GetComponentType(int componentID) => _pools[componentID].ComponentType;
|
|
||||||
#endregion
|
|
||||||
|
|
||||||
#region Getters
|
#region Getters
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
@ -76,34 +58,67 @@ namespace DCFApixels.DragonECS
|
|||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Declare/Create
|
#region ComponentInfo
|
||||||
private int DeclareComponentType(int typeCode)
|
public int GetComponentTypeID<TComponent>()
|
||||||
{
|
{
|
||||||
if (!_componentIds.TryGetValue(typeCode, out int componentId))
|
return DeclareOrGetComponentTypeID(EcsTypeCode.Get<TComponent>());
|
||||||
|
}
|
||||||
|
public int GetComponentTypeID(Type componentType)
|
||||||
|
{
|
||||||
|
return DeclareOrGetComponentTypeID(EcsTypeCode.Get(componentType));
|
||||||
|
}
|
||||||
|
public bool IsComponentTypeDeclared<TComponent>()
|
||||||
|
{
|
||||||
|
return _componentTypeCode_2_CmpTypeIDs.Contains(EcsTypeCode.Get<TComponent>());
|
||||||
|
}
|
||||||
|
public bool IsComponentTypeDeclared(Type componentType)
|
||||||
|
{
|
||||||
|
return _componentTypeCode_2_CmpTypeIDs.Contains(EcsTypeCode.Get(componentType));
|
||||||
|
}
|
||||||
|
public bool IsComponentTypeDeclared(int componentTypeID)
|
||||||
|
{
|
||||||
|
if (componentTypeID >= 0 && componentTypeID < _pools.Length)
|
||||||
{
|
{
|
||||||
componentId = _poolsCount++;
|
return _pools[componentTypeID] != _nullPool;
|
||||||
_componentIds.Add(typeCode, componentId);
|
|
||||||
}
|
}
|
||||||
return componentId;
|
return false;
|
||||||
|
}
|
||||||
|
public Type GetComponentType(int componentTypeID)
|
||||||
|
{
|
||||||
|
return _pools[componentTypeID].ComponentType;
|
||||||
|
}
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region Declare/Create
|
||||||
|
private int DeclareOrGetComponentTypeID(int componentTypeCode)
|
||||||
|
{
|
||||||
|
if (!_componentTypeCode_2_CmpTypeIDs.TryGetValue(componentTypeCode, out int ComponentTypeID))
|
||||||
|
{
|
||||||
|
ComponentTypeID = _poolsCount++;
|
||||||
|
_componentTypeCode_2_CmpTypeIDs.Add(componentTypeCode, ComponentTypeID);
|
||||||
|
}
|
||||||
|
return ComponentTypeID;
|
||||||
}
|
}
|
||||||
private TPool CreatePool<TPool>() where TPool : IEcsPoolImplementation, new()
|
private TPool CreatePool<TPool>() where TPool : IEcsPoolImplementation, new()
|
||||||
{
|
{
|
||||||
int poolTypeCode = EcsTypeCode.Get<TPool>();
|
int poolTypeCode = EcsTypeCode.Get<TPool>();
|
||||||
if (_poolIds.Contains(poolTypeCode))
|
if (_poolTypeCode_2_CmpTypeIDs.Contains(poolTypeCode))
|
||||||
throw new EcsFrameworkException("The pool has already been created.");
|
{
|
||||||
|
Throw.World_PoolAlreadyCreated();
|
||||||
|
}
|
||||||
|
|
||||||
Type componentType = typeof(TPool).GetInterfaces().First(o => o.IsGenericType && o.GetGenericTypeDefinition() == typeof(IEcsPoolImplementation<>)).GetGenericArguments()[0];
|
Type componentType = typeof(TPool).GetInterfaces().First(o => o.IsGenericType && o.GetGenericTypeDefinition() == typeof(IEcsPoolImplementation<>)).GetGenericArguments()[0];
|
||||||
int componentTypeCode = EcsTypeCode.Get(componentType);
|
int componentTypeCode = EcsTypeCode.Get(componentType);
|
||||||
|
|
||||||
if (_componentIds.TryGetValue(componentTypeCode, out int componentTypeID))
|
if (_componentTypeCode_2_CmpTypeIDs.TryGetValue(componentTypeCode, out int componentTypeID))
|
||||||
{
|
{
|
||||||
_poolIds[poolTypeCode] = componentTypeID;
|
_poolTypeCode_2_CmpTypeIDs[poolTypeCode] = componentTypeID;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
componentTypeID = _poolsCount++;
|
componentTypeID = _poolsCount++;
|
||||||
_poolIds[poolTypeCode] = componentTypeID;
|
_poolTypeCode_2_CmpTypeIDs[poolTypeCode] = componentTypeID;
|
||||||
_componentIds[componentTypeCode] = componentTypeID;
|
_componentTypeCode_2_CmpTypeIDs[componentTypeCode] = componentTypeID;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (_poolsCount >= _pools.Length)
|
if (_poolsCount >= _pools.Length)
|
||||||
@ -111,12 +126,12 @@ namespace DCFApixels.DragonECS
|
|||||||
int oldCapacity = _pools.Length;
|
int oldCapacity = _pools.Length;
|
||||||
Array.Resize(ref _pools, _pools.Length << 1);
|
Array.Resize(ref _pools, _pools.Length << 1);
|
||||||
Array.Resize(ref _poolComponentCounts, _pools.Length);
|
Array.Resize(ref _poolComponentCounts, _pools.Length);
|
||||||
//Array.Resize(ref _sortedPoolIds, _pools.Length);
|
|
||||||
//Array.Resize(ref _sortedPoolIdsMapping, _pools.Length);
|
|
||||||
ArrayUtility.Fill(_pools, _nullPool, oldCapacity, oldCapacity - _pools.Length);
|
ArrayUtility.Fill(_pools, _nullPool, oldCapacity, oldCapacity - _pools.Length);
|
||||||
|
|
||||||
for (int i = 0; i < _entitesCapacity; i++)
|
for (int i = 0; i < _entitesCapacity; i++)
|
||||||
|
{
|
||||||
Array.Resize(ref _entitiesComponentMasks[i], _pools.Length / 32 + 1);
|
Array.Resize(ref _entitiesComponentMasks[i], _pools.Length / 32 + 1);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (_pools[componentTypeID] == _nullPool)
|
if (_pools[componentTypeID] == _nullPool)
|
||||||
|
@ -104,6 +104,12 @@ namespace DCFApixels.DragonECS.Internal
|
|||||||
throw new EcsFrameworkException($"An entity with identifier {entityID} is already contained in this world");
|
throw new EcsFrameworkException($"An entity with identifier {entityID} is already contained in this world");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||||
|
public static void World_PoolAlreadyCreated()
|
||||||
|
{
|
||||||
|
throw new EcsFrameworkException("The pool has already been created.");
|
||||||
|
}
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||||
internal static void Ent_ThrowIsNotAlive(entlong entity)
|
internal static void Ent_ThrowIsNotAlive(entlong entity)
|
||||||
{
|
{
|
||||||
|
Loading…
Reference in New Issue
Block a user