2022-04-08 05:52:03 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2022-03-05 02:01:44 +00:00
|
|
|
|
|
2022-04-08 05:52:03 +00:00
|
|
|
|
namespace MoonTools.ECS
|
2022-03-05 02:01:44 +00:00
|
|
|
|
{
|
2022-04-08 05:52:03 +00:00
|
|
|
|
public abstract class System : EntityComponentReader
|
2022-03-07 07:17:05 +00:00
|
|
|
|
{
|
2022-05-03 04:51:11 +00:00
|
|
|
|
internal MessageDepot MessageDepot => World.MessageDepot;
|
2022-03-07 07:17:05 +00:00
|
|
|
|
|
2022-05-03 04:51:11 +00:00
|
|
|
|
public System(World world) : base(world) { }
|
2022-03-07 07:17:05 +00:00
|
|
|
|
|
2022-04-08 05:52:03 +00:00
|
|
|
|
public abstract void Update(TimeSpan delta);
|
2022-03-05 02:01:44 +00:00
|
|
|
|
|
2023-01-10 00:41:00 +00:00
|
|
|
|
protected Entity CreateEntity() => World.CreateEntity();
|
|
|
|
|
|
|
|
|
|
protected void Set<TComponent>(in Entity entity, in TComponent component) where TComponent : unmanaged => World.Set<TComponent>(entity, component);
|
2022-03-05 02:01:44 +00:00
|
|
|
|
|
2023-01-10 00:41:00 +00:00
|
|
|
|
protected void Remove<TComponent>(in Entity entity) where TComponent : unmanaged
|
2022-04-08 05:52:03 +00:00
|
|
|
|
{
|
2023-01-10 00:41:00 +00:00
|
|
|
|
if (EntityStorage.RemoveComponent(entity.ID, ComponentTypeIndices.GetIndex<TComponent>()))
|
2022-04-08 05:52:03 +00:00
|
|
|
|
{
|
2023-01-10 00:41:00 +00:00
|
|
|
|
ComponentDepot.Remove<TComponent>(entity.ID);
|
|
|
|
|
FilterStorage.Check<TComponent>(entity.ID);
|
2022-04-08 05:52:03 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2022-03-05 02:01:44 +00:00
|
|
|
|
|
2023-01-10 00:41:00 +00:00
|
|
|
|
protected void Set<TComponent>(in Template template, in TComponent component) where TComponent : unmanaged => World.Set<TComponent>(template, component);
|
|
|
|
|
|
|
|
|
|
// This feature is EXPERIMENTAL. USe at your own risk!!
|
|
|
|
|
protected Entity Instantiate(in Template template) => World.Instantiate(template);
|
2022-03-16 07:33:38 +00:00
|
|
|
|
|
2022-05-03 04:51:11 +00:00
|
|
|
|
protected ReadOnlySpan<TMessage> ReadMessages<TMessage>() where TMessage : unmanaged
|
2022-04-08 05:52:03 +00:00
|
|
|
|
{
|
|
|
|
|
return MessageDepot.All<TMessage>();
|
|
|
|
|
}
|
2022-03-07 07:17:05 +00:00
|
|
|
|
|
2022-05-03 04:51:11 +00:00
|
|
|
|
protected TMessage ReadMessage<TMessage>() where TMessage : unmanaged
|
2022-04-08 05:52:03 +00:00
|
|
|
|
{
|
|
|
|
|
return MessageDepot.First<TMessage>();
|
|
|
|
|
}
|
2022-03-07 07:17:05 +00:00
|
|
|
|
|
2022-05-03 04:51:11 +00:00
|
|
|
|
protected bool SomeMessage<TMessage>() where TMessage : unmanaged
|
2022-04-08 05:52:03 +00:00
|
|
|
|
{
|
|
|
|
|
return MessageDepot.Some<TMessage>();
|
|
|
|
|
}
|
2022-03-18 19:50:59 +00:00
|
|
|
|
|
2023-01-10 00:41:00 +00:00
|
|
|
|
protected ReverseSpanEnumerator<TMessage> ReadMessagesWithEntity<TMessage>(in Entity entity) where TMessage : unmanaged
|
2022-04-08 05:52:03 +00:00
|
|
|
|
{
|
|
|
|
|
return MessageDepot.WithEntity<TMessage>(entity.ID);
|
|
|
|
|
}
|
2022-03-18 19:50:59 +00:00
|
|
|
|
|
2023-01-10 00:41:00 +00:00
|
|
|
|
protected ref readonly TMessage ReadMessageWithEntity<TMessage>(in Entity entity) where TMessage : unmanaged
|
2022-04-08 05:52:03 +00:00
|
|
|
|
{
|
|
|
|
|
return ref MessageDepot.FirstWithEntity<TMessage>(entity.ID);
|
|
|
|
|
}
|
2022-03-18 19:50:59 +00:00
|
|
|
|
|
2023-01-10 00:41:00 +00:00
|
|
|
|
protected bool SomeMessageWithEntity<TMessage>(in Entity entity) where TMessage : unmanaged
|
2022-04-08 05:52:03 +00:00
|
|
|
|
{
|
|
|
|
|
return MessageDepot.SomeWithEntity<TMessage>(entity.ID);
|
|
|
|
|
}
|
2022-03-07 18:56:53 +00:00
|
|
|
|
|
2022-05-03 04:51:11 +00:00
|
|
|
|
protected void Send<TMessage>(in TMessage message) where TMessage : unmanaged
|
2022-04-08 05:52:03 +00:00
|
|
|
|
{
|
|
|
|
|
MessageDepot.Add(message);
|
|
|
|
|
}
|
2022-04-06 19:53:50 +00:00
|
|
|
|
|
2023-01-10 00:41:00 +00:00
|
|
|
|
protected void Send<TMessage>(in Entity entity, in TMessage message) where TMessage : unmanaged
|
|
|
|
|
{
|
|
|
|
|
MessageDepot.Add(entity.ID, message);
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-03 04:51:11 +00:00
|
|
|
|
protected void Relate<TRelationKind>(in Entity entityA, in Entity entityB, TRelationKind relationData) where TRelationKind : unmanaged
|
2022-04-08 05:52:03 +00:00
|
|
|
|
{
|
2023-01-10 00:41:00 +00:00
|
|
|
|
RelationDepot.Set(entityA, entityB, relationData);
|
|
|
|
|
var relationTypeIndex = RelationTypeIndices.GetIndex<TRelationKind>();
|
|
|
|
|
EntityStorage.AddRelationKind(entityA.ID, relationTypeIndex);
|
|
|
|
|
EntityStorage.AddRelationKind(entityB.ID, relationTypeIndex);
|
2022-04-08 05:52:03 +00:00
|
|
|
|
}
|
2022-04-06 19:53:50 +00:00
|
|
|
|
|
2022-05-03 04:51:11 +00:00
|
|
|
|
protected void Unrelate<TRelationKind>(in Entity entityA, in Entity entityB) where TRelationKind : unmanaged
|
2022-04-08 05:52:03 +00:00
|
|
|
|
{
|
2023-01-10 00:41:00 +00:00
|
|
|
|
var (aEmpty, bEmpty) = RelationDepot.Remove<TRelationKind>(entityA, entityB);
|
|
|
|
|
|
|
|
|
|
if (aEmpty)
|
|
|
|
|
{
|
|
|
|
|
EntityStorage.RemoveRelation(entityA.ID, RelationTypeIndices.GetIndex<TRelationKind>());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (bEmpty)
|
|
|
|
|
{
|
|
|
|
|
EntityStorage.RemoveRelation(entityB.ID, RelationTypeIndices.GetIndex<TRelationKind>());
|
|
|
|
|
}
|
2022-04-08 05:52:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-08-09 21:41:31 +00:00
|
|
|
|
protected void UnrelateAll<TRelationKind>(in Entity entity) where TRelationKind : unmanaged
|
|
|
|
|
{
|
|
|
|
|
RelationDepot.UnrelateAll<TRelationKind>(entity.ID);
|
2023-01-10 00:41:00 +00:00
|
|
|
|
EntityStorage.RemoveRelation(entity.ID, RelationTypeIndices.GetIndex<TRelationKind>());
|
2022-08-09 21:41:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-10 00:41:00 +00:00
|
|
|
|
protected void Destroy(in Entity entity) => World.Destroy(entity);
|
2022-03-05 02:01:44 +00:00
|
|
|
|
}
|
|
|
|
|
}
|