MoonTools.ECS/src/MessageDepot.cs

69 lines
1.6 KiB
C#
Raw Normal View History

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