encompass-cs/encompass-cs/Renderer.cs

76 lines
2.6 KiB
C#

using System.Collections.Generic;
namespace Encompass
{
public abstract class Renderer
{
internal EntityManager entityManager;
internal ComponentManager componentManager;
internal void AssignEntityManager(EntityManager entityManager)
{
this.entityManager = entityManager;
}
internal void AssignComponentManager(ComponentManager componentManager)
{
this.componentManager = componentManager;
}
protected IEnumerable<Entity> ReadEntities<TComponent>() where TComponent : struct, IComponent
{
foreach (var pair in ReadComponentsIncludingEntity<TComponent>())
{
yield return pair.Item2;
}
}
protected Entity ReadEntity<TComponent>() where TComponent : struct, IComponent
{
return ReadComponentIncludingEntity<TComponent>().Item2;
}
protected IEnumerable<TComponent> ReadComponents<TComponent>() where TComponent : struct, IComponent
{
return componentManager.GetComponentsByType<TComponent>();
}
protected IEnumerable<(TComponent, Entity)> ReadComponentsIncludingEntity<TComponent>() where TComponent : struct, IComponent
{
foreach (var (component, id) in componentManager.GetComponentsIncludingEntity<TComponent>())
{
yield return (component, entityManager.GetEntity(id));
}
}
protected TComponent ReadComponent<TComponent>() where TComponent : struct, IComponent
{
var enumerator = ReadComponents<TComponent>().GetEnumerator();
enumerator.MoveNext();
return enumerator.Current;
}
protected (TComponent, Entity) ReadComponentIncludingEntity<TComponent>() where TComponent : struct, IComponent
{
var enumerator = ReadComponentsIncludingEntity<TComponent>().GetEnumerator();
enumerator.MoveNext();
return enumerator.Current;
}
protected TComponent GetComponent<TComponent>(Entity entity) where TComponent : struct, IComponent
{
return componentManager.GetComponentByEntityAndType<TComponent>(entity);
}
protected bool HasComponent<TComponent>(Entity entity) where TComponent : struct, IComponent
{
return componentManager.EntityHasComponentOfType<TComponent>(entity);
}
protected bool SomeComponent<TComponent>() where TComponent : struct, IComponent
{
return componentManager.ComponentOfTypeExists<TComponent>();
}
}
}