DragonECS/src/EcsWorld.static.cs

130 lines
4.8 KiB
C#
Raw Normal View History

2023-06-25 23:22:01 +08:00
using DCFApixels.DragonECS.Utils;
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace DCFApixels.DragonECS
{
[StructLayout(LayoutKind.Sequential, Pack = 4, Size = 4)]
public struct EcsWorldCmp<T> where T : struct
{
private int _worldID;
public EcsWorldCmp(int worldID) => _worldID = worldID;
public EcsWorld World => EcsWorld.GetWorld(_worldID);
public ref T Value => ref EcsWorld.GetData<T>(_worldID);
}
public abstract partial class EcsWorld
{
private const short GEN_MASK = 0x7fff;
2023-06-25 23:22:01 +08:00
private const short DEATH_GEN_BIT = short.MinValue;
private const int DEL_ENT_BUFFER_SIZE_OFFSET = 5;
private const int DEL_ENT_BUFFER_MIN_SIZE = 64;
2023-06-25 23:22:01 +08:00
private static EcsWorld[] Worlds = new EcsWorld[4];
2024-02-03 01:12:53 +08:00
private static IdDispenser _worldIdDispenser = new IdDispenser(4);
2023-06-25 23:22:01 +08:00
private static List<DataReleaser> _dataReleaseres = new List<DataReleaser>();
2024-01-29 01:09:17 +08:00
//public static int Copacity => Worlds.Length;
2023-06-25 23:22:01 +08:00
static EcsWorld()
{
2024-02-03 01:12:53 +08:00
Worlds[0] = new NullWorld();
2023-06-25 23:22:01 +08:00
}
private static void ReleaseData(int worldID)
{
for (int i = 0, iMax = _dataReleaseres.Count; i < iMax; i++)
_dataReleaseres[i].Release(worldID);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static EcsWorld GetWorld(int worldID) => Worlds[worldID];
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ref T GetData<T>(int worldID) => ref WorldComponentPool<T>.GetForWorld(worldID);
2023-07-03 02:44:35 +08:00
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2024-01-29 01:09:17 +08:00
public static ref T GetDataUnchecked<T>(int worldID) => ref WorldComponentPool<T>.GetForWorldUnchecked(worldID);
2023-06-25 23:22:01 +08:00
private abstract class DataReleaser
{
public abstract void Release(int worldID);
}
private static class WorldComponentPool<T>
{
private static T[] _items = new T[4];
private static short[] _mapping = new short[4];
private static short _count;
private static short[] _recycledItems = new short[4];
private static short _recycledItemsCount;
2023-06-25 23:22:01 +08:00
private static IEcsWorldComponent<T> _interface = EcsWorldComponentHandler<T>.instance;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ref T Get(int itemIndex)
{
return ref _items[itemIndex];
}
2023-06-25 23:22:01 +08:00
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ref T GetForWorld(int worldID)
{
2023-12-31 21:02:53 +08:00
int index = GetItemIndex(worldID);
return ref _items[index];
}
2023-07-02 16:17:13 +08:00
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ref T GetForWorldUnchecked(int worldID)
{
#if (DEBUG && !DISABLE_DEBUG)
if (_mapping[worldID] <= 0)
throw new Exception();
#endif
return ref _items[_mapping[worldID]];
}
2023-06-25 23:22:01 +08:00
public static int GetItemIndex(int worldID)
{
if (_mapping.Length < Worlds.Length)
2023-12-31 21:02:53 +08:00
{
2023-06-25 23:22:01 +08:00
Array.Resize(ref _mapping, Worlds.Length);
2023-12-31 21:02:53 +08:00
}
ref short itemIndex = ref _mapping[worldID];
2023-06-25 23:22:01 +08:00
if (itemIndex <= 0)
{
if (_recycledItemsCount > 0)
{
_count++;
itemIndex = _recycledItems[--_recycledItemsCount];
}
else
{
itemIndex = ++_count;
}
2024-01-07 19:32:16 +08:00
if (_items.Length <= itemIndex)
2023-12-31 21:02:53 +08:00
{
Array.Resize(ref _items, _items.Length << 1);
}
2023-06-25 23:22:01 +08:00
_interface.Init(ref _items[itemIndex], Worlds[worldID]);
_dataReleaseres.Add(new Releaser());
}
return itemIndex;
}
private static void Release(int worldID)
{
ref short itemIndex = ref _mapping[worldID];
2023-06-25 23:22:01 +08:00
if (itemIndex != 0)
{
_interface.OnDestroy(ref _items[itemIndex], Worlds[worldID]);
_recycledItems[_recycledItemsCount++] = itemIndex;
}
}
private sealed class Releaser : DataReleaser
{
public sealed override void Release(int worldID)
{
WorldComponentPool<T>.Release(worldID);
}
}
}
2024-02-03 01:12:53 +08:00
private sealed class NullWorld : EcsWorld
{
2024-02-10 21:25:56 +08:00
internal NullWorld() : base(EcsWorldConfig.Empty, false) { }
2024-02-03 01:12:53 +08:00
}
}
2023-06-25 23:22:01 +08:00
}