Start to make code slightly more idiomatic.

This commit is contained in:
Anna Rose 2025-02-07 15:19:57 -05:00
parent 13e1f9d63c
commit 2227c92377
3 changed files with 24 additions and 23 deletions

View File

@ -5,15 +5,16 @@ namespace IngameScript
{ {
public class Door public class Door
{ {
public Door() public Door(MyGridProgram p)
{ {
P = p;
Hinges = new List<DoorHinge>(); Hinges = new List<DoorHinge>();
} }
// Add a hinge to the door // Add a hinge to the door
public void AddHinge(IMyMotorStator hinge) public void AddHinge(IMyMotorStator hinge)
{ {
Hinges.Add(new DoorHinge(hinge)); Hinges.Add(new DoorHinge(P, hinge));
} }
public void OpenDoor() public void OpenDoor()
@ -47,11 +48,12 @@ namespace IngameScript
{ {
foreach (DoorHinge hinge in Hinges) foreach (DoorHinge hinge in Hinges)
{ {
if (!hinge.Locked()) return false; if (!hinge.Locked) return false;
} }
return true; return true;
} }
private List<DoorHinge> Hinges; private MyGridProgram P {get; set; }
private List<DoorHinge> Hinges {get; set; }
} }
} }

View File

@ -6,9 +6,11 @@ namespace IngameScript
{ {
public class DoorHinge public class DoorHinge
{ {
public DoorHinge(IMyMotorStator hinge) public DoorHinge(MyGridProgram p, IMyMotorStator hinge)
{ {
P = p;
Hinge = hinge; Hinge = hinge;
parseConfig();
} }
// For these two functions, IMyMotorStator.Angle reports radians, but // For these two functions, IMyMotorStator.Angle reports radians, but
@ -16,14 +18,14 @@ namespace IngameScript
public void OpenDoorHinge() public void OpenDoorHinge()
{ {
Hinge.RotorLock = false; Hinge.RotorLock = false;
TargetAngle = DegToRad(OpenAngle); TargetAngle = degToRad(OpenAngle);
Hinge.RotateToAngle(MyRotationDirection.AUTO, OpenAngle, Velocity); Hinge.RotateToAngle(MyRotationDirection.AUTO, OpenAngle, Velocity);
} }
public void CloseDoorHinge() public void CloseDoorHinge()
{ {
Hinge.RotorLock = false; Hinge.RotorLock = false;
TargetAngle = DegToRad(ClosedAngle); TargetAngle = degToRad(ClosedAngle);
Hinge.RotateToAngle(MyRotationDirection.AUTO, ClosedAngle, Velocity); Hinge.RotateToAngle(MyRotationDirection.AUTO, ClosedAngle, Velocity);
} }
@ -37,22 +39,21 @@ namespace IngameScript
Hinge.RotorLock = true; Hinge.RotorLock = true;
} }
LastAngle = Hinge.Angle; LastAngle = Hinge.Angle;
return Locked(); return Locked;
} }
public bool Locked()
{
return Hinge.RotorLock;
}
private IMyMotorStator Hinge { get; set; } public bool Locked { get { return Hinge.RotorLock; } }
private float TargetAngle { get; set; }
private float LastAngle { get; set; }
private float OpenAngle { get; set; } = 90F;
private float ClosedAngle { get; set; } = 0F;
private float Velocity { get; set; } = 5F;
private void ParseConfig() 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'); string[] lines = Hinge.CustomData.Split('\n');
foreach (string line in lines) foreach (string line in lines)
@ -74,9 +75,7 @@ namespace IngameScript
} }
} }
// TODO: a utility class or function would be lovely... private float degToRad(float degrees)
// In general, the encapsulation feels a little screwy here.
private float DegToRad(float degrees)
{ {
return degrees * ((float)Math.PI / 180F); return degrees * ((float)Math.PI / 180F);
} }

View File

@ -45,7 +45,7 @@ namespace IngameScript
string doorName = hinge.CustomName.Split(' ')[0]; string doorName = hinge.CustomName.Split(' ')[0];
if (!doors.ContainsKey(doorName)) if (!doors.ContainsKey(doorName))
{ {
doors[doorName] = new Door(); doors[doorName] = new Door(this);
} }
doors[doorName].AddHinge(hinge); doors[doorName].AddHinge(hinge);
} }