2023-04-21 03:16:05 +08:00
|
|
|
using System;
|
2023-05-07 00:50:02 +08:00
|
|
|
using System.Collections;
|
|
|
|
using System.Collections.Generic;
|
2023-11-15 17:47:11 +08:00
|
|
|
using System.Reflection;
|
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
|
|
|
{
|
2023-05-07 00:50:02 +08:00
|
|
|
private EcsWorld _source;
|
2023-06-25 23:13:51 +08:00
|
|
|
private int _componentID;
|
2023-05-07 00:50:02 +08:00
|
|
|
|
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
|
|
|
|
2023-05-07 00:50:02 +08:00
|
|
|
private T _fakeComponent;
|
|
|
|
|
2023-11-15 17:47:11 +08:00
|
|
|
|
|
|
|
private static bool _isInvalidType;
|
|
|
|
static EcsTagPool()
|
|
|
|
{
|
|
|
|
_isInvalidType = typeof(T).GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic).Length > 0;
|
|
|
|
}
|
|
|
|
public EcsTagPool()
|
|
|
|
{
|
|
|
|
if (_isInvalidType)
|
|
|
|
throw new EcsFrameworkException($"{typeof(T).Name} type must not contain any data.");
|
|
|
|
}
|
|
|
|
|
2023-04-21 03:16:05 +08:00
|
|
|
#region Properites
|
|
|
|
public int Count => _count;
|
2023-05-07 00:50:02 +08:00
|
|
|
int IEcsPool.Capacity => -1;
|
2023-06-25 23:13:51 +08:00
|
|
|
public int ComponentID => _componentID;
|
2023-05-07 00:50:02 +08:00
|
|
|
public Type ComponentType => typeof(T);
|
|
|
|
public EcsWorld World => _source;
|
2023-04-21 03:16:05 +08:00
|
|
|
#endregion
|
|
|
|
|
2023-05-07 00:50:02 +08:00
|
|
|
#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);
|
2023-05-07 00:50:02 +08:00
|
|
|
#endif
|
|
|
|
_count++;
|
|
|
|
_mapping[entityID] = true;
|
|
|
|
this.IncrementEntityComponentCount(entityID);
|
2023-05-23 15:58:31 +08:00
|
|
|
_listeners.InvokeOnAdd(entityID);
|
2023-05-07 00:50:02 +08:00
|
|
|
}
|
|
|
|
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;
|
2023-05-07 00:50:02 +08:00
|
|
|
this.IncrementEntityComponentCount(entityID);
|
2023-05-23 15:58:31 +08:00
|
|
|
_listeners.InvokeOnAdd(entityID);
|
2023-04-21 03:16:05 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2023-05-07 00:50:02 +08:00
|
|
|
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);
|
2023-05-07 00:50:02 +08:00
|
|
|
#endif
|
2023-04-21 14:21:24 +08:00
|
|
|
_mapping[entityID] = false;
|
2023-04-21 03:16:05 +08:00
|
|
|
_count--;
|
2023-05-07 00:50:02 +08:00
|
|
|
this.DecrementEntityComponentCount(entityID);
|
2023-05-23 15:58:31 +08:00
|
|
|
_listeners.InvokeOnDel(entityID);
|
2023-05-07 00:50:02 +08:00
|
|
|
}
|
|
|
|
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);
|
2023-05-07 00:50:02 +08:00
|
|
|
#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);
|
|
|
|
}
|
2023-05-07 00:50:02 +08:00
|
|
|
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
|
|
|
|
|
2023-05-07 00:50:02 +08:00
|
|
|
#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;
|
|
|
|
}
|
2023-05-07 00:50:02 +08:00
|
|
|
void IEcsPoolImplementation.OnWorldResize(int newSize)
|
2023-04-21 03:16:05 +08:00
|
|
|
{
|
|
|
|
Array.Resize(ref _mapping, newSize);
|
|
|
|
}
|
2023-05-07 00:50:02 +08:00
|
|
|
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)
|
2023-05-07 00:50:02 +08:00
|
|
|
{
|
|
|
|
Add(entityID);
|
|
|
|
return ref _fakeComponent;
|
|
|
|
}
|
2023-06-25 23:13:51 +08:00
|
|
|
ref readonly T IEcsStructPool<T>.Read(int entityID)
|
2023-05-07 00:50:02 +08:00
|
|
|
{
|
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);
|
2023-05-07 00:50:02 +08:00
|
|
|
#endif
|
|
|
|
return ref _fakeComponent;
|
|
|
|
}
|
2023-06-25 23:13:51 +08:00
|
|
|
ref T IEcsStructPool<T>.Get(int entityID)
|
2023-05-07 00:50:02 +08:00
|
|
|
{
|
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);
|
2023-05-07 00:50:02 +08:00
|
|
|
#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);
|
2023-05-07 00:50:02 +08:00
|
|
|
#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);
|
2023-05-07 00:50:02 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
#endregion
|
|
|
|
|
2023-05-23 15:58:31 +08:00
|
|
|
#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
|
|
|
|
|
2023-05-07 00:50:02 +08:00
|
|
|
#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
|
|
|
|
}
|
|
|
|
|
2023-05-07 00:50:02 +08:00
|
|
|
/// <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
|
|
|
{
|
2023-07-03 02:50:16 +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
|
|
|
{
|
2023-06-04 18:32:05 +08:00
|
|
|
return self.GetPool<EcsTagPool<TTagComponent>>();
|
2023-04-21 16:03:50 +08:00
|
|
|
}
|
2023-07-03 02:50:16 +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
|
|
|
|
2023-07-03 02:50:16 +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
|
|
|
{
|
2023-06-04 18:32:05 +08:00
|
|
|
return self.Include<EcsTagPool<TTagComponent>>();
|
2023-04-21 16:03:50 +08:00
|
|
|
}
|
2023-07-03 02:50:16 +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
|
|
|
{
|
2023-06-04 18:32:05 +08:00
|
|
|
return self.Exclude<EcsTagPool<TTagComponent>>();
|
2023-04-21 16:03:50 +08:00
|
|
|
}
|
2023-07-03 02:50:16 +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
|
|
|
{
|
2023-06-04 18:32:05 +08:00
|
|
|
return self.Optional<EcsTagPool<TTagComponent>>();
|
2023-04-21 16:03:50 +08:00
|
|
|
}
|
2023-04-21 03:16:05 +08:00
|
|
|
}
|
|
|
|
}
|