2023-05-26 06:18:09 +08:00
|
|
|
|
using DCFApixels.DragonECS.Internal;
|
|
|
|
|
using DCFApixels.DragonECS.Utils;
|
|
|
|
|
using System;
|
2023-03-02 14:42:44 +08:00
|
|
|
|
using System.Collections.Generic;
|
2023-06-20 23:34:51 +08:00
|
|
|
|
using System.Reflection;
|
2023-03-02 14:42:44 +08:00
|
|
|
|
using System.Runtime.CompilerServices;
|
2023-06-21 15:03:33 +08:00
|
|
|
|
using System.Runtime.InteropServices;
|
2023-03-02 14:42:44 +08:00
|
|
|
|
|
|
|
|
|
namespace DCFApixels.DragonECS
|
|
|
|
|
{
|
2023-06-21 15:03:33 +08:00
|
|
|
|
[StructLayout(LayoutKind.Sequential, Pack = 4, Size = 4)]
|
2023-06-22 02:02:43 +08:00
|
|
|
|
public struct EcsWorldCmp<T> where T : struct
|
2023-06-21 15:03:33 +08:00
|
|
|
|
{
|
|
|
|
|
private int _worldID;
|
2023-06-22 02:02:43 +08:00
|
|
|
|
public EcsWorldCmp(int worldID) => _worldID = worldID;
|
2023-06-21 15:03:33 +08:00
|
|
|
|
public EcsWorld World => EcsWorld.GetWorld(_worldID);
|
2023-06-22 02:02:43 +08:00
|
|
|
|
public ref T Value => ref EcsWorld.GetData<T>(_worldID);
|
2023-06-21 15:03:33 +08:00
|
|
|
|
}
|
2023-06-21 01:37:05 +08:00
|
|
|
|
public abstract partial class EcsWorld
|
2023-03-02 14:42:44 +08:00
|
|
|
|
{
|
2023-04-23 17:19:52 +08:00
|
|
|
|
private const short GEN_BITS = 0x7fff;
|
|
|
|
|
private const short DEATH_GEN_BIT = short.MinValue;
|
2023-04-24 16:48:18 +08:00
|
|
|
|
private const int DEL_ENT_BUFFER_SIZE_OFFSET = 2;
|
2023-04-23 17:19:52 +08:00
|
|
|
|
|
2023-06-22 02:02:43 +08:00
|
|
|
|
private static EcsWorld[] Worlds = new EcsWorld[4];
|
2023-04-07 15:11:48 +08:00
|
|
|
|
private static IntDispenser _worldIdDispenser = new IntDispenser(0);
|
2023-06-21 01:37:05 +08:00
|
|
|
|
|
2023-06-21 15:03:33 +08:00
|
|
|
|
private static List<DataReleaser> _dataReleaseres = new List<DataReleaser>();
|
|
|
|
|
|
2023-06-21 01:37:05 +08:00
|
|
|
|
static EcsWorld()
|
|
|
|
|
{
|
|
|
|
|
Worlds[0] = new EcsNullWorld();
|
|
|
|
|
}
|
2023-06-21 15:03:33 +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)]
|
2023-06-18 02:42:20 +08:00
|
|
|
|
public static EcsWorld GetWorld(int worldID) => Worlds[worldID];
|
2023-05-26 03:45:35 +08:00
|
|
|
|
|
2023-06-21 15:03:33 +08:00
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
|
public static ref T GetData<T>(int worldID) => ref WorldComponentPool<T>.GetForWorld(worldID);
|
|
|
|
|
|
|
|
|
|
private abstract class DataReleaser
|
|
|
|
|
{
|
|
|
|
|
public abstract void Release(int worldID);
|
|
|
|
|
}
|
2023-06-21 01:37:05 +08:00
|
|
|
|
private static class WorldComponentPool<T>
|
|
|
|
|
{
|
|
|
|
|
private static T[] _items = new T[4];
|
|
|
|
|
private static int[] _mapping = new int[4];
|
|
|
|
|
private static int _count;
|
2023-06-21 15:03:33 +08:00
|
|
|
|
private static int[] _recycledItems = new int[4];
|
|
|
|
|
private static int _recycledItemsCount;
|
2023-06-21 01:37:05 +08:00
|
|
|
|
private static IEcsWorldComponent<T> _interface = EcsWorldComponentHandler<T>.instance;
|
2023-06-21 15:03:33 +08:00
|
|
|
|
|
2023-06-21 01:37:05 +08:00
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
|
public static ref T Get(int itemIndex) => ref _items[itemIndex];
|
2023-06-21 15:03:33 +08:00
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
|
public static ref T GetForWorld(int worldID) => ref _items[GetItemIndex(worldID)];
|
2023-06-21 01:37:05 +08:00
|
|
|
|
public static int GetItemIndex(int worldID)
|
|
|
|
|
{
|
|
|
|
|
if (_mapping.Length < Worlds.Length)
|
|
|
|
|
Array.Resize(ref _mapping, Worlds.Length);
|
|
|
|
|
|
|
|
|
|
ref int itemIndex = ref _mapping[worldID];
|
|
|
|
|
if (itemIndex <= 0)
|
|
|
|
|
{
|
2023-06-21 15:03:33 +08:00
|
|
|
|
if(_recycledItemsCount > 0)
|
|
|
|
|
{
|
|
|
|
|
_count++;
|
|
|
|
|
itemIndex = _recycledItems[--_recycledItemsCount];
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
itemIndex = ++_count;
|
|
|
|
|
}
|
2023-06-21 01:37:05 +08:00
|
|
|
|
_interface.Init(ref _items[itemIndex], Worlds[worldID]);
|
2023-06-21 15:03:33 +08:00
|
|
|
|
_dataReleaseres.Add(new Releaser());
|
2023-06-21 01:37:05 +08:00
|
|
|
|
}
|
|
|
|
|
return itemIndex;
|
|
|
|
|
}
|
2023-06-21 15:03:33 +08:00
|
|
|
|
private static void Release(int worldID)
|
|
|
|
|
{
|
|
|
|
|
ref int itemIndex = ref _mapping[worldID];
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-06-21 01:37:05 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
public abstract partial class EcsWorld
|
|
|
|
|
{
|
2023-06-18 02:42:20 +08:00
|
|
|
|
public readonly short id;
|
2023-04-20 20:03:26 +08:00
|
|
|
|
|
2023-06-18 18:46:19 +08:00
|
|
|
|
private Type _worldType;
|
2023-05-07 00:50:02 +08:00
|
|
|
|
private int _worldTypeID;
|
2023-04-18 19:35:42 +08:00
|
|
|
|
|
2023-03-11 17:11:40 +08:00
|
|
|
|
private IntDispenser _entityDispenser;
|
2023-04-01 22:18:40 +08:00
|
|
|
|
private int _entitiesCount;
|
2023-04-18 19:35:42 +08:00
|
|
|
|
private int _entitesCapacity;
|
2023-05-26 00:24:38 +08:00
|
|
|
|
private short[] _gens; //старший бит указывает на то жива ли сущность
|
2023-04-26 16:45:37 +08:00
|
|
|
|
private short[] _componentCounts;
|
2023-04-20 11:37:27 +08:00
|
|
|
|
private EcsGroup _allEntites;
|
|
|
|
|
|
2023-04-20 18:23:23 +08:00
|
|
|
|
private int[] _delEntBuffer;
|
2023-04-20 11:37:27 +08:00
|
|
|
|
private int _delEntBufferCount;
|
2023-04-18 19:35:42 +08:00
|
|
|
|
|
2023-05-26 00:24:38 +08:00
|
|
|
|
internal IEcsPoolImplementation[] _pools;
|
2023-06-18 00:39:25 +08:00
|
|
|
|
private EcsNullPool _nullPool = EcsNullPool.instance;
|
2023-03-02 14:42:44 +08:00
|
|
|
|
|
2023-05-07 00:50:02 +08:00
|
|
|
|
private EcsSubject[] _subjects;
|
|
|
|
|
private EcsQueryExecutor[] _executors;
|
2023-03-02 14:42:44 +08:00
|
|
|
|
|
2023-06-18 00:39:25 +08:00
|
|
|
|
private List<WeakReference<EcsGroup>> _groups = new List<WeakReference<EcsGroup>>();
|
2023-04-18 19:35:42 +08:00
|
|
|
|
private Stack<EcsGroup> _groupsPool = new Stack<EcsGroup>(64);
|
2023-04-01 20:45:37 +08:00
|
|
|
|
|
2023-06-18 00:39:25 +08:00
|
|
|
|
private List<IEcsWorldEventListener> _listeners = new List<IEcsWorldEventListener>();
|
|
|
|
|
private List<IEcsEntityEventListener> _entityListeners = new List<IEcsEntityEventListener>();
|
2023-04-01 20:45:37 +08:00
|
|
|
|
|
2023-03-02 14:42:44 +08:00
|
|
|
|
#region Properties
|
2023-06-18 18:46:19 +08:00
|
|
|
|
public int WorldTypeID => _worldTypeID;
|
2023-04-18 19:35:42 +08:00
|
|
|
|
public int Count => _entitiesCount;
|
|
|
|
|
public int Capacity => _entitesCapacity; //_denseEntities.Length;
|
2023-04-09 02:52:39 +08:00
|
|
|
|
public EcsReadonlyGroup Entities => _allEntites.Readonly;
|
2023-05-26 00:24:38 +08:00
|
|
|
|
public ReadOnlySpan<IEcsPoolImplementation> AllPools => _pools;// new ReadOnlySpan<IEcsPoolImplementation>(pools, 0, _poolsCount);
|
2023-04-23 22:55:13 +08:00
|
|
|
|
#endregion
|
|
|
|
|
|
2023-04-20 20:10:16 +08:00
|
|
|
|
#region Constructors/Destroy
|
2023-05-23 15:58:31 +08:00
|
|
|
|
public EcsWorld() : this(true) { }
|
|
|
|
|
internal EcsWorld(bool isIndexable)
|
2023-04-20 20:03:26 +08:00
|
|
|
|
{
|
2023-04-23 17:19:52 +08:00
|
|
|
|
_entitesCapacity = 512;
|
|
|
|
|
|
2023-05-07 00:50:02 +08:00
|
|
|
|
if (isIndexable)
|
|
|
|
|
{
|
2023-06-18 02:42:20 +08:00
|
|
|
|
id = (short)_worldIdDispenser.GetFree();
|
|
|
|
|
if (id >= Worlds.Length)
|
2023-05-07 00:50:02 +08:00
|
|
|
|
Array.Resize(ref Worlds, Worlds.Length << 1);
|
2023-06-18 02:42:20 +08:00
|
|
|
|
Worlds[id] = this;
|
2023-05-07 00:50:02 +08:00
|
|
|
|
}
|
2023-04-20 20:03:26 +08:00
|
|
|
|
|
2023-06-18 18:46:19 +08:00
|
|
|
|
_worldType = this.GetType();
|
|
|
|
|
_worldTypeID = WorldMetaStorage.GetWorldID(_worldType);
|
2023-04-20 20:03:26 +08:00
|
|
|
|
|
2023-04-07 15:11:48 +08:00
|
|
|
|
_entityDispenser = new IntDispenser(0);
|
2023-05-26 00:24:38 +08:00
|
|
|
|
_pools = new IEcsPoolImplementation[512];
|
|
|
|
|
ArrayUtility.Fill(_pools, _nullPool);
|
2023-04-01 20:45:37 +08:00
|
|
|
|
|
2023-04-23 17:19:52 +08:00
|
|
|
|
_gens = new short[_entitesCapacity];
|
2023-04-26 16:45:37 +08:00
|
|
|
|
_componentCounts = new short[_entitesCapacity];
|
|
|
|
|
|
2023-04-23 17:19:52 +08:00
|
|
|
|
ArrayUtility.Fill(_gens, DEATH_GEN_BIT);
|
2023-04-20 11:37:27 +08:00
|
|
|
|
_delEntBufferCount = 0;
|
2023-04-23 17:19:52 +08:00
|
|
|
|
_delEntBuffer = new int[_entitesCapacity >> DEL_ENT_BUFFER_SIZE_OFFSET];
|
2023-04-18 19:35:42 +08:00
|
|
|
|
|
2023-06-01 20:14:34 +08:00
|
|
|
|
_allEntites = GetFreeGroup();
|
2023-04-20 10:59:55 +08:00
|
|
|
|
|
2023-05-07 00:50:02 +08:00
|
|
|
|
_subjects = new EcsSubject[128];
|
|
|
|
|
_executors = new EcsQueryExecutor[128];
|
2023-03-02 14:42:44 +08:00
|
|
|
|
}
|
2023-04-20 20:10:16 +08:00
|
|
|
|
public void Destroy()
|
2023-04-20 20:03:26 +08:00
|
|
|
|
{
|
2023-04-20 20:10:16 +08:00
|
|
|
|
_entityDispenser = null;
|
|
|
|
|
_gens = null;
|
2023-05-26 00:24:38 +08:00
|
|
|
|
_pools = null;
|
2023-04-20 20:10:16 +08:00
|
|
|
|
_nullPool = null;
|
2023-05-07 00:50:02 +08:00
|
|
|
|
_subjects = null;
|
|
|
|
|
_executors = null;
|
2023-06-18 02:42:20 +08:00
|
|
|
|
Worlds[id] = null;
|
2023-06-21 15:03:33 +08:00
|
|
|
|
ReleaseData(id);
|
2023-06-18 02:42:20 +08:00
|
|
|
|
_worldIdDispenser.Release(id);
|
2023-04-20 20:03:26 +08:00
|
|
|
|
}
|
2023-03-02 14:42:44 +08:00
|
|
|
|
#endregion
|
|
|
|
|
|
2023-05-30 04:32:09 +08:00
|
|
|
|
#region ComponentInfo
|
2023-06-04 18:32:05 +08:00
|
|
|
|
public int GetComponentID<T>() => WorldMetaStorage.GetComponentID<T>(_worldTypeID);
|
|
|
|
|
public int GetComponentID(Type type) => WorldMetaStorage.GetComponentID(type, _worldTypeID);
|
2023-05-30 04:32:09 +08:00
|
|
|
|
public Type GetComponentType(int componentID) => WorldMetaStorage.GetComponentType(_worldTypeID, componentID);
|
|
|
|
|
public bool IsComponentTypeDeclared<T>() => IsComponentTypeDeclared(typeof(T));
|
|
|
|
|
public bool IsComponentTypeDeclared(Type type) => WorldMetaStorage.IsComponentTypeDeclared(_worldTypeID, type);
|
2023-04-24 16:48:18 +08:00
|
|
|
|
#endregion
|
|
|
|
|
|
2023-05-30 04:32:09 +08:00
|
|
|
|
#region Getters
|
2023-05-28 05:53:08 +08:00
|
|
|
|
#if UNITY_2020_3_OR_NEWER
|
|
|
|
|
[UnityEngine.Scripting.Preserve]
|
|
|
|
|
#endif
|
2023-06-04 18:32:05 +08:00
|
|
|
|
public TPool GetPool<TPool>() where TPool : IEcsPoolImplementation, new()
|
2023-03-02 14:42:44 +08:00
|
|
|
|
{
|
2023-06-10 19:43:19 +08:00
|
|
|
|
int index = WorldMetaStorage.GetPoolID<TPool>(_worldTypeID);
|
|
|
|
|
if (index >= _pools.Length)
|
2023-03-02 14:42:44 +08:00
|
|
|
|
{
|
2023-05-26 00:24:38 +08:00
|
|
|
|
int oldCapacity = _pools.Length;
|
|
|
|
|
Array.Resize(ref _pools, _pools.Length << 1);
|
|
|
|
|
ArrayUtility.Fill(_pools, _nullPool, oldCapacity, oldCapacity - _pools.Length);
|
2023-03-02 14:42:44 +08:00
|
|
|
|
}
|
2023-06-10 19:43:19 +08:00
|
|
|
|
if (_pools[index] == _nullPool)
|
2023-04-21 03:16:05 +08:00
|
|
|
|
{
|
|
|
|
|
var pool = new TPool();
|
2023-06-10 19:43:19 +08:00
|
|
|
|
_pools[index] = pool;
|
|
|
|
|
pool.OnInit(this, index);
|
2023-04-21 03:16:05 +08:00
|
|
|
|
}
|
2023-06-10 19:43:19 +08:00
|
|
|
|
return (TPool)_pools[index];
|
2023-03-02 14:42:44 +08:00
|
|
|
|
}
|
2023-05-07 00:50:02 +08:00
|
|
|
|
public TSubject GetSubject<TSubject>() where TSubject : EcsSubject
|
|
|
|
|
{
|
2023-06-10 19:43:19 +08:00
|
|
|
|
int index = WorldMetaStorage.GetSubjectID<TSubject>(_worldTypeID);
|
|
|
|
|
if (index >= _subjects.Length)
|
2023-05-07 00:50:02 +08:00
|
|
|
|
Array.Resize(ref _subjects, _subjects.Length << 1);
|
2023-06-10 19:43:19 +08:00
|
|
|
|
if (_subjects[index] == null)
|
|
|
|
|
_subjects[index] = EcsSubject.Builder.Build<TSubject>(this);
|
|
|
|
|
return (TSubject)_subjects[index];
|
2023-05-07 00:50:02 +08:00
|
|
|
|
}
|
2023-05-27 15:59:46 +08:00
|
|
|
|
public TExecutor GetExecutor<TExecutor>() where TExecutor : EcsQueryExecutor, new()
|
2023-05-07 00:50:02 +08:00
|
|
|
|
{
|
2023-06-04 18:32:05 +08:00
|
|
|
|
int index = WorldMetaStorage.GetExecutorID<TExecutor>(_worldTypeID);
|
2023-05-30 15:20:27 +08:00
|
|
|
|
if (index >= _executors.Length)
|
2023-05-07 00:50:02 +08:00
|
|
|
|
Array.Resize(ref _executors, _executors.Length << 1);
|
2023-06-10 19:43:19 +08:00
|
|
|
|
var result = _executors[index];
|
|
|
|
|
if (result == null)
|
2023-05-27 15:59:46 +08:00
|
|
|
|
{
|
2023-06-10 19:43:19 +08:00
|
|
|
|
result = new TExecutor();
|
|
|
|
|
_executors[index] = result;
|
|
|
|
|
result.Initialize(this);
|
2023-05-27 15:59:46 +08:00
|
|
|
|
}
|
2023-06-10 19:43:19 +08:00
|
|
|
|
return (TExecutor)result;
|
2023-05-30 15:20:27 +08:00
|
|
|
|
}
|
2023-06-21 15:03:33 +08:00
|
|
|
|
public ref T Get<T>() where T : struct => ref WorldComponentPool<T>.GetForWorld(id);
|
2023-05-30 04:32:09 +08:00
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Where Query
|
2023-06-02 03:33:33 +08:00
|
|
|
|
public EcsReadonlyGroup WhereFor<TSubject>(EcsReadonlyGroup sourceGroup, out TSubject subject) where TSubject : EcsSubject
|
2023-05-07 00:50:02 +08:00
|
|
|
|
{
|
2023-05-27 15:59:46 +08:00
|
|
|
|
var executor = GetExecutor<EcsWhereExecutor<TSubject>>();
|
2023-05-07 00:50:02 +08:00
|
|
|
|
subject = executor.Subject;
|
|
|
|
|
return executor.ExecuteFor(sourceGroup);
|
|
|
|
|
}
|
2023-06-02 03:33:33 +08:00
|
|
|
|
public EcsReadonlyGroup WhereFor<TSubject>(EcsReadonlyGroup sourceGroup) where TSubject : EcsSubject
|
2023-05-07 00:50:02 +08:00
|
|
|
|
{
|
2023-05-27 15:59:46 +08:00
|
|
|
|
return GetExecutor<EcsWhereExecutor<TSubject>>().ExecuteFor(sourceGroup);
|
2023-05-07 00:50:02 +08:00
|
|
|
|
}
|
2023-06-02 03:33:33 +08:00
|
|
|
|
public EcsReadonlyGroup Where<TSubject>(out TSubject subject) where TSubject : EcsSubject
|
2023-05-07 00:50:02 +08:00
|
|
|
|
{
|
2023-05-27 15:59:46 +08:00
|
|
|
|
var executor = GetExecutor<EcsWhereExecutor<TSubject>>();
|
2023-05-07 00:50:02 +08:00
|
|
|
|
subject = executor.Subject;
|
|
|
|
|
return executor.Execute();
|
|
|
|
|
}
|
2023-06-02 03:33:33 +08:00
|
|
|
|
public EcsReadonlyGroup Where<TSubject>() where TSubject : EcsSubject
|
2023-05-07 00:50:02 +08:00
|
|
|
|
{
|
2023-05-27 15:59:46 +08:00
|
|
|
|
return GetExecutor<EcsWhereExecutor<TSubject>>().Execute();
|
2023-05-07 00:50:02 +08:00
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
|
2023-03-26 11:19:03 +08:00
|
|
|
|
#region Entity
|
2023-05-23 01:47:28 +08:00
|
|
|
|
public int NewEmptyEntity()
|
2023-03-02 14:42:44 +08:00
|
|
|
|
{
|
2023-03-26 11:19:03 +08:00
|
|
|
|
int entityID = _entityDispenser.GetFree();
|
2023-04-18 19:35:42 +08:00
|
|
|
|
_entitiesCount++;
|
2023-04-01 22:18:40 +08:00
|
|
|
|
|
2023-04-01 20:45:37 +08:00
|
|
|
|
if (_gens.Length <= entityID)
|
|
|
|
|
{
|
2023-03-26 11:19:03 +08:00
|
|
|
|
Array.Resize(ref _gens, _gens.Length << 1);
|
2023-04-26 16:45:37 +08:00
|
|
|
|
Array.Resize(ref _componentCounts, _gens.Length);
|
2023-04-23 17:19:52 +08:00
|
|
|
|
ArrayUtility.Fill(_gens, DEATH_GEN_BIT, _entitesCapacity);
|
2023-04-18 19:35:42 +08:00
|
|
|
|
_entitesCapacity = _gens.Length;
|
2023-04-23 17:19:52 +08:00
|
|
|
|
|
2023-04-17 22:58:52 +08:00
|
|
|
|
for (int i = 0; i < _groups.Count; i++)
|
|
|
|
|
{
|
|
|
|
|
if (_groups[i].TryGetTarget(out EcsGroup group))
|
|
|
|
|
{
|
|
|
|
|
group.OnWorldResize(_gens.Length);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
int last = _groups.Count - 1;
|
|
|
|
|
_groups[i--] = _groups[last];
|
|
|
|
|
_groups.RemoveAt(last);
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-05-26 00:24:38 +08:00
|
|
|
|
foreach (var item in _pools)
|
2023-05-07 00:50:02 +08:00
|
|
|
|
item.OnWorldResize(_gens.Length);
|
2023-05-23 01:47:28 +08:00
|
|
|
|
|
2023-05-23 15:58:31 +08:00
|
|
|
|
_listeners.InvokeOnWorldResize(_gens.Length);
|
2023-04-01 20:45:37 +08:00
|
|
|
|
}
|
2023-04-23 17:19:52 +08:00
|
|
|
|
_gens[entityID] &= GEN_BITS;
|
2023-04-09 02:52:39 +08:00
|
|
|
|
_allEntites.Add(entityID);
|
2023-06-10 18:58:43 +08:00
|
|
|
|
_entityListeners.InvokeOnNewEntity(entityID);
|
2023-05-07 00:50:02 +08:00
|
|
|
|
return entityID;
|
|
|
|
|
}
|
2023-05-23 01:47:28 +08:00
|
|
|
|
public entlong NewEmptyEntityLong()
|
2023-05-07 00:50:02 +08:00
|
|
|
|
{
|
2023-05-23 01:47:28 +08:00
|
|
|
|
int e = NewEmptyEntity();
|
2023-05-07 00:50:02 +08:00
|
|
|
|
return GetEntityLong(e);
|
2023-03-26 11:19:03 +08:00
|
|
|
|
}
|
2023-04-26 16:45:37 +08:00
|
|
|
|
public void DelEntity(int entityID)
|
2023-03-26 11:19:03 +08:00
|
|
|
|
{
|
2023-04-26 16:45:37 +08:00
|
|
|
|
_allEntites.Remove(entityID);
|
|
|
|
|
_delEntBuffer[_delEntBufferCount++] = entityID;
|
|
|
|
|
_gens[entityID] |= DEATH_GEN_BIT;
|
2023-04-01 22:18:40 +08:00
|
|
|
|
_entitiesCount--;
|
2023-06-10 18:58:43 +08:00
|
|
|
|
_entityListeners.InvokeOnDelEntity(entityID);
|
2023-04-20 11:37:27 +08:00
|
|
|
|
|
2023-04-20 20:03:26 +08:00
|
|
|
|
if (_delEntBufferCount >= _delEntBuffer.Length)
|
2023-04-24 16:48:18 +08:00
|
|
|
|
ReleaseDelEntityBuffer();
|
2023-04-20 11:37:27 +08:00
|
|
|
|
}
|
2023-04-24 16:48:18 +08:00
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2023-06-18 02:42:20 +08:00
|
|
|
|
public entlong GetEntityLong(int entityID) => new entlong(entityID, _gens[entityID], id); //TODO придумать получше имя метода
|
2023-04-24 16:48:18 +08:00
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
|
public bool IsAlive(int entityID, short gen) => _gens[entityID] == gen;
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2023-05-07 00:50:02 +08:00
|
|
|
|
public bool IsUsed(int entityID) => _gens[entityID] >= 0;
|
2023-05-30 04:32:09 +08:00
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
|
public short GetGen(int entityID) => _gens[entityID];
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
|
public short GetComponentsCount(int entityID) => _componentCounts[entityID];
|
|
|
|
|
|
|
|
|
|
public bool IsMatchesMask(EcsMask mask, int entityID)
|
|
|
|
|
{
|
|
|
|
|
#if (DEBUG && !DISABLE_DEBUG) || !DISABLE_DRAGONECS_ASSERT_CHEKS
|
2023-06-18 18:46:19 +08:00
|
|
|
|
if (mask._worldTypeID != _worldTypeID)
|
2023-05-30 04:32:09 +08:00
|
|
|
|
throw new EcsFrameworkException("The types of the target world of the mask and this world are different.");
|
|
|
|
|
#endif
|
|
|
|
|
for (int i = 0, iMax = mask._inc.Length; i < iMax; i++)
|
|
|
|
|
{
|
|
|
|
|
if (!_pools[mask._inc[i]].Has(entityID))
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
for (int i = 0, iMax = mask._exc.Length; i < iMax; i++)
|
|
|
|
|
{
|
|
|
|
|
if (_pools[mask._exc[i]].Has(entityID))
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2023-04-24 16:48:18 +08:00
|
|
|
|
public void ReleaseDelEntityBuffer()
|
2023-04-20 11:37:27 +08:00
|
|
|
|
{
|
2023-05-07 00:50:02 +08:00
|
|
|
|
ReadOnlySpan<int> buffser = new ReadOnlySpan<int>(_delEntBuffer, 0, _delEntBufferCount);
|
2023-05-26 00:24:38 +08:00
|
|
|
|
foreach (var pool in _pools)
|
2023-05-07 00:50:02 +08:00
|
|
|
|
pool.OnReleaseDelEntityBuffer(buffser);
|
2023-05-23 15:58:31 +08:00
|
|
|
|
_listeners.InvokeOnReleaseDelEntityBuffer(buffser);
|
2023-04-20 11:37:27 +08:00
|
|
|
|
for (int i = 0; i < _delEntBufferCount; i++)
|
|
|
|
|
_entityDispenser.Release(_delEntBuffer[i]);
|
|
|
|
|
_delEntBufferCount = 0;
|
2023-03-02 14:42:44 +08:00
|
|
|
|
}
|
2023-04-26 16:45:37 +08:00
|
|
|
|
public void DeleteEmptyEntites()
|
|
|
|
|
{
|
|
|
|
|
foreach (var e in _allEntites)
|
|
|
|
|
{
|
2023-05-26 00:24:38 +08:00
|
|
|
|
if (_componentCounts[e] <= 0) DelEntity(e);
|
2023-04-26 16:45:37 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-30 04:32:09 +08:00
|
|
|
|
#region Copy/Clone
|
2023-05-23 01:47:28 +08:00
|
|
|
|
public void CopyEntity(int fromEntityID, int toEntityID)
|
|
|
|
|
{
|
2023-05-26 00:24:38 +08:00
|
|
|
|
foreach (var pool in _pools)
|
2023-05-23 01:47:28 +08:00
|
|
|
|
{
|
2023-05-30 04:32:09 +08:00
|
|
|
|
if (pool.Has(fromEntityID)) pool.Copy(fromEntityID, toEntityID);
|
2023-05-23 01:47:28 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
public int CloneEntity(int fromEntityID)
|
|
|
|
|
{
|
|
|
|
|
int newEntity = NewEmptyEntity();
|
|
|
|
|
CopyEntity(fromEntityID, newEntity);
|
|
|
|
|
return newEntity;
|
|
|
|
|
}
|
|
|
|
|
public void CloneEntity(int fromEntityID, int toEntityID)
|
|
|
|
|
{
|
|
|
|
|
CopyEntity(fromEntityID, toEntityID);
|
2023-05-26 00:24:38 +08:00
|
|
|
|
foreach (var pool in _pools)
|
2023-05-23 01:47:28 +08:00
|
|
|
|
{
|
2023-05-30 04:32:09 +08:00
|
|
|
|
if (!pool.Has(fromEntityID) && pool.Has(toEntityID))
|
|
|
|
|
pool.Del(toEntityID);
|
2023-05-23 01:47:28 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-05-30 04:32:09 +08:00
|
|
|
|
#endregion
|
2023-05-23 01:47:28 +08:00
|
|
|
|
|
2023-05-30 04:32:09 +08:00
|
|
|
|
#region Components Increment
|
2023-04-26 16:45:37 +08:00
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2023-05-26 00:24:38 +08:00
|
|
|
|
internal void IncrementEntityComponentCount(int entityID) => _componentCounts[entityID]++;
|
2023-04-26 16:45:37 +08:00
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
|
internal void DecrementEntityComponentCount(int entityID)
|
|
|
|
|
{
|
|
|
|
|
var count = --_componentCounts[entityID];
|
2023-05-30 04:32:09 +08:00
|
|
|
|
if (count == 0 && _allEntites.Has(entityID)) DelEntity(entityID);
|
2023-05-26 05:13:11 +08:00
|
|
|
|
#if (DEBUG && !DISABLE_DEBUG) || !DISABLE_DRAGONECS_ASSERT_CHEKS
|
2023-05-26 00:24:38 +08:00
|
|
|
|
if (count < 0) throw new EcsFrameworkException("нарушен баланс инкремента/декремента компонентов");
|
2023-04-26 16:45:37 +08:00
|
|
|
|
#endif
|
|
|
|
|
}
|
2023-03-02 14:42:44 +08:00
|
|
|
|
#endregion
|
|
|
|
|
|
2023-05-30 04:32:09 +08:00
|
|
|
|
#endregion
|
|
|
|
|
|
2023-05-28 06:29:04 +08:00
|
|
|
|
#region Groups Pool
|
2023-04-20 20:03:26 +08:00
|
|
|
|
internal void RegisterGroup(EcsGroup group)
|
2023-04-07 05:08:48 +08:00
|
|
|
|
{
|
2023-04-17 22:58:52 +08:00
|
|
|
|
_groups.Add(new WeakReference<EcsGroup>(group));
|
2023-04-07 05:08:48 +08:00
|
|
|
|
}
|
2023-06-01 20:14:34 +08:00
|
|
|
|
internal EcsGroup GetFreeGroup()
|
2023-04-18 19:35:42 +08:00
|
|
|
|
{
|
2023-05-28 06:29:04 +08:00
|
|
|
|
EcsGroup result = _groupsPool.Count <= 0 ? new EcsGroup(this) : _groupsPool.Pop();
|
|
|
|
|
result._isReleased = false;
|
|
|
|
|
return result;
|
2023-04-18 19:35:42 +08:00
|
|
|
|
}
|
2023-04-20 20:03:26 +08:00
|
|
|
|
internal void ReleaseGroup(EcsGroup group)
|
2023-04-18 19:35:42 +08:00
|
|
|
|
{
|
2023-05-26 05:13:11 +08:00
|
|
|
|
#if (DEBUG && !DISABLE_DEBUG) || !DISABLE_DRAGONECS_ASSERT_CHEKS
|
2023-04-18 19:35:42 +08:00
|
|
|
|
if (group.World != this)
|
2023-04-20 18:23:23 +08:00
|
|
|
|
throw new ArgumentException("groupFilter.WorldIndex != this");
|
2023-04-18 19:35:42 +08:00
|
|
|
|
#endif
|
2023-05-28 06:29:04 +08:00
|
|
|
|
group._isReleased = true;
|
2023-04-18 19:35:42 +08:00
|
|
|
|
group.Clear();
|
|
|
|
|
_groupsPool.Push(group);
|
|
|
|
|
}
|
2023-04-07 05:08:48 +08:00
|
|
|
|
#endregion
|
2023-05-07 00:50:02 +08:00
|
|
|
|
|
2023-06-08 04:04:39 +08:00
|
|
|
|
#region Listeners
|
2023-06-19 02:10:25 +08:00
|
|
|
|
public void AddListener(IEcsWorldEventListener worldEventListener)
|
2023-06-08 04:04:39 +08:00
|
|
|
|
{
|
2023-06-19 02:10:25 +08:00
|
|
|
|
_listeners.Add(worldEventListener);
|
2023-06-08 04:04:39 +08:00
|
|
|
|
}
|
2023-06-19 02:10:25 +08:00
|
|
|
|
public void RemoveListener(IEcsWorldEventListener worldEventListener)
|
2023-06-08 04:04:39 +08:00
|
|
|
|
{
|
2023-06-19 02:10:25 +08:00
|
|
|
|
_listeners.Remove(worldEventListener);
|
2023-06-08 04:04:39 +08:00
|
|
|
|
}
|
2023-06-19 02:10:25 +08:00
|
|
|
|
public void AddListener(IEcsEntityEventListener entityEventListener)
|
2023-06-10 18:58:43 +08:00
|
|
|
|
{
|
2023-06-19 02:10:25 +08:00
|
|
|
|
_entityListeners.Add(entityEventListener);
|
2023-06-10 18:58:43 +08:00
|
|
|
|
}
|
2023-06-19 02:10:25 +08:00
|
|
|
|
public void RemoveListener(IEcsEntityEventListener entityEventListener)
|
2023-06-10 18:58:43 +08:00
|
|
|
|
{
|
2023-06-19 02:10:25 +08:00
|
|
|
|
_entityListeners.Remove(entityEventListener);
|
2023-06-10 18:58:43 +08:00
|
|
|
|
}
|
2023-06-08 04:04:39 +08:00
|
|
|
|
#endregion
|
|
|
|
|
|
2023-05-07 00:50:02 +08:00
|
|
|
|
#region Debug
|
2023-05-23 01:47:28 +08:00
|
|
|
|
public void GetComponents(int entityID, List<object> list)
|
|
|
|
|
{
|
|
|
|
|
list.Clear();
|
|
|
|
|
var itemsCount = GetComponentsCount(entityID);
|
|
|
|
|
|
2023-05-26 00:24:38 +08:00
|
|
|
|
for (var i = 0; i < _pools.Length; i++)
|
2023-05-23 01:47:28 +08:00
|
|
|
|
{
|
2023-05-26 00:24:38 +08:00
|
|
|
|
if (_pools[i].Has(entityID))
|
2023-05-30 18:30:10 +08:00
|
|
|
|
list.Add(_pools[i].GetRaw(entityID));
|
2023-05-23 01:47:28 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-05-07 00:50:02 +08:00
|
|
|
|
#endregion
|
2023-05-30 04:32:09 +08:00
|
|
|
|
}
|
2023-05-28 05:53:08 +08:00
|
|
|
|
|
2023-06-18 18:46:19 +08:00
|
|
|
|
internal sealed class EcsNullWorld : EcsWorld { }
|
2023-04-20 20:03:26 +08:00
|
|
|
|
|
2023-05-26 00:24:38 +08:00
|
|
|
|
#region Callbacks Interface
|
2023-05-23 15:58:31 +08:00
|
|
|
|
public interface IEcsWorldEventListener
|
|
|
|
|
{
|
|
|
|
|
void OnWorldResize(int newSize);
|
|
|
|
|
void OnReleaseDelEntityBuffer(ReadOnlySpan<int> buffer);
|
|
|
|
|
void OnWorldDestroy();
|
2023-06-10 18:58:43 +08:00
|
|
|
|
}
|
|
|
|
|
public interface IEcsEntityEventListener
|
|
|
|
|
{
|
2023-05-23 15:58:31 +08:00
|
|
|
|
void OnNewEntity(int entityID);
|
|
|
|
|
void OnDelEntity(int entityID);
|
|
|
|
|
}
|
2023-05-26 00:24:38 +08:00
|
|
|
|
internal static class WorldEventListExtensions
|
2023-05-23 15:58:31 +08:00
|
|
|
|
{
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
|
public static void InvokeOnWorldResize(this List<IEcsWorldEventListener> self, int newSize)
|
|
|
|
|
{
|
|
|
|
|
for (int i = 0, iMax = self.Count; i < iMax; i++) self[i].OnWorldResize(newSize);
|
|
|
|
|
}
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
|
public static void InvokeOnReleaseDelEntityBuffer(this List<IEcsWorldEventListener> self, ReadOnlySpan<int> buffer)
|
|
|
|
|
{
|
|
|
|
|
for (int i = 0, iMax = self.Count; i < iMax; i++) self[i].OnReleaseDelEntityBuffer(buffer);
|
|
|
|
|
}
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
|
public static void InvokeOnWorldDestroy(this List<IEcsWorldEventListener> self)
|
|
|
|
|
{
|
|
|
|
|
for (int i = 0, iMax = self.Count; i < iMax; i++) self[i].OnWorldDestroy();
|
|
|
|
|
}
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2023-06-10 18:58:43 +08:00
|
|
|
|
public static void InvokeOnNewEntity(this List<IEcsEntityEventListener> self, int entityID)
|
2023-05-23 15:58:31 +08:00
|
|
|
|
{
|
|
|
|
|
for (int i = 0, iMax = self.Count; i < iMax; i++) self[i].OnNewEntity(entityID);
|
|
|
|
|
}
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2023-06-10 18:58:43 +08:00
|
|
|
|
public static void InvokeOnDelEntity(this List<IEcsEntityEventListener> self, int entityID)
|
2023-05-23 15:58:31 +08:00
|
|
|
|
{
|
|
|
|
|
for (int i = 0, iMax = self.Count; i < iMax; i++) self[i].OnDelEntity(entityID);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
2023-05-28 06:35:33 +08:00
|
|
|
|
|
|
|
|
|
#region Extensions
|
|
|
|
|
public static class IntExtensions
|
|
|
|
|
{
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
|
public static entlong ToEntityLong(this int self, EcsWorld world) => world.GetEntityLong(self);
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
2023-03-02 14:42:44 +08:00
|
|
|
|
}
|