refacotring

This commit is contained in:
Mikhail 2024-03-02 21:45:09 +08:00
parent e326ea4b9c
commit e84a26af02
4 changed files with 66 additions and 63 deletions

View File

@ -10,7 +10,7 @@ namespace DCFApixels.DragonECS
{ {
internal EcsWorld _source; internal EcsWorld _source;
internal EcsMask _mask; internal EcsMask _mask;
private bool _isInit; private bool _isInit = false;
private UnsafeArray<int> _sortIncBuffer; private UnsafeArray<int> _sortIncBuffer;
private UnsafeArray<int> _sortExcBuffer; private UnsafeArray<int> _sortExcBuffer;
@ -18,9 +18,18 @@ namespace DCFApixels.DragonECS
private UnsafeArray<EcsMaskChunck> _sortExcChunckBuffer; private UnsafeArray<EcsMaskChunck> _sortExcChunckBuffer;
#region Properties #region Properties
public EcsMask Mask => _mask; public EcsMask Mask
public EcsWorld World => _source; {
public bool IsInit => _isInit; get { return _mask; }
}
public EcsWorld World
{
get { return _source; }
}
public bool IsInit
{
get { return _isInit; }
}
#endregion #endregion
#region Methods #region Methods
@ -148,7 +157,7 @@ namespace DCFApixels.DragonECS
} }
#endregion #endregion
#region Destructor #region Finalizator
unsafe ~EcsAspect() unsafe ~EcsAspect()
{ {
_sortIncBuffer.Dispose(); _sortIncBuffer.Dispose();
@ -200,34 +209,31 @@ namespace DCFApixels.DragonECS
group.Clear(); group.Clear();
var enumerator = GetEnumerator(); var enumerator = GetEnumerator();
while (enumerator.MoveNext()) while (enumerator.MoveNext())
{
group.Add_Internal(enumerator.Current); group.Add_Internal(enumerator.Current);
} }
}
public int CopyTo(ref int[] array) public int CopyTo(ref int[] array)
{ {
var enumerator = GetEnumerator();
int count = 0; int count = 0;
var enumerator = GetEnumerator();
while (enumerator.MoveNext()) while (enumerator.MoveNext())
{ {
if (array.Length <= count) if (array.Length <= count)
{
Array.Resize(ref array, array.Length << 1); Array.Resize(ref array, array.Length << 1);
}
array[count++] = enumerator.Current; array[count++] = enumerator.Current;
} }
return count; return count;
} }
public EcsSpan CopyToSpan(ref int[] array) public EcsSpan CopyToSpan(ref int[] array)
{ {
var enumerator = GetEnumerator(); int count = CopyTo(ref array);
int count = 0;
while (enumerator.MoveNext())
{
if (array.Length <= count)
Array.Resize(ref array, array.Length << 1);
array[count++] = enumerator.Current;
}
return new EcsSpan(worldID, array, count); return new EcsSpan(worldID, array, count);
} }
#region object #region Other
public override string ToString() public override string ToString()
{ {
List<int> ints = new List<int>(); List<int> ints = new List<int>();
@ -241,7 +247,7 @@ namespace DCFApixels.DragonECS
#region Enumerator #region Enumerator
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public Enumerator GetEnumerator() => new Enumerator(_span, aspect); public Enumerator GetEnumerator() { return new Enumerator(_span, aspect); }
public unsafe ref struct Enumerator public unsafe ref struct Enumerator
{ {
@ -282,11 +288,9 @@ namespace DCFApixels.DragonECS
private UnsafeArray<EcsMaskChunck> _sortExcChunckBuffer; private UnsafeArray<EcsMaskChunck> _sortExcChunckBuffer;
private readonly int _entityComponentMaskLength; private readonly int _entityComponentMaskLength;
//private EcsAspect aspect;
public unsafe Enumerator(EcsSpan span, EcsAspect aspect) public unsafe Enumerator(EcsSpan span, EcsAspect aspect)
{ {
//this.aspect = aspect;
_span = span.GetEnumerator(); _span = span.GetEnumerator();
_entityComponentMasks = aspect.World._entityComponentMasks; _entityComponentMasks = aspect.World._entityComponentMasks;
_sortIncChunckBuffer = aspect._sortIncChunckBuffer; _sortIncChunckBuffer = aspect._sortIncChunckBuffer;
@ -368,13 +372,8 @@ namespace DCFApixels.DragonECS
public int Current public int Current
{ {
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
get => _span.Current; get { return _span.Current; }
} }
//public entlong CurrentLong
//{
// [MethodImpl(MethodImplOptions.AggressiveInlining)]
// get => aspect.World.GetEntityLong(_span.Current);
//}
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool MoveNext() public bool MoveNext()
{ {
@ -385,14 +384,18 @@ namespace DCFApixels.DragonECS
{ {
var bit = _sortIncChunckBuffer.ptr[i]; var bit = _sortIncChunckBuffer.ptr[i];
if ((_entityComponentMasks[e * _entityComponentMaskLength + bit.chankIndex] & bit.mask) != bit.mask) if ((_entityComponentMasks[e * _entityComponentMaskLength + bit.chankIndex] & bit.mask) != bit.mask)
{
goto skip; goto skip;
} }
}
for (int i = 0; i < _sortExcChunckBuffer.Length; i++) for (int i = 0; i < _sortExcChunckBuffer.Length; i++)
{ {
var bit = _sortExcChunckBuffer.ptr[i]; var bit = _sortExcChunckBuffer.ptr[i];
if ((_entityComponentMasks[e * _entityComponentMaskLength + bit.chankIndex] & bit.mask) > 0) if ((_entityComponentMasks[e * _entityComponentMaskLength + bit.chankIndex] & bit.mask) > 0)
{
goto skip; goto skip;
} }
}
return true; return true;
skip: continue; skip: continue;
} }

View File

@ -18,13 +18,28 @@ namespace DCFApixels.DragonECS
internal readonly int[] exc; internal readonly int[] exc;
#region Properties #region Properties
public int ID => id; public int ID
public int WorldID => worldID; {
public EcsWorld World => EcsWorld.GetWorld(worldID); get { return id; }
}
public int WorldID
{
get { return worldID; }
}
public EcsWorld World
{
get { return EcsWorld.GetWorld(worldID); }
}
/// <summary>Including constraints</summary> /// <summary>Including constraints</summary>
public ReadOnlySpan<int> Inc => inc; public ReadOnlySpan<int> Inc
{
get { return inc; }
}
/// <summary>Excluding constraints</summary> /// <summary>Excluding constraints</summary>
public ReadOnlySpan<int> Exc => exc; public ReadOnlySpan<int> Exc
{
get { return exc; }
}
#endregion #endregion
#region Constructors #region Constructors
@ -118,30 +133,10 @@ namespace DCFApixels.DragonECS
} }
private static bool IsSubarray(int[] super, int[] sub) private static bool IsSubarray(int[] super, int[] sub)
{ {
//if (super.Length > sub.Length)
//{
// return false;
//}
//for (int superI = 0, subI = 0;
// subI < sub.Length;
// superI++, subI++)
//{
// while (super[superI] < sub[subI])
// {
// superI++;
// }
// if (super[superI] != sub[subI])
// {
// return false;
// }
//}
//return true;
if (super.Length < sub.Length) if (super.Length < sub.Length)
{ {
return false; return false;
} }
int superI = 0; int superI = 0;
int subI = 0; int subI = 0;
@ -153,7 +148,6 @@ namespace DCFApixels.DragonECS
} }
subI++; subI++;
} }
return subI == sub.Length; return subI == sub.Length;
} }
@ -163,7 +157,6 @@ namespace DCFApixels.DragonECS
{ {
return false; return false;
} }
int superI = 0; int superI = 0;
int subI = 0; int subI = 0;
@ -175,13 +168,15 @@ namespace DCFApixels.DragonECS
} }
superI++; superI++;
} }
return subI == sub.Length; return subI == sub.Length;
} }
#endregion #endregion
#region Object #region Object
public override string ToString() => CreateLogString(worldID, inc, exc); public override string ToString()
{
return CreateLogString(worldID, inc, exc);
}
public bool Equals(EcsMask mask) public bool Equals(EcsMask mask)
{ {
return id == mask.id && worldID == mask.worldID; return id == mask.id && worldID == mask.worldID;
@ -203,11 +198,11 @@ namespace DCFApixels.DragonECS
{ {
lock (_dummyHashSet) lock (_dummyHashSet)
{ {
if (CheckRepeats(inc)) throw new EcsFrameworkException("The values in the Include constraints are repeated."); if (CheckRepeats(inc)) { throw new EcsFrameworkException("The values in the Include constraints are repeated."); }
if (CheckRepeats(exc)) throw new EcsFrameworkException("The values in the Exclude constraints are repeated."); if (CheckRepeats(exc)) { throw new EcsFrameworkException("The values in the Exclude constraints are repeated."); }
_dummyHashSet.Clear(); _dummyHashSet.Clear();
_dummyHashSet.UnionWith(inc); _dummyHashSet.UnionWith(inc);
if (_dummyHashSet.Overlaps(exc)) throw new EcsFrameworkException("Conflicting Include and Exclude constraints."); if (_dummyHashSet.Overlaps(exc)) { throw new EcsFrameworkException("Conflicting Include and Exclude constraints."); }
} }
} }
private bool CheckRepeats(int[] array) private bool CheckRepeats(int[] array)
@ -215,7 +210,10 @@ namespace DCFApixels.DragonECS
_dummyHashSet.Clear(); _dummyHashSet.Clear();
foreach (var item in array) foreach (var item in array)
{ {
if (_dummyHashSet.Contains(item)) return true; if (_dummyHashSet.Contains(item))
{
return true;
}
_dummyHashSet.Add(item); _dummyHashSet.Add(item);
} }
return false; return false;
@ -224,7 +222,7 @@ namespace DCFApixels.DragonECS
private static string CreateLogString(int worldID, int[] inc, int[] exc) private static string CreateLogString(int worldID, int[] inc, int[] 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) { return 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))})";
#else #else
return $"Inc({string.Join(", ", inc)}) Exc({string.Join(", ", exc)})"; // Release optimization return $"Inc({string.Join(", ", inc)}) Exc({string.Join(", ", exc)})"; // Release optimization
@ -252,11 +250,14 @@ namespace DCFApixels.DragonECS
excludedChunkMasks = mask.excChunckMasks; excludedChunkMasks = mask.excChunckMasks;
included = mask.inc; included = mask.inc;
excluded = mask.exc; excluded = mask.exc;
Type converter(int o) => world.GetComponentType(o); Type converter(int o) { return world.GetComponentType(o); }
includedTypes = included.Select(converter).ToArray(); includedTypes = included.Select(converter).ToArray();
excludedTypes = excluded.Select(converter).ToArray(); excludedTypes = excluded.Select(converter).ToArray();
} }
public override string ToString() => CreateLogString(_worldID, included, excluded); public override string ToString()
{
return CreateLogString(_worldID, included, excluded);
}
} }
#endregion #endregion

View File

@ -1,5 +1,4 @@
using DCFApixels.DragonECS.Internal; using System;
using System;
using System.Collections.Generic; using System.Collections.Generic;
namespace DCFApixels.DragonECS namespace DCFApixels.DragonECS