2019-12-29 05:39:35 +00:00
|
|
|
|
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
|
|
|
|
|
{
|
|
|
|
|
internal class ComponentStore
|
|
|
|
|
{
|
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-20 22:45:58 +00:00
|
|
|
|
private 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-20 22:45:58 +00:00
|
|
|
|
public virtual void RegisterComponentType<TComponent>() where TComponent : unmanaged
|
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-20 22:45:58 +00:00
|
|
|
|
private TypedComponentStore<TComponent> Lookup<TComponent>() where TComponent : unmanaged
|
2019-12-16 07:28:02 +00:00
|
|
|
|
{
|
2020-03-20 22:45:58 +00:00
|
|
|
|
RegisterComponentType<TComponent>();
|
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-20 22:45:58 +00:00
|
|
|
|
public bool Has<TComponent>(int entityID) where TComponent : unmanaged
|
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-20 07:09:57 +00:00
|
|
|
|
return _stores.ContainsKey(type) && _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-20 22:45:58 +00:00
|
|
|
|
public ref readonly TComponent Get<TComponent>(int entityID) where TComponent : unmanaged
|
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-20 22:45:58 +00:00
|
|
|
|
public virtual void Set<TComponent>(int entityID, TComponent component) where TComponent : unmanaged
|
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-20 22:45:58 +00:00
|
|
|
|
public virtual bool Set<TComponent>(int entityID, TComponent component, int priority) where TComponent : unmanaged
|
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-20 22:45:58 +00:00
|
|
|
|
public virtual bool Remove<TComponent>(int entityID, int priority) where TComponent : unmanaged
|
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-20 22:45:58 +00:00
|
|
|
|
public void ForceRemove<TComponent>(int entityID) where TComponent : unmanaged
|
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
|
|
|
|
}
|
|
|
|
|
|
2019-12-30 01:37:19 +00:00
|
|
|
|
public virtual 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-20 22:45:58 +00:00
|
|
|
|
public bool Any<TComponent>() where TComponent : unmanaged
|
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-20 22:45:58 +00:00
|
|
|
|
public IEnumerable<(TComponent, int)> All<TComponent>() where TComponent : unmanaged
|
2019-12-05 04:50:08 +00:00
|
|
|
|
{
|
2019-12-05 20:10:33 +00:00
|
|
|
|
return Lookup<TComponent>().All();
|
2019-12-05 04:50:08 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-03-20 22:45:58 +00:00
|
|
|
|
public void Clear<TComponent>() where TComponent : unmanaged
|
2019-12-05 04:50:08 +00:00
|
|
|
|
{
|
|
|
|
|
Lookup<TComponent>().Clear();
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-30 03:43:05 +00:00
|
|
|
|
public virtual void ClearAllPriorities()
|
|
|
|
|
{
|
2020-03-20 07:09:57 +00:00
|
|
|
|
foreach (var store in _stores.Values)
|
2019-12-30 03:43:05 +00:00
|
|
|
|
{
|
|
|
|
|
store.ClearPriorities();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-30 00:28:22 +00:00
|
|
|
|
public virtual 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
|
|
|
|
}
|
|
|
|
|
}
|