encompass-cs/encompass-cs/Renderer.cs

88 lines
3.0 KiB
C#

using System;
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;
}
public abstract void Render(double dt, double alpha);
protected ReadOnlySpan<Entity> ReadEntities<TComponent>() where TComponent : struct
{
return _componentManager.GetExistingEntities<TComponent>();
}
protected IEnumerable<Entity> ReadEntitiesAsEnumerable<TComponent>() where TComponent : struct
{
return _componentManager.GetExistingEntitiesAsEnumerable<TComponent>();
}
protected ref readonly Entity ReadEntity<TComponent>() where TComponent : struct
{
return ref _componentManager.ExistingSingularEntity<TComponent>();
}
protected ReadOnlySpan<TComponent> ReadComponents<TComponent>() where TComponent : struct
{
return _componentManager.GetComponentsByType<TComponent>();
}
protected IEnumerable<TComponent> ReadComponentsAsEnumerable<TComponent>() where TComponent : struct
{
return _componentManager.GetComponentsByTypeEnumerable<TComponent>();
}
protected ref readonly TComponent ReadComponent<TComponent>() where TComponent : struct
{
return ref _componentManager.ExistingSingular<TComponent>();
}
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>();
}
#if DEBUG
protected IEnumerable<object> Debug_GetAllComponents(Entity entity)
{
return _componentManager.Debug_Components(entity.ID);
}
protected IEnumerable<Entity> Debug_Entities(Type componentType)
{
var method = typeof(Renderer).GetMethod(nameof(ReadEntitiesAsEnumerable), System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
var generic = method.MakeGenericMethod(componentType);
return (IEnumerable<Entity>)generic.Invoke(this, null);
}
protected IEnumerable<Type> Debug_SearchComponentType(string typeString)
{
return _componentManager.Debug_SearchComponentType(typeString);
}
#endif
}
}