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 if (EntityStorage.SetComponent(entity.ID, ComponentTypeIndices.GetIndex())) { FilterStorage.Check(entity.ID); } ComponentDepot.Set(entity.ID, component); } protected void Remove(in Entity entity) where TComponent : unmanaged { if (EntityStorage.RemoveComponent(entity.ID, ComponentTypeIndices.GetIndex())) { ComponentDepot.Remove(entity.ID); FilterStorage.Check(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); var relationTypeIndex = RelationTypeIndices.GetIndex(); EntityStorage.AddRelation(entityA.ID, relationTypeIndex); EntityStorage.AddRelation(entityB.ID, relationTypeIndex); } protected void Unrelate(in Entity entityA, in Entity entityB) where TRelationKind : unmanaged { var (aEmpty, bEmpty) = RelationDepot.Remove(new Relation(entityA, entityB)); if (aEmpty) { EntityStorage.RemoveRelation(entityA.ID, RelationTypeIndices.GetIndex()); } if (bEmpty) { EntityStorage.RemoveRelation(entityB.ID, RelationTypeIndices.GetIndex()); } } protected void UnrelateAll(in Entity entity) where TRelationKind : unmanaged { RelationDepot.UnrelateAll(entity.ID); EntityStorage.RemoveRelation(entity.ID, RelationTypeIndices.GetIndex()); } protected void Destroy(in Entity entity) { foreach (var componentTypeIndex in EntityStorage.ComponentTypeIndices(entity.ID)) { ComponentDepot.Remove(entity.ID, componentTypeIndex); FilterStorage.RemoveEntity(entity.ID, componentTypeIndex); } foreach (var relationTypeIndex in EntityStorage.RelationTypeIndices(entity.ID)) { RelationDepot.UnrelateAll(entity.ID, relationTypeIndex); } EntityStorage.Destroy(entity); } } }