2023-02-05 19:59:45 +08:00
|
|
|
using System.Collections;
|
|
|
|
using System.Collections.Generic;
|
2023-02-08 17:07:39 +08:00
|
|
|
using DCFApixels.DragonECS.Reflection;
|
2023-02-05 19:59:45 +08:00
|
|
|
using System.Runtime.CompilerServices;
|
|
|
|
using UnityEngine;
|
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
|
|
|
|
{
|
|
|
|
public EcsWorld World { get; }
|
|
|
|
public int ID { get; }
|
|
|
|
public EcsMemberBase Type { get; }
|
2023-02-09 02:26:43 +08:00
|
|
|
public bool IsTagsPool { get; }
|
2023-02-08 17:07:39 +08:00
|
|
|
public bool Has(int index);
|
|
|
|
public void Add(int index);
|
|
|
|
public void Del(int index);
|
|
|
|
}
|
|
|
|
|
2023-02-07 17:11:56 +08:00
|
|
|
public class EcsPool<T> : IEcsPool
|
2023-02-09 02:26:43 +08:00
|
|
|
where T : struct
|
2023-02-05 19:59:45 +08:00
|
|
|
{
|
2023-02-06 01:27:32 +08:00
|
|
|
private int _id;
|
2023-02-07 17:11:56 +08:00
|
|
|
private readonly EcsWorld _source;
|
2023-02-08 17:07:39 +08:00
|
|
|
private readonly EcsMember<T> _type;
|
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-09 02:26:43 +08:00
|
|
|
private int _isTagsPoolMask;
|
|
|
|
|
2023-02-07 17:11:56 +08:00
|
|
|
#region Properites
|
|
|
|
public EcsWorld World => _source;
|
2023-02-08 17:07:39 +08:00
|
|
|
public EcsMemberBase Type => _type;
|
2023-02-09 02:26:43 +08:00
|
|
|
|
|
|
|
public int ID
|
|
|
|
{
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
get => _id;
|
|
|
|
}
|
|
|
|
public bool IsTagsPool
|
2023-02-05 19:59:45 +08:00
|
|
|
{
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2023-02-09 02:26:43 +08:00
|
|
|
get => _isTagsPoolMask < 0;
|
2023-02-05 19:59:45 +08:00
|
|
|
}
|
|
|
|
|
2023-02-09 02:26:43 +08:00
|
|
|
public ref T this[int index]
|
|
|
|
{
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
get => ref _denseItems[_sparseSet[index] | _isTagsPoolMask];
|
|
|
|
}
|
2023-02-07 17:11:56 +08:00
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region Constructors
|
2023-02-09 02:26:43 +08:00
|
|
|
public EcsPool(EcsWorld source, mem<T> type, int capacity)
|
2023-02-06 01:27:32 +08:00
|
|
|
{
|
2023-02-07 17:11:56 +08:00
|
|
|
_source = source;
|
2023-02-09 02:26:43 +08:00
|
|
|
_type = MemberDeclarator.GetMemberInfo(type);
|
2023-02-06 01:27:32 +08:00
|
|
|
_sparseSet = new SparseSet(capacity);
|
2023-02-09 02:26:43 +08:00
|
|
|
|
|
|
|
_isTagsPoolMask = typeof(T).GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic).Length <= 0 ? -1 : 0;
|
|
|
|
|
|
|
|
_denseItems = IsTagsPool ? new T[1] : 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-07 17:11:56 +08:00
|
|
|
#region Add/Has/Get/Del
|
2023-02-05 19:59:45 +08:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
public ref T Add(int index)
|
|
|
|
{
|
|
|
|
_sparseSet.Add(index);
|
2023-02-09 02:26:43 +08:00
|
|
|
if(IsTagsPool)
|
|
|
|
{
|
|
|
|
_sparseSet.Normalize(ref _denseItems);
|
|
|
|
return ref _denseItems[_sparseSet.IndexOf(index)];
|
|
|
|
}
|
|
|
|
return ref _denseItems[0];
|
2023-02-05 19:59:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
public bool Has(int index)
|
|
|
|
{
|
|
|
|
return _sparseSet.Contains(index);
|
|
|
|
}
|
|
|
|
|
2023-02-07 17:11:56 +08:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
public void Del(int index)
|
|
|
|
{
|
2023-02-09 02:26:43 +08:00
|
|
|
if (!IsTagsPool) { this[index] = default; }
|
2023-02-07 17:11:56 +08:00
|
|
|
_sparseSet.Remove(index);
|
|
|
|
}
|
|
|
|
#endregion
|
|
|
|
|
2023-02-05 19:59:45 +08:00
|
|
|
#region IEcsFieldPool
|
2023-02-07 17:11:56 +08:00
|
|
|
void IEcsPool.Add(int index)
|
2023-02-05 19:59:45 +08:00
|
|
|
{
|
|
|
|
Add(index);
|
|
|
|
}
|
|
|
|
#endregion
|
2023-02-08 17:57:06 +08:00
|
|
|
|
|
|
|
#region Equals/GetHashCode
|
|
|
|
public override bool Equals(object obj)
|
|
|
|
{
|
|
|
|
return base.Equals(obj);
|
|
|
|
}
|
|
|
|
public override int GetHashCode() => _type.GetHashCode();
|
|
|
|
#endregion
|
2023-02-05 19:59:45 +08:00
|
|
|
}
|
|
|
|
}
|