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