DragonECS/src/EcsPool.cs

79 lines
2.0 KiB
C#
Raw Normal View History

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;
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; }
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-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-07 17:11:56 +08:00
#region Properites
public EcsWorld World => _source;
2023-02-06 01:27:32 +08:00
public int ID => _id;
2023-02-08 17:07:39 +08:00
public EcsMemberBase Type => _type;
2023-02-05 19:59:45 +08:00
public ref T this[int index]
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => ref _denseItems[_sparseSet[index]];
}
2023-02-07 17:11:56 +08:00
#endregion
#region Constructors
2023-02-08 17:07:39 +08:00
public EcsPool(EcsWorld source, EcsMember<T> type, int capacity)
2023-02-06 01:27:32 +08:00
{
2023-02-07 17:11:56 +08:00
_source = source;
_type = type;
2023-02-06 01:27:32 +08:00
_denseItems = new T[capacity];
_sparseSet = new SparseSet(capacity);
}
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);
_sparseSet.Normalize(ref _denseItems);
return ref _denseItems[_sparseSet.IndexOf(index)];
}
[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)
{
_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
}
}