2024-03-18 02:45:25 +08:00
|
|
|
|
using DCFApixels.DragonECS.Graphs.Internal;
|
|
|
|
|
using System;
|
|
|
|
|
|
|
|
|
|
namespace DCFApixels.DragonECS
|
|
|
|
|
{
|
2024-11-17 21:17:06 +08:00
|
|
|
|
public static class EntityGraphExtensions
|
2024-03-18 02:45:25 +08:00
|
|
|
|
{
|
2024-11-17 13:36:14 +08:00
|
|
|
|
private static EntityGraph[] _worldGraphs = new EntityGraph[4];
|
2024-03-18 02:45:25 +08:00
|
|
|
|
|
2024-11-17 13:36:14 +08:00
|
|
|
|
public static EntityGraph CreateGraph(this EcsWorld self, EcsWorld graphWorld)
|
2024-03-18 02:45:25 +08:00
|
|
|
|
{
|
2024-11-04 07:40:19 +08:00
|
|
|
|
int worldID = self.ID;
|
2024-03-18 02:45:25 +08:00
|
|
|
|
if (_worldGraphs.Length <= worldID)
|
|
|
|
|
{
|
|
|
|
|
Array.Resize(ref _worldGraphs, worldID + 4);
|
|
|
|
|
}
|
2024-11-17 13:36:14 +08:00
|
|
|
|
ref EntityGraph graph = ref _worldGraphs[worldID];
|
2024-03-18 02:45:25 +08:00
|
|
|
|
if (graph != null)
|
|
|
|
|
{
|
|
|
|
|
Throw.UndefinedException();
|
|
|
|
|
}
|
2024-11-17 13:36:14 +08:00
|
|
|
|
graph = new EntityGraph(self, graphWorld);
|
2024-03-18 02:45:25 +08:00
|
|
|
|
new Destroyer(graph);
|
2024-11-04 07:40:19 +08:00
|
|
|
|
_worldGraphs[graphWorld.ID] = graph;
|
2024-03-18 02:45:25 +08:00
|
|
|
|
return graph;
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-17 13:36:14 +08:00
|
|
|
|
public static EntityGraph CreateOrGetGraph(this EcsWorld self, EcsWorld graphWorld)
|
2024-03-18 02:45:25 +08:00
|
|
|
|
{
|
2024-11-04 07:40:19 +08:00
|
|
|
|
int worldID = self.ID;
|
2024-03-18 02:45:25 +08:00
|
|
|
|
if (_worldGraphs.Length <= worldID)
|
|
|
|
|
{
|
|
|
|
|
Array.Resize(ref _worldGraphs, worldID + 4);
|
|
|
|
|
}
|
2024-11-17 13:36:14 +08:00
|
|
|
|
ref EntityGraph graph = ref _worldGraphs[worldID];
|
2024-03-18 02:45:25 +08:00
|
|
|
|
if (graph != null)
|
|
|
|
|
{
|
|
|
|
|
return graph;
|
|
|
|
|
}
|
2024-11-17 13:36:14 +08:00
|
|
|
|
graph = new EntityGraph(self, graphWorld);
|
2024-03-18 02:45:25 +08:00
|
|
|
|
new Destroyer(graph);
|
2024-11-04 07:40:19 +08:00
|
|
|
|
_worldGraphs[graphWorld.ID] = graph;
|
2024-03-18 02:45:25 +08:00
|
|
|
|
return graph;
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-17 13:36:14 +08:00
|
|
|
|
public static bool TryGetGraph(this EcsWorld self, out EntityGraph graph)
|
2024-03-18 02:45:25 +08:00
|
|
|
|
{
|
2024-11-04 07:40:19 +08:00
|
|
|
|
int worldID = self.ID;
|
2024-03-18 02:45:25 +08:00
|
|
|
|
if (_worldGraphs.Length <= worldID)
|
|
|
|
|
{
|
|
|
|
|
Array.Resize(ref _worldGraphs, worldID + 4);
|
|
|
|
|
}
|
|
|
|
|
graph = _worldGraphs[worldID];
|
|
|
|
|
return graph != null;
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-17 13:36:14 +08:00
|
|
|
|
public static EntityGraph GetGraph(this EcsWorld self)
|
2024-03-18 02:45:25 +08:00
|
|
|
|
{
|
2024-11-17 13:36:14 +08:00
|
|
|
|
if (self.TryGetGraph(out EntityGraph graph))
|
2024-03-18 02:45:25 +08:00
|
|
|
|
{
|
|
|
|
|
return graph;
|
|
|
|
|
}
|
|
|
|
|
Throw.UndefinedException();
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static bool IsGraphWorld(this EcsWorld self)
|
|
|
|
|
{
|
2024-11-17 13:36:14 +08:00
|
|
|
|
if (self.TryGetGraph(out EntityGraph graph))
|
2024-03-18 02:45:25 +08:00
|
|
|
|
{
|
|
|
|
|
return graph.GraphWorld == self;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-17 13:36:14 +08:00
|
|
|
|
private static void TryDestroy(EntityGraph graph)
|
2024-03-18 02:45:25 +08:00
|
|
|
|
{
|
|
|
|
|
int worldID = graph.WorldID;
|
|
|
|
|
if (_worldGraphs.Length <= worldID)
|
|
|
|
|
{
|
|
|
|
|
Array.Resize(ref _worldGraphs, worldID + 4);
|
|
|
|
|
}
|
|
|
|
|
int graphWorldID = graph.GraphWorldID;
|
|
|
|
|
if (_worldGraphs.Length <= graphWorldID)
|
|
|
|
|
{
|
|
|
|
|
Array.Resize(ref _worldGraphs, graphWorldID + 4);
|
|
|
|
|
}
|
|
|
|
|
_worldGraphs[worldID] = null;
|
|
|
|
|
_worldGraphs[graphWorldID] = null;
|
|
|
|
|
}
|
|
|
|
|
private class Destroyer : IEcsWorldEventListener
|
|
|
|
|
{
|
2024-11-17 13:36:14 +08:00
|
|
|
|
private EntityGraph _graph;
|
|
|
|
|
public Destroyer(EntityGraph graph)
|
2024-03-18 02:45:25 +08:00
|
|
|
|
{
|
|
|
|
|
_graph = graph;
|
|
|
|
|
graph.World.AddListener(this);
|
|
|
|
|
graph.GraphWorld.AddListener(this);
|
|
|
|
|
}
|
|
|
|
|
public void OnReleaseDelEntityBuffer(ReadOnlySpan<int> buffer) { }
|
|
|
|
|
public void OnWorldDestroy()
|
|
|
|
|
{
|
|
|
|
|
TryDestroy(_graph);
|
|
|
|
|
}
|
|
|
|
|
public void OnWorldResize(int newSize) { }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|