encompass-cs/encompass-cs/WorldBuilder.cs

238 lines
8.9 KiB
C#
Raw Normal View History

2019-06-17 00:56:36 +00:00
using System;
2019-06-15 00:03:56 +00:00
using System.Collections.Generic;
2019-06-17 00:56:36 +00:00
using System.Reflection;
using System.Linq;
using Encompass.Exceptions;
2019-06-15 00:03:56 +00:00
2019-06-16 01:05:56 +00:00
namespace Encompass
{
public class WorldBuilder
{
2019-06-20 17:46:15 +00:00
private readonly List<Engine> engines = new List<Engine>();
private readonly DirectedGraph<Engine> engineGraph = new DirectedGraph<Engine>();
2019-06-15 00:51:06 +00:00
2019-06-20 17:46:15 +00:00
private readonly ComponentManager componentManager;
private readonly EntityManager entityManager;
private readonly MessageManager messageManager;
private readonly DrawLayerManager drawLayerManager;
private readonly RenderManager renderManager;
2019-06-15 00:03:56 +00:00
private readonly Dictionary<Type, HashSet<Engine>> typeToEmitters = new Dictionary<Type, HashSet<Engine>>();
private readonly Dictionary<Type, HashSet<Engine>> typeToReaders = new Dictionary<Type, HashSet<Engine>>();
2019-06-17 00:56:36 +00:00
2019-06-16 01:05:56 +00:00
public WorldBuilder()
{
var entitiesWithAddedComponents = new HashSet<Guid>();
var entitiesWithRemovedComponents = new HashSet<Guid>();
drawLayerManager = new DrawLayerManager();
componentManager = new ComponentManager(drawLayerManager, entitiesWithAddedComponents, entitiesWithRemovedComponents);
2019-06-19 23:13:02 +00:00
entityManager = new EntityManager(componentManager, entitiesWithAddedComponents, entitiesWithRemovedComponents);
2019-06-16 01:55:35 +00:00
messageManager = new MessageManager();
renderManager = new RenderManager(entityManager, componentManager, drawLayerManager);
2019-06-15 00:03:56 +00:00
}
2019-06-16 01:05:56 +00:00
public Entity CreateEntity()
{
2019-06-19 21:14:44 +00:00
return entityManager.CreateEntity();
2019-06-15 00:03:56 +00:00
}
2019-06-29 05:07:48 +00:00
public void EmitMessage<TMessage>(TMessage message) where TMessage : struct, IMessage
{
messageManager.AddMessage(message);
}
2019-07-17 18:24:21 +00:00
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);
2019-07-17 18:24:21 +00:00
}
public Engine AddEngine<TEngine>(TEngine engine) where TEngine : Engine
2019-06-16 01:05:56 +00:00
{
engine.AssignEntityManager(entityManager);
engine.AssignComponentManager(componentManager);
engine.AssignMessageManager(messageManager);
2019-06-15 00:51:06 +00:00
engines.Add(engine);
2019-06-17 00:56:36 +00:00
engineGraph.AddVertex(engine);
foreach (var writeType in engine.writeTypes.Where((type) => type.GetInterfaces().Contains(typeof(IMessage))))
2019-06-17 00:56:36 +00:00
{
if (!typeToEmitters.ContainsKey(writeType))
2019-06-17 00:56:36 +00:00
{
typeToEmitters.Add(writeType, new HashSet<Engine>());
2019-06-29 05:57:18 +00:00
}
2019-06-17 00:56:36 +00:00
typeToEmitters[writeType].Add(engine);
2019-06-17 00:56:36 +00:00
if (typeToReaders.ContainsKey(writeType))
2019-06-29 05:57:18 +00:00
{
foreach (var reader in typeToReaders[writeType])
2019-06-17 00:56:36 +00:00
{
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);
}
2019-06-17 00:56:36 +00:00
}
}
}
foreach (var readType in engine.readTypes.Where((type) => type.GetInterfaces().Contains(typeof(IMessage))))
2019-06-17 00:56:36 +00:00
{
if (!typeToReaders.ContainsKey(readType))
2019-06-17 00:56:36 +00:00
{
typeToReaders.Add(readType, new HashSet<Engine>());
2019-06-29 05:57:18 +00:00
}
2019-06-17 00:56:36 +00:00
typeToReaders[readType].Add(engine);
2019-06-17 00:56:36 +00:00
if (typeToEmitters.ContainsKey(readType))
2019-06-29 05:57:18 +00:00
{
foreach (var emitter in typeToEmitters[readType])
2019-06-17 00:56:36 +00:00
{
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);
}
2019-06-17 00:56:36 +00:00
}
}
}
2019-06-15 00:51:06 +00:00
return engine;
}
public TRenderer AddEntityRenderer<TRenderer>(TRenderer renderer) where TRenderer : Renderer
2019-06-19 21:14:44 +00:00
{
renderer.AssignEntityManager(entityManager);
renderer.AssignComponentManager(componentManager);
if (renderer is EntityRenderer)
{
2019-06-19 23:13:02 +00:00
entityManager.RegisterEntityTracker(renderer as IEntityTracker);
2019-06-19 21:14:44 +00:00
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);
2019-06-19 21:14:44 +00:00
return renderer;
}
2019-06-16 01:05:56 +00:00
public World Build()
{
2019-06-17 00:56:36 +00:00
if (engineGraph.Cyclic())
{
var cycles = engineGraph.SimpleCycles();
var errorString = "Cycle(s) found in Engines: ";
foreach (var cycle in cycles.Reverse())
2019-06-17 00:56:36 +00:00
{
errorString += "\n" +
2019-06-17 01:18:08 +00:00
string.Join(" -> ", cycle.Select((engine) => engine.GetType().Name)) +
2019-06-17 00:56:36 +00:00
" -> " +
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)
{
2019-07-16 16:47:58 +00:00
var writeAttribute = engine.GetType().GetCustomAttribute<Writes>(false);
if (writeAttribute != null)
2019-06-17 00:56:36 +00:00
{
2019-07-16 16:47:58 +00:00
foreach (var writeType in writeAttribute.writeTypes)
2019-06-17 00:56:36 +00:00
{
2019-07-16 16:47:58 +00:00
if (writeType.GetInterfaces().Contains(typeof(IComponent))) // if our write type is a component
2019-06-17 00:56:36 +00:00
{
2019-07-16 16:47:58 +00:00
if (mutatedComponentTypes.Contains(writeType))
{
duplicateMutations.Add(writeType);
}
else
{
mutatedComponentTypes.Add(writeType);
}
if (!componentToEngines.ContainsKey(writeType))
{
componentToEngines[writeType] = new List<Engine>();
}
componentToEngines[writeType].Add(engine);
2019-06-17 00:56:36 +00:00
}
}
}
}
if (duplicateMutations.Count > 0)
{
2019-07-16 16:47:58 +00:00
var errorString = "Multiple Engines write the same Component: ";
2019-06-17 00:56:36 +00:00
foreach (var componentType in duplicateMutations)
{
errorString += "\n" +
2019-07-16 16:47:58 +00:00
componentType.Name + " written by: " +
2019-06-17 00:56:36 +00:00
string.Join(", ", componentToEngines[componentType].Select((engine) => engine.GetType().Name));
}
throw new EngineWriteConflictException(errorString);
2019-06-17 00:56:36 +00:00
}
var engineOrder = new List<Engine>();
foreach (var engine in engineGraph.TopologicalSort())
{
engineOrder.Add(engine);
}
2019-06-15 00:51:06 +00:00
var world = new World(
2019-06-17 00:56:36 +00:00
engineOrder,
2019-06-19 21:14:44 +00:00
entityManager,
componentManager,
messageManager,
renderManager
2019-06-15 00:51:06 +00:00
);
2019-06-15 00:03:56 +00:00
componentManager.ActivateMarkedComponents();
componentManager.DeactivateMarkedComponents();
componentManager.RemoveMarkedComponents();
2019-06-19 23:13:02 +00:00
entityManager.CheckEntitiesWithAddedComponents();
entityManager.CheckEntitiesWithRemovedComponents();
2019-06-15 00:03:56 +00:00
return world;
}
}
}