353 lines
15 KiB
C#
353 lines
15 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Reflection;
|
|
using System.Linq;
|
|
using Encompass.Exceptions;
|
|
using Encompass.Engines;
|
|
using MoonTools.Core.Graph;
|
|
using MoonTools.Core.Graph.Extensions;
|
|
|
|
namespace Encompass
|
|
{
|
|
/// <summary>
|
|
/// WorldBuilder is used to construct a World from Engines, Renderers, and an initial state of Entities, Components, and Messages.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// WorldBuilder enforces certain rules about Engine structure. It is forbidden to have messages create cycles between Engines,
|
|
/// and no Component may be written by more than one Engine.
|
|
/// The WorldBuilder uses Engines and their Message read/emit information to determine a valid ordering of the Engines, which is given to the World.
|
|
/// </remarks>
|
|
public class WorldBuilder
|
|
{
|
|
private readonly List<Engine> engines = new List<Engine>();
|
|
private readonly DirectedGraph<Engine, Unit> engineGraph = GraphBuilder.DirectedGraph<Engine>();
|
|
|
|
private readonly ComponentManager componentManager;
|
|
private readonly EntityManager entityManager;
|
|
private readonly MessageManager messageManager;
|
|
private readonly ComponentMessageManager componentMessageManager;
|
|
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>();
|
|
|
|
public WorldBuilder()
|
|
{
|
|
drawLayerManager = new DrawLayerManager();
|
|
componentManager = new ComponentManager(drawLayerManager);
|
|
messageManager = new MessageManager();
|
|
componentMessageManager = new ComponentMessageManager();
|
|
entityManager = new EntityManager(componentManager, componentMessageManager);
|
|
renderManager = new RenderManager(componentManager, drawLayerManager);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Creates and returns a new empty Entity.
|
|
/// </summary>
|
|
public Entity CreateEntity()
|
|
{
|
|
return entityManager.CreateEntity();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Specifies that the given Message should be sent immediately on the first World Update.
|
|
/// </summary>
|
|
public void SendMessage<TMessage>(TMessage message) where TMessage : struct, IMessage
|
|
{
|
|
messageManager.AddMessage(message);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Specifies that the given Message should be sent after the specified number of seconds after the first World Update.
|
|
/// </summary>
|
|
public void SendMessageDelayed<TMessage>(TMessage message, double time) where TMessage : struct, IMessage
|
|
{
|
|
messageManager.AddMessageDelayed(message, time);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Sets Component data for the specified Component Type on the specified Entity.
|
|
/// </summary>
|
|
public Guid SetComponent<TComponent>(Entity entity, TComponent component, int priority = 0) where TComponent : struct, IComponent
|
|
{
|
|
return componentManager.MarkComponentForWrite(entity, component, priority);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Sets Draw Component data for the specified Component Type on the specified Entity.
|
|
/// This method must be used for the Draw Component to be readable by an OrderedRenderer.
|
|
/// </summary>
|
|
public Guid SetDrawComponent<TComponent>(Entity entity, TComponent component, int priority = 0, int layer = 0) where TComponent : struct, IComponent, IDrawComponent
|
|
{
|
|
return componentManager.MarkDrawComponentForWrite(entity, component, priority, layer);
|
|
}
|
|
|
|
internal void RegisterComponent(Type componentType)
|
|
{
|
|
registeredComponentTypes.Add(componentType);
|
|
AddEngine((Engine)Activator.CreateInstance(typeof(ComponentMessageEmitter<>).MakeGenericType(componentType)));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Adds the specified Engine to the World.
|
|
/// </summary>
|
|
/// <param name="engine">An instance of an Engine.</param>
|
|
public Engine AddEngine<TEngine>(TEngine engine) where TEngine : Engine
|
|
{
|
|
engine.AssignEntityManager(entityManager);
|
|
engine.AssignComponentManager(componentManager);
|
|
engine.AssignMessageManager(messageManager);
|
|
engine.AssignComponentMessageManager(componentMessageManager);
|
|
|
|
engines.Add(engine);
|
|
engineGraph.AddNode(engine);
|
|
|
|
var messageReceiveTypes = engine.receiveTypes;
|
|
var messageSendTypes = engine.sendTypes;
|
|
|
|
foreach (var messageType in messageReceiveTypes.Union(messageSendTypes))
|
|
{
|
|
messageManager.RegisterMessageType(messageType);
|
|
}
|
|
|
|
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);
|
|
}
|
|
|
|
return engine;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Adds the specified OrderedRenderer to the World.
|
|
/// </summary>
|
|
public OrderedRenderer<TComponent> AddOrderedRenderer<TComponent>(OrderedRenderer<TComponent> renderer) where TComponent : struct, IComponent, IDrawComponent
|
|
{
|
|
renderer.AssignEntityManager(entityManager);
|
|
renderer.AssignComponentManager(componentManager);
|
|
renderManager.RegisterOrderedRenderer<TComponent>(renderer.InternalRender);
|
|
return renderer;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Adds the specified GeneralRenderer to the World at the specified layer.
|
|
/// Higher layer numbers draw on top of lower layer numbers.
|
|
/// </summary>
|
|
/// <param name="renderer">An instance of a GeneralRenderer.</param>
|
|
/// <param name="layer">The layer at which the GeneralRenderer should render. Higher numbers draw over lower numbers.</param>
|
|
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()
|
|
{
|
|
foreach (var senderEngine in senders)
|
|
{
|
|
foreach (var messageType in senderEngine.sendTypes)
|
|
{
|
|
if (typeToReaders.ContainsKey(messageType))
|
|
{
|
|
foreach (var readerEngine in typeToReaders[messageType])
|
|
{
|
|
if (senderEngine != readerEngine)
|
|
{
|
|
if (!engineGraph.Exists(senderEngine, readerEngine))
|
|
{
|
|
engineGraph.AddEdge(senderEngine, readerEngine);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Builds the World out of the state specified on the WorldBuilder.
|
|
/// Validates and constructs an ordering of the given Engines.
|
|
/// </summary>
|
|
/// <returns>An instance of World.</returns>
|
|
public World Build()
|
|
{
|
|
BuildEngineGraph();
|
|
|
|
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 writtenComponentTypesWithoutPriority = new HashSet<Type>();
|
|
var writtenComponentTypesWithPriority = new HashSet<Type>();
|
|
var duplicateWritesWithoutPriority = new List<Type>();
|
|
var duplicateWritesWithSamePriority = new List<Type>();
|
|
var writePriorities = new Dictionary<Type, HashSet<int>>();
|
|
var writeMessageToEngines = new Dictionary<Type, List<Engine>>();
|
|
|
|
foreach (var engine in engines)
|
|
{
|
|
var defaultWritePriorityAttribute = engine.GetType().GetCustomAttribute<DefaultWritePriority>(false);
|
|
|
|
var writeTypes = engine.sendTypes.Where((type) => type.IsGenericType && type.GetGenericTypeDefinition() == typeof(ComponentWriteMessage<>));
|
|
|
|
foreach (var writeType in writeTypes)
|
|
{
|
|
var componentType = writeType.GetGenericArguments()[0];
|
|
|
|
int? priority = null;
|
|
if (engine.writePriorities.ContainsKey(componentType))
|
|
{
|
|
priority = engine.writePriorities[componentType];
|
|
}
|
|
else if (defaultWritePriorityAttribute != null)
|
|
{
|
|
priority = defaultWritePriorityAttribute.writePriority;
|
|
}
|
|
|
|
if (priority.HasValue)
|
|
{
|
|
writtenComponentTypesWithPriority.Add(componentType);
|
|
|
|
if (!writePriorities.ContainsKey(componentType))
|
|
{
|
|
writePriorities[componentType] = new HashSet<int>();
|
|
}
|
|
|
|
if (writePriorities[componentType].Contains(priority.Value))
|
|
{
|
|
duplicateWritesWithSamePriority.Add(componentType);
|
|
}
|
|
else if (writtenComponentTypesWithoutPriority.Contains(componentType))
|
|
{
|
|
duplicateWritesWithoutPriority.Add(componentType);
|
|
}
|
|
else
|
|
{
|
|
writePriorities[componentType].Add(priority.Value);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (writtenComponentTypesWithoutPriority.Contains(componentType) || writtenComponentTypesWithPriority.Contains(componentType))
|
|
{
|
|
duplicateWritesWithoutPriority.Add(componentType);
|
|
}
|
|
else
|
|
{
|
|
writtenComponentTypesWithoutPriority.Add(componentType);
|
|
}
|
|
}
|
|
|
|
if (!writeMessageToEngines.ContainsKey(componentType))
|
|
{
|
|
writeMessageToEngines[componentType] = new List<Engine>();
|
|
}
|
|
|
|
writeMessageToEngines[componentType].Add(engine);
|
|
}
|
|
}
|
|
|
|
if (duplicateWritesWithoutPriority.Count > 0)
|
|
{
|
|
var errorString = "Multiple Engines write the same Component without declaring priority: ";
|
|
foreach (var componentType in duplicateWritesWithoutPriority)
|
|
{
|
|
errorString += "\n" +
|
|
componentType.Name + " written by: " +
|
|
string.Join(", ", writeMessageToEngines[componentType].Select((engine) => engine.GetType().Name));
|
|
}
|
|
errorString += "\nTo resolve the conflict, add priority arguments to the Writes declarations or use a DefaultWritePriority attribute.";
|
|
|
|
throw new EngineWriteConflictException(errorString);
|
|
}
|
|
|
|
if (duplicateWritesWithSamePriority.Count > 0)
|
|
{
|
|
var errorString = "Multiple Engines write the same Component with the same priority: ";
|
|
foreach (var componentType in duplicateWritesWithSamePriority)
|
|
{
|
|
errorString += "\n" +
|
|
componentType.Name + " written by: " +
|
|
string.Join(", ", writeMessageToEngines[componentType].Select(engine => engine.GetType().Name));
|
|
}
|
|
errorString += "\nTo resolve the conflict, add priority arguments to the Writes declarations or use a DefaultWritePriority attribute.";
|
|
|
|
throw new EngineWriteConflictException(errorString);
|
|
}
|
|
|
|
var engineOrder = new List<Engine>();
|
|
foreach (var engine in engineGraph.TopologicalSort())
|
|
{
|
|
engineOrder.Add(engine);
|
|
}
|
|
|
|
var world = new World(
|
|
engineOrder,
|
|
entityManager,
|
|
componentManager,
|
|
messageManager,
|
|
componentMessageManager,
|
|
renderManager
|
|
);
|
|
|
|
componentManager.RemoveMarkedComponents();
|
|
componentManager.WriteComponents();
|
|
|
|
return world;
|
|
}
|
|
}
|
|
}
|