diff --git a/WinFormsAppExcavator/WinFormsAppExcavator/Drawings/DrawingExcavator.cs b/WinFormsAppExcavator/WinFormsAppExcavator/Drawings/DrawingExcavator.cs index 79fb431..1b598b2 100644 --- a/WinFormsAppExcavator/WinFormsAppExcavator/Drawings/DrawingExcavator.cs +++ b/WinFormsAppExcavator/WinFormsAppExcavator/Drawings/DrawingExcavator.cs @@ -34,41 +34,6 @@ public class DrawingExcavator : DrawingExcavatorEmpty Brush additionalBrush = new SolidBrush(entityExcavator.AdditionalColor); Brush bodybrush = new SolidBrush(entityExcavator.BodyColor); - //границы экскаватора - g.DrawRectangle(pen, _startPosX.Value, _startPosY.Value + 20, 100, 30); - g.DrawRectangle(pen, _startPosX.Value + 30, _startPosY.Value, 10, 20); - g.DrawRectangle(pen, _startPosX.Value + 80, _startPosY.Value, 20, 20); - g.DrawArc(pen, _startPosX.Value, _startPosY.Value + 45, 30, 40, 130, 100); - g.DrawArc(pen, _startPosX.Value + 80, _startPosY.Value + 50, 30, 40, 285, 110); - g.DrawLine(pen, _startPosX.Value + 5, _startPosY.Value + 80, _startPosX.Value + 109, _startPosY.Value + 80); - g.DrawEllipse(pen, _startPosX.Value + 5, _startPosY.Value + 53, 25, 25); - g.DrawEllipse(pen, _startPosX.Value + 80, _startPosY.Value + 53, 25, 25); - g.DrawEllipse(pen, _startPosX.Value + 30, _startPosY.Value + 69, 10, 10); - g.DrawEllipse(pen, _startPosX.Value + 48, _startPosY.Value + 69, 10, 10); - g.DrawEllipse(pen, _startPosX.Value + 65, _startPosY.Value + 69, 10, 10); - g.DrawEllipse(pen, _startPosX.Value + 40, _startPosY.Value + 53, 7, 7); - g.DrawEllipse(pen, _startPosX.Value + 60, _startPosY.Value + 53, 7, 7); - - //заливка кабины и основного железа - Brush br = new SolidBrush(Color.Black); - g.FillRectangle(bodybrush, _startPosX.Value, _startPosY.Value + 20, 100, 30); - g.FillRectangle(bodybrush, _startPosX.Value + 30, _startPosY.Value, 10, 20); - g.FillRectangle(bodybrush, _startPosX.Value + 80, _startPosY.Value, 20, 20); - g.FillEllipse(br, _startPosX.Value + 5, _startPosY.Value + 53, 25, 25); - g.FillEllipse(br, _startPosX.Value + 80, _startPosY.Value + 53, 25, 25); - g.FillEllipse(br, _startPosX.Value + 30, _startPosY.Value + 69, 10, 10); - g.FillEllipse(br, _startPosX.Value + 48, _startPosY.Value + 69, 10, 10); - g.FillEllipse(br, _startPosX.Value + 65, _startPosY.Value + 69, 10, 10); - g.FillEllipse(br, _startPosX.Value + 40, _startPosY.Value + 53, 7, 7); - g.FillEllipse(br, _startPosX.Value + 60, _startPosY.Value + 53, 7, 7); - - //фары - Brush brfara = new SolidBrush(Color.Blue); - g.DrawRectangle(pen, _startPosX.Value, _startPosY.Value + 27, 10, 10); - g.FillRectangle(brfara, _startPosX.Value, _startPosY.Value + 27, 10, 10); - //окно - g.FillRectangle(brfara, _startPosX.Value + 85, _startPosY.Value + 10, 10, 10); - if (entityExcavator.Bucket) { diff --git a/WinFormsAppExcavator/WinFormsAppExcavator/Drawings/DrawingExcavatorEmpty.cs b/WinFormsAppExcavator/WinFormsAppExcavator/Drawings/DrawingExcavatorEmpty.cs index 99930f3..c7c05eb 100644 --- a/WinFormsAppExcavator/WinFormsAppExcavator/Drawings/DrawingExcavatorEmpty.cs +++ b/WinFormsAppExcavator/WinFormsAppExcavator/Drawings/DrawingExcavatorEmpty.cs @@ -45,6 +45,26 @@ public class DrawingExcavatorEmpty /// private readonly int _drawningExcavatorHeight = 75; + /// + /// Координата X объекта + /// + public int? GetPosX => _startPosX; + + /// + /// Координата Y объекта + /// + public int? GetPosY => _startPosY; + + /// + /// Ширина объекта + /// + public int GetWidth => _drawningExcavatorWidth; + + /// + /// Высота объекта + /// + public int GetHeight => _drawningExcavatorHeight; + /// /// Пустой конструктор /// diff --git a/WinFormsAppExcavator/WinFormsAppExcavator/FormExcavator.Designer.cs b/WinFormsAppExcavator/WinFormsAppExcavator/FormExcavator.Designer.cs index 9c6cc3d..5897aac 100644 --- a/WinFormsAppExcavator/WinFormsAppExcavator/FormExcavator.Designer.cs +++ b/WinFormsAppExcavator/WinFormsAppExcavator/FormExcavator.Designer.cs @@ -36,6 +36,8 @@ namespace WinFormsAppExcavator buttonDown = new Button(); buttonUp = new Button(); CreateExcavatorEmpty = new Button(); + buttonStrategyStep = new Button(); + comboBoxStrategy = new ComboBox(); ((System.ComponentModel.ISupportInitialize)pictureBoxExcavator).BeginInit(); SuspendLayout(); // @@ -118,11 +120,33 @@ namespace WinFormsAppExcavator CreateExcavatorEmpty.UseVisualStyleBackColor = true; CreateExcavatorEmpty.Click += CreateExcavatorEmpty_Click; // + // buttonStrategyStep + // + buttonStrategyStep.Location = new Point(790, 48); + buttonStrategyStep.Name = "buttonStrategyStep"; + buttonStrategyStep.Size = new Size(94, 29); + buttonStrategyStep.TabIndex = 8; + buttonStrategyStep.Text = "Шаг"; + buttonStrategyStep.UseVisualStyleBackColor = true; + buttonStrategyStep.Click += ButtonStrategyStep_Click; + // + // comboBoxStrategy + // + comboBoxStrategy.DropDownStyle = ComboBoxStyle.DropDownList; + comboBoxStrategy.FormattingEnabled = true; + comboBoxStrategy.Items.AddRange(new object[] { "К центру", "К краю" }); + comboBoxStrategy.Location = new Point(733, 12); + comboBoxStrategy.Name = "comboBoxStrategy"; + comboBoxStrategy.Size = new Size(151, 28); + comboBoxStrategy.TabIndex = 9; + // // FormExcavator // AutoScaleDimensions = new SizeF(8F, 20F); AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(898, 329); + Controls.Add(comboBoxStrategy); + Controls.Add(buttonStrategyStep); Controls.Add(CreateExcavatorEmpty); Controls.Add(buttonUp); Controls.Add(buttonDown); @@ -145,5 +169,7 @@ namespace WinFormsAppExcavator private Button buttonUp; public Button buttonCreat; public Button CreateExcavatorEmpty; + private Button buttonStrategyStep; + private ComboBox comboBoxStrategy; } } \ No newline at end of file diff --git a/WinFormsAppExcavator/WinFormsAppExcavator/FormExcavator.cs b/WinFormsAppExcavator/WinFormsAppExcavator/FormExcavator.cs index 166bba4..2ce12f8 100644 --- a/WinFormsAppExcavator/WinFormsAppExcavator/FormExcavator.cs +++ b/WinFormsAppExcavator/WinFormsAppExcavator/FormExcavator.cs @@ -1,5 +1,6 @@ using System; using WinFormsAppExcavator.Drawings; +using WinFormsAppExcavator.MovementStrategy; namespace WinFormsAppExcavator { @@ -13,12 +14,19 @@ namespace WinFormsAppExcavator /// private DrawingExcavatorEmpty? _drawingExcavatorEmpty; + + /// + /// Стратегия перемещения + /// + private AbstractStrategy? _strategy; + /// /// Конструктор формы /// public FormExcavator() { InitializeComponent(); + _strategy = null; } /// @@ -42,10 +50,10 @@ namespace WinFormsAppExcavator /// ///создание объекта класса перемещения /// - - private void CreateObject (String type) + + private void CreateObject(String type) { - Random random= new(); + Random random = new(); switch (type) { case nameof(DrawingExcavatorEmpty): @@ -65,6 +73,9 @@ namespace WinFormsAppExcavator _drawingExcavatorEmpty.SetPictureSize(pictureBoxExcavator.Width, pictureBoxExcavator.Height); _drawingExcavatorEmpty.SetPosition(random.Next(10, 100), random.Next(10, 100)); + _strategy = null; + comboBoxStrategy.Enabled = true; + Draw(); } @@ -125,6 +136,50 @@ namespace WinFormsAppExcavator } } - + + + /// + /// Обработка нажатия кнопки "Шаг" + /// + /// + /// + private void ButtonStrategyStep_Click(object sender, EventArgs e) + { + if (_drawingExcavatorEmpty == null) + { + return; + } + + if (comboBoxStrategy.Enabled) + { + _strategy = comboBoxStrategy.SelectedIndex switch + { + 0 => new MoveToCenter(), + 1 => new MoveToBorder(), + _ => null, + }; + if (_strategy == null) + { + return; + } + _strategy.SetData(new MoveableExcavator(_drawingExcavatorEmpty), pictureBoxExcavator.Width, pictureBoxExcavator.Height); + } + + if (_strategy == null) + { + return; + } + + comboBoxStrategy.Enabled = false; + _strategy.MakeStep(); + Draw(); + + if (_strategy.GetStatus() == StrategyStatus.Finish) + { + comboBoxStrategy.Enabled = true; + _strategy = null; + } + + } } } diff --git a/WinFormsAppExcavator/WinFormsAppExcavator/MovementStrategy/AbstractStrategy.cs b/WinFormsAppExcavator/WinFormsAppExcavator/MovementStrategy/AbstractStrategy.cs new file mode 100644 index 0000000..d2f93f2 --- /dev/null +++ b/WinFormsAppExcavator/WinFormsAppExcavator/MovementStrategy/AbstractStrategy.cs @@ -0,0 +1,140 @@ +namespace WinFormsAppExcavator.MovementStrategy; + +/// +/// Класс-стратегия перемещения объекта +/// +public abstract class AbstractStrategy +{ + /// + /// Перемещаемый объект + /// + private IMoveableObjectcs? _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(IMoveableObjectcs 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/WinFormsAppExcavator/WinFormsAppExcavator/MovementStrategy/IMoveableObjectcs.cs b/WinFormsAppExcavator/WinFormsAppExcavator/MovementStrategy/IMoveableObjectcs.cs new file mode 100644 index 0000000..8e45246 --- /dev/null +++ b/WinFormsAppExcavator/WinFormsAppExcavator/MovementStrategy/IMoveableObjectcs.cs @@ -0,0 +1,24 @@ +namespace WinFormsAppExcavator.MovementStrategy; + +/// +/// Интерфейс для работы с перемещаемым объектом +/// +public interface IMoveableObjectcs +{ + /// + /// Получение координаты объекта + /// + ObjectParameters? GetObjectPosition { get; } + + /// + /// Шаг объекта + /// + int GetStep { get; } + + /// + /// Попытка переместить объект в указанном направлении + /// + /// Направление + /// true - объект перемещен, false - перемещение невозможно + bool TryMoveObject(MovementDirection direction); +} diff --git a/WinFormsAppExcavator/WinFormsAppExcavator/MovementStrategy/MoveToBorder.cs b/WinFormsAppExcavator/WinFormsAppExcavator/MovementStrategy/MoveToBorder.cs new file mode 100644 index 0000000..e1f7aa8 --- /dev/null +++ b/WinFormsAppExcavator/WinFormsAppExcavator/MovementStrategy/MoveToBorder.cs @@ -0,0 +1,56 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace WinFormsAppExcavator.MovementStrategy +{ + internal class MoveToBorder : 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; + if (Math.Abs(diffX) > GetStep()) + { + if (diffX > 0) + { + MoveLeft(); + } + else + { + MoveRight(); + } + } + int diffY = objParams.ObjectMiddleVertical - FieldHeight; + if (Math.Abs(diffY) > GetStep()) + { + if (diffY > 0) + { + MoveUp(); + } + else + { + MoveDown(); + } + } + } + } +} diff --git a/WinFormsAppExcavator/WinFormsAppExcavator/MovementStrategy/MoveToCenter.cs b/WinFormsAppExcavator/WinFormsAppExcavator/MovementStrategy/MoveToCenter.cs new file mode 100644 index 0000000..2cc5426 --- /dev/null +++ b/WinFormsAppExcavator/WinFormsAppExcavator/MovementStrategy/MoveToCenter.cs @@ -0,0 +1,48 @@ +namespace WinFormsAppExcavator.MovementStrategy; + +public 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/WinFormsAppExcavator/WinFormsAppExcavator/MovementStrategy/MoveableExcavator.cs b/WinFormsAppExcavator/WinFormsAppExcavator/MovementStrategy/MoveableExcavator.cs new file mode 100644 index 0000000..9cb5a9b --- /dev/null +++ b/WinFormsAppExcavator/WinFormsAppExcavator/MovementStrategy/MoveableExcavator.cs @@ -0,0 +1,69 @@ + + +using WinFormsAppExcavator.Drawings; + +namespace WinFormsAppExcavator.MovementStrategy; +/// +/// Класс-реализация IMoveableObject с использованием DrawningExcavator +/// +public class MoveableExcavator : IMoveableObjectcs +{ + + + + /// + /// Поле-объект класса DrawningCar или его наследника + /// + private readonly DrawingExcavatorEmpty? _excavator = null; + + /// + /// Конструктор + /// + /// Объект класса DrawningCar + public MoveableExcavator(DrawingExcavatorEmpty excavator) + { + _excavator = excavator; + } + + + public ObjectParameters? GetObjectPosition + { + get + { + if (_excavator == null || _excavator.EntityExcavatorEmpty == null || !_excavator.GetPosX.HasValue || !_excavator.GetPosY.HasValue) + { + return null; + } + return new ObjectParameters(_excavator.GetPosX.Value, _excavator.GetPosY.Value, _excavator.GetWidth, _excavator.GetHeight); + } + } + + public int GetStep => (int)(_excavator?.EntityExcavatorEmpty?.Step ?? 0); + + public bool TryMoveObject(MovementDirection direction) + { + if (_excavator == null || _excavator.EntityExcavatorEmpty == null) + { + return false; + } + + return _excavator.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, + }; + } + +} diff --git a/WinFormsAppExcavator/WinFormsAppExcavator/MovementStrategy/MovementDirection.cs b/WinFormsAppExcavator/WinFormsAppExcavator/MovementStrategy/MovementDirection.cs new file mode 100644 index 0000000..4eb10e1 --- /dev/null +++ b/WinFormsAppExcavator/WinFormsAppExcavator/MovementStrategy/MovementDirection.cs @@ -0,0 +1,24 @@ +namespace WinFormsAppExcavator.MovementStrategy; +/// +/// направление перемещения +/// +public enum MovementDirection +{ + /// + /// Вверх + /// + Up = 1, + /// + /// Вниз + /// + Down = 2, + /// + /// Влево + /// + Left = 3, + /// + /// Вправо + /// + Right = 4 + +} diff --git a/WinFormsAppExcavator/WinFormsAppExcavator/MovementStrategy/ObjectParameters.cs b/WinFormsAppExcavator/WinFormsAppExcavator/MovementStrategy/ObjectParameters.cs new file mode 100644 index 0000000..20601c8 --- /dev/null +++ b/WinFormsAppExcavator/WinFormsAppExcavator/MovementStrategy/ObjectParameters.cs @@ -0,0 +1,73 @@ +namespace WinFormsAppExcavator.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; + _y = y; + _width = width; + _height = height; + } + +} diff --git a/WinFormsAppExcavator/WinFormsAppExcavator/MovementStrategy/StrategyStatus.cs b/WinFormsAppExcavator/WinFormsAppExcavator/MovementStrategy/StrategyStatus.cs new file mode 100644 index 0000000..191d357 --- /dev/null +++ b/WinFormsAppExcavator/WinFormsAppExcavator/MovementStrategy/StrategyStatus.cs @@ -0,0 +1,22 @@ + +namespace WinFormsAppExcavator.MovementStrategy; +/// +///статус выполнения операции перемещения +/// +public enum StrategyStatus +{ + /// + /// все готово к началу + /// + NotInit, + + /// + /// выполняется + /// + InProgress, + + /// + /// завершено + /// + Finish +}