using System; using System.Collections.Generic; namespace MoonTools.ECS { public abstract class System : EntityComponentReader { internal MessageDepot MessageDepot => World.MessageDepot; public System(World world) : base(world) { } public abstract void Update(TimeSpan delta); protected Entity CreateEntity() { return EntityStorage.Create(); } protected void Set(in Entity entity, in TComponent component) where TComponent : unmanaged { #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 : unmanaged { ComponentDepot.Remove(entity.ID); } protected ReadOnlySpan ReadMessages() where TMessage : unmanaged { return MessageDepot.All(); } protected TMessage ReadMessage() where TMessage : unmanaged { return MessageDepot.First(); } protected bool SomeMessage() where TMessage : unmanaged { return MessageDepot.Some(); } protected IEnumerable ReadMessagesWithEntity(in Entity entity) where TMessage : unmanaged, IHasEntity { return MessageDepot.WithEntity(entity.ID); } protected ref readonly TMessage ReadMessageWithEntity(in Entity entity) where TMessage : unmanaged, IHasEntity { return ref MessageDepot.FirstWithEntity(entity.ID); } protected bool SomeMessageWithEntity(in Entity entity) where TMessage : unmanaged, IHasEntity { return MessageDepot.SomeWithEntity(entity.ID); } protected void Send(in TMessage message) where TMessage : unmanaged { MessageDepot.Add(message); } protected void Relate(in Entity entityA, in Entity entityB, TRelationKind relationData) where TRelationKind : unmanaged { RelationDepot.Set(new Relation(entityA, entityB), relationData); } protected void Unrelate(in Entity entityA, in Entity entityB) where TRelationKind : unmanaged { RelationDepot.Remove(new Relation(entityA, entityB)); } protected void Destroy(in Entity entity) { ComponentDepot.OnEntityDestroy(entity.ID); RelationDepot.OnEntityDestroy(entity.ID); EntityStorage.Destroy(entity); } } }