optimize componentmanager and renderer
parent
d86c002305
commit
5e2ae80518
|
@ -70,57 +70,54 @@ namespace Encompass
|
||||||
|
|
||||||
internal IEnumerable<Guid> GetComponentIDsByEntityID(Guid entityID)
|
internal IEnumerable<Guid> GetComponentIDsByEntityID(Guid entityID)
|
||||||
{
|
{
|
||||||
return entityIDToComponentIDs.ContainsKey(entityID) ?
|
HashSet<Guid> idSet;
|
||||||
entityIDToComponentIDs[entityID] :
|
if (entityIDToComponentIDs.TryGetValue(entityID, out idSet))
|
||||||
Enumerable.Empty<Guid>();
|
{
|
||||||
|
return idSet;
|
||||||
|
}
|
||||||
|
return Enumerable.Empty<Guid>();
|
||||||
}
|
}
|
||||||
|
|
||||||
internal IEnumerable<ValueTuple<Guid, IComponent>> GetComponentsByEntity(Guid entityID)
|
internal IEnumerable<(Guid, TComponent)> GetComponentsByType<TComponent>() where TComponent : struct, IComponent
|
||||||
{
|
{
|
||||||
return GetComponentIDsByEntityID(entityID).Select((id) => new ValueTuple<Guid, IComponent>(id, IDToComponent[id]));
|
HashSet<Guid> idSet;
|
||||||
}
|
if (typeToComponentIDs.TryGetValue(typeof(TComponent), out idSet))
|
||||||
|
{
|
||||||
internal IEnumerable<ValueTuple<Guid, Guid, TComponent>> GetComponentsByType<TComponent>() where TComponent : struct, IComponent
|
return idSet.Select(id => (id, (TComponent)IDToComponent[id]));
|
||||||
{
|
}
|
||||||
return typeToComponentIDs.ContainsKey(typeof(TComponent)) ?
|
return Enumerable.Empty<(Guid, TComponent)>();
|
||||||
typeToComponentIDs[typeof(TComponent)].Select((id) => new ValueTuple<Guid, Guid, TComponent>(GetEntityIDByComponentID(id), id, (TComponent)IDToComponent[id])) :
|
|
||||||
Enumerable.Empty<ValueTuple<Guid, Guid, TComponent>>();
|
|
||||||
}
|
|
||||||
|
|
||||||
internal IEnumerable<ValueTuple<Guid, IComponent>> GetComponentsByType(Type type)
|
|
||||||
{
|
|
||||||
return typeToComponentIDs.ContainsKey(type) ?
|
|
||||||
typeToComponentIDs[type].Select((id) => new ValueTuple<Guid, IComponent>(id, IDToComponent[id])) :
|
|
||||||
Enumerable.Empty<ValueTuple<Guid, IComponent>>();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
internal IEnumerable<ValueTuple<Guid, TComponent>> GetComponentsByEntityAndType<TComponent>(Guid entityID) where TComponent : struct, IComponent
|
internal IEnumerable<ValueTuple<Guid, TComponent>> GetComponentsByEntityAndType<TComponent>(Guid entityID) where TComponent : struct, IComponent
|
||||||
{
|
{
|
||||||
return entityIDToComponentTypeToComponentIDs[entityID].ContainsKey(typeof(TComponent)) ?
|
HashSet<Guid> idSet;
|
||||||
entityIDToComponentTypeToComponentIDs[entityID][typeof(TComponent)].Select(id => (id, (TComponent)GetComponentByID(id))) :
|
if (entityIDToComponentTypeToComponentIDs.ContainsKey(entityID) && entityIDToComponentTypeToComponentIDs[entityID].TryGetValue(typeof(TComponent), out idSet))
|
||||||
Enumerable.Empty<(Guid, TComponent)>();
|
{
|
||||||
}
|
return idSet.Select(id => (id, (TComponent)IDToComponent[id]));
|
||||||
|
}
|
||||||
internal IEnumerable<ValueTuple<Guid, IComponent>> GetComponentsByEntityAndType(Guid entityID, Type type)
|
return Enumerable.Empty<(Guid, TComponent)>();
|
||||||
{
|
|
||||||
return entityIDToComponentTypeToComponentIDs[entityID].ContainsKey(type) ?
|
|
||||||
entityIDToComponentTypeToComponentIDs[entityID][type].Select(id => (id, GetComponentByID(id))) :
|
|
||||||
Enumerable.Empty<(Guid, IComponent)>();
|
|
||||||
}
|
|
||||||
|
|
||||||
internal IEnumerable<Type> GetAllComponentTypesOfEntity(Guid entityID)
|
|
||||||
{
|
|
||||||
return GetComponentIDsByEntityID(entityID).Select((id) => componentIDToType[id]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
internal bool EntityHasComponentOfType<TComponent>(Guid entityID) where TComponent : struct, IComponent
|
internal bool EntityHasComponentOfType<TComponent>(Guid entityID) where TComponent : struct, IComponent
|
||||||
{
|
{
|
||||||
return GetComponentsByEntityAndType<TComponent>(entityID).Any();
|
HashSet<Guid> idSet;
|
||||||
|
if (entityIDToComponentTypeToComponentIDs.ContainsKey(entityID) && entityIDToComponentTypeToComponentIDs[entityID].TryGetValue(typeof(TComponent), out idSet))
|
||||||
|
{
|
||||||
|
return idSet.Count > 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
internal bool EntityHasComponentOfType(Guid entityID, Type type)
|
internal bool ComponentOfTypeExists<TComponent>() where TComponent : struct, IComponent
|
||||||
{
|
{
|
||||||
return GetComponentsByEntityAndType(entityID, type).Any();
|
HashSet<Guid> idSet;
|
||||||
|
if (typeToComponentIDs.TryGetValue(typeof(TComponent), out idSet))
|
||||||
|
{
|
||||||
|
return idSet.Count > 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
internal IComponent GetComponentByID(Guid componentID)
|
internal IComponent GetComponentByID(Guid componentID)
|
||||||
|
@ -183,7 +180,6 @@ namespace Encompass
|
||||||
|
|
||||||
private void Remove(Guid componentID)
|
private void Remove(Guid componentID)
|
||||||
{
|
{
|
||||||
var component = IDToComponent[componentID];
|
|
||||||
var type = componentIDToType[componentID];
|
var type = componentIDToType[componentID];
|
||||||
|
|
||||||
var entityID = componentIDToEntityID[componentID];
|
var entityID = componentIDToEntityID[componentID];
|
||||||
|
|
|
@ -215,7 +215,7 @@ namespace Encompass
|
||||||
internal IEnumerable<(Guid, TComponent)> ReadExistingAndPendingComponentsByEntityAndType<TComponent>(Entity entity) where TComponent : struct, IComponent
|
internal IEnumerable<(Guid, TComponent)> ReadExistingAndPendingComponentsByEntityAndType<TComponent>(Entity entity) where TComponent : struct, IComponent
|
||||||
{
|
{
|
||||||
HashSet<Guid> idSet;
|
HashSet<Guid> idSet;
|
||||||
if (entityToTypeToComponentIDs.TryGetValue(entity, out _) && entityToTypeToComponentIDs[entity].TryGetValue(typeof(TComponent), out idSet))
|
if (entityToTypeToComponentIDs.ContainsKey(entity) && entityToTypeToComponentIDs[entity].TryGetValue(typeof(TComponent), out idSet))
|
||||||
{
|
{
|
||||||
return idSet.Select(id => (id, (TComponent)componentIDToComponent[id]));
|
return idSet.Select(id => (id, (TComponent)componentIDToComponent[id]));
|
||||||
}
|
}
|
||||||
|
@ -226,7 +226,7 @@ namespace Encompass
|
||||||
internal IEnumerable<(Guid, TComponent)> ReadExistingComponentsByEntityAndType<TComponent>(Entity entity) where TComponent : struct, IComponent
|
internal IEnumerable<(Guid, TComponent)> ReadExistingComponentsByEntityAndType<TComponent>(Entity entity) where TComponent : struct, IComponent
|
||||||
{
|
{
|
||||||
HashSet<Guid> idSet;
|
HashSet<Guid> idSet;
|
||||||
if (entityToTypeToExistingComponentIDs.TryGetValue(entity, out _) && entityToTypeToExistingComponentIDs[entity].TryGetValue(typeof(TComponent), out idSet))
|
if (entityToTypeToExistingComponentIDs.ContainsKey(entity) && entityToTypeToExistingComponentIDs[entity].TryGetValue(typeof(TComponent), out idSet))
|
||||||
{
|
{
|
||||||
return idSet.Select(id => (id, (TComponent)componentIDToComponent[id]));
|
return idSet.Select(id => (id, (TComponent)componentIDToComponent[id]));
|
||||||
}
|
}
|
||||||
|
@ -237,7 +237,7 @@ namespace Encompass
|
||||||
internal IEnumerable<(Guid, TComponent)> ReadPendingComponentsByEntityAndType<TComponent>(Entity entity) where TComponent : struct, IComponent
|
internal IEnumerable<(Guid, TComponent)> ReadPendingComponentsByEntityAndType<TComponent>(Entity entity) where TComponent : struct, IComponent
|
||||||
{
|
{
|
||||||
HashSet<Guid> idSet;
|
HashSet<Guid> idSet;
|
||||||
if (entityToTypeToPendingComponentIDs.TryGetValue(entity, out _) && entityToTypeToPendingComponentIDs[entity].TryGetValue(typeof(TComponent), out idSet))
|
if (entityToTypeToPendingComponentIDs.ContainsKey(entity) && entityToTypeToPendingComponentIDs[entity].TryGetValue(typeof(TComponent), out idSet))
|
||||||
{
|
{
|
||||||
return idSet.Select(id => (id, (TComponent)componentIDToComponent[id]));
|
return idSet.Select(id => (id, (TComponent)componentIDToComponent[id]));
|
||||||
}
|
}
|
||||||
|
|
|
@ -112,9 +112,9 @@ namespace Encompass
|
||||||
return (TComponent)componentManager.GetComponentByID(componentID);
|
return (TComponent)componentManager.GetComponentByID(componentID);
|
||||||
}
|
}
|
||||||
|
|
||||||
internal IEnumerable<ValueTuple<Entity, Guid, TComponent>> ReadComponentsFromWorld<TComponent>() where TComponent : struct, IComponent
|
internal IEnumerable<(Guid, TComponent)> ReadComponentsFromWorld<TComponent>() where TComponent : struct, IComponent
|
||||||
{
|
{
|
||||||
return componentManager.GetComponentsByType<TComponent>().Select((triple) => (GetEntity(triple.Item1), triple.Item2, triple.Item3));
|
return componentManager.GetComponentsByType<TComponent>();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected Guid AddComponent<TComponent>(Entity entity, TComponent component) where TComponent : struct, IComponent
|
protected Guid AddComponent<TComponent>(Entity entity, TComponent component) where TComponent : struct, IComponent
|
||||||
|
|
|
@ -11,10 +11,10 @@ namespace Encompass.Engines
|
||||||
|
|
||||||
public override void Update(double dt)
|
public override void Update(double dt)
|
||||||
{
|
{
|
||||||
foreach (var (entity, componentID, component) in ReadComponentsFromWorld<TComponent>())
|
foreach (var (componentID, component) in ReadComponentsFromWorld<TComponent>())
|
||||||
{
|
{
|
||||||
ComponentMessage<TComponent> componentMessage;
|
ComponentMessage<TComponent> componentMessage;
|
||||||
componentMessage.entity = entity;
|
componentMessage.entity = GetEntityByComponentID(componentID);
|
||||||
componentMessage.componentID = componentID;
|
componentMessage.componentID = componentID;
|
||||||
componentMessage.component = component;
|
componentMessage.component = component;
|
||||||
SendMessage(componentMessage);
|
SendMessage(componentMessage);
|
||||||
|
|
|
@ -41,12 +41,12 @@ namespace Encompass
|
||||||
|
|
||||||
protected IEnumerable<ValueTuple<Guid, TComponent>> ReadComponents<TComponent>() where TComponent : struct, IComponent
|
protected IEnumerable<ValueTuple<Guid, TComponent>> ReadComponents<TComponent>() where TComponent : struct, IComponent
|
||||||
{
|
{
|
||||||
return componentManager.GetComponentsByType<TComponent>().Select((triple) => (triple.Item2, triple.Item3));
|
return componentManager.GetComponentsByType<TComponent>();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected ValueTuple<Guid, TComponent> ReadComponent<TComponent>() where TComponent : struct, IComponent
|
protected ValueTuple<Guid, TComponent> ReadComponent<TComponent>() where TComponent : struct, IComponent
|
||||||
{
|
{
|
||||||
return ReadComponents<TComponent>().Single();
|
return ReadComponents<TComponent>().First();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected IEnumerable<ValueTuple<Guid, TComponent>> GetComponents<TComponent>(Entity entity) where TComponent : struct, IComponent
|
protected IEnumerable<ValueTuple<Guid, TComponent>> GetComponents<TComponent>(Entity entity) where TComponent : struct, IComponent
|
||||||
|
@ -66,7 +66,7 @@ namespace Encompass
|
||||||
|
|
||||||
protected bool SomeComponent<TComponent>() where TComponent : struct, IComponent
|
protected bool SomeComponent<TComponent>() where TComponent : struct, IComponent
|
||||||
{
|
{
|
||||||
return componentManager.GetComponentsByType<TComponent>().Any();
|
return componentManager.ComponentOfTypeExists<TComponent>();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue