From d2952a31117de082b9594460fdf9fa6d67f4cb0d Mon Sep 17 00:00:00 2001 From: annabunches Date: Fri, 7 Feb 2025 15:25:25 -0500 Subject: [PATCH] More idioms. --- MechanicalDoor/Door.cs | 18 +++++++------- MechanicalDoor/DoorHinge.cs | 49 ++++++++++++++++++------------------- 2 files changed, 33 insertions(+), 34 deletions(-) diff --git a/MechanicalDoor/Door.cs b/MechanicalDoor/Door.cs index 747af61..50158d6 100644 --- a/MechanicalDoor/Door.cs +++ b/MechanicalDoor/Door.cs @@ -7,19 +7,19 @@ namespace IngameScript { public Door(MyGridProgram p) { - P = p; - Hinges = new List(); + _p = p; + _hinges = new List(); } // Add a hinge to the door public void AddHinge(IMyMotorStator hinge) { - Hinges.Add(new DoorHinge(P, hinge)); + _hinges.Add(new DoorHinge(_p, hinge)); } public void OpenDoor() { - foreach (DoorHinge hinge in Hinges) + foreach (DoorHinge hinge in _hinges) { hinge.OpenDoorHinge(); } @@ -27,7 +27,7 @@ namespace IngameScript public void CloseDoor() { - foreach (DoorHinge hinge in Hinges) + foreach (DoorHinge hinge in _hinges) { hinge.CloseDoorHinge(); } @@ -37,7 +37,7 @@ namespace IngameScript public bool Actuate() { bool done = true; - foreach (DoorHinge hinge in Hinges) + foreach (DoorHinge hinge in _hinges) { if (!hinge.Actuate()) done = false; } @@ -46,14 +46,14 @@ namespace IngameScript public bool Locked() { - foreach (DoorHinge hinge in Hinges) + foreach (DoorHinge hinge in _hinges) { if (!hinge.Locked) return false; } return true; } - private MyGridProgram P {get; set; } - private List Hinges {get; set; } + private MyGridProgram _p; + private List _hinges; } } \ No newline at end of file diff --git a/MechanicalDoor/DoorHinge.cs b/MechanicalDoor/DoorHinge.cs index b4dae9e..f64f57c 100644 --- a/MechanicalDoor/DoorHinge.cs +++ b/MechanicalDoor/DoorHinge.cs @@ -8,8 +8,8 @@ namespace IngameScript { public DoorHinge(MyGridProgram p, IMyMotorStator hinge) { - P = p; - Hinge = hinge; + _p = p; + _hinge = hinge; parseConfig(); } @@ -17,16 +17,16 @@ namespace IngameScript // IMyMotorStator.RotateToAngle() expects degrees... public void OpenDoorHinge() { - Hinge.RotorLock = false; - TargetAngle = degToRad(OpenAngle); - Hinge.RotateToAngle(MyRotationDirection.AUTO, OpenAngle, Velocity); + _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); + _hinge.RotorLock = false; + _targetAngle = degToRad(_closedAngle); + _hinge.RotateToAngle(MyRotationDirection.AUTO, _closedAngle, _velocity); } // Process the hinge's movement. @@ -34,28 +34,19 @@ namespace IngameScript // 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) + if (Math.Abs(_hinge.Angle - _targetAngle) < 0.1 && _hinge.Angle == _lastAngle) { - Hinge.RotorLock = true; + _hinge.RotorLock = true; } - LastAngle = Hinge.Angle; + _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; + public bool Locked { get { return _hinge.RotorLock; } } private void parseConfig() { - string[] lines = Hinge.CustomData.Split('\n'); + string[] lines = _hinge.CustomData.Split('\n'); foreach (string line in lines) { string[] tokens = line.Split('='); @@ -63,13 +54,13 @@ namespace IngameScript switch (tokens[0]) { case "OpenAngle": - OpenAngle = float.Parse(tokens[1]); + _openAngle = float.Parse(tokens[1]); break; case "ClosedAngle": - ClosedAngle = float.Parse(tokens[1]); + _closedAngle = float.Parse(tokens[1]); break; case "Velocity": - Velocity = float.Parse(tokens[1]); + _velocity = float.Parse(tokens[1]); break; } } @@ -79,5 +70,13 @@ namespace IngameScript { return degrees * ((float)Math.PI / 180F); } + + private MyGridProgram _p; + private IMyMotorStator _hinge; + private float _targetAngle; + private float _lastAngle; + private float _openAngle = 90F; + private float _closedAngle = 0F; + private float _velocity = 5F; } } \ No newline at end of file