More idioms.

This commit is contained in:
Anna Rose 2025-02-07 15:25:25 -05:00
parent 2227c92377
commit d2952a3111
2 changed files with 33 additions and 34 deletions

View File

@ -7,19 +7,19 @@ namespace IngameScript
{
public Door(MyGridProgram p)
{
P = p;
Hinges = new List<DoorHinge>();
_p = p;
_hinges = new List<DoorHinge>();
}
// 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<DoorHinge> Hinges {get; set; }
private MyGridProgram _p;
private List<DoorHinge> _hinges;
}
}

View File

@ -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;
}
}