space_engineers/MechanicalDoor/DoorHinge.cs

83 lines
2.5 KiB
C#

using Sandbox.ModAPI.Ingame;
using System.Collections.Generic;
using System;
namespace IngameScript
{
public class DoorHinge
{
public DoorHinge(MyGridProgram p, IMyMotorStator hinge)
{
P = p;
Hinge = hinge;
parseConfig();
}
// 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;
}
public bool Locked { get { return Hinge.RotorLock; } }
private MyGridProgram P;
private IMyMotorStator Hinge;
private float TargetAngle;
private float LastAngle;
private float OpenAngle = 90F;
private float ClosedAngle = 0F;
private float Velocity = 5F;
private void parseConfig()
{
string[] lines = Hinge.CustomData.Split('\n');
foreach (string line in lines)
{
string[] tokens = line.Split('=');
if (tokens.Length != 2) continue;
switch (tokens[0])
{
case "OpenAngle":
OpenAngle = float.Parse(tokens[1]);
break;
case "ClosedAngle":
ClosedAngle = float.Parse(tokens[1]);
break;
case "Velocity":
Velocity = float.Parse(tokens[1]);
break;
}
}
}
private float degToRad(float degrees)
{
return degrees * ((float)Math.PI / 180F);
}
}
}