initial message system

pull/1/head
cosmonaut 2022-03-06 23:17:05 -08:00
parent 57474f1c63
commit b30923ade3
5 changed files with 110 additions and 3 deletions

6
src/IHasEntity.cs Normal file
View File

@ -0,0 +1,6 @@
namespace MoonTools.ECS;
public interface IHasEntity
{
Entity Entity { get; }
}

39
src/MessageDepot.cs Normal file
View File

@ -0,0 +1,39 @@
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();
}
public ReadOnlySpan<TMessage> Read<TMessage>() where TMessage : struct
{
return Lookup<TMessage>().All();
}
public void Clear()
{
foreach (var storage in storages.Values)
{
storage.Clear();
}
}
}

45
src/MessageStorage.cs Normal file
View File

@ -0,0 +1,45 @@
namespace MoonTools.ECS;
internal abstract class MessageStorage
{
public abstract void Clear();
}
internal class MessageStorage<TMessage> : MessageStorage where TMessage : struct
{
private int count = 0;
private int capacity = 128;
private TMessage[] messages;
public MessageStorage()
{
messages = new TMessage[capacity];
}
public void Add(in TMessage message)
{
if (count == capacity)
{
capacity *= 2;
Array.Resize(ref messages, capacity);
}
messages[count] = message;
count += 1;
}
public bool Some()
{
return count > 0;
}
public ReadOnlySpan<TMessage> All()
{
return new ReadOnlySpan<TMessage>(messages, 0, count);
}
public override void Clear()
{
count = 0;
}
}

View File

@ -2,8 +2,7 @@
public abstract class System : EntityComponentReader
{
public abstract void Update(TimeSpan delta);
internal MessageDepot MessageDepot;
public FilterBuilder FilterBuilder => new FilterBuilder(ComponentDepot);
public System(World world)
@ -11,6 +10,13 @@ public abstract class System : EntityComponentReader
world.AddSystem(this);
}
internal void RegisterMessageDepot(MessageDepot messageDepot)
{
MessageDepot = messageDepot;
}
public abstract void Update(TimeSpan delta);
protected Entity CreateEntity()
{
return EntityStorage.Create();
@ -26,6 +32,16 @@ public abstract class System : EntityComponentReader
ComponentDepot.Remove<TComponent>(entity.ID);
}
protected ReadOnlySpan<TMessage> ReadMessages<TMessage>() where TMessage : struct
{
return MessageDepot.Read<TMessage>();
}
protected bool SomeMessage<TMessage>() where TMessage : struct
{
return MessageDepot.Some<TMessage>();
}
protected void Destroy(in Entity entity)
{
ComponentDepot.OnEntityDestroy(entity.ID);

View File

@ -4,14 +4,15 @@ public class World
{
private readonly List<System> Systems = new List<System>();
private readonly List<Renderer> Renderers = new List<Renderer>();
private readonly List<Filter> Filters = new List<Filter>();
private EntityStorage EntityStorage { get; } = new EntityStorage();
private ComponentDepot ComponentDepot { get; } = new ComponentDepot();
private MessageDepot MessageDepot { get; } = new MessageDepot();
internal void AddSystem(System system)
{
system.RegisterEntityStorage(EntityStorage);
system.RegisterComponentDepot(ComponentDepot);
system.RegisterMessageDepot(MessageDepot);
Systems.Add(system);
}