2022-03-07 07:17:05 +00:00
|
|
|
namespace MoonTools.ECS;
|
|
|
|
|
|
|
|
internal class MessageDepot
|
|
|
|
{
|
|
|
|
private Dictionary<Type, MessageStorage> storages = new Dictionary<Type, MessageStorage>();
|
|
|
|
|
|
|
|
private MessageStorage<TMessage> Lookup<TMessage>() where TMessage : struct
|
|
|
|
{
|
|
|
|
if (!storages.ContainsKey(typeof(TMessage)))
|
|
|
|
{
|
|
|
|
storages.Add(typeof(TMessage), new MessageStorage<TMessage>());
|
|
|
|
}
|
|
|
|
|
|
|
|
return storages[typeof(TMessage)] as MessageStorage<TMessage>;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Add<TMessage>(in TMessage message) where TMessage : struct
|
|
|
|
{
|
|
|
|
Lookup<TMessage>().Add(message);
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool Some<TMessage>() where TMessage : struct
|
|
|
|
{
|
|
|
|
return Lookup<TMessage>().Some();
|
|
|
|
}
|
|
|
|
|
2022-03-18 19:50:59 +00:00
|
|
|
public ReadOnlySpan<TMessage> All<TMessage>() where TMessage : struct
|
2022-03-07 07:17:05 +00:00
|
|
|
{
|
|
|
|
return Lookup<TMessage>().All();
|
|
|
|
}
|
|
|
|
|
2022-03-18 19:50:59 +00:00
|
|
|
public TMessage First<TMessage>() where TMessage : struct
|
2022-03-16 07:33:38 +00:00
|
|
|
{
|
|
|
|
return Lookup<TMessage>().First();
|
|
|
|
}
|
|
|
|
|
2022-03-18 19:50:59 +00:00
|
|
|
public IEnumerable<TMessage> WithEntity<TMessage>(int entityID) where TMessage : struct, IHasEntity
|
|
|
|
{
|
|
|
|
return Lookup<TMessage>().WithEntity(entityID);
|
|
|
|
}
|
|
|
|
|
|
|
|
public ref readonly TMessage FirstWithEntity<TMessage>(int entityID) where TMessage : struct, IHasEntity
|
|
|
|
{
|
|
|
|
return ref Lookup<TMessage>().FirstWithEntity(entityID);
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool SomeWithEntity<TMessage>(int entityID) where TMessage : struct, IHasEntity
|
|
|
|
{
|
|
|
|
return Lookup<TMessage>().SomeWithEntity(entityID);
|
|
|
|
}
|
|
|
|
|
2022-03-07 07:17:05 +00:00
|
|
|
public void Clear()
|
|
|
|
{
|
|
|
|
foreach (var storage in storages.Values)
|
|
|
|
{
|
|
|
|
storage.Clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|