mirror of
https://github.com/DCFApixels/DragonECS.git
synced 2025-09-19 02:24:37 +08:00
refactoring
This commit is contained in:
parent
42a8be7345
commit
72b344b3dd
@ -10,45 +10,31 @@ namespace DCFApixels.DragonECS
|
|||||||
public byte r => color.r;
|
public byte r => color.r;
|
||||||
public byte g => color.g;
|
public byte g => color.g;
|
||||||
public byte b => color.b;
|
public byte b => color.b;
|
||||||
|
/// <summary>normalized R channel </summary>
|
||||||
public float rn => color.r / 255f;
|
public float rn => color.r / 255f;
|
||||||
|
/// <summary>normalized G channel </summary>
|
||||||
public float gn => color.g / 255f;
|
public float gn => color.g / 255f;
|
||||||
|
/// <summary>normalized B channel </summary>
|
||||||
public float bn => color.b / 255f;
|
public float bn => color.b / 255f;
|
||||||
|
public DebugColorAttribute(byte r, byte g, byte b) => color = new ColorRecord(r, g, b);
|
||||||
public DebugColorAttribute(byte r, byte g, byte b)
|
public DebugColorAttribute(DebugColor color) => this.color = new ColorRecord((int)color);
|
||||||
{
|
|
||||||
color = new ColorRecord(r, g, b);
|
|
||||||
}
|
|
||||||
public DebugColorAttribute(DebugColor color)
|
|
||||||
{
|
|
||||||
this.color = new ColorRecord((int)color);
|
|
||||||
}
|
|
||||||
|
|
||||||
[StructLayout(LayoutKind.Explicit, Pack = 1, Size = 4)]
|
[StructLayout(LayoutKind.Explicit, Pack = 1, Size = 4)]
|
||||||
private readonly struct ColorRecord // Union
|
private readonly struct ColorRecord // Union
|
||||||
{
|
{
|
||||||
[FieldOffset(0)]
|
[FieldOffset(0)] public readonly int full;
|
||||||
public readonly int full;
|
[FieldOffset(3)] public readonly byte r;
|
||||||
[FieldOffset(3)]
|
[FieldOffset(2)] public readonly byte g;
|
||||||
public readonly byte r;
|
[FieldOffset(1)] public readonly byte b;
|
||||||
[FieldOffset(2)]
|
|
||||||
public readonly byte g;
|
|
||||||
[FieldOffset(1)]
|
|
||||||
public readonly byte b;
|
|
||||||
|
|
||||||
public ColorRecord(byte r, byte g, byte b) : this()
|
public ColorRecord(byte r, byte g, byte b) : this()
|
||||||
{
|
{
|
||||||
this.r = r;
|
this.r = r;
|
||||||
this.g = g;
|
this.g = g;
|
||||||
this.b = b;
|
this.b = b;
|
||||||
}
|
}
|
||||||
public ColorRecord(int full) : this()
|
public ColorRecord(int full) : this() => this.full = full;
|
||||||
{
|
|
||||||
this.full = full;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public enum DebugColor
|
public enum DebugColor
|
||||||
{
|
{
|
||||||
/// <summary> Red. RGB is (255, 0, 0)</summary>
|
/// <summary> Red. RGB is (255, 0, 0)</summary>
|
||||||
|
@ -6,9 +6,6 @@ namespace DCFApixels.DragonECS
|
|||||||
public sealed class DebugDescriptionAttribute : Attribute
|
public sealed class DebugDescriptionAttribute : Attribute
|
||||||
{
|
{
|
||||||
public readonly string description;
|
public readonly string description;
|
||||||
public DebugDescriptionAttribute(string description)
|
public DebugDescriptionAttribute(string description) => this.description = description;
|
||||||
{
|
|
||||||
this.description = description;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -6,9 +6,6 @@ namespace DCFApixels.DragonECS
|
|||||||
public sealed class DebugNameAttribute : Attribute
|
public sealed class DebugNameAttribute : Attribute
|
||||||
{
|
{
|
||||||
public readonly string name;
|
public readonly string name;
|
||||||
public DebugNameAttribute(string name)
|
public DebugNameAttribute(string name) => this.name = name;
|
||||||
{
|
|
||||||
this.name = name;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using System.Diagnostics;
|
||||||
using System.Runtime.CompilerServices;
|
using System.Runtime.CompilerServices;
|
||||||
using System.Runtime.InteropServices;
|
using System.Runtime.InteropServices;
|
||||||
#if (DEBUG && !DISABLE_DEBUG) || !DISABLE_DRAGONECS_ASSERT_CHEKS
|
#if (DEBUG && !DISABLE_DEBUG) || !DISABLE_DRAGONECS_ASSERT_CHEKS
|
||||||
@ -8,6 +9,7 @@ using static DCFApixels.DragonECS.EcsGroup.ThrowHalper;
|
|||||||
namespace DCFApixels.DragonECS
|
namespace DCFApixels.DragonECS
|
||||||
{
|
{
|
||||||
[StructLayout(LayoutKind.Sequential, Pack = 0, Size = 8)]
|
[StructLayout(LayoutKind.Sequential, Pack = 0, Size = 8)]
|
||||||
|
[DebuggerTypeProxy(typeof(DebuggerProxy))]
|
||||||
public readonly ref struct EcsReadonlyGroup
|
public readonly ref struct EcsReadonlyGroup
|
||||||
{
|
{
|
||||||
private readonly EcsGroup _source;
|
private readonly EcsGroup _source;
|
||||||
@ -65,12 +67,7 @@ namespace DCFApixels.DragonECS
|
|||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Object
|
#region Object
|
||||||
public override string ToString()
|
public override string ToString() => _source != null ? _source.ToString() : "NULL";
|
||||||
{
|
|
||||||
if (_source != null)
|
|
||||||
return _source.ToString();
|
|
||||||
return "NULL";
|
|
||||||
}
|
|
||||||
public override int GetHashCode() => _source.GetHashCode();
|
public override int GetHashCode() => _source.GetHashCode();
|
||||||
public override bool Equals(object obj) => obj is EcsGroup group && group == this;
|
public override bool Equals(object obj) => obj is EcsGroup group && group == this;
|
||||||
public bool Equals(EcsReadonlyGroup other) => _source == other._source;
|
public bool Equals(EcsReadonlyGroup other) => _source == other._source;
|
||||||
@ -88,15 +85,23 @@ namespace DCFApixels.DragonECS
|
|||||||
public static bool operator !=(EcsReadonlyGroup a, EcsReadonlyGroup b) => !a.Equals(b);
|
public static bool operator !=(EcsReadonlyGroup a, EcsReadonlyGroup b) => !a.Equals(b);
|
||||||
public static bool operator !=(EcsReadonlyGroup a, EcsGroup b) => !a.Equals(b);
|
public static bool operator !=(EcsReadonlyGroup a, EcsGroup b) => !a.Equals(b);
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
#region DebuggerProxy
|
||||||
|
internal class DebuggerProxy : EcsGroup.DebuggerProxy
|
||||||
|
{
|
||||||
|
public DebuggerProxy(EcsReadonlyGroup group) : base(group._source) { }
|
||||||
|
}
|
||||||
|
#endregion
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[DebuggerTypeProxy(typeof(DebuggerProxy))]
|
||||||
public unsafe class EcsGroup : IDisposable, IEquatable<EcsGroup>
|
public unsafe class EcsGroup : IDisposable, IEquatable<EcsGroup>
|
||||||
{
|
{
|
||||||
private EcsWorld _source;
|
private EcsWorld _source;
|
||||||
private int[] _dense;
|
private int[] _dense;
|
||||||
private int[] _sparse;
|
private int[] _sparse;
|
||||||
private int _count;
|
private int _count;
|
||||||
private bool _isReleased = true;
|
internal bool _isReleased = true;
|
||||||
|
|
||||||
#region Properties
|
#region Properties
|
||||||
public EcsWorld World => _source;
|
public EcsWorld World => _source;
|
||||||
@ -391,10 +396,7 @@ namespace DCFApixels.DragonECS
|
|||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Object
|
#region Object
|
||||||
public override string ToString()
|
public override string ToString() => string.Join(", ", _dense.AsSpan(1, _count).ToArray());
|
||||||
{
|
|
||||||
return string.Join(", ", _dense.AsSpan(1, _count).ToArray());
|
|
||||||
}
|
|
||||||
public override bool Equals(object obj) => obj is EcsGroup group && Equals(group);
|
public override bool Equals(object obj) => obj is EcsGroup group && Equals(group);
|
||||||
public bool Equals(EcsReadonlyGroup other) => Equals(other.GetGroupInternal());
|
public bool Equals(EcsReadonlyGroup other) => Equals(other.GetGroupInternal());
|
||||||
public bool Equals(EcsGroup other)
|
public bool Equals(EcsGroup other)
|
||||||
@ -448,13 +450,9 @@ namespace DCFApixels.DragonECS
|
|||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region IDisposable/Release
|
#region IDisposable/Release
|
||||||
public void Dispose()
|
public void Dispose() => Release();
|
||||||
{
|
|
||||||
Release();
|
|
||||||
}
|
|
||||||
public void Release()
|
public void Release()
|
||||||
{
|
{
|
||||||
_isReleased = true;
|
|
||||||
_source.ReleaseGroup(this);
|
_source.ReleaseGroup(this);
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
@ -472,21 +470,30 @@ namespace DCFApixels.DragonECS
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
#endregion
|
#endregion
|
||||||
}
|
|
||||||
|
|
||||||
#region Extensions
|
#region DebuggerProxy
|
||||||
public static class EcsGroupExtensions
|
internal class DebuggerProxy
|
||||||
{
|
{
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
private EcsGroup _group;
|
||||||
public static void Normalize<T>(this EcsGroup self, ref T[] array)
|
public EcsWorld World => _group.World;
|
||||||
|
public bool IsReleased => _group.IsReleased;
|
||||||
|
public entlong[] Entities
|
||||||
{
|
{
|
||||||
if (array.Length < self.CapacityDense) Array.Resize(ref array, self.CapacityDense);
|
get
|
||||||
|
{
|
||||||
|
entlong[] result = new entlong[_group.Count];
|
||||||
|
int i = 0;
|
||||||
|
foreach (var e in _group)
|
||||||
|
result[i++] = _group.World.GetEntityLong(e);
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
||||||
public static void Normalize<T>(this EcsReadonlyGroup self, ref T[] array)
|
|
||||||
{
|
|
||||||
if (array.Length < self.CapacityDense) Array.Resize(ref array, self.CapacityDense);
|
|
||||||
}
|
}
|
||||||
|
public int Count => _group.Count;
|
||||||
|
public int CapacityDense => _group.CapacityDense;
|
||||||
|
public int CapacitySparce => _group.CapacitySparce;
|
||||||
|
|
||||||
|
public DebuggerProxy(EcsGroup group) => _group = group;
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
}
|
}
|
||||||
|
}
|
@ -322,15 +322,16 @@ namespace DCFApixels.DragonECS
|
|||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Groups
|
#region Groups Pool
|
||||||
internal void RegisterGroup(EcsGroup group)
|
internal void RegisterGroup(EcsGroup group)
|
||||||
{
|
{
|
||||||
_groups.Add(new WeakReference<EcsGroup>(group));
|
_groups.Add(new WeakReference<EcsGroup>(group));
|
||||||
}
|
}
|
||||||
internal EcsGroup GetGroupFromPool()
|
internal EcsGroup GetGroupFromPool()
|
||||||
{
|
{
|
||||||
if (_groupsPool.Count <= 0) return new EcsGroup(this);
|
EcsGroup result = _groupsPool.Count <= 0 ? new EcsGroup(this) : _groupsPool.Pop();
|
||||||
return _groupsPool.Pop();
|
result._isReleased = false;
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
internal void ReleaseGroup(EcsGroup group)
|
internal void ReleaseGroup(EcsGroup group)
|
||||||
{
|
{
|
||||||
@ -338,6 +339,7 @@ namespace DCFApixels.DragonECS
|
|||||||
if (group.World != this)
|
if (group.World != this)
|
||||||
throw new ArgumentException("groupFilter.WorldIndex != this");
|
throw new ArgumentException("groupFilter.WorldIndex != this");
|
||||||
#endif
|
#endif
|
||||||
|
group._isReleased = true;
|
||||||
group.Clear();
|
group.Clear();
|
||||||
_groupsPool.Push(group);
|
_groupsPool.Push(group);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user