caching more info in ComponentMessageManager

pull/5/head
thatcosmonaut 2019-08-19 17:30:31 -07:00
parent bea247e896
commit 1b30f3bd39
3 changed files with 34 additions and 7 deletions

View File

@ -9,6 +9,9 @@ namespace Encompass
class ComponentMessageManager class ComponentMessageManager
{ {
private readonly Dictionary<Guid, IComponent> componentIDToComponent = new Dictionary<Guid, IComponent>(); private readonly Dictionary<Guid, IComponent> componentIDToComponent = new Dictionary<Guid, IComponent>();
private readonly Dictionary<Guid, Type> componentIDToType = new Dictionary<Guid, Type>();
private readonly Dictionary<Guid, Guid> componentIDToEntityID = new Dictionary<Guid, Guid>();
private readonly Dictionary<Type, HashSet<Guid>> componentMessageTypeToExistingComponentIDs = new Dictionary<Type, HashSet<Guid>>(); private readonly Dictionary<Type, HashSet<Guid>> componentMessageTypeToExistingComponentIDs = new Dictionary<Type, HashSet<Guid>>();
private readonly Dictionary<Type, HashSet<Guid>> componentMessageTypeToPendingComponentIDs = new Dictionary<Type, HashSet<Guid>>(); private readonly Dictionary<Type, HashSet<Guid>> componentMessageTypeToPendingComponentIDs = new Dictionary<Type, HashSet<Guid>>();
@ -40,6 +43,8 @@ namespace Encompass
internal void ClearMessages() internal void ClearMessages()
{ {
componentIDToComponent.Clear(); componentIDToComponent.Clear();
componentIDToType.Clear();
componentIDToEntityID.Clear();
foreach (var set in componentMessageTypeToExistingComponentIDs.Values) foreach (var set in componentMessageTypeToExistingComponentIDs.Values)
{ {
@ -117,6 +122,8 @@ namespace Encompass
private void RegisterExistingOrPendingComponentMessage<TComponent>(Entity entity, Guid componentID, TComponent component) where TComponent: struct, IComponent private void RegisterExistingOrPendingComponentMessage<TComponent>(Entity entity, Guid componentID, TComponent component) where TComponent: struct, IComponent
{ {
componentIDToComponent[componentID] = component; componentIDToComponent[componentID] = component;
componentIDToEntityID[componentID] = entity.ID;
componentIDToType[componentID] = typeof(TComponent);
if (!componentMessageTypeToComponentIDs.ContainsKey(typeof(TComponent))) if (!componentMessageTypeToComponentIDs.ContainsKey(typeof(TComponent)))
{ {
@ -250,5 +257,20 @@ namespace Encompass
{ {
return entityToTypeToPendingComponentID.ContainsKey(entity) && entityToTypeToPendingComponentID[entity].ContainsKey(typeof(TComponent)); return entityToTypeToPendingComponentID.ContainsKey(entity) && entityToTypeToPendingComponentID[entity].ContainsKey(typeof(TComponent));
} }
internal IComponent GetComponentByID(Guid componentID)
{
return componentIDToComponent[componentID];
}
internal Type GetComponentTypeByID(Guid componentID)
{
return componentIDToType[componentID];
}
internal Guid GetEntityIDByComponentID(Guid componentID)
{
return componentIDToEntityID[componentID];
}
} }
} }

View File

@ -12,8 +12,8 @@ namespace Encompass
internal readonly HashSet<Type> receiveTypes = new HashSet<Type>(); internal readonly HashSet<Type> receiveTypes = new HashSet<Type>();
private EntityManager entityManager; private EntityManager entityManager;
private ComponentManager componentManager;
private MessageManager messageManager; private MessageManager messageManager;
private ComponentManager componentManager;
private ComponentMessageManager componentMessageManager; private ComponentMessageManager componentMessageManager;
protected Engine() protected Engine()
@ -94,7 +94,7 @@ namespace Encompass
protected Guid GetEntityIDByComponentID(Guid componentID) protected Guid GetEntityIDByComponentID(Guid componentID)
{ {
return componentManager.GetEntityIDByComponentID(componentID); return componentMessageManager.GetEntityIDByComponentID(componentID);
} }
protected Entity GetEntityByComponentID(Guid componentID) protected Entity GetEntityByComponentID(Guid componentID)
@ -104,12 +104,12 @@ namespace Encompass
protected TComponent GetComponentByID<TComponent>(Guid componentID) where TComponent : struct, IComponent protected TComponent GetComponentByID<TComponent>(Guid componentID) where TComponent : struct, IComponent
{ {
if (componentManager.GetComponentTypeByID(componentID) != typeof(TComponent)) if (componentMessageManager.GetComponentTypeByID(componentID) != typeof(TComponent))
{ {
throw new ComponentTypeMismatchException("Expected Component to be of type {0} but was actually of type {1}", typeof(TComponent).Name, componentManager.GetComponentTypeByID(componentID).Name); throw new ComponentTypeMismatchException("Expected Component to be of type {0} but was actually of type {1}", typeof(TComponent).Name, componentMessageManager.GetComponentTypeByID(componentID).Name);
} }
return (TComponent)componentManager.GetComponentByID(componentID); return (TComponent)componentMessageManager.GetComponentByID(componentID);
} }
internal IEnumerable<(Guid, TComponent)> ReadComponentsFromWorld<TComponent>() where TComponent : struct, IComponent internal IEnumerable<(Guid, TComponent)> ReadComponentsFromWorld<TComponent>() where TComponent : struct, IComponent
@ -117,6 +117,11 @@ namespace Encompass
return componentManager.GetComponentsByType<TComponent>(); return componentManager.GetComponentsByType<TComponent>();
} }
internal Entity ReadEntityFromWorld(Guid componentID)
{
return GetEntity(componentManager.GetEntityIDByComponentID(componentID));
}
protected Guid AddComponent<TComponent>(Entity entity, TComponent component) where TComponent : struct, IComponent protected Guid AddComponent<TComponent>(Entity entity, TComponent component) where TComponent : struct, IComponent
{ {
var componentID = componentManager.MarkComponentForAdd(entity, component); var componentID = componentManager.MarkComponentForAdd(entity, component);

View File

@ -14,7 +14,7 @@ namespace Encompass.Engines
foreach (var (componentID, component) in ReadComponentsFromWorld<TComponent>()) foreach (var (componentID, component) in ReadComponentsFromWorld<TComponent>())
{ {
ComponentMessage<TComponent> componentMessage; ComponentMessage<TComponent> componentMessage;
componentMessage.entity = GetEntityByComponentID(componentID); componentMessage.entity = ReadEntityFromWorld(componentID);
componentMessage.componentID = componentID; componentMessage.componentID = componentID;
componentMessage.component = component; componentMessage.component = component;
SendMessage(componentMessage); SendMessage(componentMessage);