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-15 00:03:56 +00:00
|
|
|
|
2019-06-16 01:05:56 +00:00
|
|
|
namespace Encompass
|
|
|
|
{
|
|
|
|
public class WorldBuilder
|
|
|
|
{
|
2019-06-15 00:51:06 +00:00
|
|
|
private List<Engine> engines = new List<Engine>();
|
2019-06-17 00:56:36 +00:00
|
|
|
private DirectedGraph<Engine> engineGraph = new DirectedGraph<Engine>();
|
2019-06-15 00:51:06 +00:00
|
|
|
|
2019-06-15 00:03:56 +00:00
|
|
|
private ComponentManager componentManager;
|
|
|
|
private EntityManager entityManager;
|
2019-06-16 01:55:35 +00:00
|
|
|
private MessageManager messageManager;
|
2019-06-15 00:03:56 +00:00
|
|
|
|
2019-06-17 00:56:36 +00:00
|
|
|
private Dictionary<Type, HashSet<Engine>> messageTypeToEmitters = new Dictionary<Type, HashSet<Engine>>();
|
|
|
|
private Dictionary<Type, HashSet<Engine>> messageTypeToReaders = new Dictionary<Type, HashSet<Engine>>();
|
|
|
|
|
2019-06-16 01:05:56 +00:00
|
|
|
public WorldBuilder()
|
|
|
|
{
|
2019-06-15 00:03:56 +00:00
|
|
|
componentManager = new ComponentManager();
|
|
|
|
entityManager = new EntityManager(componentManager);
|
2019-06-16 01:55:35 +00:00
|
|
|
messageManager = new MessageManager();
|
2019-06-15 00:03:56 +00:00
|
|
|
}
|
|
|
|
|
2019-06-16 01:05:56 +00:00
|
|
|
public Entity CreateEntity()
|
|
|
|
{
|
2019-06-15 00:03:56 +00:00
|
|
|
return this.entityManager.CreateEntity();
|
|
|
|
}
|
|
|
|
|
2019-06-16 01:05:56 +00:00
|
|
|
public Engine AddEngine<TEngine>() where TEngine : Engine, new()
|
|
|
|
{
|
2019-06-15 00:51:06 +00:00
|
|
|
var engine = new TEngine();
|
|
|
|
|
|
|
|
engine.AssignEntityManager(this.entityManager);
|
|
|
|
engine.AssignComponentManager(this.componentManager);
|
2019-06-16 01:55:35 +00:00
|
|
|
engine.AssignMessageManager(this.messageManager);
|
2019-06-15 00:51:06 +00:00
|
|
|
|
|
|
|
engines.Add(engine);
|
|
|
|
|
2019-06-17 00:56:36 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-15 00:51:06 +00:00
|
|
|
return engine;
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2019-06-15 00:51:06 +00:00
|
|
|
var world = new World(
|
2019-06-17 00:56:36 +00:00
|
|
|
engineOrder,
|
2019-06-15 00:51:06 +00:00
|
|
|
this.entityManager,
|
2019-06-16 01:55:35 +00:00
|
|
|
this.componentManager,
|
|
|
|
this.messageManager
|
2019-06-15 00:51:06 +00:00
|
|
|
);
|
2019-06-15 00:03:56 +00:00
|
|
|
|
|
|
|
this.componentManager.ActivateComponents();
|
|
|
|
this.componentManager.RemoveComponents();
|
|
|
|
|
|
|
|
return world;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|