2023-02-05 19:59:45 +08:00
|
|
|
using System.Collections;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Runtime.CompilerServices;
|
2023-02-08 17:57:06 +08:00
|
|
|
using System;
|
2023-02-09 02:26:43 +08:00
|
|
|
using System.Reflection;
|
|
|
|
using System.Linq;
|
2023-02-05 19:59:45 +08:00
|
|
|
|
|
|
|
namespace DCFApixels.DragonECS
|
|
|
|
{
|
2023-02-08 17:07:39 +08:00
|
|
|
public interface IEcsPool
|
|
|
|
{
|
2023-03-02 14:42:44 +08:00
|
|
|
public IEcsWorld World { get; }
|
2023-02-08 17:07:39 +08:00
|
|
|
public int ID { get; }
|
|
|
|
public bool Has(int index);
|
2023-02-27 18:16:23 +08:00
|
|
|
public void Write(int index);
|
2023-02-08 17:07:39 +08:00
|
|
|
public void Del(int index);
|
|
|
|
}
|
|
|
|
|
2023-03-02 14:42:44 +08:00
|
|
|
public interface IEcsPool<T> : IEcsPool
|
|
|
|
where T : struct
|
|
|
|
{
|
|
|
|
public ref readonly T Read(int entity);
|
|
|
|
public new ref T Write(int entity);
|
|
|
|
}
|
2023-03-13 04:32:24 +08:00
|
|
|
|
|
|
|
public class EcsNullPool : IEcsPool
|
|
|
|
{
|
|
|
|
private readonly IEcsWorld _source;
|
|
|
|
|
|
|
|
public EcsNullPool(IEcsWorld source)
|
|
|
|
{
|
|
|
|
_source = source;
|
|
|
|
}
|
|
|
|
|
|
|
|
public IEcsWorld World => _source;
|
|
|
|
public int ID => -1;
|
|
|
|
public void Del(int index) { }
|
|
|
|
public bool Has(int index) => false;
|
|
|
|
public void Write(int index) { }
|
|
|
|
}
|
|
|
|
|
2023-03-02 14:42:44 +08:00
|
|
|
public class EcsPool<T> : IEcsPool<T>
|
2023-02-09 02:26:43 +08:00
|
|
|
where T : struct
|
2023-02-05 19:59:45 +08:00
|
|
|
{
|
2023-02-13 21:11:54 +08:00
|
|
|
private readonly int _id;
|
2023-03-02 14:42:44 +08:00
|
|
|
private readonly IEcsWorld _source;
|
2023-02-07 17:11:56 +08:00
|
|
|
private readonly SparseSet _sparseSet;
|
2023-02-05 19:59:45 +08:00
|
|
|
private T[] _denseItems;
|
|
|
|
|
2023-02-07 17:11:56 +08:00
|
|
|
#region Properites
|
2023-03-02 14:42:44 +08:00
|
|
|
public IEcsWorld World => _source;
|
2023-02-13 21:11:54 +08:00
|
|
|
public int ID => _id;
|
2023-02-07 17:11:56 +08:00
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region Constructors
|
2023-03-13 04:32:24 +08:00
|
|
|
public EcsPool(IEcsWorld source, int id, int capacity)
|
2023-02-06 01:27:32 +08:00
|
|
|
{
|
2023-02-07 17:11:56 +08:00
|
|
|
_source = source;
|
2023-03-13 04:32:24 +08:00
|
|
|
_id = id;
|
2023-03-11 17:11:40 +08:00
|
|
|
_sparseSet = new SparseSet(capacity, capacity);
|
2023-02-09 02:26:43 +08:00
|
|
|
|
2023-02-13 21:11:54 +08:00
|
|
|
_denseItems =new T[capacity];
|
2023-02-06 01:27:32 +08:00
|
|
|
}
|
2023-02-07 17:11:56 +08:00
|
|
|
#endregion
|
2023-02-06 01:27:32 +08:00
|
|
|
|
2023-02-13 21:11:54 +08:00
|
|
|
#region Read/Write/Has/Del
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2023-03-02 14:42:44 +08:00
|
|
|
public ref readonly T Read(int entity)
|
2023-02-13 21:11:54 +08:00
|
|
|
{
|
2023-03-13 04:32:24 +08:00
|
|
|
return ref _denseItems[_sparseSet.IndexOf(entity)];
|
2023-02-13 21:11:54 +08:00
|
|
|
}
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2023-03-02 14:42:44 +08:00
|
|
|
public ref T Write(int entity)
|
2023-02-13 21:11:54 +08:00
|
|
|
{
|
2023-03-02 14:42:44 +08:00
|
|
|
if (_sparseSet.Contains(entity))
|
2023-02-27 18:16:23 +08:00
|
|
|
{
|
2023-03-13 04:32:24 +08:00
|
|
|
return ref _denseItems[_sparseSet.IndexOf(entity)];
|
2023-02-27 18:16:23 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-03-02 14:42:44 +08:00
|
|
|
_sparseSet.Add(entity);
|
2023-02-27 18:16:23 +08:00
|
|
|
_sparseSet.Normalize(ref _denseItems);
|
2023-03-02 14:42:44 +08:00
|
|
|
_source.OnEntityComponentAdded(entity, _id);
|
2023-03-13 04:32:24 +08:00
|
|
|
int indexof = _sparseSet.IndexOf(entity);
|
|
|
|
return ref _denseItems[indexof];
|
2023-02-27 18:16:23 +08:00
|
|
|
}
|
2023-02-13 21:11:54 +08:00
|
|
|
}
|
|
|
|
|
2023-02-05 19:59:45 +08:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2023-03-02 14:42:44 +08:00
|
|
|
public bool Has(int entity)
|
2023-02-05 19:59:45 +08:00
|
|
|
{
|
2023-03-13 04:32:24 +08:00
|
|
|
return _sparseSet.IndexOf(entity) >= 0;
|
2023-02-05 19:59:45 +08:00
|
|
|
}
|
2023-02-07 17:11:56 +08:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2023-03-02 14:42:44 +08:00
|
|
|
public void Del(int entity)
|
2023-02-07 17:11:56 +08:00
|
|
|
{
|
2023-03-02 14:42:44 +08:00
|
|
|
_sparseSet.RemoveAt(entity);
|
|
|
|
_source.OnEntityComponentRemoved(entity, _id);
|
2023-02-07 17:11:56 +08:00
|
|
|
}
|
|
|
|
#endregion
|
|
|
|
|
2023-03-13 04:32:24 +08:00
|
|
|
#region IEcsPool
|
2023-02-27 18:16:23 +08:00
|
|
|
void IEcsPool.Write(int index)
|
2023-02-05 19:59:45 +08:00
|
|
|
{
|
2023-02-27 18:16:23 +08:00
|
|
|
Write(index);
|
2023-02-05 19:59:45 +08:00
|
|
|
}
|
|
|
|
#endregion
|
2023-02-08 17:57:06 +08:00
|
|
|
|
|
|
|
#region Equals/GetHashCode
|
|
|
|
public override bool Equals(object obj)
|
|
|
|
{
|
|
|
|
return base.Equals(obj);
|
|
|
|
}
|
2023-02-13 21:11:54 +08:00
|
|
|
public override int GetHashCode() => _source.GetHashCode() + ID;
|
2023-02-08 17:57:06 +08:00
|
|
|
#endregion
|
2023-02-05 19:59:45 +08:00
|
|
|
}
|
|
|
|
}
|