api polishing/add world name

This commit is contained in:
DCFApixels 2024-12-03 16:59:32 +08:00
parent c23e3ce621
commit 0182614819
10 changed files with 120 additions and 48 deletions

View File

@ -10,8 +10,9 @@ namespace DCFApixels.DragonECS
[MetaID("4EE3527C92015BAB0299CB7B4E2663D1")] [MetaID("4EE3527C92015BAB0299CB7B4E2663D1")]
public sealed class EcsDefaultWorld : EcsWorld, IInjectionUnit public sealed class EcsDefaultWorld : EcsWorld, IInjectionUnit
{ {
public EcsDefaultWorld(EcsWorldConfig config, short worldID = -1) : base(config, worldID) { } public EcsDefaultWorld() : base() { }
public EcsDefaultWorld(IConfigContainer configs = null, short worldID = -1) : base(configs, worldID) { } public EcsDefaultWorld(EcsWorldConfig config = null, string name = null, short worldID = -1) : base(config, name == null ? "Default" : name, worldID) { }
public EcsDefaultWorld(IConfigContainer configs, string name = null, short worldID = -1) : base(configs, name == null ? "Default" : name, worldID) { }
void IInjectionUnit.InitInjectionNode(InjectionGraph nodes) { nodes.AddNode(this); } void IInjectionUnit.InitInjectionNode(InjectionGraph nodes) { nodes.AddNode(this); }
} }
/// <summary> EcsWrold for store event entities. </summary> /// <summary> EcsWrold for store event entities. </summary>
@ -22,8 +23,9 @@ namespace DCFApixels.DragonECS
[MetaID("D7CE527C920160BCD765EFA72DBF8B89")] [MetaID("D7CE527C920160BCD765EFA72DBF8B89")]
public sealed class EcsEventWorld : EcsWorld, IInjectionUnit public sealed class EcsEventWorld : EcsWorld, IInjectionUnit
{ {
public EcsEventWorld(EcsWorldConfig config, short worldID = -1) : base(config, worldID) { } public EcsEventWorld() : base() { }
public EcsEventWorld(IConfigContainer configs = null, short worldID = -1) : base(configs, worldID) { } public EcsEventWorld(EcsWorldConfig config = null, string name = null, short worldID = -1) : base(config, name == null ? "Events" : name, worldID) { }
public EcsEventWorld(IConfigContainer configs, string name = null, short worldID = -1) : base(configs, name == null ? "Events" : name, worldID) { }
void IInjectionUnit.InitInjectionNode(InjectionGraph nodes) { nodes.AddNode(this); } void IInjectionUnit.InitInjectionNode(InjectionGraph nodes) { nodes.AddNode(this); }
} }
} }

View File

@ -190,7 +190,7 @@ namespace DCFApixels.DragonECS
#region Pages #region Pages
internal int* TakePage() internal int* TakePage()
{ {
if(_groupSparsePagePoolCount <= 0) if (_groupSparsePagePoolCount <= 0)
{ {
var x = UnmanagedArrayUtility.NewAndInit<int>(EcsGroup.PAGE_SIZE); var x = UnmanagedArrayUtility.NewAndInit<int>(EcsGroup.PAGE_SIZE);
return x; return x;
@ -308,17 +308,17 @@ namespace DCFApixels.DragonECS
#endif #endif
return _dense[++index]; return _dense[++index];
} }
// [MethodImpl(MethodImplOptions.AggressiveInlining)] // [MethodImpl(MethodImplOptions.AggressiveInlining)]
// set // set
// { // {
// // TODO добавить лок енумератора на изменение // // TODO добавить лок енумератора на изменение
//#if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS //#if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS
// if (index < 0 || index >= Count) { Throw.ArgumentOutOfRange(); } // if (index < 0 || index >= Count) { Throw.ArgumentOutOfRange(); }
//#endif //#endif
// var oldValue = _dense[index]; // var oldValue = _dense[index];
// _dense[index] = value; // _dense[index] = value;
// _sparse[oldValue] = 0; // _sparse[oldValue] = 0;
// } // }
} }
#endregion #endregion
@ -393,7 +393,7 @@ namespace DCFApixels.DragonECS
//_sparse[entityID] = _count; //_sparse[entityID] = _count;
PageSlot* page = _sparsePages + (entityID >> PageSlot.SHIFT); PageSlot* page = _sparsePages + (entityID >> PageSlot.SHIFT);
if(page->Count == 0) if (page->Count == 0)
{ {
page->Indexes = _source.TakePage(); page->Indexes = _source.TakePage();
} }

View File

@ -11,6 +11,10 @@ using static DCFApixels.DragonECS.EcsConsts;
namespace DCFApixels.DragonECS namespace DCFApixels.DragonECS
{ {
public interface IEcsMember { } public interface IEcsMember { }
public interface INamedMember
{
string Name { get; }
}
[MetaColor(MetaColor.DragonRose)] [MetaColor(MetaColor.DragonRose)]
[MetaGroup(PACK_GROUP, OTHER_GROUP)] [MetaGroup(PACK_GROUP, OTHER_GROUP)]

View File

@ -6,11 +6,13 @@ using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
#if ENABLE_IL2CPP
using Unity.IL2CPP.CompilerServices;
#endif
namespace DCFApixels.DragonECS namespace DCFApixels.DragonECS
{ {
#if ENABLE_IL2CPP #if ENABLE_IL2CPP
using Unity.IL2CPP.CompilerServices;
[Il2CppSetOption(Option.NullChecks, false)] [Il2CppSetOption(Option.NullChecks, false)]
[Il2CppSetOption(Option.ArrayBoundsChecks, false)] [Il2CppSetOption(Option.ArrayBoundsChecks, false)]
#endif #endif
@ -41,10 +43,11 @@ namespace DCFApixels.DragonECS
[MetaDescription(EcsConsts.AUTHOR, "It is a container for entities and components.")] [MetaDescription(EcsConsts.AUTHOR, "It is a container for entities and components.")]
[MetaID("AEF3557C92019C976FC48F90E95A9DA6")] [MetaID("AEF3557C92019C976FC48F90E95A9DA6")]
[DebuggerTypeProxy(typeof(DebuggerProxy))] [DebuggerTypeProxy(typeof(DebuggerProxy))]
public partial class EcsWorld : IEntityStorage, IEcsMember public partial class EcsWorld : IEntityStorage, IEcsMember, INamedMember
{ {
public readonly short ID; public readonly short ID;
private IConfigContainer _configs; private readonly IConfigContainer _configs;
private readonly string _name;
private bool _isDestroyed = false; private bool _isDestroyed = false;
@ -73,6 +76,7 @@ namespace DCFApixels.DragonECS
#region Properties #region Properties
EcsWorld IEntityStorage.World EcsWorld IEntityStorage.World
{ {
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get { return this; } get { return this; }
} }
public IConfigContainer Configs public IConfigContainer Configs
@ -80,6 +84,11 @@ namespace DCFApixels.DragonECS
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
get { return _configs; } get { return _configs; }
} }
public string Name
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get { return _name; }
}
public long Version public long Version
{ {
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
@ -134,12 +143,15 @@ namespace DCFApixels.DragonECS
#endregion #endregion
#region Constructors/Destroy #region Constructors/Destroy
public EcsWorld(EcsWorldConfig config, short worldID = -1) : this(config == null ? ConfigContainer.Empty : new ConfigContainer().Set(config), worldID) { } public EcsWorld() : this(ConfigContainer.Empty, null, -1) { }
public EcsWorld(IConfigContainer configs = null, short worldID = -1) public EcsWorld(EcsWorldConfig config = null, string name = null, short worldID = -1) : this(config == null ? ConfigContainer.Empty : new ConfigContainer().Set(config), name, worldID) { }
public EcsWorld(IConfigContainer configs, string name = null, short worldID = -1)
{ {
lock (_worldLock) lock (_worldLock)
{ {
if (configs == null) { configs = ConfigContainer.Empty; } if (configs == null) { configs = ConfigContainer.Empty; }
if (name == null) { name = string.Empty; }
_name = name;
bool nullWorld = this is NullWorld; bool nullWorld = this is NullWorld;
if (nullWorld == false && worldID == NULL_WORLD_ID) if (nullWorld == false && worldID == NULL_WORLD_ID)
{ {
@ -264,6 +276,18 @@ namespace DCFApixels.DragonECS
#region New/Del #region New/Del
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public entlong NewEntityLong()
{
int entityID = NewEntity();
return GetEntityLong(entityID);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public entlong NewEntityLong(int entityID)
{
NewEntity(entityID);
return GetEntityLong(entityID);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public int NewEntity() public int NewEntity()
{ {
int entityID = _entityDispenser.UseFree(); int entityID = _entityDispenser.UseFree();
@ -294,24 +318,26 @@ namespace DCFApixels.DragonECS
_entityListeners.InvokeOnNewEntity(entityID); _entityListeners.InvokeOnNewEntity(entityID);
} }
public entlong NewEntityLong()
{
int entityID = NewEntity();
return GetEntityLong(entityID);
}
public entlong NewEntityLong(int entityID)
{
NewEntity(entityID);
return GetEntityLong(entityID);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public void TryDelEntity(int entityID) public bool TryDelEntity(entlong entity)
{
if (entity.TryGetID(out int entityID))
{
TryDelEntity(entityID);
return true;
}
return false;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool TryDelEntity(int entityID)
{ {
if (IsUsed(entityID)) if (IsUsed(entityID))
{ {
DelEntity(entityID); DelEntity(entityID);
return true;
} }
return false;
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public void DelEntity(entlong entity) public void DelEntity(entlong entity)
@ -360,6 +386,15 @@ namespace DCFApixels.DragonECS
return slot.gen == gen && slot.isUsed; return slot.gen == gen && slot.isUsed;
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool IsAlive(entlong entity)
{
#if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS
if (entity.GetWorldIDUnchecked() != ID) { Throw.World_MaskDoesntBelongWorld(); }
#endif
ref var slot = ref _entities[entity.GetIDUnchecked()];
return slot.gen == entity.GetIDUnchecked() && slot.isUsed;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool IsUsed(int entityID) public bool IsUsed(int entityID)
{ {
return _entities[entityID].isUsed; return _entities[entityID].isUsed;

View File

@ -94,6 +94,10 @@ namespace DCFApixels.DragonECS
{ {
return DeclareOrGetComponentTypeID(EcsTypeCodeManager.Get(componentType)); return DeclareOrGetComponentTypeID(EcsTypeCodeManager.Get(componentType));
} }
public Type GetComponentType(int componentTypeID)
{
return _pools[componentTypeID].ComponentType;
}
public bool IsComponentTypeDeclared<TComponent>() public bool IsComponentTypeDeclared<TComponent>()
{ {
return _cmpTypeCode_2_CmpTypeIDs.Contains((int)EcsTypeCodeManager.Get<TComponent>()); return _cmpTypeCode_2_CmpTypeIDs.Contains((int)EcsTypeCodeManager.Get<TComponent>());
@ -102,6 +106,7 @@ namespace DCFApixels.DragonECS
{ {
return _cmpTypeCode_2_CmpTypeIDs.Contains((int)EcsTypeCodeManager.Get(componentType)); return _cmpTypeCode_2_CmpTypeIDs.Contains((int)EcsTypeCodeManager.Get(componentType));
} }
//TODO пересмотреть нейминг или функцию
public bool IsComponentTypeDeclared(int componentTypeID) public bool IsComponentTypeDeclared(int componentTypeID)
{ {
if (componentTypeID >= 0 && componentTypeID < _pools.Length) if (componentTypeID >= 0 && componentTypeID < _pools.Length)
@ -110,10 +115,6 @@ namespace DCFApixels.DragonECS
} }
return false; return false;
} }
public Type GetComponentType(int componentTypeID)
{
return _pools[componentTypeID].ComponentType;
}
#endregion #endregion
#region Declare #region Declare
@ -364,10 +365,10 @@ namespace DCFApixels.DragonECS
#endregion #endregion
#region LockPool/UnLockPool #region LockPool/UnLockPool
public void LockPool_Debug(int ComponentTypeID) public void LockPool_Debug(int componentTypeID)
{ {
#if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS #if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS
ref var slot = ref _poolSlots[ComponentTypeID]; ref var slot = ref _poolSlots[componentTypeID];
if (slot.locked == false) if (slot.locked == false)
{ {
slot.locked = true; slot.locked = true;
@ -376,14 +377,14 @@ namespace DCFApixels.DragonECS
ReleaseDelEntityBufferAll(); ReleaseDelEntityBufferAll();
} }
_lockedPoolCount++; _lockedPoolCount++;
_pools[ComponentTypeID].OnLockedChanged_Debug(true); _pools[componentTypeID].OnLockedChanged_Debug(true);
} }
#endif #endif
} }
public void UnlockPool_Debug(int ComponentTypeID) public void UnlockPool_Debug(int componentTypeID)
{ {
#if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS #if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS
ref var slot = ref _poolSlots[ComponentTypeID]; ref var slot = ref _poolSlots[componentTypeID];
if (slot.locked == true) if (slot.locked == true)
{ {
slot.locked = false; slot.locked = false;
@ -394,21 +395,21 @@ namespace DCFApixels.DragonECS
_lockedPoolCount = 0; _lockedPoolCount = 0;
Throw.OpeningClosingMethodsBalanceError(); Throw.OpeningClosingMethodsBalanceError();
} }
_pools[ComponentTypeID].OnLockedChanged_Debug(false); _pools[componentTypeID].OnLockedChanged_Debug(false);
} }
#endif #endif
} }
public bool CheckPoolLocked_Debug(int ComponentTypeID) public bool CheckPoolLocked_Debug(int componentTypeID)
{ {
#if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS #if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS
return _poolSlots[ComponentTypeID].locked; return _poolSlots[componentTypeID].locked;
#else #else
return false; return false;
#endif #endif
} }
#endregion #endregion
#region PoolSlot #region Utils
internal struct PoolSlot internal struct PoolSlot
{ {
public int count; public int count;

View File

@ -163,7 +163,7 @@ namespace DCFApixels.DragonECS
} }
private sealed class NullWorld : EcsWorld private sealed class NullWorld : EcsWorld
{ {
internal NullWorld() : base(new EcsWorldConfig(4, 4, 4, 4, 4), 0) { } internal NullWorld() : base(new EcsWorldConfig(4, 4, 4, 4, 4), null, 0) { }
} }
#region Obsolete #region Obsolete

View File

@ -3,6 +3,7 @@ using DCFApixels.DragonECS.PoolsCore;
using System; using System;
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
#if ENABLE_IL2CPP #if ENABLE_IL2CPP
using Unity.IL2CPP.CompilerServices; using Unity.IL2CPP.CompilerServices;
@ -25,6 +26,7 @@ namespace DCFApixels.DragonECS
[MetaGroup(EcsConsts.PACK_GROUP, EcsConsts.POOLS_GROUP)] [MetaGroup(EcsConsts.PACK_GROUP, EcsConsts.POOLS_GROUP)]
[MetaDescription(EcsConsts.AUTHOR, "Pool for IEcsComponent components.")] [MetaDescription(EcsConsts.AUTHOR, "Pool for IEcsComponent components.")]
[MetaID("C501547C9201A4B03FC25632E4FAAFD7")] [MetaID("C501547C9201A4B03FC25632E4FAAFD7")]
[DebuggerDisplay("Count: {Count}")]
public sealed class EcsPool<T> : IEcsPoolImplementation<T>, IEcsStructPool<T>, IEnumerable<T> //IEnumerable<T> - IntelliSense hack public sealed class EcsPool<T> : IEcsPoolImplementation<T>, IEcsStructPool<T>, IEnumerable<T> //IEnumerable<T> - IntelliSense hack
where T : struct, IEcsComponent where T : struct, IEcsComponent
{ {

View File

@ -2,6 +2,7 @@
using DCFApixels.DragonECS.PoolsCore; using DCFApixels.DragonECS.PoolsCore;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
namespace DCFApixels.DragonECS.PoolsCore namespace DCFApixels.DragonECS.PoolsCore
@ -58,6 +59,7 @@ namespace DCFApixels.DragonECS.Internal
[MetaDescription(EcsConsts.AUTHOR, "A placeholder type, an instance of this type replaces the null ref.")] [MetaDescription(EcsConsts.AUTHOR, "A placeholder type, an instance of this type replaces the null ref.")]
[MetaTags(MetaTags.HIDDEN)] [MetaTags(MetaTags.HIDDEN)]
[MetaID("460E547C9201227A4956AC297F67B484")] [MetaID("460E547C9201227A4956AC297F67B484")]
[DebuggerDisplay("-")]
public sealed class EcsNullPool : IEcsPoolImplementation<NullComponent> public sealed class EcsNullPool : IEcsPoolImplementation<NullComponent>
{ {
public static readonly EcsNullPool instance = new EcsNullPool(); public static readonly EcsNullPool instance = new EcsNullPool();

View File

@ -3,6 +3,8 @@ using System;
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using System.Diagnostics;
#if (DEBUG && !DISABLE_DEBUG) #if (DEBUG && !DISABLE_DEBUG)
using System.Reflection; using System.Reflection;
#endif #endif
@ -27,6 +29,7 @@ namespace DCFApixels.DragonECS
[MetaGroup(EcsConsts.PACK_GROUP, EcsConsts.POOLS_GROUP)] [MetaGroup(EcsConsts.PACK_GROUP, EcsConsts.POOLS_GROUP)]
[MetaDescription(EcsConsts.AUTHOR, "Pool for IEcsTagComponent components. EcsTagPool is optimized for storing tag components or components without data.")] [MetaDescription(EcsConsts.AUTHOR, "Pool for IEcsTagComponent components. EcsTagPool is optimized for storing tag components or components without data.")]
[MetaID("9D80547C9201E852E4F17324EAC1E15A")] [MetaID("9D80547C9201E852E4F17324EAC1E15A")]
[DebuggerDisplay("Count: {Count}")]
public sealed class EcsTagPool<T> : IEcsPoolImplementation<T>, IEcsStructPool<T>, IEnumerable<T> //IEnumerable<T> - IntelliSense hack public sealed class EcsTagPool<T> : IEcsPoolImplementation<T>, IEcsStructPool<T>, IEnumerable<T> //IEnumerable<T> - IntelliSense hack
where T : struct, IEcsTagComponent where T : struct, IEcsTagComponent
{ {

View File

@ -6,6 +6,16 @@ namespace DCFApixels.DragonECS
} }
public static class ITemplateNodeExtensions public static class ITemplateNodeExtensions
{ {
public static int ApplyAndReturn(this ITemplateNode self, short worldID, int entityID)
{
self.Apply(worldID, entityID);
return entityID;
}
public static entlong ApplyAndReturnLong(this ITemplateNode self, short worldID, int entityID)
{
self.Apply(worldID, entityID);
return (EcsWorld.GetWorld(worldID), entityID);
}
public static int NewEntity(this EcsWorld world, ITemplateNode template) public static int NewEntity(this EcsWorld world, ITemplateNode template)
{ {
int e = world.NewEntity(); int e = world.NewEntity();
@ -18,5 +28,18 @@ namespace DCFApixels.DragonECS
template.Apply(world.ID, e.ID); template.Apply(world.ID, e.ID);
return e; return e;
} }
public static int NewEntity(this EcsWorld world, int entityID, ITemplateNode template)
{
int e = world.NewEntity(entityID);
template.Apply(world.ID, e);
return e;
}
public static entlong NewEntityLong(this EcsWorld world, int entityID, ITemplateNode template)
{
entlong e = world.NewEntityLong(entityID);
template.Apply(world.ID, e.ID);
return e;
}
} }
} }