update mask, add world.TryFindPoolInstance

This commit is contained in:
DCFApixels 2025-03-17 11:06:52 +08:00
parent 8ad85548a2
commit 3986149267
3 changed files with 32 additions and 11 deletions

View File

@ -272,13 +272,13 @@ namespace DCFApixels.DragonECS
public TPool IncludePool<TPool>() where TPool : IEcsPoolImplementation, new() public TPool IncludePool<TPool>() where TPool : IEcsPoolImplementation, new()
{ {
var pool = CachePool<TPool>(); var pool = CachePool<TPool>();
IncludeImplicit(pool.ComponentType); SetMaskInclude(pool.ComponentType);
return pool; return pool;
} }
public TPool ExcludePool<TPool>() where TPool : IEcsPoolImplementation, new() public TPool ExcludePool<TPool>() where TPool : IEcsPoolImplementation, new()
{ {
var pool = CachePool<TPool>(); var pool = CachePool<TPool>();
ExcludeImplicit(pool.ComponentType); SetMaskExclude(pool.ComponentType);
return pool; return pool;
} }
public TPool OptionalPool<TPool>() where TPool : IEcsPoolImplementation, new() public TPool OptionalPool<TPool>() where TPool : IEcsPoolImplementation, new()
@ -291,14 +291,14 @@ namespace DCFApixels.DragonECS
var pool = _world.GetPoolInstance<TPool>(); var pool = _world.GetPoolInstance<TPool>();
return pool; return pool;
} }
private void IncludeImplicit(Type type) public void SetMaskInclude(Type type)
{ {
if (_maskBuilder.IsNull == false) if (_maskBuilder.IsNull == false)
{ {
_maskBuilder.Inc(type); _maskBuilder.Inc(type);
} }
} }
private void ExcludeImplicit(Type type) public void SetMaskExclude(Type type)
{ {
if (_maskBuilder.IsNull == false) if (_maskBuilder.IsNull == false)
{ {
@ -334,8 +334,8 @@ namespace DCFApixels.DragonECS
IncludePool<TPool>(); IncludePool<TPool>();
ExcludePool<TPool>(); ExcludePool<TPool>();
OptionalPool<TPool>(); OptionalPool<TPool>();
IncludeImplicit(null); SetMaskInclude(null);
ExcludeImplicit(null); SetMaskExclude(null);
} }
#endregion #endregion
} }

View File

@ -488,7 +488,7 @@ namespace DCFApixels.DragonECS
[Il2CppSetOption(Option.NullChecks, false)] [Il2CppSetOption(Option.NullChecks, false)]
[Il2CppSetOption(Option.ArrayBoundsChecks, false)] [Il2CppSetOption(Option.ArrayBoundsChecks, false)]
#endif #endif
public class EcsMaskIterator public class EcsMaskIterator : IDisposable
{ {
// TODO есть идея перенести эти ChunckBuffer-ы в стек, // TODO есть идея перенести эти ChunckBuffer-ы в стек,
// для этого нужно проработать дизайн так чтобы память в стеке выделялась за пределами итератора и GetEnumerator, // для этого нужно проработать дизайн так чтобы память в стеке выделялась за пределами итератора и GetEnumerator,
@ -555,6 +555,15 @@ namespace DCFApixels.DragonECS
} }
} }
unsafe ~EcsMaskIterator() unsafe ~EcsMaskIterator()
{
Cleanup(false);
}
public void Dispose()
{
Cleanup(true);
GC.SuppressFinalize(this);
}
private void Cleanup(bool disposing)
{ {
_sortIncBuffer.ReadonlyDispose(); _sortIncBuffer.ReadonlyDispose();
//_sortExcBuffer.ReadonlyDispose();// использует общую памяять с _sortIncBuffer; //_sortExcBuffer.ReadonlyDispose();// использует общую памяять с _sortIncBuffer;
@ -563,7 +572,7 @@ namespace DCFApixels.DragonECS
} }
#endregion #endregion
#region SortConstraints #region SortConstraints/TryFindEntityStorage
private unsafe int SortConstraints_Internal() private unsafe int SortConstraints_Internal()
{ {
UnsafeArray<int> sortIncBuffer = _sortIncBuffer; UnsafeArray<int> sortIncBuffer = _sortIncBuffer;
@ -611,7 +620,7 @@ namespace DCFApixels.DragonECS
// Поэтому исключающее ограничение игнорируется для maxEntites. // Поэтому исключающее ограничение игнорируется для maxEntites.
return maxEntites; return maxEntites;
} }
private unsafe bool TryGetEntityStorage(out IEntityStorage storage) private unsafe bool TryFindEntityStorage(out IEntityStorage storage)
{ {
if (_isHasAnyEntityStorage) if (_isHasAnyEntityStorage)
{ {
@ -737,7 +746,7 @@ namespace DCFApixels.DragonECS
{ {
return new Enumerator(_span.Slice(0, 0), _iterator); return new Enumerator(_span.Slice(0, 0), _iterator);
} }
if (_iterator.TryGetEntityStorage(out IEntityStorage storage)) if (_iterator.TryFindEntityStorage(out IEntityStorage storage))
{ {
return new Enumerator(storage.ToSpan(), _iterator); return new Enumerator(storage.ToSpan(), _iterator);
} }
@ -811,6 +820,7 @@ namespace DCFApixels.DragonECS
#region Iterate/Enumerable OnlyInc #region Iterate/Enumerable OnlyInc
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public OnlyIncEnumerable IterateOnlyInc(EcsSpan span) { return new OnlyIncEnumerable(this, span); } public OnlyIncEnumerable IterateOnlyInc(EcsSpan span) { return new OnlyIncEnumerable(this, span); }
#if ENABLE_IL2CPP #if ENABLE_IL2CPP
[Il2CppSetOption(Option.NullChecks, false)] [Il2CppSetOption(Option.NullChecks, false)]
[Il2CppSetOption(Option.ArrayBoundsChecks, false)] [Il2CppSetOption(Option.ArrayBoundsChecks, false)]
@ -876,7 +886,7 @@ namespace DCFApixels.DragonECS
{ {
return new Enumerator(_span.Slice(0, 0), _iterator); return new Enumerator(_span.Slice(0, 0), _iterator);
} }
if (_iterator.TryGetEntityStorage(out IEntityStorage storage)) if (_iterator.TryFindEntityStorage(out IEntityStorage storage))
{ {
return new Enumerator(storage.ToSpan(), _iterator); return new Enumerator(storage.ToSpan(), _iterator);
} }

View File

@ -40,6 +40,17 @@ namespace DCFApixels.DragonECS
{ {
return FindPoolInstance_Internal(GetComponentTypeID(componentType)); return FindPoolInstance_Internal(GetComponentTypeID(componentType));
} }
public bool TryFindPoolInstance(int componentTypeID, out IEcsPool pool)
{
pool = FindPoolInstance(componentTypeID);
return pool.IsNullOrDummy() == false;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool TryFindPoolInstance(Type componentType, out IEcsPool pool)
{
pool = FindPoolInstance(componentType);
return pool.IsNullOrDummy() == false;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
private IEcsPool FindPoolInstance_Internal(int componentTypeID) private IEcsPool FindPoolInstance_Internal(int componentTypeID)
{ {