mirror of
https://github.com/DCFApixels/DragonECS.git
synced 2025-09-18 01:44:35 +08:00
update new masks
This commit is contained in:
parent
5edd55df88
commit
f9db91eeb2
123
src/EcsAspect.cs
123
src/EcsAspect.cs
@ -30,8 +30,8 @@ namespace DCFApixels.DragonECS
|
|||||||
public sealed class Builder : EcsAspectBuilderBase
|
public sealed class Builder : EcsAspectBuilderBase
|
||||||
{
|
{
|
||||||
private EcsWorld _world;
|
private EcsWorld _world;
|
||||||
private HashSet<int> _inc;
|
private Dictionary<int, int> _inc;
|
||||||
private HashSet<int> _exc;
|
private Dictionary<int, int> _exc;
|
||||||
private List<Combined> _combined;
|
private List<Combined> _combined;
|
||||||
|
|
||||||
public EcsWorld World => _world;
|
public EcsWorld World => _world;
|
||||||
@ -40,8 +40,8 @@ namespace DCFApixels.DragonECS
|
|||||||
{
|
{
|
||||||
_world = world;
|
_world = world;
|
||||||
_combined = new List<Combined>();
|
_combined = new List<Combined>();
|
||||||
_inc = new HashSet<int>();
|
_inc = new Dictionary<int, int>();
|
||||||
_exc = new HashSet<int>();
|
_exc = new Dictionary<int, int>();
|
||||||
}
|
}
|
||||||
internal static TAspect Build<TAspect>(EcsWorld world) where TAspect : EcsAspect
|
internal static TAspect Build<TAspect>(EcsWorld world) where TAspect : EcsAspect
|
||||||
{
|
{
|
||||||
@ -82,18 +82,24 @@ namespace DCFApixels.DragonECS
|
|||||||
private void IncludeImplicit(Type type)
|
private void IncludeImplicit(Type type)
|
||||||
{
|
{
|
||||||
int id = _world.GetComponentID(type);
|
int id = _world.GetComponentID(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
|
||||||
_inc.Add(id);
|
var bit = EcsMaskBit.FromPoolID(id);
|
||||||
|
if (!_inc.ContainsKey(bit.chankIndex))
|
||||||
|
_inc.Add(bit.chankIndex, 0);
|
||||||
|
_inc[bit.chankIndex] = _inc[bit.chankIndex] | bit.mask;
|
||||||
}
|
}
|
||||||
private void ExcludeImplicit(Type type)
|
private void ExcludeImplicit(Type type)
|
||||||
{
|
{
|
||||||
int id = _world.GetComponentID(type);
|
int id = _world.GetComponentID(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
|
||||||
_exc.Add(id);
|
var bit = EcsMaskBit.FromPoolID(id);
|
||||||
|
if (!_exc.ContainsKey(bit.chankIndex))
|
||||||
|
_exc.Add(bit.chankIndex, 0);
|
||||||
|
_exc[bit.chankIndex] = _exc[bit.chankIndex] | bit.mask;
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
@ -113,25 +119,26 @@ namespace DCFApixels.DragonECS
|
|||||||
|
|
||||||
private void End(out EcsMask mask)
|
private void End(out EcsMask mask)
|
||||||
{
|
{
|
||||||
HashSet<int> maskInc;
|
Dictionary<int, int> maskInc;
|
||||||
HashSet<int> maskExc;
|
Dictionary<int, int> maskExc;
|
||||||
if (_combined.Count > 0)
|
if (_combined.Count > 0)
|
||||||
{
|
{
|
||||||
maskInc = new HashSet<int>();
|
throw new NotImplementedException();
|
||||||
maskExc = new HashSet<int>();
|
// maskInc = new Dictionary<int, int>();
|
||||||
_combined.Sort((a, b) => a.order - b.order);
|
// maskExc = new Dictionary<int, int>();
|
||||||
foreach (var item in _combined)
|
// _combined.Sort((a, b) => a.order - b.order);
|
||||||
{
|
// foreach (var item in _combined)
|
||||||
EcsMask submask = item.aspect.mask;
|
// {
|
||||||
maskInc.ExceptWith(submask.excChunckMasks);//удаляю конфликтующие ограничения
|
// EcsMask submask = item.aspect.mask;
|
||||||
maskExc.ExceptWith(submask.incChunckMasks);//удаляю конфликтующие ограничения
|
// maskInc.ExceptWith(submask.excChunckMasks);//удаляю конфликтующие ограничения
|
||||||
maskInc.UnionWith(submask.incChunckMasks);
|
// maskExc.ExceptWith(submask.incChunckMasks);//удаляю конфликтующие ограничения
|
||||||
maskExc.UnionWith(submask.excChunckMasks);
|
// maskInc.UnionWith(submask.incChunckMasks);
|
||||||
}
|
// maskExc.UnionWith(submask.excChunckMasks);
|
||||||
maskInc.ExceptWith(_exc);//удаляю конфликтующие ограничения
|
// }
|
||||||
maskExc.ExceptWith(_inc);//удаляю конфликтующие ограничения
|
// maskInc.ExceptWith(_exc);//удаляю конфликтующие ограничения
|
||||||
maskInc.UnionWith(_inc);
|
// maskExc.ExceptWith(_inc);//удаляю конфликтующие ограничения
|
||||||
maskExc.UnionWith(_exc);
|
// maskInc.UnionWith(_inc);
|
||||||
|
// maskExc.UnionWith(_exc);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@ -139,29 +146,10 @@ namespace DCFApixels.DragonECS
|
|||||||
maskExc = _exc;
|
maskExc = _exc;
|
||||||
}
|
}
|
||||||
|
|
||||||
int[] inc = new int[0];
|
mask = new EcsMask(
|
||||||
int[] exc = new int[0];
|
_world.id,
|
||||||
foreach (var v in maskInc)
|
maskInc.Select(o => new EcsMaskBit(o.Key, o.Value)).ToArray(),
|
||||||
{
|
maskExc.Select(o => new EcsMaskBit(o.Key, o.Value)).ToArray());
|
||||||
var bit = EcsMaskBit.FromPoolID(v);
|
|
||||||
if (inc.Length <= bit.chankIndex)
|
|
||||||
Array.Resize(ref inc, bit.chankIndex + 1);
|
|
||||||
inc[bit.chankIndex] |= bit.mask;
|
|
||||||
}
|
|
||||||
foreach (var v in maskExc)
|
|
||||||
{
|
|
||||||
var bit = EcsMaskBit.FromPoolID(v);
|
|
||||||
if (exc.Length <= bit.chankIndex)
|
|
||||||
Array.Resize(ref exc, bit.chankIndex + 1);
|
|
||||||
exc[bit.chankIndex] |= bit.mask;
|
|
||||||
}
|
|
||||||
|
|
||||||
//var inc = maskInc.ToArray();
|
|
||||||
//Array.Sort(inc);
|
|
||||||
//var exc = maskExc.ToArray();
|
|
||||||
//Array.Sort(exc);
|
|
||||||
|
|
||||||
mask = new EcsMask(_world.id, inc.ToArray(), exc.ToArray());
|
|
||||||
_world = null;
|
_world = null;
|
||||||
_inc = null;
|
_inc = null;
|
||||||
_exc = null;
|
_exc = null;
|
||||||
@ -220,14 +208,14 @@ namespace DCFApixels.DragonECS
|
|||||||
public sealed class EcsMask
|
public sealed class EcsMask
|
||||||
{
|
{
|
||||||
internal readonly int worldID;
|
internal readonly int worldID;
|
||||||
internal readonly int[] incChunckMasks;
|
internal readonly EcsMaskBit[] incChunckMasks;
|
||||||
internal readonly int[] excChunckMasks;
|
internal readonly EcsMaskBit[] excChunckMasks;
|
||||||
public int WorldID => worldID;
|
public int WorldID => worldID;
|
||||||
/// <summary>Including constraints</summary>
|
/// <summary>Including constraints</summary>
|
||||||
public ReadOnlySpan<int> Inc => incChunckMasks;
|
public ReadOnlySpan<EcsMaskBit> Inc => incChunckMasks;
|
||||||
/// <summary>Excluding constraints</summary>
|
/// <summary>Excluding constraints</summary>
|
||||||
public ReadOnlySpan<int> Exc => excChunckMasks;
|
public ReadOnlySpan<EcsMaskBit> Exc => excChunckMasks;
|
||||||
internal EcsMask(int worldID, int[] inc, int[] exc)
|
internal EcsMask(int worldID, EcsMaskBit[] inc, EcsMaskBit[] exc)
|
||||||
{
|
{
|
||||||
#if DEBUG
|
#if DEBUG
|
||||||
//CheckConstraints(inc, exc);
|
//CheckConstraints(inc, exc);
|
||||||
@ -266,11 +254,12 @@ namespace DCFApixels.DragonECS
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
private static string CreateLogString(int worldID, int[] inc, int[] exc)
|
private static string CreateLogString(int worldID, EcsMaskBit[] inc, EcsMaskBit[] exc)
|
||||||
{
|
{
|
||||||
#if (DEBUG && !DISABLE_DEBUG)
|
#if (DEBUG && !DISABLE_DEBUG)
|
||||||
string converter(int o) => EcsDebugUtility.GetGenericTypeName(EcsWorld.GetWorld(worldID).AllPools[o].ComponentType, 1);
|
//string converter(int o) => EcsDebugUtility.GetGenericTypeName(EcsWorld.GetWorld(worldID).AllPools[o].ComponentType, 1);
|
||||||
return $"Inc({string.Join(", ", inc.Select(converter))}) Exc({string.Join(", ", exc.Select(converter))})";
|
//return $"Inc({string.Join(", ", inc.Select(converter))}) Exc({string.Join(", ", exc.Select(converter))})";
|
||||||
|
return $"Inc({string.Join(", ", inc)}) Exc({string.Join(", ", exc)})"; // Release optimization
|
||||||
#else
|
#else
|
||||||
return $"Inc({string.Join(", ", inc)}) Exc({string.Join(", ", exc)})"; // Release optimization
|
return $"Inc({string.Join(", ", inc)}) Exc({string.Join(", ", exc)})"; // Release optimization
|
||||||
#endif
|
#endif
|
||||||
@ -279,8 +268,8 @@ namespace DCFApixels.DragonECS
|
|||||||
{
|
{
|
||||||
public readonly EcsWorld world;
|
public readonly EcsWorld world;
|
||||||
public readonly int worldID;
|
public readonly int worldID;
|
||||||
public readonly int[] included;
|
public readonly EcsMaskBit[] included;
|
||||||
public readonly int[] excluded;
|
public readonly EcsMaskBit[] excluded;
|
||||||
//public readonly Type[] includedTypes;
|
//public readonly Type[] includedTypes;
|
||||||
//public readonly Type[] excludedTypes;
|
//public readonly Type[] excludedTypes;
|
||||||
public DebuggerProxy(EcsMask mask)
|
public DebuggerProxy(EcsMask mask)
|
||||||
@ -350,8 +339,8 @@ namespace DCFApixels.DragonECS
|
|||||||
public ref struct Enumerator
|
public ref struct Enumerator
|
||||||
{
|
{
|
||||||
private EcsGroup.Enumerator _sourceGroup;
|
private EcsGroup.Enumerator _sourceGroup;
|
||||||
private readonly int[] _inc;
|
private readonly EcsMaskBit[] _inc;
|
||||||
private readonly int[] _exc;
|
private readonly EcsMaskBit[] _exc;
|
||||||
private readonly int[][] _entitiesComponentMasks;
|
private readonly int[][] _entitiesComponentMasks;
|
||||||
|
|
||||||
public Enumerator(EcsReadonlyGroup sourceGroup, EcsMask mask)
|
public Enumerator(EcsReadonlyGroup sourceGroup, EcsMask mask)
|
||||||
@ -374,11 +363,13 @@ namespace DCFApixels.DragonECS
|
|||||||
int e = _sourceGroup.Current;
|
int e = _sourceGroup.Current;
|
||||||
for (int i = 0, iMax = _inc.Length; i < iMax; i++)
|
for (int i = 0, iMax = _inc.Length; i < iMax; i++)
|
||||||
{
|
{
|
||||||
if (_inc[i] > 0 && (_entitiesComponentMasks[e][i] & _inc[i]) == 0) goto skip;
|
EcsMaskBit bit = _inc[i];
|
||||||
|
if ((_entitiesComponentMasks[e][bit.chankIndex] & bit.mask) == 0) goto skip;
|
||||||
}
|
}
|
||||||
for (int i = 0, iMax = _exc.Length; i < iMax; i++)
|
for (int i = 0, iMax = _exc.Length; i < iMax; i++)
|
||||||
{
|
{
|
||||||
if (_exc[i] > 0 && (_entitiesComponentMasks[e][i] & _exc[i]) != 0) goto skip;
|
EcsMaskBit bit = _exc[i];
|
||||||
|
if ((_entitiesComponentMasks[e][bit.chankIndex] & bit.mask) != 0) goto skip;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
skip: continue;
|
skip: continue;
|
||||||
|
@ -218,21 +218,22 @@ namespace DCFApixels.DragonECS
|
|||||||
|
|
||||||
public bool IsMatchesMask(EcsMask mask, int entityID)
|
public bool IsMatchesMask(EcsMask mask, int entityID)
|
||||||
{
|
{
|
||||||
#if (DEBUG && !DISABLE_DEBUG) || !DISABLE_DRAGONECS_ASSERT_CHEKS
|
throw new NotImplementedException();
|
||||||
if (mask.worldID != id)
|
//#if (DEBUG && !DISABLE_DEBUG) || !DISABLE_DRAGONECS_ASSERT_CHEKS
|
||||||
throw new EcsFrameworkException("The types of the target world of the mask and this world are different.");
|
// if (mask.worldID != id)
|
||||||
#endif
|
// throw new EcsFrameworkException("The types of the target world of the mask and this world are different.");
|
||||||
for (int i = 0, iMax = mask.incChunckMasks.Length; i < iMax; i++)
|
//#endif
|
||||||
{
|
// for (int i = 0, iMax = mask.incChunckMasks.Length; i < iMax; i++)
|
||||||
if (!_pools[mask.incChunckMasks[i]].Has(entityID))
|
// {
|
||||||
return false;
|
// if (!_pools[mask.incChunckMasks[i]].Has(entityID))
|
||||||
}
|
// return false;
|
||||||
for (int i = 0, iMax = mask.excChunckMasks.Length; i < iMax; i++)
|
// }
|
||||||
{
|
// for (int i = 0, iMax = mask.excChunckMasks.Length; i < iMax; i++)
|
||||||
if (_pools[mask.excChunckMasks[i]].Has(entityID))
|
// {
|
||||||
return false;
|
// if (_pools[mask.excChunckMasks[i]].Has(entityID))
|
||||||
}
|
// return false;
|
||||||
return true;
|
// }
|
||||||
|
// return true;
|
||||||
}
|
}
|
||||||
public void ReleaseDelEntityBuffer()
|
public void ReleaseDelEntityBuffer()
|
||||||
{
|
{
|
||||||
|
@ -13,5 +13,10 @@
|
|||||||
{
|
{
|
||||||
return new EcsMaskBit(id / 32, 1 << (id % 32));
|
return new EcsMaskBit(id / 32, 1 << (id % 32));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public override string ToString()
|
||||||
|
{
|
||||||
|
return $"bit({chankIndex}, {mask})";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user