DragonECS/src/Pools/EcsTagPool.cs

214 lines
7.5 KiB
C#
Raw Normal View History

2023-04-21 03:16:05 +08:00
using System;
using System.Collections;
using System.Collections.Generic;
2023-04-21 03:16:05 +08:00
using System.Runtime.CompilerServices;
namespace DCFApixels.DragonECS
{
2023-06-25 23:13:51 +08:00
public sealed class EcsTagPool<T> : IEcsPoolImplementation<T>, IEcsStructPool<T>, IEnumerable<T> //IEnumerable<T> - IntelliSense hack
2023-04-21 14:21:24 +08:00
where T : struct, IEcsTagComponent
2023-04-21 03:16:05 +08:00
{
private EcsWorld _source;
2023-06-25 23:13:51 +08:00
private int _componentID;
2023-04-21 14:21:24 +08:00
private bool[] _mapping;// index = entityID / value = itemIndex;/ value = 0 = no entityID
2023-04-21 03:16:05 +08:00
private int _count;
2023-06-25 23:13:51 +08:00
private List<IEcsPoolEventListener> _listeners = new List<IEcsPoolEventListener>();
2023-04-21 03:16:05 +08:00
private T _fakeComponent;
2023-04-21 03:16:05 +08:00
#region Properites
public int Count => _count;
int IEcsPool.Capacity => -1;
2023-06-25 23:13:51 +08:00
public int ComponentID => _componentID;
public Type ComponentType => typeof(T);
public EcsWorld World => _source;
2023-04-21 03:16:05 +08:00
#endregion
#region Method
2023-04-21 03:16:05 +08:00
public void Add(int entityID)
{
2023-06-02 01:20:46 +08:00
#if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS
2023-06-26 02:53:55 +08:00
if (Has(entityID)) EcsPoolThrowHalper.ThrowAlreadyHasComponent<T>(entityID);
#endif
_count++;
_mapping[entityID] = true;
this.IncrementEntityComponentCount(entityID);
_listeners.InvokeOnAdd(entityID);
}
public void TryAdd(int entityID)
{
if (!_mapping[entityID])
2023-04-21 03:16:05 +08:00
{
2023-04-21 14:21:24 +08:00
_count++;
_mapping[entityID] = true;
this.IncrementEntityComponentCount(entityID);
_listeners.InvokeOnAdd(entityID);
2023-04-21 03:16:05 +08:00
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool Has(int entityID)
2023-04-21 03:16:05 +08:00
{
2023-04-21 14:21:24 +08:00
return _mapping[entityID];
2023-04-21 03:16:05 +08:00
}
public void Del(int entityID)
{
2023-06-02 01:20:46 +08:00
#if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS
2023-06-26 02:53:55 +08:00
if (!Has(entityID)) EcsPoolThrowHalper.ThrowNotHaveComponent<T>(entityID);
#endif
2023-04-21 14:21:24 +08:00
_mapping[entityID] = false;
2023-04-21 03:16:05 +08:00
_count--;
this.DecrementEntityComponentCount(entityID);
_listeners.InvokeOnDel(entityID);
}
public void TryDel(int entityID)
{
if (Has(entityID)) Del(entityID);
}
public void Copy(int fromEntityID, int toEntityID)
{
2023-06-02 01:20:46 +08:00
#if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS
2023-06-26 02:53:55 +08:00
if (!Has(fromEntityID)) EcsPoolThrowHalper.ThrowNotHaveComponent<T>(fromEntityID);
#endif
TryAdd(toEntityID);
}
2023-05-23 01:47:28 +08:00
public void Copy(int fromEntityID, EcsWorld toWorld, int toEntityID)
{
2023-06-02 01:20:46 +08:00
#if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS
2023-06-26 02:53:55 +08:00
if (!Has(fromEntityID)) EcsPoolThrowHalper.ThrowNotHaveComponent<T>(fromEntityID);
2023-05-23 01:47:28 +08:00
#endif
toWorld.GetPool<T>().TryAdd(toEntityID);
}
public void Set(int entityID, bool isHas)
{
if (isHas)
{
if (!Has(entityID))
Add(entityID);
}
else
{
if (Has(entityID))
Del(entityID);
}
2023-04-21 03:16:05 +08:00
}
2023-05-23 01:47:28 +08:00
public void Toggle(int entityID)
{
if (Has(entityID))
Del(entityID);
else
Add(entityID);
}
2023-04-21 03:16:05 +08:00
#endregion
#region Callbacks
2023-11-08 15:15:10 +08:00
void IEcsPoolImplementation.OnInit(EcsWorld world, int componentID)
{
_source = world;
_componentID = componentID;
_mapping = new bool[world.Capacity];
_count = 0;
}
void IEcsPoolImplementation.OnWorldResize(int newSize)
2023-04-21 03:16:05 +08:00
{
Array.Resize(ref _mapping, newSize);
}
void IEcsPoolImplementation.OnWorldDestroy() { }
void IEcsPoolImplementation.OnReleaseDelEntityBuffer(ReadOnlySpan<int> buffer)
{
foreach (var entityID in buffer)
TryDel(entityID);
}
#endregion
#region Other
2023-06-25 23:13:51 +08:00
ref T IEcsStructPool<T>.Add(int entityID)
{
Add(entityID);
return ref _fakeComponent;
}
2023-06-25 23:13:51 +08:00
ref readonly T IEcsStructPool<T>.Read(int entityID)
{
2023-06-02 01:20:46 +08:00
#if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS
2023-06-26 02:53:55 +08:00
if (!Has(entityID)) EcsPoolThrowHalper.ThrowNotHaveComponent<T>(entityID);
#endif
return ref _fakeComponent;
}
2023-06-25 23:13:51 +08:00
ref T IEcsStructPool<T>.Get(int entityID)
{
2023-06-02 01:20:46 +08:00
#if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS
2023-06-26 02:53:55 +08:00
if (!Has(entityID)) EcsPoolThrowHalper.ThrowNotHaveComponent<T>(entityID);
#endif
return ref _fakeComponent;
}
void IEcsPool.AddRaw(int entityID, object dataRaw) => Add(entityID);
object IEcsPool.GetRaw(int entityID)
{
2023-06-02 01:20:46 +08:00
#if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS
2023-06-26 02:53:55 +08:00
if (!Has(entityID)) EcsPoolThrowHalper.ThrowNotHaveComponent<T>(entityID);
#endif
return _fakeComponent;
}
void IEcsPool.SetRaw(int entityID, object dataRaw)
{
2023-06-02 01:20:46 +08:00
#if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS
2023-06-26 02:53:55 +08:00
if (!Has(entityID)) EcsPoolThrowHalper.ThrowNotHaveComponent<T>(entityID);
#endif
}
#endregion
#region Listeners
public void AddListener(IEcsPoolEventListener listener)
{
if (listener == null) { throw new ArgumentNullException("listener is null"); }
_listeners.Add(listener);
}
public void RemoveListener(IEcsPoolEventListener listener)
{
if (listener == null) { throw new ArgumentNullException("listener is null"); }
_listeners.Remove(listener);
}
#endregion
#region IEnumerator - IntelliSense hack
IEnumerator<T> IEnumerable<T>.GetEnumerator() => throw new NotImplementedException();
IEnumerator IEnumerable.GetEnumerator() => throw new NotImplementedException();
2023-04-21 03:16:05 +08:00
#endregion
}
/// <summary>Component without data</summary>
2023-04-21 03:46:51 +08:00
public interface IEcsTagComponent { }
public static class EcsTagPoolExt
2023-04-21 03:16:05 +08:00
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2023-04-21 16:03:50 +08:00
public static EcsTagPool<TTagComponent> GetPool<TTagComponent>(this EcsWorld self) where TTagComponent : struct, IEcsTagComponent
2023-04-21 03:16:05 +08:00
{
return self.GetPool<EcsTagPool<TTagComponent>>();
2023-04-21 16:03:50 +08:00
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static EcsTagPool<TTagComponent> UncheckedGetPool<TTagComponent>(this EcsWorld self) where TTagComponent : struct, IEcsTagComponent
{
return self.UncheckedGetPool<EcsTagPool<TTagComponent>>();
}
2023-04-21 16:03:50 +08:00
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2023-06-22 14:31:13 +08:00
public static EcsTagPool<TTagComponent> Include<TTagComponent>(this EcsAspectBuilderBase self) where TTagComponent : struct, IEcsTagComponent
2023-04-21 16:03:50 +08:00
{
return self.Include<EcsTagPool<TTagComponent>>();
2023-04-21 16:03:50 +08:00
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2023-06-22 14:31:13 +08:00
public static EcsTagPool<TTagComponent> Exclude<TTagComponent>(this EcsAspectBuilderBase self) where TTagComponent : struct, IEcsTagComponent
2023-04-21 16:03:50 +08:00
{
return self.Exclude<EcsTagPool<TTagComponent>>();
2023-04-21 16:03:50 +08:00
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2023-06-22 14:31:13 +08:00
public static EcsTagPool<TTagComponent> Optional<TTagComponent>(this EcsAspectBuilderBase self) where TTagComponent : struct, IEcsTagComponent
2023-04-21 16:03:50 +08:00
{
return self.Optional<EcsTagPool<TTagComponent>>();
2023-04-21 16:03:50 +08:00
}
2023-04-21 03:16:05 +08:00
}
}