From 3b3f009fa6c5492c8293367eec9f821f586a3ba4 Mon Sep 17 00:00:00 2001 From: Mikhail <99481254+DCFApixels@users.noreply.github.com> Date: Sun, 7 Jan 2024 23:19:03 +0800 Subject: [PATCH] update --- src/EcsArc.cs | 101 +++++++----------- src/EcsJoinGroup.cs | 1 - src/{Builtin => Executors}/EcsJoinExecutor.cs | 0 src/Utils/ArrayUtility.cs | 6 +- 4 files changed, 40 insertions(+), 68 deletions(-) rename src/{Builtin => Executors}/EcsJoinExecutor.cs (100%) diff --git a/src/EcsArc.cs b/src/EcsArc.cs index 6b815df..996b3bc 100644 --- a/src/EcsArc.cs +++ b/src/EcsArc.cs @@ -1,6 +1,5 @@ using DCFApixels.DragonECS.Relations.Utils; using System; -using System.Collections.Generic; using System.Runtime.CompilerServices; namespace DCFApixels.DragonECS @@ -8,45 +7,41 @@ namespace DCFApixels.DragonECS //Edge world //Relation entity //Relation component - - public class EcsEdge - { - - } public class EcsArc : IEcsWorldEventListener, IEcsEntityEventListener { private readonly EcsWorld _startWorld; private readonly EcsWorld _endWorld; private readonly EcsArcWorld _arcWorld; - private readonly VertexWorldHandler _worldHandler; - - private readonly IdsBasket _basket = new IdsBasket(256); + private readonly VertexWorldHandler _startWorldHandler; private readonly SparseArray64 _relationsMatrix = new SparseArray64(); + private EcsGroup _arcEntities; private ArcEntityInfo[] _arkEntityInfos; //N * (N - 1) / 2 #region Properties public EcsWorld StartWorld => _startWorld; public EcsWorld EndWorld => _endWorld; public EcsArcWorld ArcWorld => _arcWorld; - + public EcsReadonlyGroup ArcEntities => _arcEntities.Readonly; public bool IsLoop => _startWorld == _endWorld; #endregion #region Constructors - internal EcsArc(EcsWorld world, EcsWorld otherWorld, EcsArcWorld arcWorld) + internal EcsArc(EcsWorld startWorld, EcsWorld endWorld, EcsArcWorld arcWorld) { + _startWorld = startWorld; + _endWorld = endWorld; _arcWorld = arcWorld; - _startWorld = world; - _endWorld = otherWorld; - - _worldHandler = new VertexWorldHandler(this, _startWorld, _basket); - _startWorld.AddListener(_worldHandler); _arkEntityInfos = new ArcEntityInfo[arcWorld.Capacity]; + _startWorldHandler = new VertexWorldHandler(this, _startWorld); + + _startWorld.AddListener(worldEventListener: _startWorldHandler); + _startWorld.AddListener(entityEventListener: _startWorldHandler); + _arcWorld.AddListener(worldEventListener: this); _arcWorld.AddListener(entityEventListener: this); } @@ -57,20 +52,24 @@ namespace DCFApixels.DragonECS { if (Has(startEntityID, endEntityID)) throw new EcsRelationException(); + int arcEntity = _arcWorld.NewEntity(); - _basket.AddToHead(startEntityID, endEntityID); _relationsMatrix.Add(startEntityID, endEntityID, arcEntity); + _arkEntityInfos[arcEntity] = new ArcEntityInfo(startEntityID, endEntityID); + _arcEntities.Add(arcEntity); return arcEntity; } 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(); + _relationsMatrix.Remove(startEntityID, endEntityID); - _basket.DelHead(startEntityID); - _arcWorld.DelEntity(e); - _arkEntityInfos[e] = ArcEntityInfo.Empty; + _arcWorld.DelEntity(arcEntity); + + _arkEntityInfos[arcEntity] = ArcEntityInfo.Empty; + _arcEntities.Remove(arcEntity); } #endregion @@ -79,40 +78,16 @@ namespace DCFApixels.DragonECS { 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) { - if (!_relationsMatrix.TryGetValue(startEntityID, endEntityID, out int e)) + if (!_relationsMatrix.TryGetValue(startEntityID, endEntityID, out int arcEntityID)) throw new EcsRelationException(); - return e; + return arcEntityID; } public bool TryGet(int startEntityID, int endEntityID, out int 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 временная заглушка, потому тут будет спан из линкедлиста - ////public ReadOnlySpan GetRelationsWith(EcsSubject subject, int entityID) - ////{ - //// if (subject.World != _relationWorld) - //// throw new ArgumentException(); - //// throw new NotImplementedException(); - ////} - //#endregion #endregion #region ArcEntityInfo @@ -151,11 +126,6 @@ namespace DCFApixels.DragonECS { 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 #region Callbacks @@ -163,7 +133,10 @@ namespace DCFApixels.DragonECS { Array.Resize(ref _arkEntityInfos, newSize); } - void IEcsWorldEventListener.OnReleaseDelEntityBuffer(ReadOnlySpan buffer) { } + void IEcsWorldEventListener.OnReleaseDelEntityBuffer(ReadOnlySpan buffer) + { + _arcWorld.ReleaseDelEntityBuffer(buffer.Length); + } void IEcsWorldEventListener.OnWorldDestroy() { } void IEcsEntityEventListener.OnNewEntity(int entityID) { } @@ -176,30 +149,34 @@ namespace DCFApixels.DragonECS #endregion #region VertexWorldHandler - private class VertexWorldHandler : IEcsEntityEventListener + private class VertexWorldHandler : IEcsWorldEventListener, IEcsEntityEventListener { private readonly EcsArc _source; private readonly EcsWorld _world; - private readonly IdsBasket _basket; - public VertexWorldHandler(EcsArc source, EcsWorld world, IdsBasket basket) + public VertexWorldHandler(EcsArc source, EcsWorld world) { _source = source; _world = world; - _basket = basket; } + #region Callbacks public void OnDelEntity(int entityID) { - var span = _basket.GetSpanFor(entityID); - foreach (var arcEntityID in span) - { - } } public void OnNewEntity(int entityID) { - } + public void OnReleaseDelEntityBuffer(ReadOnlySpan buffer) + { + } + public void OnWorldDestroy() + { + } + public void OnWorldResize(int newSize) + { + } + #endregion } #endregion } diff --git a/src/EcsJoinGroup.cs b/src/EcsJoinGroup.cs index 554adf9..2dea2d5 100644 --- a/src/EcsJoinGroup.cs +++ b/src/EcsJoinGroup.cs @@ -1,5 +1,4 @@ using DCFApixels.DragonECS.Relations.Utils; -using System.Runtime.CompilerServices; namespace DCFApixels.DragonECS { diff --git a/src/Builtin/EcsJoinExecutor.cs b/src/Executors/EcsJoinExecutor.cs similarity index 100% rename from src/Builtin/EcsJoinExecutor.cs rename to src/Executors/EcsJoinExecutor.cs diff --git a/src/Utils/ArrayUtility.cs b/src/Utils/ArrayUtility.cs index 7d15f07..19d26b1 100644 --- a/src/Utils/ArrayUtility.cs +++ b/src/Utils/ArrayUtility.cs @@ -1,8 +1,4 @@ -using System.Runtime.InteropServices; -using System; -using System.Runtime.CompilerServices; - -namespace DCFApixels.DragonECS.Relations.Utils +namespace DCFApixels.DragonECS.Relations.Utils { internal static class ArrayUtility {