DragonECS-Graphs/src/WorldGraph.cs

99 lines
4.0 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;
2023-12-12 00:14:28 +08:00
namespace DCFApixels.DragonECS.Relations.Internal
2023-06-29 14:25:51 +08:00
{
internal static class WorldGraph
{
2023-12-25 08:59:00 +08:00
private static readonly SparseArray64<EcsArc> _matrix = new SparseArray64<EcsArc>(4);
2023-06-29 14:25:51 +08:00
2023-12-25 08:59:00 +08:00
internal static EcsArc Register(EcsWorld startWorld, EcsWorld endWorld, EcsArcWorld edgeWorld)
2023-06-29 14:25:51 +08:00
{
2023-12-25 08:59:00 +08:00
int startWorldID = startWorld.id;
int endWorldID = endWorld.id;
2023-06-29 14:25:51 +08:00
#if DEBUG
2023-12-25 08:59:00 +08:00
if (_matrix.Contains(startWorldID, endWorldID))
2023-06-29 14:25:51 +08:00
throw new EcsFrameworkException();
#endif
2023-12-25 08:59:00 +08:00
EcsArc edge = new EcsArc(startWorld, endWorld, edgeWorld);
_matrix[startWorldID, endWorldID] = edge;
2023-06-29 14:25:51 +08:00
return edge;
}
2023-12-25 08:59:00 +08:00
internal 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;
_matrix.Remove(startWorldID, endWorldID);
2023-06-29 14:25:51 +08:00
}
2023-12-25 08:59:00 +08:00
internal 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))
2023-06-29 14:25:51 +08:00
throw new EcsFrameworkException();
#endif
2023-12-25 08:59:00 +08:00
return _matrix[startWorld.id, otherWorld.id];
2023-06-29 14:25:51 +08:00
}
2023-12-25 08:59:00 +08:00
internal static bool HasArc(EcsWorld startWorld, EcsWorld endWorld) => HasArc(startWorld.id, endWorld.id);
internal static bool HasArc(int startWorldID, int endWorldID) => _matrix.Contains(startWorldID, endWorldID);
2023-06-29 14:25:51 +08:00
}
2023-12-12 00:14:28 +08:00
}
2023-06-29 14:25:51 +08:00
2023-12-12 00:14:28 +08:00
namespace DCFApixels.DragonECS
2023-12-24 18:18:49 +08:00
{
2023-06-29 14:25:51 +08:00
public static class WorldGraphExtensions
{
2023-12-25 08:59:00 +08:00
public static EcsArc SetLoopArc(this EcsWorld self) => SetArcWith(self, self);
public static EcsArc SetArcWith(this EcsWorld self, EcsWorld endWorld)
2023-06-29 14:25:51 +08:00
{
2023-12-25 08:59:00 +08:00
if (self == null || endWorld == null)
2023-06-29 14:25:51 +08:00
throw new ArgumentNullException();
2023-12-25 08:59:00 +08:00
return WorldGraph.Register(self, endWorld, new EcsArcWorld());
2023-06-29 14:25:51 +08:00
}
2023-12-25 08:59:00 +08:00
public static EcsArc SetLoopArc(this EcsWorld self, EcsArcWorld edgeWorld) => SetEdgeWith(self, self, edgeWorld);
public static EcsArc SetEdgeWith(this EcsWorld startWorld, EcsWorld endWorld, EcsArcWorld edgeWorld)
{
if (startWorld == null || endWorld == null || edgeWorld == null)
throw new ArgumentNullException();
return WorldGraph.Register(startWorld, endWorld, edgeWorld);
}
public static bool HasLoopArc(this EcsWorld self) => HasArcWith(self, self);
public static bool HasArcWith(this EcsWorld startWorld, EcsWorld endWorld)
2023-06-29 14:25:51 +08:00
{
2023-12-25 08:59:00 +08:00
if (startWorld == null || endWorld == null)
2023-06-29 14:25:51 +08:00
throw new ArgumentNullException();
2023-12-25 08:59:00 +08:00
return WorldGraph.HasArc(startWorld, endWorld);
2023-06-29 14:25:51 +08:00
}
2023-12-25 08:59:00 +08:00
public static EcsArc GetLoopArc(this EcsWorld self) => GetArcWith(self, self);
public static EcsArc GetArcWith(this EcsWorld startWorld, EcsWorld endWorld)
2023-06-29 14:25:51 +08:00
{
2023-12-25 08:59:00 +08:00
if (startWorld == null || endWorld == null)
2023-06-29 14:25:51 +08:00
throw new ArgumentNullException();
2023-12-25 08:59:00 +08:00
return WorldGraph.Get(startWorld, endWorld);
2023-06-29 14:25:51 +08:00
}
2023-12-25 08:59:00 +08:00
public static bool TryGetLoopArc(this EcsWorld self, out EcsArc arc) => TryGetArcWith(self, self, out arc);
public static bool TryGetArcWith(this EcsWorld startWorld, EcsWorld endWorld, out EcsArc arc)
2023-06-29 14:25:51 +08:00
{
2023-12-25 08:59:00 +08:00
if (startWorld == null || endWorld == null)
2023-06-29 14:25:51 +08:00
throw new ArgumentNullException();
2023-12-25 08:59:00 +08:00
bool result = WorldGraph.HasArc(startWorld, endWorld);
arc = result ? WorldGraph.Get(startWorld, endWorld) : null;
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);
public static void DestroyArcWith(this EcsWorld startWorld, EcsWorld endWorld)
2023-06-29 14:25:51 +08:00
{
2023-12-25 08:59:00 +08:00
if (startWorld == null || endWorld == null)
2023-06-29 14:25:51 +08:00
throw new ArgumentNullException();
2023-12-25 08:59:00 +08:00
WorldGraph.Get(startWorld, endWorld).ArcWorld.Destroy();
WorldGraph.Unregister(startWorld, endWorld);
2023-06-29 14:25:51 +08:00
}
}
}