encompass-cs/encompass-cs/WorldBuilder.cs

268 lines
10 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-07-18 21:02:57 +00:00
using Encompass.Engines;
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;
2019-08-01 23:24:57 +00:00
private readonly ComponentMessageManager componentMessageManager;
2019-06-20 17:46:15 +00:00
private readonly DrawLayerManager drawLayerManager;
private readonly RenderManager renderManager;
2019-06-15 00:03:56 +00:00
private readonly Dictionary<Type, HashSet<Engine>> typeToReaders = new Dictionary<Type, HashSet<Engine>>();
2019-06-17 00:56:36 +00:00
2019-07-18 21:02:57 +00:00
private readonly HashSet<Engine> senders = new HashSet<Engine>();
private readonly HashSet<Type> registeredComponentTypes = new HashSet<Type>();
2019-06-16 01:05:56 +00:00
public WorldBuilder()
{
drawLayerManager = new DrawLayerManager();
componentManager = new ComponentManager(drawLayerManager);
2019-06-16 01:55:35 +00:00
messageManager = new MessageManager();
2019-08-01 23:24:57 +00:00
componentMessageManager = new ComponentMessageManager();
entityManager = new EntityManager(componentManager, componentMessageManager);
2019-08-10 02:29:08 +00:00
renderManager = new RenderManager(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
}
public void SendMessage<TMessage>(TMessage message) where TMessage : struct, IMessage
2019-06-29 05:07:48 +00:00
{
messageManager.AddMessage(message);
}
2019-08-20 02:05:18 +00:00
public void SendMessageDelayed<TMessage>(TMessage message, double time) where TMessage : struct, IMessage
{
messageManager.AddMessageDelayed(message, time);
}
2019-07-17 18:24:21 +00:00
public Guid AddComponent<TComponent>(Entity entity, TComponent component) where TComponent : struct, IComponent
{
return componentManager.MarkComponentForAdd(entity, component);
2019-07-17 18:24:21 +00:00
}
public Guid AddDrawComponent<TComponent>(Entity entity, TComponent component, int layer = 0) where TComponent : struct, IComponent
{
return componentManager.MarkDrawComponentForAdd(entity, component, layer);
2019-07-17 18:24:21 +00:00
}
2019-07-18 21:02:57 +00:00
internal void RegisterComponent(Type componentType)
{
registeredComponentTypes.Add(componentType);
AddEngine((Engine)Activator.CreateInstance(typeof(ComponentMessageEmitter<>).MakeGenericType(componentType)));
}
2019-07-20 00:50:13 +00:00
internal void RegisterNewComponentUpdater(Type componentType)
2019-07-19 19:47:17 +00:00
{
2019-07-20 00:50:13 +00:00
AddEngine((Engine)Activator.CreateInstance(typeof(ComponentUpdater<>).MakeGenericType(componentType)));
2019-07-19 19:47:17 +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-08-01 23:24:57 +00:00
engine.AssignComponentMessageManager(componentMessageManager);
2019-06-15 00:51:06 +00:00
engines.Add(engine);
2019-06-17 00:56:36 +00:00
engineGraph.AddVertex(engine);
2019-07-19 19:47:17 +00:00
var messageReceiveTypes = engine.receiveTypes;
2019-07-19 01:20:38 +00:00
var messageSendTypes = engine.sendTypes;
2019-06-17 00:56:36 +00:00
2019-07-29 02:17:00 +00:00
foreach (var messageType in messageReceiveTypes.Union(messageSendTypes))
{
messageManager.RegisterMessageType(messageType);
}
2019-07-19 19:47:17 +00:00
foreach (var messageType in messageReceiveTypes.Intersect(messageSendTypes))
2019-07-18 21:02:57 +00:00
{
2019-07-19 23:15:48 +00:00
if ((messageType.IsGenericType && messageType.GetGenericTypeDefinition() == typeof(PendingComponentMessage<>)))
{
2019-07-19 23:15:48 +00:00
var componentType = messageType.GetGenericArguments().Single();
throw new EngineSelfCycleException("Engine {0} both activates and reads pending Component {1}", engine.GetType().Name, componentType.Name);
}
2019-07-19 23:15:48 +00:00
throw new EngineSelfCycleException("Engine {0} both receives and sends Message {1}", engine.GetType().Name, messageType.Name);
2019-07-18 21:02:57 +00:00
}
2019-06-17 00:56:36 +00:00
2019-07-18 21:02:57 +00:00
if (messageSendTypes.Any())
{
senders.Add(engine);
}
2019-07-19 19:47:17 +00:00
foreach (var receiveType in engine.receiveTypes)
2019-07-18 21:02:57 +00:00
{
2019-07-19 19:47:17 +00:00
if (receiveType.IsGenericType)
2019-06-29 05:57:18 +00:00
{
2019-07-19 19:47:17 +00:00
var genericTypeDefinition = receiveType.GetGenericTypeDefinition();
2019-07-19 23:15:48 +00:00
if (genericTypeDefinition == typeof(ComponentMessage<>) || genericTypeDefinition == typeof(PendingComponentMessage<>))
2019-06-17 00:56:36 +00:00
{
2019-07-19 19:47:17 +00:00
var componentType = receiveType.GetGenericArguments().Single();
if (!registeredComponentTypes.Contains(componentType))
{
RegisterComponent(componentType);
}
2019-06-17 00:56:36 +00:00
}
}
2019-07-19 19:47:17 +00:00
if (!typeToReaders.ContainsKey(receiveType))
2019-06-17 00:56:36 +00:00
{
2019-07-19 19:47:17 +00:00
typeToReaders.Add(receiveType, new HashSet<Engine>());
2019-06-29 05:57:18 +00:00
}
2019-06-17 00:56:36 +00:00
2019-07-19 19:47:17 +00:00
typeToReaders[receiveType].Add(engine);
}
foreach (var sendType in engine.sendTypes)
{
if (sendType.IsGenericType)
{
var genericTypeDefinition = sendType.GetGenericTypeDefinition();
2019-07-20 00:50:13 +00:00
if (genericTypeDefinition == typeof(ComponentUpdateMessage<>))
2019-07-19 19:47:17 +00:00
{
var componentType = sendType.GetGenericArguments().Single();
2019-07-20 00:50:13 +00:00
RegisterNewComponentUpdater(componentType);
2019-07-19 19:47:17 +00:00
}
}
2019-06-17 00:56:36 +00:00
}
2019-06-15 00:51:06 +00:00
return engine;
}
public OrderedRenderer<TComponent> AddOrderedRenderer<TComponent>(OrderedRenderer<TComponent> renderer) where TComponent : struct, IComponent
2019-06-19 21:14:44 +00:00
{
renderer.AssignEntityManager(entityManager);
renderer.AssignComponentManager(componentManager);
renderManager.RegisterOrderedRenderer<TComponent>(renderer.InternalRender);
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-07-18 21:02:57 +00:00
private void BuildEngineGraph()
{
foreach (var senderEngine in senders)
{
2019-07-19 01:20:38 +00:00
foreach (var messageType in senderEngine.sendTypes.Where((type) => type.GetInterfaces().Contains(typeof(IMessage))))
2019-07-18 21:02:57 +00:00
{
if (typeToReaders.ContainsKey(messageType))
{
foreach (var readerEngine in typeToReaders[messageType])
{
if (senderEngine != readerEngine)
{
engineGraph.AddEdge(senderEngine, readerEngine);
}
2019-07-18 21:02:57 +00:00
}
}
}
}
}
2019-06-16 01:05:56 +00:00
public World Build()
{
2019-07-18 21:02:57 +00:00
BuildEngineGraph();
2019-06-17 00:56:36 +00:00
if (engineGraph.Cyclic())
{
var cycles = engineGraph.SimpleCycles();
var errorString = "Cycle(s) found in Engines: ";
2019-07-19 23:15:48 +00:00
foreach (var cycle in cycles)
2019-06-17 00:56:36 +00:00
{
2019-07-19 23:15:48 +00:00
var reversed = cycle.Reverse();
2019-06-17 00:56:36 +00:00
errorString += "\n" +
2019-07-19 23:15:48 +00:00
string.Join(" -> ", reversed.Select((engine) => engine.GetType().Name)) +
2019-06-17 00:56:36 +00:00
" -> " +
2019-07-19 23:15:48 +00:00
reversed.First().GetType().Name;
2019-06-17 00:56:36 +00:00
}
throw new EngineCycleException(errorString);
}
var mutatedComponentTypes = new HashSet<Type>();
var duplicateMutations = new List<Type>();
2019-07-20 00:50:13 +00:00
var updateMessageToEngines = new Dictionary<Type, List<Engine>>();
2019-06-17 00:56:36 +00:00
foreach (var engine in engines)
{
2019-07-20 00:50:13 +00:00
var updateTypes = engine.sendTypes.Where((type) => type.IsGenericType && type.GetGenericTypeDefinition() == typeof(ComponentUpdateMessage<>));
foreach (var updateType in updateTypes)
2019-06-17 00:56:36 +00:00
{
2019-07-19 23:15:48 +00:00
if (mutatedComponentTypes.Contains(updateType))
2019-06-17 00:56:36 +00:00
{
2019-07-19 23:15:48 +00:00
duplicateMutations.Add(updateType);
}
else
{
mutatedComponentTypes.Add(updateType);
}
2019-07-19 01:20:38 +00:00
2019-07-20 00:50:13 +00:00
if (!updateMessageToEngines.ContainsKey(updateType))
2019-07-19 23:15:48 +00:00
{
2019-07-20 00:50:13 +00:00
updateMessageToEngines[updateType] = new List<Engine>();
2019-06-17 00:56:36 +00:00
}
2019-07-19 23:15:48 +00:00
2019-07-20 00:50:13 +00:00
updateMessageToEngines[updateType].Add(engine);
2019-06-17 00:56:36 +00:00
}
}
if (duplicateMutations.Count > 0)
{
2019-07-19 23:15:48 +00:00
var errorString = "Multiple Engines update the same Component: ";
2019-06-17 00:56:36 +00:00
foreach (var componentType in duplicateMutations)
{
errorString += "\n" +
2019-07-19 23:15:48 +00:00
componentType.Name + " updated by: " +
2019-07-20 00:50:13 +00:00
string.Join(", ", updateMessageToEngines[componentType].Select((engine) => engine.GetType().Name));
2019-06-17 00:56:36 +00:00
}
2019-07-19 23:15:48 +00:00
throw new EngineUpdateConflictException(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,
2019-08-01 23:24:57 +00:00
componentMessageManager,
2019-06-19 21:14:44 +00:00
renderManager
2019-06-15 00:51:06 +00:00
);
2019-06-15 00:03:56 +00:00
2019-07-18 01:53:31 +00:00
componentManager.PerformComponentUpdates();
componentManager.RemoveMarkedComponents();
componentManager.AddMarkedComponents();
2019-07-18 01:53:31 +00:00
2019-06-15 00:03:56 +00:00
return world;
}
}
}