DragonECS-Graphs/src/WorldGraph.cs

86 lines
3.4 KiB
C#
Raw Normal View History

2023-06-29 14:25:51 +08:00
using DCFApixels.DragonECS.Relations.Utils;
using System;
namespace DCFApixels.DragonECS
{
internal static class WorldGraph
{
private static SparseArray64<EcsEdge> _matrix = new SparseArray64<EcsEdge>(4);
internal static EcsEdge Register(EcsWorld world, EcsWorld otherWorld, EcsEdgeWorld edgeWorld)
{
int worldID = world.id;
int otherWorldID = otherWorld.id;
#if DEBUG
if (_matrix.Contains(worldID, otherWorldID))
throw new EcsFrameworkException();
#endif
EcsEdge edge = new EcsEdge(world, otherWorld, edgeWorld);
_matrix[worldID, otherWorldID] = edge;
2023-07-04 05:10:37 +08:00
_matrix[otherWorldID, worldID] = edge;
2023-06-29 14:25:51 +08:00
return edge;
}
internal static void Unregister(EcsWorld world, EcsWorld otherWorld)
{
int worldID = world.id;
int otherWorldID = otherWorld.id;
//var manager = _matrix[worldID, otherWorldID];
_matrix.Remove(worldID, otherWorldID);
2023-07-04 05:10:37 +08:00
_matrix.Remove(otherWorldID, worldID);
2023-06-29 14:25:51 +08:00
}
internal static EcsEdge Get(EcsWorld world, EcsWorld otherWorld)
{
#if DEBUG
if (!_matrix.Contains(world.id, otherWorld.id))
throw new EcsFrameworkException();
#endif
return _matrix[world.id, otherWorld.id];
}
internal static bool HasEdge(EcsWorld world, EcsWorld otherWorld) => HasEdge(world.id, otherWorld.id);
internal static bool HasEdge(int worldID, int otherWorldID) => _matrix.Contains(worldID, otherWorldID);
}
public static class WorldGraphExtensions
{
public static void SetEdgeWithSelf(this EcsWorld self) => SetEdgeWith(self, self);
public static void SetEdgeWith(this EcsWorld self, EcsWorld otherWorld)
{
if (self == null || otherWorld == null)
throw new ArgumentNullException();
WorldGraph.Register(self, otherWorld, new EcsEdgeWorld());
}
public static void SetEdgeWithSelf(this EcsWorld self, EcsEdgeWorld relationWorld) => SetEdgeWith(self, self, relationWorld);
public static void SetEdgeWith(this EcsWorld self, EcsWorld otherWorld, EcsEdgeWorld edgeWorld)
{
if (self == null || otherWorld == null || edgeWorld == null)
throw new ArgumentNullException();
WorldGraph.Register(self, otherWorld, edgeWorld);
}
public static void HasEdgeWithSelf(this EcsWorld self) => HasEdgeWith(self, self);
public static void HasEdgeWith(this EcsWorld self, EcsWorld otherWorld)
{
if (self == null || otherWorld == null)
throw new ArgumentNullException();
WorldGraph.HasEdge(self, otherWorld);
}
2023-07-04 05:10:37 +08:00
public static EcsEdge GetEdgeWithSelf(this EcsWorld self) => GetEdgeWith(self, self);
public static EcsEdge GetEdgeWith(this EcsWorld self, EcsWorld otherWorld)
2023-06-29 14:25:51 +08:00
{
if (self == null || otherWorld == null)
throw new ArgumentNullException();
return WorldGraph.Get(self, otherWorld);
}
public static void DelEdgeWithSelf(this EcsWorld self) => DelEdgeWith(self, self);
public static void DelEdgeWith(this EcsWorld self, EcsWorld otherWorld)
{
if (self == null || otherWorld == null)
throw new ArgumentNullException();
WorldGraph.Unregister(self, otherWorld);
}
}
}