mirror of
https://github.com/DCFApixels/DragonECS.git
synced 2025-09-17 17:34:36 +08:00
update masks
This commit is contained in:
parent
813b986e45
commit
0871b3d6af
@ -46,6 +46,8 @@ namespace DCFApixels.DragonECS
|
|||||||
/// <summary> Sorted </summary>
|
/// <summary> Sorted </summary>
|
||||||
internal readonly int[] _anys;
|
internal readonly int[] _anys;
|
||||||
|
|
||||||
|
internal readonly EcsMaskFlags _flags;
|
||||||
|
|
||||||
private EcsMaskIterator _iterator;
|
private EcsMaskIterator _iterator;
|
||||||
|
|
||||||
#region Properties
|
#region Properties
|
||||||
@ -66,14 +68,19 @@ namespace DCFApixels.DragonECS
|
|||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
get { return _excs; }
|
get { return _excs; }
|
||||||
}
|
}
|
||||||
|
public EcsMaskFlags Flags
|
||||||
|
{
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
get { return _flags; }
|
||||||
|
}
|
||||||
public bool IsEmpty
|
public bool IsEmpty
|
||||||
{
|
{
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
get { return _incs.Length == 0 && _excs.Length == 0; }
|
get { return _flags == EcsMaskFlags.Empty; }
|
||||||
}
|
}
|
||||||
public bool IsBroken
|
public bool IsBroken
|
||||||
{
|
{
|
||||||
get { return (_incs.Length & _excs.Length) == 1 && _incs[0] == _excs[0]; }
|
get { return (_flags & EcsMaskFlags.Broken) != 0; }
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
@ -81,20 +88,38 @@ namespace DCFApixels.DragonECS
|
|||||||
public static Builder New(EcsWorld world) { return new Builder(world); }
|
public static Builder New(EcsWorld world) { return new Builder(world); }
|
||||||
internal static EcsMask CreateEmpty(int id, short worldID)
|
internal static EcsMask CreateEmpty(int id, short worldID)
|
||||||
{
|
{
|
||||||
return new EcsMask(EcsStaticMask.Empty, id, worldID, new int[0], new int[0], new int[0]);
|
return new EcsMask(EcsStaticMask.Empty, id, worldID);
|
||||||
}
|
}
|
||||||
internal static EcsMask CreateBroken(int id, short worldID)
|
internal static EcsMask CreateBroken(int id, short worldID)
|
||||||
{
|
{
|
||||||
return new EcsMask(EcsStaticMask.Broken, id, worldID, new int[1] { 1 }, new int[1] { 1 }, new int[0]);
|
return new EcsMask(EcsStaticMask.Broken, id, worldID);
|
||||||
}
|
}
|
||||||
private EcsMask(EcsStaticMask staticMask, int id, short worldID, int[] incs, int[] excs, int[] anys)
|
private EcsMask(EcsStaticMask staticMask, int id, short worldID)
|
||||||
{
|
{
|
||||||
|
int[] ConvertTypeCodeToComponentTypeID(ReadOnlySpan<EcsTypeCode> from_, EcsWorld world_)
|
||||||
|
{
|
||||||
|
int[] to = new int[from_.Length];
|
||||||
|
for (int i = 0; i < to.Length; i++)
|
||||||
|
{
|
||||||
|
to[i] = world_.DeclareOrGetComponentTypeID(from_[i]);
|
||||||
|
}
|
||||||
|
Array.Sort(to);
|
||||||
|
return to;
|
||||||
|
}
|
||||||
|
|
||||||
_staticMask = staticMask;
|
_staticMask = staticMask;
|
||||||
ID = id;
|
ID = id;
|
||||||
|
WorldID = worldID;
|
||||||
|
_flags = staticMask.Flags;
|
||||||
|
|
||||||
|
EcsWorld world = EcsWorld.GetWorld(worldID);
|
||||||
|
int[] incs = ConvertTypeCodeToComponentTypeID(staticMask.IncTypeCodes, world);
|
||||||
|
int[] excs = ConvertTypeCodeToComponentTypeID(staticMask.ExcTypeCodes, world);
|
||||||
|
int[] anys = ConvertTypeCodeToComponentTypeID(staticMask.AnyTypeCodes, world);
|
||||||
|
|
||||||
_incs = incs;
|
_incs = incs;
|
||||||
_excs = excs;
|
_excs = excs;
|
||||||
_anys = anys;
|
_anys = anys;
|
||||||
WorldID = worldID;
|
|
||||||
|
|
||||||
_incChunckMasks = MakeMaskChuncsArray(incs);
|
_incChunckMasks = MakeMaskChuncsArray(incs);
|
||||||
_excChunckMasks = MakeMaskChuncsArray(excs);
|
_excChunckMasks = MakeMaskChuncsArray(excs);
|
||||||
@ -314,25 +339,11 @@ namespace DCFApixels.DragonECS
|
|||||||
|
|
||||||
internal EcsMask ConvertFromStatic(EcsStaticMask staticMask)
|
internal EcsMask ConvertFromStatic(EcsStaticMask staticMask)
|
||||||
{
|
{
|
||||||
int[] ConvertTypeCodeToComponentTypeID(ReadOnlySpan<EcsTypeCode> from, EcsWorld world)
|
|
||||||
{
|
|
||||||
int[] to = new int[from.Length];
|
|
||||||
for (int i = 0; i < to.Length; i++)
|
|
||||||
{
|
|
||||||
to[i] = world.DeclareOrGetComponentTypeID(from[i]);
|
|
||||||
}
|
|
||||||
Array.Sort(to);
|
|
||||||
return to;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (_staticMasks.TryGetValue(staticMask.ID, out EcsMask result) == false)
|
if (_staticMasks.TryGetValue(staticMask.ID, out EcsMask result) == false)
|
||||||
{
|
{
|
||||||
int[] incs = ConvertTypeCodeToComponentTypeID(staticMask.IncTypeCodes, _world);
|
result = new EcsMask(staticMask, _staticMasks.Count, _world.ID);
|
||||||
int[] excs = ConvertTypeCodeToComponentTypeID(staticMask.ExcTypeCodes, _world);
|
|
||||||
int[] anys = ConvertTypeCodeToComponentTypeID(staticMask.AnyTypeCodes, _world);
|
|
||||||
|
|
||||||
result = new EcsMask(staticMask, _staticMasks.Count, _world.ID, incs, excs, anys);
|
|
||||||
|
|
||||||
_staticMasks.Add(staticMask.ID, result);
|
_staticMasks.Add(staticMask.ID, result);
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
@ -368,14 +379,13 @@ namespace DCFApixels.DragonECS
|
|||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Debug utils
|
#region Debug utils
|
||||||
//TODO доработать дебаг с учетом Any
|
|
||||||
private static string CreateLogString(short worldID, int[] incs, int[] excs, int[] anys)
|
private static string CreateLogString(short worldID, int[] incs, int[] excs, int[] anys)
|
||||||
{
|
{
|
||||||
#if DEBUG
|
#if DEBUG
|
||||||
string converter(int o) { return EcsDebugUtility.GetGenericTypeName(EcsWorld.GetWorld(worldID).AllPools[o].ComponentType, 1); }
|
string converter(int o) { return EcsDebugUtility.GetGenericTypeName(EcsWorld.GetWorld(worldID).AllPools[o].ComponentType, 1); }
|
||||||
return $"Inc({string.Join(", ", incs.Select(converter))}) Exc({string.Join(", ", excs.Select(converter))}) Any({string.Join(", ", anys.Select(converter))})";
|
return $"Inc({string.Join(", ", incs.Select(converter))}); Exc({string.Join(", ", excs.Select(converter))}); Any({string.Join(", ", anys.Select(converter))})";
|
||||||
#else
|
#else
|
||||||
return $"Inc({string.Join(", ", inc)}) Exc({string.Join(", ", exc)})"; // Release optimization
|
return $"Inc({string.Join(", ", incs)}); Exc({string.Join(", ", excs)}; Any({string.Join(", ", anys)})"; // Release optimization
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -453,6 +463,20 @@ namespace DCFApixels.DragonECS
|
|||||||
#endregion
|
#endregion
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Flags]
|
||||||
|
public enum EcsMaskFlags : byte
|
||||||
|
{
|
||||||
|
Empty = 0,
|
||||||
|
Inc = 1 << 0,
|
||||||
|
Exc = 1 << 1,
|
||||||
|
Any = 1 << 2,
|
||||||
|
IncExc = Inc | Exc,
|
||||||
|
IncAny = Inc | Any,
|
||||||
|
ExcAny = Exc | Any,
|
||||||
|
IncExcAny = Inc | Exc | Any,
|
||||||
|
Broken = IncExcAny + 1,
|
||||||
|
}
|
||||||
|
|
||||||
#region EcsMaskChunck
|
#region EcsMaskChunck
|
||||||
[DebuggerTypeProxy(typeof(DebuggerProxy))]
|
[DebuggerTypeProxy(typeof(DebuggerProxy))]
|
||||||
public readonly struct EcsMaskChunck
|
public readonly struct EcsMaskChunck
|
||||||
|
@ -50,6 +50,8 @@ namespace DCFApixels.DragonECS
|
|||||||
/// <summary> Sorted </summary>
|
/// <summary> Sorted </summary>
|
||||||
private readonly EcsTypeCode[] _anys;
|
private readonly EcsTypeCode[] _anys;
|
||||||
|
|
||||||
|
private readonly EcsMaskFlags _flags;
|
||||||
|
|
||||||
#region Properties
|
#region Properties
|
||||||
/// <summary> Sorted set including constraints presented as global type codes. </summary>
|
/// <summary> Sorted set including constraints presented as global type codes. </summary>
|
||||||
public ReadOnlySpan<EcsTypeCode> IncTypeCodes
|
public ReadOnlySpan<EcsTypeCode> IncTypeCodes
|
||||||
@ -69,14 +71,19 @@ namespace DCFApixels.DragonECS
|
|||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
get { return _anys; }
|
get { return _anys; }
|
||||||
}
|
}
|
||||||
|
public EcsMaskFlags Flags
|
||||||
|
{
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
get { return _flags; }
|
||||||
|
}
|
||||||
public bool IsEmpty
|
public bool IsEmpty
|
||||||
{
|
{
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
get { return _incs.Length == 0 && _excs.Length == 0; }
|
get { return _flags == EcsMaskFlags.Empty; }
|
||||||
}
|
}
|
||||||
public bool IsBroken
|
public bool IsBroken
|
||||||
{
|
{
|
||||||
get { return (_incs.Length & _excs.Length) == 1 && _incs[0] == _excs[0]; }
|
get { return (_flags & EcsMaskFlags.Broken) != 0; }
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
@ -87,6 +94,13 @@ namespace DCFApixels.DragonECS
|
|||||||
_incs = key.Incs;
|
_incs = key.Incs;
|
||||||
_excs = key.Excs;
|
_excs = key.Excs;
|
||||||
_anys = key.Anys;
|
_anys = key.Anys;
|
||||||
|
if (_incs.Length > 0) { _flags |= EcsMaskFlags.Inc; }
|
||||||
|
if (_excs.Length > 0) { _flags |= EcsMaskFlags.Exc; }
|
||||||
|
if (_anys.Length > 0) { _flags |= EcsMaskFlags.Any; }
|
||||||
|
if ((_incs.Length & _excs.Length) == 1 && _incs[0] == _excs[0])
|
||||||
|
{
|
||||||
|
_flags = EcsMaskFlags.Broken;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
public static Builder New() { return Builder.New(); }
|
public static Builder New() { return Builder.New(); }
|
||||||
public static Builder Inc<T>() { return Builder.New().Inc<T>(); }
|
public static Builder Inc<T>() { return Builder.New().Inc<T>(); }
|
||||||
@ -107,7 +121,7 @@ namespace DCFApixels.DragonECS
|
|||||||
if (_ids.TryGetValue(key, out result) == false)
|
if (_ids.TryGetValue(key, out result) == false)
|
||||||
{
|
{
|
||||||
#if DEBUG
|
#if DEBUG
|
||||||
CheckConstraints(key.Incs, key.Excs); //TODO сделать прроверку для key.Anys
|
CheckConstraints(key.Incs, key.Excs, key.Anys);
|
||||||
#endif
|
#endif
|
||||||
result = new EcsStaticMask(_idDIspenser.UseFree(), key);
|
result = new EcsStaticMask(_idDIspenser.UseFree(), key);
|
||||||
_ids[key] = result;
|
_ids[key] = result;
|
||||||
@ -119,6 +133,7 @@ namespace DCFApixels.DragonECS
|
|||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Checks
|
#region Checks
|
||||||
|
//TODO доработать проверки с учетом Any
|
||||||
public bool IsSubmaskOf(EcsStaticMask otherMask)
|
public bool IsSubmaskOf(EcsStaticMask otherMask)
|
||||||
{
|
{
|
||||||
return IsSubmask(otherMask, this);
|
return IsSubmask(otherMask, this);
|
||||||
@ -129,11 +144,11 @@ namespace DCFApixels.DragonECS
|
|||||||
}
|
}
|
||||||
public bool IsConflictWith(EcsStaticMask otherMask)
|
public bool IsConflictWith(EcsStaticMask otherMask)
|
||||||
{
|
{
|
||||||
return OverlapsArray(_incs, otherMask._excs) || OverlapsArray(_excs, otherMask._incs);
|
return OverlapsArray(_incs, otherMask._excs) || OverlapsArray(_excs, otherMask._incs) || OverlapsArray(_anys, otherMask._excs) || OverlapsArray(_anys, otherMask._incs);
|
||||||
}
|
}
|
||||||
private static bool IsSubmask(EcsStaticMask super, EcsStaticMask sub)
|
private static bool IsSubmask(EcsStaticMask super, EcsStaticMask sub)
|
||||||
{
|
{
|
||||||
return IsSubarray(sub._incs, super._incs) && IsSuperarray(sub._excs, super._excs);
|
return IsSubarray(sub._incs, super._incs) && IsSuperarray(sub._excs, super._excs) && IsSubarray(sub._anys, super._anys);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static bool OverlapsArray(EcsTypeCode[] l, EcsTypeCode[] r)
|
private static bool OverlapsArray(EcsTypeCode[] l, EcsTypeCode[] r)
|
||||||
@ -544,11 +559,14 @@ namespace DCFApixels.DragonECS
|
|||||||
}
|
}
|
||||||
|
|
||||||
#if DEBUG
|
#if DEBUG
|
||||||
private static void CheckConstraints(EcsTypeCode[] incs, EcsTypeCode[] excs)
|
private static void CheckConstraints(EcsTypeCode[] incs, EcsTypeCode[] excs, EcsTypeCode[] anys)
|
||||||
{
|
{
|
||||||
if (CheckRepeats(incs)) { throw new ArgumentException("The values in the Include constraints are repeated."); }
|
if (CheckRepeats(incs)) { throw new ArgumentException("The values in the Include constraints are repeated."); }
|
||||||
if (CheckRepeats(excs)) { throw new ArgumentException("The values in the Exclude constraints are repeated."); }
|
if (CheckRepeats(excs)) { throw new ArgumentException("The values in the Exclude constraints are repeated."); }
|
||||||
|
if (CheckRepeats(anys)) { throw new ArgumentException("The values in the Any constraints are repeated."); }
|
||||||
if (OverlapsArray(incs, excs)) { throw new ArgumentException("Conflicting Include and Exclude constraints."); }
|
if (OverlapsArray(incs, excs)) { throw new ArgumentException("Conflicting Include and Exclude constraints."); }
|
||||||
|
if (OverlapsArray(incs, anys)) { throw new ArgumentException("Conflicting Include and Any constraints."); }
|
||||||
|
if (OverlapsArray(anys, excs)) { throw new ArgumentException("Conflicting Any and Exclude constraints."); }
|
||||||
}
|
}
|
||||||
private static bool CheckRepeats(EcsTypeCode[] array)
|
private static bool CheckRepeats(EcsTypeCode[] array)
|
||||||
{
|
{
|
||||||
|
@ -556,8 +556,6 @@ namespace DCFApixels.DragonECS
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//TODO оптимизировать
|
|
||||||
if (mask_._anys.Length != 0)
|
if (mask_._anys.Length != 0)
|
||||||
{
|
{
|
||||||
int count = 0;
|
int count = 0;
|
||||||
|
@ -103,6 +103,8 @@ namespace DCFApixels.DragonECS.Core
|
|||||||
protected abstract void OnDestroy();
|
protected abstract void OnDestroy();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//TODO добавить Any
|
||||||
public readonly unsafe struct WorldStateVersionsChecker : IDisposable
|
public readonly unsafe struct WorldStateVersionsChecker : IDisposable
|
||||||
{
|
{
|
||||||
private readonly EcsWorld _world;
|
private readonly EcsWorld _world;
|
||||||
|
Loading…
Reference in New Issue
Block a user