2019-12-17 04:40:15 +00:00
|
|
|
|
using System.Collections.Generic;
|
2019-06-19 21:14:44 +00:00
|
|
|
|
|
|
|
|
|
namespace Encompass
|
|
|
|
|
{
|
|
|
|
|
public abstract class Renderer
|
|
|
|
|
{
|
2020-03-20 07:09:57 +00:00
|
|
|
|
internal EntityManager _entityManager;
|
|
|
|
|
internal ComponentManager _componentManager;
|
2019-06-19 21:14:44 +00:00
|
|
|
|
|
|
|
|
|
internal void AssignEntityManager(EntityManager entityManager)
|
|
|
|
|
{
|
2020-03-20 07:09:57 +00:00
|
|
|
|
_entityManager = entityManager;
|
2019-06-19 21:14:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal void AssignComponentManager(ComponentManager componentManager)
|
|
|
|
|
{
|
2020-03-20 07:09:57 +00:00
|
|
|
|
_componentManager = componentManager;
|
2019-06-19 21:14:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-03-20 22:45:58 +00:00
|
|
|
|
protected IEnumerable<Entity> ReadEntities<TComponent>() where TComponent : unmanaged
|
2019-11-22 21:14:36 +00:00
|
|
|
|
{
|
2019-12-17 04:40:15 +00:00
|
|
|
|
foreach (var pair in ReadComponentsIncludingEntity<TComponent>())
|
|
|
|
|
{
|
|
|
|
|
yield return pair.Item2;
|
|
|
|
|
}
|
2019-11-22 21:14:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-03-20 22:45:58 +00:00
|
|
|
|
protected Entity ReadEntity<TComponent>() where TComponent : unmanaged
|
2019-11-22 21:14:36 +00:00
|
|
|
|
{
|
|
|
|
|
return ReadComponentIncludingEntity<TComponent>().Item2;
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-20 22:45:58 +00:00
|
|
|
|
protected IEnumerable<TComponent> ReadComponents<TComponent>() where TComponent : unmanaged
|
2019-07-13 04:48:08 +00:00
|
|
|
|
{
|
2020-03-20 07:09:57 +00:00
|
|
|
|
return _componentManager.GetComponentsByType<TComponent>();
|
2019-07-13 04:48:08 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-03-20 22:45:58 +00:00
|
|
|
|
protected IEnumerable<(TComponent, Entity)> ReadComponentsIncludingEntity<TComponent>() where TComponent : unmanaged
|
2019-07-13 04:48:08 +00:00
|
|
|
|
{
|
2020-03-20 07:09:57 +00:00
|
|
|
|
foreach (var (component, id) in _componentManager.GetComponentsIncludingEntity<TComponent>())
|
2019-12-24 02:45:49 +00:00
|
|
|
|
{
|
2020-03-20 07:09:57 +00:00
|
|
|
|
yield return (component, _entityManager.GetEntity(id));
|
2019-12-24 02:45:49 +00:00
|
|
|
|
}
|
2019-07-13 04:48:08 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-03-20 22:45:58 +00:00
|
|
|
|
protected TComponent ReadComponent<TComponent>() where TComponent : unmanaged
|
2019-06-19 21:14:44 +00:00
|
|
|
|
{
|
2019-12-17 04:40:15 +00:00
|
|
|
|
var enumerator = ReadComponents<TComponent>().GetEnumerator();
|
|
|
|
|
enumerator.MoveNext();
|
|
|
|
|
return enumerator.Current;
|
2019-06-19 21:14:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-03-20 22:45:58 +00:00
|
|
|
|
protected (TComponent, Entity) ReadComponentIncludingEntity<TComponent>() where TComponent : unmanaged
|
2019-06-19 21:14:44 +00:00
|
|
|
|
{
|
2019-12-17 04:40:15 +00:00
|
|
|
|
var enumerator = ReadComponentsIncludingEntity<TComponent>().GetEnumerator();
|
|
|
|
|
enumerator.MoveNext();
|
|
|
|
|
return enumerator.Current;
|
2019-06-19 21:14:44 +00:00
|
|
|
|
}
|
2019-07-17 18:24:21 +00:00
|
|
|
|
|
2020-03-20 22:45:58 +00:00
|
|
|
|
protected ref readonly TComponent GetComponent<TComponent>(Entity entity) where TComponent : unmanaged
|
2019-07-17 18:24:21 +00:00
|
|
|
|
{
|
2020-03-20 07:09:57 +00:00
|
|
|
|
return ref _componentManager.GetComponentByEntityAndType<TComponent>(entity.ID);
|
2019-07-17 18:24:21 +00:00
|
|
|
|
}
|
2019-07-17 20:15:15 +00:00
|
|
|
|
|
2020-03-20 22:45:58 +00:00
|
|
|
|
protected bool HasComponent<TComponent>(Entity entity) where TComponent : unmanaged
|
2019-07-17 20:15:15 +00:00
|
|
|
|
{
|
2020-03-20 07:09:57 +00:00
|
|
|
|
return _componentManager.EntityHasComponentOfType<TComponent>(entity.ID);
|
2019-07-17 20:15:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-03-20 22:45:58 +00:00
|
|
|
|
protected bool SomeComponent<TComponent>() where TComponent : unmanaged
|
2019-07-17 20:15:15 +00:00
|
|
|
|
{
|
2020-03-20 07:09:57 +00:00
|
|
|
|
return _componentManager.SomeExistingComponent<TComponent>();
|
2019-07-17 20:15:15 +00:00
|
|
|
|
}
|
2019-06-19 21:14:44 +00:00
|
|
|
|
}
|
|
|
|
|
}
|