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 abstract class MessageStorage
|
2022-03-07 07:17:05 +00:00
|
|
|
|
{
|
2022-04-08 05:52:03 +00:00
|
|
|
|
public abstract void Clear();
|
2022-03-07 07:17:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-04-08 05:52:03 +00:00
|
|
|
|
internal class MessageStorage<TMessage> : MessageStorage where TMessage : struct
|
2022-03-07 07:17:05 +00:00
|
|
|
|
{
|
2022-04-08 05:52:03 +00:00
|
|
|
|
private int count = 0;
|
|
|
|
|
private int capacity = 128;
|
|
|
|
|
private TMessage[] messages;
|
|
|
|
|
private Dictionary<int, List<int>> entityToIndices = new Dictionary<int, List<int>>();
|
|
|
|
|
|
|
|
|
|
public MessageStorage()
|
2022-03-07 07:17:05 +00:00
|
|
|
|
{
|
2022-04-08 05:52:03 +00:00
|
|
|
|
messages = new TMessage[capacity];
|
2022-03-07 07:17:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-04-08 05:52:03 +00:00
|
|
|
|
public void Add(in TMessage message)
|
2022-03-18 19:50:59 +00:00
|
|
|
|
{
|
2022-04-08 05:52:03 +00:00
|
|
|
|
if (count == capacity)
|
2022-03-18 19:50:59 +00:00
|
|
|
|
{
|
2022-04-08 05:52:03 +00:00
|
|
|
|
capacity *= 2;
|
|
|
|
|
Array.Resize(ref messages, capacity);
|
2022-03-18 19:50:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-04-08 05:52:03 +00:00
|
|
|
|
messages[count] = message;
|
2022-03-18 19:50:59 +00:00
|
|
|
|
|
2022-04-08 05:52:03 +00:00
|
|
|
|
if (message is IHasEntity entityMessage)
|
|
|
|
|
{
|
|
|
|
|
if (!entityToIndices.ContainsKey(entityMessage.Entity.ID))
|
|
|
|
|
{
|
|
|
|
|
entityToIndices.Add(entityMessage.Entity.ID, new List<int>());
|
|
|
|
|
}
|
2022-03-07 07:17:05 +00:00
|
|
|
|
|
2022-04-08 05:52:03 +00:00
|
|
|
|
entityToIndices[entityMessage.Entity.ID].Add(count);
|
|
|
|
|
}
|
2022-03-07 07:17:05 +00:00
|
|
|
|
|
2022-04-08 05:52:03 +00:00
|
|
|
|
count += 1;
|
|
|
|
|
}
|
2022-03-07 07:17:05 +00:00
|
|
|
|
|
2022-04-08 05:52:03 +00:00
|
|
|
|
public bool Some()
|
|
|
|
|
{
|
|
|
|
|
return count > 0;
|
|
|
|
|
}
|
2022-03-16 07:33:38 +00:00
|
|
|
|
|
2022-04-08 05:52:03 +00:00
|
|
|
|
public ReadOnlySpan<TMessage> All()
|
|
|
|
|
{
|
|
|
|
|
return new ReadOnlySpan<TMessage>(messages, 0, count);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public TMessage First()
|
|
|
|
|
{
|
|
|
|
|
return messages[0];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IEnumerable<TMessage> WithEntity(int entityID)
|
2022-03-18 19:50:59 +00:00
|
|
|
|
{
|
2022-04-08 05:52:03 +00:00
|
|
|
|
if (entityToIndices.ContainsKey(entityID))
|
2022-03-18 19:50:59 +00:00
|
|
|
|
{
|
2022-04-08 05:52:03 +00:00
|
|
|
|
foreach (var index in entityToIndices[entityID])
|
|
|
|
|
{
|
|
|
|
|
yield return messages[index];
|
|
|
|
|
}
|
2022-03-18 19:50:59 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-08 05:52:03 +00:00
|
|
|
|
public ref readonly TMessage FirstWithEntity(int entityID)
|
|
|
|
|
{
|
|
|
|
|
return ref messages[entityToIndices[entityID][0]];
|
|
|
|
|
}
|
2022-03-18 19:50:59 +00:00
|
|
|
|
|
2022-04-08 05:52:03 +00:00
|
|
|
|
public bool SomeWithEntity(int entityID)
|
|
|
|
|
{
|
|
|
|
|
return entityToIndices.ContainsKey(entityID) && entityToIndices[entityID].Count > 0;
|
|
|
|
|
}
|
2022-03-18 19:50:59 +00:00
|
|
|
|
|
2022-04-08 05:52:03 +00:00
|
|
|
|
public override void Clear()
|
2022-03-18 19:50:59 +00:00
|
|
|
|
{
|
2022-04-08 05:52:03 +00:00
|
|
|
|
count = 0;
|
|
|
|
|
foreach (var set in entityToIndices.Values)
|
|
|
|
|
{
|
|
|
|
|
set.Clear();
|
|
|
|
|
}
|
2022-03-18 19:50:59 +00:00
|
|
|
|
}
|
2022-03-07 07:17:05 +00:00
|
|
|
|
}
|
|
|
|
|
}
|