2020-07-20 03:09:44 +00:00
|
|
|
|
using Encompass.Exceptions;
|
|
|
|
|
using MoonTools.FastCollections;
|
2019-12-23 00:21:07 +00:00
|
|
|
|
using System;
|
2019-12-05 04:50:08 +00:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
|
|
|
|
namespace Encompass
|
|
|
|
|
{
|
2020-03-22 03:29:15 +00:00
|
|
|
|
internal sealed class ComponentStore
|
2019-12-05 04:50:08 +00:00
|
|
|
|
{
|
2020-03-20 07:09:57 +00:00
|
|
|
|
private Dictionary<Type, TypedComponentStore> _stores = new Dictionary<Type, TypedComponentStore>(512);
|
2019-12-23 02:13:35 +00:00
|
|
|
|
public ComponentBitSet ComponentBitSet { get; private set; }
|
2020-03-21 01:09:24 +00:00
|
|
|
|
private readonly Dictionary<Type, int> _typeToIndex;
|
2019-12-22 09:15:58 +00:00
|
|
|
|
|
|
|
|
|
public ComponentStore(Dictionary<Type, int> typeToIndex)
|
|
|
|
|
{
|
2020-03-20 22:45:58 +00:00
|
|
|
|
_typeToIndex = typeToIndex;
|
2019-12-23 02:13:35 +00:00
|
|
|
|
ComponentBitSet = new ComponentBitSet(typeToIndex);
|
2019-12-22 09:15:58 +00:00
|
|
|
|
}
|
2019-12-05 20:10:33 +00:00
|
|
|
|
|
2020-03-22 03:29:15 +00:00
|
|
|
|
public void RegisterComponentType<TComponent>() where TComponent : struct
|
2019-12-05 04:50:08 +00:00
|
|
|
|
{
|
2020-03-20 07:09:57 +00:00
|
|
|
|
if (!_stores.ContainsKey(typeof(TComponent)))
|
2019-12-05 04:50:08 +00:00
|
|
|
|
{
|
2019-12-05 20:10:33 +00:00
|
|
|
|
var store = new TypedComponentStore<TComponent>();
|
2020-03-20 07:09:57 +00:00
|
|
|
|
_stores.Add(typeof(TComponent), store);
|
2019-12-05 04:50:08 +00:00
|
|
|
|
}
|
2020-03-20 22:45:58 +00:00
|
|
|
|
|
|
|
|
|
if (!_typeToIndex.ContainsKey(typeof(TComponent))) { _typeToIndex.Add(typeof(TComponent), _typeToIndex.Count); }
|
2019-12-16 07:28:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-03-22 00:38:35 +00:00
|
|
|
|
private TypedComponentStore<TComponent> Lookup<TComponent>() where TComponent : struct
|
2019-12-16 07:28:02 +00:00
|
|
|
|
{
|
2020-03-20 07:09:57 +00:00
|
|
|
|
return _stores[typeof(TComponent)] as TypedComponentStore<TComponent>;
|
2019-12-05 20:10:33 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-03-22 00:38:35 +00:00
|
|
|
|
public bool Has<TComponent>(int entityID) where TComponent : struct
|
2019-12-05 20:10:33 +00:00
|
|
|
|
{
|
2019-12-24 02:45:49 +00:00
|
|
|
|
return Lookup<TComponent>().Has(entityID);
|
2019-12-05 20:10:33 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-24 02:45:49 +00:00
|
|
|
|
public bool Has(Type type, int entityID)
|
2019-12-05 20:10:33 +00:00
|
|
|
|
{
|
2020-03-25 18:52:42 +00:00
|
|
|
|
return _stores[type].Has(entityID);
|
2019-12-05 20:10:33 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-29 05:39:35 +00:00
|
|
|
|
public BitSet512 EntityBitArray(int entityID)
|
2019-12-20 19:10:42 +00:00
|
|
|
|
{
|
2019-12-24 02:45:49 +00:00
|
|
|
|
return ComponentBitSet.EntityBitArray(entityID);
|
2019-12-20 19:10:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-03-22 05:19:31 +00:00
|
|
|
|
public ref TComponent Get<TComponent>(int entityID) where TComponent : struct
|
2019-12-05 20:10:33 +00:00
|
|
|
|
{
|
2020-03-19 22:44:04 +00:00
|
|
|
|
return ref Lookup<TComponent>().Get(entityID);
|
2019-12-05 20:10:33 +00:00
|
|
|
|
}
|
2019-12-05 04:50:08 +00:00
|
|
|
|
|
2020-03-23 02:10:28 +00:00
|
|
|
|
public ref readonly TComponent Singular<TComponent>() where TComponent : struct
|
|
|
|
|
{
|
2020-07-20 03:09:44 +00:00
|
|
|
|
if (!Any<TComponent>()) { throw new NoComponentOfTypeException("No component of type {0} exists", typeof(TComponent).Name); }
|
|
|
|
|
|
2020-03-23 02:10:28 +00:00
|
|
|
|
return ref Lookup<TComponent>().Singular();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ref readonly Entity SingularEntity<TComponent>() where TComponent : struct
|
|
|
|
|
{
|
2020-07-20 03:09:44 +00:00
|
|
|
|
if (!Any<TComponent>()) { throw new NoComponentOfTypeException("No component of type {0} exists", typeof(TComponent).Name); }
|
|
|
|
|
|
2020-03-23 02:10:28 +00:00
|
|
|
|
return ref Lookup<TComponent>().SingularEntity();
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-22 05:19:31 +00:00
|
|
|
|
public void Set<TComponent>(int entityID, in TComponent component) where TComponent : struct
|
2019-12-05 20:10:33 +00:00
|
|
|
|
{
|
2019-12-24 02:45:49 +00:00
|
|
|
|
Lookup<TComponent>().Set(entityID, component);
|
|
|
|
|
ComponentBitSet.Set<TComponent>(entityID);
|
2019-12-05 04:50:08 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-03-22 05:19:31 +00:00
|
|
|
|
public bool Set<TComponent>(int entityID, in TComponent component, int priority) where TComponent : struct
|
2019-12-05 04:50:08 +00:00
|
|
|
|
{
|
2019-12-24 02:45:49 +00:00
|
|
|
|
if (Lookup<TComponent>().Set(entityID, component, priority))
|
2019-12-24 02:01:49 +00:00
|
|
|
|
{
|
2019-12-24 02:45:49 +00:00
|
|
|
|
ComponentBitSet.Set<TComponent>(entityID);
|
2019-12-24 02:01:49 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-22 03:29:15 +00:00
|
|
|
|
public bool Remove<TComponent>(int entityID, int priority) where TComponent : struct
|
2019-12-24 02:01:49 +00:00
|
|
|
|
{
|
2019-12-24 02:45:49 +00:00
|
|
|
|
if (Lookup<TComponent>().Remove(entityID, priority))
|
2019-12-24 02:01:49 +00:00
|
|
|
|
{
|
2019-12-24 02:45:49 +00:00
|
|
|
|
ComponentBitSet.RemoveComponent<TComponent>(entityID);
|
2019-12-24 02:01:49 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
2019-12-05 04:50:08 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-03-22 00:38:35 +00:00
|
|
|
|
public void ForceRemove<TComponent>(int entityID) where TComponent : struct
|
2019-12-05 04:50:08 +00:00
|
|
|
|
{
|
2019-12-24 02:45:49 +00:00
|
|
|
|
Lookup<TComponent>().ForceRemove(entityID);
|
2019-12-30 03:08:32 +00:00
|
|
|
|
ComponentBitSet.RemoveComponent<TComponent>(entityID);
|
2019-12-05 04:50:08 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-03-22 03:29:15 +00:00
|
|
|
|
public void Remove(int entityID)
|
2019-12-05 04:50:08 +00:00
|
|
|
|
{
|
2020-03-20 07:09:57 +00:00
|
|
|
|
foreach (var entry in _stores.Values)
|
2019-12-05 20:10:33 +00:00
|
|
|
|
{
|
2019-12-24 02:45:49 +00:00
|
|
|
|
entry.ForceRemove(entityID);
|
2019-12-05 20:10:33 +00:00
|
|
|
|
}
|
2019-12-24 02:45:49 +00:00
|
|
|
|
ComponentBitSet.RemoveEntity(entityID);
|
2019-12-05 04:50:08 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-03-22 00:38:35 +00:00
|
|
|
|
public bool Any<TComponent>() where TComponent : struct
|
2019-12-05 04:50:08 +00:00
|
|
|
|
{
|
2019-12-05 20:10:33 +00:00
|
|
|
|
return Lookup<TComponent>().Count > 0;
|
2019-12-05 04:50:08 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-03-23 02:10:28 +00:00
|
|
|
|
public Span<TComponent> All<TComponent>() where TComponent : struct
|
|
|
|
|
{
|
|
|
|
|
return Lookup<TComponent>().AllComponents();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Span<Entity> AllEntities<TComponent>() where TComponent : struct
|
2019-12-05 04:50:08 +00:00
|
|
|
|
{
|
2020-03-23 02:10:28 +00:00
|
|
|
|
return Lookup<TComponent>().AllEntities();
|
2019-12-05 04:50:08 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-03-22 00:38:35 +00:00
|
|
|
|
public void Clear<TComponent>() where TComponent : struct
|
2019-12-05 04:50:08 +00:00
|
|
|
|
{
|
|
|
|
|
Lookup<TComponent>().Clear();
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-22 03:29:15 +00:00
|
|
|
|
public void ClearAllPriorities()
|
2019-12-30 03:43:05 +00:00
|
|
|
|
{
|
2020-03-20 07:09:57 +00:00
|
|
|
|
foreach (var store in _stores.Values)
|
2019-12-30 03:43:05 +00:00
|
|
|
|
{
|
|
|
|
|
store.ClearPriorities();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-22 03:29:15 +00:00
|
|
|
|
public void ClearAll()
|
2019-12-05 04:50:08 +00:00
|
|
|
|
{
|
2019-12-23 02:13:35 +00:00
|
|
|
|
ComponentBitSet.Clear();
|
2020-03-20 07:09:57 +00:00
|
|
|
|
foreach (var store in _stores.Values)
|
2019-12-05 04:50:08 +00:00
|
|
|
|
{
|
2019-12-05 20:10:33 +00:00
|
|
|
|
store.Clear();
|
2019-12-05 04:50:08 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-12-05 22:59:55 +00:00
|
|
|
|
|
|
|
|
|
public void SwapWith(ComponentStore other)
|
|
|
|
|
{
|
2020-03-20 07:09:57 +00:00
|
|
|
|
(_stores, other._stores) = (other._stores, _stores);
|
2019-12-23 02:13:35 +00:00
|
|
|
|
(ComponentBitSet, other.ComponentBitSet) = (other.ComponentBitSet, ComponentBitSet);
|
2019-12-05 22:59:55 +00:00
|
|
|
|
}
|
2019-12-30 00:28:22 +00:00
|
|
|
|
|
|
|
|
|
public void UpdateUsing(ComponentDeltaStore delta)
|
|
|
|
|
{
|
|
|
|
|
foreach (var replayer in delta.CurrentReplayers)
|
|
|
|
|
{
|
|
|
|
|
replayer.Replay(this);
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-12-05 04:50:08 +00:00
|
|
|
|
}
|
|
|
|
|
}
|