From b8787908c3444fc4f1376298df40dfc293e07eb3 Mon Sep 17 00:00:00 2001 From: Ctepa Date: Mon, 26 Feb 2024 14:20:56 +0300 Subject: [PATCH] =?UTF-8?q?=D0=AD=D1=82=D0=BE=20=D0=BF=D0=BE=D0=B1=D0=B5?= =?UTF-8?q?=D0=B4=D0=B0!=20=202=20=D0=BB=D0=B0=D0=B1=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DrawningAntiAircrfatGun.cs | 210 ----------------- .../{ => Drawnings}/DirectionType.cs | 8 +- .../Drawnings/DrawningAntiAircrfatGun.cs | 53 +++++ .../AntiAircraftGun/Drawnings/DrawningGun.cs | 223 ++++++++++++++++++ .../Entities/EntityAntiAircraftGun.cs | 30 +++ .../AntiAircraftGun/Entities/EntityGun.cs | 42 ++++ .../AntiAircraftGun/EntityAntiAircraftGun.cs | 59 ----- .../FormAntiAircraftGun.Designer.cs | 47 +++- .../AntiAircraftGun/FormAntiAircraftGun.cs | 126 ++++++++-- .../MovementStrategy/AbstractStrategy.cs | 122 ++++++++++ .../MovementStrategy/IMoveableObject.cs | 27 +++ .../MovementStrategy/MoveToBorder.cs | 35 +++ .../MovementStrategy/MoveToCenter.cs | 57 +++++ .../MovementStrategy/MoveableGun.cs | 71 ++++++ .../MovementStrategy/MovementDirection.cs | 29 +++ .../MovementStrategy/ObjectParameters.cs | 67 ++++++ .../MovementStrategy/StrategyStatus.cs | 25 ++ 17 files changed, 932 insertions(+), 299 deletions(-) delete mode 100644 AntiAircraftGun/AntiAircraftGun/DrawningAntiAircrfatGun.cs rename AntiAircraftGun/AntiAircraftGun/{ => Drawnings}/DirectionType.cs (69%) create mode 100644 AntiAircraftGun/AntiAircraftGun/Drawnings/DrawningAntiAircrfatGun.cs create mode 100644 AntiAircraftGun/AntiAircraftGun/Drawnings/DrawningGun.cs create mode 100644 AntiAircraftGun/AntiAircraftGun/Entities/EntityAntiAircraftGun.cs create mode 100644 AntiAircraftGun/AntiAircraftGun/Entities/EntityGun.cs delete mode 100644 AntiAircraftGun/AntiAircraftGun/EntityAntiAircraftGun.cs create mode 100644 AntiAircraftGun/AntiAircraftGun/MovementStrategy/AbstractStrategy.cs create mode 100644 AntiAircraftGun/AntiAircraftGun/MovementStrategy/IMoveableObject.cs create mode 100644 AntiAircraftGun/AntiAircraftGun/MovementStrategy/MoveToBorder.cs create mode 100644 AntiAircraftGun/AntiAircraftGun/MovementStrategy/MoveToCenter.cs create mode 100644 AntiAircraftGun/AntiAircraftGun/MovementStrategy/MoveableGun.cs create mode 100644 AntiAircraftGun/AntiAircraftGun/MovementStrategy/MovementDirection.cs create mode 100644 AntiAircraftGun/AntiAircraftGun/MovementStrategy/ObjectParameters.cs create mode 100644 AntiAircraftGun/AntiAircraftGun/MovementStrategy/StrategyStatus.cs diff --git a/AntiAircraftGun/AntiAircraftGun/DrawningAntiAircrfatGun.cs b/AntiAircraftGun/AntiAircraftGun/DrawningAntiAircrfatGun.cs deleted file mode 100644 index bb4510d..0000000 --- a/AntiAircraftGun/AntiAircraftGun/DrawningAntiAircrfatGun.cs +++ /dev/null @@ -1,210 +0,0 @@ -using ProjectSportCar; - -namespace AntiAircraftGun; -/// -/// Класс, отвечающий за прорисовку и перемещение объекта -/// -public class DrawningAntiAircraftGun -{ - /// - /// Класс-сущность - /// - public EntityAntiAircraftGun? EntityAntiAircraftGun { get; private set; } - /// - /// Ширина окна - /// - private int? _pictureWidth; - /// - /// Высота окна - /// - private int? _pictureHeight; - /// - /// Левая координата прорисовки зенитной установки - /// - private int? _startPosX; - /// - /// Правая координата прорисовку зенитной установки - /// - private int? _startPosY; - /// - /// Ширина прорисовки зенитной установки - /// - private readonly int _drawningGunWidth = 150; - /// - /// Высота прорисовки зенитной установки - /// - private readonly int _drawingGunHeight = 115; - /// - /// Иницилизация - /// - /// - /// - /// - /// - /// - /// - public void Init(int speed, double weight, Color bodyColor, Color optionalElementsColor, double barrelLenth, bool hatchHeight, bool radar) - { - EntityAntiAircraftGun = new EntityAntiAircraftGun(); - EntityAntiAircraftGun.Init(speed, weight, bodyColor, optionalElementsColor, barrelLenth, hatchHeight, radar); - _pictureWidth = null; - _pictureHeight = null; - _startPosX = null; - _startPosY = null; - } - /// - /// Установка гранц поля - /// - /// - /// - /// true - границы заданы, false - проверка не пройдена, нельзя разместить объект в этих размерах - public bool SetPictureSize(int width, int height) - { - // TODO проверка, что объект "влезает" в размеры поля - // если влезает, сохраняем границы и корректируем позицию объекта, если она была уже установлена - if (_drawningGunWidth > width || _drawingGunHeight > height) { return false; } - if (_startPosX.HasValue && _startPosY.HasValue) - { - if (_startPosX.Value + _drawningGunWidth > width) - { - _startPosX = width - _drawningGunWidth; - } - if (_startPosY.Value + _drawingGunHeight > height) - { - _startPosY = height - _drawingGunHeight; - } - } - _pictureHeight = height; - _pictureWidth = width; - return true; - - } - /// - /// Установка позиции - /// - /// - /// - public void SetPosition(int x, int y) - { - if (!_pictureHeight.HasValue || !_pictureWidth.HasValue) - { - return; - } - if (x + _drawningGunWidth > _pictureWidth.Value || x < 0) - { - Random random = new(); - _startPosX = random.Next(0, _pictureWidth.Value - _drawningGunWidth); - } - else - { - _startPosX = x; - } - if (y + _drawingGunHeight > _drawingGunHeight) - { - Random rand = new(); - _startPosY = rand.Next(0, _pictureHeight.Value - _drawingGunHeight); - } - else - { - _startPosY = y; - } - } - /// - /// Изменение напаравления перемещения - /// - /// - /// - public bool MoveTransport(DirectionType direction) - { - if (EntityAntiAircraftGun == null || !_startPosX.HasValue || - !_startPosY.HasValue) - { - return false; - } - switch (direction) - { - //влево - case DirectionType.Left: - if (_startPosX.Value - EntityAntiAircraftGun.Step > 0) - { - _startPosX -= (int)EntityAntiAircraftGun.Step; - } - return true; - //вверх - case DirectionType.Up: - if (_startPosY.Value - EntityAntiAircraftGun.Step > 0) - { - _startPosY -= (int)EntityAntiAircraftGun.Step; - } - return true; - // вправо - case DirectionType.Right: - //TODO прописать логику сдвига в право - if (_startPosX.Value + EntityAntiAircraftGun.Step + _drawningGunWidth < _pictureWidth) - { - _startPosX += (int)EntityAntiAircraftGun.Step; - } - return true; - //вниз - case DirectionType.Down: - //TODO прописать логику сдвига в вниз - if (_startPosY.Value + EntityAntiAircraftGun.Step + _drawingGunHeight < _pictureHeight) - { - _startPosY += (int)EntityAntiAircraftGun.Step; - } - return true; - default: - return false; - } - } - /// - /// Прорисовка объекта - /// - /// - public void DrawTransport(Graphics g) - { - if (EntityAntiAircraftGun == null || !_startPosX.HasValue || !_startPosY.HasValue) return; - Pen pen = new(Color.Black); - Brush additionalBrush = new SolidBrush(EntityAntiAircraftGun.OptionalElementsColor); - Brush MainBrush = new SolidBrush(EntityAntiAircraftGun.BodyColor); - // Башня - g.FillRectangle(MainBrush, _startPosX.Value + 50, _startPosY.Value + 50, 60, 25); - g.FillRectangle(MainBrush, _startPosX.Value + 25, _startPosY.Value + 75, 110, 10); - // Гусеницы - g.DrawArc(pen, _startPosX.Value + 110, _startPosY.Value + 85, 40, 30, 270, 180); - g.DrawArc(pen, _startPosX.Value + 10, _startPosY.Value + 85, 40, 30, 90, 180); - g.DrawLine(pen, _startPosX.Value + 30, _startPosY.Value + 115, _startPosX.Value + 130, _startPosY.Value + 115); - // Катки большие - g.DrawEllipse(pen, _startPosX.Value + 13, _startPosY.Value + 93, 20, 20); - g.DrawEllipse(pen, _startPosX.Value + 126, _startPosY.Value + 93, 20, 20); - // Катки малые - g.DrawEllipse(pen, _startPosX.Value + 40, _startPosY.Value + 105, 10, 10); - g.DrawEllipse(pen, _startPosX.Value + 60, _startPosY.Value + 105, 10, 10); - g.DrawEllipse(pen, _startPosX.Value + 80, _startPosY.Value + 105, 10, 10); - g.DrawEllipse(pen, _startPosX.Value + 100, _startPosY.Value + 105, 10, 10); - // Орудие - Pen penWeapon = new Pen(Color.Black, 8); - g.DrawLine(penWeapon, _startPosX.Value + 100, _startPosY.Value + 70, _startPosX.Value + 150, _startPosY.Value + 10); - // Люк - if (EntityAntiAircraftGun.Hatch) - { - Random random = new(); - g.FillRectangle(additionalBrush, _startPosX.Value + 85, _startPosY.Value + 45, 20, 5); - } - // Радар - if (EntityAntiAircraftGun.Radar) - { - Pen penRadar = new Pen(Color.Green, 3); - Brush brushRadar = new SolidBrush(Color.Black); - g.DrawLine(pen, _startPosX.Value + 65, _startPosY.Value + 50, _startPosX.Value + 65, _startPosY.Value + 25); - g.FillEllipse(brushRadar, _startPosX.Value + 35, _startPosY.Value, 60, 25); - g.DrawLine(penRadar, _startPosX.Value + 65, _startPosY.Value + 25, _startPosX.Value + 65, _startPosY.Value); - g.DrawLine(penRadar, _startPosX.Value + 35, _startPosY.Value + 13, _startPosX.Value + 95, _startPosY.Value + 13); - - } - - - - } - -} \ No newline at end of file diff --git a/AntiAircraftGun/AntiAircraftGun/DirectionType.cs b/AntiAircraftGun/AntiAircraftGun/Drawnings/DirectionType.cs similarity index 69% rename from AntiAircraftGun/AntiAircraftGun/DirectionType.cs rename to AntiAircraftGun/AntiAircraftGun/Drawnings/DirectionType.cs index bc591bb..68c99f6 100644 --- a/AntiAircraftGun/AntiAircraftGun/DirectionType.cs +++ b/AntiAircraftGun/AntiAircraftGun/Drawnings/DirectionType.cs @@ -1,4 +1,4 @@ -namespace ProjectSportCar; +namespace AntiAircraftGun.Drawnings; /// /// Направление перемещения /// @@ -19,5 +19,9 @@ public enum DirectionType /// /// Вправо /// - Right = 4 + Right = 4, + /// + /// Неизвестно направление + /// + Unknow = -1 } diff --git a/AntiAircraftGun/AntiAircraftGun/Drawnings/DrawningAntiAircrfatGun.cs b/AntiAircraftGun/AntiAircraftGun/Drawnings/DrawningAntiAircrfatGun.cs new file mode 100644 index 0000000..a0e45f8 --- /dev/null +++ b/AntiAircraftGun/AntiAircraftGun/Drawnings/DrawningAntiAircrfatGun.cs @@ -0,0 +1,53 @@ +using AntiAircraftGun.Entities; + +namespace AntiAircraftGun.Drawnings; +/// +/// Класс, отвечающий за прорисовку и перемещение объекта +/// +public class DrawningAntiAircraftGun:DrawningGun +{ + /// + /// Конструктор + /// + /// + /// + /// + /// + /// + /// + public DrawningAntiAircraftGun(int speed, double weight, Color bodyColor, Color optionalElementsColor, double barrelLenth, bool hatchHeight, bool radar) : base(150,115) //140, 65 + { + EntityGun = new EntityAntiAircraftGun(speed,weight,bodyColor,optionalElementsColor,barrelLenth,hatchHeight,radar); + } + + public override void DrawTransport(Graphics g) + { + _startPosX += 10; + _startPosY += 50; + base.DrawTransport(g); + _startPosX -= 10; + _startPosY -= 50; + if (EntityGun == null || !_startPosX.HasValue || !_startPosY.HasValue || EntityGun is not EntityAntiAircraftGun antiAircraftGun) return; + Pen pen = new(Color.Black); + Brush additionalBrush = new SolidBrush(antiAircraftGun.OptionalElementsColor); + // Орудие + Pen penWeapon = new Pen(Color.Black, 8); + g.DrawLine(penWeapon, _startPosX.Value + 100, _startPosY.Value + 70, _startPosX.Value + 150, _startPosY.Value + 10); + // Люк + if (antiAircraftGun.Hatch) + { + Random random = new(); + g.FillRectangle(additionalBrush, _startPosX.Value + 85, _startPosY.Value + 45, 20, 5); + } + // Радар + if (antiAircraftGun.Radar) + { + Pen penRadar = new Pen(Color.Green, 3); + Brush brushRadar = new SolidBrush(Color.Black); + g.DrawLine(pen, _startPosX.Value + 65, _startPosY.Value + 50, _startPosX.Value + 65, _startPosY.Value + 25); + g.FillEllipse(brushRadar, _startPosX.Value + 35, _startPosY.Value, 60, 25); + g.DrawLine(penRadar, _startPosX.Value + 65, _startPosY.Value + 25, _startPosX.Value + 65, _startPosY.Value); + g.DrawLine(penRadar, _startPosX.Value + 35, _startPosY.Value + 13, _startPosX.Value + 95, _startPosY.Value + 13); + } + } +} \ No newline at end of file diff --git a/AntiAircraftGun/AntiAircraftGun/Drawnings/DrawningGun.cs b/AntiAircraftGun/AntiAircraftGun/Drawnings/DrawningGun.cs new file mode 100644 index 0000000..9bf0093 --- /dev/null +++ b/AntiAircraftGun/AntiAircraftGun/Drawnings/DrawningGun.cs @@ -0,0 +1,223 @@ +using AntiAircraftGun.Entities; +using System; +using System.Collections.Generic; +using System.Diagnostics.Tracing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AntiAircraftGun.Drawnings; + +public class DrawningGun +{ + /// + /// Класс-сущность + /// + public EntityGun? EntityGun { get; protected set; } + /// + /// Ширина окна + /// + private int? _pictureWidth; + /// + /// Высота окна + /// + private int? _pictureHeight; + /// + /// Левая координата прорисовки зенитной установки + /// + protected int? _startPosX; + /// + /// Правая координата прорисовку зенитной установки + /// + protected int? _startPosY; + /// + /// Ширина прорисовки зенитной установки + /// + private readonly int _drawningGunWidth = 140; + /// + /// Высота прорисовки зенитной установки + /// + private readonly int _drawingGunHeight = 65; + /// + /// Координата Х объекта + /// + public int? GetPosX ()=> _startPosX; + /// + /// Координата Y объекта + /// + public int? GetPosY() => _startPosY; + /// + /// Ширина объекта + /// + public int GetWidth() => _drawningGunWidth; + /// + /// Высота объекта + /// + public int GetHeight() => _drawingGunHeight; + /// + /// Пустой конструктор + /// + private DrawningGun() + { + _pictureWidth = null; + _pictureHeight = null; + _startPosX = null; + _startPosY = null; + } + /// + /// Констурктор + /// + /// Скорость + /// Вес + /// Основной цвет + public DrawningGun(int speed, double weight, Color bodyColor):this() + { + EntityGun = new EntityGun(speed, weight, bodyColor); + + } + /// + /// Конструктор для наследников + /// + /// Ширина прорисовки ракетной установки + /// Высота прорисовки ракетной установки + protected DrawningGun(int drawningGunWidth, int drawningGunHeight):this() + { + _drawingGunHeight = drawningGunHeight; + _drawningGunWidth = drawningGunWidth; + } + /// + /// Установка гранц поля + /// + /// + /// + /// true - границы заданы, false - проверка не пройдена, нельзя разместить объект в этих размерах + public bool SetPictureSize(int width, int height) + { + // TODO проверка, что объект "влезает" в размеры поля + // если влезает, сохраняем границы и корректируем позицию объекта, если она была уже установлена + if (_drawningGunWidth > width || _drawingGunHeight > height) { return false; } + if (_startPosX.HasValue && _startPosY.HasValue) + { + if (_startPosX.Value + _drawningGunWidth > width) + { + _startPosX = width - _drawningGunWidth; + } + if (_startPosY.Value + _drawingGunHeight > height) + { + _startPosY = height - _drawingGunHeight; + } + } + _pictureHeight = height; + _pictureWidth = width; + return true; + + } + /// + /// Установка позиции + /// + /// + /// + public void SetPosition(int x, int y) + { + if (!_pictureHeight.HasValue || !_pictureWidth.HasValue) + { + return; + } + if (x + _drawningGunWidth > _pictureWidth.Value ) + { + _startPosX = x - (x + _drawningGunWidth - _pictureWidth.Value); + }else if (x < 0) + { + _startPosX = 0; + } + else + { + _startPosX = x; + } + if (y + _drawingGunHeight > _pictureHeight.Value ) + { + _startPosY = y - (y + _drawingGunHeight - _pictureHeight.Value); + } + else if (y < 0) + { + _startPosY = 0; + } + else + { + _startPosY = y; + } + } + /// + /// Изменение напаравления перемещения + /// + /// + /// + public bool MoveTransport(DirectionType direction) + { + if (EntityGun == null || !_startPosX.HasValue || + !_startPosY.HasValue) + { + return false; + } + switch (direction) + { + //влево + case DirectionType.Left: + if (_startPosX.Value - EntityGun.Step > 0) + { + _startPosX -= (int)EntityGun.Step; + } + return true; + //вверх + case DirectionType.Up: + if (_startPosY.Value - EntityGun.Step > 0) + { + _startPosY -= (int)EntityGun.Step; + } + return true; + // вправо + case DirectionType.Right: + //TODO прописать логику сдвига в право + if (_startPosX.Value + EntityGun.Step + _drawningGunWidth < _pictureWidth) + { + _startPosX += (int)EntityGun.Step; + } + return true; + //вниз + case DirectionType.Down: + //TODO прописать логику сдвига в вниз + if (_startPosY.Value + EntityGun.Step + _drawingGunHeight < _pictureHeight) + { + _startPosY += (int)EntityGun.Step; + } + return true; + default: + return false; + } + } + /// + /// Прорисовка объекта + /// + /// + public virtual void DrawTransport(Graphics g) + { + if (EntityGun == null || !_startPosX.HasValue || !_startPosY.HasValue) return; + Pen pen = new(Color.Black); + Brush MainBrush = new SolidBrush(EntityGun.BodyColor); + // Башня + g.FillRectangle(MainBrush, _startPosX.Value + 50 - 10, _startPosY.Value + 50 - 50, 60, 25); + g.FillRectangle(MainBrush, _startPosX.Value + 25 - 10, _startPosY.Value + 75 - 50, 110, 10); + // Гусеницы + g.DrawArc(pen, _startPosX.Value + 110 - 10, _startPosY.Value + 85 - 50, 40, 30, 270, 180); + g.DrawArc(pen, _startPosX.Value + 10 - 10, _startPosY.Value + 85 - 50, 40, 30, 90, 180); + g.DrawLine(pen, _startPosX.Value + 30 - 10, _startPosY.Value + 115 - 50, _startPosX.Value + 130 - 10, _startPosY.Value + 115 - 50); + // Катки большие + g.DrawEllipse(pen, _startPosX.Value + 13 - 10, _startPosY.Value + 93 - 50, 20, 20); + g.DrawEllipse(pen, _startPosX.Value + 126 - 10, _startPosY.Value + 93 - 50, 20, 20); + // Катки малые + g.DrawEllipse(pen, _startPosX.Value + 40 - 10, _startPosY.Value + 105 - 50, 10, 10); + g.DrawEllipse(pen, _startPosX.Value + 60 - 10, _startPosY.Value + 105 - 50, 10, 10); + g.DrawEllipse(pen, _startPosX.Value + 80 - 10, _startPosY.Value + 105 - 50, 10, 10); + g.DrawEllipse(pen, _startPosX.Value + 100 - 10, _startPosY.Value + 105 - 50, 10, 10); + } +} diff --git a/AntiAircraftGun/AntiAircraftGun/Entities/EntityAntiAircraftGun.cs b/AntiAircraftGun/AntiAircraftGun/Entities/EntityAntiAircraftGun.cs new file mode 100644 index 0000000..ea0486f --- /dev/null +++ b/AntiAircraftGun/AntiAircraftGun/Entities/EntityAntiAircraftGun.cs @@ -0,0 +1,30 @@ +namespace AntiAircraftGun.Entities; +/// +/// Класс-сущность "Зенитная установка" +/// +public class EntityAntiAircraftGun:EntityGun +{ + private EntityGun? EntityGun; + public Color OptionalElementsColor { get; private set; } + /// + /// Длинна ствола + /// + public double BarrelLength { get; private set; } + /// + /// Люк + /// + public bool Hatch { get; private set; } + /// + /// Радар + /// + public bool Radar { get; private set; } + public EntityAntiAircraftGun(int speed, double weight, Color bodyColor, Color optionalElementsColor, double barrelLenth, bool hatch, bool radar) : base(speed, weight, bodyColor) + { + EntityGun = new EntityGun(speed, weight, bodyColor); + OptionalElementsColor = optionalElementsColor; + BarrelLength = barrelLenth; + Radar = radar; + Hatch = hatch; + + } +} \ No newline at end of file diff --git a/AntiAircraftGun/AntiAircraftGun/Entities/EntityGun.cs b/AntiAircraftGun/AntiAircraftGun/Entities/EntityGun.cs new file mode 100644 index 0000000..9ec27ad --- /dev/null +++ b/AntiAircraftGun/AntiAircraftGun/Entities/EntityGun.cs @@ -0,0 +1,42 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using static System.Windows.Forms.VisualStyles.VisualStyleElement; + +namespace AntiAircraftGun.Entities; +/// +/// Класс-сущности "Орудие" +/// +public class EntityGun +{ + /// + /// Скорость + /// + public int Speed { get; private set; } + /// + /// Вес + /// + public double Weight { get; private set; } + /// + /// Основной цвет + /// + public Color BodyColor { get; private set; } + /// + /// Шаг + /// + public double Step { get { return Speed * 100 / Weight; } private set { } } + /// + /// Конструктор сущности + /// + /// + /// + /// + public EntityGun(int speed, double weight, Color bodyColor) + { + Speed = speed; + Weight = weight; + BodyColor = bodyColor; + } +} diff --git a/AntiAircraftGun/AntiAircraftGun/EntityAntiAircraftGun.cs b/AntiAircraftGun/AntiAircraftGun/EntityAntiAircraftGun.cs deleted file mode 100644 index 46c7e1d..0000000 --- a/AntiAircraftGun/AntiAircraftGun/EntityAntiAircraftGun.cs +++ /dev/null @@ -1,59 +0,0 @@ -namespace AntiAircraftGun; -/// -/// Класс-сущность "Зенитная установка" -/// -public class EntityAntiAircraftGun -{ /// - /// Скорость - /// - public int Speed { get; private set; } - /// - /// Вес - /// - public double Weight { get; private set; } - /// - /// Основной цвет - /// - public Color BodyColor { get; private set; } - /// - /// Дополнительный цвет - /// - public Color OptionalElementsColor { get; private set; } - /// - /// Длинна ствола - /// - public double BarrelLength { get; private set; } - /// - /// Люк - /// - public bool Hatch { get; private set; } - /// - /// Радар - /// - public bool Radar { get; private set; } - /// - /// Шаг - /// - public double Step { get { return Speed * 100 / Weight; } private set { } } - /// - /// Инициализация свойств - /// - /// - /// - /// - /// - /// - /// - /// - public void Init(int speed, double weight, Color bodyColor, Color optionalElementsColor, double barrelLenth, bool hatch,bool radar) - { - Speed = speed; - Weight = weight; - BodyColor = bodyColor; - OptionalElementsColor = optionalElementsColor; - BarrelLength = barrelLenth; - Hatch = hatch; - Radar = radar; - } - -} \ No newline at end of file diff --git a/AntiAircraftGun/AntiAircraftGun/FormAntiAircraftGun.Designer.cs b/AntiAircraftGun/AntiAircraftGun/FormAntiAircraftGun.Designer.cs index 8ad8233..54ca8fe 100644 --- a/AntiAircraftGun/AntiAircraftGun/FormAntiAircraftGun.Designer.cs +++ b/AntiAircraftGun/AntiAircraftGun/FormAntiAircraftGun.Designer.cs @@ -34,6 +34,9 @@ buttonLeft = new Button(); buttonUp = new Button(); buttonRight = new Button(); + buttonCreateGun = new Button(); + comboBoxStrategy = new ComboBox(); + buttonStrategyStep = new Button(); ((System.ComponentModel.ISupportInitialize)pictureBoxAntiAircraftGun).BeginInit(); SuspendLayout(); // @@ -43,7 +46,7 @@ pictureBoxAntiAircraftGun.Location = new Point(0, 0); pictureBoxAntiAircraftGun.Name = "pictureBoxAntiAircraftGun"; pictureBoxAntiAircraftGun.Size = new Size(939, 393); - pictureBoxAntiAircraftGun.TabIndex = 6; + pictureBoxAntiAircraftGun.TabIndex = 8; pictureBoxAntiAircraftGun.TabStop = false; // // buttonCreate @@ -51,9 +54,9 @@ buttonCreate.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; buttonCreate.Location = new Point(12, 352); buttonCreate.Name = "buttonCreate"; - buttonCreate.Size = new Size(94, 29); + buttonCreate.Size = new Size(261, 29); buttonCreate.TabIndex = 1; - buttonCreate.Text = "Создать"; + buttonCreate.Text = "Создать зенитную установку"; buttonCreate.UseVisualStyleBackColor = true; buttonCreate.Click += ButtonCreate_Click; // @@ -105,11 +108,46 @@ buttonRight.UseVisualStyleBackColor = true; buttonRight.Click += ButtonMove_Click; // + // buttonCreateGun + // + buttonCreateGun.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; + buttonCreateGun.Location = new Point(288, 352); + buttonCreateGun.Name = "buttonCreateGun"; + buttonCreateGun.Size = new Size(203, 29); + buttonCreateGun.TabIndex = 7; + buttonCreateGun.Text = "Создать установку"; + buttonCreateGun.UseVisualStyleBackColor = true; + buttonCreateGun.Click += buttonCreateGun_Click; + // + // comboBoxStrategy + // + comboBoxStrategy.DropDownStyle = ComboBoxStyle.DropDownList; + comboBoxStrategy.FormattingEnabled = true; + comboBoxStrategy.Items.AddRange(new object[] { "К центру", "К краю" }); + comboBoxStrategy.Location = new Point(776, 12); + comboBoxStrategy.Name = "comboBoxStrategy"; + comboBoxStrategy.Size = new Size(151, 28); + comboBoxStrategy.TabIndex = 9; + // + // buttonStrategyStep + // + buttonStrategyStep.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; + buttonStrategyStep.Location = new Point(797, 65); + buttonStrategyStep.Name = "buttonStrategyStep"; + buttonStrategyStep.Size = new Size(130, 29); + buttonStrategyStep.TabIndex = 10; + buttonStrategyStep.Text = "Шаг"; + buttonStrategyStep.UseVisualStyleBackColor = true; + buttonStrategyStep.Click += buttonStrategyStep_Click; + // // FormAntiAircraftGun // AutoScaleDimensions = new SizeF(8F, 20F); AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(939, 393); + Controls.Add(buttonStrategyStep); + Controls.Add(comboBoxStrategy); + Controls.Add(buttonCreateGun); Controls.Add(buttonRight); Controls.Add(buttonUp); Controls.Add(buttonLeft); @@ -130,5 +168,8 @@ private Button buttonLeft; private Button buttonUp; private Button buttonRight; + private Button buttonCreateGun; + private ComboBox comboBoxStrategy; + private Button buttonStrategyStep; } } \ No newline at end of file diff --git a/AntiAircraftGun/AntiAircraftGun/FormAntiAircraftGun.cs b/AntiAircraftGun/AntiAircraftGun/FormAntiAircraftGun.cs index 6b8f612..5f7fc0e 100644 --- a/AntiAircraftGun/AntiAircraftGun/FormAntiAircraftGun.cs +++ b/AntiAircraftGun/AntiAircraftGun/FormAntiAircraftGun.cs @@ -1,50 +1,90 @@ -using ProjectSportCar; +using AntiAircraftGun.Drawnings; +using AntiAircraftGun.MovementStrategy; namespace AntiAircraftGun { public partial class FormAntiAircraftGun : Form { - private DrawningAntiAircraftGun? _drawningAntiAircraftGun; + /// + /// Стратегия перемещения + /// + private AbstractStrategy? _abstractStrategy; + + + private DrawningGun? _drawningGun; public FormAntiAircraftGun() { InitializeComponent(); + _abstractStrategy = null; } - + /// + /// Метод рисования машины + /// private void Draw() { - if (_drawningAntiAircraftGun == null) + if (_drawningGun == null) { return; } Bitmap bmp = new(pictureBoxAntiAircraftGun.Width, pictureBoxAntiAircraftGun.Height); Graphics gr = Graphics.FromImage(bmp); - _drawningAntiAircraftGun.DrawTransport(gr); + _drawningGun.DrawTransport(gr); pictureBoxAntiAircraftGun.Image = bmp; - _drawningAntiAircraftGun.DrawTransport(gr); + _drawningGun.DrawTransport(gr); } - private void ButtonCreate_Click(object sender, EventArgs e) + private void CreateObj(string type) { Random random = new(); - _drawningAntiAircraftGun = new DrawningAntiAircraftGun(); - _drawningAntiAircraftGun.Init( - random.Next(10, 100), - random.Next(10, 1000), - 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)), - random.Next(10, 100), - Convert.ToBoolean(random.Next(0, 2)), Convert.ToBoolean(random.Next(0, 2))); - _drawningAntiAircraftGun.SetPictureSize(pictureBoxAntiAircraftGun.Width, pictureBoxAntiAircraftGun.Height); - _drawningAntiAircraftGun.SetPosition(random.Next(10, 100), random.Next(10, 100)); - + switch (type) + { + case nameof(DrawningGun): + _drawningGun = new DrawningGun(random.Next(100, 300), + random.Next(1000, 3000), + Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256))); + break; + case nameof(DrawningAntiAircraftGun): + _drawningGun = new DrawningAntiAircraftGun(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)), + random.Next(10, 100), + Convert.ToBoolean(random.Next(0, 2)), + Convert.ToBoolean(random.Next(0, 2))); + break; + } + _drawningGun.SetPictureSize(pictureBoxAntiAircraftGun.Width, pictureBoxAntiAircraftGun.Height); + _drawningGun.SetPosition(random.Next(10, 100), random.Next(10, 100)); + _abstractStrategy = null; + comboBoxStrategy.Enabled = true; Draw(); - } - + /// + /// Обработка нажатия "Создать зенитную установку" + /// + /// + /// + private void ButtonCreate_Click(object sender, EventArgs e) + { + CreateObj(nameof(DrawningAntiAircraftGun)); + } + /// + /// Обработка нажатия "Создать установку" + /// + /// + /// + private void buttonCreateGun_Click(object sender, EventArgs e) + { + CreateObj(nameof(DrawningGun)); + } private void ButtonMove_Click(object sender, EventArgs e) { - if (_drawningAntiAircraftGun == null) + if (_drawningGun == null) { return; } @@ -54,19 +94,19 @@ namespace AntiAircraftGun { case "buttonUp": result = - _drawningAntiAircraftGun.MoveTransport(DirectionType.Up); + _drawningGun.MoveTransport(DirectionType.Up); break; case "buttonDown": result = - _drawningAntiAircraftGun.MoveTransport(DirectionType.Down); + _drawningGun.MoveTransport(DirectionType.Down); break; case "buttonLeft": result = - _drawningAntiAircraftGun.MoveTransport(DirectionType.Left); + _drawningGun.MoveTransport(DirectionType.Left); break; case "buttonRight": result = - _drawningAntiAircraftGun.MoveTransport(DirectionType.Right); + _drawningGun.MoveTransport(DirectionType.Right); break; } if (result) @@ -75,5 +115,41 @@ namespace AntiAircraftGun } } + + private void buttonStrategyStep_Click(object sender, EventArgs e) + { + if (_drawningGun == null) + { + return; + } + if (comboBoxStrategy.Enabled) + { + _abstractStrategy = comboBoxStrategy.SelectedIndex switch + { + 0 => new MoveToCenter(), + 1 => new MoveToBorder(), + _ => null, + }; + if (_abstractStrategy == null) + { + return; + } + _abstractStrategy.SetData(new MoveableGun(_drawningGun), + pictureBoxAntiAircraftGun.Width, pictureBoxAntiAircraftGun.Height); + } + if (_abstractStrategy == null) + { + return; + } + comboBoxStrategy.Enabled = false; + _abstractStrategy.MakeStep(); + Draw(); + if (_abstractStrategy.GetStatus() == StrategyStatus.Finish) + { + comboBoxStrategy.Enabled = true; + _abstractStrategy = null; + } + + } } } diff --git a/AntiAircraftGun/AntiAircraftGun/MovementStrategy/AbstractStrategy.cs b/AntiAircraftGun/AntiAircraftGun/MovementStrategy/AbstractStrategy.cs new file mode 100644 index 0000000..6aeead3 --- /dev/null +++ b/AntiAircraftGun/AntiAircraftGun/MovementStrategy/AbstractStrategy.cs @@ -0,0 +1,122 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AntiAircraftGun.MovementStrategy; + +public abstract class AbstractStrategy +{ + /// + /// Перемещаемый объект + /// + private IMoveableObject? _moveableObject; + /// + /// Статус перемещения + /// + private StrategyStatus _state = StrategyStatus.NotInit; + /// + /// Ширина поля + /// + protected int FieldWidth { get; private set; } + /// + /// Высота поля + /// + protected int FieldHeight { get; private set; } + /// + /// Статус перемещения + /// + public StrategyStatus GetStatus() { return _state; } + /// + /// Установка данных + /// + /// Перемещаемый объект + /// Ширина поля + /// Высота поля + public void SetData(IMoveableObject moveableObject, int width, int height) + { + if (moveableObject == null) + { + _state = StrategyStatus.NotInit; + return; + } + _state = StrategyStatus.InProgress; + _moveableObject = moveableObject; + FieldWidth = width; + FieldHeight = height; + } + /// + /// Шаг перемещения + /// + public void MakeStep() + { + if (_state != StrategyStatus.InProgress) + { + return; + } + if (IsTargetDestinaion()) + { + _state = StrategyStatus.Finish; + return; + } + MoveToTarget(); + } + /// + /// Перемещение влево + /// + /// Результат перемещения (true - удалось переместиться, false -неудача) + protected bool MoveLeft() => MoveTo(MovementDirection.Left); + /// + /// Перемещение вправо + /// + /// Результат перемещения (true - удалось переместиться, false -неудача) + protected bool MoveRight() => MoveTo(MovementDirection.Right); + /// + /// Перемещение вверх + /// + /// Результат перемещения (true - удалось переместиться, false -неудача) + protected bool MoveUp() => MoveTo(MovementDirection.Up); + /// + /// Перемещение вниз + /// + /// Результат перемещения (true - удалось переместиться, false -неудача) + protected bool MoveDown() => MoveTo(MovementDirection.Down); + /// /// Параметры объекта + /// + protected ObjectParameters? GetObjectParameters() => _moveableObject?.GetObjectPosition; + /// + /// Шаг объекта + /// + /// + protected int? GetStep() + { + if (_state != StrategyStatus.InProgress) + { + return null; + } + return _moveableObject?.GetStep; + } + /// + /// Перемещение к цели + /// + protected abstract void MoveToTarget(); + /// + /// Достигнута ли цель + /// + /// + protected abstract bool IsTargetDestinaion(); + /// + /// Попытка перемещения в требуемом направлении + /// + /// Направление + /// Результат попытки (true - удалось переместиться, false -неудача) + private bool MoveTo(MovementDirection movementDirection) + { + if (_state != StrategyStatus.InProgress) + { + return false; + } + return _moveableObject?.TryMoveObject(movementDirection) ?? false; + } +} diff --git a/AntiAircraftGun/AntiAircraftGun/MovementStrategy/IMoveableObject.cs b/AntiAircraftGun/AntiAircraftGun/MovementStrategy/IMoveableObject.cs new file mode 100644 index 0000000..19b31c8 --- /dev/null +++ b/AntiAircraftGun/AntiAircraftGun/MovementStrategy/IMoveableObject.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AntiAircraftGun.MovementStrategy; +/// +/// Интерфейс для работы с перемещаемым объектом +/// +public interface IMoveableObject +{ + /// + /// Получение координаты объекта + /// + ObjectParameters? GetObjectPosition { get; } + /// + /// Шаг объекта + /// + int GetStep { get; } + /// + /// Попытка переместить объект в указанном направлении + /// + /// Направление + /// true - объект перемещен, false - перемещение невозможно + bool TryMoveObject(MovementDirection direction); +} diff --git a/AntiAircraftGun/AntiAircraftGun/MovementStrategy/MoveToBorder.cs b/AntiAircraftGun/AntiAircraftGun/MovementStrategy/MoveToBorder.cs new file mode 100644 index 0000000..217296b --- /dev/null +++ b/AntiAircraftGun/AntiAircraftGun/MovementStrategy/MoveToBorder.cs @@ -0,0 +1,35 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AntiAircraftGun.MovementStrategy; + +public class MoveToBorder : AbstractStrategy +{ + protected override bool IsTargetDestinaion() + { + ObjectParameters? objectParameters = GetObjectParameters(); + if (objectParameters == null) + { + return false; + } + return objectParameters.RightBorder() <= FieldWidth && objectParameters.RightBorder() + GetStep() >= FieldWidth && + objectParameters.DownBorder() <= FieldHeight && objectParameters.DownBorder() + GetStep() >= FieldHeight; + } + + protected override void MoveToTarget() + { + ObjectParameters? objectParameters=GetObjectParameters(); + if(objectParameters == null) { return; } + if (objectParameters.RightBorder() < FieldWidth) + { + MoveRight(); + } + if (objectParameters.DownBorder() = FieldWidth / 2 + && objParams.ObjectMiddleVertical - GetStep() <= FieldHeight / 2 + && objParams.ObjectMiddleVertical + GetStep() >= FieldHeight / 2; + } + + protected override void MoveToTarget() + { + ObjectParameters? objParams = GetObjectParameters(); + if (objParams == null) + { + return; + } + int diffX = objParams.ObjectMiddleHorizontal - FieldWidth / 2; + if (Math.Abs(diffX) > GetStep()) + { + if (diffX > 0) + { + MoveLeft(); + } + else + { + MoveRight(); + } + } + int diffY = objParams.ObjectMiddleVertical - FieldHeight / 2; + if (Math.Abs(diffY) > GetStep()) + { + if (diffY > 0) + { + MoveUp(); + } + else + { + MoveDown(); + } + } + + } +} diff --git a/AntiAircraftGun/AntiAircraftGun/MovementStrategy/MoveableGun.cs b/AntiAircraftGun/AntiAircraftGun/MovementStrategy/MoveableGun.cs new file mode 100644 index 0000000..98200fb --- /dev/null +++ b/AntiAircraftGun/AntiAircraftGun/MovementStrategy/MoveableGun.cs @@ -0,0 +1,71 @@ +using AntiAircraftGun.Drawnings; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AntiAircraftGun.MovementStrategy; +/// +/// Класс-реализация IMoveableObject с ипользованием DrawningGun +/// +public class MoveableGun : IMoveableObject +{ + /// + /// Поле-объект класса DrawningGun или его наследника + /// + private readonly DrawningGun? _drawningGun = null; + /// + /// Конструктор + /// + /// + public MoveableGun(DrawningGun? drawningGun) + { + _drawningGun = drawningGun; + } + + public ObjectParameters? GetObjectPosition + { + get + { + if (_drawningGun == null || _drawningGun.EntityGun == null || + !_drawningGun.GetPosX().HasValue || + !_drawningGun.GetPosY().HasValue) + { + return null; + } + return new ObjectParameters( + _drawningGun.GetPosX().Value, + _drawningGun.GetPosY().Value, + _drawningGun.GetWidth(), + _drawningGun.GetHeight()); + } + } + + public int GetStep => (int)(_drawningGun?.EntityGun?.Step ?? 0); + + public bool TryMoveObject(MovementDirection direction) + { + if (_drawningGun == null || _drawningGun.EntityGun == null) + { + return false; + } + return _drawningGun.MoveTransport(GetDirectionType(direction)); + } + /// + /// Конвертация из MovementDirection в DirectionType + /// + /// MovementDirection + /// + private static DirectionType GetDirectionType(MovementDirection direction) + { + return direction switch + { + MovementDirection.Left=>DirectionType.Left, + MovementDirection.Right=>DirectionType.Right, + MovementDirection.Up=>DirectionType.Up, + MovementDirection.Down=>DirectionType.Down, + _=>DirectionType.Unknow, + }; + } +} \ No newline at end of file diff --git a/AntiAircraftGun/AntiAircraftGun/MovementStrategy/MovementDirection.cs b/AntiAircraftGun/AntiAircraftGun/MovementStrategy/MovementDirection.cs new file mode 100644 index 0000000..010be3d --- /dev/null +++ b/AntiAircraftGun/AntiAircraftGun/MovementStrategy/MovementDirection.cs @@ -0,0 +1,29 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AntiAircraftGun.MovementStrategy; +/// +/// Направление перемещения +/// +public enum MovementDirection +{ + /// + /// Вврех + /// + Up = 1, + /// + /// Вниз + /// + Down = 2, + /// + /// Влево + /// + Left = 3, + /// + /// Вправо + /// + Right = 4, +} diff --git a/AntiAircraftGun/AntiAircraftGun/MovementStrategy/ObjectParameters.cs b/AntiAircraftGun/AntiAircraftGun/MovementStrategy/ObjectParameters.cs new file mode 100644 index 0000000..9dd69c5 --- /dev/null +++ b/AntiAircraftGun/AntiAircraftGun/MovementStrategy/ObjectParameters.cs @@ -0,0 +1,67 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AntiAircraftGun.MovementStrategy; +/// +/// Параметры координаты объекта +/// +public class ObjectParameters +{ + /// + /// Координата x + /// + private readonly int _x; + /// + /// Координата y + /// + private readonly int _y; + /// + /// Ширина объекта + /// + private readonly int _width; + /// + /// Высота объекта + /// + private readonly int _height; + /// + /// Левая граница + /// + public int LeftBorder() => _x; + /// + /// Верхняя граница + /// + public int TopBorder() => _y; + /// + /// Правая граница + /// + public int RightBorder() => _x + _width; + /// + /// Нижняя граница + /// + public int DownBorder() => _y + _height; + /// + /// Середина объекта + /// + public int ObjectMiddleHorizontal => _x + _width / 2; + /// + /// Середина объекта + /// + public int ObjectMiddleVertical => _y + _height / 2; + /// + /// Конструктор + /// + /// + /// + /// + /// + public ObjectParameters(int x, int y, int width, int height) + { + _x = x; + _y = y; + _width = width; + _height = height; + } +} \ No newline at end of file diff --git a/AntiAircraftGun/AntiAircraftGun/MovementStrategy/StrategyStatus.cs b/AntiAircraftGun/AntiAircraftGun/MovementStrategy/StrategyStatus.cs new file mode 100644 index 0000000..d0d0e2d --- /dev/null +++ b/AntiAircraftGun/AntiAircraftGun/MovementStrategy/StrategyStatus.cs @@ -0,0 +1,25 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AntiAircraftGun.MovementStrategy; +/// +/// Статус выполнения операции перемещения +/// +public enum StrategyStatus +{ + /// + /// Все готово к началу + /// + NotInit, + /// + /// Выполянется + /// + InProgress, + /// + /// Завершено + /// + Finish +}