remove IHasEntity

filter_relations
cosmonaut 2022-12-09 11:29:43 -08:00
parent 307577d399
commit 01a9211ff4
4 changed files with 33 additions and 30 deletions

View File

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

View File

@ -7,7 +7,7 @@ namespace MoonTools.ECS
{ {
private Dictionary<Type, MessageStorage> storages = new Dictionary<Type, MessageStorage>(); private Dictionary<Type, MessageStorage> storages = new Dictionary<Type, MessageStorage>();
private MessageStorage<TMessage> Lookup<TMessage>() where TMessage : struct private MessageStorage<TMessage> Lookup<TMessage>() where TMessage : unmanaged
{ {
if (!storages.ContainsKey(typeof(TMessage))) if (!storages.ContainsKey(typeof(TMessage)))
{ {
@ -17,37 +17,42 @@ namespace MoonTools.ECS
return storages[typeof(TMessage)] as MessageStorage<TMessage>; return storages[typeof(TMessage)] as MessageStorage<TMessage>;
} }
public void Add<TMessage>(in TMessage message) where TMessage : struct public void Add<TMessage>(in TMessage message) where TMessage : unmanaged
{ {
Lookup<TMessage>().Add(message); Lookup<TMessage>().Add(message);
} }
public bool Some<TMessage>() where TMessage : struct public void Add<TMessage>(int entityID, in TMessage message) where TMessage : unmanaged
{
Lookup<TMessage>().Add(entityID, message);
}
public bool Some<TMessage>() where TMessage : unmanaged
{ {
return Lookup<TMessage>().Some(); return Lookup<TMessage>().Some();
} }
public ReadOnlySpan<TMessage> All<TMessage>() where TMessage : struct public ReadOnlySpan<TMessage> All<TMessage>() where TMessage : unmanaged
{ {
return Lookup<TMessage>().All(); return Lookup<TMessage>().All();
} }
public TMessage First<TMessage>() where TMessage : struct public TMessage First<TMessage>() where TMessage : unmanaged
{ {
return Lookup<TMessage>().First(); return Lookup<TMessage>().First();
} }
public IEnumerable<TMessage> WithEntity<TMessage>(int entityID) where TMessage : struct, IHasEntity public IEnumerable<TMessage> WithEntity<TMessage>(int entityID) where TMessage : unmanaged
{ {
return Lookup<TMessage>().WithEntity(entityID); return Lookup<TMessage>().WithEntity(entityID);
} }
public ref readonly TMessage FirstWithEntity<TMessage>(int entityID) where TMessage : struct, IHasEntity public ref readonly TMessage FirstWithEntity<TMessage>(int entityID) where TMessage : unmanaged
{ {
return ref Lookup<TMessage>().FirstWithEntity(entityID); return ref Lookup<TMessage>().FirstWithEntity(entityID);
} }
public bool SomeWithEntity<TMessage>(int entityID) where TMessage : struct, IHasEntity public bool SomeWithEntity<TMessage>(int entityID) where TMessage : unmanaged
{ {
return Lookup<TMessage>().SomeWithEntity(entityID); return Lookup<TMessage>().SomeWithEntity(entityID);
} }

View File

@ -8,7 +8,7 @@ namespace MoonTools.ECS
public abstract void Clear(); public abstract void Clear();
} }
internal class MessageStorage<TMessage> : MessageStorage where TMessage : struct internal class MessageStorage<TMessage> : MessageStorage where TMessage : unmanaged
{ {
private int count = 0; private int count = 0;
private int capacity = 128; private int capacity = 128;
@ -29,20 +29,20 @@ namespace MoonTools.ECS
} }
messages[count] = message; messages[count] = message;
if (message is IHasEntity entityMessage)
{
if (!entityToIndices.ContainsKey(entityMessage.Entity.ID))
{
entityToIndices.Add(entityMessage.Entity.ID, new List<int>());
}
entityToIndices[entityMessage.Entity.ID].Add(count);
}
count += 1; count += 1;
} }
public void Add(int entityID, in TMessage message)
{
if (!entityToIndices.ContainsKey(entityID))
{
entityToIndices.Add(entityID, new List<int>());
}
entityToIndices[entityID].Add(count);
Add(message);
}
public bool Some() public bool Some()
{ {
return count > 0; return count > 0;

View File

@ -43,17 +43,17 @@ namespace MoonTools.ECS
return MessageDepot.Some<TMessage>(); return MessageDepot.Some<TMessage>();
} }
protected IEnumerable<TMessage> ReadMessagesWithEntity<TMessage>(in Entity entity) where TMessage : unmanaged, IHasEntity protected IEnumerable<TMessage> ReadMessagesWithEntity<TMessage>(in Entity entity) where TMessage : unmanaged
{ {
return MessageDepot.WithEntity<TMessage>(entity.ID); return MessageDepot.WithEntity<TMessage>(entity.ID);
} }
protected ref readonly TMessage ReadMessageWithEntity<TMessage>(in Entity entity) where TMessage : unmanaged, IHasEntity protected ref readonly TMessage ReadMessageWithEntity<TMessage>(in Entity entity) where TMessage : unmanaged
{ {
return ref MessageDepot.FirstWithEntity<TMessage>(entity.ID); return ref MessageDepot.FirstWithEntity<TMessage>(entity.ID);
} }
protected bool SomeMessageWithEntity<TMessage>(in Entity entity) where TMessage : unmanaged, IHasEntity protected bool SomeMessageWithEntity<TMessage>(in Entity entity) where TMessage : unmanaged
{ {
return MessageDepot.SomeWithEntity<TMessage>(entity.ID); return MessageDepot.SomeWithEntity<TMessage>(entity.ID);
} }
@ -63,6 +63,11 @@ namespace MoonTools.ECS
MessageDepot.Add(message); MessageDepot.Add(message);
} }
protected void Send<TMessage>(in Entity entity, in TMessage message) where TMessage : unmanaged
{
MessageDepot.Add(entity.ID, message);
}
protected void Relate<TRelationKind>(in Entity entityA, in Entity entityB, TRelationKind relationData) where TRelationKind : unmanaged protected void Relate<TRelationKind>(in Entity entityA, in Entity entityB, TRelationKind relationData) where TRelationKind : unmanaged
{ {
RelationDepot.Set<TRelationKind>(new Relation(entityA, entityB), relationData); RelationDepot.Set<TRelationKind>(new Relation(entityA, entityB), relationData);