fix EcsGroup

This commit is contained in:
Mikhail 2023-12-24 18:05:30 +08:00
parent 7a6dc93bdb
commit 89cb4c68b9

View File

@ -10,6 +10,7 @@ using System.Runtime.InteropServices;
namespace DCFApixels.DragonECS namespace DCFApixels.DragonECS
{ {
//_dense заполняется с индекса 1 //_dense заполняется с индекса 1
//в операциях изменяющих состояние группы нельзя итерироваться по this, либо осторожно учитывать этот момент
[StructLayout(LayoutKind.Sequential, Pack = 0, Size = 8)] [StructLayout(LayoutKind.Sequential, Pack = 0, Size = 8)]
[DebuggerTypeProxy(typeof(DebuggerProxy))] [DebuggerTypeProxy(typeof(DebuggerProxy))]
public readonly ref struct EcsReadonlyGroup public readonly ref struct EcsReadonlyGroup
@ -369,19 +370,41 @@ namespace DCFApixels.DragonECS
#if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS #if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS
if (_source != group.World) Throw.Group_ArgumentDifferentWorldsException(); if (_source != group.World) Throw.Group_ArgumentDifferentWorldsException();
#endif #endif
//if (group.Count > Count) // вариант 1. есть итерация по this
//{
// foreach (var item in this)
// if (group.Has(item))
// RemoveInternal(item);
//}
//else
//{
// foreach (var item in group)
// if (Has(item))
// RemoveInternal(item);
//}
//foreach (var item in group) // вариант 2
// if (Has(item))
// RemoveInternal(item);
if (group.Count > Count) if (group.Count > Count)
{ {
foreach (var item in this) for (int i = _count; i > 0; i--)//итерация в обратном порядке исключает ошибки при удалении элементов
{
int item = _dense[i];
if (group.Has(item)) if (group.Has(item))
RemoveInternal(item); RemoveInternal(item);
} }
}
else else
{ {
foreach (var item in group) foreach (var item in group)
{
if (Has(item)) if (Has(item))
RemoveInternal(item); RemoveInternal(item);
} }
} }
}
public void ExceptWith(EcsSpan span) public void ExceptWith(EcsSpan span)
{ {
#if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS #if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS
@ -405,10 +428,13 @@ namespace DCFApixels.DragonECS
#if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS #if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS
if (World != group.World) Throw.Group_ArgumentDifferentWorldsException(); if (World != group.World) Throw.Group_ArgumentDifferentWorldsException();
#endif #endif
foreach (var item in this) for (int i = _count; i > 0; i--)//итерация в обратном порядке исключает ошибки при удалении элементов
{
int item = _dense[i];
if (!group.Has(item)) if (!group.Has(item))
RemoveInternal(item); RemoveInternal(item);
} }
}
#endregion #endregion
#region SymmetricExceptWith #region SymmetricExceptWith