diff --git a/MechanicalDoor/Door.cs b/MechanicalDoor/Door.cs index 5a41af5..747af61 100644 --- a/MechanicalDoor/Door.cs +++ b/MechanicalDoor/Door.cs @@ -5,15 +5,16 @@ namespace IngameScript { public class Door { - public Door() + public Door(MyGridProgram p) { + P = p; Hinges = new List(); } // Add a hinge to the door public void AddHinge(IMyMotorStator hinge) { - Hinges.Add(new DoorHinge(hinge)); + Hinges.Add(new DoorHinge(P, hinge)); } public void OpenDoor() @@ -47,11 +48,12 @@ namespace IngameScript { foreach (DoorHinge hinge in Hinges) { - if (!hinge.Locked()) return false; + if (!hinge.Locked) return false; } return true; } - private List Hinges; + private MyGridProgram P {get; set; } + private List Hinges {get; set; } } } \ No newline at end of file diff --git a/MechanicalDoor/DoorHinge.cs b/MechanicalDoor/DoorHinge.cs index f929948..b4dae9e 100644 --- a/MechanicalDoor/DoorHinge.cs +++ b/MechanicalDoor/DoorHinge.cs @@ -6,9 +6,11 @@ namespace IngameScript { public class DoorHinge { - public DoorHinge(IMyMotorStator hinge) + public DoorHinge(MyGridProgram p, IMyMotorStator hinge) { + P = p; Hinge = hinge; + parseConfig(); } // For these two functions, IMyMotorStator.Angle reports radians, but @@ -16,14 +18,14 @@ namespace IngameScript public void OpenDoorHinge() { Hinge.RotorLock = false; - TargetAngle = DegToRad(OpenAngle); + TargetAngle = degToRad(OpenAngle); Hinge.RotateToAngle(MyRotationDirection.AUTO, OpenAngle, Velocity); } public void CloseDoorHinge() { Hinge.RotorLock = false; - TargetAngle = DegToRad(ClosedAngle); + TargetAngle = degToRad(ClosedAngle); Hinge.RotateToAngle(MyRotationDirection.AUTO, ClosedAngle, Velocity); } @@ -37,22 +39,21 @@ namespace IngameScript Hinge.RotorLock = true; } LastAngle = Hinge.Angle; - return Locked(); + return Locked; } - public bool Locked() - { - return Hinge.RotorLock; - } - private IMyMotorStator Hinge { get; set; } - 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; + public bool Locked { get { return Hinge.RotorLock; } } - 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'); foreach (string line in lines) @@ -74,9 +75,7 @@ namespace IngameScript } } - // TODO: a utility class or function would be lovely... - // In general, the encapsulation feels a little screwy here. - private float DegToRad(float degrees) + private float degToRad(float degrees) { return degrees * ((float)Math.PI / 180F); } diff --git a/MechanicalDoor/Program.cs b/MechanicalDoor/Program.cs index e2f22eb..e248bd7 100644 --- a/MechanicalDoor/Program.cs +++ b/MechanicalDoor/Program.cs @@ -45,7 +45,7 @@ namespace IngameScript string doorName = hinge.CustomName.Split(' ')[0]; if (!doors.ContainsKey(doorName)) { - doors[doorName] = new Door(); + doors[doorName] = new Door(this); } doors[doorName].AddHinge(hinge); }