From 28fd4805b728ec6789ed50c5d1d10b679850c168 Mon Sep 17 00:00:00 2001 From: vladimir_zinovev Date: Fri, 6 Oct 2023 10:43:19 +0400 Subject: [PATCH 1/3] lab2 is done --- ProjectTank/ProjectTank/AbstractStrategy.cs | 134 +++++++++++ .../ProjectTank/DrawningArmoredTransport.cs | 226 ++++++++++++++++++ .../DrawningObjectArmoredTransport.cs | 37 +++ ProjectTank/ProjectTank/DrawningTank.cs | 176 ++------------ .../ProjectTank/EntityArmoredTransport.cs | 43 ++++ ProjectTank/ProjectTank/EntityTank.cs | 42 +--- ProjectTank/ProjectTank/IMoveableObject.cs | 35 +++ ProjectTank/ProjectTank/MoveToBorder.cs | 45 ++++ ProjectTank/ProjectTank/MoveToCenter.cs | 59 +++++ ProjectTank/ProjectTank/ObjectParameters.cs | 57 +++++ ProjectTank/ProjectTank/Status.cs | 18 ++ ProjectTank/ProjectTank/TankForm.Designer.cs | 64 ++++- ProjectTank/ProjectTank/TankForm.cs | 89 +++++-- 13 files changed, 818 insertions(+), 207 deletions(-) create mode 100644 ProjectTank/ProjectTank/AbstractStrategy.cs create mode 100644 ProjectTank/ProjectTank/DrawningArmoredTransport.cs create mode 100644 ProjectTank/ProjectTank/DrawningObjectArmoredTransport.cs create mode 100644 ProjectTank/ProjectTank/EntityArmoredTransport.cs create mode 100644 ProjectTank/ProjectTank/IMoveableObject.cs create mode 100644 ProjectTank/ProjectTank/MoveToBorder.cs create mode 100644 ProjectTank/ProjectTank/MoveToCenter.cs create mode 100644 ProjectTank/ProjectTank/ObjectParameters.cs create mode 100644 ProjectTank/ProjectTank/Status.cs diff --git a/ProjectTank/ProjectTank/AbstractStrategy.cs b/ProjectTank/ProjectTank/AbstractStrategy.cs new file mode 100644 index 0000000..15c9d35 --- /dev/null +++ b/ProjectTank/ProjectTank/AbstractStrategy.cs @@ -0,0 +1,134 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using ProjectTank.DrawningObjects; + +namespace ProjectTank.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/ProjectTank/ProjectTank/DrawningArmoredTransport.cs b/ProjectTank/ProjectTank/DrawningArmoredTransport.cs new file mode 100644 index 0000000..4394b3d --- /dev/null +++ b/ProjectTank/ProjectTank/DrawningArmoredTransport.cs @@ -0,0 +1,226 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using ProjectTank.Entities; + +namespace ProjectTank.DrawningObjects +{ + /// + /// Класс, отвечающий за прорисовку и перемещение объекта-сущности + /// + public class DrawningArmoredTransport + { + /// + /// Класс-сущность + /// + public EntityArmoredTransport? EntityArmoredTransport { get; protected set; } + /// + /// Ширина окна + /// + private int _pictureWidth; + /// + /// Высота окна + /// + private int _pictureHeight; + /// + /// Левая координата прорисовки автомобиля + /// + protected int _startPosX; + /// + /// Верхняя кооридната прорисовки автомобиля + /// + protected int _startPosY; + /// + /// Ширина прорисовки автомобиля + /// + protected readonly int _transportWidth = 200; + /// + /// Высота прорисовки автомобиля + /// + protected readonly int _transportHeight = 80; + /// + /// Координата X объекта + /// + public int GetPosX => _startPosX; + /// + /// Координата Y объекта + /// + public int GetPosY => _startPosY; + /// + /// Ширина объекта + /// + public int GetWidth => _transportWidth; + /// + /// Высота объекта + /// + public int GetHeight => _transportHeight; + /// + /// Конструктор + /// + /// Скорость + /// Вес + /// Основной цвет + /// Ширина картинки + /// Высота картинки + public DrawningArmoredTransport(int speed, double weight, Color bodyColor, int + width, int height) + { + _pictureWidth = width; + _pictureHeight = height; + if ((_pictureHeight > _transportHeight) && (_pictureWidth > _transportWidth)) + { + EntityArmoredTransport = new EntityArmoredTransport(speed, weight, bodyColor); + } + } + /// + /// Конструктор + /// + /// Скорость + /// Вес + /// Основной цвет + /// Ширина картинки + /// Высота картинки + /// Ширина прорисовки автомобиля + /// Высота прорисовки автомобиля + protected DrawningArmoredTransport(int speed, double weight, Color bodyColor, int + width, int height, int transportWidth, int transportHeight) + { + _pictureWidth = width; + _pictureHeight = height; + _transportWidth = transportWidth; + _transportHeight = transportHeight; + if ((_pictureHeight > _transportHeight) && (_pictureWidth > _transportWidth)) + { + EntityArmoredTransport = new EntityArmoredTransport(speed, weight, bodyColor); + } + } + /// + /// Установка позиции + /// + /// Координата X + /// Координата Y + public void SetPosition(int x, int y) + { + if ((x < 0 || y < 0) || (x + _transportWidth > _pictureWidth || y + _transportHeight > _pictureHeight)) + { + _startPosX = 0; + _startPosY = 0; + } + else + { + _startPosX = x; + _startPosY = y; + } + } + /// + /// Прорисовка объекта + /// + /// + public virtual void DrawTransport(Graphics g) + { + if (EntityArmoredTransport == null) + { + return; + } + Pen pen = new(Color.Black, 3); + Pen penGray = new(Color.Gray, 4); + Brush grayColorBrush = new SolidBrush(Color.Gray); + Brush blackColorBrush = new SolidBrush(Color.Black); + Brush bodyBrush = new SolidBrush(EntityArmoredTransport.BodyColor); + + + // Границы автомобиля + // гусеницы + g.DrawEllipse(pen, _startPosX + 14, _startPosY + 44, 151, 31); + g.FillEllipse(blackColorBrush, _startPosX + 15, _startPosY + 45, 150, 30); + g.DrawEllipse(penGray, _startPosX + 24, _startPosY + 54, 10, 10); + g.DrawEllipse(penGray, _startPosX + 144, _startPosY + 54, 10, 10); + g.DrawEllipse(penGray, _startPosX + 44, _startPosY + 59, 10, 10); + g.DrawEllipse(penGray, _startPosX + 124, _startPosY + 59, 10, 10); + g.DrawEllipse(penGray, _startPosX + 64, _startPosY + 61, 10, 10); + g.DrawEllipse(penGray, _startPosX + 104, _startPosY + 61, 10, 10); + g.DrawEllipse(penGray, _startPosX + 84, _startPosY + 62, 10, 10); + // Кузов + g.DrawRectangle(pen, _startPosX + 19, _startPosY + 34, 141, 21); + g.FillRectangle(bodyBrush, _startPosX + 20, _startPosY + 35, 140, 20); + + // Башня + g.FillRectangle(blackColorBrush, _startPosX + 75, _startPosY + 10, 25, 5); + g.DrawRectangle(pen, _startPosX + 64, _startPosY + 14, 66, 21); + g.FillRectangle(bodyBrush, _startPosX + 65, _startPosY + 15, 65, 20); //*** -> bodyBrush <- additionalBrush + + //Точки для отрисовки передней и задней части + Point pointFirstBackSide = new Point(_startPosX + 18, _startPosY + 35); + Point pointSecondBackSide = new Point(_startPosX + 18, _startPosY + 55); + Point pointThirdBackSide = new Point(_startPosX + 0, _startPosY + 55); + Point pointFirstFrontSide = new Point(_startPosX + 162, _startPosY + 35); + Point pointSecondFrontSide = new Point(_startPosX + 185, _startPosY + 55); + Point pointThirdFrontSide = new Point(_startPosX + 162, _startPosY + 55); + + // Задняя часть + Point[] backSide = { pointFirstBackSide, pointSecondBackSide, pointThirdBackSide }; + g.DrawPolygon(pen, backSide); + g.FillPolygon(bodyBrush, backSide); + // Передняя часть + Point[] frontSide = { pointFirstFrontSide, pointSecondFrontSide, pointThirdFrontSide }; + g.DrawPolygon(pen, frontSide); + g.FillPolygon(bodyBrush, frontSide); + } + /// + /// Проверка, что объект может переместится по указанному направлению + /// + /// Направление + /// true - можно переместится по указанномунаправлению + public bool CanMove(DirectionType direction) + { + if (EntityArmoredTransport == null) + { + return false; + } + return direction switch + { + //влево + DirectionType.Left => _startPosX - EntityArmoredTransport.Step > 0, + //вверх + DirectionType.Up => _startPosY - EntityArmoredTransport.Step > 0, + // вправо + DirectionType.Right => _startPosX + _transportWidth + EntityArmoredTransport.Step < _pictureWidth, + //вниз + DirectionType.Down => _startPosY + _transportHeight + EntityArmoredTransport.Step < _pictureHeight, + _ => false, + }; + } + /// + /// Изменение направления перемещения + /// + /// Направление + public void MoveTransport(DirectionType direction) + { + if (!CanMove(direction) || EntityArmoredTransport == null) + { + return; + } + switch (direction) + { + //влево + case DirectionType.Left: + _startPosX -= (int)EntityArmoredTransport.Step; + break; + //вверх + case DirectionType.Up: + _startPosY -= (int)EntityArmoredTransport.Step; + break; + // вправо + case DirectionType.Right: + _startPosX += (int)EntityArmoredTransport.Step; + break; + //вниз + case DirectionType.Down: + _startPosY += (int)EntityArmoredTransport.Step; + break; + } + } + } +} diff --git a/ProjectTank/ProjectTank/DrawningObjectArmoredTransport.cs b/ProjectTank/ProjectTank/DrawningObjectArmoredTransport.cs new file mode 100644 index 0000000..fe3b14d --- /dev/null +++ b/ProjectTank/ProjectTank/DrawningObjectArmoredTransport.cs @@ -0,0 +1,37 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using ProjectTank.DrawningObjects; + +namespace ProjectTank.MovementStrategy +{ + /// + /// Реализация интерфейса IDrawningObject для работы с объектом DrawningCar(паттерн Adapter) +/// + public class DrawningObjectArmoredTransport : IMoveableObject + { + private readonly DrawningArmoredTransport? _drawningArmoredTransport = null; + public DrawningObjectArmoredTransport(DrawningArmoredTransport drawningArmoredTransport) + { + _drawningArmoredTransport = drawningArmoredTransport; + } + public ObjectParameters? GetObjectPosition + { + get + { + if (_drawningArmoredTransport == null || _drawningArmoredTransport.EntityArmoredTransport == + null) + { + return null; + } + return new ObjectParameters(_drawningArmoredTransport.GetPosX, + _drawningArmoredTransport.GetPosY, _drawningArmoredTransport.GetWidth, _drawningArmoredTransport.GetHeight); + } + } + public int GetStep => (int)(_drawningArmoredTransport?.EntityArmoredTransport?.Step ?? 0); + public bool CheckCanMove(DirectionType direction) =>_drawningArmoredTransport?.CanMove(direction) ?? false; + public void MoveObject(DirectionType direction) =>_drawningArmoredTransport?.MoveTransport(direction); + } +} \ No newline at end of file diff --git a/ProjectTank/ProjectTank/DrawningTank.cs b/ProjectTank/ProjectTank/DrawningTank.cs index aa1d415..f3e90cf 100644 --- a/ProjectTank/ProjectTank/DrawningTank.cs +++ b/ProjectTank/ProjectTank/DrawningTank.cs @@ -1,182 +1,51 @@ using System; using System.Collections.Generic; +using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; +using ProjectTank.Entities; -namespace ProjectTank +namespace ProjectTank.DrawningObjects { - public class DrawningTank + /// + /// Класс, отвечающий за прорисовку и перемещение объекта-сущности + /// + public class DrawningTank : DrawningArmoredTransport { /// - /// Класс-сущность - /// - public EntityTank? _EntityTank { get; private set; } - /// - /// Ширина окна - /// - private int _pictureWidth; - /// - /// Высота окна - /// - private int _pictureHeight; - /// - /// Левая координата прорисовки танка - /// - private int _startPosX; - /// - /// Верхняя кооридната прорисовки танка - /// - private int _startPosY; - /// - /// Ширина прорисовки танка - /// - private readonly int _tankWidth = 200; - /// - /// Высота прорисовки танка - /// - private readonly int _tankHeight = 80; - /// - /// Инициализация свойств + /// Конструктор /// /// Скорость /// Вес - /// Цвет кузова + /// Основной цвет /// Дополнительный цвет + /// Пушка + /// Пулемет /// Ширина картинки /// Высота картинки - /// true - объект создан, false - проверка не пройдена,нельзя создать объект в этих размерах - public bool Init(int speed, double weight, Color bodyColor, Color additionalColor, - bool gun, bool machineGun, int width, int height) + public DrawningTank(int speed, double weight, Color bodyColor, Color + additionalColor, bool gun, bool machineGun, int width, int height) : + base(speed, weight, bodyColor, width, height, 200, 80) { - _pictureWidth = width; - _pictureHeight = height; - - if ((_pictureHeight < _tankHeight) || (_pictureWidth < _tankWidth)) + if (EntityArmoredTransport != null) { - return false; - } - _EntityTank = new EntityTank(); - _EntityTank.Init(speed, weight, gun, machineGun, bodyColor, additionalColor); - return true; - } - /// - /// Установка позиции - /// - /// Координата X - /// Координата Y - public void SetPosition(int x, int y) - { - if ((x < 0 || y < 0) || (x + _tankWidth > _pictureWidth || y + _tankHeight > _pictureHeight)) - { - _startPosX = 0; - _startPosY = 0; - } - else - { - _startPosX = x; - _startPosY = y; + EntityArmoredTransport = new EntityTank(speed, weight, bodyColor, + additionalColor, gun, machineGun); } } - /// - /// Изменение направления перемещения - /// - /// Направление - public void MoveTransport(DirectionType direction) + public override void DrawTransport(Graphics g) { - if (_EntityTank == null) - { - return; - } - switch (direction) - { - //влево - case DirectionType.Left: - if (_startPosX - _EntityTank.Step > 0) - { - _startPosX -= (int)_EntityTank.Step; - } - break; - //вверх - case DirectionType.Up: - if (_startPosY - _EntityTank.Step > 0) - { - _startPosY -= (int)_EntityTank.Step; - } - break; - //вправо - case DirectionType.Right: - if (_startPosX + _tankWidth + _EntityTank.Step < _pictureWidth) - { - _startPosX += (int)_EntityTank.Step; - } - break; - //вниз - case DirectionType.Down: - if (_startPosY + _tankHeight + _EntityTank.Step < _pictureHeight) - { - _startPosY += (int)_EntityTank.Step; - } - break; - } - } - /// - /// Прорисовка объекта - /// - /// - public void DrawTransport(Graphics g) - { - if (_EntityTank == null) + if (EntityArmoredTransport is not EntityTank tank) { return; } Pen pen = new(Color.Black, 3); - Pen penGray = new(Color.Gray, 4); - Brush grayColorBrush = new SolidBrush(Color.Gray); Brush blackColorBrush = new SolidBrush(Color.Black); - Brush additionalBrush = new SolidBrush(_EntityTank.AdditionalColor); - Brush bodyBrush = new SolidBrush(_EntityTank.BodyColor); - - // Границы автомобиля - // гусеницы - g.DrawEllipse(pen, _startPosX+14, _startPosY+44, 151, 31); - g.FillEllipse(blackColorBrush, _startPosX + 15, _startPosY + 45, 150, 30); - g.DrawEllipse(penGray, _startPosX + 24, _startPosY + 54, 10, 10); - g.DrawEllipse(penGray, _startPosX + 144, _startPosY + 54, 10, 10); - g.DrawEllipse(penGray, _startPosX + 44, _startPosY + 59, 10, 10); - g.DrawEllipse(penGray, _startPosX + 124, _startPosY + 59, 10, 10); - g.DrawEllipse(penGray, _startPosX + 64, _startPosY + 61, 10, 10); - g.DrawEllipse(penGray, _startPosX + 104, _startPosY + 61, 10, 10); - g.DrawEllipse(penGray, _startPosX + 84, _startPosY + 62, 10, 10); - // Кузов - g.DrawRectangle(pen, _startPosX + 19, _startPosY + 34, 141, 21); - g.FillRectangle(bodyBrush, _startPosX+20, _startPosY+35, 140, 20); - - // Башня - g.FillRectangle(blackColorBrush, _startPosX + 75, _startPosY + 10, 25, 5); - g.DrawRectangle(pen, _startPosX + 64, _startPosY + 14, 66, 21); - g.FillRectangle(additionalBrush, _startPosX + 65, _startPosY + 15, 65, 20); - - //Точки для отрисовки передней и задней части - Point pointFirstBackSide = new Point(_startPosX + 18, _startPosY + 35); - Point pointSecondBackSide = new Point(_startPosX + 18, _startPosY + 55); - Point pointThirdBackSide = new Point(_startPosX+0, _startPosY+55); - Point pointFirstFrontSide = new Point(_startPosX + 162, _startPosY + 35); - Point pointSecondFrontSide = new Point(_startPosX + 185, _startPosY + 55); - Point pointThirdFrontSide = new Point(_startPosX + 162, _startPosY + 55); - - // Задняя часть - Point[] backSide = { pointFirstBackSide, pointSecondBackSide, pointThirdBackSide }; - g.DrawPolygon(pen, backSide); - g.FillPolygon(bodyBrush, backSide); - // Передняя часть - Point[] frontSide ={ pointFirstFrontSide, pointSecondFrontSide, pointThirdFrontSide }; - g.DrawPolygon(pen, frontSide); - g.FillPolygon(bodyBrush, frontSide); - + Brush additionalBrush = new SolidBrush(tank.AdditionalColor); // пушка - if (_EntityTank.Gun) + if (tank.Gun) { g.DrawRectangle(pen, _startPosX + 129, _startPosY + 19, 56, 6); g.FillRectangle(additionalBrush, _startPosX + 130, _startPosY + 20, 55, 5); @@ -184,7 +53,7 @@ namespace ProjectTank g.FillRectangle(blackColorBrush, _startPosX + 185, _startPosY + 18, 15, 10); } // пулемет - if (_EntityTank.MachineGun) + if (tank.MachineGun) { g.DrawRectangle(pen, _startPosX + 104, _startPosY + 9, 16, 6); g.FillRectangle(additionalBrush, _startPosX + 105, _startPosY + 10, 15, 5); @@ -195,6 +64,7 @@ namespace ProjectTank g.FillRectangle(additionalBrush, _startPosX + 105, _startPosY + 3, 29, 3); g.FillRectangle(blackColorBrush, _startPosX + 135, _startPosY, 15, 8); } + base.DrawTransport(g); } } } diff --git a/ProjectTank/ProjectTank/EntityArmoredTransport.cs b/ProjectTank/ProjectTank/EntityArmoredTransport.cs new file mode 100644 index 0000000..bf3b54a --- /dev/null +++ b/ProjectTank/ProjectTank/EntityArmoredTransport.cs @@ -0,0 +1,43 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectTank.Entities +{ + /// + /// Класс-сущность "Бронирванный транспорт" + /// + public class EntityArmoredTransport + { + /// + /// Скорость + /// + 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 EntityArmoredTransport(int speed, double weight, Color bodyColor) + { + Speed = speed; + Weight = weight; + BodyColor = bodyColor; + } + } +} diff --git a/ProjectTank/ProjectTank/EntityTank.cs b/ProjectTank/ProjectTank/EntityTank.cs index 3834940..fcde423 100644 --- a/ProjectTank/ProjectTank/EntityTank.cs +++ b/ProjectTank/ProjectTank/EntityTank.cs @@ -1,22 +1,21 @@ -using System; +using ProjectTank.Entities; +using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; -namespace ProjectTank +namespace ProjectTank.Entities { - public class EntityTank + /// + /// Класс-сущность "Танк" + /// + public class EntityTank : EntityArmoredTransport { /// - /// Скорость + /// Дополнительный цвет (для опциональных элементов) /// - public int Speed { get; private set; } - - /// - /// Вес - /// - public double Weight { get; private set; } + public Color AdditionalColor { get; private set; } /// /// Пушка @@ -28,21 +27,6 @@ namespace ProjectTank /// public bool MachineGun { get; private set; } - /// - /// Основной цвет - /// - public Color BodyColor { get; private set; } - - /// - /// Дополнительный цвет (для опциональных элементов) - /// - public Color AdditionalColor { get; private set; } - - /// - /// Расчет шага по карте - /// - public double Step => (double)Speed * 100 / Weight; - /// /// Инициализация полей объекта-класса спортивного автомобиля /// @@ -52,13 +36,9 @@ namespace ProjectTank /// Дополнительный цвет /// Признак наличия пушки /// Признак наличия пулемета - /// - public void Init(int speed, double weight, bool gun, - bool machineGun, Color bodyColor, Color additionalColor) + public EntityTank(int speed, double weight, Color bodyColor, Color additionalColor, bool gun, + bool machineGun) : base(speed, weight, bodyColor) { - Speed = speed; - Weight = weight; - BodyColor = bodyColor; AdditionalColor = additionalColor; Gun = gun; MachineGun = machineGun; diff --git a/ProjectTank/ProjectTank/IMoveableObject.cs b/ProjectTank/ProjectTank/IMoveableObject.cs new file mode 100644 index 0000000..8403d7a --- /dev/null +++ b/ProjectTank/ProjectTank/IMoveableObject.cs @@ -0,0 +1,35 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using ProjectTank.DrawningObjects; + +namespace ProjectTank.MovementStrategy +{ + /// + /// Интерфейс для работы с перемещаемым объектом + /// + public interface IMoveableObject + { + /// + /// Получение координаты X объекта + /// + ObjectParameters? GetObjectPosition { get; } + /// + /// Шаг объекта + /// + int GetStep { get; } + /// + /// Проверка, можно ли переместиться по нужному направлению + /// + /// + /// + bool CheckCanMove(DirectionType direction); + /// + /// Изменение направления пермещения объекта + /// + /// Направление + void MoveObject(DirectionType direction); + } +} diff --git a/ProjectTank/ProjectTank/MoveToBorder.cs b/ProjectTank/ProjectTank/MoveToBorder.cs new file mode 100644 index 0000000..9079642 --- /dev/null +++ b/ProjectTank/ProjectTank/MoveToBorder.cs @@ -0,0 +1,45 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectTank.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()) + { + MoveRight(); + } + var diffY = objParams.DownBorder - FieldHeight; + if (Math.Abs(diffY) > GetStep()) + { + MoveDown(); + } + } + } +} diff --git a/ProjectTank/ProjectTank/MoveToCenter.cs b/ProjectTank/ProjectTank/MoveToCenter.cs new file mode 100644 index 0000000..32a10cc --- /dev/null +++ b/ProjectTank/ProjectTank/MoveToCenter.cs @@ -0,0 +1,59 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectTank.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/ProjectTank/ProjectTank/ObjectParameters.cs b/ProjectTank/ProjectTank/ObjectParameters.cs new file mode 100644 index 0000000..14e4f3d --- /dev/null +++ b/ProjectTank/ProjectTank/ObjectParameters.cs @@ -0,0 +1,57 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectTank.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/ProjectTank/ProjectTank/Status.cs b/ProjectTank/ProjectTank/Status.cs new file mode 100644 index 0000000..346c4dc --- /dev/null +++ b/ProjectTank/ProjectTank/Status.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectTank.MovementStrategy +{ + /// + /// Статус выполнения операции перемещения + /// + public enum Status + { + NotInit, + InProgress, + Finish + } +} diff --git a/ProjectTank/ProjectTank/TankForm.Designer.cs b/ProjectTank/ProjectTank/TankForm.Designer.cs index 9340b66..dcc809e 100644 --- a/ProjectTank/ProjectTank/TankForm.Designer.cs +++ b/ProjectTank/ProjectTank/TankForm.Designer.cs @@ -35,6 +35,9 @@ this.buttonLeft = new System.Windows.Forms.Button(); this.buttonDown = new System.Windows.Forms.Button(); this.label1 = new System.Windows.Forms.Label(); + this.comboBoxStrategy = new System.Windows.Forms.ComboBox(); + this.ButtonCreateArmoredTransport = new System.Windows.Forms.Button(); + this.ButtonStep = new System.Windows.Forms.Button(); ((System.ComponentModel.ISupportInitialize)(this.pictureBoxTanks)).BeginInit(); this.SuspendLayout(); // @@ -42,7 +45,7 @@ // this.pictureBoxTanks.Dock = System.Windows.Forms.DockStyle.Fill; this.pictureBoxTanks.Location = new System.Drawing.Point(0, 0); - this.pictureBoxTanks.Margin = new System.Windows.Forms.Padding(6, 6, 6, 6); + this.pictureBoxTanks.Margin = new System.Windows.Forms.Padding(6); this.pictureBoxTanks.Name = "pictureBoxTanks"; this.pictureBoxTanks.Size = new System.Drawing.Size(1445, 881); this.pictureBoxTanks.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize; @@ -53,11 +56,11 @@ // this.ButtonCreateTank.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); this.ButtonCreateTank.Location = new System.Drawing.Point(22, 806); - this.ButtonCreateTank.Margin = new System.Windows.Forms.Padding(6, 6, 6, 6); + this.ButtonCreateTank.Margin = new System.Windows.Forms.Padding(6); this.ButtonCreateTank.Name = "ButtonCreateTank"; - this.ButtonCreateTank.Size = new System.Drawing.Size(139, 49); + this.ButtonCreateTank.Size = new System.Drawing.Size(192, 49); this.ButtonCreateTank.TabIndex = 1; - this.ButtonCreateTank.Text = "Создать"; + this.ButtonCreateTank.Text = "Создать Танк"; this.ButtonCreateTank.UseVisualStyleBackColor = true; this.ButtonCreateTank.Click += new System.EventHandler(this.ButtonCreateTank_Click); // @@ -67,7 +70,7 @@ this.buttonUp.BackgroundImage = global::ProjectTank.Properties.Resources.btnUp; this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom; this.buttonUp.Location = new System.Drawing.Point(1300, 730); - this.buttonUp.Margin = new System.Windows.Forms.Padding(6, 6, 6, 6); + this.buttonUp.Margin = new System.Windows.Forms.Padding(6); this.buttonUp.Name = "buttonUp"; this.buttonUp.Size = new System.Drawing.Size(56, 64); this.buttonUp.TabIndex = 2; @@ -80,7 +83,7 @@ this.buttonRight.BackgroundImage = global::ProjectTank.Properties.Resources.btnRight; this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom; this.buttonRight.Location = new System.Drawing.Point(1367, 806); - this.buttonRight.Margin = new System.Windows.Forms.Padding(6, 6, 6, 6); + this.buttonRight.Margin = new System.Windows.Forms.Padding(6); this.buttonRight.Name = "buttonRight"; this.buttonRight.Size = new System.Drawing.Size(56, 64); this.buttonRight.TabIndex = 3; @@ -93,7 +96,7 @@ this.buttonLeft.BackgroundImage = global::ProjectTank.Properties.Resources.btnLeft; this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom; this.buttonLeft.Location = new System.Drawing.Point(1233, 806); - this.buttonLeft.Margin = new System.Windows.Forms.Padding(6, 6, 6, 6); + this.buttonLeft.Margin = new System.Windows.Forms.Padding(6); this.buttonLeft.Name = "buttonLeft"; this.buttonLeft.Size = new System.Drawing.Size(56, 64); this.buttonLeft.TabIndex = 4; @@ -106,7 +109,7 @@ this.buttonDown.BackgroundImage = global::ProjectTank.Properties.Resources.btnDown; this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom; this.buttonDown.Location = new System.Drawing.Point(1300, 806); - this.buttonDown.Margin = new System.Windows.Forms.Padding(6, 6, 6, 6); + this.buttonDown.Margin = new System.Windows.Forms.Padding(6); this.buttonDown.Name = "buttonDown"; this.buttonDown.Size = new System.Drawing.Size(56, 64); this.buttonDown.TabIndex = 5; @@ -122,11 +125,51 @@ this.label1.Size = new System.Drawing.Size(0, 32); this.label1.TabIndex = 6; // + // comboBoxStrategy + // + this.comboBoxStrategy.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); + this.comboBoxStrategy.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; + this.comboBoxStrategy.FormattingEnabled = true; + this.comboBoxStrategy.Items.AddRange(new object[] { + "0", + "1"}); + this.comboBoxStrategy.Location = new System.Drawing.Point(1181, 12); + this.comboBoxStrategy.Name = "comboBoxStrategy"; + this.comboBoxStrategy.Size = new System.Drawing.Size(242, 40); + this.comboBoxStrategy.TabIndex = 7; + // + // ButtonCreateArmoredTransport + // + this.ButtonCreateArmoredTransport.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); + this.ButtonCreateArmoredTransport.Location = new System.Drawing.Point(241, 806); + this.ButtonCreateArmoredTransport.Margin = new System.Windows.Forms.Padding(6); + this.ButtonCreateArmoredTransport.Name = "ButtonCreateArmoredTransport"; + this.ButtonCreateArmoredTransport.Size = new System.Drawing.Size(401, 49); + this.ButtonCreateArmoredTransport.TabIndex = 8; + this.ButtonCreateArmoredTransport.Text = "Создать Бронированную машину"; + this.ButtonCreateArmoredTransport.UseVisualStyleBackColor = true; + this.ButtonCreateArmoredTransport.Click += new System.EventHandler(this.ButtonCreateArmoredTransport_Click); + // + // ButtonStep + // + this.ButtonStep.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); + this.ButtonStep.Location = new System.Drawing.Point(1263, 61); + this.ButtonStep.Margin = new System.Windows.Forms.Padding(6); + this.ButtonStep.Name = "ButtonStep"; + this.ButtonStep.Size = new System.Drawing.Size(160, 49); + this.ButtonStep.TabIndex = 9; + this.ButtonStep.Text = "Шаг"; + this.ButtonStep.UseVisualStyleBackColor = true; + this.ButtonStep.Click += new System.EventHandler(this.ButtonStep_Click); + // // TankForm // this.AutoScaleDimensions = new System.Drawing.SizeF(13F, 32F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(1445, 881); + this.Controls.Add(this.ButtonStep); + this.Controls.Add(this.ButtonCreateArmoredTransport); + this.Controls.Add(this.comboBoxStrategy); this.Controls.Add(this.label1); this.Controls.Add(this.buttonDown); this.Controls.Add(this.buttonLeft); @@ -134,7 +177,7 @@ this.Controls.Add(this.buttonUp); this.Controls.Add(this.ButtonCreateTank); this.Controls.Add(this.pictureBoxTanks); - this.Margin = new System.Windows.Forms.Padding(6, 6, 6, 6); + this.Margin = new System.Windows.Forms.Padding(6); this.Name = "TankForm"; this.Text = "Tank"; ((System.ComponentModel.ISupportInitialize)(this.pictureBoxTanks)).EndInit(); @@ -151,5 +194,8 @@ private Button buttonDown; public PictureBox pictureBoxTanks; private Label label1; + private ComboBox comboBoxStrategy; + private Button ButtonCreateArmoredTransport; + private Button ButtonStep; } } \ No newline at end of file diff --git a/ProjectTank/ProjectTank/TankForm.cs b/ProjectTank/ProjectTank/TankForm.cs index 72273a3..c4bfa90 100644 --- a/ProjectTank/ProjectTank/TankForm.cs +++ b/ProjectTank/ProjectTank/TankForm.cs @@ -1,4 +1,6 @@ using System.Reflection.Emit; +using ProjectTank.DrawningObjects; +using ProjectTank.MovementStrategy; namespace ProjectTank { @@ -10,7 +12,11 @@ namespace ProjectTank /// /// - /// - private DrawningTank? _drawningTank; + private DrawningArmoredTransport? _drawningArmoredTransport; + /// + /// + /// + private AbstractStrategy? _abstractStrategy; /// /// /// @@ -23,13 +29,13 @@ namespace ProjectTank /// private void Draw() { - if (_drawningTank == null) + if (_drawningArmoredTransport == null) { return; } Bitmap bmp = new(pictureBoxTanks.Width, pictureBoxTanks.Height); Graphics gr = Graphics.FromImage(bmp); - _drawningTank.DrawTransport(gr); + _drawningArmoredTransport.DrawTransport(gr); pictureBoxTanks.Image = bmp; } /// @@ -40,12 +46,27 @@ namespace ProjectTank private void ButtonCreateTank_Click(object sender, EventArgs e) { Random random = new(); - _drawningTank = new DrawningTank(); - _drawningTank.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)), - pictureBoxTanks.Width, pictureBoxTanks.Height); - _drawningTank.SetPosition(random.Next(0, 100), random.Next(0, 100)); + _drawningArmoredTransport = new DrawningTank(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)), + pictureBoxTanks.Width, pictureBoxTanks.Height); + _drawningArmoredTransport.SetPosition(random.Next(0, 100), random.Next(0, 100)); + Draw(); + } + /// + /// " " + /// + /// + /// + private void ButtonCreateArmoredTransport_Click(object sender, EventArgs e) + { + Random random = new(); + _drawningArmoredTransport = new DrawningArmoredTransport(random.Next(100, 300),random.Next(1000, 3000), + Color.FromArgb(random.Next(0, 256), random.Next(0, 256), + random.Next(0, 256)), + pictureBoxTanks.Width, pictureBoxTanks.Height); + _drawningArmoredTransport.SetPosition(random.Next(10, 100), random.Next(10,100)); Draw(); } /// @@ -55,7 +76,7 @@ namespace ProjectTank /// private void ButtonMove_Click(object sender, EventArgs e) { - if (_drawningTank == null) + if (_drawningArmoredTransport == null) { return; } @@ -63,19 +84,59 @@ namespace ProjectTank switch (name) { case "buttonUp": - _drawningTank.MoveTransport(DirectionType.Up); + _drawningArmoredTransport.MoveTransport(DirectionType.Up); break; case "buttonDown": - _drawningTank.MoveTransport(DirectionType.Down); + _drawningArmoredTransport.MoveTransport(DirectionType.Down); break; case "buttonLeft": - _drawningTank.MoveTransport(DirectionType.Left); + _drawningArmoredTransport.MoveTransport(DirectionType.Left); break; case "buttonRight": - _drawningTank.MoveTransport(DirectionType.Right); + _drawningArmoredTransport.MoveTransport(DirectionType.Right); break; } Draw(); } + /// + /// "" + /// + /// + /// + private void ButtonStep_Click(object sender, EventArgs e) + { + if (_drawningArmoredTransport == null) + { + return; + } + if (comboBoxStrategy.Enabled) + { + _abstractStrategy = comboBoxStrategy.SelectedIndex + switch + { + 0 => new MoveToCenter(), + 1 => new MoveToBorder(), + _ => null, + }; + if (_abstractStrategy == null) + { + return; + } + _abstractStrategy.SetData(new DrawningObjectArmoredTransport(_drawningArmoredTransport), + pictureBoxTanks.Width, pictureBoxTanks.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 -- 2.25.1 From 04570866d92c4b058f1b3ef30f292009cc82bc38 Mon Sep 17 00:00:00 2001 From: vladimir_zinovev Date: Fri, 6 Oct 2023 11:52:16 +0400 Subject: [PATCH 2/3] lab2 is done with amendment --- ProjectTank/ProjectTank/DrawningArmoredTransport.cs | 5 +---- ProjectTank/ProjectTank/DrawningObjectArmoredTransport.cs | 2 +- ProjectTank/ProjectTank/DrawningTank.cs | 5 ++--- ProjectTank/ProjectTank/EntityTank.cs | 3 --- 4 files changed, 4 insertions(+), 11 deletions(-) diff --git a/ProjectTank/ProjectTank/DrawningArmoredTransport.cs b/ProjectTank/ProjectTank/DrawningArmoredTransport.cs index 4394b3d..3effdab 100644 --- a/ProjectTank/ProjectTank/DrawningArmoredTransport.cs +++ b/ProjectTank/ProjectTank/DrawningArmoredTransport.cs @@ -130,7 +130,6 @@ namespace ProjectTank.DrawningObjects Brush blackColorBrush = new SolidBrush(Color.Black); Brush bodyBrush = new SolidBrush(EntityArmoredTransport.BodyColor); - // Границы автомобиля // гусеницы g.DrawEllipse(pen, _startPosX + 14, _startPosY + 44, 151, 31); @@ -145,11 +144,10 @@ namespace ProjectTank.DrawningObjects // Кузов g.DrawRectangle(pen, _startPosX + 19, _startPosY + 34, 141, 21); g.FillRectangle(bodyBrush, _startPosX + 20, _startPosY + 35, 140, 20); - // Башня g.FillRectangle(blackColorBrush, _startPosX + 75, _startPosY + 10, 25, 5); g.DrawRectangle(pen, _startPosX + 64, _startPosY + 14, 66, 21); - g.FillRectangle(bodyBrush, _startPosX + 65, _startPosY + 15, 65, 20); //*** -> bodyBrush <- additionalBrush + g.FillRectangle(bodyBrush, _startPosX + 65, _startPosY + 15, 65, 20); //Точки для отрисовки передней и задней части Point pointFirstBackSide = new Point(_startPosX + 18, _startPosY + 35); @@ -158,7 +156,6 @@ namespace ProjectTank.DrawningObjects Point pointFirstFrontSide = new Point(_startPosX + 162, _startPosY + 35); Point pointSecondFrontSide = new Point(_startPosX + 185, _startPosY + 55); Point pointThirdFrontSide = new Point(_startPosX + 162, _startPosY + 55); - // Задняя часть Point[] backSide = { pointFirstBackSide, pointSecondBackSide, pointThirdBackSide }; g.DrawPolygon(pen, backSide); diff --git a/ProjectTank/ProjectTank/DrawningObjectArmoredTransport.cs b/ProjectTank/ProjectTank/DrawningObjectArmoredTransport.cs index fe3b14d..f8198ab 100644 --- a/ProjectTank/ProjectTank/DrawningObjectArmoredTransport.cs +++ b/ProjectTank/ProjectTank/DrawningObjectArmoredTransport.cs @@ -9,7 +9,7 @@ namespace ProjectTank.MovementStrategy { /// /// Реализация интерфейса IDrawningObject для работы с объектом DrawningCar(паттерн Adapter) -/// + /// public class DrawningObjectArmoredTransport : IMoveableObject { private readonly DrawningArmoredTransport? _drawningArmoredTransport = null; diff --git a/ProjectTank/ProjectTank/DrawningTank.cs b/ProjectTank/ProjectTank/DrawningTank.cs index f3e90cf..2b85acc 100644 --- a/ProjectTank/ProjectTank/DrawningTank.cs +++ b/ProjectTank/ProjectTank/DrawningTank.cs @@ -26,13 +26,12 @@ namespace ProjectTank.DrawningObjects /// Ширина картинки /// Высота картинки public DrawningTank(int speed, double weight, Color bodyColor, Color - additionalColor, bool gun, bool machineGun, int width, int height) : - base(speed, weight, bodyColor, width, height, 200, 80) + additionalColor, bool gun, bool machineGun, int width, int height) : base(speed,weight, bodyColor, width, height, 200, 80) { if (EntityArmoredTransport != null) { EntityArmoredTransport = new EntityTank(speed, weight, bodyColor, - additionalColor, gun, machineGun); + additionalColor, gun, machineGun); } } public override void DrawTransport(Graphics g) diff --git a/ProjectTank/ProjectTank/EntityTank.cs b/ProjectTank/ProjectTank/EntityTank.cs index fcde423..136aca2 100644 --- a/ProjectTank/ProjectTank/EntityTank.cs +++ b/ProjectTank/ProjectTank/EntityTank.cs @@ -16,17 +16,14 @@ namespace ProjectTank.Entities /// Дополнительный цвет (для опциональных элементов) /// public Color AdditionalColor { get; private set; } - /// /// Пушка /// public bool Gun { get; private set; } - /// /// Пулемет /// public bool MachineGun { get; private set; } - /// /// Инициализация полей объекта-класса спортивного автомобиля /// -- 2.25.1 From 803043d6b8222a37ef7388d6297033ac1337d558 Mon Sep 17 00:00:00 2001 From: vladimir_zinovev Date: Thu, 19 Oct 2023 20:58:56 +0400 Subject: [PATCH 3/3] Edited from first lab --- .../{Direction.cs => DirectionType.cs} | 56 +++++++++---------- .../ProjectTank/DrawningArmoredTransport.cs | 32 +++++------ .../DrawningObjectArmoredTransport.cs | 7 +-- 3 files changed, 47 insertions(+), 48 deletions(-) rename ProjectTank/ProjectTank/{Direction.cs => DirectionType.cs} (94%) diff --git a/ProjectTank/ProjectTank/Direction.cs b/ProjectTank/ProjectTank/DirectionType.cs similarity index 94% rename from ProjectTank/ProjectTank/Direction.cs rename to ProjectTank/ProjectTank/DirectionType.cs index 303cf57..7e6a2b7 100644 --- a/ProjectTank/ProjectTank/Direction.cs +++ b/ProjectTank/ProjectTank/DirectionType.cs @@ -1,28 +1,28 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace ProjectTank -{ - public enum DirectionType - { - /// - /// Вверх - /// - Up = 1, - /// - /// Вниз - /// - Down = 2, - /// - /// Влево - /// - Left = 3, - /// - /// Вправо - /// - Right = 4 - } -} +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectTank +{ + public enum DirectionType + { + /// + /// Вверх + /// + Up = 1, + /// + /// Вниз + /// + Down = 2, + /// + /// Влево + /// + Left = 3, + /// + /// Вправо + /// + Right = 4 + } +} diff --git a/ProjectTank/ProjectTank/DrawningArmoredTransport.cs b/ProjectTank/ProjectTank/DrawningArmoredTransport.cs index 3effdab..ac81c1e 100644 --- a/ProjectTank/ProjectTank/DrawningArmoredTransport.cs +++ b/ProjectTank/ProjectTank/DrawningArmoredTransport.cs @@ -25,19 +25,19 @@ namespace ProjectTank.DrawningObjects /// private int _pictureHeight; /// - /// Левая координата прорисовки автомобиля + /// Левая координата прорисовки транспорта /// protected int _startPosX; /// - /// Верхняя кооридната прорисовки автомобиля + /// Верхняя кооридната прорисовки транспорта /// protected int _startPosY; /// - /// Ширина прорисовки автомобиля + /// Ширина прорисовки транспорта /// protected readonly int _transportWidth = 200; /// - /// Высота прорисовки автомобиля + /// Высота прорисовки транспорта /// protected readonly int _transportHeight = 80; /// @@ -65,14 +65,14 @@ namespace ProjectTank.DrawningObjects /// Ширина картинки /// Высота картинки public DrawningArmoredTransport(int speed, double weight, Color bodyColor, int - width, int height) + width, int height) { - _pictureWidth = width; - _pictureHeight = height; - if ((_pictureHeight > _transportHeight) && (_pictureWidth > _transportWidth)) + if ((height > _transportHeight) && (width > _transportWidth)) { EntityArmoredTransport = new EntityArmoredTransport(speed, weight, bodyColor); } + _pictureWidth = width; + _pictureHeight = height; } /// /// Конструктор @@ -82,19 +82,19 @@ namespace ProjectTank.DrawningObjects /// Основной цвет /// Ширина картинки /// Высота картинки - /// Ширина прорисовки автомобиля - /// Высота прорисовки автомобиля + /// Ширина прорисовки транспорта + /// Высота прорисовки транспорта protected DrawningArmoredTransport(int speed, double weight, Color bodyColor, int - width, int height, int transportWidth, int transportHeight) + width, int height, int transportWidth, int transportHeight) { + if ((height > transportHeight) && (width > transportWidth)) + { + EntityArmoredTransport = new EntityArmoredTransport(speed, weight, bodyColor); + } _pictureWidth = width; _pictureHeight = height; _transportWidth = transportWidth; _transportHeight = transportHeight; - if ((_pictureHeight > _transportHeight) && (_pictureWidth > _transportWidth)) - { - EntityArmoredTransport = new EntityArmoredTransport(speed, weight, bodyColor); - } } /// /// Установка позиции @@ -130,7 +130,7 @@ namespace ProjectTank.DrawningObjects Brush blackColorBrush = new SolidBrush(Color.Black); Brush bodyBrush = new SolidBrush(EntityArmoredTransport.BodyColor); - // Границы автомобиля + // Границы транспорта // гусеницы g.DrawEllipse(pen, _startPosX + 14, _startPosY + 44, 151, 31); g.FillEllipse(blackColorBrush, _startPosX + 15, _startPosY + 45, 150, 30); diff --git a/ProjectTank/ProjectTank/DrawningObjectArmoredTransport.cs b/ProjectTank/ProjectTank/DrawningObjectArmoredTransport.cs index f8198ab..e7a51c9 100644 --- a/ProjectTank/ProjectTank/DrawningObjectArmoredTransport.cs +++ b/ProjectTank/ProjectTank/DrawningObjectArmoredTransport.cs @@ -21,13 +21,12 @@ namespace ProjectTank.MovementStrategy { get { - if (_drawningArmoredTransport == null || _drawningArmoredTransport.EntityArmoredTransport == - null) + if (_drawningArmoredTransport == null || _drawningArmoredTransport.EntityArmoredTransport == null) { return null; } - return new ObjectParameters(_drawningArmoredTransport.GetPosX, - _drawningArmoredTransport.GetPosY, _drawningArmoredTransport.GetWidth, _drawningArmoredTransport.GetHeight); + return new ObjectParameters(_drawningArmoredTransport.GetPosX, _drawningArmoredTransport.GetPosY, + _drawningArmoredTransport.GetWidth, _drawningArmoredTransport.GetHeight); } } public int GetStep => (int)(_drawningArmoredTransport?.EntityArmoredTransport?.Step ?? 0); -- 2.25.1