fix emitters not being generated for certain component types +rename component message manager

pull/5/head
Evan Hemsley 2019-12-05 22:32:09 -08:00
parent 005405512e
commit 8218ada6d1
11 changed files with 76 additions and 103 deletions

View File

@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
namespace Encompass
{
@ -69,11 +68,6 @@ namespace Encompass
return Lookup<TComponent>().Count > 0;
}
// public IEnumerable<TComponent> All<TComponent>() where TComponent : struct, IComponent
// {
// return Lookup<TComponent>().All<TComponent>();
// }
public IEnumerable<(Entity, Type, IComponent)> AllInterfaceTyped()
{
foreach (var store in Stores.Values)

View File

@ -6,15 +6,15 @@ namespace Encompass
internal class ComponentManager
{
private readonly DrawLayerManager drawLayerManager;
private readonly ComponentMessageManager componentMessageManager;
private readonly ComponentUpdateManager componentUpdateManager;
private readonly ComponentStore componentStore = new ComponentStore();
private readonly HashSet<Entity> entitiesMarkedForRemoval = new HashSet<Entity>();
public ComponentManager(DrawLayerManager drawLayerManager, ComponentMessageManager componentMessageManager)
public ComponentManager(DrawLayerManager drawLayerManager, ComponentUpdateManager componentUpdateManager)
{
this.drawLayerManager = drawLayerManager;
this.componentMessageManager = componentMessageManager;
this.componentUpdateManager = componentUpdateManager;
}
internal void SetComponentStore(ComponentStore componentStore)
@ -29,12 +29,12 @@ namespace Encompass
internal void AddComponent<TComponent>(Entity entity, TComponent component) where TComponent : struct, IComponent
{
componentStore.Set<TComponent>(entity, component);
componentStore.Set(entity, component);
}
internal void WriteComponents()
{
componentStore.SwapWith(componentMessageManager.UpToDateComponentStore);
componentStore.SwapWith(componentUpdateManager.UpToDateComponentStore);
}
internal IEnumerable<(TComponent, Entity)> GetComponentsIncludingEntity<TComponent>() where TComponent : struct, IComponent
@ -80,8 +80,7 @@ namespace Encompass
public void Remove<TComponent>(Entity entity) where TComponent : struct, IComponent
{
//componentStore.Remove<TComponent>(entity);
componentMessageManager.Remove<TComponent>(entity);
componentUpdateManager.Remove<TComponent>(entity);
drawLayerManager.UnRegisterComponentWithLayer<TComponent>(entity);
}
}

View File

@ -1,25 +1,21 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Encompass.Exceptions;
namespace Encompass
{
class ComponentMessageManager
internal class ComponentUpdateManager
{
private readonly ComponentStore componentStore = new ComponentStore();
private readonly ComponentStore existingAndPendingComponentStore = new ComponentStore();
private readonly ComponentStore existingComponentStore = new ComponentStore();
private readonly ComponentStore pendingComponentStore = new ComponentStore();
private ComponentStore upToDateComponentStore = new ComponentStore();
private readonly Dictionary<Type, Dictionary<Entity, int>> typeToEntityToPendingComponentPriority = new Dictionary<Type, Dictionary<Entity, int>>(128);
public ComponentStore UpToDateComponentStore { get => upToDateComponentStore; }
public ComponentStore UpToDateComponentStore { get; private set; } = new ComponentStore();
internal void ClearMessages()
internal void Clear()
{
componentStore.ClearAll();
existingAndPendingComponentStore.ClearAll();
existingComponentStore.ClearAll();
pendingComponentStore.ClearAll();
UpToDateComponentStore.ClearAll();
@ -32,7 +28,7 @@ namespace Encompass
internal void SetStartingComponentStore(ComponentStore componentStore)
{
upToDateComponentStore = componentStore;
UpToDateComponentStore = componentStore;
}
internal void AddExistingComponent<TComponent>(Entity entity, TComponent component) where TComponent : struct, IComponent
@ -52,20 +48,20 @@ namespace Encompass
private void RegisterExistingOrPendingComponentMessage<TComponent>(Entity entity, TComponent component) where TComponent : struct, IComponent
{
componentStore.Set(entity, component);
upToDateComponentStore.Set(entity, component);
existingAndPendingComponentStore.Set(entity, component);
UpToDateComponentStore.Set(entity, component);
}
public void UpdateComponent<TComponent>(Entity entity, TComponent component, int priority) where TComponent : struct, IComponent
{
upToDateComponentStore.Set<TComponent>(entity, component, priority);
UpToDateComponentStore.Set<TComponent>(entity, component, priority);
}
// general component reads by type
internal IEnumerable<(Entity, TComponent)> ReadExistingAndPendingComponentsByType<TComponent>() where TComponent : struct, IComponent
{
return componentStore.All<TComponent>();
return existingAndPendingComponentStore.All<TComponent>();
}
internal IEnumerable<(Entity, TComponent)> ReadExistingComponentsByType<TComponent>() where TComponent : struct, IComponent
@ -102,7 +98,7 @@ namespace Encompass
internal bool SomeExistingOrPendingComponent<TComponent>() where TComponent : struct, IComponent
{
return componentStore.Any<TComponent>();
return existingAndPendingComponentStore.Any<TComponent>();
}
internal bool SomeExistingComponent<TComponent>() where TComponent : struct, IComponent
@ -131,12 +127,12 @@ namespace Encompass
internal bool HasExistingOrPendingComponent<TComponent>(Entity entity) where TComponent : struct, IComponent
{
return componentStore.Has<TComponent>(entity);
return existingAndPendingComponentStore.Has<TComponent>(entity);
}
internal bool HasExistingOrPendingComponent(Entity entity, Type type)
{
return componentStore.Has(type, entity);
return existingAndPendingComponentStore.Has(type, entity);
}
internal bool HasExistingComponent<TComponent>(Entity entity) where TComponent : struct, IComponent
@ -161,7 +157,7 @@ namespace Encompass
internal void Remove<TComponent>(Entity entity) where TComponent : struct, IComponent
{
upToDateComponentStore.Remove<TComponent>(entity);
UpToDateComponentStore.Remove<TComponent>(entity);
}
}
}

View File

@ -28,14 +28,11 @@ namespace Encompass
/// </summary>
internal bool usesTimeDilation = true;
public bool TimeDilationActive { get => usesTimeDilation && timeManager.TimeDilationActive; }
/// <summary>
/// Used when activating time dilation. Lower priority overrides higher priority.
/// </summary>
private EntityManager entityManager;
private MessageManager messageManager;
private ComponentManager componentManager;
private ComponentMessageManager componentMessageManager;
private ComponentUpdateManager componentUpdateManager;
private TimeManager timeManager;
protected Engine()
@ -81,9 +78,9 @@ namespace Encompass
public override bool Equals(object obj)
{
if (obj is Engine)
if (obj is Engine engine)
{
return this.Equals((Engine)obj);
return Equals(engine);
}
return false;
@ -114,9 +111,9 @@ namespace Encompass
this.messageManager = messageManager;
}
internal void AssignComponentMessageManager(ComponentMessageManager componentMessageManager)
internal void AssignComponentUpdateManager(ComponentUpdateManager componentUpdateManager)
{
this.componentMessageManager = componentMessageManager;
this.componentUpdateManager = componentUpdateManager;
}
internal void AssignTimeManager(TimeManager timeManager)
@ -186,15 +183,15 @@ namespace Encompass
var existingRead = readTypes.Contains(typeof(TComponent));
if (existingRead && pendingRead)
{
return componentMessageManager.ReadExistingAndPendingComponentsByType<TComponent>();
return componentUpdateManager.ReadExistingAndPendingComponentsByType<TComponent>();
}
else if (existingRead)
{
return componentMessageManager.ReadExistingComponentsByType<TComponent>();
return componentUpdateManager.ReadExistingComponentsByType<TComponent>();
}
else if (pendingRead)
{
return componentMessageManager.ReadPendingComponentsByType<TComponent>();
return componentUpdateManager.ReadPendingComponentsByType<TComponent>();
}
else
{
@ -229,15 +226,15 @@ namespace Encompass
var existingRead = readTypes.Contains(typeof(TComponent));
if (existingRead && pendingRead)
{
return componentMessageManager.ReadFirstExistingOrPendingComponentByType<TComponent>();
return componentUpdateManager.ReadFirstExistingOrPendingComponentByType<TComponent>();
}
else if (existingRead)
{
return componentMessageManager.ReadFirstExistingComponentByType<TComponent>();
return componentUpdateManager.ReadFirstExistingComponentByType<TComponent>();
}
else if (pendingRead)
{
return componentMessageManager.ReadFirstPendingComponentByType<TComponent>();
return componentUpdateManager.ReadFirstPendingComponentByType<TComponent>();
}
else
{
@ -271,15 +268,15 @@ namespace Encompass
var existingRead = readTypes.Contains(typeof(TComponent));
if (existingRead && pendingRead)
{
return componentMessageManager.SomeExistingOrPendingComponent<TComponent>();
return componentUpdateManager.SomeExistingOrPendingComponent<TComponent>();
}
else if (existingRead)
{
return componentMessageManager.SomeExistingComponent<TComponent>();
return componentUpdateManager.SomeExistingComponent<TComponent>();
}
else if (pendingRead)
{
return componentMessageManager.SomePendingComponent<TComponent>();
return componentUpdateManager.SomePendingComponent<TComponent>();
}
else
{
@ -293,13 +290,13 @@ namespace Encompass
var existingRead = readTypes.Contains(typeof(TComponent));
if (existingRead && pendingRead)
{
if (componentMessageManager.HasPendingComponent<TComponent>(entity))
if (componentUpdateManager.HasPendingComponent<TComponent>(entity))
{
return componentMessageManager.ReadPendingComponentByEntityAndType<TComponent>(entity);
return componentUpdateManager.ReadPendingComponentByEntityAndType<TComponent>(entity);
}
else if (componentMessageManager.HasExistingComponent<TComponent>(entity))
else if (componentUpdateManager.HasExistingComponent<TComponent>(entity))
{
return componentMessageManager.ReadExistingComponentByEntityAndType<TComponent>(entity);
return componentUpdateManager.ReadExistingComponentByEntityAndType<TComponent>(entity);
}
else
{
@ -308,11 +305,11 @@ namespace Encompass
}
else if (existingRead)
{
return componentMessageManager.ReadExistingComponentByEntityAndType<TComponent>(entity);
return componentUpdateManager.ReadExistingComponentByEntityAndType<TComponent>(entity);
}
else if (pendingRead)
{
return componentMessageManager.ReadPendingComponentByEntityAndType<TComponent>(entity);
return componentUpdateManager.ReadPendingComponentByEntityAndType<TComponent>(entity);
}
else
{
@ -347,15 +344,15 @@ namespace Encompass
if (pendingRead && existingRead)
{
return componentMessageManager.HasExistingOrPendingComponent<TComponent>(entity);
return componentUpdateManager.HasExistingOrPendingComponent<TComponent>(entity);
}
else if (existingRead)
{
return componentMessageManager.HasExistingComponent<TComponent>(entity);
return componentUpdateManager.HasExistingComponent<TComponent>(entity);
}
else if (pendingRead)
{
return componentMessageManager.HasPendingComponent<TComponent>(entity);
return componentUpdateManager.HasPendingComponent<TComponent>(entity);
}
else
{
@ -376,15 +373,15 @@ namespace Encompass
if (pendingRead && existingRead)
{
return componentMessageManager.HasExistingOrPendingComponent(entity, type);
return componentUpdateManager.HasExistingOrPendingComponent(entity, type);
}
else if (existingRead)
{
return componentMessageManager.HasExistingComponent(entity, type);
return componentUpdateManager.HasExistingComponent(entity, type);
}
else if (pendingRead)
{
return componentMessageManager.HasPendingComponent(entity, type);
return componentUpdateManager.HasPendingComponent(entity, type);
}
else
{
@ -413,7 +410,7 @@ namespace Encompass
}
else
{
componentMessageManager.UpdateComponent(entity, component, priority);
componentUpdateManager.UpdateComponent(entity, component, priority);
}
if (component is IDrawableComponent drawableComponent)
@ -471,12 +468,12 @@ namespace Encompass
internal void AddExistingComponent<TComponent>(Entity entity, TComponent component) where TComponent : struct, IComponent
{
componentMessageManager.AddExistingComponent(entity, component);
componentUpdateManager.AddExistingComponent(entity, component);
}
internal void AddPendingComponent<TComponent>(Entity entity, TComponent component, int priority) where TComponent : struct, IComponent
{
componentMessageManager.AddPendingComponent<TComponent>(entity, component, priority);
componentUpdateManager.AddPendingComponent<TComponent>(entity, component, priority);
}
/// <summary>

View File

@ -12,14 +12,14 @@ namespace Encompass
internal Entity(Guid id)
{
this.ID = id;
ID = id;
}
public override bool Equals(object obj)
{
if (obj is Entity)
if (obj is Entity entity)
{
return this.Equals((Entity)obj);
return Equals(entity);
}
return false;

View File

@ -5,21 +5,13 @@ namespace Encompass
{
internal class RenderManager
{
private readonly ComponentManager componentManager;
private readonly DrawLayerManager drawLayerManager;
private readonly EntityManager entityManager;
private readonly Dictionary<Type, Action<Entity, IComponent>> drawComponentTypeToOrderedRenderer = new Dictionary<Type, Action<Entity, IComponent>>(256);
public RenderManager(
ComponentManager componentManager,
DrawLayerManager drawLayerManager,
EntityManager entityManager
)
public RenderManager(DrawLayerManager drawLayerManager)
{
this.componentManager = componentManager;
this.drawLayerManager = drawLayerManager;
this.entityManager = entityManager;
}
public void RegisterOrderedRenderer<TComponent>(Action<Entity, IComponent> renderAction) where TComponent : struct, IComponent

View File

@ -14,23 +14,21 @@ namespace Encompass
{
get
{
double calculatedFactor = 1;
if (elapsedTime < easeInTime)
{
calculatedFactor = easeInFunction(elapsedTime, 1, factor - 1, easeInTime);
return easeInFunction(elapsedTime, 1, factor - 1, easeInTime);
}
else if (elapsedTime < easeInTime + activeTime)
{
calculatedFactor = factor;
return factor;
}
else if (elapsedTime < easeInTime + activeTime + easeOutTime)
{
var elapsedOutTime = elapsedTime - easeInTime - activeTime;
calculatedFactor = easeOutFunction(elapsedOutTime, factor, 1 - factor, easeOutTime);
return easeOutFunction(elapsedOutTime, factor, 1 - factor, easeOutTime);
}
return calculatedFactor;
return 1;
}
}
}

View File

@ -9,7 +9,7 @@ namespace Encompass
private double Linear(double t, double b, double c, double d)
{
return c * t / d + b;
return (c * t / d) + b;
}
public double TimeDilationFactor

View File

@ -11,7 +11,7 @@ namespace Encompass
private readonly EntityManager entityManager;
private readonly ComponentManager componentManager;
private readonly MessageManager messageManager;
private readonly ComponentMessageManager componentMessageManager;
private readonly ComponentUpdateManager componentUpdateManager;
private readonly TimeManager timeManager;
private readonly RenderManager renderManager;
@ -20,7 +20,7 @@ namespace Encompass
EntityManager entityManager,
ComponentManager componentManager,
MessageManager messageManager,
ComponentMessageManager componentMessageManager,
ComponentUpdateManager componentUpdateManager,
TimeManager timeManager,
RenderManager renderManager
)
@ -29,7 +29,7 @@ namespace Encompass
this.entityManager = entityManager;
this.componentManager = componentManager;
this.messageManager = messageManager;
this.componentMessageManager = componentMessageManager;
this.componentUpdateManager = componentUpdateManager;
this.timeManager = timeManager;
this.renderManager = renderManager;
}
@ -61,7 +61,7 @@ namespace Encompass
componentManager.WriteComponents();
componentManager.RemoveMarkedComponents();
componentMessageManager.ClearMessages();
componentUpdateManager.Clear();
}
/// <summary>

View File

@ -21,12 +21,12 @@ namespace Encompass
private readonly List<Engine> engines = new List<Engine>();
private readonly DirectedGraph<Engine, Unit> engineGraph = GraphBuilder.DirectedGraph<Engine>();
private readonly ComponentStore startingComponentStoreForComponentManager = new ComponentStore();
private readonly ComponentStore startingComponentStoreForComponentMessageManager = new ComponentStore();
private readonly ComponentStore startingComponentStoreForComponentUpdateManager = new ComponentStore();
private readonly ComponentManager componentManager;
private readonly EntityManager entityManager;
private readonly MessageManager messageManager;
private readonly ComponentMessageManager componentMessageManager;
private readonly ComponentUpdateManager componentUpdateManager;
private readonly TimeManager timeManager;
private readonly DrawLayerManager drawLayerManager;
private readonly RenderManager renderManager;
@ -41,11 +41,11 @@ namespace Encompass
{
drawLayerManager = new DrawLayerManager();
timeManager = new TimeManager();
componentMessageManager = new ComponentMessageManager();
componentManager = new ComponentManager(drawLayerManager, componentMessageManager);
componentUpdateManager = new ComponentUpdateManager();
componentManager = new ComponentManager(drawLayerManager, componentUpdateManager);
messageManager = new MessageManager(timeManager);
entityManager = new EntityManager(componentManager);
renderManager = new RenderManager(componentManager, drawLayerManager, entityManager);
renderManager = new RenderManager(drawLayerManager);
}
/// <summary>
@ -78,7 +78,9 @@ namespace Encompass
public void SetComponent<TComponent>(Entity entity, TComponent component) where TComponent : struct, IComponent
{
startingComponentStoreForComponentManager.Set(entity, component);
startingComponentStoreForComponentMessageManager.Set(entity, component);
startingComponentStoreForComponentUpdateManager.Set(entity, component);
RegisterComponent(typeof(TComponent));
if (component is IDrawableComponent drawableComponent)
{
@ -101,7 +103,7 @@ namespace Encompass
engine.AssignEntityManager(entityManager);
engine.AssignComponentManager(componentManager);
engine.AssignMessageManager(messageManager);
engine.AssignComponentMessageManager(componentMessageManager);
engine.AssignComponentUpdateManager(componentUpdateManager);
engine.AssignTimeManager(timeManager);
engines.Add(engine);
@ -130,11 +132,11 @@ namespace Encompass
senders.Add(engine);
}
foreach (var readTypes in engine.readTypes)
foreach (var componentType in engine.readTypes.Union(engine.writeTypes))
{
if (!registeredComponentTypes.Contains(readTypes))
if (!registeredComponentTypes.Contains(componentType))
{
RegisterComponent(readTypes);
RegisterComponent(componentType);
}
}
@ -335,13 +337,13 @@ namespace Encompass
entityManager,
componentManager,
messageManager,
componentMessageManager,
componentUpdateManager,
timeManager,
renderManager
);
componentUpdateManager.SetStartingComponentStore(startingComponentStoreForComponentUpdateManager);
componentManager.SetComponentStore(startingComponentStoreForComponentManager);
componentMessageManager.SetStartingComponentStore(startingComponentStoreForComponentMessageManager);
return world;
}

View File

@ -1,12 +1,7 @@
using System.ComponentModel;
using NUnit.Framework;
using FluentAssertions;
using Encompass;
using System.Collections.Generic;
using System;
using System.Linq;
using Encompass.Exceptions;
namespace Tests
{