space_engineers/Mixins/Sequencer/SequenceableRotor.cs

64 lines
1.9 KiB
C#

using Sandbox.ModAPI.Ingame;
using System.Collections.Generic;
using System;
namespace IngameScript
{
partial class Program
{
public class SequenceableRotor : ISequenceable
{
public bool Running { get; private set; } = false;
public int Step { get; private set; }
private float _targetAngle;
private float _lastAngle;
private float _velocity;
private float _openAngle;
private float _closedAngle;
private IMyMotorStator _rotor;
public SequenceableRotor(IMyMotorStator rotor, int defaultStep = 0)
{
_rotor = rotor;
ConfigParser config = new ConfigParser(_rotor);
_openAngle = config.GetValue("OpenAngle", 90F);
_closedAngle = config.GetValue("ClosedAngle", 0F);
_velocity = config.GetValue("Velocity", 5F);
Step = config.GetValue("Step", defaultStep);
}
public void Start(bool reverse)
{
float degAngle = reverse ? _closedAngle : _openAngle;
_targetAngle = degToRad(degAngle);
_rotor.RotorLock = false;
_rotor.RotateToAngle(MyRotationDirection.AUTO, degAngle, _velocity);
Running = true;
}
public bool Check()
{
if (Math.Abs(_rotor.Angle - _targetAngle) < 0.1 && _rotor.Angle == _lastAngle)
{
return false;
}
_lastAngle = _rotor.Angle;
return true;
}
public void Finish()
{
_rotor.RotorLock = true;
Running = false;
}
private float degToRad(float degrees)
{
return degrees * ((float)Math.PI / 180F);
}
}
}
}