167 lines
6.7 KiB
C#
167 lines
6.7 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using Encompass.Exceptions;
|
|
|
|
namespace Encompass
|
|
{
|
|
class ComponentMessageManager
|
|
{
|
|
private readonly ComponentStore componentStore = new ComponentStore();
|
|
|
|
private readonly ComponentStore existingComponentStore = new ComponentStore();
|
|
private readonly ComponentStore pendingComponentStore = new ComponentStore();
|
|
private ComponentStore upToDateComponentStore = new ComponentStore();
|
|
|
|
private readonly Dictionary<Type, Dictionary<Entity, int>> typeToEntityToPendingComponentPriority = new Dictionary<Type, Dictionary<Entity, int>>(128);
|
|
|
|
public ComponentStore UpToDateComponentStore { get => upToDateComponentStore; }
|
|
|
|
internal void ClearMessages()
|
|
{
|
|
componentStore.ClearAll();
|
|
existingComponentStore.ClearAll();
|
|
pendingComponentStore.ClearAll();
|
|
|
|
foreach (var dictionary in typeToEntityToPendingComponentPriority.Values)
|
|
{
|
|
dictionary.Clear();
|
|
}
|
|
}
|
|
|
|
internal void SetStartingComponentStore(ComponentStore componentStore)
|
|
{
|
|
upToDateComponentStore = componentStore;
|
|
}
|
|
|
|
internal void AddExistingComponentMessage<TComponent>(ComponentMessage<TComponent> componentMessage) where TComponent : struct, IComponent
|
|
{
|
|
RegisterExistingOrPendingComponentMessage(componentMessage.entity, componentMessage.component);
|
|
|
|
existingComponentStore.Set(componentMessage.entity, componentMessage.component);
|
|
}
|
|
|
|
internal void AddPendingComponentMessage<TComponent>(PendingComponentMessage<TComponent> pendingComponentMessage) where TComponent : struct, IComponent
|
|
{
|
|
if (pendingComponentStore.Set(pendingComponentMessage.entity, pendingComponentMessage.component, pendingComponentMessage.priority))
|
|
{
|
|
RegisterExistingOrPendingComponentMessage(pendingComponentMessage.entity, pendingComponentMessage.component);
|
|
}
|
|
}
|
|
|
|
private void RegisterExistingOrPendingComponentMessage<TComponent>(Entity entity, TComponent component) where TComponent : struct, IComponent
|
|
{
|
|
componentStore.Set(entity, component);
|
|
upToDateComponentStore.Set(entity, component);
|
|
}
|
|
|
|
public void UpdateComponent<TComponent>(Entity entity, TComponent component, int priority) where TComponent : struct, IComponent
|
|
{
|
|
upToDateComponentStore.Set<TComponent>(entity, component, priority);
|
|
}
|
|
|
|
// general component reads by type
|
|
|
|
internal IEnumerable<(Entity, TComponent)> ReadExistingAndPendingComponentsByType<TComponent>() where TComponent : struct, IComponent
|
|
{
|
|
return componentStore.All<TComponent>();
|
|
}
|
|
|
|
internal IEnumerable<(Entity, TComponent)> ReadExistingComponentsByType<TComponent>() where TComponent : struct, IComponent
|
|
{
|
|
return existingComponentStore.All<TComponent>();
|
|
}
|
|
|
|
internal IEnumerable<(Entity, TComponent)> ReadPendingComponentsByType<TComponent>() where TComponent : struct, IComponent
|
|
{
|
|
return pendingComponentStore.All<TComponent>();
|
|
}
|
|
|
|
// singular component reads by type
|
|
|
|
internal (Entity, TComponent) ReadFirstExistingOrPendingComponentByType<TComponent>() where TComponent : struct, IComponent
|
|
{
|
|
if (!SomeExistingOrPendingComponent<TComponent>()) { throw new Exceptions.NoComponentOfTypeException($"No Component with type {typeof(TComponent)} exists"); }
|
|
return ReadExistingAndPendingComponentsByType<TComponent>().First();
|
|
}
|
|
|
|
internal (Entity, TComponent) ReadFirstExistingComponentByType<TComponent>() where TComponent : struct, IComponent
|
|
{
|
|
if (!SomeExistingComponent<TComponent>()) { throw new Exceptions.NoComponentOfTypeException($"No Component with type {typeof(TComponent)} exists"); }
|
|
return ReadExistingComponentsByType<TComponent>().First();
|
|
}
|
|
|
|
internal (Entity, TComponent) ReadFirstPendingComponentByType<TComponent>() where TComponent : struct, IComponent
|
|
{
|
|
if (!SomeExistingComponent<TComponent>()) { throw new Exceptions.NoComponentOfTypeException($"No Component with type {typeof(TComponent)} exists"); }
|
|
return ReadPendingComponentsByType<TComponent>().First();
|
|
}
|
|
|
|
// check if some component of type exists in the world
|
|
|
|
internal bool SomeExistingOrPendingComponent<TComponent>() where TComponent : struct, IComponent
|
|
{
|
|
return componentStore.Any<TComponent>();
|
|
}
|
|
|
|
internal bool SomeExistingComponent<TComponent>() where TComponent : struct, IComponent
|
|
{
|
|
return existingComponentStore.Any<TComponent>();
|
|
}
|
|
|
|
internal bool SomePendingComponent<TComponent>() where TComponent : struct, IComponent
|
|
{
|
|
return pendingComponentStore.Any<TComponent>();
|
|
}
|
|
|
|
// read components by entity and type
|
|
|
|
internal TComponent ReadExistingComponentByEntityAndType<TComponent>(Entity entity) where TComponent : struct, IComponent
|
|
{
|
|
return existingComponentStore.Get<TComponent>(entity);
|
|
}
|
|
|
|
internal TComponent ReadPendingComponentByEntityAndType<TComponent>(Entity entity) where TComponent : struct, IComponent
|
|
{
|
|
return pendingComponentStore.Get<TComponent>(entity);
|
|
}
|
|
|
|
// check if entity has component of type
|
|
|
|
internal bool HasExistingOrPendingComponent<TComponent>(Entity entity) where TComponent : struct, IComponent
|
|
{
|
|
return componentStore.Has<TComponent>(entity);
|
|
}
|
|
|
|
internal bool HasExistingOrPendingComponent(Entity entity, Type type)
|
|
{
|
|
return componentStore.Has(type, entity);
|
|
}
|
|
|
|
internal bool HasExistingComponent<TComponent>(Entity entity) where TComponent : struct, IComponent
|
|
{
|
|
return existingComponentStore.Has<TComponent>(entity);
|
|
}
|
|
|
|
internal bool HasExistingComponent(Entity entity, Type type)
|
|
{
|
|
return existingComponentStore.Has(type, entity);
|
|
}
|
|
|
|
internal bool HasPendingComponent<TComponent>(Entity entity) where TComponent : struct, IComponent
|
|
{
|
|
return pendingComponentStore.Has<TComponent>(entity);
|
|
}
|
|
|
|
internal bool HasPendingComponent(Entity entity, Type type)
|
|
{
|
|
return pendingComponentStore.Has(type, entity);
|
|
}
|
|
|
|
internal void Remove<TComponent>(Entity entity) where TComponent : struct, IComponent
|
|
{
|
|
upToDateComponentStore.Remove<TComponent>(entity);
|
|
}
|
|
}
|
|
}
|