space_engineers/MechanicalDoor/DoorHinge.cs
2025-02-08 21:26:21 -05:00

64 lines
2.0 KiB
C#

using Sandbox.ModAPI.Ingame;
using System.Collections.Generic;
using System;
namespace IngameScript
{
public class DoorHinge
{
public bool Locked { get { return _hinge.RotorLock; } }
private PrefixedConsole _console;
private IMyMotorStator _hinge;
private float _targetAngle;
private float _lastAngle;
private float _openAngle = 90F;
private float _closedAngle = 0F;
private float _velocity = 5F;
public DoorHinge(PrefixedConsole console, IMyMotorStator hinge)
{
_hinge = hinge;
_console = console;
ConfigParser config = new ConfigParser(_hinge);
_openAngle = config.GetValue("OpenAngle", 90F);
_closedAngle = config.GetValue("ClosedAngle", 0F);
_velocity = config.GetValue("Velocity", 5F);
}
// For these two functions, IMyMotorStator.Angle reports radians, but
// IMyMotorStator.RotateToAngle() expects degrees...
public void OpenDoorHinge()
{
_hinge.RotorLock = false;
_targetAngle = degToRad(_openAngle);
_hinge.RotateToAngle(MyRotationDirection.AUTO, _openAngle, _velocity);
}
public void CloseDoorHinge()
{
_hinge.RotorLock = false;
_targetAngle = degToRad(_closedAngle);
_hinge.RotateToAngle(MyRotationDirection.AUTO, _closedAngle, _velocity);
}
// Process the hinge's movement.
// It will return true when the panel has finished moving.
// TODO: Add a mechanism to determine when a door gets stuck or can't reach the target angle.
public bool Actuate()
{
if (Math.Abs(_hinge.Angle - _targetAngle) < 0.1 && _hinge.Angle == _lastAngle)
{
_hinge.RotorLock = true;
}
_lastAngle = _hinge.Angle;
return Locked;
}
private float degToRad(float degrees)
{
return degrees * ((float)Math.PI / 180F);
}
}
}