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;
|
2019-06-24 19:14:37 +00:00
|
|
|
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;
|
|
|
|
private readonly DrawLayerManager drawLayerManager;
|
|
|
|
private readonly RenderManager renderManager;
|
2019-06-15 00:03:56 +00:00
|
|
|
|
2019-07-16 18:17:07 +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()
|
|
|
|
{
|
2019-07-11 01:22:08 +00:00
|
|
|
var entitiesWithAddedComponents = new HashSet<Guid>();
|
|
|
|
var entitiesWithRemovedComponents = new HashSet<Guid>();
|
2019-06-20 00:40:01 +00:00
|
|
|
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();
|
2019-06-20 00:40:01 +00:00
|
|
|
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)
|
|
|
|
{
|
2019-07-18 01:12:29 +00:00
|
|
|
componentManager.MarkForDeactivation(componentID);
|
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-06-24 19:26:19 +00:00
|
|
|
public Engine AddEngine<TEngine>(TEngine engine) where TEngine : Engine
|
2019-06-16 01:05:56 +00:00
|
|
|
{
|
2019-06-24 19:14:37 +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);
|
|
|
|
|
2019-07-18 21:02:57 +00:00
|
|
|
foreach (var activateType in engine.activateTypes)
|
2019-06-17 00:56:36 +00:00
|
|
|
{
|
2019-07-19 01:20:38 +00:00
|
|
|
engine.sendTypes.Add(activateType);
|
2019-07-18 21:02:57 +00:00
|
|
|
}
|
|
|
|
|
2019-07-19 03:31:31 +00:00
|
|
|
var messageReadTypes = engine.readTypes;
|
2019-07-19 01:20:38 +00:00
|
|
|
var messageSendTypes = engine.sendTypes;
|
2019-06-17 00:56:36 +00:00
|
|
|
|
2019-07-19 03:31:31 +00:00
|
|
|
foreach (var messageType in messageReadTypes.Intersect(messageSendTypes))
|
2019-07-18 21:02:57 +00:00
|
|
|
{
|
2019-07-19 03:31:31 +00:00
|
|
|
// ComponentMessages can safely self-cycle
|
|
|
|
// this does introduce a gotcha though: if you AddComponent and then HasComponent or GetComponent you will receive a false negative
|
|
|
|
// there is no point to doing this but it is a gotcha i suppose
|
|
|
|
if (!(messageType.IsGenericType && messageType.GetGenericTypeDefinition() == typeof(ComponentMessage<>)))
|
|
|
|
{
|
|
|
|
throw new EngineMessageSelfCycleException("Engine {0} both reads and writes 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);
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach (var readType in engine.readTypes)
|
|
|
|
{
|
|
|
|
if (readType.IsGenericType && readType.GetGenericTypeDefinition() == typeof(ComponentMessage<>))
|
2019-06-29 05:57:18 +00:00
|
|
|
{
|
2019-07-18 21:02:57 +00:00
|
|
|
var componentType = readType.GetGenericArguments().Single();
|
|
|
|
if (!registeredComponentTypes.Contains(componentType))
|
2019-06-17 00:56:36 +00:00
|
|
|
{
|
2019-07-18 21:02:57 +00:00
|
|
|
RegisterComponent(componentType);
|
2019-06-17 00:56:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-16 18:17:07 +00:00
|
|
|
if (!typeToReaders.ContainsKey(readType))
|
2019-06-17 00:56:36 +00:00
|
|
|
{
|
2019-07-16 18:17:07 +00:00
|
|
|
typeToReaders.Add(readType, new HashSet<Engine>());
|
2019-06-29 05:57:18 +00:00
|
|
|
}
|
2019-06-17 00:56:36 +00:00
|
|
|
|
2019-07-16 18:17:07 +00:00
|
|
|
typeToReaders[readType].Add(engine);
|
2019-06-17 00:56:36 +00:00
|
|
|
}
|
|
|
|
|
2019-06-15 00:51:06 +00:00
|
|
|
return engine;
|
|
|
|
}
|
|
|
|
|
2019-06-24 23:49:26 +00:00
|
|
|
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);
|
|
|
|
}
|
2019-06-20 05:38:56 +00:00
|
|
|
|
|
|
|
return renderer;
|
|
|
|
}
|
|
|
|
|
2019-06-24 23:49:26 +00:00
|
|
|
public TRenderer AddGeneralRenderer<TRenderer>(TRenderer renderer, int layer) where TRenderer : GeneralRenderer
|
2019-06-20 05:38:56 +00:00
|
|
|
{
|
|
|
|
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])
|
|
|
|
{
|
|
|
|
engineGraph.AddEdge(senderEngine, readerEngine);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-06-29 04:48:04 +00:00
|
|
|
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-19 01:20:38 +00:00
|
|
|
foreach (var updateType in engine.updateTypes)
|
2019-06-17 00:56:36 +00:00
|
|
|
{
|
2019-07-19 01:20:38 +00:00
|
|
|
if (updateType.GetInterfaces().Contains(typeof(IComponent))) // if our write type is a component
|
2019-06-17 00:56:36 +00:00
|
|
|
{
|
2019-07-19 01:20:38 +00:00
|
|
|
if (mutatedComponentTypes.Contains(updateType))
|
2019-06-17 00:56:36 +00:00
|
|
|
{
|
2019-07-19 01:20:38 +00:00
|
|
|
duplicateMutations.Add(updateType);
|
2019-06-17 00:56:36 +00:00
|
|
|
}
|
2019-07-19 01:20:38 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
mutatedComponentTypes.Add(updateType);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!componentToEngines.ContainsKey(updateType))
|
|
|
|
{
|
|
|
|
componentToEngines[updateType] = new List<Engine>();
|
|
|
|
}
|
|
|
|
|
|
|
|
componentToEngines[updateType].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));
|
|
|
|
}
|
|
|
|
|
2019-07-16 18:17:07 +00:00
|
|
|
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
|
|
|
|
2019-07-18 01:53:31 +00:00
|
|
|
componentManager.PerformComponentUpdates();
|
2019-07-18 01:12:29 +00:00
|
|
|
componentManager.DeactivateMarkedComponents();
|
|
|
|
componentManager.RemoveMarkedComponents();
|
2019-07-18 01:53:31 +00:00
|
|
|
|
2019-06-19 23:13:02 +00:00
|
|
|
entityManager.CheckEntitiesWithAddedComponents();
|
|
|
|
entityManager.CheckEntitiesWithRemovedComponents();
|
2019-06-15 00:03:56 +00:00
|
|
|
|
|
|
|
return world;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|