using System; using System.Collections.Generic; namespace MoonTools.ECS { public abstract class System : EntityComponentReader { internal MessageDepot MessageDepot; internal void RegisterMessageDepot(MessageDepot messageDepot) { MessageDepot = messageDepot; } public System(World world) { world.AddSystem(this); } public abstract void Update(TimeSpan delta); protected Entity CreateEntity() { return EntityStorage.Create(); } protected void Set(in Entity entity, in TComponent component) where TComponent : struct { #if DEBUG // check for use after destroy if (!Exists(entity)) { throw new ArgumentException("This entity is not valid!"); } #endif ComponentDepot.Set(entity.ID, component); } protected void Remove(in Entity entity) where TComponent : struct { ComponentDepot.Remove(entity.ID); } protected ReadOnlySpan ReadMessages() where TMessage : struct { return MessageDepot.All(); } protected TMessage ReadMessage() where TMessage : struct { return MessageDepot.First(); } protected bool SomeMessage() where TMessage : struct { return MessageDepot.Some(); } protected IEnumerable ReadMessagesWithEntity(in Entity entity) where TMessage : struct, IHasEntity { return MessageDepot.WithEntity(entity.ID); } protected ref readonly TMessage ReadMessageWithEntity(in Entity entity) where TMessage : struct, IHasEntity { return ref MessageDepot.FirstWithEntity(entity.ID); } protected bool SomeMessageWithEntity(in Entity entity) where TMessage : struct, IHasEntity { return MessageDepot.SomeWithEntity(entity.ID); } protected void Send(in TMessage message) where TMessage : struct { MessageDepot.Add(message); } protected void Relate(in Entity entityA, in Entity entityB) { RelationDepot.Add(new Relation(entityA, entityB)); } protected void Unrelate(in Entity entityA, in Entity entityB) { RelationDepot.Remove(new Relation(entityA, entityB)); } protected void Destroy(in Entity entity) { ComponentDepot.OnEntityDestroy(entity.ID); RelationDepot.OnEntityDestroy(entity.ID); EntityStorage.Destroy(entity); } } }