2023-02-05 19:59:45 +08:00
|
|
|
using System.Collections;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
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 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-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-13 21:11:54 +08:00
|
|
|
private readonly int _id;
|
2023-02-07 17:11:56 +08:00
|
|
|
private readonly EcsWorld _source;
|
|
|
|
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-13 21:11:54 +08:00
|
|
|
public int ID => _id;
|
2023-02-07 17:11:56 +08:00
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region Constructors
|
2023-02-13 21:11:54 +08:00
|
|
|
public EcsPool(EcsWorld source, int capacity)
|
2023-02-06 01:27:32 +08:00
|
|
|
{
|
2023-02-07 17:11:56 +08:00
|
|
|
_source = source;
|
2023-02-06 01:27:32 +08:00
|
|
|
_sparseSet = new SparseSet(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)]
|
|
|
|
public ref readonly T Read(int index)
|
|
|
|
{
|
|
|
|
return ref _denseItems[_sparseSet[index]];
|
|
|
|
}
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
public ref T Write(int index)
|
|
|
|
{
|
2023-02-27 18:16:23 +08:00
|
|
|
if (_sparseSet.Contains(index))
|
|
|
|
{
|
|
|
|
return ref _denseItems[_sparseSet[index]];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_sparseSet.Add(index);
|
|
|
|
_sparseSet.Normalize(ref _denseItems);
|
|
|
|
return ref _denseItems[_sparseSet.IndexOf(index)];
|
|
|
|
}
|
2023-02-13 21:11:54 +08:00
|
|
|
}
|
|
|
|
|
2023-02-05 19:59:45 +08:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
public bool Has(int index)
|
|
|
|
{
|
2023-02-27 18:16:23 +08:00
|
|
|
return _sparseSet.IndexOf(index) > 0;
|
2023-02-05 19:59:45 +08:00
|
|
|
}
|
2023-02-07 17:11:56 +08:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
public void Del(int index)
|
|
|
|
{
|
2023-02-27 18:16:23 +08:00
|
|
|
_sparseSet.RemoveAt(index);
|
2023-02-07 17:11:56 +08:00
|
|
|
}
|
|
|
|
#endregion
|
|
|
|
|
2023-02-05 19:59:45 +08:00
|
|
|
#region IEcsFieldPool
|
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
|
|
|
}
|
2023-02-27 18:16:23 +08:00
|
|
|
|
|
|
|
public static partial class EntityExtensions
|
|
|
|
{
|
|
|
|
public static ref readonly T Read<T>(this in Entity self)
|
|
|
|
where T : struct
|
|
|
|
{
|
|
|
|
return ref self.world.GetPool<T>().Read(self.id);
|
|
|
|
}
|
|
|
|
public static ref T Write<T>(this in Entity self)
|
|
|
|
where T : struct
|
|
|
|
{
|
|
|
|
return ref self.world.GetPool<T>().Write(self.id);
|
|
|
|
}
|
|
|
|
public static bool Has<T>(this in Entity self)
|
|
|
|
where T : struct
|
|
|
|
{
|
|
|
|
return self.world.GetPool<T>().Has(self.id);
|
|
|
|
}
|
|
|
|
public static void Del<T>(this in Entity self)
|
|
|
|
where T : struct
|
|
|
|
{
|
|
|
|
self.world.GetPool<T>().Del(self.id);
|
|
|
|
}
|
|
|
|
}
|
2023-02-05 19:59:45 +08:00
|
|
|
}
|