DragonECS-Graphs/src/EcsWorldGraph.cs

160 lines
5.2 KiB
C#
Raw Normal View History

2023-12-12 00:14:28 +08:00
using DCFApixels.DragonECS.Relations.Internal;
using DCFApixels.DragonECS.Relations.Utils;
2023-06-29 14:25:51 +08:00
using System;
2024-01-26 21:56:03 +08:00
namespace DCFApixels.DragonECS
2023-06-29 14:25:51 +08:00
{
2024-01-29 17:40:38 +08:00
public static class EcsWorldGraph
2023-06-29 14:25:51 +08:00
{
2023-12-25 08:59:00 +08:00
private static readonly SparseArray64<EcsArc> _matrix = new SparseArray64<EcsArc>(4);
2024-01-26 21:56:03 +08:00
private static EcsArc[] _arcsMapping = new EcsArc[4];
2023-06-29 14:25:51 +08:00
2024-01-29 17:40:38 +08:00
#region Register/Unregister
2024-01-26 21:56:03 +08:00
private static EcsArc Register(EcsWorld startWorld, EcsWorld endWorld, EcsArcWorld arcWorld)
2023-06-29 14:25:51 +08:00
{
2023-12-25 08:59:00 +08:00
int startWorldID = startWorld.id;
int endWorldID = endWorld.id;
2024-01-26 21:56:03 +08:00
int arcWorldID = arcWorld.id;
if(_arcsMapping.Length <= arcWorldID)
{
Array.Resize(ref _arcsMapping, arcWorldID + 4);
}
2023-06-29 14:25:51 +08:00
#if DEBUG
2023-12-25 08:59:00 +08:00
if (_matrix.Contains(startWorldID, endWorldID))
2024-01-26 21:56:03 +08:00
{
2023-06-29 14:25:51 +08:00
throw new EcsFrameworkException();
2024-01-26 21:56:03 +08:00
}
2023-06-29 14:25:51 +08:00
#endif
2024-01-26 21:56:03 +08:00
EcsArc arc = new EcsArc(startWorld, endWorld, arcWorld);
_matrix[startWorldID, endWorldID] = arc;
_arcsMapping[arcWorldID] = arc;
return arc;
2023-06-29 14:25:51 +08:00
}
2024-01-26 21:56:03 +08:00
private static void Unregister(EcsWorld startWorld, EcsWorld endWorld)
2023-06-29 14:25:51 +08:00
{
2023-12-25 08:59:00 +08:00
int startWorldID = startWorld.id;
int endWorldID = endWorld.id;
2024-01-26 21:56:03 +08:00
EcsArc arc = _matrix[startWorldID, endWorldID];
_arcsMapping[arc.ArcWorld.id] = null;
2023-12-25 08:59:00 +08:00
_matrix.Remove(startWorldID, endWorldID);
2023-06-29 14:25:51 +08:00
}
2024-01-29 17:40:38 +08:00
#endregion
2023-06-29 14:25:51 +08:00
2024-01-29 17:40:38 +08:00
#region Get/Has
// private static EcsArc GetOrRigister(EcsWorld startWorld, EcsWorld otherWorld)
// {
//#if DEBUG
// if (!_matrix.Contains(startWorld.id, otherWorld.id))
// {
// return Register();
// }
//#endif
// return _matrix[startWorld.id, otherWorld.id];
// }
2024-01-26 21:56:03 +08:00
private static EcsArc Get(EcsWorld startWorld, EcsWorld otherWorld)
2023-06-29 14:25:51 +08:00
{
#if DEBUG
2023-12-25 08:59:00 +08:00
if (!_matrix.Contains(startWorld.id, otherWorld.id))
2024-01-26 21:56:03 +08:00
{
2023-06-29 14:25:51 +08:00
throw new EcsFrameworkException();
2024-01-26 21:56:03 +08:00
}
2023-06-29 14:25:51 +08:00
#endif
2023-12-25 08:59:00 +08:00
return _matrix[startWorld.id, otherWorld.id];
2023-06-29 14:25:51 +08:00
}
2024-01-26 21:56:03 +08:00
private static bool Has(EcsWorld startWorld, EcsWorld endWorld) => Has(startWorld.id, endWorld.id);
private static bool Has(int startWorldID, int endWorldID) => _matrix.Contains(startWorldID, endWorldID);
2024-01-29 17:40:38 +08:00
#endregion
2024-01-26 21:56:03 +08:00
#region Extension
public static bool IsRegistered(this EcsArcWorld self)
{
if (self == null)
{
Throw.ArgumentNull();
}
int id = self.id;
return id < _arcsMapping.Length && _arcsMapping[self.id] != null;
}
public static EcsArc GetRegisteredArc(this EcsArcWorld self)
{
if (self == null)
{
Throw.ArgumentNull();
}
int id = self.id;
if(id < _arcsMapping.Length && _arcsMapping[self.id] == null)
{
throw new Exception();
}
return _arcsMapping[self.id];
}
2024-01-29 17:40:38 +08:00
public static EcsArc SetLoopArcAuto(this EcsWorld self) => SetArcAuto(self, self);
public static EcsArc SetArcAuto(this EcsWorld start, EcsWorld end)
2023-06-29 14:25:51 +08:00
{
2024-01-26 21:56:03 +08:00
if (start == null || end == null)
{
Throw.ArgumentNull();
}
return Register(start, end, new EcsArcWorld());
2023-06-29 14:25:51 +08:00
}
2023-12-25 08:59:00 +08:00
2024-01-26 21:56:03 +08:00
public static EcsArc SetLoopArc(this EcsWorld self, EcsArcWorld arc) => SetArc(self, self, arc);
public static EcsArc SetArc(this EcsWorld start, EcsWorld end, EcsArcWorld arc)
2023-12-25 08:59:00 +08:00
{
2024-01-26 21:56:03 +08:00
if (start == null || end == null || arc == null)
{
Throw.ArgumentNull();
}
return Register(start, end, arc);
2023-12-25 08:59:00 +08:00
}
2024-01-26 21:56:03 +08:00
public static bool HasLoopArc(this EcsWorld self) => HasArc(self, self);
public static bool HasArc(this EcsWorld start, EcsWorld end)
2023-06-29 14:25:51 +08:00
{
2024-01-26 21:56:03 +08:00
if (start == null || end == null)
{
Throw.ArgumentNull();
}
return Has(start, end);
2023-06-29 14:25:51 +08:00
}
2024-01-26 21:56:03 +08:00
public static EcsArc GetLoopArc(this EcsWorld self) => GetArc(self, self);
public static EcsArc GetArc(this EcsWorld start, EcsWorld end)
2023-06-29 14:25:51 +08:00
{
2024-01-26 21:56:03 +08:00
if (start == null || end == null)
{
Throw.ArgumentNull();
}
return Get(start, end);
2023-06-29 14:25:51 +08:00
}
2024-01-26 21:56:03 +08:00
public static bool TryGetLoopArc(this EcsWorld self, out EcsArc arc) => TryGetArc(self, self, out arc);
public static bool TryGetArc(this EcsWorld start, EcsWorld end, out EcsArc arc)
2023-06-29 14:25:51 +08:00
{
2024-01-26 21:56:03 +08:00
if (start == null || end == null)
{
Throw.ArgumentNull();
}
bool result = Has(start, end);
arc = result ? Get(start, end) : null;
2023-12-25 08:59:00 +08:00
return result;
2023-06-29 14:25:51 +08:00
}
2023-12-25 08:59:00 +08:00
public static void DestroyLoopArc(this EcsWorld self) => DestroyArcWith(self, self);
2024-01-26 21:56:03 +08:00
public static void DestroyArcWith(this EcsWorld start, EcsWorld end)
2023-06-29 14:25:51 +08:00
{
2024-01-26 21:56:03 +08:00
if (start == null || end == null)
{
Throw.ArgumentNull();
}
Get(start, end).ArcWorld.Destroy();
Unregister(start, end);
2023-06-29 14:25:51 +08:00
}
2024-01-26 21:56:03 +08:00
#endregion
2023-06-29 14:25:51 +08:00
}
2024-01-26 21:56:03 +08:00
}