Controller
Outerminds edited this page Feb 9, 2019
·
12 revisions
A controller is a wrapper around a execution node. It analyses, builds, executes and manages the state of its node based on each execution phase.
using System;
using Entia;
using Entia.Core;
using Entia.Modules;
using Entia.Nodes;
using Entia.Phases;
using Entia.Systems;
using static Entia.Nodes.Node;
public static class Game
{
public static readonly Node Node = Sequence(
System<Systems.Motion>(),
System<Systems.Input>(),
System<Systems.Health>()
);
public static void Run()
{
var world = new World();
var controllers = world.Controllers();
var resources = world.Resources();
// 'Control' returns a 'Result<T>' so the success case has to be extracted.
// When a controller is contructed with a 'Parallel' node, thread safety
// analysis will run and may detect data races causing the construction
// to fail, reporting error messages in the failure.
var result = controllers.Control(Node);
if (Result.TryValue(result, out var controller))
{
// This resource will allow the application to close.
ref var game = ref resources.Get<Resources.Game>();
// Execution phases can be ordered as desired but the order below
// highly recommended.
// Any additional execution phase can be inserted among these.
// Alternatively 'controller.Run(in game.Quit)' can be used as a
// shorthand if the execution routine doesn't need to be modified.
// Executes all 'IInitialize' systems.
controller.Run<Initialize>();
// Registers all 'IReact<T>' systems to their 'Reaction<T>'.
controller.Run<React.Initialize>();
while (!game.Quit)
{
// Executes all 'IRun' systems.
controller.Run<Run>();
}
// Unregisters all 'IReact<T>' systems from their 'Reaction<T>'.
controller.Run<React.Dispose>();
// Executes all 'IDispose' systems.
controller.Run<Dispose>();
}
// If the result has error messages, log them to the console.
else if (Result.TryMessages(result, out var messages))
foreach (var message in messages) Console.WriteLine(message);
}
}
namespace Resources
{
public struct Game : IResource { public bool Quit; }
}
namespace Systems
{
public struct Motion : IRun
{
public void Run() { /* TODO */ }
}
public struct Input : IRun
{
public void Run() { /* TODO */ }
}
public struct Health : IRun
{
public void Run() { /* TODO */ }
}
}