space_engineers/Mixins/ActionGroups/ActionGroup.cs

62 lines
2.1 KiB
C#

// Represents a series of ActionSequences
// that are mutually exclusive. Typically
// this is designed to represent multiple ActionSequences
// that act on the same set of blocks, such as an
// "Extend" and "Retract" action for a multi-stage mechanical
// construction, or perhaps "Open" and "Close" sequences for a set of
// mechanical doors.
using System.Collections.Generic;
using System.Linq;
namespace IngameScript
{
partial class Program
{
public class ActionGroup
{
public string Name { get; private set; }
public bool Running { get; private set; } = false;
private Dictionary<string, ActionSequence> _actions = new Dictionary<string, ActionSequence>();
private IConsole _console;
public ActionGroup(IConsole console, string name)
{
// Todo: use a PrefixedConsole here?
_console = console;
Name = name;
}
// Add an action to the sequence called actionName, which will be created if it doesn't already exist.
public void AddActionBlock(string actionName, int step, IBlockAction actionBlock)
{
if (!_actions.ContainsKey(actionName)) _actions[actionName] = new ActionSequence(actionName);
_actions[actionName].Add(step, actionBlock);
}
public IEnumerator<bool> RunAction(string actionName)
{
if (Running)
{
_console.Print($"Ignoring action '{actionName}' on '{Name}'. Already running an action.");
yield break;
}
if (!_actions.ContainsKey(actionName))
{
_console.Print($"No action '{actionName}' defined for '{Name}'");
yield break;
}
Running = true;
IEnumerator<bool> job = _actions[actionName].Run();
while (job.MoveNext()) yield return true;
Running = false;
}
}
}
}