encompass-cs/encompass-cs/WorldBuilder.cs

170 lines
6.1 KiB
C#

using System;
using System.Collections.Generic;
using System.Reflection;
using System.Linq;
namespace Encompass
{
public class WorldBuilder
{
private List<Engine> engines = new List<Engine>();
private DirectedGraph<Engine> engineGraph = new DirectedGraph<Engine>();
private ComponentManager componentManager;
private EntityManager entityManager;
private MessageManager messageManager;
private Dictionary<Type, HashSet<Engine>> messageTypeToEmitters = new Dictionary<Type, HashSet<Engine>>();
private Dictionary<Type, HashSet<Engine>> messageTypeToReaders = new Dictionary<Type, HashSet<Engine>>();
public WorldBuilder()
{
componentManager = new ComponentManager();
entityManager = new EntityManager(componentManager);
messageManager = new MessageManager();
}
public Entity CreateEntity()
{
return this.entityManager.CreateEntity();
}
public Engine AddEngine<TEngine>() where TEngine : Engine, new()
{
var engine = new TEngine();
engine.AssignEntityManager(this.entityManager);
engine.AssignComponentManager(this.componentManager);
engine.AssignMessageManager(this.messageManager);
engines.Add(engine);
engineGraph.AddVertex(engine);
var emitMessageAttribute = engine.GetType().GetCustomAttribute<Emits>(false);
if (emitMessageAttribute != null)
{
foreach (var emitMessageType in engine.GetType().GetCustomAttribute<Emits>(false).emitMessageTypes)
{
if (!messageTypeToEmitters.ContainsKey(emitMessageType))
{
messageTypeToEmitters.Add(emitMessageType, new HashSet<Engine>());
}
messageTypeToEmitters[emitMessageType].Add(engine);
if (messageTypeToReaders.ContainsKey(emitMessageType))
{
foreach (var reader in messageTypeToReaders[emitMessageType])
{
engineGraph.AddEdge(engine, reader);
}
}
}
}
var readMessageAttribute = engine.GetType().GetCustomAttribute<Reads>(false);
if (readMessageAttribute != null)
{
foreach (var readMessageType in engine.GetType().GetCustomAttribute<Reads>(false).readMessageTypes)
{
if (!messageTypeToReaders.ContainsKey(readMessageType))
{
messageTypeToReaders.Add(readMessageType, new HashSet<Engine>());
}
messageTypeToReaders[readMessageType].Add(engine);
if (messageTypeToEmitters.ContainsKey(readMessageType))
{
foreach (var emitter in messageTypeToEmitters[readMessageType])
{
engineGraph.AddEdge(emitter, engine);
}
}
}
}
return engine;
}
public World Build()
{
if (engineGraph.Cyclic())
{
var cycles = engineGraph.SimpleCycles();
var errorString = "Cycle(s) found in Engines: ";
foreach (var cycle in cycles)
{
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 mutateAttribute = engine.GetType().GetCustomAttribute<Mutates>(false);
if (mutateAttribute != null)
{
foreach (var mutateComponentType in engine.GetType().GetCustomAttribute<Mutates>(false).mutateComponentTypes)
{
if (mutatedComponentTypes.Contains(mutateComponentType))
{
duplicateMutations.Add(mutateComponentType);
}
else
{
mutatedComponentTypes.Add(mutateComponentType);
}
if (!componentToEngines.ContainsKey(mutateComponentType))
{
componentToEngines[mutateComponentType] = new List<Engine>();
}
componentToEngines[mutateComponentType].Add(engine);
}
}
}
if (duplicateMutations.Count > 0)
{
var errorString = "Multiple Engines mutate the same Component: ";
foreach (var componentType in duplicateMutations)
{
errorString += "\n" +
componentType.Name + " mutated by: " +
string.Join(", ", componentToEngines[componentType].Select((engine) => engine.GetType().Name));
}
throw new EngineMutationConflictException(errorString);
}
var engineOrder = new List<Engine>();
foreach (var engine in engineGraph.TopologicalSort())
{
engineOrder.Add(engine);
}
var world = new World(
engineOrder,
this.entityManager,
this.componentManager,
this.messageManager
);
this.componentManager.ActivateComponents();
this.componentManager.RemoveComponents();
return world;
}
}
}