diff --git a/WinFormsLabRad1/WinFormsLabRad1/Drawnings/DirectionType.cs b/WinFormsLabRad1/WinFormsLabRad1/Drawnings/DirectionType.cs index 5df2d6a..e0aab54 100644 --- a/WinFormsLabRad1/WinFormsLabRad1/Drawnings/DirectionType.cs +++ b/WinFormsLabRad1/WinFormsLabRad1/Drawnings/DirectionType.cs @@ -11,6 +11,10 @@ namespace WinFormsLabRad1.Drawnings /// + /// Неизвестное направление + /// + Unknow = -1, /// /// Вверх /// diff --git a/WinFormsLabRad1/WinFormsLabRad1/Drawnings/DrawningPlatforma.cs b/WinFormsLabRad1/WinFormsLabRad1/Drawnings/DrawningPlatforma.cs index b000e75..70a2a35 100644 --- a/WinFormsLabRad1/WinFormsLabRad1/Drawnings/DrawningPlatforma.cs +++ b/WinFormsLabRad1/WinFormsLabRad1/Drawnings/DrawningPlatforma.cs @@ -40,6 +40,22 @@ namespace WinFormsLabRad1.Drawnings /// Высота прорисовки автомобиля /// private readonly int _drawningPlatformaHeight = 30; + /// + /// Координата X объекта + /// + public int? GetPosX => _startPosX; + /// + /// Координата Y объекта + /// + public int? GetPosY => _startPosY; + /// + /// Ширина объекта + /// + public int GetWidth => _drawningPlatformaWidth; + /// + /// Высота объекта + /// + public int GetHeight => _drawningPlatformaHeight; private DrawningPlatforma() { diff --git a/WinFormsLabRad1/WinFormsLabRad1/MovementStrategy/AbstractStrategy.cs b/WinFormsLabRad1/WinFormsLabRad1/MovementStrategy/AbstractStrategy.cs new file mode 100644 index 0000000..2233525 --- /dev/null +++ b/WinFormsLabRad1/WinFormsLabRad1/MovementStrategy/AbstractStrategy.cs @@ -0,0 +1,128 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace WinFormsLabRad1.MovementStrategy +{ + /// + /// Класс-стратегия перемещения объекта + /// + public abstract class AbstractStrategy + { + /// + /// Перемещаемый объект + /// + private IMoveableObject? _moveableObject; + /// + /// Статус перемещения + /// + private StrategyStatus _state = StrategyStatus.NotInit; + /// + /// Ширина поля + /// + protected int FieldWidth { get; private set; } + /// + /// /// Высота поля + /// + protected int FieldHeight { get; private set; } + /// + /// Статус перемещения + /// + public StrategyStatus GetStatus() { return _state; } + /// + /// Установка данных + /// + /// Перемещаемый объект + /// Ширина поля + /// Высота поля + public void SetData(IMoveableObject moveableObject, int width, int height) + { + if (moveableObject == null) + { + _state = StrategyStatus.NotInit; + return; + } + _state = StrategyStatus.InProgress; + _moveableObject = moveableObject; + FieldWidth = width; + FieldHeight = height; + } + /// + /// Шаг перемещения + /// + public void MakeStep() + { + if (_state != StrategyStatus.InProgress) + { + return; + } + if (IsTargetDestinaion()) + { + _state = StrategyStatus.Finish; + return; + } + MoveToTarget(); + } + + /// + /// Перемещение влево + /// + /// Результат перемещения (true - удалось переместиться, false - неудача) + protected bool MoveLeft() => MoveTo(MovementDirection.Left); + /// + /// Перемещение вправо + /// + /// Результат перемещения (true - удалось переместиться, false - неудача) + protected bool MoveRight() => MoveTo(MovementDirection.Right); + /// + /// Перемещение вверх + /// + /// Результат перемещения (true - удалось переместиться, false - неудача) + protected bool MoveUp() => MoveTo(MovementDirection.Up); + /// + /// Перемещение вниз + /// + /// Результат перемещения (true - удалось переместиться, false - неудача) + protected bool MoveDown() => MoveTo(MovementDirection.Down); + /// + /// Параметры объекта + /// + protected ObjectParameters? GetObjectParameters => _moveableObject?.GetObjectPosition; + /// + /// Шаг объекта + /// + /// + protected int? GetStep() + { + if (_state != StrategyStatus.InProgress) + { + return null; + } + return _moveableObject?.GetStep; + } + /// + /// Перемещение к цели + /// + protected abstract void MoveToTarget(); + /// + /// Достигнута ли цель + /// + /// + protected abstract bool IsTargetDestinaion(); + /// + /// Попытка перемещения в требуемом направлении + /// + /// Направление + /// Результат попытки (true - удалось переместиться, false - неудача) + private bool MoveTo(MovementDirection movementDirection) + { + if (_state != StrategyStatus.InProgress) + { + return false; + } + return _moveableObject?.TryMoveObject(movementDirection) ?? false; + } + } +} diff --git a/WinFormsLabRad1/WinFormsLabRad1/MovementStrategy/IMoveableObject.cs b/WinFormsLabRad1/WinFormsLabRad1/MovementStrategy/IMoveableObject.cs new file mode 100644 index 0000000..806989d --- /dev/null +++ b/WinFormsLabRad1/WinFormsLabRad1/MovementStrategy/IMoveableObject.cs @@ -0,0 +1,29 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace WinFormsLabRad1.MovementStrategy +{ + /// + /// Интерфейс для работы с перемещаемым объектом + /// + public interface IMoveableObject + { + /// + /// Получение координаты объекта + /// + ObjectParameters? GetObjectPosition { get; } + /// + /// Шаг объекта + /// + int GetStep { get; } + /// + /// Попытка переместить объект в указанном направлении + /// + /// Направление + /// true - объект перемещен, false - перемещение невозможно + bool TryMoveObject(MovementDirection direction); + } +} diff --git a/WinFormsLabRad1/WinFormsLabRad1/MovementStrategy/MoveablePlatforma.cs b/WinFormsLabRad1/WinFormsLabRad1/MovementStrategy/MoveablePlatforma.cs new file mode 100644 index 0000000..256d266 --- /dev/null +++ b/WinFormsLabRad1/WinFormsLabRad1/MovementStrategy/MoveablePlatforma.cs @@ -0,0 +1,65 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using WinFormsLabRad1.Drawnings; + +namespace WinFormsLabRad1.MovementStrategy +{ + /// + /// Класс-реализация IMoveableObject с использованием DrawningCar + /// + public class MoveablePlatforma : IMoveableObject + { + /// + /// Поле-объект класса DrawningCar или его наследника + /// + private readonly DrawningPlatforma? _platforma = null; + /// + /// Конструктор + /// + /// Объект класса DrawningCar + public MoveablePlatforma(DrawningPlatforma platforma) + { + _platforma = platforma); + } + public ObjectParameters? GetObjectPosition + { + get + { + if (_platforma == null || _platforma.EntityPlatforma == null || !_platforma.GetPosX.HasValue || !_platforma.GetPosY.HasValue) + { + return null; + } + return new ObjectParameters(_platforma.GetPosX.Value, _platforma.GetPosY.Value, _platforma.GetWidth, _platforma.GetHeight); + } + } + public int GetStep => (int)(_platforma?.EntityPlatforma?.Step ?? 0); + public bool TryMoveObject(MovementDirection direction) + { + if (_platforma == null || _platforma.EntityPlatforma == null) + { + return false; + } + return _platforma.MoveTransport(GetDirectionType(direction)); + } + /// + /// Конвертация из MovementDirection в DirectionType + /// + /// MovementDirection + /// DirectionType + private static DirectionType GetDirectionType(MovementDirection direction) + { + return direction switch + { + MovementDirection.Left => DirectionType.Left, + MovementDirection.Right => DirectionType.Right, + MovementDirection.Up => DirectionType.Up, + MovementDirection.Down => DirectionType.Down, + _ => DirectionType.Unknow, + }; + } + + } +} diff --git a/WinFormsLabRad1/WinFormsLabRad1/MovementStrategy/MovementDirection.cs b/WinFormsLabRad1/WinFormsLabRad1/MovementStrategy/MovementDirection.cs new file mode 100644 index 0000000..6b852d7 --- /dev/null +++ b/WinFormsLabRad1/WinFormsLabRad1/MovementStrategy/MovementDirection.cs @@ -0,0 +1,31 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace WinFormsLabRad1.MovementStrategy +{ + /// + /// Направление перемещения + /// + /// Вверх + /// + Up = 1, + /// + /// Вниз + /// + Down = 2, + /// + /// Влево + /// + Left = 3, + /// + /// Вправо + /// + Right = 4 + } +} diff --git a/WinFormsLabRad1/WinFormsLabRad1/MovementStrategy/ObjectParameters.cs b/WinFormsLabRad1/WinFormsLabRad1/MovementStrategy/ObjectParameters.cs new file mode 100644 index 0000000..3364b36 --- /dev/null +++ b/WinFormsLabRad1/WinFormsLabRad1/MovementStrategy/ObjectParameters.cs @@ -0,0 +1,67 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace WinFormsLabRad1.MovementStrategy +{ + public class ObjectParameters + { + /// + /// Координата X + /// + private readonly int _x; + /// + /// Координата Y + /// + 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; + /// + /// Конструктор + /// + /// Координата X + /// Координата Y + /// Ширина объекта + /// Высота объекта + public ObjectParameters(int x, int y, int width, int height) + { + _x = x; + 45 + _y = y; + _width = width; + _height = height; + } + } +} diff --git a/WinFormsLabRad1/WinFormsLabRad1/MovementStrategy/StrategyStatus.cs b/WinFormsLabRad1/WinFormsLabRad1/MovementStrategy/StrategyStatus.cs new file mode 100644 index 0000000..d597b85 --- /dev/null +++ b/WinFormsLabRad1/WinFormsLabRad1/MovementStrategy/StrategyStatus.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace WinFormsLabRad1.MovementStrategy +{ + /// + /// Статус выполнения операции перемещения + /// + public enum StrategyStatus + { + /// + /// Все готово к началу + /// + NotInit, + /// + /// Выполняется + /// + InProgress, + /// + /// Завершено + /// + Finish + } +}