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

@ -508,7 +508,7 @@ namespace DCFApixels.DragonECS
#region Inverse
public void Inverse()
{
if(_count == 0)
if (_count == 0)
{
foreach (var entityID in _source.Entities)
{

View File

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

View File

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

View File

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