encompass-cs/encompass-cs/WorldBuilder.cs

285 lines
11 KiB
C#

using System;
using System.Collections.Generic;
using System.Reflection;
using System.Linq;
using Encompass.Exceptions;
using Encompass.Engines;
namespace Encompass
{
class WorldEngine : Engine // used to simulate the World, removed when passed to World
{
public override void Update(double dt) { }
}
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>> typeToReaders = new Dictionary<Type, HashSet<Engine>>();
private readonly HashSet<Engine> senders = new HashSet<Engine>();
private readonly HashSet<Type> registeredComponentTypes = new HashSet<Type>();
private readonly HashSet<Type> registeredNewComponentTypes = new HashSet<Type>();
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 SendMessage<TMessage>(TMessage message) where TMessage : struct, IMessage
{
messageManager.AddMessage(message);
}
public Guid AddComponent<TComponent>(Entity entity, TComponent component) where TComponent : struct, IComponent
{
var componentID = componentManager.NextID();
return componentManager.AddComponent(entity, componentID, component);
}
public Guid AddDrawComponent<TComponent>(Entity entity, TComponent component, int layer = 0) where TComponent : struct, IComponent
{
var componentID = componentManager.NextID();
return componentManager.AddDrawComponent(entity, componentID, component, layer);
}
public void DeactivateComponent(Guid componentID)
{
componentManager.MarkForDeactivation(componentID);
}
internal void RegisterComponent(Type componentType)
{
registeredComponentTypes.Add(componentType);
AddEngine((Engine)Activator.CreateInstance(typeof(ComponentMessageEmitter<>).MakeGenericType(componentType)));
}
internal void RegisterNewComponentUpdater(Type componentType)
{
AddEngine((Engine)Activator.CreateInstance(typeof(ComponentUpdater<>).MakeGenericType(componentType)));
}
public Engine AddEngine<TEngine>(TEngine engine) where TEngine : Engine
{
engine.AssignEntityManager(entityManager);
engine.AssignComponentManager(componentManager);
engine.AssignMessageManager(messageManager);
engines.Add(engine);
engineGraph.AddVertex(engine);
var messageReceiveTypes = engine.receiveTypes;
var messageSendTypes = engine.sendTypes;
foreach (var messageType in messageReceiveTypes.Intersect(messageSendTypes))
{
if ((messageType.IsGenericType && messageType.GetGenericTypeDefinition() == typeof(PendingComponentMessage<>)))
{
var componentType = messageType.GetGenericArguments().Single();
throw new EngineSelfCycleException("Engine {0} both activates and reads pending Component {1}", engine.GetType().Name, componentType.Name);
}
throw new EngineSelfCycleException("Engine {0} both receives and sends Message {1}", engine.GetType().Name, messageType.Name);
}
if (messageSendTypes.Any())
{
senders.Add(engine);
}
foreach (var receiveType in engine.receiveTypes)
{
if (receiveType.IsGenericType)
{
var genericTypeDefinition = receiveType.GetGenericTypeDefinition();
if (genericTypeDefinition == typeof(ComponentMessage<>) || genericTypeDefinition == typeof(PendingComponentMessage<>))
{
var componentType = receiveType.GetGenericArguments().Single();
if (!registeredComponentTypes.Contains(componentType))
{
RegisterComponent(componentType);
}
}
}
if (!typeToReaders.ContainsKey(receiveType))
{
typeToReaders.Add(receiveType, new HashSet<Engine>());
}
typeToReaders[receiveType].Add(engine);
}
foreach (var sendType in engine.sendTypes)
{
if (sendType.IsGenericType)
{
var genericTypeDefinition = sendType.GetGenericTypeDefinition();
if (genericTypeDefinition == typeof(ComponentUpdateMessage<>))
{
var componentType = sendType.GetGenericArguments().Single();
RegisterNewComponentUpdater(componentType);
}
}
}
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;
}
private void BuildEngineGraph(Engine worldEngine)
{
foreach (var senderEngine in senders)
{
foreach (var messageType in senderEngine.sendTypes.Where((type) => type.GetInterfaces().Contains(typeof(IMessage))))
{
if (typeToReaders.ContainsKey(messageType))
{
foreach (var readerEngine in typeToReaders[messageType])
{
if (senderEngine != readerEngine)
{
engineGraph.AddEdge(senderEngine, readerEngine);
}
}
}
}
}
engineGraph.AddVertex(worldEngine);
foreach (var engine in engines.Where(engine => engine.GetType().IsGenericType && engine.GetType().GetGenericTypeDefinition() == typeof(ComponentMessageEmitter<>)))
{
engineGraph.AddEdge(worldEngine, engine);
}
foreach (var engine in engines.Where(engine => !engineGraph.IncomingEdges(engine).Any()))
{
engineGraph.AddEdge(worldEngine, engine);
}
}
public World Build()
{
var worldEngine = new WorldEngine();
BuildEngineGraph(worldEngine);
if (engineGraph.Cyclic())
{
var cycles = engineGraph.SimpleCycles();
var errorString = "Cycle(s) found in Engines: ";
foreach (var cycle in cycles)
{
var reversed = cycle.Reverse();
errorString += "\n" +
string.Join(" -> ", reversed.Select((engine) => engine.GetType().Name)) +
" -> " +
reversed.First().GetType().Name;
}
throw new EngineCycleException(errorString);
}
var mutatedComponentTypes = new HashSet<Type>();
var duplicateMutations = new List<Type>();
var updateMessageToEngines = new Dictionary<Type, List<Engine>>();
foreach (var engine in engines)
{
var updateTypes = engine.sendTypes.Where((type) => type.IsGenericType && type.GetGenericTypeDefinition() == typeof(ComponentUpdateMessage<>));
foreach (var updateType in updateTypes)
{
if (mutatedComponentTypes.Contains(updateType))
{
duplicateMutations.Add(updateType);
}
else
{
mutatedComponentTypes.Add(updateType);
}
if (!updateMessageToEngines.ContainsKey(updateType))
{
updateMessageToEngines[updateType] = new List<Engine>();
}
updateMessageToEngines[updateType].Add(engine);
}
}
if (duplicateMutations.Count > 0)
{
var errorString = "Multiple Engines update the same Component: ";
foreach (var componentType in duplicateMutations)
{
errorString += "\n" +
componentType.Name + " updated by: " +
string.Join(", ", updateMessageToEngines[componentType].Select((engine) => engine.GetType().Name));
}
throw new EngineUpdateConflictException(errorString);
}
var world = new World(
engineGraph.LongestPaths(worldEngine).Where(pair => pair.Item2 != 0).ToLookup(pair => pair.Item2, pair => pair.Item1),
entityManager,
componentManager,
messageManager,
renderManager
);
componentManager.PerformComponentUpdates();
componentManager.DeactivateMarkedComponents();
componentManager.RemoveMarkedComponents();
entityManager.CheckEntitiesWithAddedComponents();
entityManager.CheckEntitiesWithRemovedComponents();
return world;
}
}
}