From 963213940d79e27e6371223614014013fd5cc11a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=92=D0=BB=D0=B0=D0=B4=D0=B8=D0=BC=D0=B8=D1=80=20=D0=94?= =?UTF-8?q?=D0=B0=D0=BD=D0=B8=D0=BB=D0=BE=D0=B2?= Date: Sat, 28 Oct 2023 13:29:28 +0400 Subject: [PATCH] LabWork02 --- .../Project_DumpTruck/AbstractStrategy.cs | 131 ++++++++++ .../Project_DumpTruck/DrawningDumpTruck.cs | 179 ++------------ .../Project_DumpTruck/DrawningObjectTruck.cs | 33 +++ .../Project_DumpTruck/DrawningTruck.cs | 223 ++++++++++++++++++ .../Project_DumpTruck/EntityDumpTruck.cs | 33 +-- .../Project_DumpTruck/EntityTruck.cs | 44 ++++ .../FormDumpTruck.Designer.cs | 68 +++++- .../Project_DumpTruck/FormDumpTruck.cs | 89 +++++-- .../Project_DumpTruck/IMoveableObject.cs | 31 +++ .../Project_DumpTruck/MoveToBorder.cs | 59 +++++ .../Project_DumpTruck/MoveToCenter.cs | 54 +++++ .../Project_DumpTruck/ObjectParameters.cs | 55 +++++ Project_DumpTruck/Project_DumpTruck/Status.cs | 17 ++ 13 files changed, 799 insertions(+), 217 deletions(-) create mode 100644 Project_DumpTruck/Project_DumpTruck/AbstractStrategy.cs create mode 100644 Project_DumpTruck/Project_DumpTruck/DrawningObjectTruck.cs create mode 100644 Project_DumpTruck/Project_DumpTruck/DrawningTruck.cs create mode 100644 Project_DumpTruck/Project_DumpTruck/EntityTruck.cs create mode 100644 Project_DumpTruck/Project_DumpTruck/IMoveableObject.cs create mode 100644 Project_DumpTruck/Project_DumpTruck/MoveToBorder.cs create mode 100644 Project_DumpTruck/Project_DumpTruck/MoveToCenter.cs create mode 100644 Project_DumpTruck/Project_DumpTruck/ObjectParameters.cs create mode 100644 Project_DumpTruck/Project_DumpTruck/Status.cs diff --git a/Project_DumpTruck/Project_DumpTruck/AbstractStrategy.cs b/Project_DumpTruck/Project_DumpTruck/AbstractStrategy.cs new file mode 100644 index 0000000..91773ad --- /dev/null +++ b/Project_DumpTruck/Project_DumpTruck/AbstractStrategy.cs @@ -0,0 +1,131 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Project_DumpTruck.MovementStrategy +{ + public abstract class AbstractStrategy + { + /// + /// Перемещаемый объект + /// + private IMoveableObject? _moveableObject; + /// + /// Статус перемещения + /// + private Status _state = Status.NotInit; + /// + /// Ширина поля + /// + protected int FieldWidth { get; private set; } + /// + /// Высота поля + /// + protected int FieldHeight { get; private set; } + /// + /// Статус перемещения + /// + public Status GetStatus() { return _state; } + /// + /// Установка данных + /// + /// Перемещаемый объект + /// Ширина поля + /// Высота поля + public void SetData(IMoveableObject moveableObject, int width, int height) + { + if (moveableObject == null) + { + _state = Status.NotInit; + return; + } + _state = Status.InProgress; + _moveableObject = moveableObject; + FieldWidth = width; + FieldHeight = height; + } + + /// + /// Шаг перемещения + /// + public void MakeStep() + { + if (_state != Status.InProgress) + { + return; + } + if (IsTargetDestinaion()) + { + _state = Status.Finish; + return; + } + MoveToTarget(); + } + /// + /// Перемещение влево + /// + /// Результат перемещения (true - удалось переместиться, false - неудача) + protected bool MoveLeft() => MoveTo(DirectionType.Left); + /// + /// Перемещение вправо + /// + /// Результат перемещения (true - удалось переместиться, false - неудача) + protected bool MoveRight() => MoveTo(DirectionType.Right); + /// + /// Перемещение вверх + /// + /// Результат перемещения (true - удалось переместиться, false - неудача) + protected bool MoveUp() => MoveTo(DirectionType.Up); + /// + /// Перемещение вниз + /// + /// Результат перемещения (true - удалось переместиться, false - неудача) + protected bool MoveDown() => MoveTo(DirectionType.Down); + /// + /// Параметры объекта + /// + protected ObjectParameters? GetObjectParameters => _moveableObject?.GetObjectPosition; + /// + /// Шаг объекта + /// + /// + protected int? GetStep() + { + if (_state != Status.InProgress) + { + return null; + } + return _moveableObject?.GetStep; + } + + /// + /// Перемещение к цели + /// + protected abstract void MoveToTarget(); + /// + /// Достигнута ли цель + /// + /// + protected abstract bool IsTargetDestinaion(); + /// + /// Попытка перемещения в требуемом направлении + /// + /// Направление + /// Результат попытки (true - удалось переместиться, false - неудача) + private bool MoveTo(DirectionType directionType) + { + if (_state != Status.InProgress) + { + return false; + } + if (_moveableObject?.CheckCanMove(directionType) ?? false) + { + _moveableObject.MoveObject(directionType); + return true; + } + return false; + } + } +} diff --git a/Project_DumpTruck/Project_DumpTruck/DrawningDumpTruck.cs b/Project_DumpTruck/Project_DumpTruck/DrawningDumpTruck.cs index 1e1e3ab..5c38d7b 100644 --- a/Project_DumpTruck/Project_DumpTruck/DrawningDumpTruck.cs +++ b/Project_DumpTruck/Project_DumpTruck/DrawningDumpTruck.cs @@ -3,195 +3,62 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using Project_DumpTruck.Entities; -namespace Project_DumpTruck +namespace Project_DumpTruck.DrawningObjects { - internal class DrawningDumpTruck + public class DrawningDumpTruck: DrawningTruck { /// - /// Класс-сущность - /// - public EntityDumpTruck? EntityDumpTruck { get; private set; } - /// - /// Левая координата отрисовки автомобиля - /// - private float _startPosX; - /// - /// Верхняя кооридната отрисовки автомобиля - /// - private float _startPosY; - /// - /// Ширина окна отрисовки - /// - private int? _pictureWidth; - /// - /// Высота окна отрисовки - /// - private int? _pictureHeight; - /// - /// Ширина отрисовки автомобиля - /// - private readonly int _dumptruckWidth = 110; - /// - /// Высота отрисовки автомобиля - /// - private readonly int _dumptruckHeight = 60; - - /// - /// Инициализация свойств + /// Конструктор /// /// Скорость - /// Вес автомобиля - /// Цвет кузова - /// + /// Вес + /// Основной цвет + /// Дополнительный цвет /// Ширина картинки /// Высота картинки - public bool Init(int speed, float weight, Color bodyColor, Color additionalColor, bool body, bool tent, int width, int height) + /// Признак наличия груза + /// Признак наличия тента + public DrawningDumpTruck(int speed, double weight, Color bodyColor, Color additionalColor, bool bodyKit, bool tent, int width, int height) : + base(speed, weight, bodyColor, width, height, 110, 60) { - // TODO: Продумать проверки - if (width < _dumptruckWidth || height < _dumptruckHeight) - return false; - - _pictureWidth = width; - _pictureHeight = height; - - EntityDumpTruck = new EntityDumpTruck(); - EntityDumpTruck.Init(speed, weight, bodyColor, additionalColor, body, tent); - return true; - } - /// - /// Установка позиции автомобиля - /// - /// Координата X - /// Координата Y - /// Ширина картинки - /// Высота картинки - public void SetPosition(int x, int y) - { - if (x < 0 || y < 0 || x > _pictureWidth || y > _pictureHeight) + if (EntityTruck != null) { - _startPosX = 0; - _startPosY = 0; - } - - _startPosX = x; - _startPosY = y; - } - /// - /// Изменение направления пермещения - /// - /// Направление - public void MoveTransport(DirectionType direction) - { - if (EntityDumpTruck == null) - { - return; - } - switch (direction) - { - // вправо - case DirectionType.Right: - if (_startPosX + _dumptruckWidth + EntityDumpTruck.Step < _pictureWidth) - { - _startPosX += (int)EntityDumpTruck.Step; - } - break; - //влево - case DirectionType.Left: - if (_startPosX - EntityDumpTruck.Step > 0) - { - _startPosX -= (int)EntityDumpTruck.Step; - } - break; - //вверх - case DirectionType.Up: - if (_startPosY - EntityDumpTruck.Step > 0) - { - _startPosY -= (int)EntityDumpTruck.Step; - } - break; - //вниз - case DirectionType.Down: - if (_startPosY + _dumptruckHeight + EntityDumpTruck.Step < _pictureHeight) - { - _startPosY += (int)EntityDumpTruck.Step; - } - break; + EntityTruck = new EntityDumpTruck(speed, weight, bodyColor, additionalColor, bodyKit, tent); } } + /// /// Отрисовка автомобиля /// /// - public void DrawTransport(Graphics g) + public override void DrawTransport(Graphics g) { - if (EntityDumpTruck == null) + if (EntityTruck is not EntityDumpTruck dumpTruck) { return; } - Pen penBlack = new Pen(Color.Black); - Brush brushBodyColor = new SolidBrush(EntityDumpTruck.BodyColor); - Brush brushAdditionalColor = new SolidBrush(EntityDumpTruck.AdditionalColor); - Brush brushBlack = new SolidBrush(Color.Black); + Brush brushAdditionalColor = new SolidBrush(dumpTruck.AdditionalColor); Brush brushWhite = new SolidBrush(Color.White); + Pen penBlack = new Pen(Color.Black); - //Кабина - g.FillRectangle(brushBodyColor, _startPosX + 80, _startPosY, 20, 30); - g.DrawRectangle(penBlack, _startPosX + 80, _startPosY, 20, 30); + base.DrawTransport(g); - //Рама - g.FillRectangle(brushBodyColor, _startPosX, _startPosY + 30, 100, 5); - g.DrawRectangle(penBlack, _startPosX, _startPosY + 30, 100, 5); - - //Колёса - g.FillEllipse(brushBlack, _startPosX, _startPosY + 35, 20, 20); - g.FillEllipse(brushBlack, _startPosX + 22, _startPosY + 35, 20, 20); - g.FillEllipse(brushBlack, _startPosX + 80, _startPosY + 35, 20, 20); - - g.FillEllipse(brushWhite, _startPosX + 5, _startPosY + 40, 10, 10); - g.FillEllipse(brushWhite, _startPosX + 27, _startPosY + 40, 10, 10); - g.FillEllipse(brushWhite, _startPosX + 85, _startPosY + 40, 10, 10); - - g.DrawEllipse(penBlack, _startPosX, _startPosY + 35, 20, 20); - g.DrawEllipse(penBlack, _startPosX + 22, _startPosY + 35, 20, 20); - g.DrawEllipse(penBlack, _startPosX + 80, _startPosY + 35, 20, 20); - - if (EntityDumpTruck.BodyKit) + if (dumpTruck.BodyKit) { + g.FillRectangle(brushAdditionalColor, _startPosX, _startPosY + 30, 100, 5); g.FillRectangle(brushAdditionalColor, _startPosX, _startPosY + 10, 70, 20); + g.DrawRectangle(penBlack, _startPosX, _startPosY + 30, 100, 5); g.DrawRectangle(penBlack, _startPosX, _startPosY + 10, 70, 20); - if (EntityDumpTruck.Tent) + if (dumpTruck.Tent) { g.FillRectangle(brushWhite, _startPosX, _startPosY + 10, 70, 5); g.DrawRectangle(penBlack, _startPosX, _startPosY + 10, 70, 5); } } } - /// - /// Смена границ формы отрисовки - /// - /// Ширина картинки - /// Высота картинки - public void ChangeBorders(int width, int height) - { - _pictureWidth = width; - _pictureHeight = height; - if (_pictureWidth <= _dumptruckWidth || _pictureHeight <= _dumptruckHeight) - { - _pictureWidth = null; - _pictureHeight = null; - return; - } - if (_startPosX + _dumptruckWidth > _pictureWidth) - { - _startPosX = _pictureWidth.Value - _dumptruckWidth; - } - if (_startPosY + _dumptruckHeight > _pictureHeight) - { - _startPosY = _pictureHeight.Value - _dumptruckHeight; - } - } } } diff --git a/Project_DumpTruck/Project_DumpTruck/DrawningObjectTruck.cs b/Project_DumpTruck/Project_DumpTruck/DrawningObjectTruck.cs new file mode 100644 index 0000000..ae77ee4 --- /dev/null +++ b/Project_DumpTruck/Project_DumpTruck/DrawningObjectTruck.cs @@ -0,0 +1,33 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Project_DumpTruck.DrawningObjects; + +namespace Project_DumpTruck.MovementStrategy +{ + public class DrawningObjectTruck : IMoveableObject + { + private readonly DrawningTruck? _drawningTruck = null; + public DrawningObjectTruck(DrawningTruck drawningTruck) + { + _drawningTruck = drawningTruck; + } + public ObjectParameters? GetObjectPosition + { + get + { + if (_drawningTruck == null || _drawningTruck.EntityTruck == null) + { + return null; + } + return new ObjectParameters(_drawningTruck.GetPosX, _drawningTruck.GetPosY, _drawningTruck.GetWidth, _drawningTruck.GetHeight); + } + } + public int GetStep => (int)(_drawningTruck?.EntityTruck?.Step ?? 0); + + public bool CheckCanMove(DirectionType direction) => _drawningTruck?.CanMove(direction) ?? false; + public void MoveObject(DirectionType direction) => _drawningTruck?.MoveTransport(direction); + } +} diff --git a/Project_DumpTruck/Project_DumpTruck/DrawningTruck.cs b/Project_DumpTruck/Project_DumpTruck/DrawningTruck.cs new file mode 100644 index 0000000..4c86835 --- /dev/null +++ b/Project_DumpTruck/Project_DumpTruck/DrawningTruck.cs @@ -0,0 +1,223 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Project_DumpTruck.Entities; + +namespace Project_DumpTruck.DrawningObjects +{ + public class DrawningTruck + { + /// + /// Класс-сущность + /// + public EntityTruck? EntityTruck { get; protected set; } + /// + /// Ширина окна + /// + private int _pictureWidth; + /// + /// Высота окна + /// + private int _pictureHeight; + /// + /// Левая координата прорисовки автомобиля + /// + protected int _startPosX; + /// + /// Верхняя кооридната прорисовки автомобиля + /// + protected int _startPosY; + /// + /// Ширина прорисовки автомобиля + /// + protected readonly int _truckWidth = 110; + /// + /// Высота прорисовки автомобиля + /// + protected readonly int _truckHeight = 60; + /// + /// + /// Координата X объекта + /// + public int GetPosX => _startPosX; + /// + /// Координата Y объекта + /// + public int GetPosY => _startPosY; + /// + /// Ширина объекта + /// + public int GetWidth => _truckWidth; + /// + /// Высота объекта + /// + public int GetHeight => _truckHeight; + + /// Конструктор + /// + /// Скорость + /// Вес + /// Основной цвет + /// Ширина картинки + /// Высота картинки + public DrawningTruck(int speed, double weight, Color bodyColor, int width, int height) + { + if (width < _truckWidth || height < _truckHeight) + { + return; + } + _pictureWidth = width; + _pictureHeight = height; + EntityTruck = new EntityTruck(speed, weight, bodyColor); + } + + /// + /// Конструктор + /// + /// Скорость + /// Вес + /// Основной цвет + /// Ширина картинки + /// Высота картинки + /// Ширина прорисовки автомобиля + /// Высота прорисовки автомобиля + protected DrawningTruck(int speed, double weight, Color bodyColor, int width, int height, int truckWidth, int truckHeight) + { + if (width <= _truckWidth || height <= _truckHeight) + { + return; + } + _pictureWidth = width; + _pictureHeight = height; + _truckWidth = truckWidth; + _truckHeight = truckHeight; + EntityTruck = new EntityTruck(speed, weight, bodyColor); + } + + /// + /// Проверка, что объект может переместится по указанному направлению + /// + /// Направление + /// true - можно переместится по указанному направлению + public bool CanMove(DirectionType direction) + { + if (EntityTruck == null) + { + return false; + } + return direction switch + { + //влево + DirectionType.Left => _startPosX - EntityTruck.Step > 0, + //вверх + DirectionType.Up => _startPosY - EntityTruck.Step > 0, + // вправо + DirectionType.Right => _startPosX + _truckWidth + EntityTruck.Step < _pictureWidth, + //вниз + DirectionType.Down => _startPosY + _truckHeight + EntityTruck.Step < _pictureHeight, + _ => false, + }; + } + + /// + /// Установка позиции + /// + /// Координата X + /// Координата Y + public void SetPosition(int x, int y) + { + if (x < 0 || x + _truckWidth > _pictureWidth) + { + x = Math.Max(0, _pictureWidth - _truckWidth); + } + if (y < 0 || y + _truckHeight > _pictureHeight) + { + y = Math.Max(0, _pictureHeight - _truckHeight); + } + _startPosX = x; + _startPosY = y; + } + + /// + /// Изменение направления перемещения + /// + /// Направление + public void MoveTransport(DirectionType direction) + { + if (!CanMove(direction) || EntityTruck == null) + { + return; + } + switch (direction) + { + //влево + case DirectionType.Left: + if (_startPosX - EntityTruck.Step > 0) + { + _startPosX -= (int)EntityTruck.Step; + } + break; + //вверх + case DirectionType.Up: + if (_startPosY - EntityTruck.Step > 0) + { + _startPosY -= (int)EntityTruck.Step; + } + break; + // вправо + case DirectionType.Right: + if (_startPosX + _truckWidth + EntityTruck.Step < _pictureWidth) + { + _startPosX += (int)EntityTruck.Step; + } + break; + //вниз + case DirectionType.Down: + if (_startPosY + _truckHeight + EntityTruck.Step < _pictureHeight) + { + _startPosY += (int)EntityTruck.Step; + } + break; + } + } + + /// + /// Прорисовка объекта + /// + /// + public virtual void DrawTransport(Graphics g) + { + if (EntityTruck == null) + { + return; + } + Pen penBlack = new Pen(Color.Black); + Brush brushBodyColor = new SolidBrush(EntityTruck.BodyColor); + Brush brushBlack = new SolidBrush(Color.Black); + Brush brushWhite = new SolidBrush(Color.White); + + //Кабина + g.FillRectangle(brushBodyColor, _startPosX + 80, _startPosY, 20, 30); + g.DrawRectangle(penBlack, _startPosX + 80, _startPosY, 20, 30); + + //Рама + g.FillRectangle(brushBodyColor, _startPosX, _startPosY + 30, 100, 5); + g.DrawRectangle(penBlack, _startPosX, _startPosY + 30, 100, 5); + + //Колёса + g.FillEllipse(brushBlack, _startPosX, _startPosY + 35, 20, 20); + g.FillEllipse(brushBlack, _startPosX + 22, _startPosY + 35, 20, 20); + g.FillEllipse(brushBlack, _startPosX + 80, _startPosY + 35, 20, 20); + + g.FillEllipse(brushWhite, _startPosX + 5, _startPosY + 40, 10, 10); + g.FillEllipse(brushWhite, _startPosX + 27, _startPosY + 40, 10, 10); + g.FillEllipse(brushWhite, _startPosX + 85, _startPosY + 40, 10, 10); + + g.DrawEllipse(penBlack, _startPosX, _startPosY + 35, 20, 20); + g.DrawEllipse(penBlack, _startPosX + 22, _startPosY + 35, 20, 20); + g.DrawEllipse(penBlack, _startPosX + 80, _startPosY + 35, 20, 20); + } + } +} diff --git a/Project_DumpTruck/Project_DumpTruck/EntityDumpTruck.cs b/Project_DumpTruck/Project_DumpTruck/EntityDumpTruck.cs index a6b95c3..0c223e8 100644 --- a/Project_DumpTruck/Project_DumpTruck/EntityDumpTruck.cs +++ b/Project_DumpTruck/Project_DumpTruck/EntityDumpTruck.cs @@ -1,28 +1,14 @@ -using System; +using Project_DumpTruck.Entities; +using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; -namespace Project_DumpTruck +namespace Project_DumpTruck.Entities { - internal class EntityDumpTruck + internal class EntityDumpTruck: EntityTruck { - /// - /// Скорость - /// - public int Speed { get; private set; } - - /// - /// Вес - /// - public double Weight { get; private set; } - - /// - /// Цвет кузова - /// - public Color BodyColor { get; private set; } - /// /// Дополнительный цвет (для опциональных элементов) /// @@ -38,11 +24,6 @@ namespace Project_DumpTruck /// public bool Tent { get; private set; } - /// - /// Шаг перемещения автомобиля - /// - public double Step => (double)Speed * 100 / Weight; - /// /// Инициализация полей объекта-класса автомобиля /// @@ -51,11 +32,9 @@ namespace Project_DumpTruck /// /// /// - public void Init(int speed, float weight, Color bodyColor, Color additionalColor, bool bodyKit, bool tent) + public EntityDumpTruck(int speed, double weight, Color bodyColor, Color additionalColor, bool bodyKit, bool tent) : + base(speed, weight, bodyColor) { - Speed = speed; - Weight = weight; - BodyColor = bodyColor; AdditionalColor = additionalColor; BodyKit = bodyKit; Tent = tent; diff --git a/Project_DumpTruck/Project_DumpTruck/EntityTruck.cs b/Project_DumpTruck/Project_DumpTruck/EntityTruck.cs new file mode 100644 index 0000000..582ca5a --- /dev/null +++ b/Project_DumpTruck/Project_DumpTruck/EntityTruck.cs @@ -0,0 +1,44 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Project_DumpTruck.Entities +{ + public class EntityTruck + { + /// + /// Скорость + /// + public int Speed { get; private set; } + + /// + /// Вес + /// + public double Weight { get; private set; } + + /// + /// Основной цвет + /// + public Color BodyColor { get; private set; } + + /// + /// Шаг перемещения автомобиля + /// + public double Step => (double)Speed * 100 / Weight; + + /// + /// Конструктор с параметрами + /// + /// Скорость + /// Вес автомобиля + /// Основной цвет + public EntityTruck(int speed, double weight, Color bodyColor) + { + Speed = speed; + Weight = weight; + BodyColor = bodyColor; + } + } +} diff --git a/Project_DumpTruck/Project_DumpTruck/FormDumpTruck.Designer.cs b/Project_DumpTruck/Project_DumpTruck/FormDumpTruck.Designer.cs index d615664..0f87456 100644 --- a/Project_DumpTruck/Project_DumpTruck/FormDumpTruck.Designer.cs +++ b/Project_DumpTruck/Project_DumpTruck/FormDumpTruck.Designer.cs @@ -29,11 +29,14 @@ private void InitializeComponent() { pictureBoxDumpTruck = new PictureBox(); - buttonCreate = new Button(); + buttonCreateTruck = new Button(); buttonLeft = new Button(); buttonUp = new Button(); buttonDown = new Button(); buttonRight = new Button(); + buttonCreateDumpTruck = new Button(); + comboBoxStrategy = new ComboBox(); + buttonStep = new Button(); ((System.ComponentModel.ISupportInitialize)pictureBoxDumpTruck).BeginInit(); SuspendLayout(); // @@ -47,16 +50,16 @@ pictureBoxDumpTruck.TabIndex = 0; pictureBoxDumpTruck.TabStop = false; // - // buttonCreate + // buttonCreateTruck // - buttonCreate.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; - buttonCreate.Location = new Point(12, 426); - buttonCreate.Name = "buttonCreate"; - buttonCreate.Size = new Size(75, 23); - buttonCreate.TabIndex = 1; - buttonCreate.Text = "Создать"; - buttonCreate.UseVisualStyleBackColor = true; - buttonCreate.Click += buttonCreate_Click; + buttonCreateTruck.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; + buttonCreateTruck.Location = new Point(12, 406); + buttonCreateTruck.Name = "buttonCreateTruck"; + buttonCreateTruck.Size = new Size(107, 43); + buttonCreateTruck.TabIndex = 1; + buttonCreateTruck.Text = "Создать грузовик"; + buttonCreateTruck.UseVisualStyleBackColor = true; + buttonCreateTruck.Click += buttonCreateTruck_Click; // // buttonLeft // @@ -106,20 +109,56 @@ buttonRight.UseVisualStyleBackColor = true; buttonRight.Click += buttonMove_Click; // + // buttonCreateDumpTruck + // + buttonCreateDumpTruck.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; + buttonCreateDumpTruck.Location = new Point(125, 406); + buttonCreateDumpTruck.Name = "buttonCreateDumpTruck"; + buttonCreateDumpTruck.Size = new Size(118, 43); + buttonCreateDumpTruck.TabIndex = 6; + buttonCreateDumpTruck.Text = "Создать грузовик с кузовом"; + buttonCreateDumpTruck.UseVisualStyleBackColor = true; + buttonCreateDumpTruck.Click += buttonCreateDumpTruck_Click; + // + // comboBoxStrategy + // + comboBoxStrategy.Anchor = AnchorStyles.Top | AnchorStyles.Right; + comboBoxStrategy.DropDownStyle = ComboBoxStyle.DropDownList; + comboBoxStrategy.FormattingEnabled = true; + comboBoxStrategy.Items.AddRange(new object[] { "Путь к центру", "Путь к правому нижнему углу" }); + comboBoxStrategy.Location = new Point(751, 12); + comboBoxStrategy.Name = "comboBoxStrategy"; + comboBoxStrategy.Size = new Size(121, 23); + comboBoxStrategy.TabIndex = 7; + // + // buttonStep + // + buttonStep.Anchor = AnchorStyles.Top | AnchorStyles.Right; + buttonStep.Location = new Point(797, 41); + buttonStep.Name = "buttonStep"; + buttonStep.Size = new Size(75, 23); + buttonStep.TabIndex = 8; + buttonStep.Text = "Шаг"; + buttonStep.UseVisualStyleBackColor = true; + buttonStep.Click += buttonStep_Click; + // // FormDumpTruck // AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(884, 461); + Controls.Add(buttonStep); + Controls.Add(comboBoxStrategy); + Controls.Add(buttonCreateDumpTruck); Controls.Add(buttonRight); Controls.Add(buttonDown); Controls.Add(buttonUp); Controls.Add(buttonLeft); - Controls.Add(buttonCreate); + Controls.Add(buttonCreateTruck); Controls.Add(pictureBoxDumpTruck); Name = "FormDumpTruck"; StartPosition = FormStartPosition.CenterScreen; - Text = "Form1"; + Text = "Проект"; ((System.ComponentModel.ISupportInitialize)pictureBoxDumpTruck).EndInit(); ResumeLayout(false); PerformLayout(); @@ -128,10 +167,13 @@ #endregion private PictureBox pictureBoxDumpTruck; - private Button buttonCreate; + private Button buttonCreateTruck; private Button buttonLeft; private Button buttonUp; private Button buttonDown; private Button buttonRight; + private Button buttonCreateDumpTruck; + private ComboBox comboBoxStrategy; + private Button buttonStep; } } \ No newline at end of file diff --git a/Project_DumpTruck/Project_DumpTruck/FormDumpTruck.cs b/Project_DumpTruck/Project_DumpTruck/FormDumpTruck.cs index 4881247..2670295 100644 --- a/Project_DumpTruck/Project_DumpTruck/FormDumpTruck.cs +++ b/Project_DumpTruck/Project_DumpTruck/FormDumpTruck.cs @@ -1,11 +1,16 @@ -namespace Project_DumpTruck +using Project_DumpTruck.DrawningObjects; +using Project_DumpTruck.MovementStrategy; + +namespace Project_DumpTruck { public partial class FormDumpTruck : Form { /// /// Поле-объект для прорисовки объекта /// - private DrawningDumpTruck? _drawningDumpTruck; + private DrawningTruck? _drawningTruck; + + private AbstractStrategy? _abstractStrategy; /// /// Инициализация формы @@ -20,33 +25,26 @@ /// private void Draw() { - if (_drawningDumpTruck == null) + if (_drawningTruck == null) return; Bitmap bmp = new(pictureBoxDumpTruck.Width, pictureBoxDumpTruck.Height); Graphics gr = Graphics.FromImage(bmp); - _drawningDumpTruck.DrawTransport(gr); + _drawningTruck.DrawTransport(gr); pictureBoxDumpTruck.Image = bmp; } /// - /// Обработка нажатия кнопки "Создать" + /// Обработка нажатия кнопки "Создать truck" /// /// /// - private void buttonCreate_Click(object sender, EventArgs e) + private void buttonCreateTruck_Click(object sender, EventArgs e) { Random random = new(); - _drawningDumpTruck = new DrawningDumpTruck(); - _drawningDumpTruck.Init(random.Next(100, 300), random.Next(1000, 3000), - Color.FromArgb(random.Next(0, 256), random.Next(0, 256), - random.Next(0, 256)), - Color.FromArgb(random.Next(0, 256), random.Next(0, 256), - random.Next(0, 256)), - Convert.ToBoolean(random.Next(0, 2)), - Convert.ToBoolean(random.Next(0, 2)), + _drawningTruck = new DrawningTruck(random.Next(100, 300), random.Next(1000, 3000), + Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)), pictureBoxDumpTruck.Width, pictureBoxDumpTruck.Height); - _drawningDumpTruck.SetPosition(random.Next(10, 100), - random.Next(10, 100)); + _drawningTruck.SetPosition(random.Next(10, 100), random.Next(10, 100)); Draw(); } @@ -58,7 +56,7 @@ /// private void buttonMove_Click(object sender, EventArgs e) { - if (_drawningDumpTruck == null) + if (_drawningTruck == null) { return; } @@ -66,19 +64,68 @@ switch (name) { case "buttonUp": - _drawningDumpTruck.MoveTransport(DirectionType.Up); + _drawningTruck.MoveTransport(DirectionType.Up); break; case "buttonDown": - _drawningDumpTruck.MoveTransport(DirectionType.Down); + _drawningTruck.MoveTransport(DirectionType.Down); break; case "buttonLeft": - _drawningDumpTruck.MoveTransport(DirectionType.Left); + _drawningTruck.MoveTransport(DirectionType.Left); break; case "buttonRight": - _drawningDumpTruck.MoveTransport(DirectionType.Right); + _drawningTruck.MoveTransport(DirectionType.Right); break; } Draw(); } + + private void buttonCreateDumpTruck_Click(object sender, EventArgs e) + { + Random random = new(); + _drawningTruck = new DrawningDumpTruck(random.Next(100, 300), random.Next(1000, 3000), + Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)), + Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)), + Convert.ToBoolean(random.Next(0, 2)), Convert.ToBoolean(random.Next(0, 2)), + pictureBoxDumpTruck.Width, pictureBoxDumpTruck.Height); + _drawningTruck.SetPosition(random.Next(10, 100), random.Next(10, 100)); + Draw(); + } + + private void buttonStep_Click(object sender, EventArgs e) + { + if (_drawningTruck == null) + { + return; + } + if (comboBoxStrategy.Enabled) + { + _abstractStrategy = comboBoxStrategy.SelectedIndex + switch + { + 0 => new MoveToCenter(), + 1 => new MoveToBorder(), + _ => null, + }; + if (_abstractStrategy == null) + { + return; + } + _abstractStrategy.SetData(new + DrawningObjectTruck(_drawningTruck), pictureBoxDumpTruck.Width, + pictureBoxDumpTruck.Height); + comboBoxStrategy.Enabled = false; + } + if (_abstractStrategy == null) + { + return; + } + _abstractStrategy.MakeStep(); + Draw(); + if (_abstractStrategy.GetStatus() == Status.Finish) + { + comboBoxStrategy.Enabled = true; + _abstractStrategy = null; + } + } } } \ No newline at end of file diff --git a/Project_DumpTruck/Project_DumpTruck/IMoveableObject.cs b/Project_DumpTruck/Project_DumpTruck/IMoveableObject.cs new file mode 100644 index 0000000..a950170 --- /dev/null +++ b/Project_DumpTruck/Project_DumpTruck/IMoveableObject.cs @@ -0,0 +1,31 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Project_DumpTruck.MovementStrategy +{ + public interface IMoveableObject + { + /// + /// Получение координаты X объекта + /// + ObjectParameters? GetObjectPosition { get; } + /// + /// Шаг объекта + /// + int GetStep { get; } + /// + /// Проверка, можно ли переместиться по нужному направлению + /// + /// + /// + bool CheckCanMove(DirectionType direction); + /// + /// Изменение направления пермещения объекта + /// + /// Направление + void MoveObject(DirectionType direction); + } +} diff --git a/Project_DumpTruck/Project_DumpTruck/MoveToBorder.cs b/Project_DumpTruck/Project_DumpTruck/MoveToBorder.cs new file mode 100644 index 0000000..95d1b84 --- /dev/null +++ b/Project_DumpTruck/Project_DumpTruck/MoveToBorder.cs @@ -0,0 +1,59 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Project_DumpTruck.MovementStrategy; + +namespace Project_DumpTruck +{ + internal class MoveToBorder : AbstractStrategy + { + protected override bool IsTargetDestinaion() + { + var objParams = GetObjectParameters; + if (objParams == null) + { + return false; + } + + return objParams.RightBorder <= FieldWidth && + objParams.RightBorder + GetStep() >= FieldWidth && + objParams.DownBorder <= FieldHeight && + objParams.DownBorder + GetStep() >= FieldHeight; + } + + protected override void MoveToTarget() + { + var objParams = GetObjectParameters; + if (objParams == null) + { + return; + } + var diffX = objParams.RightBorder - FieldWidth; + if (Math.Abs(diffX) > GetStep()) + { + if (diffX > 0) + { + MoveLeft(); + } + else + { + MoveRight(); + } + } + var diffY = objParams.DownBorder - FieldHeight; + if (Math.Abs(diffY) > GetStep()) + { + if (diffY > 0) + { + MoveUp(); + } + else + { + MoveDown(); + } + } + } + } +} diff --git a/Project_DumpTruck/Project_DumpTruck/MoveToCenter.cs b/Project_DumpTruck/Project_DumpTruck/MoveToCenter.cs new file mode 100644 index 0000000..251c973 --- /dev/null +++ b/Project_DumpTruck/Project_DumpTruck/MoveToCenter.cs @@ -0,0 +1,54 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Project_DumpTruck.MovementStrategy +{ + public class MoveToCenter : AbstractStrategy + { + protected override bool IsTargetDestinaion() + { + var objParams = GetObjectParameters; + if (objParams == null) + { + return false; + } + return objParams.ObjectMiddleHorizontal <= FieldWidth / 2 && objParams.ObjectMiddleHorizontal + GetStep() >= FieldWidth / 2 && + objParams.ObjectMiddleVertical <= FieldHeight / 2 && objParams.ObjectMiddleVertical + GetStep() >= FieldHeight / 2; + } + protected override void MoveToTarget() + { + var objParams = GetObjectParameters; + if (objParams == null) + { + return; + } + var diffX = objParams.ObjectMiddleHorizontal - FieldWidth / 2; + if (Math.Abs(diffX) > GetStep()) + { + if (diffX > 0) + { + MoveLeft(); + } + else + { + MoveRight(); + } + } + var diffY = objParams.ObjectMiddleVertical - FieldHeight / 2; + if (Math.Abs(diffY) > GetStep()) + { + if (diffY > 0) + { + MoveUp(); + } + else + { + MoveDown(); + } + } + } + } +} diff --git a/Project_DumpTruck/Project_DumpTruck/ObjectParameters.cs b/Project_DumpTruck/Project_DumpTruck/ObjectParameters.cs new file mode 100644 index 0000000..f834ab6 --- /dev/null +++ b/Project_DumpTruck/Project_DumpTruck/ObjectParameters.cs @@ -0,0 +1,55 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Project_DumpTruck.MovementStrategy +{ + public class ObjectParameters + { + private readonly int _x; + 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/Project_DumpTruck/Project_DumpTruck/Status.cs b/Project_DumpTruck/Project_DumpTruck/Status.cs new file mode 100644 index 0000000..8e3b463 --- /dev/null +++ b/Project_DumpTruck/Project_DumpTruck/Status.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Project_DumpTruck.MovementStrategy +{ + public enum Status + { + NotInit, + + InProgress, + + Finish + } +} -- 2.25.1