oecs-sharp/OECS/ISystem.cs

45 lines
1.3 KiB
C#
Raw Normal View History

namespace OECS;
/// <summary>
/// A system that runs on every tick.
/// Implement <see cref="RunImpl"/> with the system logic.
/// Call <see cref="ISystem.Run"/> to execute with batching.
/// </summary>
public interface ISystem
{
/// <summary>
/// Implement this with the system's logic for this tick.
/// The caller (e.g., <see cref="ISystem.Run"/>) wraps this
/// in a batching scope so mutations and component accesses
/// are automatically tracked.
/// </summary>
void RunImpl(World world);
}
/// <summary>
/// Extension methods for running systems with proper batching.
/// </summary>
public static class SystemExtensions
{
/// <summary>
/// Runs a system with a batching scope. Structural mutations are
/// buffered, and <see cref="World.GetComponent{T}"/> calls are
/// auto-tracked for modification until the system returns.
/// </summary>
public static void Run(this ISystem system, World world)
{
world.BeginBatching();
system.RunImpl(world);
world.EndBatching();
}
/// <summary>
/// Runs a ticked system with a batching scope.
/// </summary>
public static void Run(this ITickedSystem system, World world, Tick tick)
{
world.BeginBatching();
system.RunImpl(world, tick);
world.EndBatching();
}
}