add DISABLE_POOLS_EVENTS define

This commit is contained in:
Mikhail 2024-05-01 16:58:54 +08:00
parent 22304f56c0
commit 935127219f
4 changed files with 37 additions and 1 deletions

View File

@ -22,7 +22,13 @@
public const int MAGIC_PRIME = 314159;
/// defs
public const bool DISABLE_POOLS_EVENTS =
#if DISABLE_POOLS_EVENTS
true;
#else
false;
#endif
public const bool ENABLE_DRAGONECS_DEBUGGER =
#if ENABLE_DRAGONECS_DEBUGGER
true;

View File

@ -37,8 +37,10 @@ namespace DCFApixels.DragonECS
private IEcsComponentCopy<T> _componentCopyHandler = EcsComponentCopyHandler<T>.instance;
private bool _isHasComponentCopyHandler = EcsComponentCopyHandler<T>.isHasHandler;
#if !DISABLE_POOLS_EVENTS
private List<IEcsPoolEventListener> _listeners = new List<IEcsPoolEventListener>();
private int _listenersCachedCount = 0;
#endif
private EcsWorld.PoolsMediator _mediator;
@ -91,7 +93,9 @@ namespace DCFApixels.DragonECS
}
_mediator.RegisterComponent(entityID, _componentTypeID, _maskBit);
EnableComponent(ref _items[itemIndex]);
#if !DISABLE_POOLS_EVENTS
_listeners.InvokeOnAddAndGet(entityID, _listenersCachedCount);
#endif
return ref _items[itemIndex];
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
@ -100,7 +104,9 @@ namespace DCFApixels.DragonECS
#if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS
if (!Has(entityID)) { EcsPoolThrowHalper.ThrowNotHaveComponent<T>(entityID); }
#endif
#if !DISABLE_POOLS_EVENTS
_listeners.InvokeOnGet(entityID, _listenersCachedCount);
#endif
return ref _items[_mapping[entityID]];
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
@ -131,9 +137,13 @@ namespace DCFApixels.DragonECS
}
_mediator.RegisterComponent(entityID, _componentTypeID, _maskBit);
EnableComponent(ref _items[itemIndex]);
#if !DISABLE_POOLS_EVENTS
_listeners.InvokeOnAdd(entityID, _listenersCachedCount);
#endif
}
#if !DISABLE_POOLS_EVENTS
_listeners.InvokeOnGet(entityID, _listenersCachedCount);
#endif
return ref _items[itemIndex];
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
@ -156,7 +166,9 @@ namespace DCFApixels.DragonECS
itemIndex = 0;
_itemsCount--;
_mediator.UnregisterComponent(entityID, _componentTypeID, _maskBit);
#if !DISABLE_POOLS_EVENTS
_listeners.InvokeOnDel(entityID, _listenersCachedCount);
#endif
}
public void TryDel(int entityID)
{
@ -191,7 +203,9 @@ namespace DCFApixels.DragonECS
DisableComponent(ref _items[itemIndex]);
itemIndex = 0;
_mediator.UnregisterComponent(entityID, _componentTypeID, _maskBit);
#if !DISABLE_POOLS_EVENTS
_listeners.InvokeOnDel(entityID, _listenersCachedCount);
#endif
}
}
#endregion
@ -239,6 +253,7 @@ namespace DCFApixels.DragonECS
#endregion
#region Listeners
#if !DISABLE_POOLS_EVENTS
public void AddListener(IEcsPoolEventListener listener)
{
if (listener == null) { EcsPoolThrowHalper.ThrowNullListener(); }
@ -253,6 +268,7 @@ namespace DCFApixels.DragonECS
_listenersCachedCount--;
}
}
#endif
#endregion
#region Enable/Disable/Copy

View File

@ -156,10 +156,12 @@ namespace DCFApixels.DragonECS
void Copy(int fromEntityID, EcsWorld toWorld, int toEntityID);
#endregion
#if !DISABLE_POOLS_EVENTS
#region Add/Remove Listeners
void AddListener(IEcsPoolEventListener listener);
void RemoveListener(IEcsPoolEventListener listener);
#endregion
#endif
}
public interface IEcsPool : IEcsReadonlyPool
{
@ -218,6 +220,7 @@ namespace DCFApixels.DragonECS
/// <summary>Called after deleting an entity from the pool</summary>
void OnDel(int entityID);
}
#if !DISABLE_POOLS_EVENTS
public static class PoolEventListExtensions
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
@ -268,5 +271,6 @@ namespace DCFApixels.DragonECS
for (int i = 0; i < cachedCount; i++) self[i].OnDel(entityID);
}
}
#endif
#endregion
}

View File

@ -29,8 +29,10 @@ namespace DCFApixels.DragonECS
private bool[] _mapping;// index = entityID / value = itemIndex;/ value = 0 = no entityID
private int _count = 0;
#if !DISABLE_POOLS_EVENTS
private List<IEcsPoolEventListener> _listeners = new List<IEcsPoolEventListener>();
private int _listenersCachedCount = 0;
#endif
private T _fakeComponent;
private EcsWorld.PoolsMediator _mediator;
@ -86,7 +88,9 @@ namespace DCFApixels.DragonECS
_count++;
_mapping[entityID] = true;
_mediator.RegisterComponent(entityID, _componentTypeID, _maskBit);
#if !DISABLE_POOLS_EVENTS
_listeners.InvokeOnAdd(entityID, _listenersCachedCount);
#endif
}
public void TryAdd(int entityID)
{
@ -108,7 +112,9 @@ namespace DCFApixels.DragonECS
_mapping[entityID] = false;
_count--;
_mediator.UnregisterComponent(entityID, _componentTypeID, _maskBit);
#if !DISABLE_POOLS_EVENTS
_listeners.InvokeOnDel(entityID, _listenersCachedCount);
#endif
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void TryDel(int entityID)
@ -166,7 +172,9 @@ namespace DCFApixels.DragonECS
{
_mapping[entityID] = false;
_mediator.UnregisterComponent(entityID, _componentTypeID, _maskBit);
#if !DISABLE_POOLS_EVENTS
_listeners.InvokeOnDel(entityID, _listenersCachedCount);
#endif
}
}
#endregion
@ -237,6 +245,7 @@ namespace DCFApixels.DragonECS
#endregion
#region Listeners
#if !DISABLE_POOLS_EVENTS
public void AddListener(IEcsPoolEventListener listener)
{
if (listener == null) { EcsPoolThrowHalper.ThrowNullListener(); }
@ -251,6 +260,7 @@ namespace DCFApixels.DragonECS
_listenersCachedCount--;
}
}
#endif
#endregion
#region IEnumerator - IntelliSense hack