update pools / update masks api/ remove obsolete

This commit is contained in:
Mikhail 2026-03-15 17:28:11 +08:00
parent 5e11f255e6
commit b6d3da46f7
8 changed files with 185 additions and 241 deletions

View File

@ -11,63 +11,84 @@ namespace DCFApixels.DragonECS.Core
void Init(ref T component, EcsWorld world);
void OnDestroy(ref T component, EcsWorld world);
}
public static class EcsWorldComponentHandler<T>
public static class EcsWorldComponent<T> where T : struct
{
public static readonly IEcsWorldComponent<T> instance;
public static readonly bool isHasHandler;
static EcsWorldComponentHandler()
public static readonly IEcsWorldComponent<T> CustomHandler;
public static readonly bool IsCustom;
static EcsWorldComponent()
{
T def = default;
if (def is IEcsWorldComponent<T> intrf)
T raw = default;
if (raw is IEcsWorldComponent<T> handler)
{
isHasHandler = true;
instance = intrf;
IsCustom = true;
CustomHandler = handler;
}
else
{
isHasHandler = false;
instance = new DummyHandler();
IsCustom = false;
CustomHandler = new DummyHandler();
}
}
private class DummyHandler : IEcsWorldComponent<T>
private sealed class DummyHandler : IEcsWorldComponent<T>
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Init(ref T component, EcsWorld world) { }
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void OnDestroy(ref T component, EcsWorld world) { }
}
}
#endregion
#region IEcsComponentReset
#region IEcsComponentLifecycle
public interface IEcsComponentLifecycle<T>
{
void Enable(ref T component);
void Disable(ref T component);
void OnAdd(ref T component, short worldID, int entityID);
void OnDel(ref T component, short worldID, int entityID);
}
public static class EcsComponentLifecycleHandler<T>
public static class EcsComponentLifecycle<T> where T : struct
{
public static readonly IEcsComponentLifecycle<T> instance;
public static readonly bool isHasHandler;
static EcsComponentLifecycleHandler()
public static readonly IEcsComponentLifecycle<T> CustomHandler;
public static readonly bool IsCustom;
static EcsComponentLifecycle()
{
T def = default;
if (def is IEcsComponentLifecycle<T> intrf)
T raw = default;
if (raw is IEcsComponentLifecycle<T> handler)
{
isHasHandler = true;
instance = intrf;
IsCustom = true;
CustomHandler = handler;
}
else
{
isHasHandler = false;
instance = new DummyHandler();
IsCustom = false;
CustomHandler = new DummyHandler();
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void OnAdd(bool isCustom, IEcsComponentLifecycle<T> custom, ref T component, short worldID, int entityID)
{
if (isCustom)
{
custom.OnAdd(ref component, worldID, entityID);
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void OnDel(bool isCustom, IEcsComponentLifecycle<T> custom, ref T component, short worldID, int entityID)
{
if (isCustom)
{
custom.OnDel(ref component, worldID, entityID);
}
else
{
component = default;
}
}
private sealed class DummyHandler : IEcsComponentLifecycle<T>
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Enable(ref T component) { component = default; }
public void OnAdd(ref T component, short worldID, int entityID) { component = default; }
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Disable(ref T component) { component = default; }
public void OnDel(ref T component, short worldID, int entityID) { component = default; }
}
}
#endregion
@ -77,22 +98,34 @@ namespace DCFApixels.DragonECS.Core
{
void Copy(ref T from, ref T to);
}
public static class EcsComponentCopyHandler<T>
public static class EcsComponentCopy<T> where T : struct
{
public static readonly IEcsComponentCopy<T> instance;
public static readonly bool isHasHandler;
static EcsComponentCopyHandler()
public static readonly IEcsComponentCopy<T> CustomHandler;
public static readonly bool IsCustom;
static EcsComponentCopy()
{
T def = default;
if (def is IEcsComponentCopy<T> intrf)
T raw = default;
if (raw is IEcsComponentCopy<T> handler)
{
isHasHandler = true;
instance = intrf;
IsCustom = true;
CustomHandler = handler;
}
else
{
isHasHandler = false;
instance = new DummyHandler();
IsCustom = false;
CustomHandler = new DummyHandler();
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Copy(bool isCustom, IEcsComponentCopy<T> custom, ref T from, ref T to)
{
if (isCustom)
{
custom.Copy(ref from, ref to);
}
else
{
to = from;
}
}
private sealed class DummyHandler : IEcsComponentCopy<T>
@ -102,4 +135,4 @@ namespace DCFApixels.DragonECS.Core
}
}
#endregion
}
}

View File

@ -285,7 +285,7 @@ namespace DCFApixels.DragonECS
public static TypeMeta GetTypeMeta(object obj)
{
if (obj == null) { return TypeMeta.NullTypeMeta; }
return TypeMeta.Get(GetTypeMetaSource(obj).GetType());
return TypeMeta.Get(Type.GetTypeHandle(GetTypeMetaSource(obj)));
}
public static TypeMeta GetTypeMeta<T>()
{
@ -295,6 +295,10 @@ namespace DCFApixels.DragonECS
{
return TypeMeta.Get(type);
}
public static TypeMeta GetTypeMeta(RuntimeTypeHandle typeHandle)
{
return TypeMeta.Get(typeHandle);
}
#endregion
#region TypeMetaProvider

View File

@ -46,7 +46,7 @@ namespace DCFApixels.DragonECS
public static readonly TypeMeta NullTypeMeta;
private static readonly object _lock = new object();
private static readonly Dictionary<Type, TypeMeta> _metaCache = new Dictionary<Type, TypeMeta>();
private static readonly Dictionary<RuntimeTypeHandle, TypeMeta> _metaCache = new Dictionary<RuntimeTypeHandle, TypeMeta>();
private static int _increment = 1;
private readonly int _uniqueID;
@ -93,16 +93,17 @@ namespace DCFApixels.DragonECS
_initFlags = InitFlag.All,
};
_metaCache.Add(typeof(void), NullTypeMeta);
_metaCache.Add(typeof(void).TypeHandle, NullTypeMeta);
}
public static TypeMeta Get(Type type)
public static TypeMeta Get(Type type) { return Get(type.TypeHandle); }
public static TypeMeta Get(RuntimeTypeHandle typeHandle)
{
lock (_lock) //TODO посмотреть можно ли тут убрать лок
{
if (_metaCache.TryGetValue(type, out TypeMeta result) == false)
if (_metaCache.TryGetValue(typeHandle, out TypeMeta result) == false)
{
result = new TypeMeta(type);
_metaCache.Add(type, result);
result = new TypeMeta(Type.GetTypeFromHandle(typeHandle));
_metaCache.Add(typeHandle, result);
}
return result;
}
@ -375,7 +376,7 @@ namespace DCFApixels.DragonECS
lock (_lock)
{
_metaCache.Clear();
_metaCache.Add(typeof(void), NullTypeMeta);
_metaCache.Add(typeof(void).TypeHandle, NullTypeMeta);
}
}
ITypeMeta ITypeMeta.BaseMeta
@ -395,7 +396,7 @@ namespace DCFApixels.DragonECS
{
if (IsHasCustomMeta(type))
{
meta = type.ToMeta();
meta = type.GetMeta();
return true;
}
meta = null;
@ -469,15 +470,6 @@ namespace DCFApixels.DragonECS
}
#endregion
#region Obsolete
[Obsolete("Use TryGetCustomMeta(type)")]
[EditorBrowsable(EditorBrowsableState.Never)]
public static bool IsHasMeta(Type type)
{
return IsHasCustomMeta(type);
}
#endregion
#region MetaGenerator
private static class MetaGenerator
{

View File

@ -34,6 +34,7 @@ namespace DCFApixels.DragonECS
{
public readonly int ID;
public readonly short WorldID;
public readonly EcsWorld World;
internal readonly EcsStaticMask _staticMask;
internal readonly EcsMaskChunck[] _incChunckMasks;
@ -51,11 +52,6 @@ namespace DCFApixels.DragonECS
private EcsMaskIterator _iterator;
#region Properties
public EcsWorld World
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get { return EcsWorld.GetWorld(WorldID); }
}
/// <summary> Sorted set excluding constraints. </summary>
public ReadOnlySpan<int> Incs
{
@ -74,6 +70,25 @@ namespace DCFApixels.DragonECS
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get { return _anys; }
}
/// <summary> Sorted set including constraints presented as global type codes. </summary>
public ReadOnlySpan<EcsTypeCode> IncTypeCodes
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get { return ToStatic().IncTypeCodes; }
}
/// <summary> Sorted set excluding constraints presented as global type codes. </summary>
public ReadOnlySpan<EcsTypeCode> ExcTypeCodes
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get { return ToStatic().ExcTypeCodes; }
}
/// <summary> Sorted set any constraints presented as global type codes. </summary>
public ReadOnlySpan<EcsTypeCode> AnyTypeCodes
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get { return ToStatic().AnyTypeCodes; }
}
public EcsMaskFlags Flags
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
@ -116,6 +131,7 @@ namespace DCFApixels.DragonECS
_staticMask = staticMask;
ID = id;
WorldID = worldID;
World = EcsWorld.GetWorld(worldID);
_flags = staticMask.Flags;
EcsWorld world = EcsWorld.GetWorld(worldID);
@ -345,8 +361,6 @@ namespace DCFApixels.DragonECS
internal EcsMask ConvertFromStatic(EcsStaticMask staticMask)
{
if (_staticMasks.TryGetValue(staticMask.ID, out EcsMask result) == false)
{
result = new EcsMask(staticMask, _staticMasks.Count, _world.ID);
@ -367,20 +381,42 @@ namespace DCFApixels.DragonECS
_world = world;
_builder = EcsStaticMask.New();
}
public Builder Inc() { return this; }
public Builder Exc() { return this; }
public Builder Any() { return this; }
public Builder Inc<T>() { _builder.Inc<T>(); return this; }
public Builder Exc<T>() { _builder.Exc<T>(); return this; }
public Builder Any<T>() { _builder.Any<T>(); return this; }
public Builder Inc(Type type) { _builder.Inc(type); return this; }
public Builder Exc(Type type) { _builder.Exc(type); return this; }
public Builder Any(Type type) { _builder.Any(type); return this; }
public Builder Inc(params Type[] types) { _builder.Inc(types); return this; }
public Builder Exc(params Type[] types) { _builder.Exc(types); return this; }
public Builder Any(params Type[] types) { _builder.Any(types); return this; }
public Builder Inc(ReadOnlySpan<Type> types) { _builder.Inc(types); return this; }
public Builder Exc(ReadOnlySpan<Type> types) { _builder.Exc(types); return this; }
public Builder Any(ReadOnlySpan<Type> types) { _builder.Any(types); return this; }
public Builder Inc(IEnumerable<Type> types) { _builder.Inc(types); return this; }
public Builder Exc(IEnumerable<Type> types) { _builder.Exc(types); return this; }
public Builder Any(IEnumerable<Type> types) { _builder.Any(types); return this; }
public Builder Inc(EcsTypeCode typeCode) { _builder.Inc(typeCode); return this; }
public Builder Exc(EcsTypeCode typeCode) { _builder.Exc(typeCode); return this; }
public Builder Any(EcsTypeCode typeCode) { _builder.Any(typeCode); return this; }
public Builder Inc(params EcsTypeCode[] typeCodes) { _builder.Inc(typeCodes); return this; }
public Builder Exc(params EcsTypeCode[] typeCodes) { _builder.Exc(typeCodes); return this; }
public Builder Any(params EcsTypeCode[] typeCodes) { _builder.Any(typeCodes); return this; }
public Builder Inc(ReadOnlySpan<EcsTypeCode> typeCodes) { _builder.Inc(typeCodes); return this; }
public Builder Exc(ReadOnlySpan<EcsTypeCode> typeCodes) { _builder.Exc(typeCodes); return this; }
public Builder Any(ReadOnlySpan<EcsTypeCode> typeCodes) { _builder.Any(typeCodes); return this; }
public Builder Inc(IEnumerable<EcsTypeCode> typeCodes) { _builder.Inc(typeCodes); return this; }
public Builder Exc(IEnumerable<EcsTypeCode> typeCodes) { _builder.Exc(typeCodes); return this; }
public Builder Any(IEnumerable<EcsTypeCode> typeCodes) { _builder.Any(typeCodes); return this; }
public Builder Combine(EcsMask mask) { _builder.Combine(mask._staticMask); return this; }
public Builder Except(EcsMask mask) { _builder.Except(mask._staticMask); return this; }
public EcsMask Build() { return _world.Get<WorldMaskComponent>().ConvertFromStatic(_builder.Build()); }
public static implicit operator EcsMask(Builder a) { return a.Build(); }
}
#endregion

View File

@ -65,7 +65,7 @@ namespace DCFApixels.DragonECS
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get { return _excs; }
}
/// <summary> Sorted set excluding constraints presented as global type codes. </summary>
/// <summary> Sorted set any constraints presented as global type codes. </summary>
public ReadOnlySpan<EcsTypeCode> AnyTypeCodes
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
@ -109,9 +109,15 @@ namespace DCFApixels.DragonECS
public static Builder Inc(Type type) { return Builder.New().Inc(type); }
public static Builder Exc(Type type) { return Builder.New().Exc(type); }
public static Builder Any(Type type) { return Builder.New().Any(type); }
public static Builder Inc(params Type[] types) { return Builder.New().Inc(types); }
public static Builder Exc(params Type[] types) { return Builder.New().Exc(types); }
public static Builder Any(params Type[] types) { return Builder.New().Any(types); }
public static Builder Inc(EcsTypeCode typeCode) { return Builder.New().Inc(typeCode); }
public static Builder Exc(EcsTypeCode typeCode) { return Builder.New().Exc(typeCode); }
public static Builder Any(EcsTypeCode typeCode) { return Builder.New().Any(typeCode); }
public static Builder Inc(params EcsTypeCode[] typeCodes) { return Builder.New().Inc(typeCodes); }
public static Builder Exc(params EcsTypeCode[] typeCodes) { return Builder.New().Exc(typeCodes); }
public static Builder Any(params EcsTypeCode[] typeCodes) { return Builder.New().Any(typeCodes); }
private static EcsStaticMask CreateMask(Key key)
{
if (_ids.TryGetValue(key, out EcsStaticMask result) == false)
@ -305,12 +311,24 @@ namespace DCFApixels.DragonECS
#endregion
#region Inc/Exc/Combine/Except
public Builder Inc() { return this; }
public Builder Exc() { return this; }
public Builder Any() { return this; }
public Builder Inc<T>() { return Inc(EcsTypeCodeManager.Get<T>()); }
public Builder Exc<T>() { return Exc(EcsTypeCodeManager.Get<T>()); }
public Builder Any<T>() { return Any(EcsTypeCodeManager.Get<T>()); }
public Builder Inc(Type type) { return Inc(EcsTypeCodeManager.Get(type)); }
public Builder Exc(Type type) { return Exc(EcsTypeCodeManager.Get(type)); }
public Builder Any(Type type) { return Any(EcsTypeCodeManager.Get(type)); }
public Builder Inc(params Type[] types) { foreach (var type in types) { Inc(type); } return this; }
public Builder Exc(params Type[] types) { foreach (var type in types) { Exc(type); } return this; }
public Builder Any(params Type[] types) { foreach (var type in types) { Any(type); } return this; }
public Builder Inc(ReadOnlySpan<Type> types) { foreach (var type in types) { Inc(type); } return this; }
public Builder Exc(ReadOnlySpan<Type> types) { foreach (var type in types) { Exc(type); } return this; }
public Builder Any(ReadOnlySpan<Type> types) { foreach (var type in types) { Any(type); } return this; }
public Builder Inc(IEnumerable<Type> types) { foreach (var type in types) { Inc(type); } return this; }
public Builder Exc(IEnumerable<Type> types) { foreach (var type in types) { Exc(type); } return this; }
public Builder Any(IEnumerable<Type> types) { foreach (var type in types) { Any(type); } return this; }
public Builder Inc(EcsTypeCode typeCode)
{
if (_version != _builder._version) { Throw.CantReuseBuilder(); }
@ -329,6 +347,15 @@ namespace DCFApixels.DragonECS
_builder.Any(typeCode);
return this;
}
public Builder Inc(params EcsTypeCode[] typeCodes) { foreach (var typeCode in typeCodes) { Inc(typeCode); } return this; }
public Builder Exc(params EcsTypeCode[] typeCodes) { foreach (var typeCode in typeCodes) { Exc(typeCode); } return this; }
public Builder Any(params EcsTypeCode[] typeCodes) { foreach (var typeCode in typeCodes) { Any(typeCode); } return this; }
public Builder Inc(ReadOnlySpan<EcsTypeCode> typeCodes) { foreach (var typeCode in typeCodes) { Inc(typeCode); } return this; }
public Builder Exc(ReadOnlySpan<EcsTypeCode> typeCodes) { foreach (var typeCode in typeCodes) { Exc(typeCode); } return this; }
public Builder Any(ReadOnlySpan<EcsTypeCode> typeCodes) { foreach (var typeCode in typeCodes) { Any(typeCode); } return this; }
public Builder Inc(IEnumerable<EcsTypeCode> typeCodes) { foreach (var typeCode in typeCodes) { Inc(typeCode); } return this; }
public Builder Exc(IEnumerable<EcsTypeCode> typeCodes) { foreach (var typeCode in typeCodes) { Exc(typeCode); } return this; }
public Builder Any(IEnumerable<EcsTypeCode> typeCodes) { foreach (var typeCode in typeCodes) { Any(typeCode); } return this; }
public Builder Combine(EcsStaticMask mask)
{
if (_version != _builder._version) { Throw.CantReuseBuilder(); }
@ -354,6 +381,7 @@ namespace DCFApixels.DragonECS
return result;
}
}
public static implicit operator EcsStaticMask(Builder a) { return a.Build(); }
public void Cancel()
{
if (_version != _builder._version) { Throw.CantReuseBuilder(); }

View File

@ -26,7 +26,6 @@ namespace DCFApixels.DragonECS
private static EcsWorld[] _worlds = Array.Empty<EcsWorld>();
private static readonly IdDispenser _worldIdDispenser = new IdDispenser(4, 0, n => Array.Resize(ref _worlds, n));
private static StructList<WorldComponentPoolAbstract> _allWorldComponentPools = new StructList<WorldComponentPoolAbstract>(64);
private static readonly object _worldLock = new object();
private StructList<WorldComponentPoolAbstract> _worldComponentPools;
@ -145,7 +144,7 @@ namespace DCFApixels.DragonECS
private static short _count;
private static short[] _recycledItems = new short[4];
private static short _recycledItemsCount;
private static readonly IEcsWorldComponent<T> _interface = EcsWorldComponentHandler<T>.instance;
private static readonly IEcsWorldComponent<T> _interface = EcsWorldComponent<T>.CustomHandler;
private static readonly Abstract _controller = new Abstract();
[MethodImpl(MethodImplOptions.AggressiveInlining)]
@ -321,27 +320,5 @@ namespace DCFApixels.DragonECS
}
}
#endregion
#region Obsolete
[EditorBrowsable(EditorBrowsableState.Never)]
[Obsolete("Use EcsWorld.ID")]
public short id
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get { return ID; }
}
[EditorBrowsable(EditorBrowsableState.Never)]
[Obsolete("The GetPoolInstance(int componentTypeID) method will be removed in future updates, use FindPoolInstance(Type componentType)")]
public IEcsPool GetPoolInstance(int componentTypeID)
{
return FindPoolInstance(componentTypeID);
}
[EditorBrowsable(EditorBrowsableState.Never)]
[Obsolete("The GetPoolInstance(Type componentType) method will be removed in future updates, use FindPoolInstance(Type componentType)")]
public IEcsPool GetPoolInstance(Type componentType)
{
return FindPoolInstance(componentType);
}
#endregion
}
}

View File

@ -35,7 +35,8 @@ namespace DCFApixels.DragonECS
public sealed class EcsPool<T> : IEcsPoolImplementation<T>, IEcsStructPool<T>, IEnumerable<T> //IEnumerable<T> - IntelliSense hack
where T : struct, IEcsComponent
{
private EcsWorld _source;
private short _worldID;
private EcsWorld _world;
private int _componentTypeID;
private EcsMaskChunck _maskBit;
@ -45,10 +46,10 @@ namespace DCFApixels.DragonECS
private int[] _recycledItems;
private int _recycledItemsCount = 0;
private readonly IEcsComponentLifecycle<T> _componentLifecycleHandler = EcsComponentLifecycleHandler<T>.instance;
private readonly bool _isHasComponentLifecycleHandler = EcsComponentLifecycleHandler<T>.isHasHandler;
private readonly IEcsComponentCopy<T> _componentCopyHandler = EcsComponentCopyHandler<T>.instance;
private readonly bool _isHasComponentCopyHandler = EcsComponentCopyHandler<T>.isHasHandler;
private readonly IEcsComponentLifecycle<T> _customLifecycle = EcsComponentLifecycle<T>.CustomHandler;
private readonly bool _isCustomLifecycle = EcsComponentLifecycle<T>.IsCustom;
private readonly IEcsComponentCopy<T> _customCopy = EcsComponentCopy<T>.CustomHandler;
private readonly bool _isCustomCopy = EcsComponentCopy<T>.IsCustom;
#if !DRAGONECS_DISABLE_POOLS_EVENTS
private StructList<IEcsPoolEventListener> _listeners = new StructList<IEcsPoolEventListener>(2);
@ -77,7 +78,7 @@ namespace DCFApixels.DragonECS
}
public EcsWorld World
{
get { return _source; }
get { return _world; }
}
public bool IsReadOnly
{
@ -104,7 +105,8 @@ namespace DCFApixels.DragonECS
}
void IEcsPoolImplementation.OnInit(EcsWorld world, EcsWorld.PoolsMediator mediator, int componentTypeID)
{
_source = world;
_world = world;
_worldID = world.ID;
_mediator = mediator;
_componentTypeID = componentTypeID;
_maskBit = EcsMaskChunck.FromID(componentTypeID);
@ -128,8 +130,8 @@ namespace DCFApixels.DragonECS
{
ref int itemIndex = ref _mapping[entityID];
#if DEBUG
if (entityID == EcsConsts.NULL_ENTITY_ID) { EcsPoolThrowHelper.ThrowEntityIsNotAlive(_source, entityID); }
if (_source.IsUsed(entityID) == false) { EcsPoolThrowHelper.ThrowEntityIsNotAlive(_source, entityID); }
if (entityID == EcsConsts.NULL_ENTITY_ID) { EcsPoolThrowHelper.ThrowEntityIsNotAlive(_world, entityID); }
if (_world.IsUsed(entityID) == false) { EcsPoolThrowHelper.ThrowEntityIsNotAlive(_world, entityID); }
if (itemIndex > 0) { EcsPoolThrowHelper.ThrowAlreadyHasComponent<T>(entityID); }
if (_isLocked) { EcsPoolThrowHelper.ThrowPoolLocked(); }
#elif DRAGONECS_STABILITY_MODE
@ -151,7 +153,7 @@ namespace DCFApixels.DragonECS
}
_mediator.RegisterComponent(entityID, _componentTypeID, _maskBit);
ref T result = ref _items[itemIndex];
EnableComponent(ref result);
EcsComponentLifecycle<T>.OnAdd( _isCustomLifecycle, _customLifecycle, ref result, _worldID, entityID);
#if !DRAGONECS_DISABLE_POOLS_EVENTS
_listeners.InvokeOnAddAndGet(entityID, _listenersCachedCount);
#endif
@ -179,7 +181,7 @@ namespace DCFApixels.DragonECS
public ref T TryAddOrGet(int entityID)
{
#if DEBUG
if (entityID == EcsConsts.NULL_ENTITY_ID) { EcsPoolThrowHelper.ThrowEntityIsNotAlive(_source, entityID); }
if (entityID == EcsConsts.NULL_ENTITY_ID) { EcsPoolThrowHelper.ThrowEntityIsNotAlive(_world, entityID); }
#endif
ref int itemIndex = ref _mapping[entityID];
if (itemIndex <= 0)
@ -203,7 +205,7 @@ namespace DCFApixels.DragonECS
}
}
_mediator.RegisterComponent(entityID, _componentTypeID, _maskBit);
EnableComponent(ref _items[itemIndex]);
EcsComponentLifecycle<T>.OnAdd( _isCustomLifecycle, _customLifecycle, ref _items[itemIndex], _worldID, entityID);
#if !DRAGONECS_DISABLE_POOLS_EVENTS
_listeners.InvokeOnAdd(entityID, _listenersCachedCount);
#endif
@ -222,14 +224,14 @@ namespace DCFApixels.DragonECS
{
ref int itemIndex = ref _mapping[entityID];
#if DEBUG
if (entityID == EcsConsts.NULL_ENTITY_ID) { EcsPoolThrowHelper.ThrowEntityIsNotAlive(_source, entityID); }
if (entityID == EcsConsts.NULL_ENTITY_ID) { EcsPoolThrowHelper.ThrowEntityIsNotAlive(_world, entityID); }
if (itemIndex <= 0) { EcsPoolThrowHelper.ThrowNotHaveComponent<T>(entityID); }
if (_isLocked) { EcsPoolThrowHelper.ThrowPoolLocked(); }
#elif DRAGONECS_STABILITY_MODE
if (itemIndex <= 0) { return; }
if (_isLocked) { return; }
#endif
DisableComponent(ref _items[itemIndex]);
EcsComponentLifecycle<T>.OnDel( _isCustomLifecycle, _customLifecycle, ref _items[itemIndex], _worldID, entityID);
if (_recycledItemsCount >= _recycledItems.Length)
{
Array.Resize(ref _recycledItems, ArrayUtility.NextPow2Safe(_recycledItemsCount));
@ -256,7 +258,7 @@ namespace DCFApixels.DragonECS
#elif DRAGONECS_STABILITY_MODE
if (!Has(fromEntityID)) { return; }
#endif
CopyComponent(ref Get(fromEntityID), ref TryAddOrGet(toEntityID));
EcsComponentCopy<T>.Copy(_isCustomCopy, _customCopy, ref Get(fromEntityID), ref TryAddOrGet(toEntityID));
}
public void Copy(int fromEntityID, EcsWorld toWorld, int toEntityID)
{
@ -265,7 +267,7 @@ namespace DCFApixels.DragonECS
#elif DRAGONECS_STABILITY_MODE
if (!Has(fromEntityID)) { return; }
#endif
CopyComponent(ref Get(fromEntityID), ref toWorld.GetPool<T>().TryAddOrGet(toEntityID));
EcsComponentCopy<T>.Copy(_isCustomCopy, _customCopy, ref Get(fromEntityID), ref toWorld.GetPool<T>().TryAddOrGet(toEntityID));
}
public void ClearAll()
@ -277,11 +279,11 @@ namespace DCFApixels.DragonECS
#endif
_recycledItemsCount = 0; // ñïåðåäè ÷òîáû îáíóëÿëîñü, òàê êàê Del íå îáíóëÿåò
if (_itemsCount <= 0) { return; }
var span = _source.Where(out SingleAspect<T> _);
var span = _world.Where(out SingleAspect<T> _);
foreach (var entityID in span)
{
ref int itemIndex = ref _mapping[entityID];
DisableComponent(ref _items[itemIndex]);
EcsComponentLifecycle<T>.OnDel(_isCustomLifecycle, _customLifecycle, ref _items[itemIndex], _worldID, entityID);
itemIndex = 0;
_mediator.UnregisterComponent(entityID, _componentTypeID, _maskBit);
#if !DRAGONECS_DISABLE_POOLS_EVENTS
@ -345,45 +347,6 @@ namespace DCFApixels.DragonECS
#endif
#endregion
#region Enable/Disable/Copy
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void EnableComponent(ref T component)
{
if (_isHasComponentLifecycleHandler)
{
_componentLifecycleHandler.Enable(ref component);
}
else
{
component = default;
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void DisableComponent(ref T component)
{
if (_isHasComponentLifecycleHandler)
{
_componentLifecycleHandler.Disable(ref component);
}
else
{
component = default;
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void CopyComponent(ref T from, ref T to)
{
if (_isHasComponentCopyHandler)
{
_componentCopyHandler.Copy(ref from, ref to);
}
else
{
to = from;
}
}
#endregion
#region IEnumerator - IntelliSense hack
IEnumerator<T> IEnumerable<T>.GetEnumerator() { throw new NotImplementedException(); }
IEnumerator IEnumerable.GetEnumerator() { throw new NotImplementedException(); }
@ -484,7 +447,6 @@ namespace DCFApixels.DragonECS
#endregion
}
public static class EcsPoolExtensions
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
@ -518,29 +480,5 @@ namespace DCFApixels.DragonECS
{
return self.AnyPool<EcsPool<TComponent>>();
}
#region Obsolete
[Obsolete("Use " + nameof(EcsAspect) + "." + nameof(EcsAspect.Builder) + "." + nameof(Inc) + "<T>()")]
[EditorBrowsable(EditorBrowsableState.Never)]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static EcsPool<TComponent> Include<TComponent>(this EcsAspect.Builder self) where TComponent : struct, IEcsComponent
{
return self.IncludePool<EcsPool<TComponent>>();
}
[Obsolete("Use " + nameof(EcsAspect) + "." + nameof(EcsAspect.Builder) + "." + nameof(Exc) + "<T>()")]
[EditorBrowsable(EditorBrowsableState.Never)]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static EcsPool<TComponent> Exclude<TComponent>(this EcsAspect.Builder self) where TComponent : struct, IEcsComponent
{
return self.ExcludePool<EcsPool<TComponent>>();
}
[Obsolete("Use " + nameof(EcsAspect) + "." + nameof(EcsAspect.Builder) + "." + nameof(Opt) + "<T>()")]
[EditorBrowsable(EditorBrowsableState.Never)]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static EcsPool<TComponent> Optional<TComponent>(this EcsAspect.Builder self) where TComponent : struct, IEcsComponent
{
return self.OptionalPool<EcsPool<TComponent>>();
}
#endregion
}
}
}

View File

@ -35,7 +35,7 @@ namespace DCFApixels.DragonECS
public sealed class EcsTagPool<T> : IEcsPoolImplementation<T>, IEcsStructPool<T>, IEnumerable<T> //IEnumerable<T> - IntelliSense hack
where T : struct, IEcsTagComponent
{
private EcsWorld _source;
private EcsWorld _world;
private int _componentTypeID;
private EcsMaskChunck _maskBit;
@ -78,7 +78,7 @@ namespace DCFApixels.DragonECS
}
public EcsWorld World
{
get { return _source; }
get { return _world; }
}
public bool IsReadOnly
{
@ -102,7 +102,7 @@ namespace DCFApixels.DragonECS
}
void IEcsPoolImplementation.OnInit(EcsWorld world, EcsWorld.PoolsMediator mediator, int componentTypeID)
{
_source = world;
_world = world;
_mediator = mediator;
_componentTypeID = componentTypeID;
_maskBit = EcsMaskChunck.FromID(componentTypeID);
@ -116,7 +116,7 @@ namespace DCFApixels.DragonECS
public void Add(int entityID)
{
#if DEBUG
if (_source.IsUsed(entityID) == false) { EcsPoolThrowHelper.ThrowEntityIsNotAlive(_source, entityID); }
if (_world.IsUsed(entityID) == false) { EcsPoolThrowHelper.ThrowEntityIsNotAlive(_world, entityID); }
if (Has(entityID)) { EcsPoolThrowHelper.ThrowAlreadyHasComponent<T>(entityID); }
if (_isLocked) { EcsPoolThrowHelper.ThrowPoolLocked(); }
#elif DRAGONECS_STABILITY_MODE
@ -216,7 +216,7 @@ namespace DCFApixels.DragonECS
if (_isLocked) { return; }
#endif
if (_count <= 0) { return; }
var span = _source.Where(out SingleTagAspect<T> _);
var span = _world.Where(out SingleTagAspect<T> _);
_count = 0;
foreach (var entityID in span)
{
@ -407,7 +407,6 @@ namespace DCFApixels.DragonECS
public static implicit operator ReadonlyEcsTagPool<T>(EcsWorld.GetPoolInstanceMarker a) { return a.GetInstance<EcsTagPool<T>>(); }
#endregion
}
public static class EcsTagPoolExtensions
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
@ -441,68 +440,5 @@ namespace DCFApixels.DragonECS
{
return self.OptionalPool<EcsTagPool<TTagComponent>>();
}
#region Obsolete
[Obsolete("Use " + nameof(EcsAspect) + "." + nameof(EcsAspect.Builder) + "." + nameof(Inc) + "<T>()")]
[EditorBrowsable(EditorBrowsableState.Never)]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static EcsTagPool<TTagComponent> Include<TTagComponent>(this EcsAspect.Builder self) where TTagComponent : struct, IEcsTagComponent
{
return self.IncludePool<EcsTagPool<TTagComponent>>();
}
[Obsolete("Use " + nameof(EcsAspect) + "." + nameof(EcsAspect.Builder) + "." + nameof(Exc) + "<T>()")]
[EditorBrowsable(EditorBrowsableState.Never)]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static EcsTagPool<TTagComponent> Exclude<TTagComponent>(this EcsAspect.Builder self) where TTagComponent : struct, IEcsTagComponent
{
return self.ExcludePool<EcsTagPool<TTagComponent>>();
}
[Obsolete("Use " + nameof(EcsAspect) + "." + nameof(EcsAspect.Builder) + "." + nameof(Opt) + "<T>()")]
[EditorBrowsable(EditorBrowsableState.Never)]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static EcsTagPool<TTagComponent> Optional<TTagComponent>(this EcsAspect.Builder self) where TTagComponent : struct, IEcsTagComponent
{
return self.OptionalPool<EcsTagPool<TTagComponent>>();
}
//---------------------------------------------------
[Obsolete("Use " + nameof(EcsWorld) + "." + nameof(GetPool) + "<T>()")]
[EditorBrowsable(EditorBrowsableState.Never)]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static EcsTagPool<TTagComponent> GetTagPool<TTagComponent>(this EcsWorld self) where TTagComponent : struct, IEcsTagComponent
{
return self.GetPoolInstance<EcsTagPool<TTagComponent>>();
}
[Obsolete("Use " + nameof(EcsWorld) + "." + nameof(GetPoolUnchecked) + "<T>()")]
[EditorBrowsable(EditorBrowsableState.Never)]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static EcsTagPool<TTagComponent> GetTagPoolUnchecked<TTagComponent>(this EcsWorld self) where TTagComponent : struct, IEcsTagComponent
{
return self.GetPoolInstanceUnchecked<EcsTagPool<TTagComponent>>();
}
[Obsolete("Use " + nameof(EcsAspect) + "." + nameof(EcsAspect.Builder) + "." + nameof(Inc) + "<T>()")]
[EditorBrowsable(EditorBrowsableState.Never)]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static EcsTagPool<TTagComponent> IncludeTag<TTagComponent>(this EcsAspect.Builder self) where TTagComponent : struct, IEcsTagComponent
{
return self.IncludePool<EcsTagPool<TTagComponent>>();
}
[Obsolete("Use " + nameof(EcsAspect) + "." + nameof(EcsAspect.Builder) + "." + nameof(Exc) + "<T>()")]
[EditorBrowsable(EditorBrowsableState.Never)]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static EcsTagPool<TTagComponent> ExcludeTag<TTagComponent>(this EcsAspect.Builder self) where TTagComponent : struct, IEcsTagComponent
{
return self.ExcludePool<EcsTagPool<TTagComponent>>();
}
[Obsolete("Use " + nameof(EcsAspect) + "." + nameof(EcsAspect.Builder) + "." + nameof(Opt) + "<T>()")]
[EditorBrowsable(EditorBrowsableState.Never)]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static EcsTagPool<TTagComponent> OptionalTag<TTagComponent>(this EcsAspect.Builder self) where TTagComponent : struct, IEcsTagComponent
{
return self.OptionalPool<EcsTagPool<TTagComponent>>();
}
#endregion
}
}
}