mirror of
https://github.com/DCFApixels/DragonECS.git
synced 2025-09-18 01:44:35 +08:00
refactoring
This commit is contained in:
parent
ed01cfc454
commit
28e786ca7a
@ -1,5 +1,5 @@
|
|||||||
namespace DCFApixels.DragonECS
|
namespace DCFApixels.DragonECS
|
||||||
{
|
{
|
||||||
public sealed class EcsDefaultWorld : EcsWorld<EcsDefaultWorld> { }
|
public sealed class EcsDefaultWorld : EcsWorld { }
|
||||||
public sealed class EcsEventWorld : EcsWorld<EcsEventWorld> { }
|
public sealed class EcsEventWorld : EcsWorld { }
|
||||||
}
|
}
|
||||||
|
@ -144,7 +144,7 @@ namespace DCFApixels.DragonECS
|
|||||||
var exc = maskExc.ToArray();
|
var exc = maskExc.ToArray();
|
||||||
Array.Sort(exc);
|
Array.Sort(exc);
|
||||||
|
|
||||||
mask = new EcsMask(_world.Archetype, inc, exc);
|
mask = new EcsMask(_world.WorldTypeID, inc, exc);
|
||||||
_world = null;
|
_world = null;
|
||||||
_inc = null;
|
_inc = null;
|
||||||
_exc = null;
|
_exc = null;
|
||||||
@ -202,24 +202,24 @@ namespace DCFApixels.DragonECS
|
|||||||
[DebuggerTypeProxy(typeof(DebuggerProxy))]
|
[DebuggerTypeProxy(typeof(DebuggerProxy))]
|
||||||
public sealed class EcsMask
|
public sealed class EcsMask
|
||||||
{
|
{
|
||||||
internal readonly Type _worldType;
|
internal readonly int _worldTypeID;
|
||||||
/// <summary>Including constraints</summary>
|
/// <summary>Including constraints</summary>
|
||||||
internal readonly int[] _inc;
|
internal readonly int[] _inc;
|
||||||
/// <summary>Excluding constraints</summary>
|
/// <summary>Excluding constraints</summary>
|
||||||
internal readonly int[] _exc;
|
internal readonly int[] _exc;
|
||||||
internal EcsMask(Type worldType, int[] inc, int[] exc)
|
internal EcsMask(int worldTypeID, int[] inc, int[] exc)
|
||||||
{
|
{
|
||||||
#if DEBUG
|
#if DEBUG
|
||||||
if (worldType is null) throw new ArgumentNullException();
|
if (worldTypeID == 0) throw new ArgumentException();
|
||||||
CheckConstraints(inc, exc);
|
CheckConstraints(inc, exc);
|
||||||
#endif
|
#endif
|
||||||
_worldType = worldType;
|
_worldTypeID = worldTypeID;
|
||||||
_inc = inc;
|
_inc = inc;
|
||||||
_exc = exc;
|
_exc = exc;
|
||||||
}
|
}
|
||||||
|
|
||||||
#region Object
|
#region Object
|
||||||
public override string ToString() => CreateLogString(_worldType, _inc, _exc);
|
public override string ToString() => CreateLogString(_worldTypeID, _inc, _exc);
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Debug utils
|
#region Debug utils
|
||||||
@ -247,11 +247,10 @@ namespace DCFApixels.DragonECS
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
private static string CreateLogString(Type worldType, int[] inc, int[] exc)
|
private static string CreateLogString(int worldTypeID, int[] inc, int[] exc)
|
||||||
{
|
{
|
||||||
#if (DEBUG && !DISABLE_DEBUG)
|
#if (DEBUG && !DISABLE_DEBUG)
|
||||||
int worldID = WorldMetaStorage.GetWorldID(worldType);
|
string converter(int o) => EcsDebugUtility.GetGenericTypeName(WorldMetaStorage.GetComponentType(worldTypeID, o), 1);
|
||||||
string converter(int o) => EcsDebugUtility.GetGenericTypeName(WorldMetaStorage.GetComponentType(worldID, o), 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
|
||||||
@ -260,21 +259,22 @@ namespace DCFApixels.DragonECS
|
|||||||
internal class DebuggerProxy
|
internal class DebuggerProxy
|
||||||
{
|
{
|
||||||
public readonly Type worldType;
|
public readonly Type worldType;
|
||||||
|
public readonly int worldTypeID;
|
||||||
public readonly int[] included;
|
public readonly int[] included;
|
||||||
public readonly int[] excluded;
|
public readonly int[] 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)
|
||||||
{
|
{
|
||||||
worldType = mask._worldType;
|
worldType = WorldMetaStorage.GetWorldType(mask._worldTypeID);
|
||||||
int worldID = WorldMetaStorage.GetWorldID(worldType);
|
worldTypeID = mask._worldTypeID;
|
||||||
included = mask._inc;
|
included = mask._inc;
|
||||||
excluded = mask._exc;
|
excluded = mask._exc;
|
||||||
Type converter(int o) => WorldMetaStorage.GetComponentType(worldID, o);
|
Type converter(int o) => WorldMetaStorage.GetComponentType(worldTypeID, 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(worldType, included, excluded);
|
public override string ToString() => CreateLogString(worldTypeID, included, excluded);
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
@ -18,6 +18,7 @@ namespace DCFApixels.DragonECS
|
|||||||
|
|
||||||
public readonly short id;
|
public readonly short id;
|
||||||
|
|
||||||
|
private Type _worldType;
|
||||||
private int _worldTypeID;
|
private int _worldTypeID;
|
||||||
|
|
||||||
private IntDispenser _entityDispenser;
|
private IntDispenser _entityDispenser;
|
||||||
@ -45,7 +46,7 @@ namespace DCFApixels.DragonECS
|
|||||||
private object[] _components = new object[2];
|
private object[] _components = new object[2];
|
||||||
|
|
||||||
#region Properties
|
#region Properties
|
||||||
public abstract Type Archetype { get; }
|
public int WorldTypeID => _worldTypeID;
|
||||||
public int Count => _entitiesCount;
|
public int Count => _entitiesCount;
|
||||||
public int Capacity => _entitesCapacity; //_denseEntities.Length;
|
public int Capacity => _entitesCapacity; //_denseEntities.Length;
|
||||||
public EcsReadonlyGroup Entities => _allEntites.Readonly;
|
public EcsReadonlyGroup Entities => _allEntites.Readonly;
|
||||||
@ -67,7 +68,8 @@ namespace DCFApixels.DragonECS
|
|||||||
Worlds[id] = this;
|
Worlds[id] = this;
|
||||||
}
|
}
|
||||||
|
|
||||||
_worldTypeID = WorldMetaStorage.GetWorldID(Archetype);
|
_worldType = this.GetType();
|
||||||
|
_worldTypeID = WorldMetaStorage.GetWorldID(_worldType);
|
||||||
|
|
||||||
_entityDispenser = new IntDispenser(0);
|
_entityDispenser = new IntDispenser(0);
|
||||||
_pools = new IEcsPoolImplementation[512];
|
_pools = new IEcsPoolImplementation[512];
|
||||||
@ -274,7 +276,7 @@ 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
|
#if (DEBUG && !DISABLE_DEBUG) || !DISABLE_DRAGONECS_ASSERT_CHEKS
|
||||||
if (mask._worldType != Archetype)
|
if (mask._worldTypeID != _worldTypeID)
|
||||||
throw new EcsFrameworkException("The types of the target world of the mask and this world are different.");
|
throw new EcsFrameworkException("The types of the target world of the mask and this world are different.");
|
||||||
#endif
|
#endif
|
||||||
for (int i = 0, iMax = mask._inc.Length; i < iMax; i++)
|
for (int i = 0, iMax = mask._inc.Length; i < iMax; i++)
|
||||||
@ -405,18 +407,7 @@ namespace DCFApixels.DragonECS
|
|||||||
#endregion
|
#endregion
|
||||||
}
|
}
|
||||||
|
|
||||||
internal sealed class EcsNullWorld : EcsWorld<EcsNullWorld>
|
internal sealed class EcsNullWorld : EcsWorld { }
|
||||||
{
|
|
||||||
public EcsNullWorld() : base(false) { }
|
|
||||||
}
|
|
||||||
|
|
||||||
public abstract class EcsWorld<TWorldArchetype> : EcsWorld
|
|
||||||
where TWorldArchetype : EcsWorld<TWorldArchetype>
|
|
||||||
{
|
|
||||||
public override Type Archetype => typeof(TWorldArchetype);
|
|
||||||
public EcsWorld() : base() { }
|
|
||||||
internal EcsWorld(bool isIndexable) : base(isIndexable) { }
|
|
||||||
}
|
|
||||||
|
|
||||||
#region Callbacks Interface
|
#region Callbacks Interface
|
||||||
public interface IEcsWorldComponent
|
public interface IEcsWorldComponent
|
||||||
|
@ -16,9 +16,9 @@ namespace DCFApixels.DragonECS
|
|||||||
{
|
{
|
||||||
public static int id = GetWorldID(typeof(TWorldArchetype));
|
public static int id = GetWorldID(typeof(TWorldArchetype));
|
||||||
}
|
}
|
||||||
private static int GetToken()
|
private static int GetToken(Type worldType)
|
||||||
{
|
{
|
||||||
WorldTypeMeta meta = new WorldTypeMeta();
|
WorldTypeMeta meta = new WorldTypeMeta(worldType);
|
||||||
meta.id = _tokenCount;
|
meta.id = _tokenCount;
|
||||||
Array.Resize(ref _metas, ++_tokenCount);
|
Array.Resize(ref _metas, ++_tokenCount);
|
||||||
_metas[_tokenCount - 1] = meta;
|
_metas[_tokenCount - 1] = meta;
|
||||||
@ -28,16 +28,18 @@ namespace DCFApixels.DragonECS
|
|||||||
return _tokenCount - 1;
|
return _tokenCount - 1;
|
||||||
}
|
}
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
public static int GetWorldID(Type archetype)
|
public static int GetWorldID(Type worldType)
|
||||||
{
|
{
|
||||||
if (!_worldIds.TryGetValue(archetype, out int id))
|
if (!_worldIds.TryGetValue(worldType, out int id))
|
||||||
{
|
{
|
||||||
id = GetToken();
|
id = GetToken(worldType);
|
||||||
_worldIds.Add(archetype, id);
|
_worldIds.Add(worldType, id);
|
||||||
}
|
}
|
||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public static Type GetWorldType(int worldTypeID) => _metas[worldTypeID].worldType;
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
public static int GetWorldID<TWorldArchetype>() => WorldIndex<TWorldArchetype>.id;
|
public static int GetWorldID<TWorldArchetype>() => WorldIndex<TWorldArchetype>.id;
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
public static int GetComponentID<T>(int worldID) => Component<T>.Get(worldID);
|
public static int GetComponentID<T>(int worldID) => Component<T>.Get(worldID);
|
||||||
@ -255,6 +257,7 @@ namespace DCFApixels.DragonECS
|
|||||||
public struct XXX : IEcsComponent { }
|
public struct XXX : IEcsComponent { }
|
||||||
private class WorldTypeMeta
|
private class WorldTypeMeta
|
||||||
{
|
{
|
||||||
|
public readonly Type worldType;
|
||||||
public int id;
|
public int id;
|
||||||
public int componentCount;
|
public int componentCount;
|
||||||
public int subjectsCount;
|
public int subjectsCount;
|
||||||
@ -276,10 +279,11 @@ namespace DCFApixels.DragonECS
|
|||||||
{
|
{
|
||||||
return PoolComponentIdArrays.GetComponentID(type, id);
|
return PoolComponentIdArrays.GetComponentID(type, id);
|
||||||
}
|
}
|
||||||
public WorldTypeMeta()
|
public WorldTypeMeta(Type worldType)
|
||||||
{
|
{
|
||||||
_types = new Type[10];
|
_types = new Type[10];
|
||||||
_declaredComponentTypes = new Dictionary<Type, int>();
|
_declaredComponentTypes = new Dictionary<Type, int>();
|
||||||
|
this.worldType = worldType;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user