238 lines
8.9 KiB
C#
238 lines
8.9 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Reflection;
|
|
using System.Linq;
|
|
using Encompass.Exceptions;
|
|
|
|
namespace Encompass
|
|
{
|
|
public class WorldBuilder
|
|
{
|
|
private readonly List<Engine> engines = new List<Engine>();
|
|
private readonly DirectedGraph<Engine> engineGraph = new DirectedGraph<Engine>();
|
|
|
|
private readonly ComponentManager componentManager;
|
|
private readonly EntityManager entityManager;
|
|
private readonly MessageManager messageManager;
|
|
private readonly DrawLayerManager drawLayerManager;
|
|
private readonly RenderManager renderManager;
|
|
|
|
private readonly Dictionary<Type, HashSet<Engine>> typeToEmitters = new Dictionary<Type, HashSet<Engine>>();
|
|
private readonly Dictionary<Type, HashSet<Engine>> typeToReaders = new Dictionary<Type, HashSet<Engine>>();
|
|
|
|
public WorldBuilder()
|
|
{
|
|
var entitiesWithAddedComponents = new HashSet<Guid>();
|
|
var entitiesWithRemovedComponents = new HashSet<Guid>();
|
|
drawLayerManager = new DrawLayerManager();
|
|
componentManager = new ComponentManager(drawLayerManager, entitiesWithAddedComponents, entitiesWithRemovedComponents);
|
|
entityManager = new EntityManager(componentManager, entitiesWithAddedComponents, entitiesWithRemovedComponents);
|
|
messageManager = new MessageManager();
|
|
renderManager = new RenderManager(entityManager, componentManager, drawLayerManager);
|
|
}
|
|
|
|
public Entity CreateEntity()
|
|
{
|
|
return entityManager.CreateEntity();
|
|
}
|
|
|
|
public void EmitMessage<TMessage>(TMessage message) where TMessage : struct, IMessage
|
|
{
|
|
messageManager.AddMessage(message);
|
|
}
|
|
|
|
public Guid AddComponent<TComponent>(Entity entity, TComponent component) where TComponent : struct, IComponent
|
|
{
|
|
return componentManager.AddComponent(entity.ID, component);
|
|
}
|
|
|
|
public Guid AddDrawComponent<TComponent>(Entity entity, TComponent component, int layer = 0) where TComponent : struct, IComponent
|
|
{
|
|
return componentManager.AddDrawComponent(entity.ID, component, layer);
|
|
}
|
|
|
|
public void DeactivateComponent(Guid componentID)
|
|
{
|
|
componentManager.MarkForDeactivation(componentID);
|
|
}
|
|
|
|
public Engine AddEngine<TEngine>(TEngine engine) where TEngine : Engine
|
|
{
|
|
engine.AssignEntityManager(entityManager);
|
|
engine.AssignComponentManager(componentManager);
|
|
engine.AssignMessageManager(messageManager);
|
|
|
|
engines.Add(engine);
|
|
engineGraph.AddVertex(engine);
|
|
|
|
foreach (var writeType in engine.writeTypes.Where((type) => type.GetInterfaces().Contains(typeof(IMessage))))
|
|
{
|
|
if (!typeToEmitters.ContainsKey(writeType))
|
|
{
|
|
typeToEmitters.Add(writeType, new HashSet<Engine>());
|
|
}
|
|
|
|
typeToEmitters[writeType].Add(engine);
|
|
|
|
if (typeToReaders.ContainsKey(writeType))
|
|
{
|
|
foreach (var reader in typeToReaders[writeType])
|
|
{
|
|
if (engine == reader)
|
|
{
|
|
if (writeType.GetInterfaces().Contains(typeof(IMessage)))
|
|
{
|
|
throw new EngineMessageSelfCycleException("Engine both reads and writes Message {0}", writeType.Name);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
engineGraph.AddEdge(engine, reader);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
foreach (var readType in engine.readTypes.Where((type) => type.GetInterfaces().Contains(typeof(IMessage))))
|
|
{
|
|
if (!typeToReaders.ContainsKey(readType))
|
|
{
|
|
typeToReaders.Add(readType, new HashSet<Engine>());
|
|
}
|
|
|
|
typeToReaders[readType].Add(engine);
|
|
|
|
if (typeToEmitters.ContainsKey(readType))
|
|
{
|
|
foreach (var emitter in typeToEmitters[readType])
|
|
{
|
|
if (emitter == engine)
|
|
{
|
|
if (readType.GetInterfaces().Contains(typeof(IMessage)))
|
|
{
|
|
throw new EngineMessageSelfCycleException("Engine both reads and writes Message {0}", readType.Name);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
engineGraph.AddEdge(emitter, engine);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return engine;
|
|
}
|
|
|
|
public TRenderer AddEntityRenderer<TRenderer>(TRenderer renderer) where TRenderer : Renderer
|
|
{
|
|
renderer.AssignEntityManager(entityManager);
|
|
renderer.AssignComponentManager(componentManager);
|
|
|
|
if (renderer is EntityRenderer)
|
|
{
|
|
entityManager.RegisterEntityTracker(renderer as IEntityTracker);
|
|
renderManager.RegisterEntityRenderer(renderer as EntityRenderer);
|
|
}
|
|
|
|
return renderer;
|
|
}
|
|
|
|
public TRenderer AddGeneralRenderer<TRenderer>(TRenderer renderer, int layer) where TRenderer : GeneralRenderer
|
|
{
|
|
renderer.AssignEntityManager(entityManager);
|
|
renderer.AssignComponentManager(componentManager);
|
|
|
|
renderManager.RegisterGeneralRendererWithLayer(renderer, layer);
|
|
|
|
return renderer;
|
|
}
|
|
|
|
public World Build()
|
|
{
|
|
if (engineGraph.Cyclic())
|
|
{
|
|
var cycles = engineGraph.SimpleCycles();
|
|
var errorString = "Cycle(s) found in Engines: ";
|
|
foreach (var cycle in cycles.Reverse())
|
|
{
|
|
errorString += "\n" +
|
|
string.Join(" -> ", cycle.Select((engine) => engine.GetType().Name)) +
|
|
" -> " +
|
|
cycle.First().GetType().Name;
|
|
}
|
|
throw new EngineCycleException(errorString);
|
|
}
|
|
|
|
var mutatedComponentTypes = new HashSet<Type>();
|
|
var duplicateMutations = new List<Type>();
|
|
var componentToEngines = new Dictionary<Type, List<Engine>>();
|
|
|
|
foreach (var engine in engines)
|
|
{
|
|
var writeAttribute = engine.GetType().GetCustomAttribute<Writes>(false);
|
|
if (writeAttribute != null)
|
|
{
|
|
foreach (var writeType in writeAttribute.writeTypes)
|
|
{
|
|
if (writeType.GetInterfaces().Contains(typeof(IComponent))) // if our write type is a component
|
|
{
|
|
if (mutatedComponentTypes.Contains(writeType))
|
|
{
|
|
duplicateMutations.Add(writeType);
|
|
}
|
|
else
|
|
{
|
|
mutatedComponentTypes.Add(writeType);
|
|
}
|
|
|
|
if (!componentToEngines.ContainsKey(writeType))
|
|
{
|
|
componentToEngines[writeType] = new List<Engine>();
|
|
}
|
|
|
|
componentToEngines[writeType].Add(engine);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if (duplicateMutations.Count > 0)
|
|
{
|
|
var errorString = "Multiple Engines write the same Component: ";
|
|
foreach (var componentType in duplicateMutations)
|
|
{
|
|
errorString += "\n" +
|
|
componentType.Name + " written by: " +
|
|
string.Join(", ", componentToEngines[componentType].Select((engine) => engine.GetType().Name));
|
|
}
|
|
|
|
throw new EngineWriteConflictException(errorString);
|
|
}
|
|
|
|
var engineOrder = new List<Engine>();
|
|
foreach (var engine in engineGraph.TopologicalSort())
|
|
{
|
|
engineOrder.Add(engine);
|
|
}
|
|
|
|
var world = new World(
|
|
engineOrder,
|
|
entityManager,
|
|
componentManager,
|
|
messageManager,
|
|
renderManager
|
|
);
|
|
|
|
componentManager.ActivateMarkedComponents();
|
|
componentManager.DeactivateMarkedComponents();
|
|
componentManager.RemoveMarkedComponents();
|
|
|
|
entityManager.CheckEntitiesWithAddedComponents();
|
|
entityManager.CheckEntitiesWithRemovedComponents();
|
|
|
|
return world;
|
|
}
|
|
}
|
|
}
|