This commit is contained in:
Mikhail 2024-01-07 23:19:03 +08:00
parent 970f22510b
commit 3b3f009fa6
4 changed files with 40 additions and 68 deletions

View File

@ -1,6 +1,5 @@
using DCFApixels.DragonECS.Relations.Utils; using DCFApixels.DragonECS.Relations.Utils;
using System; using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
namespace DCFApixels.DragonECS namespace DCFApixels.DragonECS
@ -8,45 +7,41 @@ namespace DCFApixels.DragonECS
//Edge world //Edge world
//Relation entity //Relation entity
//Relation component //Relation component
public class EcsEdge
{
}
public class EcsArc : IEcsWorldEventListener, IEcsEntityEventListener public class EcsArc : IEcsWorldEventListener, IEcsEntityEventListener
{ {
private readonly EcsWorld _startWorld; private readonly EcsWorld _startWorld;
private readonly EcsWorld _endWorld; private readonly EcsWorld _endWorld;
private readonly EcsArcWorld _arcWorld; private readonly EcsArcWorld _arcWorld;
private readonly VertexWorldHandler _worldHandler; private readonly VertexWorldHandler _startWorldHandler;
private readonly IdsBasket _basket = new IdsBasket(256);
private readonly SparseArray64<int> _relationsMatrix = new SparseArray64<int>(); private readonly SparseArray64<int> _relationsMatrix = new SparseArray64<int>();
private EcsGroup _arcEntities;
private ArcEntityInfo[] _arkEntityInfos; //N * (N - 1) / 2 private ArcEntityInfo[] _arkEntityInfos; //N * (N - 1) / 2
#region Properties #region Properties
public EcsWorld StartWorld => _startWorld; public EcsWorld StartWorld => _startWorld;
public EcsWorld EndWorld => _endWorld; public EcsWorld EndWorld => _endWorld;
public EcsArcWorld ArcWorld => _arcWorld; public EcsArcWorld ArcWorld => _arcWorld;
public EcsReadonlyGroup ArcEntities => _arcEntities.Readonly;
public bool IsLoop => _startWorld == _endWorld; public bool IsLoop => _startWorld == _endWorld;
#endregion #endregion
#region Constructors #region Constructors
internal EcsArc(EcsWorld world, EcsWorld otherWorld, EcsArcWorld arcWorld) internal EcsArc(EcsWorld startWorld, EcsWorld endWorld, EcsArcWorld arcWorld)
{ {
_startWorld = startWorld;
_endWorld = endWorld;
_arcWorld = arcWorld; _arcWorld = arcWorld;
_startWorld = world;
_endWorld = otherWorld;
_worldHandler = new VertexWorldHandler(this, _startWorld, _basket);
_startWorld.AddListener(_worldHandler);
_arkEntityInfos = new ArcEntityInfo[arcWorld.Capacity]; _arkEntityInfos = new ArcEntityInfo[arcWorld.Capacity];
_startWorldHandler = new VertexWorldHandler(this, _startWorld);
_startWorld.AddListener(worldEventListener: _startWorldHandler);
_startWorld.AddListener(entityEventListener: _startWorldHandler);
_arcWorld.AddListener(worldEventListener: this); _arcWorld.AddListener(worldEventListener: this);
_arcWorld.AddListener(entityEventListener: this); _arcWorld.AddListener(entityEventListener: this);
} }
@ -57,20 +52,24 @@ namespace DCFApixels.DragonECS
{ {
if (Has(startEntityID, endEntityID)) if (Has(startEntityID, endEntityID))
throw new EcsRelationException(); throw new EcsRelationException();
int arcEntity = _arcWorld.NewEntity(); int arcEntity = _arcWorld.NewEntity();
_basket.AddToHead(startEntityID, endEntityID);
_relationsMatrix.Add(startEntityID, endEntityID, arcEntity); _relationsMatrix.Add(startEntityID, endEntityID, arcEntity);
_arkEntityInfos[arcEntity] = new ArcEntityInfo(startEntityID, endEntityID); _arkEntityInfos[arcEntity] = new ArcEntityInfo(startEntityID, endEntityID);
_arcEntities.Add(arcEntity);
return arcEntity; return arcEntity;
} }
public void Del(int startEntityID, int endEntityID) public void Del(int startEntityID, int endEntityID)
{ {
if (!_relationsMatrix.TryGetValue(startEntityID, endEntityID, out int e)) if (!_relationsMatrix.TryGetValue(startEntityID, endEntityID, out int arcEntity))
throw new EcsRelationException(); throw new EcsRelationException();
_relationsMatrix.Remove(startEntityID, endEntityID); _relationsMatrix.Remove(startEntityID, endEntityID);
_basket.DelHead(startEntityID); _arcWorld.DelEntity(arcEntity);
_arcWorld.DelEntity(e);
_arkEntityInfos[e] = ArcEntityInfo.Empty; _arkEntityInfos[arcEntity] = ArcEntityInfo.Empty;
_arcEntities.Remove(arcEntity);
} }
#endregion #endregion
@ -79,40 +78,16 @@ namespace DCFApixels.DragonECS
{ {
return _relationsMatrix.Contains(startEntityID, endEntityID); return _relationsMatrix.Contains(startEntityID, endEntityID);
} }
//public bool HasRelationWith(EcsSubject subject, int entityID, int otherEntityID)
//{
// if (subject.World != _relationWorld)
// throw new ArgumentException();
// return _source._relationsMatrix.TryGetValue(entityID, otherEntityID, out int entity) && subject.IsMatches(entity);
//}
public int Get(int startEntityID, int endEntityID) public int Get(int startEntityID, int endEntityID)
{ {
if (!_relationsMatrix.TryGetValue(startEntityID, endEntityID, out int e)) if (!_relationsMatrix.TryGetValue(startEntityID, endEntityID, out int arcEntityID))
throw new EcsRelationException(); throw new EcsRelationException();
return e; return arcEntityID;
} }
public bool TryGet(int startEntityID, int endEntityID, out int arcEntityID) public bool TryGet(int startEntityID, int endEntityID, out int arcEntityID)
{ {
return _relationsMatrix.TryGetValue(startEntityID, endEntityID, out arcEntityID); return _relationsMatrix.TryGetValue(startEntityID, endEntityID, out arcEntityID);
} }
//public bool TryGetRelation(EcsSubject subject, int entityID, int otherEntityID, out int entity)
//{
// return _source._relationsMatrix.TryGetValue(entityID, otherEntityID, out entity) && subject.IsMatches(entity);
//}
//#region GetRelations
//private IdsLinkedList.Span GetRelations(int entityID)
//{
// return _basket.GetSpanFor(entityID);
//}
////ReadOnlySpan<int> временная заглушка, потому тут будет спан из линкедлиста
////public ReadOnlySpan<int> GetRelationsWith(EcsSubject subject, int entityID)
////{
//// if (subject.World != _relationWorld)
//// throw new ArgumentException();
//// throw new NotImplementedException();
////}
//#endregion
#endregion #endregion
#region ArcEntityInfo #region ArcEntityInfo
@ -151,11 +126,6 @@ namespace DCFApixels.DragonECS
{ {
return _endWorld.TryGetArcWith(_startWorld, out arc); return _endWorld.TryGetArcWith(_startWorld, out arc);
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public IdsLinkedList.Span Get(int entityID) => _basket.GetSpanFor(entityID);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public IdsLinkedList.LongSpan GetLongs(int entityID) => _basket.GetLongSpanFor(_startWorld, entityID);
#endregion #endregion
#region Callbacks #region Callbacks
@ -163,7 +133,10 @@ namespace DCFApixels.DragonECS
{ {
Array.Resize(ref _arkEntityInfos, newSize); Array.Resize(ref _arkEntityInfos, newSize);
} }
void IEcsWorldEventListener.OnReleaseDelEntityBuffer(ReadOnlySpan<int> buffer) { } void IEcsWorldEventListener.OnReleaseDelEntityBuffer(ReadOnlySpan<int> buffer)
{
_arcWorld.ReleaseDelEntityBuffer(buffer.Length);
}
void IEcsWorldEventListener.OnWorldDestroy() { } void IEcsWorldEventListener.OnWorldDestroy() { }
void IEcsEntityEventListener.OnNewEntity(int entityID) { } void IEcsEntityEventListener.OnNewEntity(int entityID) { }
@ -176,30 +149,34 @@ namespace DCFApixels.DragonECS
#endregion #endregion
#region VertexWorldHandler #region VertexWorldHandler
private class VertexWorldHandler : IEcsEntityEventListener private class VertexWorldHandler : IEcsWorldEventListener, IEcsEntityEventListener
{ {
private readonly EcsArc _source; private readonly EcsArc _source;
private readonly EcsWorld _world; private readonly EcsWorld _world;
private readonly IdsBasket _basket;
public VertexWorldHandler(EcsArc source, EcsWorld world, IdsBasket basket) public VertexWorldHandler(EcsArc source, EcsWorld world)
{ {
_source = source; _source = source;
_world = world; _world = world;
_basket = basket;
} }
#region Callbacks
public void OnDelEntity(int entityID) public void OnDelEntity(int entityID)
{ {
var span = _basket.GetSpanFor(entityID);
foreach (var arcEntityID in span)
{
}
} }
public void OnNewEntity(int entityID) public void OnNewEntity(int entityID)
{ {
} }
public void OnReleaseDelEntityBuffer(ReadOnlySpan<int> buffer)
{
}
public void OnWorldDestroy()
{
}
public void OnWorldResize(int newSize)
{
}
#endregion
} }
#endregion #endregion
} }

View File

@ -1,5 +1,4 @@
using DCFApixels.DragonECS.Relations.Utils; using DCFApixels.DragonECS.Relations.Utils;
using System.Runtime.CompilerServices;
namespace DCFApixels.DragonECS namespace DCFApixels.DragonECS
{ {

View File

@ -1,8 +1,4 @@
using System.Runtime.InteropServices; namespace DCFApixels.DragonECS.Relations.Utils
using System;
using System.Runtime.CompilerServices;
namespace DCFApixels.DragonECS.Relations.Utils
{ {
internal static class ArrayUtility internal static class ArrayUtility
{ {