encompass-cs/encompass-cs/ComponentUpdateManager.cs

200 lines
7.8 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
2019-12-22 09:15:58 +00:00
using System.Linq;
2019-08-02 00:34:54 +00:00
namespace Encompass
{
internal class ComponentUpdateManager
{
2019-12-22 09:15:58 +00:00
private readonly ComponentStore existingComponentStore;
2019-12-24 03:04:26 +00:00
private readonly ComponentStore immediateComponentStore;
public Dictionary<Type, int> TypeToIndex { get; }
2019-12-22 09:15:58 +00:00
public ComponentStore UpToDateComponentStore { get; private set; }
2019-12-22 09:15:58 +00:00
public ComponentUpdateManager(Dictionary<Type, int> typeToIndex)
{
existingComponentStore = new ComponentStore(typeToIndex);
2019-12-24 03:04:26 +00:00
immediateComponentStore = new ComponentStore(typeToIndex);
2019-12-22 09:15:58 +00:00
UpToDateComponentStore = new ComponentStore(typeToIndex);
TypeToIndex = typeToIndex;
2019-12-22 09:15:58 +00:00
}
2019-12-05 22:59:55 +00:00
2019-12-16 07:28:02 +00:00
public void RegisterComponentType<TComponent>() where TComponent : struct, IComponent
{
existingComponentStore.RegisterComponentType<TComponent>();
2019-12-24 03:04:26 +00:00
immediateComponentStore.RegisterComponentType<TComponent>();
2019-12-17 02:51:45 +00:00
UpToDateComponentStore.RegisterComponentType<TComponent>();
2019-12-16 07:28:02 +00:00
}
internal void Clear()
2019-08-01 23:24:57 +00:00
{
2019-12-05 20:10:33 +00:00
existingComponentStore.ClearAll();
2019-12-24 03:04:26 +00:00
immediateComponentStore.ClearAll();
2019-12-06 03:55:17 +00:00
UpToDateComponentStore.ClearAll();
2019-08-01 23:24:57 +00:00
}
internal void SetStartingComponentStore(ComponentStore componentStore)
{
UpToDateComponentStore = componentStore;
}
2019-12-06 03:55:17 +00:00
internal void AddExistingComponent<TComponent>(Entity entity, TComponent component) where TComponent : struct, IComponent
2019-08-01 23:24:57 +00:00
{
2019-12-24 03:04:26 +00:00
RegisterExistingOrImmediateComponentMessage(entity, component);
2019-08-01 23:24:57 +00:00
existingComponentStore.Set(entity.ID, component);
2019-08-01 23:24:57 +00:00
}
2019-12-24 03:04:26 +00:00
internal bool AddImmediateComponent<TComponent>(Entity entity, TComponent component, int priority) where TComponent : struct, IComponent
2019-08-01 23:24:57 +00:00
{
2019-12-24 03:04:26 +00:00
if (immediateComponentStore.Set(entity.ID, component, priority))
2019-12-05 22:59:55 +00:00
{
2019-12-24 03:04:26 +00:00
RegisterExistingOrImmediateComponentMessage(entity, component);
return true;
2019-12-05 22:59:55 +00:00
}
return false;
2019-08-01 23:24:57 +00:00
}
2019-12-24 03:04:26 +00:00
internal bool RemoveImmediate<TComponent>(Entity entity, int priority) where TComponent : struct, IComponent
{
2019-12-24 04:15:01 +00:00
if (immediateComponentStore.Remove<TComponent>(entity.ID, priority))
{
UpToDateComponentStore.Remove<TComponent>(entity.ID, priority);
return true;
}
return false;
}
internal bool Remove<TComponent>(Entity entity, int priority) where TComponent : struct, IComponent
{
2019-12-24 04:15:01 +00:00
if (existingComponentStore.Remove<TComponent>(entity.ID, priority))
{
UpToDateComponentStore.Remove<TComponent>(entity.ID, priority);
return true;
}
return false;
}
2019-12-24 03:04:26 +00:00
private void RegisterExistingOrImmediateComponentMessage<TComponent>(Entity entity, TComponent component) where TComponent : struct, IComponent
2019-08-01 23:24:57 +00:00
{
UpToDateComponentStore.Set(entity.ID, component);
2019-12-05 23:14:28 +00:00
}
public bool UpdateComponent<TComponent>(Entity entity, TComponent component, int priority) where TComponent : struct, IComponent
2019-12-05 23:14:28 +00:00
{
return UpToDateComponentStore.Set<TComponent>(entity.ID, component, priority);
2019-08-01 23:24:57 +00:00
}
// general component reads by type
2019-12-24 03:04:26 +00:00
internal IEnumerable<(TComponent, int)> ReadExistingAndImmediateComponentsByType<TComponent>() where TComponent : struct, IComponent
2019-08-01 23:24:57 +00:00
{
return UpToDateComponentStore.All<TComponent>();
2019-08-01 23:24:57 +00:00
}
internal IEnumerable<(TComponent, int)> 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-24 03:04:26 +00:00
internal IEnumerable<(TComponent, int)> ReadImmediateComponentsByType<TComponent>() where TComponent : struct, IComponent
2019-08-01 23:24:57 +00:00
{
2019-12-24 03:04:26 +00:00
return immediateComponentStore.All<TComponent>();
2019-08-01 23:24:57 +00:00
}
// singular component reads by type
2019-12-24 03:04:26 +00:00
internal (TComponent, int) ReadFirstExistingOrImmediateComponentByType<TComponent>() where TComponent : struct, IComponent
2019-08-01 23:24:57 +00:00
{
2019-12-24 03:04:26 +00:00
if (!SomeExistingOrImmediateComponent<TComponent>()) { throw new Exceptions.NoComponentOfTypeException($"No Component with type {typeof(TComponent)} exists"); }
var enumerator = ReadExistingAndImmediateComponentsByType<TComponent>().GetEnumerator();
2019-12-17 04:40:15 +00:00
enumerator.MoveNext();
return enumerator.Current;
2019-08-01 23:24:57 +00:00
}
internal (TComponent, int) 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-12-17 04:40:15 +00:00
var enumerator = ReadExistingComponentsByType<TComponent>().GetEnumerator();
enumerator.MoveNext();
return enumerator.Current;
2019-08-01 23:24:57 +00:00
}
2019-12-24 03:04:26 +00:00
internal (TComponent, int) ReadFirstImmediateComponentByType<TComponent>() where TComponent : struct, IComponent
2019-08-01 23:24:57 +00:00
{
2019-12-24 03:04:26 +00:00
if (!SomeImmediateComponent<TComponent>()) { throw new Exceptions.NoComponentOfTypeException($"No Component with type {typeof(TComponent)} exists"); }
var enumerator = ReadImmediateComponentsByType<TComponent>().GetEnumerator();
2019-12-17 04:40:15 +00:00
enumerator.MoveNext();
return enumerator.Current;
2019-08-01 23:24:57 +00:00
}
// check if some component of type exists in the world
2019-12-24 03:04:26 +00:00
internal bool SomeExistingOrImmediateComponent<TComponent>() where TComponent : struct, IComponent
2019-08-01 23:24:57 +00:00
{
return UpToDateComponentStore.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
}
2019-12-24 03:04:26 +00:00
internal bool SomeImmediateComponent<TComponent>() where TComponent : struct, IComponent
2019-08-01 23:24:57 +00:00
{
2019-12-24 03:04:26 +00:00
return immediateComponentStore.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
{
return existingComponentStore.Get<TComponent>(entity.ID);
2019-08-01 23:24:57 +00:00
}
2019-12-24 03:04:26 +00:00
internal TComponent ReadImmediateComponentByEntityAndType<TComponent>(Entity entity) where TComponent : struct, IComponent
2019-08-01 23:24:57 +00:00
{
2019-12-24 03:04:26 +00:00
return immediateComponentStore.Get<TComponent>(entity.ID);
2019-08-01 23:24:57 +00:00
}
// check if entity has component of type
2019-12-24 03:04:26 +00:00
internal bool HasExistingOrImmediateComponent<TComponent>(Entity entity) where TComponent : struct, IComponent
2019-08-01 23:24:57 +00:00
{
return UpToDateComponentStore.Has<TComponent>(entity.ID);
2019-08-01 23:24:57 +00:00
}
2019-12-24 03:04:26 +00:00
internal bool HasExistingOrImmediateComponent(Entity entity, Type type)
2019-11-13 21:15:43 +00:00
{
return UpToDateComponentStore.Has(type, entity.ID);
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
{
return existingComponentStore.Has<TComponent>(entity.ID);
2019-08-01 23:24:57 +00:00
}
2019-11-13 21:15:43 +00:00
internal bool HasExistingComponent(Entity entity, Type type)
{
return existingComponentStore.Has(type, entity.ID);
2019-11-13 21:15:43 +00:00
}
2019-12-24 03:04:26 +00:00
internal bool HasImmediateComponent<TComponent>(Entity entity) where TComponent : struct, IComponent
2019-08-01 23:24:57 +00:00
{
2019-12-24 03:04:26 +00:00
return immediateComponentStore.Has<TComponent>(entity.ID);
2019-08-01 23:24:57 +00:00
}
2019-12-24 03:04:26 +00:00
internal bool HasImmediateComponent(Entity entity, Type type)
2019-11-13 21:15:43 +00:00
{
2019-12-24 03:04:26 +00:00
return immediateComponentStore.Has(type, entity.ID);
}
2019-12-05 23:14:28 +00:00
2019-12-24 03:04:26 +00:00
internal ComponentBitSet ImmediateBits { get { return immediateComponentStore.ComponentBitSet; } }
internal ComponentBitSet ExistingBits { get { return existingComponentStore.ComponentBitSet; } }
}
}