DragonECS/src/EcsPool.cs

124 lines
3.4 KiB
C#
Raw Normal View History

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
{
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);
}
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
{
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;
public int ID => _id;
2023-02-07 17:11:56 +08:00
#endregion
#region Constructors
2023-03-02 14:42:44 +08:00
public EcsPool(IEcsWorld 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
_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
#region Read/Write/Has/Del
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2023-03-02 14:42:44 +08:00
public ref readonly T Read(int entity)
{
2023-03-02 14:42:44 +08:00
return ref _denseItems[_sparseSet[entity]];
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2023-03-02 14:42:44 +08:00
public ref T Write(int entity)
{
2023-03-02 14:42:44 +08:00
if (_sparseSet.Contains(entity))
2023-02-27 18:16:23 +08:00
{
2023-03-02 14:42:44 +08:00
return ref _denseItems[_sparseSet[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);
return ref _denseItems[_sparseSet.IndexOf(entity)];
2023-02-27 18:16:23 +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-02 14:42:44 +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-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);
}
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
}