diff --git a/ProjectExcavator/ProjectExcavator/Drawnings/DirectionType.cs b/ProjectExcavator/ProjectExcavator/Drawnings/DirectionType.cs index cb7f6cf..adb0851 100644 --- a/ProjectExcavator/ProjectExcavator/Drawnings/DirectionType.cs +++ b/ProjectExcavator/ProjectExcavator/Drawnings/DirectionType.cs @@ -5,6 +5,11 @@ /// public enum DirectionType { + /// + /// Неизвестное направление + /// + Unknow = -1, + /// /// Вверх /// diff --git a/ProjectExcavator/ProjectExcavator/Drawnings/DrawningBulldozer.cs b/ProjectExcavator/ProjectExcavator/Drawnings/DrawningBulldozer.cs index 8005728..65b2d32 100644 --- a/ProjectExcavator/ProjectExcavator/Drawnings/DrawningBulldozer.cs +++ b/ProjectExcavator/ProjectExcavator/Drawnings/DrawningBulldozer.cs @@ -32,12 +32,32 @@ public class DrawningBulldozer /// /// Ширина прорисовки автомобиля /// - private readonly int _drawingExcWidth = 80; //дописать ширина + private readonly int _drawingExcWidth = 65; /// /// Высота прорисовки автомобиля /// - private readonly int _drawingExcHeight = 61; //дописать высота + private readonly int _drawingExcHeight = 61; + + /// + /// Координата X объекта + /// + public int? GetPosX => _startPosX; + + /// + /// Координата Y объекта + /// + public int? GetPosY => _startPosY; + + /// + /// Ширина объекта + /// + public int GetWidth => _drawingExcWidth; + + /// + /// Высота объекта + /// + public int GetHeight => _drawingExcHeight; /// /// Пустой конструктор @@ -69,7 +89,7 @@ public class DrawningBulldozer protected DrawningBulldozer(int drawingExcWidth, int drawingExcHeight) : this() { _drawingExcWidth = drawingExcWidth; - _pictureHeight = drawingExcHeight; + _drawingExcHeight = drawingExcHeight; } /// diff --git a/ProjectExcavator/ProjectExcavator/Drawnings/DrawningExcavator.cs b/ProjectExcavator/ProjectExcavator/Drawnings/DrawningExcavator.cs index 78fe878..b3f3e6a 100644 --- a/ProjectExcavator/ProjectExcavator/Drawnings/DrawningExcavator.cs +++ b/ProjectExcavator/ProjectExcavator/Drawnings/DrawningExcavator.cs @@ -1,5 +1,4 @@ using ProjectExcavator.Entities; -using System.Net.Sockets; namespace ProjectExcavator.Drawnings; @@ -20,7 +19,7 @@ public class DrawningExcavator : DrawningBulldozer public DrawningExcavator(int speed, double weight, Color bodyColor, Color additionalColor, bool prop, bool ladle) : base(120, 70) { EntityBulldozer = new EntityExcavator(speed, weight, bodyColor, additionalColor, prop, ladle); - } //ДОПИСАТЬ!!!!!!!!!!!!!!!!!!!!!!!!! + } public override void DrawTransport(Graphics g) { diff --git a/ProjectExcavator/ProjectExcavator/FormExcavator.Designer.cs b/ProjectExcavator/ProjectExcavator/FormExcavator.Designer.cs index e614414..bf47ad7 100644 --- a/ProjectExcavator/ProjectExcavator/FormExcavator.Designer.cs +++ b/ProjectExcavator/ProjectExcavator/FormExcavator.Designer.cs @@ -35,6 +35,8 @@ buttonDown = new Button(); buttonUp = new Button(); buttonCreateBulldozer = new Button(); + comboBoxStrategy = new ComboBox(); + buttonStrategyStep = new Button(); ((System.ComponentModel.ISupportInitialize)pictureBoxExcavator).BeginInit(); SuspendLayout(); // @@ -118,11 +120,33 @@ buttonCreateBulldozer.UseVisualStyleBackColor = false; buttonCreateBulldozer.Click += ButtonCreateBulldozer_Click; // + // comboBoxStrategy + // + comboBoxStrategy.DropDownStyle = ComboBoxStyle.DropDownList; + comboBoxStrategy.FormattingEnabled = true; + comboBoxStrategy.Items.AddRange(new object[] { "К центру", "К краю" }); + comboBoxStrategy.Location = new Point(798, 12); + comboBoxStrategy.Name = "comboBoxStrategy"; + comboBoxStrategy.Size = new Size(121, 23); + comboBoxStrategy.TabIndex = 7; + // + // buttonStrategyStep + // + buttonStrategyStep.Location = new Point(844, 41); + buttonStrategyStep.Name = "buttonStrategyStep"; + buttonStrategyStep.Size = new Size(75, 23); + buttonStrategyStep.TabIndex = 8; + buttonStrategyStep.Text = "Шаг"; + buttonStrategyStep.UseVisualStyleBackColor = true; + buttonStrategyStep.Click += ButtonStrategyStep_Click; + // // FormExcavator // AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(931, 604); + Controls.Add(buttonStrategyStep); + Controls.Add(comboBoxStrategy); Controls.Add(buttonCreateBulldozer); Controls.Add(buttonUp); Controls.Add(buttonDown); @@ -145,5 +169,7 @@ private Button buttonDown; private Button buttonUp; private Button buttonCreateBulldozer; + private ComboBox comboBoxStrategy; + private Button buttonStrategyStep; } } \ No newline at end of file diff --git a/ProjectExcavator/ProjectExcavator/FormExcavator.cs b/ProjectExcavator/ProjectExcavator/FormExcavator.cs index 6403319..1b9b10f 100644 --- a/ProjectExcavator/ProjectExcavator/FormExcavator.cs +++ b/ProjectExcavator/ProjectExcavator/FormExcavator.cs @@ -1,15 +1,32 @@ using ProjectExcavator.Drawnings; +using ProjectExcavator.MovementStrategy; namespace ProjectExcavator; public partial class FormExcavator : Form { + /// + /// Поле-объект для прорисовки объекта + /// private DrawningBulldozer? _drawningBulldozer; + + /// + /// Стратегия перемещения + /// + private AbstractStrategy? _strategy; + + /// + /// Конструктор формы + /// public FormExcavator() { InitializeComponent(); + _strategy = null; } + /// + /// Метод прорисовки Бульдозера + /// private void Draw() { if (_drawningBulldozer == null) @@ -23,6 +40,10 @@ public partial class FormExcavator : Form pictureBoxExcavator.Image = bmp; } + /// + /// Создание объекта класса-перемещения + /// + /// Тип создаваемого объекта private void CreateObject(string type) { Random random = new(); @@ -44,6 +65,8 @@ public partial class FormExcavator : Form _drawningBulldozer.SetPictureSize(pictureBoxExcavator.Width, pictureBoxExcavator.Height); _drawningBulldozer.SetPosition(random.Next(10, 100), random.Next(10, 100), pictureBoxExcavator.Width, pictureBoxExcavator.Height); + _strategy = null; + comboBoxStrategy.Enabled = true; Draw(); } @@ -90,4 +113,46 @@ public partial class FormExcavator : Form } } + /// + /// Обработка нажатия кнопки "Шаг" + /// + /// + /// + private void ButtonStrategyStep_Click(object sender, EventArgs e) + { + if (_drawningBulldozer == null) + { + return; + } + + if (comboBoxStrategy.Enabled) + { + _strategy = comboBoxStrategy.SelectedIndex switch + { + 0 => new MoveToCenter(), + 1 => new MoveToBorder(), + _ => null, + }; + if (_strategy == null) + { + return; + } + _strategy.SetData(new MoveableBulldozer(_drawningBulldozer), 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/ProjectExcavator/ProjectExcavator/MovementStrategy/AbstractStrategy.cs b/ProjectExcavator/ProjectExcavator/MovementStrategy/AbstractStrategy.cs new file mode 100644 index 0000000..ccb5cc3 --- /dev/null +++ b/ProjectExcavator/ProjectExcavator/MovementStrategy/AbstractStrategy.cs @@ -0,0 +1,137 @@ +namespace ProjectExcavator.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 (IsTargetDestination()) + { + _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 IsTargetDestination(); + + + private bool MoveTo(MovementDirection movementDirection) + { + if (_state != StrategyStatus.InProgress) + { + return false; + } + + return _moveableObject?.TryMoveObject(movementDirection) ?? false; + } +} \ No newline at end of file diff --git a/ProjectExcavator/ProjectExcavator/MovementStrategy/IMoveableObject.cs b/ProjectExcavator/ProjectExcavator/MovementStrategy/IMoveableObject.cs new file mode 100644 index 0000000..a96bd5d --- /dev/null +++ b/ProjectExcavator/ProjectExcavator/MovementStrategy/IMoveableObject.cs @@ -0,0 +1,21 @@ +namespace ProjectExcavator.MovementStrategy; + +public interface IMoveableObject +{ + /// + /// Получение координаты объекта + /// + ObjectParameters? GetObjectPosition { get; } + + /// + /// Шаг объекта + /// + int GetStep { get; } + + /// + /// Попытка переместить объект в указанном направлении + /// + /// Направление + /// true - объект перемещен, false - перемещение невозможно + bool TryMoveObject(MovementDirection direction); +} diff --git a/ProjectExcavator/ProjectExcavator/MovementStrategy/MoveToBorder.cs b/ProjectExcavator/ProjectExcavator/MovementStrategy/MoveToBorder.cs new file mode 100644 index 0000000..e0b170d --- /dev/null +++ b/ProjectExcavator/ProjectExcavator/MovementStrategy/MoveToBorder.cs @@ -0,0 +1,51 @@ +namespace ProjectExcavator.MovementStrategy; + +public class MoveToBorder : AbstractStrategy +{ + protected override bool IsTargetDestination() + { + 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(); + } + } + } +} diff --git a/ProjectExcavator/ProjectExcavator/MovementStrategy/MoveToCenter.cs b/ProjectExcavator/ProjectExcavator/MovementStrategy/MoveToCenter.cs new file mode 100644 index 0000000..ce47061 --- /dev/null +++ b/ProjectExcavator/ProjectExcavator/MovementStrategy/MoveToCenter.cs @@ -0,0 +1,54 @@ +namespace ProjectExcavator.MovementStrategy; + +/// +/// Стратегия перемещения объекта в центр экрана +/// +public class MoveToCenter : AbstractStrategy +{ + protected override bool IsTargetDestination() + { + 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/ProjectExcavator/ProjectExcavator/MovementStrategy/MoveableBulldozer.cs b/ProjectExcavator/ProjectExcavator/MovementStrategy/MoveableBulldozer.cs new file mode 100644 index 0000000..ed9e4d2 --- /dev/null +++ b/ProjectExcavator/ProjectExcavator/MovementStrategy/MoveableBulldozer.cs @@ -0,0 +1,64 @@ +using ProjectExcavator.Drawnings; + +namespace ProjectExcavator.MovementStrategy; + +/// +/// Класс-реализация IMoveableObject с использованием DrawningBulldozer +/// +public class MoveableBulldozer : IMoveableObject +{ + /// + /// Поле-объект класса DrawningBulldozer или его наследника + /// + private readonly DrawningBulldozer? _bulldozer = null; + + /// + /// Конструктор + /// + /// Объект класса DrawningBulldozer + public MoveableBulldozer(DrawningBulldozer bulldozer) + { + _bulldozer = bulldozer; + } + + public ObjectParameters? GetObjectPosition + { + get + { + if (_bulldozer == null || _bulldozer.EntityBulldozer == null || !_bulldozer.GetPosX.HasValue || !_bulldozer.GetPosY.HasValue) + { + return null; + } + return new ObjectParameters(_bulldozer.GetPosX.Value, _bulldozer.GetPosY.Value, _bulldozer.GetWidth, _bulldozer.GetHeight); + } + } + + public int GetStep => (int)(_bulldozer?.EntityBulldozer?.Step ?? 0); + + public bool TryMoveObject(MovementDirection direction) + { + if (_bulldozer == null || _bulldozer.EntityBulldozer == null) + { + return false; + } + + return _bulldozer.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, + }; + } +} \ No newline at end of file diff --git a/ProjectExcavator/ProjectExcavator/MovementStrategy/MovementDirection.cs b/ProjectExcavator/ProjectExcavator/MovementStrategy/MovementDirection.cs new file mode 100644 index 0000000..f6651a1 --- /dev/null +++ b/ProjectExcavator/ProjectExcavator/MovementStrategy/MovementDirection.cs @@ -0,0 +1,27 @@ +namespace ProjectExcavator.MovementStrategy; + +/// +/// Направление перемещения +/// +public enum MovementDirection +{ + /// + /// Вверх + /// + Up = 1, + + /// + /// Вниз + /// + Down = 2, + + /// + /// Влево + /// + Left = 3, + + /// + /// Вправо + /// + Right = 4 +} \ No newline at end of file diff --git a/ProjectExcavator/ProjectExcavator/MovementStrategy/ObjectParameters.cs b/ProjectExcavator/ProjectExcavator/MovementStrategy/ObjectParameters.cs new file mode 100644 index 0000000..c0c0b50 --- /dev/null +++ b/ProjectExcavator/ProjectExcavator/MovementStrategy/ObjectParameters.cs @@ -0,0 +1,72 @@ +namespace ProjectExcavator.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; + } +} \ No newline at end of file diff --git a/ProjectExcavator/ProjectExcavator/MovementStrategy/StrategyStatus.cs b/ProjectExcavator/ProjectExcavator/MovementStrategy/StrategyStatus.cs new file mode 100644 index 0000000..eb706e9 --- /dev/null +++ b/ProjectExcavator/ProjectExcavator/MovementStrategy/StrategyStatus.cs @@ -0,0 +1,22 @@ +namespace ProjectExcavator.MovementStrategy; + +/// +/// Статус выполнения операции перемещения +/// +public enum StrategyStatus +{ + /// + /// Все готово к началу + /// + NotInit, + + /// + /// Выполняется + /// + InProgress, + + /// + /// Завершено + /// + Finish +} \ No newline at end of file