From 5a1e792e5be3d8975d6de6c1fd93ac1ab5ea9d5a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=92=D1=8F=D1=87=D0=B5=D1=81=D0=BB=D0=B0=D0=B2=20=D0=98?= =?UTF-8?q?=D0=B2=D0=B0=D0=BD=D0=BE=D0=B2?= Date: Mon, 2 Oct 2023 20:01:33 +0400 Subject: [PATCH 1/2] =?UTF-8?q?=D0=B3=D0=BE=D1=82=D0=BE=D0=B2=D0=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DrawningDoubleDeckerBus.cs | 196 --------------- .../DrawningObjects/DrawningBus.cs | 227 ++++++++++++++++++ .../DrawningDoubleDeckerBus.cs | 100 ++++++++ .../Entities/EntityBus.cs | 47 ++++ .../{ => Entities}/EntityDoubleDeckerBus.cs | 44 ++-- .../FormDoubleDeckerBus.Designer.cs | 76 ++++-- .../FormDoubleDeckerBus.cs | 105 ++++++-- .../MovementStrategy/AbstractStrategy.cs | 148 ++++++++++++ .../MovementStrategy/DrawningObjectBus.cs | 43 ++++ .../MovementStrategy/IMoveableObject.cs | 37 +++ .../MovementStrategy/MoveToBorder.cs | 61 +++++ .../MovementStrategy/MoveToCenter.cs | 60 +++++ .../MovementStrategy/ObjectParameters.cs | 67 ++++++ .../MovementStrategy/Status.cs | 18 ++ 14 files changed, 964 insertions(+), 265 deletions(-) delete mode 100644 PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/DrawningDoubleDeckerBus.cs create mode 100644 PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/DrawningObjects/DrawningBus.cs create mode 100644 PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/DrawningObjects/DrawningDoubleDeckerBus.cs create mode 100644 PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/Entities/EntityBus.cs rename PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/{ => Entities}/EntityDoubleDeckerBus.cs (55%) create mode 100644 PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/MovementStrategy/AbstractStrategy.cs create mode 100644 PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/MovementStrategy/DrawningObjectBus.cs create mode 100644 PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/MovementStrategy/IMoveableObject.cs create mode 100644 PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/MovementStrategy/MoveToBorder.cs create mode 100644 PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/MovementStrategy/MoveToCenter.cs create mode 100644 PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/MovementStrategy/ObjectParameters.cs create mode 100644 PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/MovementStrategy/Status.cs diff --git a/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/DrawningDoubleDeckerBus.cs b/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/DrawningDoubleDeckerBus.cs deleted file mode 100644 index 9ef7901..0000000 --- a/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/DrawningDoubleDeckerBus.cs +++ /dev/null @@ -1,196 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base -{ - internal class DrawningDoubleDeckerBus - { - /// - /// Класс-сущность - /// - public EntityDoubleDeckerBus? EntityDoubleDeckerBus { get; private set; } - - /// - /// Ширина окна - /// - private int _pictureWidth; - - /// - /// Высота окна - /// - private int _pictureHeight; - - /// - /// Левая координата прорисовки автобуса - /// - /// - private int _startPosX; - - /// - /// Верхняя кооридната прорисовки автобуса - /// - private int _startPosY; - - /// - /// Ширина прорисовки автобуса - /// - private readonly int _busWidth = 110; - - /// - /// Высота прорисовки автобуса - /// - private readonly int _busHeight = 70; - - /// - /// Инициализация свойств - /// - /// Скорость - /// Вес - /// Цвет кузова - /// Дополнительный цвет - /// Признак наличия второго этажа - /// Признак наличия лестницы на второй этаж - /// Признак наличия дорожной полосы - /// Ширина картинки - /// Высота картинки - /// true - объект создан, false - проверка не пройдена, - /// нельзя создать объект в этих размерах - public bool Init(int speed, double weight, Color bodyColor, Color -additionalColor, bool secondFloor, bool ladder, bool roadLine, int width, int height) - { - _pictureWidth = width; - _pictureHeight = height; - if (_pictureHeight < _busHeight || _pictureWidth < _busWidth) - { - return false; - } - EntityDoubleDeckerBus = new EntityDoubleDeckerBus(); - EntityDoubleDeckerBus.Init(speed, weight, bodyColor, additionalColor, - secondFloor, ladder, roadLine); - return true; - } - - /// - /// Установка позиции - /// - /// Координата X - /// Координата Y - public void SetPosition(int x, int y) - { - _startPosX = Math.Min(x, _pictureWidth - _busWidth); - _startPosY = Math.Min(y, _pictureHeight - _busHeight); - } - - /// - /// Изменение направления перемещения - /// - /// Направление - public void MoveTransport(DirectionType direction) - { - if (EntityDoubleDeckerBus == null) - { - return; - } - switch (direction) - { - //влево - case DirectionType.Left: - if (_startPosX - EntityDoubleDeckerBus.Step > 0) - { - _startPosX -= (int)EntityDoubleDeckerBus.Step; - } - break; - //вверх - case DirectionType.Up: - if (_startPosY - EntityDoubleDeckerBus.Step > 0) - { - _startPosY -= (int)EntityDoubleDeckerBus.Step; - } - break; - // вправо - case DirectionType.Right: - if (_startPosX + _busWidth + EntityDoubleDeckerBus.Step < _pictureWidth) - { - _startPosX += (int)EntityDoubleDeckerBus.Step; - } - break; - //вниз - case DirectionType.Down: - if (_startPosY + _busHeight + EntityDoubleDeckerBus.Step < _pictureHeight) - { - _startPosY += (int)EntityDoubleDeckerBus.Step; - } - break; - } - } - - /// - /// Прорисовка объекта - /// - /// - public void DrawTransport(Graphics g) - { - if (EntityDoubleDeckerBus == null) - { - return; - } - - Pen pen = new(Color.Black); - - // Границы первого этажа автобуса - g.DrawRectangle(pen, _startPosX, _startPosY + 30, 100, 30); - Brush brBodyColor = new SolidBrush(EntityDoubleDeckerBus.BodyColor); - g.FillRectangle(brBodyColor, _startPosX, _startPosY + 30, 100, 30); - - // Дверь - g.DrawRectangle(pen, _startPosX + 30, _startPosY + 40, 10, 20); - Brush brBlack = new SolidBrush(Color.Black); - g.FillRectangle(brBlack, _startPosX + 30, _startPosY + 40, 10, 20); - - // Колеса - g.DrawEllipse(pen, _startPosX + 7, _startPosY + 55, 10, 10); - g.DrawEllipse(pen, _startPosX + 77, _startPosY + 55, 10, 10); - g.FillEllipse(brBlack, _startPosX + 7, _startPosY + 55, 10, 10); - g.FillEllipse(brBlack, _startPosX + 77, _startPosY + 55, 10, 10); - - // Окна - Brush brBlue = new SolidBrush(Color.Blue); - g.FillEllipse(brBlue, _startPosX + 10, _startPosY + 35, 10, 15); - g.FillEllipse(brBlue, _startPosX + 50, _startPosY + 35, 10, 15); - g.FillEllipse(brBlue, _startPosX + 70, _startPosY + 35, 10, 15); - g.FillEllipse(brBlue, _startPosX + 90, _startPosY + 35, 10, 15); - - if (EntityDoubleDeckerBus.SecondFloor) - { - // Границы второго этажа автобуса - g.FillRectangle(brBodyColor, _startPosX, _startPosY, 100, 30); - - // Дверь второго этажа - g.DrawRectangle(pen, _startPosX, _startPosY + 10, 10, 20); - g.FillRectangle(brBlack, _startPosX, _startPosY + 10, 10, 20); - - // Окна второго этажа - g.FillEllipse(brBlue, _startPosX + 12, _startPosY + 5, 10, 15); - g.FillEllipse(brBlue, _startPosX + 30, _startPosY + 5, 10, 15); - g.FillEllipse(brBlue, _startPosX + 50, _startPosY + 5, 10, 15); - g.FillEllipse(brBlue, _startPosX + 70, _startPosY + 5, 10, 15); - g.FillEllipse(brBlue, _startPosX + 90, _startPosY + 5, 10, 15); - } - - if (EntityDoubleDeckerBus.Ladder) - { - //Вертикальные прямые - g.DrawLine(pen, new Point((int)(_startPosX), (int)(_startPosY + 55)), new Point((int)(_startPosX), (int)(_startPosY + 25))); - g.DrawLine(pen, new Point((int)(_startPosX + 10), (int)(_startPosY + 55)), new Point((int)(_startPosX + 10), (int)(_startPosY + 25))); - - //Горизонтальные прямые - g.DrawLine(pen, new Point((int)(_startPosX), (int)(_startPosY + 35)), new Point((int)(_startPosX + 10), (int)(_startPosY + 35))); - g.DrawLine(pen, new Point((int)(_startPosX), (int)(_startPosY + 45)), new Point((int)(_startPosX + 10), (int)(_startPosY + 45))); - g.DrawLine(pen, new Point((int)(_startPosX), (int)(_startPosY + 55)), new Point((int)(_startPosX + 10), (int)(_startPosY + 55))); - } - } - } -} diff --git a/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/DrawningObjects/DrawningBus.cs b/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/DrawningObjects/DrawningBus.cs new file mode 100644 index 0000000..16b3f7a --- /dev/null +++ b/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/DrawningObjects/DrawningBus.cs @@ -0,0 +1,227 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base.Entities; + +namespace PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base.DrawningObjects +{ + /// + /// Класс, отвечающий за прорисовку и перемещение объекта-сущности + /// + public class DrawningBus + { + /// + /// Класс-сущность + /// + public EntityBus? EntityBus { get; protected set; } + + /// + /// Ширина окна + /// + private int _pictureWidth; + + /// + /// Высота окна + /// + private int _pictureHeight; + + /// + /// Левая координата прорисовки автобуса + /// + /// + protected int _startPosX; + + /// + /// Верхняя кооридната прорисовки автобуса + /// + protected int _startPosY; + + /// + /// Ширина прорисовки автобуса + /// + protected readonly int _busWidth = 110; + + /// + /// Высота прорисовки автобуса + /// + protected readonly int _busHeight = 70; + + /// + /// Координата X объекта + /// + public int GetPosX => _startPosX; + + /// + /// Координата Y объекта + /// + public int GetPosY => _startPosY; + + /// + /// Ширина объекта + /// + public int GetWidth => _busWidth; + + /// + /// Высота объекта + /// + public int GetHeight => _busHeight; + + /// + /// Конструктор + /// + /// Скорость + /// Вес + /// Основной цвет + /// Ширина картинки + /// Высота картинки + public DrawningBus(int speed, double weight, Color bodyColor, int + width, int height) + { + if (width < _busWidth || height < _busHeight) + { + return; + } + _pictureWidth = width; + _pictureHeight = height; + EntityBus = new EntityBus(speed, weight, bodyColor); + } + + /// + /// Конструктор + /// + /// Скорость + /// Вес + /// Основной цвет + /// Ширина картинки + /// Высота картинки + /// Ширина прорисовки автобуса + /// Высота прорисовки автобуса + protected DrawningBus(int speed, double weight, Color bodyColor, int + width, int height, int busWidth, int busHeight) + { + if (width <= _busWidth || height <= _busHeight) + { + return; + } + _pictureWidth = width; + _pictureHeight = height; + _busWidth = busWidth; + _busHeight = busHeight; + EntityBus = new EntityBus(speed, weight, bodyColor); + } + + /// + /// Установка позиции + /// + /// Координата X + /// Координата Y + public void SetPosition(int x, int y) + { + if (x < 0 || x + _busWidth > _pictureWidth) + { + x = Math.Max(0, _pictureWidth - _busWidth); + } + if (y < 0 || y + _busHeight > _pictureHeight) + { + y = Math.Max(0, _pictureHeight - _busHeight); + } + _startPosX = x; + _startPosY = y; + } + + /// + /// Проверка, что объект может переместится по указанному направлению + /// + /// Направление + /// true - можно переместится по указанному направлению + public bool CanMove(DirectionType direction) + { + if (EntityBus == null) + { + return false; + } + return direction switch + { + //влево + DirectionType.Left => _startPosX - EntityBus.Step > 0, + //вверх + DirectionType.Up => _startPosY - EntityBus.Step > 0, + //вправо + DirectionType.Right => _startPosX + _busWidth + EntityBus.Step < _pictureWidth, + //вниз + DirectionType.Down => _startPosY + _busHeight + EntityBus.Step < _pictureHeight, + _ => false, + }; + } + + /// + /// Изменение направления перемещения + /// + /// Направление + public void MoveTransport(DirectionType direction) + { + if (!CanMove(direction) || EntityBus == null) + { + return; + } + switch (direction) + { + //влево + case DirectionType.Left: + _startPosX -= (int)EntityBus.Step; + break; + //вверх + case DirectionType.Up: + _startPosY -= (int)EntityBus.Step; + break; + // вправо + case DirectionType.Right: + _startPosX += (int)EntityBus.Step; + break; + //вниз + case DirectionType.Down: + _startPosY += (int)EntityBus.Step; + break; + } + } + + /// + /// Прорисовка объекта + /// + /// + public virtual void DrawTransport(Graphics g) + { + if (EntityBus == null) + { + return; + } + + Pen pen = new(Color.Black); + + // Границы первого этажа автобуса + g.DrawRectangle(pen, _startPosX, _startPosY + 30, 100, 30); + Brush brBodyColor = new SolidBrush(EntityBus.BodyColor); + g.FillRectangle(brBodyColor, _startPosX, _startPosY + 30, 100, 30); + + // Дверь + g.DrawRectangle(pen, _startPosX + 30, _startPosY + 40, 10, 20); + Brush brBlack = new SolidBrush(Color.Black); + g.FillRectangle(brBlack, _startPosX + 30, _startPosY + 40, 10, 20); + + // Колеса + g.DrawEllipse(pen, _startPosX + 7, _startPosY + 55, 10, 10); + g.DrawEllipse(pen, _startPosX + 77, _startPosY + 55, 10, 10); + g.FillEllipse(brBlack, _startPosX + 7, _startPosY + 55, 10, 10); + g.FillEllipse(brBlack, _startPosX + 77, _startPosY + 55, 10, 10); + + // Окна + Brush brBlue = new SolidBrush(Color.Blue); + g.FillEllipse(brBlue, _startPosX + 10, _startPosY + 35, 10, 15); + g.FillEllipse(brBlue, _startPosX + 50, _startPosY + 35, 10, 15); + g.FillEllipse(brBlue, _startPosX + 70, _startPosY + 35, 10, 15); + g.FillEllipse(brBlue, _startPosX + 90, _startPosY + 35, 10, 15); + } + } +} diff --git a/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/DrawningObjects/DrawningDoubleDeckerBus.cs b/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/DrawningObjects/DrawningDoubleDeckerBus.cs new file mode 100644 index 0000000..4bc5e21 --- /dev/null +++ b/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/DrawningObjects/DrawningDoubleDeckerBus.cs @@ -0,0 +1,100 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net.NetworkInformation; +using System.Text; +using System.Threading.Tasks; +using PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base.Entities; + +namespace PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base.DrawningObjects +{ + /// + /// Класс, отвечающий за прорисовку и перемещение объекта-сущности + /// + public class DrawningDoubleDeckerBus : DrawningBus + { + /// + /// Инициализация свойств + /// + /// Скорость + /// Вес + /// Цвет кузова + /// Дополнительный цвет + /// Признак наличия второго этажа + /// Признак наличия лестницы на второй этаж + /// Признак наличия полосы между этажами + /// Ширина картинки + /// Высота картинки + /// true - объект создан, false - проверка не пройдена, + /// нельзя создать объект в этих размерах + public DrawningDoubleDeckerBus(int speed, double weight, Color bodyColor, Color +additionalColor, bool secondFloor, bool ladder, bool lineBetweenFloor, int width, int height) : + base(speed, weight, bodyColor, width, height, 120, 85) + { + if (EntityBus != null) + { + EntityBus = new EntityDoubleDeckerBus(speed, weight, bodyColor, + additionalColor, secondFloor, ladder, lineBetweenFloor); + } + } + + /// + /// Прорисовка объекта + /// + /// + public override void DrawTransport(Graphics g) + { + if (EntityBus is not EntityDoubleDeckerBus doubleDeckerBus) + { + return; + } + + Pen pen = new(Color.Black); + Brush brAdditionalColor = new SolidBrush(doubleDeckerBus.AdditionalColor); + Brush brBlue = new SolidBrush(Color.Blue); + Brush brBlack = new SolidBrush(Color.Black); + + // второй этаж + if (doubleDeckerBus.SecondFloor) + { + // Границы второго этажа автобуса + g.FillRectangle(brAdditionalColor, _startPosX, _startPosY, 100, 30); + + // Дверь второго этажа + g.DrawRectangle(pen, _startPosX, _startPosY + 10, 10, 20); + g.FillRectangle(brAdditionalColor, _startPosX, _startPosY + 10, 10, 20); + + // Окна второго этажа + g.FillEllipse(brBlue, _startPosX + 12, _startPosY + 5, 10, 15); + g.FillEllipse(brBlue, _startPosX + 30, _startPosY + 5, 10, 15); + g.FillEllipse(brBlue, _startPosX + 50, _startPosY + 5, 10, 15); + g.FillEllipse(brBlue, _startPosX + 70, _startPosY + 5, 10, 15); + g.FillEllipse(brBlue, _startPosX + 90, _startPosY + 5, 10, 15); + } + + base.DrawTransport(g); + + // лестница на второй этаж + if (doubleDeckerBus.Ladder) + { + if (doubleDeckerBus.SecondFloor == true) + { + //Вертикальные прямые + g.DrawLine(pen, new Point(_startPosX, _startPosY + 55), new Point(_startPosX, _startPosY + 25)); + g.DrawLine(pen, new Point(_startPosX + 10, _startPosY + 55), new Point(_startPosX + 10, _startPosY + 25)); + + //Горизонтальные прямые + g.DrawLine(pen, new Point(_startPosX, _startPosY + 35), new Point(_startPosX + 10, _startPosY + 35)); + g.DrawLine(pen, new Point(_startPosX, _startPosY + 45), new Point(_startPosX + 10, _startPosY + 45)); + g.DrawLine(pen, new Point(_startPosX, _startPosY + 55), new Point(_startPosX + 10, _startPosY + 55)); + } + } + + // полоса между этажами + if (doubleDeckerBus.LineBetweenFloor) + { + g.FillRectangle(brBlack, _startPosX, _startPosY + 30, 100, 3); + } + } + } +} diff --git a/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/Entities/EntityBus.cs b/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/Entities/EntityBus.cs new file mode 100644 index 0000000..b483493 --- /dev/null +++ b/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/Entities/EntityBus.cs @@ -0,0 +1,47 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base.Entities +{ + /// + /// Класс-сущность "Автобус" + /// + public class EntityBus + { + /// + /// Скорость + /// + 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 EntityBus(int speed, double weight, Color bodyColor) + { + Speed = speed; + Weight = weight; + BodyColor = bodyColor; + } + } +} diff --git a/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/EntityDoubleDeckerBus.cs b/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/Entities/EntityDoubleDeckerBus.cs similarity index 55% rename from PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/EntityDoubleDeckerBus.cs rename to PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/Entities/EntityDoubleDeckerBus.cs index e0506e4..7fc6596 100644 --- a/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/EntityDoubleDeckerBus.cs +++ b/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/Entities/EntityDoubleDeckerBus.cs @@ -4,45 +4,35 @@ using System.Linq; using System.Text; using System.Threading.Tasks; -namespace PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base +namespace PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base.Entities { - public class EntityDoubleDeckerBus + /// + /// Класс-сущность "Двухэтажный автобус" + /// + public class EntityDoubleDeckerBus : EntityBus { - /// - /// Скорость - /// - public int Speed { get; private set; } - /// - /// Вес - /// - public double Weight { get; private set; } - /// - /// Основной цвет - /// - public Color BodyColor { get; private set; } /// /// Дополнительный цвет (для опциональных элементов) /// public Color AdditionalColor { get; private set; } + /// /// Признак (опция) наличия второго этажа /// public bool SecondFloor { get; private set; } + /// /// Признак (опция) наличия лестницы на второй этаж /// public bool Ladder { get; private set; } - /// - /// Признак (опция) наличия гоночной полосы - /// - public bool RoadLine { get; private set; } /// - /// Шаг перемещения автобуса + /// Признак (опция) наличия полосы между этажами /// - public double Step => (double)Speed * 100 / Weight; + public bool LineBetweenFloor { get; private set; } + /// - /// Инициализация полей объекта-класса спортивного автомобиля + /// Инициализация полей объекта-класса двухэтажного автобуса /// /// Скорость /// Вес автобуса @@ -50,18 +40,14 @@ namespace PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base /// Дополнительный цвет /// Признак наличия второго этажа /// Признак наличия лестницы на второй этаж - /// Признак наличия дорожной полосы - - public void Init(int speed, double weight, Color bodyColor, Color - additionalColor, bool secondFloor, bool ladder, bool roadLine) + /// Признак наличия полосы между этажами + public EntityDoubleDeckerBus(int speed, double weight, Color bodyColor, Color + additionalColor, bool secondFloor, bool ladder, bool lineBetweenFloor) : base(speed, weight, bodyColor) { - Speed = speed; - Weight = weight; - BodyColor = bodyColor; AdditionalColor = additionalColor; SecondFloor = secondFloor; Ladder = ladder; - RoadLine = roadLine; + LineBetweenFloor = lineBetweenFloor; } } } diff --git a/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/FormDoubleDeckerBus.Designer.cs b/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/FormDoubleDeckerBus.Designer.cs index 2f9ee74..ee0678b 100644 --- a/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/FormDoubleDeckerBus.Designer.cs +++ b/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/FormDoubleDeckerBus.Designer.cs @@ -29,11 +29,14 @@ private void InitializeComponent() { pictureBoxDoubleDeckerBus = new PictureBox(); - buttonCreate = new Button(); buttonLeft = new Button(); buttonUp = new Button(); buttonDown = new Button(); buttonRight = new Button(); + buttonCreateDoubleDeckerBus = new Button(); + comboBoxStrategy = new ComboBox(); + buttonStep = new Button(); + buttonCreateBus = new Button(); ((System.ComponentModel.ISupportInitialize)pictureBoxDoubleDeckerBus).BeginInit(); SuspendLayout(); // @@ -48,17 +51,6 @@ pictureBoxDoubleDeckerBus.TabIndex = 0; pictureBoxDoubleDeckerBus.TabStop = false; // - // buttonCreate - // - buttonCreate.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; - buttonCreate.Location = new Point(12, 412); - buttonCreate.Name = "buttonCreate"; - buttonCreate.Size = new Size(94, 29); - buttonCreate.TabIndex = 1; - buttonCreate.Text = "Создать"; - buttonCreate.UseVisualStyleBackColor = true; - buttonCreate.Click += buttonCreate_Click; - // // buttonLeft // buttonLeft.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; @@ -69,7 +61,7 @@ buttonLeft.Size = new Size(30, 30); buttonLeft.TabIndex = 2; buttonLeft.UseVisualStyleBackColor = true; - buttonLeft.Click += buttonMove_Click; + buttonLeft.Click += ButtonMove_Click; // // buttonUp // @@ -81,7 +73,7 @@ buttonUp.Size = new Size(30, 30); buttonUp.TabIndex = 3; buttonUp.UseVisualStyleBackColor = true; - buttonUp.Click += buttonMove_Click; + buttonUp.Click += ButtonMove_Click; // // buttonDown // @@ -93,7 +85,7 @@ buttonDown.Size = new Size(30, 30); buttonDown.TabIndex = 4; buttonDown.UseVisualStyleBackColor = true; - buttonDown.Click += buttonMove_Click; + buttonDown.Click += ButtonMove_Click; // // buttonRight // @@ -105,18 +97,63 @@ buttonRight.Size = new Size(30, 30); buttonRight.TabIndex = 5; buttonRight.UseVisualStyleBackColor = true; - buttonRight.Click += buttonMove_Click; + buttonRight.Click += ButtonMove_Click; + // + // buttonCreateDoubleDeckerBus + // + buttonCreateDoubleDeckerBus.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; + buttonCreateDoubleDeckerBus.Location = new Point(12, 389); + buttonCreateDoubleDeckerBus.Name = "buttonCreateDoubleDeckerBus"; + buttonCreateDoubleDeckerBus.Size = new Size(167, 52); + buttonCreateDoubleDeckerBus.TabIndex = 6; + buttonCreateDoubleDeckerBus.Text = "Создать двухэтажный автобус"; + buttonCreateDoubleDeckerBus.UseVisualStyleBackColor = true; + buttonCreateDoubleDeckerBus.Click += ButtonCreateDoubleDeckerBus_Click; + // + // comboBoxStrategy + // + comboBoxStrategy.DropDownStyle = ComboBoxStyle.DropDownList; + comboBoxStrategy.FormattingEnabled = true; + comboBoxStrategy.Items.AddRange(new object[] { "К центру", "К краю" }); + comboBoxStrategy.Location = new Point(768, 12); + comboBoxStrategy.Name = "comboBoxStrategy"; + comboBoxStrategy.Size = new Size(102, 28); + comboBoxStrategy.TabIndex = 7; + // + // buttonStep + // + buttonStep.Location = new Point(804, 46); + buttonStep.Name = "buttonStep"; + buttonStep.Size = new Size(66, 26); + buttonStep.TabIndex = 8; + buttonStep.Text = "Шаг"; + buttonStep.UseVisualStyleBackColor = true; + buttonStep.Click += ButtonStep_Click; + // + // buttonCreateBus + // + buttonCreateBus.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; + buttonCreateBus.Location = new Point(185, 389); + buttonCreateBus.Name = "buttonCreateBus"; + buttonCreateBus.Size = new Size(167, 52); + buttonCreateBus.TabIndex = 10; + buttonCreateBus.Text = "Создать автобус"; + buttonCreateBus.UseVisualStyleBackColor = true; + buttonCreateBus.Click += buttonCreateBus_Click; // // FormDoubleDeckerBus // AutoScaleDimensions = new SizeF(8F, 20F); AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(882, 453); + Controls.Add(buttonCreateBus); + Controls.Add(buttonStep); + Controls.Add(comboBoxStrategy); + Controls.Add(buttonCreateDoubleDeckerBus); Controls.Add(buttonRight); Controls.Add(buttonDown); Controls.Add(buttonUp); Controls.Add(buttonLeft); - Controls.Add(buttonCreate); Controls.Add(pictureBoxDoubleDeckerBus); Name = "FormDoubleDeckerBus"; StartPosition = FormStartPosition.CenterScreen; @@ -129,10 +166,13 @@ #endregion private PictureBox pictureBoxDoubleDeckerBus; - private Button buttonCreate; private Button buttonLeft; private Button buttonUp; private Button buttonDown; private Button buttonRight; + private Button buttonCreateDoubleDeckerBus; + private ComboBox comboBoxStrategy; + private Button buttonStep; + private Button buttonCreateBus; } } \ No newline at end of file diff --git a/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/FormDoubleDeckerBus.cs b/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/FormDoubleDeckerBus.cs index 496be9f..f94315a 100644 --- a/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/FormDoubleDeckerBus.cs +++ b/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/FormDoubleDeckerBus.cs @@ -1,12 +1,19 @@ +using PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base.DrawningObjects; +using PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base.MovementStrategy; + namespace PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base { public partial class FormDoubleDeckerBus : Form { + /// + /// " " + /// + private DrawningBus? _drawningBus; /// - /// - + /// /// - private DrawningDoubleDeckerBus? _drawningDoubleDeckerBus; + private AbstractStrategy? _abstractStrategy; /// /// @@ -21,45 +28,57 @@ namespace PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base /// private void Draw() { - if (_drawningDoubleDeckerBus == null) + if (_drawningBus == null) { return; } - Bitmap bmp = new(pictureBoxDoubleDeckerBus.Width, - pictureBoxDoubleDeckerBus.Height); + Bitmap bmp = new(pictureBoxDoubleDeckerBus.Width, pictureBoxDoubleDeckerBus.Height); Graphics gr = Graphics.FromImage(bmp); - _drawningDoubleDeckerBus.DrawTransport(gr); + _drawningBus.DrawTransport(gr); pictureBoxDoubleDeckerBus.Image = bmp; } /// - /// "" + /// " " /// /// /// - private void buttonCreate_Click(object sender, EventArgs e) + private void ButtonCreateDoubleDeckerBus_Click(object sender, EventArgs e) { Random random = new(); - _drawningDoubleDeckerBus = new DrawningDoubleDeckerBus(); - _drawningDoubleDeckerBus.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)), + _drawningBus = new DrawningDoubleDeckerBus(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)), Convert.ToBoolean(random.Next(0, 2)), pictureBoxDoubleDeckerBus.Width, pictureBoxDoubleDeckerBus.Height); - _drawningDoubleDeckerBus.SetPosition(random.Next(10, 100), - random.Next(10, 100)); + _drawningBus.SetPosition(random.Next(10, 100), random.Next(10, 100)); Draw(); } /// - /// + /// " " /// /// /// - private void buttonMove_Click(object sender, EventArgs e) + private void buttonCreateBus_Click(object sender, EventArgs e) { - if (_drawningDoubleDeckerBus == null) + Random random = new(); + _drawningBus = new DrawningBus(random.Next(100, 300), random.Next(1000, 3000), + Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)), + pictureBoxDoubleDeckerBus.Width, pictureBoxDoubleDeckerBus.Height); + _drawningBus.SetPosition(random.Next(10, 100), random.Next(10, 100)); + Draw(); + } + + /// + /// + /// + /// + /// + private void ButtonMove_Click(object sender, EventArgs e) + { + if (_drawningBus == null) { return; } @@ -67,19 +86,61 @@ namespace PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base switch (name) { case "buttonUp": - _drawningDoubleDeckerBus.MoveTransport(DirectionType.Up); + _drawningBus.MoveTransport(DirectionType.Up); break; case "buttonDown": - _drawningDoubleDeckerBus.MoveTransport(DirectionType.Down); + _drawningBus.MoveTransport(DirectionType.Down); break; case "buttonLeft": - _drawningDoubleDeckerBus.MoveTransport(DirectionType.Left); + _drawningBus.MoveTransport(DirectionType.Left); break; case "buttonRight": - _drawningDoubleDeckerBus.MoveTransport(DirectionType.Right); + _drawningBus.MoveTransport(DirectionType.Right); break; } Draw(); } + + /// + /// "" + /// + /// + /// + private void ButtonStep_Click(object sender, EventArgs e) + { + if (_drawningBus == null) + { + return; + } + if (comboBoxStrategy.Enabled) + { + _abstractStrategy = comboBoxStrategy.SelectedIndex + switch + { + 0 => new MoveToCenter(), + 1 => new MoveToBorder(), + _ => null, + }; + if (_abstractStrategy == null) + { + return; + } + _abstractStrategy.SetData(new + DrawningObjectBus(_drawningBus), pictureBoxDoubleDeckerBus.Width, + pictureBoxDoubleDeckerBus.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/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/MovementStrategy/AbstractStrategy.cs b/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/MovementStrategy/AbstractStrategy.cs new file mode 100644 index 0000000..ddcd788 --- /dev/null +++ b/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/MovementStrategy/AbstractStrategy.cs @@ -0,0 +1,148 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base.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; + } + } +} \ No newline at end of file diff --git a/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/MovementStrategy/DrawningObjectBus.cs b/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/MovementStrategy/DrawningObjectBus.cs new file mode 100644 index 0000000..2d2ab27 --- /dev/null +++ b/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/MovementStrategy/DrawningObjectBus.cs @@ -0,0 +1,43 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base.DrawningObjects; + +namespace PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base.MovementStrategy +{ + /// + /// Реализация интерфейса IDrawningObject для работы с объектом DrawningBus (паттерн Adapter) + /// + public class DrawningObjectBus : IMoveableObject + { + private readonly DrawningBus? _drawningBus = null; + + public DrawningObjectBus(DrawningBus drawningBus) + { + _drawningBus = drawningBus; + } + + public ObjectParameters? GetObjectPosition + { + get + { + if (_drawningBus == null || _drawningBus.EntityBus == null) + { + return null; + } + return new ObjectParameters(_drawningBus.GetPosX, + _drawningBus.GetPosY, _drawningBus.GetWidth, _drawningBus.GetHeight); + } + } + + public int GetStep => (int)(_drawningBus?.EntityBus?.Step ?? 0); + + public bool CheckCanMove(DirectionType direction) => + _drawningBus?.CanMove(direction) ?? false; + + public void MoveObject(DirectionType direction) => + _drawningBus?.MoveTransport(direction); + } +} diff --git a/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/MovementStrategy/IMoveableObject.cs b/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/MovementStrategy/IMoveableObject.cs new file mode 100644 index 0000000..9ac4dd3 --- /dev/null +++ b/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/MovementStrategy/IMoveableObject.cs @@ -0,0 +1,37 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base.MovementStrategy +{ + /// + /// Интерфейс для работы с перемещаемым объектом + /// + public interface IMoveableObject + { + /// + /// Получение координаты X объекта + /// + ObjectParameters? GetObjectPosition { get; } + + /// + /// Шаг объекта + /// + int GetStep { get; } + + /// + /// Проверка, можно ли переместиться по нужному направлению + /// + /// + /// + bool CheckCanMove(DirectionType direction); + + /// + /// Изменение направления пермещения объекта + /// + /// Направление + void MoveObject(DirectionType direction); + } +} diff --git a/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/MovementStrategy/MoveToBorder.cs b/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/MovementStrategy/MoveToBorder.cs new file mode 100644 index 0000000..0139738 --- /dev/null +++ b/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/MovementStrategy/MoveToBorder.cs @@ -0,0 +1,61 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base.MovementStrategy +{ + /// + /// Стратегия перемещения объекта в правый нижний край экрана + /// + public 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/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/MovementStrategy/MoveToCenter.cs b/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/MovementStrategy/MoveToCenter.cs new file mode 100644 index 0000000..383c37c --- /dev/null +++ b/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/MovementStrategy/MoveToCenter.cs @@ -0,0 +1,60 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base.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/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/MovementStrategy/ObjectParameters.cs b/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/MovementStrategy/ObjectParameters.cs new file mode 100644 index 0000000..24bce8c --- /dev/null +++ b/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/MovementStrategy/ObjectParameters.cs @@ -0,0 +1,67 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base.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/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/MovementStrategy/Status.cs b/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/MovementStrategy/Status.cs new file mode 100644 index 0000000..765e06a --- /dev/null +++ b/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/MovementStrategy/Status.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base.MovementStrategy +{ + /// + /// Статус выполнения операции перемещения + /// + public enum Status + { + NotInit, + InProgress, + Finish + } +} -- 2.25.1 From 125590d0fc12979208546b843be81a2791c216f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=92=D1=8F=D1=87=D0=B5=D1=81=D0=BB=D0=B0=D0=B2=20=D0=98?= =?UTF-8?q?=D0=B2=D0=B0=D0=BD=D0=BE=D0=B2?= Date: Mon, 2 Oct 2023 20:20:29 +0400 Subject: [PATCH 2/2] fix --- .../FormDoubleDeckerBus.Designer.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/FormDoubleDeckerBus.Designer.cs b/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/FormDoubleDeckerBus.Designer.cs index ee0678b..6419c2f 100644 --- a/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/FormDoubleDeckerBus.Designer.cs +++ b/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/FormDoubleDeckerBus.Designer.cs @@ -157,7 +157,7 @@ Controls.Add(pictureBoxDoubleDeckerBus); Name = "FormDoubleDeckerBus"; StartPosition = FormStartPosition.CenterScreen; - Text = "FormDoubleDeckerBus"; + Text = "Двухэтажный автобус"; ((System.ComponentModel.ISupportInitialize)pictureBoxDoubleDeckerBus).EndInit(); ResumeLayout(false); PerformLayout(); -- 2.25.1