encompass-cs/encompass-cs/Renderer.cs

76 lines
2.5 KiB
C#

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