From fdd3f8a0427bb1abc26fc4ee118a53c80e4b5c5b Mon Sep 17 00:00:00 2001 From: Ivan_Starostin Date: Fri, 8 Dec 2023 20:12:06 +0400 Subject: [PATCH] =?UTF-8?q?=D1=84=D0=B0=D0=B9=D0=BB=D1=8B=20=D0=BB=D0=B0?= =?UTF-8?q?=D0=B1=D0=BE=D1=80=D0=B0=D1=82=D0=BE=D1=80=D0=BD=D0=BE=D0=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lainer/Lainer1/MoveToCenter.cs | 50 ++++++++++++++++++++++++++++++ lainer/Lainer1/ObjectParameters.cs | 23 ++++++++++++++ lainer/Lainer1/Status.cs | 9 ++++++ 3 files changed, 82 insertions(+) create mode 100644 lainer/Lainer1/MoveToCenter.cs create mode 100644 lainer/Lainer1/ObjectParameters.cs create mode 100644 lainer/Lainer1/Status.cs 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 + } +}