From 1da62721f7488a85c065baa4ce0cb7d5f1c7734f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=93=D0=B0=D0=BB=D0=B8=D0=BD=D0=B0=20=D0=A4=D0=B5=D0=B4?= =?UTF-8?q?=D0=BE=D1=80=D0=B5=D0=BD=D0=BA=D0=BE?= Date: Sun, 8 Oct 2023 21:22:30 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9D=D0=BE=D0=B2=D1=8B=D0=B5=20=D0=BC=D0=B5?= =?UTF-8?q?=D1=82=D0=BE=D0=B4=D1=8B=20=D0=B2=20DrawningCar=20=D0=B8=20?= =?UTF-8?q?=D1=80=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7=D0=B0=D1=86=D0=B8=D1=8F=20?= =?UTF-8?q?=D0=B8=D0=BD=D1=82=D0=B5=D1=80=D1=84=D0=B5=D0=B9=D1=81=D0=B0=20?= =?UTF-8?q?IMoveableObject?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Hydroplane/DrawningObjectCar.cs | 40 +++++++++++++++++++++++++ Hydroplane/DrawningPlane.cs | 52 ++++++++++++++++++++++++++++----- 2 files changed, 85 insertions(+), 7 deletions(-) create mode 100644 Hydroplane/DrawningObjectCar.cs diff --git a/Hydroplane/DrawningObjectCar.cs b/Hydroplane/DrawningObjectCar.cs new file mode 100644 index 0000000..1bcf0cb --- /dev/null +++ b/Hydroplane/DrawningObjectCar.cs @@ -0,0 +1,40 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Hydroplane.DrawningObjects; + +namespace Hydroplane.MovementStrategy +{ + /// + /// Реализация интерфейса IDrawningObject для работы с объектом DrawningCar (паттерн Adapter) + /// + public class DrawningObjectCar : IMoveableObject + { + private readonly DrawningPlane? _drawningPlane = null; + public DrawningObjectCar(DrawningPlane drawningCar) + { + _drawningPlane = drawningCar; + } + public ObjectParameters? GetObjectPosition + { + get + { + if (_drawningPlane == null || _drawningPlane.EntityPlane == + null) + { + return null; + } + return new ObjectParameters(_drawningPlane.GetPosX, + _drawningPlane.GetPosY, _drawningPlane.GetWidth, _drawningPlane.GetHeight); + } + } + public int GetStep => (int)(_drawningPlane?.EntityPlane?.Step ?? 0); + public bool CheckCanMove(DirectionType direction) => + _drawningPlane?.CanMove(direction) ?? false; + public void MoveObject(DirectionType direction) => + _drawningPlane?.MoveTransport(direction); + } + +} diff --git a/Hydroplane/DrawningPlane.cs b/Hydroplane/DrawningPlane.cs index fd0e107..54ecfac 100644 --- a/Hydroplane/DrawningPlane.cs +++ b/Hydroplane/DrawningPlane.cs @@ -92,42 +92,80 @@ namespace Hydroplane _startPosY = Math.Min(y, _pictureHeight - _planeHeight); } + /// + /// Координата X объекта + /// + public int GetPosX => _startPosX; + /// + /// Координата Y объекта + /// /// + public int GetPosY => _startPosY; + /// + /// Ширина объекта + /// + public int GetWidth => _pictureWidth; + /// + /// Высота объекта + /// + public int GetHeight => _pictureHeight; + /// + /// Проверка, что объект может переместится по указанному направлению + /// + /// Направление + /// true - можно переместится по указанному направлению + public bool CanMove(DirectionType direction) + { + if (EntityPlane == null) + { + return false; + } + return direction switch + { + //влево + DirectionType.Left => _startPosX - EntityPlane.Step > 0, + //вверх + DirectionType.Up => _startPosY - EntityPlane.Step > 0, + //вправо + DirectionType.Right => _startPosX + EntityPlane.Step < _pictureWidth, + //вниз + DirectionType.Down => _startPosY + EntityPlane.Step < _pictureHeight, + _ => false, + }; + } + /// /// Изменение направления перемещения /// /// Направление + public void MoveTransport(DirectionType direction) { - if (EntityPlane == null) + if (!CanMove(direction) || EntityPlane == null) { return; } switch (direction) { - //влево case DirectionType.Left: if (_startPosX - EntityPlane.Step > 0) { _startPosX -= (int)EntityPlane.Step; } break; - //вверх case DirectionType.Up: if (_startPosY - EntityPlane.Step > 0) { _startPosY -= (int)EntityPlane.Step; } break; - // вправо case DirectionType.Right: - if (_startPosX + EntityPlane.Step + _planeWidth < _pictureWidth) + if (_startPosX + EntityPlane.Step + _pictureWidth < _pictureWidth) { _startPosX += (int)EntityPlane.Step; } break; - //вниз case DirectionType.Down: - if (_startPosY + EntityPlane.Step + _planeHeight < _pictureHeight) + if (_startPosY + EntityPlane.Step + _pictureHeight < _pictureHeight) { _startPosY += (int)EntityPlane.Step; }