2022-03-05 02:01:44 +00:00
|
|
|
|
namespace MoonTools.ECS;
|
|
|
|
|
|
|
|
|
|
public abstract class System : EntityComponentReader
|
|
|
|
|
{
|
2022-03-07 07:17:05 +00:00
|
|
|
|
internal MessageDepot MessageDepot;
|
2022-03-06 06:13:44 +00:00
|
|
|
|
|
2022-03-07 07:17:05 +00:00
|
|
|
|
internal void RegisterMessageDepot(MessageDepot messageDepot)
|
|
|
|
|
{
|
|
|
|
|
MessageDepot = messageDepot;
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-07 19:46:17 +00:00
|
|
|
|
public System(World world)
|
|
|
|
|
{
|
|
|
|
|
world.AddSystem(this);
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-07 07:17:05 +00:00
|
|
|
|
public abstract void Update(TimeSpan delta);
|
|
|
|
|
|
2022-03-07 19:46:17 +00:00
|
|
|
|
public virtual void InitializeFilters()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-05 02:01:44 +00:00
|
|
|
|
protected Entity CreateEntity()
|
|
|
|
|
{
|
|
|
|
|
return EntityStorage.Create();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected void Set<TComponent>(in Entity entity, in TComponent component) where TComponent : struct
|
|
|
|
|
{
|
|
|
|
|
ComponentDepot.Set<TComponent>(entity.ID, component);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected void Remove<TComponent>(in Entity entity) where TComponent : struct
|
|
|
|
|
{
|
|
|
|
|
ComponentDepot.Remove<TComponent>(entity.ID);
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-07 07:17:05 +00:00
|
|
|
|
protected ReadOnlySpan<TMessage> ReadMessages<TMessage>() where TMessage : struct
|
|
|
|
|
{
|
2022-03-18 19:50:59 +00:00
|
|
|
|
return MessageDepot.All<TMessage>();
|
2022-03-16 07:33:38 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected TMessage ReadMessage<TMessage>() where TMessage : struct
|
|
|
|
|
{
|
2022-03-18 19:50:59 +00:00
|
|
|
|
return MessageDepot.First<TMessage>();
|
2022-03-07 07:17:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected bool SomeMessage<TMessage>() where TMessage : struct
|
|
|
|
|
{
|
|
|
|
|
return MessageDepot.Some<TMessage>();
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-18 19:50:59 +00:00
|
|
|
|
protected IEnumerable<TMessage> ReadMessagesWithEntity<TMessage>(in Entity entity) where TMessage : struct, IHasEntity
|
|
|
|
|
{
|
|
|
|
|
return MessageDepot.WithEntity<TMessage>(entity.ID);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected ref readonly TMessage ReadMessageWithEntity<TMessage>(in Entity entity) where TMessage : struct, IHasEntity
|
|
|
|
|
{
|
|
|
|
|
return ref MessageDepot.FirstWithEntity<TMessage>(entity.ID);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected bool SomeMessageWithEntity<TMessage>(in Entity entity) where TMessage : struct, IHasEntity
|
|
|
|
|
{
|
|
|
|
|
return MessageDepot.SomeWithEntity<TMessage>(entity.ID);
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-07 18:56:53 +00:00
|
|
|
|
protected void Send<TMessage>(in TMessage message) where TMessage : struct
|
|
|
|
|
{
|
|
|
|
|
MessageDepot.Add(message);
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-05 02:01:44 +00:00
|
|
|
|
protected void Destroy(in Entity entity)
|
|
|
|
|
{
|
|
|
|
|
ComponentDepot.OnEntityDestroy(entity.ID);
|
|
|
|
|
EntityStorage.Destroy(entity);
|
|
|
|
|
}
|
|
|
|
|
}
|