using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; using JetBrains.Annotations; using Robust.Shared.Interfaces.GameObjects; using Robust.Shared.Interfaces.GameObjects.Systems; using Robust.Shared.Interfaces.Network; using Robust.Shared.IoC; using Robust.Shared.Reflection; namespace Robust.Shared.GameObjects.Systems { /// /// A subsystem that acts on all components of a type at once. /// /// /// This class is instantiated by the EntitySystemManager, and any IoC Dependencies will be resolved. /// [Reflect(false), PublicAPI] public abstract class EntitySystem : IEntitySystem { [Dependency] protected readonly IEntityManager EntityManager; [Dependency] protected readonly IEntitySystemManager EntitySystemManager; [Dependency] protected readonly IEntityNetworkManager EntityNetworkManager; protected IEntityQuery EntityQuery; protected IEnumerable RelevantEntities => EntityManager.GetEntities(EntityQuery); /// public virtual void Initialize() { } /// public virtual void Update(float frameTime) { } /// public virtual void FrameUpdate(float frameTime) { } /// public virtual void Shutdown() { } #region Event Proxy protected void SubscribeNetworkEvent(EntityEventHandler handler) where T : EntitySystemMessage { EntityManager.EventBus.SubscribeEvent(EventSource.Network, this, handler); } protected void SubscribeNetworkEvent(EntitySessionEventHandler handler) where T : EntitySystemMessage { EntityManager.EventBus.SubscribeSessionEvent(EventSource.Network, this, handler); } protected void SubscribeLocalEvent(EntityEventHandler handler) where T : EntitySystemMessage { EntityManager.EventBus.SubscribeEvent(EventSource.Local, this, handler); } protected void SubscribeLocalEvent(EntitySessionEventHandler handler) where T : EntitySystemMessage { EntityManager.EventBus.SubscribeSessionEvent(EventSource.Local, this, handler); } protected void UnsubscribeNetworkEvent() where T : EntitySystemMessage { EntityManager.EventBus.UnsubscribeEvent(EventSource.Network, this); } protected void UnsubscribeLocalEvent() where T : EntitySystemMessage { EntityManager.EventBus.UnsubscribeEvent(EventSource.Local, this); } protected void RaiseLocalEvent(EntitySystemMessage message) { EntityManager.EventBus.RaiseEvent(EventSource.Local, message); } protected void QueueLocalEvent(EntitySystemMessage message) { EntityManager.EventBus.QueueEvent(EventSource.Local, message); } protected void RaiseNetworkEvent(EntitySystemMessage message) { EntityNetworkManager.SendSystemNetworkMessage(message); } protected void RaiseNetworkEvent(EntitySystemMessage message, INetChannel channel) { EntityNetworkManager.SendSystemNetworkMessage(message, channel); } protected Task AwaitNetworkEvent(CancellationToken cancellationToken) where T : EntitySystemMessage { return EntityManager.EventBus.AwaitEvent(EventSource.Network, cancellationToken); } #endregion #region Static Helpers /* NOTE: Static helpers relating to EntitySystems are here rather than in a static helper class for conciseness / usability. If we had an "EntitySystems" static class it would conflict with any imported namespace called "EntitySystems" and require using alias directive, and if we called it something longer like "EntitySystemUtility", writing out "EntitySystemUtility.Get" seems pretty tedious for a potentially commonly-used method. Putting it here allows writing "EntitySystem.Get" which is nice and concise. */ /// /// Gets the indicated entity system. /// /// entity system to get /// public static T Get() where T : IEntitySystem { return IoCManager.Resolve().GetEntitySystem(); } /// /// Tries to get an entity system of the specified type. /// /// Type of entity system to find. /// instance matching the specified type (if exists). /// If an instance of the specified entity system type exists. public static bool TryGet(out T entitySystem) where T : IEntitySystem { return IoCManager.Resolve().TryGetEntitySystem(out entitySystem); } #endregion } }