diff --git a/lainer/Lainer1/MoveToCenter.cs b/lainer/Lainer1/MoveToCenter.cs new file mode 100644 index 0000000..651dcb2 --- /dev/null +++ b/lainer/Lainer1/MoveToCenter.cs @@ -0,0 +1,50 @@ +namespace ProjectLainer.MovementStrategy +{ + public class MoveToCenter : AbstractStrategy + { + protected override bool IsTargetDestinaion() + { + var objParams = GetObjectParameters; + if (objParams == null) + { + return false; + } + return objParams.ObjectMiddleHorizontal <= FieldWidth / 2 && + objParams.ObjectMiddleHorizontal + GetStep() >= FieldWidth / 2 && + objParams.ObjectMiddleVertical <= FieldHeight / 2 && + objParams.ObjectMiddleVertical + GetStep() >= FieldHeight / 2; + } + protected override void MoveToTarget() + { + var objParams = GetObjectParameters; + if (objParams == null) + { + return; + } + var diffX = objParams.ObjectMiddleHorizontal - FieldWidth / 2; + if (Math.Abs(diffX) > GetStep()) + { + if (diffX > 0) + { + MoveLeft(); + } + else + { + MoveRight(); + } + } + var diffY = objParams.ObjectMiddleVertical - FieldHeight / 2; + if (Math.Abs(diffY) > GetStep()) + { + if (diffY > 0) + { + MoveUp(); + } + else + { + MoveDown(); + } + } + } + } +} diff --git a/lainer/Lainer1/ObjectParameters.cs b/lainer/Lainer1/ObjectParameters.cs new file mode 100644 index 0000000..ddd3a33 --- /dev/null +++ b/lainer/Lainer1/ObjectParameters.cs @@ -0,0 +1,23 @@ +namespace ProjectLainer.MovementStrategy +{ + public class ObjectParameters + { + private readonly int _x; + private readonly int _y; + private readonly int _width; + private readonly int _height; + public int LeftBorder => _x; + public int TopBorder => _y; + public int RightBorder => _x + _width; + public int DownBorder => _y + _height; + public int ObjectMiddleHorizontal => _x + _width / 2; + public int ObjectMiddleVertical => _y + _height / 2; + public ObjectParameters(int x, int y, int width, int height) + { + _x = x; + _y = y; + _width = width; + _height = height; + } + } +} \ No newline at end of file diff --git a/lainer/Lainer1/Status.cs b/lainer/Lainer1/Status.cs new file mode 100644 index 0000000..66dba9a --- /dev/null +++ b/lainer/Lainer1/Status.cs @@ -0,0 +1,9 @@ +namespace ProjectLainer.MovementStrategy +{ + public enum Status + { + NotInit, + InProgress, + Finish + } +}