From 47dfbb82445fa31afa103394d612845065e51140 Mon Sep 17 00:00:00 2001 From: cyxarukk Date: Mon, 11 Mar 2024 19:10:31 +0400 Subject: [PATCH] =?UTF-8?q?=D0=B8=D0=B7=D0=BC=D0=B5=D0=BD=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../MovementStrategy/MoveToBorder.cs | 52 +++++++++++++++++++ .../MovementStrategy/MoveToCenter.cs | 48 +++++++++++++++++ .../MovementStrategy/MoveableMachine.cs | 4 +- 3 files changed, 102 insertions(+), 2 deletions(-) create mode 100644 ProjectCar/ProjectCar/MovementStrategy/MoveToBorder.cs create mode 100644 ProjectCar/ProjectCar/MovementStrategy/MoveToCenter.cs diff --git a/ProjectCar/ProjectCar/MovementStrategy/MoveToBorder.cs b/ProjectCar/ProjectCar/MovementStrategy/MoveToBorder.cs new file mode 100644 index 0000000..88926fb --- /dev/null +++ b/ProjectCar/ProjectCar/MovementStrategy/MoveToBorder.cs @@ -0,0 +1,52 @@ +namespace ProjectGasMachine.MovementStrategy; +/// +/// Стратегия перемещения объекта к краю +/// +public class MoveToBorder : AbstractStrategy +{ + protected override bool IsTargetDestinaion() + { + ObjectParameters? objParams = GetObjectParameters; + if (objParams == null) + { + return false; + } + return objParams.RightBorder - GetStep() <= FieldWidth && objParams.RightBorder + GetStep() >= FieldWidth && + objParams.DownBorder - GetStep() <= FieldHeight && objParams.DownBorder + GetStep() >= FieldHeight; + } + + protected override void MoveToTarget() + { + ObjectParameters? objParams = GetObjectParameters; + if (objParams == null) + { + return; + } + + int diffX = objParams.RightBorder - FieldWidth; + if (Math.Abs(diffX) > GetStep()) + { + if (diffX > 0) + { + MoveLeft(); + } + else + { + MoveRight(); + } + } + + int diffY = objParams.DownBorder - FieldHeight; + if (Math.Abs(diffY) > GetStep()) + { + if (diffY > 0) + { + MoveUp(); + } + else + { + MoveDown(); + } + } + } +} \ No newline at end of file diff --git a/ProjectCar/ProjectCar/MovementStrategy/MoveToCenter.cs b/ProjectCar/ProjectCar/MovementStrategy/MoveToCenter.cs new file mode 100644 index 0000000..b23e939 --- /dev/null +++ b/ProjectCar/ProjectCar/MovementStrategy/MoveToCenter.cs @@ -0,0 +1,48 @@ +namespace ProjectGasMachine.MovementStrategy; + +internal class MoveToCenter : AbstractStrategy +{ + protected override bool IsTargetDestinaion() + { + ObjectParameters? objParams = GetObjectParameters; + if (objParams == null) + { + return false; + } + return objParams.ObjectMiddleHorizontal - GetStep() <= FieldWidth / 2 && objParams.ObjectMiddleHorizontal + GetStep() >= FieldWidth / 2 && + objParams.ObjectMiddleVertical - GetStep() <= FieldHeight / 2 && objParams.ObjectMiddleVertical + GetStep() >= FieldHeight / 2; + } + + protected override void MoveToTarget() + { + ObjectParameters? objParams = GetObjectParameters; + if (objParams == null) + { + return; + } + int diffX = objParams.ObjectMiddleHorizontal - FieldWidth / 2; + if (Math.Abs(diffX) > GetStep()) + { + if (diffX > 0) + { + MoveLeft(); + } + else + { + MoveRight(); + } + } + int diffY = objParams.ObjectMiddleVertical - FieldHeight / 2; + if (Math.Abs(diffY) > GetStep()) + { + if (diffY > 0) + { + MoveUp(); + } + else + { + MoveDown(); + } + } + } +} diff --git a/ProjectCar/ProjectCar/MovementStrategy/MoveableMachine.cs b/ProjectCar/ProjectCar/MovementStrategy/MoveableMachine.cs index 50e6c0e..5c18839 100644 --- a/ProjectCar/ProjectCar/MovementStrategy/MoveableMachine.cs +++ b/ProjectCar/ProjectCar/MovementStrategy/MoveableMachine.cs @@ -53,8 +53,8 @@ public class MoveableMachine : IMoveableObjects /// /// конвертация из MovementDirection в DirectionType /// - /// - /// + /// MovementDirection + /// DirectionType private static DirectionType GetDirectionType(MovementDirection direction) { return direction switch