encompass-cs/encompass-cs/ComponentMessageManager.cs

168 lines
6.7 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
2019-08-01 23:24:57 +00:00
using System.Linq;
using Encompass.Exceptions;
2019-08-02 00:34:54 +00:00
namespace Encompass
{
class ComponentMessageManager
{
2019-12-05 20:10:33 +00:00
private readonly ComponentStore componentStore = new ComponentStore();
2019-12-05 20:10:33 +00:00
private readonly ComponentStore existingComponentStore = new ComponentStore();
private readonly ComponentStore pendingComponentStore = new ComponentStore();
private ComponentStore upToDateComponentStore = new ComponentStore();
2019-08-01 23:24:57 +00:00
private readonly Dictionary<Type, Dictionary<Entity, int>> typeToEntityToPendingComponentPriority = new Dictionary<Type, Dictionary<Entity, int>>(128);
2019-12-05 23:14:28 +00:00
public ComponentStore UpToDateComponentStore { get => upToDateComponentStore; }
2019-12-05 22:59:55 +00:00
2019-08-01 23:24:57 +00:00
internal void ClearMessages()
{
2019-12-05 20:10:33 +00:00
componentStore.ClearAll();
existingComponentStore.ClearAll();
pendingComponentStore.ClearAll();
2019-12-05 23:14:28 +00:00
upToDateComponentStore.ClearAll();
foreach (var dictionary in typeToEntityToPendingComponentPriority.Values)
{
dictionary.Clear();
}
2019-08-01 23:24:57 +00:00
}
internal void SetStartingComponentStore(ComponentStore componentStore)
{
upToDateComponentStore = componentStore;
}
2019-08-01 23:24:57 +00:00
internal void AddExistingComponentMessage<TComponent>(ComponentMessage<TComponent> componentMessage) where TComponent : struct, IComponent
{
2019-12-05 20:10:33 +00:00
RegisterExistingOrPendingComponentMessage(componentMessage.entity, componentMessage.component);
2019-08-01 23:24:57 +00:00
2019-12-05 20:10:33 +00:00
existingComponentStore.Set(componentMessage.entity, componentMessage.component);
2019-08-01 23:24:57 +00:00
}
internal void AddPendingComponentMessage<TComponent>(PendingComponentMessage<TComponent> pendingComponentMessage) where TComponent : struct, IComponent
{
2019-12-05 22:59:55 +00:00
if (pendingComponentStore.Set(pendingComponentMessage.entity, pendingComponentMessage.component, pendingComponentMessage.priority))
{
RegisterExistingOrPendingComponentMessage(pendingComponentMessage.entity, pendingComponentMessage.component);
}
2019-08-01 23:24:57 +00:00
}
2019-12-05 20:10:33 +00:00
private void RegisterExistingOrPendingComponentMessage<TComponent>(Entity entity, TComponent component) where TComponent : struct, IComponent
2019-08-01 23:24:57 +00:00
{
2019-12-05 20:10:33 +00:00
componentStore.Set(entity, component);
2019-12-05 23:14:28 +00:00
upToDateComponentStore.Set(entity, component);
}
public void UpdateComponent<TComponent>(Entity entity, TComponent component, int priority) where TComponent : struct, IComponent
{
upToDateComponentStore.Set<TComponent>(entity, component, priority);
2019-08-01 23:24:57 +00:00
}
// general component reads by type
2019-12-05 20:10:33 +00:00
internal IEnumerable<(Entity, TComponent)> ReadExistingAndPendingComponentsByType<TComponent>() where TComponent : struct, IComponent
2019-08-01 23:24:57 +00:00
{
2019-12-05 20:10:33 +00:00
return componentStore.All<TComponent>();
2019-08-01 23:24:57 +00:00
}
2019-12-05 20:10:33 +00:00
internal IEnumerable<(Entity, TComponent)> ReadExistingComponentsByType<TComponent>() where TComponent : struct, IComponent
2019-08-01 23:24:57 +00:00
{
2019-12-05 20:10:33 +00:00
return existingComponentStore.All<TComponent>();
2019-08-01 23:24:57 +00:00
}
2019-12-05 20:10:33 +00:00
internal IEnumerable<(Entity, TComponent)> ReadPendingComponentsByType<TComponent>() where TComponent : struct, IComponent
2019-08-01 23:24:57 +00:00
{
2019-12-05 20:10:33 +00:00
return pendingComponentStore.All<TComponent>();
2019-08-01 23:24:57 +00:00
}
// singular component reads by type
2019-12-05 20:10:33 +00:00
internal (Entity, TComponent) ReadFirstExistingOrPendingComponentByType<TComponent>() where TComponent : struct, IComponent
2019-08-01 23:24:57 +00:00
{
if (!SomeExistingOrPendingComponent<TComponent>()) { throw new Exceptions.NoComponentOfTypeException($"No Component with type {typeof(TComponent)} exists"); }
2019-08-01 23:24:57 +00:00
return ReadExistingAndPendingComponentsByType<TComponent>().First();
}
2019-12-05 20:10:33 +00:00
internal (Entity, TComponent) ReadFirstExistingComponentByType<TComponent>() where TComponent : struct, IComponent
2019-08-01 23:24:57 +00:00
{
if (!SomeExistingComponent<TComponent>()) { throw new Exceptions.NoComponentOfTypeException($"No Component with type {typeof(TComponent)} exists"); }
2019-08-01 23:24:57 +00:00
return ReadExistingComponentsByType<TComponent>().First();
}
2019-12-05 20:10:33 +00:00
internal (Entity, TComponent) ReadFirstPendingComponentByType<TComponent>() where TComponent : struct, IComponent
2019-08-01 23:24:57 +00:00
{
if (!SomeExistingComponent<TComponent>()) { throw new Exceptions.NoComponentOfTypeException($"No Component with type {typeof(TComponent)} exists"); }
2019-08-01 23:24:57 +00:00
return ReadPendingComponentsByType<TComponent>().First();
}
// check if some component of type exists in the world
internal bool SomeExistingOrPendingComponent<TComponent>() where TComponent : struct, IComponent
{
2019-12-05 20:10:33 +00:00
return componentStore.Any<TComponent>();
2019-08-01 23:24:57 +00:00
}
internal bool SomeExistingComponent<TComponent>() where TComponent : struct, IComponent
{
2019-12-05 20:10:33 +00:00
return existingComponentStore.Any<TComponent>();
2019-08-01 23:24:57 +00:00
}
internal bool SomePendingComponent<TComponent>() where TComponent : struct, IComponent
{
2019-12-05 20:10:33 +00:00
return pendingComponentStore.Any<TComponent>();
2019-08-01 23:24:57 +00:00
}
// read components by entity and type
2019-12-05 20:10:33 +00:00
internal TComponent ReadExistingComponentByEntityAndType<TComponent>(Entity entity) where TComponent : struct, IComponent
2019-08-01 23:24:57 +00:00
{
2019-12-05 20:10:33 +00:00
return existingComponentStore.Get<TComponent>(entity);
2019-08-01 23:24:57 +00:00
}
2019-12-05 20:10:33 +00:00
internal TComponent ReadPendingComponentByEntityAndType<TComponent>(Entity entity) where TComponent : struct, IComponent
2019-08-01 23:24:57 +00:00
{
2019-12-05 20:10:33 +00:00
return pendingComponentStore.Get<TComponent>(entity);
2019-08-01 23:24:57 +00:00
}
// check if entity has component of type
internal bool HasExistingOrPendingComponent<TComponent>(Entity entity) where TComponent : struct, IComponent
{
2019-12-05 20:10:33 +00:00
return componentStore.Has<TComponent>(entity);
2019-08-01 23:24:57 +00:00
}
2019-11-13 21:15:43 +00:00
internal bool HasExistingOrPendingComponent(Entity entity, Type type)
{
2019-12-05 20:10:33 +00:00
return componentStore.Has(type, entity);
2019-11-13 21:15:43 +00:00
}
2019-08-01 23:24:57 +00:00
internal bool HasExistingComponent<TComponent>(Entity entity) where TComponent : struct, IComponent
{
2019-12-05 20:10:33 +00:00
return existingComponentStore.Has<TComponent>(entity);
2019-08-01 23:24:57 +00:00
}
2019-11-13 21:15:43 +00:00
internal bool HasExistingComponent(Entity entity, Type type)
{
2019-12-05 20:10:33 +00:00
return existingComponentStore.Has(type, entity);
2019-11-13 21:15:43 +00:00
}
2019-08-01 23:24:57 +00:00
internal bool HasPendingComponent<TComponent>(Entity entity) where TComponent : struct, IComponent
{
2019-12-05 20:10:33 +00:00
return pendingComponentStore.Has<TComponent>(entity);
2019-08-01 23:24:57 +00:00
}
2019-11-13 21:15:43 +00:00
internal bool HasPendingComponent(Entity entity, Type type)
{
2019-12-05 20:10:33 +00:00
return pendingComponentStore.Has(type, entity);
}
2019-12-05 23:14:28 +00:00
internal void Remove<TComponent>(Entity entity) where TComponent : struct, IComponent
{
upToDateComponentStore.Remove<TComponent>(entity);
}
}
}