Add a docking controller script using some new Block Actions.

This commit is contained in:
2025-02-19 19:41:53 -05:00
parent 54f04a7cf0
commit 11298083e7
20 changed files with 344 additions and 42 deletions

View File

@ -30,7 +30,7 @@ namespace IngameScript
}
// 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)
public void AddActionBlock(string actionName, int step, BlockAction actionBlock)
{
if (!_actions.ContainsKey(actionName)) _actions[actionName] = new ActionSequence(actionName);
_actions[actionName].Add(step, actionBlock);

View File

@ -8,9 +8,12 @@
<ItemGroup>
<Compile Include="$(MSBuildThisFileDirectory)ActionGroup.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ActionSequence.cs" />
<Compile Include="$(MSBuildThisFileDirectory)BlockActionConnector.cs" />
<Compile Include="$(MSBuildThisFileDirectory)BlockActionDoor.cs" />
<Compile Include="$(MSBuildThisFileDirectory)BlockActionGasTank.cs" />
<Compile Include="$(MSBuildThisFileDirectory)BlockActionPiston.cs" />
<Compile Include="$(MSBuildThisFileDirectory)BlockActionRotor.cs" />
<Compile Include="$(MSBuildThisFileDirectory)IBlockAction.cs" />
<Compile Include="$(MSBuildThisFileDirectory)BlockAction.cs" />
<Compile Include="$(MSBuildThisFileDirectory)BlockActionThruster.cs" />
</ItemGroup>
</Project>

View File

@ -14,6 +14,6 @@
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<OutputPath>bin\Release\</OutputPath>
</PropertyGroup>
<Import Project="Sequencer.projitems" Label="Shared"/>
<Import Project="ActionGroups.projitems" Label="Shared"/>
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\CodeSharing\Microsoft.CodeSharing.CSharp.targets"/>
</Project>

View File

@ -14,16 +14,16 @@ namespace IngameScript
public string Name { get; private set; }
public bool Running { get; private set; } = false;
private SortedDictionary<int, List<IBlockAction>> _steps = new SortedDictionary<int, List<IBlockAction>>();
private SortedDictionary<int, List<BlockAction>> _steps = new SortedDictionary<int, List<BlockAction>>();
public ActionSequence(string name)
{
Name = name;
}
public void Add(int step, IBlockAction action)
public void Add(int step, BlockAction action)
{
if (!_steps.ContainsKey(step)) _steps[step] = new List<IBlockAction>();
if (!_steps.ContainsKey(step)) _steps[step] = new List<BlockAction>();
_steps[step].Add(action);
}
@ -33,9 +33,9 @@ namespace IngameScript
Running = true;
List<IEnumerator<bool>> jobs = new List<IEnumerator<bool>>();
foreach (List<IBlockAction> step in _steps.Values)
foreach (List<BlockAction> step in _steps.Values)
{
foreach (IBlockAction action in step)
foreach (BlockAction action in step)
{
jobs.Add(action.Run());
}

View File

@ -0,0 +1,29 @@
// An interface to represent a pre-configured action performed asynchronously
// on a block. Implements a Run() method that executes the action and provides an
// Enumerator to monitor when the action is complete.
using System.Collections.Generic;
namespace IngameScript
{
partial class Program
{
public abstract class BlockAction
{
public bool Running { get; private set; } = false;
public IEnumerator<bool> Run()
{
if (Running) yield break;
Running = true;
IEnumerator<bool> childRun = onRun();
while (childRun.MoveNext()) yield return true;
Running = false;
}
protected abstract IEnumerator<bool> onRun();
}
}
}

View File

@ -0,0 +1,44 @@
using System.Collections.Generic;
using Sandbox.ModAPI.Ingame;
namespace IngameScript
{
partial class Program
{
public enum ConnectorAction
{
Connect,
Disconnect,
}
public class BlockActionConnector : BlockAction
{
private IMyShipConnector _connector;
private ConnectorAction _action;
public BlockActionConnector(
IMyShipConnector connector,
ConnectorAction action
)
{
_connector = connector;
_action = action;
}
protected override IEnumerator<bool> onRun()
{
switch (_action)
{
case ConnectorAction.Connect:
_connector.Connect();
while (_connector.Status != MyShipConnectorStatus.Connected) yield return true;
break;
case ConnectorAction.Disconnect:
_connector.Disconnect();
while (_connector.Status == MyShipConnectorStatus.Connected) yield return true;
break;
}
}
}
}
}

View File

@ -5,16 +5,14 @@ namespace IngameScript
{
partial class Program
{
public class BlockActionDoor : IBlockAction
public enum DoorAction
{
public enum DoorAction
{
Open,
Close,
}
public bool Running { get; private set; } = false;
Open,
Close,
}
public class BlockActionDoor : BlockAction
{
private IMyDoor _door;
private DoorAction _action;
private bool _lockDoor;
@ -30,7 +28,7 @@ namespace IngameScript
_lockDoor = lockDoor;
}
public IEnumerator<bool> Run()
protected override IEnumerator<bool> onRun()
{
switch (_action)
{

View File

@ -0,0 +1,43 @@
using System.Collections.Generic;
using Sandbox.ModAPI.Ingame;
namespace IngameScript
{
partial class Program
{
public enum GasTankAction
{
Stockpile,
Dispense,
}
public class BlockActionGasTank : BlockAction
{
private IMyGasTank _gasTank;
private GasTankAction _action;
public BlockActionGasTank(
IMyGasTank gasTank,
GasTankAction action
)
{
_gasTank = gasTank;
_action = action;
}
protected override IEnumerator<bool> onRun()
{
switch (_action)
{
case GasTankAction.Stockpile:
_gasTank.Stockpile = true;
break;
case GasTankAction.Dispense:
_gasTank.Stockpile = false;
break;
}
yield return true;
}
}
}
}

View File

@ -5,7 +5,7 @@ namespace IngameScript
{
partial class Program
{
public class BlockActionPiston : IBlockAction
public class BlockActionPiston : BlockAction
{
public bool Running { get; private set; } = false;

View File

@ -5,7 +5,7 @@ namespace IngameScript
{
partial class Program
{
public class BlockActionRotor : IBlockAction
public class BlockActionRotor : BlockAction
{
public bool Running { get; private set; } = false;

View File

@ -0,0 +1,43 @@
using System.Collections.Generic;
using Sandbox.ModAPI.Ingame;
namespace IngameScript
{
partial class Program
{
public enum ThrusterAction
{
Enable,
Disable,
}
public class BlockActionThruster : BlockAction
{
private IMyThrust _thruster;
private ThrusterAction _action;
public BlockActionThruster(
IMyThrust thruster,
ThrusterAction action
)
{
_thruster = thruster;
_action = action;
}
protected override IEnumerator<bool> onRun()
{
switch (_action)
{
case ThrusterAction.Enable:
_thruster.Enabled = true;
break;
case ThrusterAction.Disable:
_thruster.Enabled = false;
break;
}
yield return true;
}
}
}
}

View File

@ -1,17 +0,0 @@
// An interface to represent a pre-configured action performed asynchronously
// on a block. Implements a Run() method that executes the action and provides an
// Enumerator to monitor when the action is complete.
using System.Collections.Generic;
namespace IngameScript
{
partial class Program
{
public interface IBlockAction
{
bool Running { get; }
IEnumerator<bool> Run();
}
}
}