mirror of
https://github.com/DCFApixels/DragonECS.git
synced 2025-09-18 18:14:37 +08:00
moving part of the API into a separate module
This commit is contained in:
parent
d230afdeb7
commit
55f6843451
@ -1,72 +0,0 @@
|
|||||||
using System.ComponentModel;
|
|
||||||
using System.Runtime.CompilerServices;
|
|
||||||
|
|
||||||
namespace DCFApixels.DragonECS
|
|
||||||
{
|
|
||||||
[DebugColor(DebugColor.White)]
|
|
||||||
public struct Parent : IEcsAttachComponent
|
|
||||||
{
|
|
||||||
public entlong entity;
|
|
||||||
|
|
||||||
[EditorBrowsable(EditorBrowsableState.Never)]
|
|
||||||
public entlong Target
|
|
||||||
{
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
||||||
get => entity;
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
||||||
set => entity = value;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static class ParentUtility
|
|
||||||
{
|
|
||||||
// public static int GetRootOrSelf(this HierarchySubject s, int entityID) => s.parents.GetRootOrSelf(entityID);
|
|
||||||
public static int GetRootOrSelf(this EcsAttachPool<Parent> parents, int entityID)
|
|
||||||
{
|
|
||||||
while (parents.Has(entityID) && parents.Read(entityID).entity.TryGetID(out int child))
|
|
||||||
entityID = child;
|
|
||||||
return entityID;
|
|
||||||
}
|
|
||||||
// public static bool IsRoot(this HierarchySubject s, int entityID) => s.parents.IsRoot(entityID);
|
|
||||||
public static bool IsRoot(this EcsAttachPool<Parent> parents, int entityID)
|
|
||||||
{
|
|
||||||
return !(parents.Has(entityID) && parents.Read(entityID).entity.IsAlive);
|
|
||||||
}
|
|
||||||
|
|
||||||
// public static bool TryGetRoot(this HierarchySubject s, int entityID, out int rootEntityID) => TryGetRoot(s.parents, entityID, out rootEntityID);
|
|
||||||
public static bool TryGetRoot(this EcsAttachPool<Parent> parents, EcsSubject conditionSubject, int entityID, out int rootEntityID)
|
|
||||||
{
|
|
||||||
rootEntityID = entityID;
|
|
||||||
while (parents.Has(rootEntityID) && parents.Read(rootEntityID).entity.TryGetID(out int child) && !conditionSubject.IsMatches(child))
|
|
||||||
rootEntityID = child;
|
|
||||||
return rootEntityID != entityID;
|
|
||||||
}
|
|
||||||
public static bool TryGetRoot(this EcsAttachPool<Parent> parents, int entityID, out int rootEntityID)
|
|
||||||
{
|
|
||||||
rootEntityID = entityID;
|
|
||||||
while (parents.Has(rootEntityID) && parents.Read(rootEntityID).entity.TryGetID(out int child))
|
|
||||||
rootEntityID = child;
|
|
||||||
return rootEntityID != entityID;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static bool TryFindParentWithSubject(this EcsAttachPool<Parent> parents, EcsSubject conditionSubject, int entityID, out int resultEntityID)
|
|
||||||
{
|
|
||||||
resultEntityID = entityID;
|
|
||||||
while (parents.Has(resultEntityID) && parents.Read(resultEntityID).entity.TryGetID(out int child) && !conditionSubject.IsMatches(resultEntityID))
|
|
||||||
resultEntityID = child;
|
|
||||||
return conditionSubject.IsMatches(resultEntityID);
|
|
||||||
}
|
|
||||||
public static bool TryFindParentWith<TComponent>(this EcsAttachPool<Parent> parents, int entityID, out int resultEntityID) where TComponent : struct
|
|
||||||
{
|
|
||||||
var pool = parents.World.AllPools[parents.World.GetComponentID<TComponent>()];
|
|
||||||
resultEntityID = entityID;
|
|
||||||
while (!pool.Has(resultEntityID) &&
|
|
||||||
parents.Has(resultEntityID) &&
|
|
||||||
parents.Read(resultEntityID).entity.TryGetID(out int child))
|
|
||||||
{
|
|
||||||
resultEntityID = child;
|
|
||||||
}
|
|
||||||
return pool.Has(resultEntityID);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,11 +0,0 @@
|
|||||||
namespace DCFApixels.DragonECS
|
|
||||||
{
|
|
||||||
public sealed class HierarchySubject : EcsSubject
|
|
||||||
{
|
|
||||||
public readonly EcsAttachPool<Parent> parents;
|
|
||||||
public HierarchySubject(Builder b)
|
|
||||||
{
|
|
||||||
parents = b.Include<Parent>();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,224 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Collections;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Runtime.CompilerServices;
|
|
||||||
|
|
||||||
namespace DCFApixels.DragonECS
|
|
||||||
{
|
|
||||||
using static EcsPoolThrowHalper;
|
|
||||||
//íå âëèÿåò íà ñ÷åò÷èê êîìïîíåíòîâ íà ñóùíîñòè
|
|
||||||
/// <summary>Pool for IEcsAttachComponent components</summary>
|
|
||||||
public sealed class EcsAttachPool<T> : IEcsPoolImplementation<T>, IEnumerable<T> //IntelliSense hack
|
|
||||||
where T : struct, IEcsAttachComponent
|
|
||||||
{
|
|
||||||
private EcsWorld _source;
|
|
||||||
private int _id;
|
|
||||||
|
|
||||||
private bool[] _entityFlags;// index = entityID / value = entityFlag;/ value = 0 = no entityID
|
|
||||||
private T[] _items; //sparse
|
|
||||||
private int _count;
|
|
||||||
|
|
||||||
private List<IEcsPoolEventListener> _listeners;
|
|
||||||
|
|
||||||
private EcsGroup _entities;
|
|
||||||
public EcsReadonlyGroup Entities
|
|
||||||
{
|
|
||||||
get
|
|
||||||
{
|
|
||||||
_entities.RemoveUnusedEntityIDs();
|
|
||||||
return _entities.Readonly;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#if (DEBUG && !DISABLE_DEBUG) || !DISABLE_DRAGONECS_ASSERT_CHEKS
|
|
||||||
private short _sanitizeTargetWorld = -1;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#region Properites
|
|
||||||
public int Count => _count;
|
|
||||||
public int Capacity => _items.Length;
|
|
||||||
public int ComponentID => _id;
|
|
||||||
public Type ComponentType => typeof(T);
|
|
||||||
public EcsWorld World => _source;
|
|
||||||
#endregion
|
|
||||||
|
|
||||||
#region Init
|
|
||||||
void IEcsPoolImplementation.OnInit(EcsWorld world, int componentID)
|
|
||||||
{
|
|
||||||
_source = world;
|
|
||||||
_id = componentID;
|
|
||||||
|
|
||||||
_listeners = new List<IEcsPoolEventListener>();
|
|
||||||
|
|
||||||
_entities = EcsGroup.New(world);
|
|
||||||
|
|
||||||
_entityFlags = new bool[world.Capacity];
|
|
||||||
_items = new T[world.Capacity];
|
|
||||||
_count = 0;
|
|
||||||
}
|
|
||||||
#endregion
|
|
||||||
|
|
||||||
#region Methods
|
|
||||||
public void Add(int entityID, entlong target)
|
|
||||||
{
|
|
||||||
#if (DEBUG && !DISABLE_DEBUG) || !DISABLE_DRAGONECS_ASSERT_CHEKS
|
|
||||||
if (_sanitizeTargetWorld > 0 && target.world != _sanitizeTargetWorld) ThrowWorldDifferent<T>(entityID);
|
|
||||||
_sanitizeTargetWorld = target.world;
|
|
||||||
if (Has(entityID)) ThrowAlreadyHasComponent<T>(entityID);
|
|
||||||
#endif
|
|
||||||
ref bool entityFlag = ref _entityFlags[entityID];
|
|
||||||
if (entityFlag == false)
|
|
||||||
{
|
|
||||||
entityFlag = true;
|
|
||||||
_count++;
|
|
||||||
_entities.Add(entityID);
|
|
||||||
foreach (var item in _listeners) item.OnAdd(entityID);
|
|
||||||
}
|
|
||||||
foreach (var item in _listeners) item.OnWrite(entityID);
|
|
||||||
_items[entityID].Target = target;
|
|
||||||
}
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
||||||
public void Set(int entityID, entlong target)
|
|
||||||
{
|
|
||||||
#if (DEBUG && !DISABLE_DEBUG) || !DISABLE_DRAGONECS_ASSERT_CHEKS
|
|
||||||
if (!Has(entityID)) ThrowNotHaveComponent<T>(entityID);
|
|
||||||
if (_sanitizeTargetWorld >= 0 && target.world != _sanitizeTargetWorld) ThrowWorldDifferent<T>(entityID);
|
|
||||||
_sanitizeTargetWorld = target.world;
|
|
||||||
#endif
|
|
||||||
_listeners.InvokeOnWrite(entityID);
|
|
||||||
_items[entityID].Target = target;
|
|
||||||
}
|
|
||||||
public void AddOrSet(int entityID, entlong target)
|
|
||||||
{
|
|
||||||
if (Has(entityID))
|
|
||||||
Set(entityID, target);
|
|
||||||
else
|
|
||||||
Add(entityID, target);
|
|
||||||
}
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
||||||
public ref readonly T Read(int entityID)
|
|
||||||
{
|
|
||||||
#if (DEBUG && !DISABLE_DEBUG) || !DISABLE_DRAGONECS_ASSERT_CHEKS
|
|
||||||
if (!Has(entityID)) ThrowNotHaveComponent<T>(entityID);
|
|
||||||
#endif
|
|
||||||
return ref _items[entityID];
|
|
||||||
}
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
||||||
public bool Has(int entityID)
|
|
||||||
{
|
|
||||||
return _entityFlags[entityID];
|
|
||||||
}
|
|
||||||
public void Del(int entityID)
|
|
||||||
{
|
|
||||||
#if (DEBUG && !DISABLE_DEBUG) || !DISABLE_DRAGONECS_ASSERT_CHEKS
|
|
||||||
if (!Has(entityID)) ThrowNotHaveComponent<T>(entityID);
|
|
||||||
#endif
|
|
||||||
_entities.Remove(entityID);
|
|
||||||
_entityFlags[entityID] = false;
|
|
||||||
_count--;
|
|
||||||
_listeners.InvokeOnDel(entityID);
|
|
||||||
}
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
||||||
public void TryDel(int entityID)
|
|
||||||
{
|
|
||||||
if (Has(entityID)) Del(entityID);
|
|
||||||
}
|
|
||||||
public void Copy(int fromEntityID, int toEntityID)
|
|
||||||
{
|
|
||||||
#if (DEBUG && !DISABLE_DEBUG) || !DISABLE_DRAGONECS_ASSERT_CHEKS
|
|
||||||
if (!Has(fromEntityID)) ThrowNotHaveComponent<T>(fromEntityID);
|
|
||||||
#endif
|
|
||||||
if (Has(toEntityID))
|
|
||||||
Set(toEntityID, Read(fromEntityID).Target);
|
|
||||||
else
|
|
||||||
Add(toEntityID, Read(fromEntityID).Target);
|
|
||||||
}
|
|
||||||
public void Copy(int fromEntityID, EcsWorld toWorld, int toEntityID)
|
|
||||||
{
|
|
||||||
#if (DEBUG && !DISABLE_DEBUG) || !DISABLE_DRAGONECS_ASSERT_CHEKS
|
|
||||||
if (!Has(fromEntityID)) ThrowNotHaveComponent<T>(fromEntityID);
|
|
||||||
#endif
|
|
||||||
if (Has(toEntityID))
|
|
||||||
toWorld.GetPool<T>().Set(toEntityID, Read(fromEntityID).Target);
|
|
||||||
else
|
|
||||||
toWorld.GetPool<T>().Add(toEntityID, Read(fromEntityID).Target);
|
|
||||||
}
|
|
||||||
#endregion
|
|
||||||
|
|
||||||
#region WorldCallbacks
|
|
||||||
void IEcsPoolImplementation.OnWorldResize(int newSize)
|
|
||||||
{
|
|
||||||
Array.Resize(ref _entityFlags, newSize);
|
|
||||||
Array.Resize(ref _items, newSize);
|
|
||||||
}
|
|
||||||
void IEcsPoolImplementation.OnWorldDestroy() { }
|
|
||||||
void IEcsPoolImplementation.OnReleaseDelEntityBuffer(ReadOnlySpan<int> buffer)
|
|
||||||
{
|
|
||||||
foreach (var item in buffer)
|
|
||||||
TryDel(item);
|
|
||||||
}
|
|
||||||
#endregion
|
|
||||||
|
|
||||||
#region Other
|
|
||||||
ref T IEcsPool<T>.Add(int entityID)
|
|
||||||
{
|
|
||||||
if (!Has(entityID))
|
|
||||||
Add(entityID, entlong.NULL);
|
|
||||||
return ref _items[entityID];
|
|
||||||
}
|
|
||||||
ref T IEcsPool<T>.Write(int entityID)
|
|
||||||
{
|
|
||||||
#if (DEBUG && !DISABLE_DEBUG) || !DISABLE_DRAGONECS_ASSERT_CHEKS
|
|
||||||
if (!Has(entityID)) ThrowNotHaveComponent<T>(entityID);
|
|
||||||
#endif
|
|
||||||
return ref _items[entityID];
|
|
||||||
}
|
|
||||||
void IEcsPool.AddRaw(int entityID, object dataRaw) => ((IEcsPool<T>)this).Add(entityID) = (T)dataRaw;
|
|
||||||
object IEcsPool.GetRaw(int entityID) => Read(entityID);
|
|
||||||
void IEcsPool.SetRaw(int entityID, object dataRaw) => ((IEcsPool<T>)this).Write(entityID) = (T)dataRaw;
|
|
||||||
#endregion
|
|
||||||
|
|
||||||
#region Listeners
|
|
||||||
public void AddListener(IEcsPoolEventListener listener)
|
|
||||||
{
|
|
||||||
if (listener == null) { throw new ArgumentNullException("listener is null"); }
|
|
||||||
_listeners.Add(listener);
|
|
||||||
}
|
|
||||||
public void RemoveListener(IEcsPoolEventListener listener)
|
|
||||||
{
|
|
||||||
if (listener == null) { throw new ArgumentNullException("listener is null"); }
|
|
||||||
_listeners.Remove(listener);
|
|
||||||
}
|
|
||||||
#endregion
|
|
||||||
|
|
||||||
#region IEnumerator - IntelliSense hack
|
|
||||||
IEnumerator<T> IEnumerable<T>.GetEnumerator() => throw new NotImplementedException();
|
|
||||||
IEnumerator IEnumerable.GetEnumerator() => throw new NotImplementedException();
|
|
||||||
#endregion
|
|
||||||
}
|
|
||||||
|
|
||||||
public interface IEcsAttachComponent
|
|
||||||
{
|
|
||||||
public entlong Target { get; set; }
|
|
||||||
}
|
|
||||||
public static class EcsAttachComponentPoolExt
|
|
||||||
{
|
|
||||||
public static EcsAttachPool<TAttachComponent> GetPool<TAttachComponent>(this EcsWorld self) where TAttachComponent : struct, IEcsAttachComponent
|
|
||||||
{
|
|
||||||
return self.GetPool<TAttachComponent, EcsAttachPool<TAttachComponent>>();
|
|
||||||
}
|
|
||||||
|
|
||||||
public static EcsAttachPool<TAttachComponent> Include<TAttachComponent>(this EcsSubjectBuilderBase self) where TAttachComponent : struct, IEcsAttachComponent
|
|
||||||
{
|
|
||||||
return self.Include<TAttachComponent, EcsAttachPool<TAttachComponent>>();
|
|
||||||
}
|
|
||||||
public static EcsAttachPool<TAttachComponent> Exclude<TAttachComponent>(this EcsSubjectBuilderBase self) where TAttachComponent : struct, IEcsAttachComponent
|
|
||||||
{
|
|
||||||
return self.Exclude<TAttachComponent, EcsAttachPool<TAttachComponent>>();
|
|
||||||
}
|
|
||||||
public static EcsAttachPool<TAttachComponent> Optional<TAttachComponent>(this EcsSubjectBuilderBase self) where TAttachComponent : struct, IEcsAttachComponent
|
|
||||||
{
|
|
||||||
return self.Optional<TAttachComponent, EcsAttachPool<TAttachComponent>>();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -7,7 +7,7 @@ namespace DCFApixels.DragonECS
|
|||||||
{
|
{
|
||||||
using static EcsPoolThrowHalper;
|
using static EcsPoolThrowHalper;
|
||||||
/// <summary>Pool for IEcsComponent components</summary>
|
/// <summary>Pool for IEcsComponent components</summary>
|
||||||
public sealed class EcsPool<T> : IEcsPoolImplementation<T>, IEnumerable<T> //IntelliSense hack
|
public sealed class EcsPool<T> : IEcsPoolImplementation<T>, IEnumerable<T> //IEnumerable<T> - IntelliSense hack
|
||||||
where T : struct, IEcsComponent
|
where T : struct, IEcsComponent
|
||||||
{
|
{
|
||||||
private EcsWorld _source;
|
private EcsWorld _source;
|
||||||
|
@ -6,7 +6,7 @@ using System.Runtime.CompilerServices;
|
|||||||
namespace DCFApixels.DragonECS
|
namespace DCFApixels.DragonECS
|
||||||
{
|
{
|
||||||
using static EcsPoolThrowHalper;
|
using static EcsPoolThrowHalper;
|
||||||
public sealed class EcsTagPool<T> : IEcsPoolImplementation<T>, IEnumerable<T> //IntelliSense hack
|
public sealed class EcsTagPool<T> : IEcsPoolImplementation<T>, IEnumerable<T> //IEnumerable<T> - IntelliSense hack
|
||||||
where T : struct, IEcsTagComponent
|
where T : struct, IEcsTagComponent
|
||||||
{
|
{
|
||||||
private EcsWorld _source;
|
private EcsWorld _source;
|
||||||
|
@ -5,7 +5,7 @@ namespace DCFApixels.DragonECS
|
|||||||
{
|
{
|
||||||
public class EntityLinkedList
|
public class EntityLinkedList
|
||||||
{
|
{
|
||||||
public const int Enter = 0;
|
private const int ENTER = 0;
|
||||||
|
|
||||||
private Node[] _nodes;
|
private Node[] _nodes;
|
||||||
private int _count;
|
private int _count;
|
||||||
@ -35,7 +35,7 @@ namespace DCFApixels.DragonECS
|
|||||||
//ArrayUtility.Fill(_nodes, Node.Empty);
|
//ArrayUtility.Fill(_nodes, Node.Empty);
|
||||||
for (int i = 0; i < _nodes.Length; i++)
|
for (int i = 0; i < _nodes.Length; i++)
|
||||||
_nodes[i].next = 0;
|
_nodes[i].next = 0;
|
||||||
_lastNodeIndex = Enter;
|
_lastNodeIndex = ENTER;
|
||||||
_count = 0;
|
_count = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -82,7 +82,7 @@ namespace DCFApixels.DragonECS
|
|||||||
{
|
{
|
||||||
_nodes = nodes;
|
_nodes = nodes;
|
||||||
_index = -1;
|
_index = -1;
|
||||||
_next = Enter;
|
_next = ENTER;
|
||||||
}
|
}
|
||||||
public int Current => _nodes[_index].entityID;
|
public int Current => _nodes[_index].entityID;
|
||||||
public bool MoveNext()
|
public bool MoveNext()
|
||||||
|
Loading…
Reference in New Issue
Block a user