From 18c3e57349c0ce44139914f6821e107ef9c530a3 Mon Sep 17 00:00:00 2001 From: rakhaliullov Date: Thu, 7 Mar 2024 11:16:16 +0300 Subject: [PATCH 1/3] lab2 --- .../{ => Drawnings}/DirectionType.cs | 6 +- .../Drawnings/DrawningAircraft.cs | 125 ++++++++++++++++++ .../{ => Drawnings}/DrawningStormtrooper.cs | 18 +-- .../Stormtrooper/Entities/EntityAircraft.cs | 34 +++++ .../Entities/EntityStormtrooper.cs | 47 +++++++ .../Stormtrooper/EntityStormtrooper.cs | 50 ------- Stormtrooper/Stormtrooper/FormStormtrooper.cs | 1 + 7 files changed, 217 insertions(+), 64 deletions(-) rename Stormtrooper/Stormtrooper/{ => Drawnings}/DirectionType.cs (87%) create mode 100644 Stormtrooper/Stormtrooper/Drawnings/DrawningAircraft.cs rename Stormtrooper/Stormtrooper/{ => Drawnings}/DrawningStormtrooper.cs (94%) create mode 100644 Stormtrooper/Stormtrooper/Entities/EntityAircraft.cs create mode 100644 Stormtrooper/Stormtrooper/Entities/EntityStormtrooper.cs delete mode 100644 Stormtrooper/Stormtrooper/EntityStormtrooper.cs diff --git a/Stormtrooper/Stormtrooper/DirectionType.cs b/Stormtrooper/Stormtrooper/Drawnings/DirectionType.cs similarity index 87% rename from Stormtrooper/Stormtrooper/DirectionType.cs rename to Stormtrooper/Stormtrooper/Drawnings/DirectionType.cs index cf5dd7d..e30a328 100644 --- a/Stormtrooper/Stormtrooper/DirectionType.cs +++ b/Stormtrooper/Stormtrooper/Drawnings/DirectionType.cs @@ -4,17 +4,17 @@ using System.Linq; using System.Text; using System.Threading.Tasks; -namespace Stormtrooper +namespace Stormtrooper.Drawnings { public enum DirectionType { Up = 1, Down = 2, - + Left = 3, Right = 4, - + } } diff --git a/Stormtrooper/Stormtrooper/Drawnings/DrawningAircraft.cs b/Stormtrooper/Stormtrooper/Drawnings/DrawningAircraft.cs new file mode 100644 index 0000000..94c3524 --- /dev/null +++ b/Stormtrooper/Stormtrooper/Drawnings/DrawningAircraft.cs @@ -0,0 +1,125 @@ +using Stormtrooper.Entities; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Stormtrooper.Drawnings; + +public class DrawningAircraft +{ + // Класс-сущность + public EntityAircraft? EntityAircraft { get; private set; } // объявление 1 + + // Ширина окна + + public int? _pictureWidth; + + // Высота окна + + public int? _pictureHeight; + + // Левая координата прорисовки штурмовика + + protected int? _startPosX; + + // Верхняя координата прорисовки автомобиля + + protected int? _startPosY; + + // Ширина прорисовки штурмовика + + private readonly int _drawningAircraftWidth = 120; + + // Высота прорисовки штурмовика + + private readonly int _drawningAircraftHeight = 140; + + // Пустой конструктор + private DrawningAircraft() + { + _pictureWidth = null; + _pictureHeight = null; + _startPosX = null; + _startPosY = null; + } + // Конструктор + + /// Скорость + /// Вес + /// Основной цвет + public DrawningAircraft(int speed, double weight, Color bodyColor) : this() + { + EntityAircraft = new EntityAircraft(speed, weight, bodyColor); + } + + // Конструктор для наследников + + /// Ширина прорисовки штурмовика + /// Высота прорисовки штурмовика + protected DrawningAircraft(int drawningAircraftWidth, int drawningAircraftHeight) : this() + { + _drawningAircraftWidth = drawningAircraftWidth; + _pictureHeight = drawningAircraftHeight; + } + + /// Прорисовка объекта + /// + public virtual void DrawTransport(Graphics g) + { + if (EntityAircraft == null || !_startPosX.HasValue || !_startPosY.HasValue) + { + return; + } + + Pen pen = new(Color.Black); + Brush bodyColorBrush = new SolidBrush(EntityAircraft.BodyColor); + + //нос штурмовика + Brush brBlack = new SolidBrush(Color.Black); + + Point[] Nose = new Point[3]; + Nose[0].X = _startPosX.Value + 10; Nose[0].Y = _startPosY.Value + 70; + Nose[1].X = _startPosX.Value + 10; Nose[1].Y = _startPosY.Value + 50; + Nose[2].X = _startPosX.Value - 10; Nose[2].Y = _startPosY.Value + 60; + g.FillPolygon(brBlack, Nose); + g.DrawPolygon(pen, Nose); + //Заднии крылья штурмовика + + Point[] pflybtwings = new Point[6]; + pflybtwings[0].X = _startPosX.Value + 110; pflybtwings[0].Y = _startPosY.Value + 50; + pflybtwings[1].X = _startPosX.Value + 110; pflybtwings[1].Y = _startPosY.Value + 40; + pflybtwings[2].X = _startPosX.Value + 130; pflybtwings[2].Y = _startPosY.Value + 20; + pflybtwings[3].X = _startPosX.Value + 130; pflybtwings[3].Y = _startPosY.Value + 100; + pflybtwings[4].X = _startPosX.Value + 110; pflybtwings[4].Y = _startPosY.Value + 80; + pflybtwings[5].X = _startPosX.Value + 110; pflybtwings[5].Y = _startPosY.Value + 70; + g.FillPolygon(bodyColorBrush, pflybtwings); + g.DrawPolygon(pen, pflybtwings); + //Тело штурмовика + g.FillRectangle(bodyColorBrush, _startPosX.Value + 10, _startPosY.Value + 50, 120, 20); + g.DrawRectangle(pen, _startPosX.Value + 10, _startPosY.Value + 50, 120, 20); + + + //Крылья штурмовика + + + Point[] frontwings = new Point[4]; + frontwings[0].X = _startPosX.Value + 50; frontwings[0].Y = _startPosY.Value + 50; + frontwings[1].X = _startPosX.Value + 50; frontwings[1].Y = _startPosY.Value - 10; + frontwings[2].X = _startPosX.Value + 60; frontwings[2].Y = _startPosY.Value - 10; + frontwings[3].X = _startPosX.Value + 70; frontwings[3].Y = _startPosY.Value + 50; + g.FillPolygon(bodyColorBrush, frontwings); + g.DrawPolygon(pen, frontwings); + + Point[] frontwings2 = new Point[4]; + frontwings2[0].X = _startPosX.Value + 50; frontwings2[0].Y = _startPosY.Value + 70; + frontwings2[1].X = _startPosX.Value + 50; frontwings2[1].Y = _startPosY.Value + 130; + frontwings2[2].X = _startPosX.Value + 60; frontwings2[2].Y = _startPosY.Value + 130; + frontwings2[3].X = _startPosX.Value + 70; frontwings2[3].Y = _startPosY.Value + 70; + g.FillPolygon(bodyColorBrush, frontwings2); + g.DrawPolygon(pen, frontwings2); + + + } +} diff --git a/Stormtrooper/Stormtrooper/DrawningStormtrooper.cs b/Stormtrooper/Stormtrooper/Drawnings/DrawningStormtrooper.cs similarity index 94% rename from Stormtrooper/Stormtrooper/DrawningStormtrooper.cs rename to Stormtrooper/Stormtrooper/Drawnings/DrawningStormtrooper.cs index 937731a..eb60a20 100644 --- a/Stormtrooper/Stormtrooper/DrawningStormtrooper.cs +++ b/Stormtrooper/Stormtrooper/Drawnings/DrawningStormtrooper.cs @@ -7,12 +7,11 @@ using System.Reflection.Metadata; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; +using Stormtrooper.Entities; -namespace Stormtrooper; -public class DrawningStormtrooper +namespace Stormtrooper.Drawnings; +public class DrawningStormtrooper : DrawningAircraft { - // Класс-сущность - public EntityStormtrooper? EntityStormtrooper { get; private set; } // объявление // Ширина окна @@ -47,15 +46,12 @@ public class DrawningStormtrooper /// Признак наличия ракет /// Признак наличия бомбы /// Признак наличия крыла - public void Init(int speed, double weight, Color bodyColor, Color + public DrawningStormtrooper(int speed, double weight, Color bodyColor, Color additionalColor, bool rocket, bool bomb, bool wing) { - EntityStormtrooper = new EntityStormtrooper(); // инициализация - EntityStormtrooper.Init(speed, weight, bodyColor, additionalColor,rocket, bomb, wing); - _pictureWidth = null; - _pictureHeight = null; - _startPosX = null; - _startPosY = null; + EntityStormtrooper = new EntityStormtrooper(); + EntityStormtrooper.Init(speed, weight, bodyColor, additionalColor, rocket, bomb, wing); + } // Установка границ поля diff --git a/Stormtrooper/Stormtrooper/Entities/EntityAircraft.cs b/Stormtrooper/Stormtrooper/Entities/EntityAircraft.cs new file mode 100644 index 0000000..c81d626 --- /dev/null +++ b/Stormtrooper/Stormtrooper/Entities/EntityAircraft.cs @@ -0,0 +1,34 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net.NetworkInformation; +using System.Net.Sockets; +using System.Text; +using System.Threading.Tasks; + +namespace Stormtrooper.Entities; + +// Класс-сущность "самолет" +public class EntityAircraft +{ + public int Speed { get; private set; } + // скорость + public double Weight { get; private set; } + // вес + public Color BodyColor { get; private set; } + // цвет + public double Step => Speed * 100 / Weight; + // шаг в поле + /// Конструктор сущности + /// Скорость + /// Вес штурмовика + /// Основной цвет + + public EntityAircraft(int speed, double weight, Color bodyColor) + { + Speed = speed; + Weight = weight; + BodyColor = bodyColor; + + } +} diff --git a/Stormtrooper/Stormtrooper/Entities/EntityStormtrooper.cs b/Stormtrooper/Stormtrooper/Entities/EntityStormtrooper.cs new file mode 100644 index 0000000..da05b84 --- /dev/null +++ b/Stormtrooper/Stormtrooper/Entities/EntityStormtrooper.cs @@ -0,0 +1,47 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Stormtrooper.Entities; + +public class EntityStormtrooper : EntityAircraft +{ + 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 Rocket { get; private set; } + // ракета + public bool Bomb { get; private set; } + // бомба + public bool Wing { get; private set; } + // крыло + + /// Инициализация полей объекта-класса штурмовика + + /// Скорость + /// Вес штурмовика + /// Основной цвет + /// Дополнительный цвет + /// Признак наличия ракет + /// Признак наличия бомб + /// Признак наличия крыла а + + public EntityStormtrooper(int speed, double weight, Color bodyColor, Color + additionalColor, bool rocket, bool bomb, bool wing) : base(speed, weight, bodyColor) + { + Speed = speed; + Weight = weight; + BodyColor = bodyColor; + AdditionalColor = additionalColor; + Rocket = rocket; + Bomb = bomb; + Wing = wing; + } +} diff --git a/Stormtrooper/Stormtrooper/EntityStormtrooper.cs b/Stormtrooper/Stormtrooper/EntityStormtrooper.cs deleted file mode 100644 index 3a6e4cf..0000000 --- a/Stormtrooper/Stormtrooper/EntityStormtrooper.cs +++ /dev/null @@ -1,50 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace Stormtrooper -{ - public class EntityStormtrooper - { - 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 Rocket { get; private set; } - // ракета - public bool Bomb { get; private set; } - // бомба - public bool Wing { get; private set; } - // крыло - public double Step => Speed * 100 / Weight; - // шаг в поле - - /// Инициализация полей объекта-класса штурмовика - - /// Скорость - /// Вес штурмовика - /// Основной цвет - /// Дополнительный цвет - /// Признак наличия ракет - /// Признак наличия бомб - /// Признак наличия крыла а - - public void Init(int speed, double weight, Color bodyColor, Color - additionalColor, bool rocket, bool bomb, bool wing) - { - Speed = speed; - Weight = weight; - BodyColor = bodyColor; - AdditionalColor = additionalColor; - Rocket = rocket; - Bomb = bomb; - Wing = wing; - } - } -} diff --git a/Stormtrooper/Stormtrooper/FormStormtrooper.cs b/Stormtrooper/Stormtrooper/FormStormtrooper.cs index 00bc307..58c6536 100644 --- a/Stormtrooper/Stormtrooper/FormStormtrooper.cs +++ b/Stormtrooper/Stormtrooper/FormStormtrooper.cs @@ -7,6 +7,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; +using Stormtrooper.Drawnings; namespace Stormtrooper { -- 2.25.1 From 8d308dab1124868b7b32dfc560cda721c4097f40 Mon Sep 17 00:00:00 2001 From: rakhaliullov Date: Thu, 7 Mar 2024 14:27:50 +0300 Subject: [PATCH 2/3] lab2 --- .../Drawnings/DrawningAircraft.cs | 95 ++++++++- .../Drawnings/DrawningStormtrooper.cs | 189 +----------------- .../Entities/EntityStormtrooper.cs | 3 - .../Stormtrooper/FormStormtrooper.Designer.cs | 20 +- Stormtrooper/Stormtrooper/FormStormtrooper.cs | 158 ++++++++------- 5 files changed, 209 insertions(+), 256 deletions(-) diff --git a/Stormtrooper/Stormtrooper/Drawnings/DrawningAircraft.cs b/Stormtrooper/Stormtrooper/Drawnings/DrawningAircraft.cs index 94c3524..1c54f1d 100644 --- a/Stormtrooper/Stormtrooper/Drawnings/DrawningAircraft.cs +++ b/Stormtrooper/Stormtrooper/Drawnings/DrawningAircraft.cs @@ -10,7 +10,7 @@ namespace Stormtrooper.Drawnings; public class DrawningAircraft { // Класс-сущность - public EntityAircraft? EntityAircraft { get; private set; } // объявление 1 + public EntityAircraft? EntityAircraft { get; protected set; } // объявление 1 // Ширина окна @@ -66,6 +66,99 @@ public class DrawningAircraft /// Прорисовка объекта /// + /// // Установка границ поля + + /// Ширина поля + /// Высота поля + public bool SetPictureSize(int width, int height) + { + if (_drawningAircraftHeight > height || _drawningAircraftWidth > width) + { + return false; + } + + _pictureWidth = width; + _pictureHeight = height; + + if (_startPosX.HasValue && _startPosY.HasValue) + { + SetPosition(_startPosX.Value, _startPosY.Value); + } + + return true; + } + + // Установка позиции + /// Координата X + /// Координата Y + public void SetPosition(int x, int y) + { + if (!_pictureHeight.HasValue || !_pictureWidth.HasValue) + { + return; + } + + if (x < 0 || x + _drawningAircraftWidth > _pictureWidth) + { + _startPosX = _pictureWidth - _drawningAircraftWidth; + } + else + { + _startPosX = x; + } + + if (y < 0 || y + _drawningAircraftHeight > _pictureHeight) + { + _startPosY = _pictureHeight - _drawningAircraftHeight; + } + else + { + _startPosY = y; + } + } + + // Изменение направления перемещения + public bool MoveTransport(DirectionType direction) + { + if (EntityAircraft == null || !_startPosX.HasValue || !_startPosY.HasValue) + { + return false; + } + switch (direction) + { + //влево + case DirectionType.Left: + if (_startPosX.Value - EntityAircraft.Step > 0) + { + _startPosX -= (int)EntityAircraft.Step; + } + return true; + //вверх + case DirectionType.Up: + if (_startPosY.Value - EntityAircraft.Step > 0) + { + _startPosY -= (int)EntityAircraft.Step; + } + return true; + // вправо + case DirectionType.Right: + if (_startPosX.Value + EntityAircraft.Step + _drawningAircraftWidth < _pictureWidth) + { + _startPosX += (int)EntityAircraft.Step; + return true; + } + return true; + //вниз + case DirectionType.Down: + if (_startPosY.Value + EntityAircraft.Step + _drawningAircraftHeight < _pictureHeight) + { + _startPosY += (int)EntityAircraft.Step; + } + return true; + default: + return false; + } + } public virtual void DrawTransport(Graphics g) { if (EntityAircraft == null || !_startPosX.HasValue || !_startPosY.HasValue) diff --git a/Stormtrooper/Stormtrooper/Drawnings/DrawningStormtrooper.cs b/Stormtrooper/Stormtrooper/Drawnings/DrawningStormtrooper.cs index eb60a20..201e9a3 100644 --- a/Stormtrooper/Stormtrooper/Drawnings/DrawningStormtrooper.cs +++ b/Stormtrooper/Stormtrooper/Drawnings/DrawningStormtrooper.cs @@ -12,32 +12,7 @@ using Stormtrooper.Entities; namespace Stormtrooper.Drawnings; public class DrawningStormtrooper : DrawningAircraft { - - // Ширина окна - - public int? _pictureWidth; - - // Высота окна - - public int? _pictureHeight; - - // Левая координата прорисовки штурмовика - - public int? _startPosX; - - // Верхняя координата прорисовки автомобиля - - public int? _startPosY; - - // Ширина прорисовки штурмовика - - private readonly int _drawningStormtrooperWidth = 140; - - // Высота прорисовки штурмовика - - private readonly int _drawningStormtrooperHeight = 150; - - // Инициализация свойств + // Конструктор /// Скорость /// Вес @@ -47,168 +22,26 @@ public class DrawningStormtrooper : DrawningAircraft /// Признак наличия бомбы /// Признак наличия крыла public DrawningStormtrooper(int speed, double weight, Color bodyColor, Color - additionalColor, bool rocket, bool bomb, bool wing) + additionalColor, bool rocket, bool bomb, bool wing) : base(140,150) { - EntityStormtrooper = new EntityStormtrooper(); - EntityStormtrooper.Init(speed, weight, bodyColor, additionalColor, rocket, bomb, wing); + EntityAircraft = new EntityStormtrooper(speed, weight, bodyColor, additionalColor, rocket, bomb, wing); } - // Установка границ поля - /// Ширина поля - /// Высота поля - public bool SetPictureSize(int width, int height) + public override void DrawTransport(Graphics g) { - if (_drawningStormtrooperHeight > height || _drawningStormtrooperWidth > width) - { - return false; - } - - _pictureWidth = width; - _pictureHeight = height; - - if (_startPosX.HasValue && _startPosY.HasValue) - { - SetPosition(_startPosX.Value, _startPosY.Value); - } - - return true; - } - - // Установка позиции - /// Координата X - /// Координата Y - public void SetPosition(int x, int y) - { - if (!_pictureHeight.HasValue || !_pictureWidth.HasValue) + if (EntityAircraft == null || EntityAircraft is not EntityStormtrooper entityStormtrooper|| !_startPosX.HasValue || !_startPosY.HasValue) { return; } - - if (x < 0 || x + _drawningStormtrooperWidth > _pictureWidth) - { - _startPosX = _pictureWidth - _drawningStormtrooperWidth; - } - else - { - _startPosX = x; - } - - if (y < 0 || y + _drawningStormtrooperHeight > _pictureHeight) - { - _startPosY = _pictureHeight - _drawningStormtrooperHeight; - } - else - { - _startPosY = y; - } - } - - // Изменение направления перемещения - public bool MoveTransport(DirectionType direction) - { - if (EntityStormtrooper == null || !_startPosX.HasValue || !_startPosY.HasValue) - { - return false; - } - switch (direction) - { - //влево - case DirectionType.Left: - if (_startPosX.Value - EntityStormtrooper.Step > 0) - { - _startPosX -= (int)EntityStormtrooper.Step; - } - return true; - //вверх - case DirectionType.Up: - if (_startPosY.Value - EntityStormtrooper.Step > 0) - { - _startPosY -= (int)EntityStormtrooper.Step; - } - return true; - // вправо - case DirectionType.Right: - if (_startPosX.Value + EntityStormtrooper.Step + _drawningStormtrooperWidth < _pictureWidth) - { - _startPosX += (int)EntityStormtrooper.Step; - return true; - } - return true; - //вниз - case DirectionType.Down: - if (_startPosY.Value + EntityStormtrooper.Step + _drawningStormtrooperHeight < _pictureHeight) - { - _startPosY += (int)EntityStormtrooper.Step; - } - return true; - default: - return false; - } - } - /// Прорисовка объекта - /// - public void DrawTransport(Graphics g) - { - if (EntityStormtrooper == null || !_startPosX.HasValue || !_startPosY.HasValue) - { - return; - } - Pen pen = new(Color.Black); - Brush bodyColorBrush = new SolidBrush(EntityStormtrooper.BodyColor); - Brush additionalBrush = new SolidBrush(EntityStormtrooper.AdditionalColor); - - //нос штурмовика - Brush brBlack = new SolidBrush(Color.Black); - - Point[] Nose = new Point[3]; - Nose[0].X = _startPosX.Value + 20; Nose[0].Y = _startPosY.Value + 80; - Nose[1].X = _startPosX.Value + 20; Nose[1].Y = _startPosY.Value + 60; - Nose[2].X = _startPosX.Value; Nose[2].Y = _startPosY.Value + 70; - g.FillPolygon(brBlack, Nose); - g.DrawPolygon(pen, Nose); - //Заднии крылья штурмовика - - Point[] pflybtwings = new Point[6]; - pflybtwings[0].X = _startPosX.Value + 120; pflybtwings[0].Y = _startPosY.Value + 60; - pflybtwings[1].X = _startPosX.Value + 120; pflybtwings[1].Y = _startPosY.Value + 50; - pflybtwings[2].X = _startPosX.Value + 140; pflybtwings[2].Y = _startPosY.Value + 30; - pflybtwings[3].X = _startPosX.Value + 140; pflybtwings[3].Y = _startPosY.Value + 110; - pflybtwings[4].X = _startPosX.Value + 120; pflybtwings[4].Y = _startPosY.Value + 90; - pflybtwings[5].X = _startPosX.Value + 120; pflybtwings[5].Y = _startPosY.Value + 80; - g.FillPolygon(bodyColorBrush, pflybtwings); - g.DrawPolygon(pen, pflybtwings); - //Тело штурмовика - g.FillRectangle(bodyColorBrush, _startPosX.Value + 20, _startPosY.Value + 60, 120, 20); - g.DrawRectangle(pen, _startPosX.Value + 20, _startPosY.Value + 60, 120, 20); - - - //Крылья штурмовика - - - Point[] frontwings = new Point[4]; - frontwings[0].X = _startPosX.Value + 60; frontwings[0].Y = _startPosY.Value + 60; - frontwings[1].X = _startPosX.Value + 60; frontwings[1].Y = _startPosY.Value; - frontwings[2].X = _startPosX.Value + 70; frontwings[2].Y = _startPosY.Value; - frontwings[3].X = _startPosX.Value + 80; frontwings[3].Y = _startPosY.Value + 60; - g.FillPolygon(bodyColorBrush, frontwings); - g.DrawPolygon(pen, frontwings); - - Point[] frontwings2 = new Point[4]; - frontwings2[0].X = _startPosX.Value + 60; frontwings2[0].Y = _startPosY.Value + 80; - frontwings2[1].X = _startPosX.Value + 60; frontwings2[1].Y = _startPosY.Value + 140; - frontwings2[2].X = _startPosX.Value + 70; frontwings2[2].Y = _startPosY.Value + 140; - frontwings2[3].X = _startPosX.Value + 80; frontwings2[3].Y = _startPosY.Value + 80; - g.FillPolygon(bodyColorBrush, frontwings2); - g.DrawPolygon(pen, frontwings2); - + Brush bodyColorBrush = new SolidBrush(entityStormtrooper.BodyColor); + Brush additionalBrush = new SolidBrush(entityStormtrooper.AdditionalColor); + base.DrawTransport(g); + //Ракеты штурмовика - - - - if (EntityStormtrooper.Rocket) + if (entityStormtrooper.Rocket) { Point[] rockets = new Point[3]; rockets[0].X = _startPosX.Value + 45; rockets[0].Y = _startPosY.Value + 20; @@ -233,7 +66,7 @@ public class DrawningStormtrooper : DrawningAircraft //Бомбы бомбардировщика - if (EntityStormtrooper.Bomb) + if (entityStormtrooper.Bomb) { g.FillRectangle(additionalBrush, _startPosX.Value + 50, _startPosY.Value + 40, 10, 10); g.FillRectangle(additionalBrush, _startPosX.Value + 50, _startPosY.Value + 90, 10, 10); diff --git a/Stormtrooper/Stormtrooper/Entities/EntityStormtrooper.cs b/Stormtrooper/Stormtrooper/Entities/EntityStormtrooper.cs index da05b84..bbc70c5 100644 --- a/Stormtrooper/Stormtrooper/Entities/EntityStormtrooper.cs +++ b/Stormtrooper/Stormtrooper/Entities/EntityStormtrooper.cs @@ -36,9 +36,6 @@ public class EntityStormtrooper : EntityAircraft public EntityStormtrooper(int speed, double weight, Color bodyColor, Color additionalColor, bool rocket, bool bomb, bool wing) : base(speed, weight, bodyColor) { - Speed = speed; - Weight = weight; - BodyColor = bodyColor; AdditionalColor = additionalColor; Rocket = rocket; Bomb = bomb; diff --git a/Stormtrooper/Stormtrooper/FormStormtrooper.Designer.cs b/Stormtrooper/Stormtrooper/FormStormtrooper.Designer.cs index 3561ad3..686930c 100644 --- a/Stormtrooper/Stormtrooper/FormStormtrooper.Designer.cs +++ b/Stormtrooper/Stormtrooper/FormStormtrooper.Designer.cs @@ -34,6 +34,7 @@ buttonDown = new Button(); buttonLeft = new Button(); buttonRight = new Button(); + buttonCreateAircraft = new Button(); ((System.ComponentModel.ISupportInitialize)pictureBoxStormtrooper).BeginInit(); SuspendLayout(); // @@ -50,11 +51,11 @@ // buttonCreate // buttonCreate.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; - buttonCreate.Location = new Point(12, 398); + buttonCreate.Location = new Point(12, 395); buttonCreate.Name = "buttonCreate"; - buttonCreate.Size = new Size(94, 29); + buttonCreate.Size = new Size(205, 32); buttonCreate.TabIndex = 1; - buttonCreate.Text = "Создать"; + buttonCreate.Text = "Создать штурмовик"; buttonCreate.UseVisualStyleBackColor = true; buttonCreate.Click += ButtonCreate_Click; // @@ -106,11 +107,23 @@ buttonRight.UseVisualStyleBackColor = true; buttonRight.Click += ButtonMove_Click; // + // buttonCreateAircraft + // + buttonCreateAircraft.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; + buttonCreateAircraft.Location = new Point(239, 395); + buttonCreateAircraft.Name = "buttonCreateAircraft"; + buttonCreateAircraft.Size = new Size(205, 32); + buttonCreateAircraft.TabIndex = 7; + buttonCreateAircraft.Text = "Создать самолет"; + buttonCreateAircraft.UseVisualStyleBackColor = true; + buttonCreateAircraft.Click += buttonCreateAircraft_Click; + // // FormStormtrooper // AutoScaleDimensions = new SizeF(8F, 20F); AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(773, 439); + Controls.Add(buttonCreateAircraft); Controls.Add(buttonRight); Controls.Add(buttonLeft); Controls.Add(buttonDown); @@ -133,5 +146,6 @@ private Button buttonDown; private Button buttonLeft; private Button buttonRight; + private Button buttonCreateAircraft; } } \ No newline at end of file diff --git a/Stormtrooper/Stormtrooper/FormStormtrooper.cs b/Stormtrooper/Stormtrooper/FormStormtrooper.cs index 58c6536..6ebbe5c 100644 --- a/Stormtrooper/Stormtrooper/FormStormtrooper.cs +++ b/Stormtrooper/Stormtrooper/FormStormtrooper.cs @@ -9,88 +9,104 @@ using System.Threading.Tasks; using System.Windows.Forms; using Stormtrooper.Drawnings; -namespace Stormtrooper +namespace Stormtrooper; + +public partial class FormStormtrooper : Form { - public partial class FormStormtrooper : Form + // Поле объект для прорисовки объекта + public DrawningAircraft? _drawningAircraft; + // Конструктор формы + public FormStormtrooper() { - // Поле объект для прорисовки объекта - public DrawningStormtrooper? _drawningStormtrooper; - // Конструктор формы - public FormStormtrooper() - { - InitializeComponent(); - } + InitializeComponent(); + } - // Метод прорисовки машины - private void FormStormtrooper_Load(object sender, EventArgs e) - { + // Метод прорисовки машины + private void FormStormtrooper_Load(object sender, EventArgs e) + { - } - - // Обработка нажатия кнопки Создать - private void ButtonCreate_Click(object sender, EventArgs e) + } + /// + /// Создание объекта класса-перемещения + /// + /// Тип создаваемого объекта + private void CreateObject(string type) + { + Random random = new(); + switch (type) { - Random random = new(); - _drawningStormtrooper = new DrawningStormtrooper(); - _drawningStormtrooper.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)), Convert.ToBoolean(random.Next(0, 2))); - _drawningStormtrooper.SetPictureSize(pictureBoxStormtrooper.Width, pictureBoxStormtrooper.Height); - _drawningStormtrooper.SetPosition(random.Next(10, 100), random.Next(10, 100)); - - Bitmap bmp = new(pictureBoxStormtrooper.Width, pictureBoxStormtrooper.Height); - Graphics gr = Graphics.FromImage(bmp); - _drawningStormtrooper.DrawTransport(gr); - pictureBoxStormtrooper.Image = bmp; - } - private void Draw() - { - if (_drawningStormtrooper == null) - { + case nameof(DrawningAircraft): + _drawningAircraft = new DrawningAircraft(random.Next(100, + 300), random.Next(1000, 3000), + Color.FromArgb(random.Next(0, 256))); + break; + case nameof(DrawningStormtrooper): + _drawningAircraft = new DrawningStormtrooper(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))); + break; + default: return; - } - Bitmap bmp = new(pictureBoxStormtrooper.Width, - pictureBoxStormtrooper.Height); - Graphics gr = Graphics.FromImage(bmp); - _drawningStormtrooper.DrawTransport(gr); - pictureBoxStormtrooper.Image = bmp; } - private void ButtonMove_Click(object sender, EventArgs e) + _drawningAircraft.SetPictureSize(pictureBoxStormtrooper.Width, + pictureBoxStormtrooper.Height); + _drawningAircraft.SetPosition(random.Next(10, 100), random.Next(10, 100)); + Draw(); + } + + // Обработка нажатия кнопки Создать штурмовик + private void ButtonCreate_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningStormtrooper)); + // Обработка нажатия кнопки Создать самолет + private void buttonCreateAircraft_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningAircraft)); + // Прорисовка + private void Draw() + { + if (_drawningAircraft == null) { - if (_drawningStormtrooper == null) - { - return; - } - - string name = ((Button)sender)?.Name ?? string.Empty; - bool result = false; - switch (name) - { - case "buttonUp": - result = _drawningStormtrooper.MoveTransport(DirectionType.Up); - break; - case "buttonDown": - result = _drawningStormtrooper.MoveTransport(DirectionType.Down); - break; - case "buttonLeft": - result = _drawningStormtrooper.MoveTransport(DirectionType.Left); - break; - case "buttonRight": - result = _drawningStormtrooper.MoveTransport(DirectionType.Right); - break; - } - if (result) - { - Draw(); - } - - + return; + } + Bitmap bmp = new(pictureBoxStormtrooper.Width, + pictureBoxStormtrooper.Height); + Graphics gr = Graphics.FromImage(bmp); + _drawningAircraft.DrawTransport(gr); + pictureBoxStormtrooper.Image = bmp; + } + // Перемещение объекта по форме + private void ButtonMove_Click(object sender, EventArgs e) + { + if (_drawningAircraft == null) + { + return; } - private void pictureBoxStormtrooper_Click(object sender, EventArgs e) + string name = ((Button)sender)?.Name ?? string.Empty; + bool result = false; + switch (name) { - + case "buttonUp": + result = _drawningAircraft.MoveTransport(DirectionType.Up); + break; + case "buttonDown": + result = _drawningAircraft.MoveTransport(DirectionType.Down); + break; + case "buttonLeft": + result = _drawningAircraft.MoveTransport(DirectionType.Left); + break; + case "buttonRight": + result = _drawningAircraft.MoveTransport(DirectionType.Right); + break; + } + if (result) + { + Draw(); } } + + private void pictureBoxStormtrooper_Click(object sender, EventArgs e) + { + + } + + } -- 2.25.1 From 085834970cc3a2931cdd8350e91c7039afc082d1 Mon Sep 17 00:00:00 2001 From: rakhaliullov Date: Thu, 14 Mar 2024 13:53:17 +0300 Subject: [PATCH 3/3] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5=20=D1=81=D1=82=D1=80=D0=B0=D1=82=D0=B5=D0=B3?= =?UTF-8?q?=D0=B8=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Stormtrooper/Drawnings/DirectionType.cs | 26 ++-- .../Drawnings/DrawningAircraft.cs | 55 +++++--- .../Drawnings/DrawningStormtrooper.cs | 2 +- .../Stormtrooper/FormStormtrooper.Designer.cs | 27 ++++ Stormtrooper/Stormtrooper/FormStormtrooper.cs | 54 +++++++- .../MovementStrategy/AbstractStrategy.cs | 121 ++++++++++++++++++ .../MovementStrategy/IMoveableObject.cs | 22 ++++ .../MovementStrategy/MoveToBorder.cs | 52 ++++++++ .../MovementStrategy/MoveToCenter.cs | 52 ++++++++ .../MovementStrategy/MoveableAircraft.cs | 62 +++++++++ .../MovementStrategy/MovementDirection.cs | 27 ++++ .../MovementStrategy/ObjectParameters.cs | 64 +++++++++ .../MovementStrategy/StrategyStatus.cs | 23 ++++ 13 files changed, 556 insertions(+), 31 deletions(-) create mode 100644 Stormtrooper/Stormtrooper/MovementStrategy/AbstractStrategy.cs create mode 100644 Stormtrooper/Stormtrooper/MovementStrategy/IMoveableObject.cs create mode 100644 Stormtrooper/Stormtrooper/MovementStrategy/MoveToBorder.cs create mode 100644 Stormtrooper/Stormtrooper/MovementStrategy/MoveToCenter.cs create mode 100644 Stormtrooper/Stormtrooper/MovementStrategy/MoveableAircraft.cs create mode 100644 Stormtrooper/Stormtrooper/MovementStrategy/MovementDirection.cs create mode 100644 Stormtrooper/Stormtrooper/MovementStrategy/ObjectParameters.cs create mode 100644 Stormtrooper/Stormtrooper/MovementStrategy/StrategyStatus.cs diff --git a/Stormtrooper/Stormtrooper/Drawnings/DirectionType.cs b/Stormtrooper/Stormtrooper/Drawnings/DirectionType.cs index e30a328..7d90484 100644 --- a/Stormtrooper/Stormtrooper/Drawnings/DirectionType.cs +++ b/Stormtrooper/Stormtrooper/Drawnings/DirectionType.cs @@ -4,17 +4,27 @@ using System.Linq; using System.Text; using System.Threading.Tasks; -namespace Stormtrooper.Drawnings +namespace Stormtrooper.Drawnings; + +public enum DirectionType { - public enum DirectionType - { - Up = 1, + // Неизвестное направление + Unknow = -1, - Down = 2, + // Вверх - Left = 3, + Up = 1, - Right = 4, + // Вниз + + Down = 2, + + // Влево + + Left = 3, + + // Вправо + + Right = 4, - } } diff --git a/Stormtrooper/Stormtrooper/Drawnings/DrawningAircraft.cs b/Stormtrooper/Stormtrooper/Drawnings/DrawningAircraft.cs index 1c54f1d..3127ca7 100644 --- a/Stormtrooper/Stormtrooper/Drawnings/DrawningAircraft.cs +++ b/Stormtrooper/Stormtrooper/Drawnings/DrawningAircraft.cs @@ -30,11 +30,24 @@ public class DrawningAircraft // Ширина прорисовки штурмовика - private readonly int _drawningAircraftWidth = 120; + private readonly int _drawningAircraftWidth = 140; // Высота прорисовки штурмовика - private readonly int _drawningAircraftHeight = 140; + private readonly int _drawningAircraftHeight = 135; + public int? GetPosX => _startPosX; + /// + /// Координата Y объекта + /// + public int? GetPosY => _startPosY; + /// + /// Ширина объекта + /// + public int GetWidth => _drawningAircraftWidth; + /// + /// Высота объекта + /// + public int GetHeight => _drawningAircraftHeight; // Пустой конструктор private DrawningAircraft() @@ -173,43 +186,43 @@ public class DrawningAircraft Brush brBlack = new SolidBrush(Color.Black); Point[] Nose = new Point[3]; - Nose[0].X = _startPosX.Value + 10; Nose[0].Y = _startPosY.Value + 70; - Nose[1].X = _startPosX.Value + 10; Nose[1].Y = _startPosY.Value + 50; - Nose[2].X = _startPosX.Value - 10; Nose[2].Y = _startPosY.Value + 60; + Nose[0].X = _startPosX.Value + 20; Nose[0].Y = _startPosY.Value + 80; + Nose[1].X = _startPosX.Value + 20; Nose[1].Y = _startPosY.Value + 60; + Nose[2].X = _startPosX.Value; Nose[2].Y = _startPosY.Value + 70; g.FillPolygon(brBlack, Nose); g.DrawPolygon(pen, Nose); //Заднии крылья штурмовика Point[] pflybtwings = new Point[6]; - pflybtwings[0].X = _startPosX.Value + 110; pflybtwings[0].Y = _startPosY.Value + 50; - pflybtwings[1].X = _startPosX.Value + 110; pflybtwings[1].Y = _startPosY.Value + 40; - pflybtwings[2].X = _startPosX.Value + 130; pflybtwings[2].Y = _startPosY.Value + 20; - pflybtwings[3].X = _startPosX.Value + 130; pflybtwings[3].Y = _startPosY.Value + 100; - pflybtwings[4].X = _startPosX.Value + 110; pflybtwings[4].Y = _startPosY.Value + 80; - pflybtwings[5].X = _startPosX.Value + 110; pflybtwings[5].Y = _startPosY.Value + 70; + pflybtwings[0].X = _startPosX.Value + 120; pflybtwings[0].Y = _startPosY.Value + 60; + pflybtwings[1].X = _startPosX.Value + 120; pflybtwings[1].Y = _startPosY.Value + 50; + pflybtwings[2].X = _startPosX.Value + 140; pflybtwings[2].Y = _startPosY.Value + 30; + pflybtwings[3].X = _startPosX.Value + 140; pflybtwings[3].Y = _startPosY.Value + 110; + pflybtwings[4].X = _startPosX.Value + 120; pflybtwings[4].Y = _startPosY.Value + 90; + pflybtwings[5].X = _startPosX.Value + 120; pflybtwings[5].Y = _startPosY.Value + 80; g.FillPolygon(bodyColorBrush, pflybtwings); g.DrawPolygon(pen, pflybtwings); //Тело штурмовика - g.FillRectangle(bodyColorBrush, _startPosX.Value + 10, _startPosY.Value + 50, 120, 20); - g.DrawRectangle(pen, _startPosX.Value + 10, _startPosY.Value + 50, 120, 20); + g.FillRectangle(bodyColorBrush, _startPosX.Value + 20, _startPosY.Value + 60, 120, 20); + g.DrawRectangle(pen, _startPosX.Value + 20, _startPosY.Value + 60, 120, 20); //Крылья штурмовика Point[] frontwings = new Point[4]; - frontwings[0].X = _startPosX.Value + 50; frontwings[0].Y = _startPosY.Value + 50; - frontwings[1].X = _startPosX.Value + 50; frontwings[1].Y = _startPosY.Value - 10; - frontwings[2].X = _startPosX.Value + 60; frontwings[2].Y = _startPosY.Value - 10; - frontwings[3].X = _startPosX.Value + 70; frontwings[3].Y = _startPosY.Value + 50; + frontwings[0].X = _startPosX.Value + 60; frontwings[0].Y = _startPosY.Value + 60; + frontwings[1].X = _startPosX.Value + 60; frontwings[1].Y = _startPosY.Value; + frontwings[2].X = _startPosX.Value + 70; frontwings[2].Y = _startPosY.Value; + frontwings[3].X = _startPosX.Value + 80; frontwings[3].Y = _startPosY.Value + 60; g.FillPolygon(bodyColorBrush, frontwings); g.DrawPolygon(pen, frontwings); Point[] frontwings2 = new Point[4]; - frontwings2[0].X = _startPosX.Value + 50; frontwings2[0].Y = _startPosY.Value + 70; - frontwings2[1].X = _startPosX.Value + 50; frontwings2[1].Y = _startPosY.Value + 130; - frontwings2[2].X = _startPosX.Value + 60; frontwings2[2].Y = _startPosY.Value + 130; - frontwings2[3].X = _startPosX.Value + 70; frontwings2[3].Y = _startPosY.Value + 70; + frontwings2[0].X = _startPosX.Value + 60; frontwings2[0].Y = _startPosY.Value + 80; + frontwings2[1].X = _startPosX.Value + 60; frontwings2[1].Y = _startPosY.Value + 140; + frontwings2[2].X = _startPosX.Value + 70; frontwings2[2].Y = _startPosY.Value + 140; + frontwings2[3].X = _startPosX.Value + 80; frontwings2[3].Y = _startPosY.Value + 80; g.FillPolygon(bodyColorBrush, frontwings2); g.DrawPolygon(pen, frontwings2); diff --git a/Stormtrooper/Stormtrooper/Drawnings/DrawningStormtrooper.cs b/Stormtrooper/Stormtrooper/Drawnings/DrawningStormtrooper.cs index 201e9a3..21d7fab 100644 --- a/Stormtrooper/Stormtrooper/Drawnings/DrawningStormtrooper.cs +++ b/Stormtrooper/Stormtrooper/Drawnings/DrawningStormtrooper.cs @@ -22,7 +22,7 @@ public class DrawningStormtrooper : DrawningAircraft /// Признак наличия бомбы /// Признак наличия крыла public DrawningStormtrooper(int speed, double weight, Color bodyColor, Color - additionalColor, bool rocket, bool bomb, bool wing) : base(140,150) + additionalColor, bool rocket, bool bomb, bool wing) : base(140,135) { EntityAircraft = new EntityStormtrooper(speed, weight, bodyColor, additionalColor, rocket, bomb, wing); diff --git a/Stormtrooper/Stormtrooper/FormStormtrooper.Designer.cs b/Stormtrooper/Stormtrooper/FormStormtrooper.Designer.cs index 686930c..3be5a11 100644 --- a/Stormtrooper/Stormtrooper/FormStormtrooper.Designer.cs +++ b/Stormtrooper/Stormtrooper/FormStormtrooper.Designer.cs @@ -35,6 +35,8 @@ buttonLeft = new Button(); buttonRight = new Button(); buttonCreateAircraft = new Button(); + comboBoxStrategy = new ComboBox(); + buttonStrategyStep = new Button(); ((System.ComponentModel.ISupportInitialize)pictureBoxStormtrooper).BeginInit(); SuspendLayout(); // @@ -118,11 +120,34 @@ buttonCreateAircraft.UseVisualStyleBackColor = true; buttonCreateAircraft.Click += buttonCreateAircraft_Click; // + // comboBoxStrategy + // + comboBoxStrategy.DropDownStyle = ComboBoxStyle.DropDownList; + comboBoxStrategy.FormattingEnabled = true; + comboBoxStrategy.Items.AddRange(new object[] { "К центру", "К краю" }); + comboBoxStrategy.Location = new Point(610, 12); + comboBoxStrategy.Name = "comboBoxStrategy"; + comboBoxStrategy.Size = new Size(151, 28); + comboBoxStrategy.TabIndex = 8; + comboBoxStrategy.SelectedIndexChanged += comboBox1_SelectedIndexChanged; + // + // buttonStrategyStep + // + buttonStrategyStep.Location = new Point(667, 46); + buttonStrategyStep.Name = "buttonStrategyStep"; + buttonStrategyStep.Size = new Size(94, 29); + buttonStrategyStep.TabIndex = 9; + buttonStrategyStep.Text = "Шаг"; + buttonStrategyStep.UseVisualStyleBackColor = true; + buttonStrategyStep.Click += buttonStrategyStep_Click; + // // FormStormtrooper // AutoScaleDimensions = new SizeF(8F, 20F); AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(773, 439); + Controls.Add(buttonStrategyStep); + Controls.Add(comboBoxStrategy); Controls.Add(buttonCreateAircraft); Controls.Add(buttonRight); Controls.Add(buttonLeft); @@ -147,5 +172,7 @@ private Button buttonLeft; private Button buttonRight; private Button buttonCreateAircraft; + private ComboBox comboBoxStrategy; + private Button buttonStrategyStep; } } \ No newline at end of file diff --git a/Stormtrooper/Stormtrooper/FormStormtrooper.cs b/Stormtrooper/Stormtrooper/FormStormtrooper.cs index 6ebbe5c..236594e 100644 --- a/Stormtrooper/Stormtrooper/FormStormtrooper.cs +++ b/Stormtrooper/Stormtrooper/FormStormtrooper.cs @@ -7,7 +7,9 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; +using Aircraft.MovementStrategy; using Stormtrooper.Drawnings; +using Stormtrooper.MovementStrategy; namespace Stormtrooper; @@ -15,10 +17,16 @@ public partial class FormStormtrooper : Form { // Поле объект для прорисовки объекта public DrawningAircraft? _drawningAircraft; + + // Стратегия перемещения + + private AbstractStrategy? _strategy; + // Конструктор формы public FormStormtrooper() { InitializeComponent(); + _strategy = null; } // Метод прорисовки машины @@ -38,7 +46,7 @@ public partial class FormStormtrooper : Form case nameof(DrawningAircraft): _drawningAircraft = new DrawningAircraft(random.Next(100, 300), random.Next(1000, 3000), - Color.FromArgb(random.Next(0, 256))); + Color.FromArgb(random.Next(0, 256),random.Next(0,256), random.Next(0, 256))); break; case nameof(DrawningStormtrooper): _drawningAircraft = new DrawningStormtrooper(random.Next(100, 300), random.Next(1000, 3000), @@ -52,6 +60,8 @@ public partial class FormStormtrooper : Form _drawningAircraft.SetPictureSize(pictureBoxStormtrooper.Width, pictureBoxStormtrooper.Height); _drawningAircraft.SetPosition(random.Next(10, 100), random.Next(10, 100)); + _strategy = null; + Draw(); } @@ -108,5 +118,47 @@ public partial class FormStormtrooper : Form } + private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) + { + } + private void buttonStrategyStep_Click(object sender, EventArgs e) + { + if (_drawningAircraft == null) + { + return; + } + + if (comboBoxStrategy.Enabled) + { + _strategy = comboBoxStrategy.SelectedIndex switch + { + 0 => new MoveToCenter(), + 1 => new MoveToBorder(), + _ => null, + }; + + if (_strategy == null) + { + return; + } + _strategy.SetData(new MoveableAircraft(_drawningAircraft), pictureBoxStormtrooper.Width, pictureBoxStormtrooper.Height); + } + + if (_strategy == null) + { + return; + } + + comboBoxStrategy.Enabled = false; + _strategy.MakeStep(); + Draw(); + + if (_strategy.GetStatus() == StrategyStatus.Finish) + { + comboBoxStrategy.Enabled = true; + _strategy = null; + } + + } } diff --git a/Stormtrooper/Stormtrooper/MovementStrategy/AbstractStrategy.cs b/Stormtrooper/Stormtrooper/MovementStrategy/AbstractStrategy.cs new file mode 100644 index 0000000..991bb44 --- /dev/null +++ b/Stormtrooper/Stormtrooper/MovementStrategy/AbstractStrategy.cs @@ -0,0 +1,121 @@ +namespace Stormtrooper.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/Stormtrooper/Stormtrooper/MovementStrategy/IMoveableObject.cs b/Stormtrooper/Stormtrooper/MovementStrategy/IMoveableObject.cs new file mode 100644 index 0000000..d2283dd --- /dev/null +++ b/Stormtrooper/Stormtrooper/MovementStrategy/IMoveableObject.cs @@ -0,0 +1,22 @@ +namespace Stormtrooper.MovementStrategy; + +/// +/// Интерфейс для работы с перемещаемым объектом +/// +public interface IMoveableObject +{ + /// + /// Получение координаты объекта + /// + ObjectParameters? GetObjectPosition { get; } + /// + /// Шаг объекта + /// + int GetStep { get; } + /// + /// Попытка переместить объект в указанном направлении + /// + /// Направление + /// true - объект перемещен, false - перемещение невозможно + bool TryMoveObject(MovementDirection direction); +} diff --git a/Stormtrooper/Stormtrooper/MovementStrategy/MoveToBorder.cs b/Stormtrooper/Stormtrooper/MovementStrategy/MoveToBorder.cs new file mode 100644 index 0000000..82bf242 --- /dev/null +++ b/Stormtrooper/Stormtrooper/MovementStrategy/MoveToBorder.cs @@ -0,0 +1,52 @@ +namespace Stormtrooper.MovementStrategy; + +/// +/// Стратегия перемещения объекта к краю экрана +/// +internal class MoveToBorder : AbstractStrategy +{ + protected override bool IsTargetDestinaion() + { + ObjectParameters? objParams = GetObjectParameters; + if (objParams == null) + { + return false; + } + return objParams.RightBorder - GetStep() <= FieldWidth + && objParams.RightBorder + GetStep() >= FieldWidth && + objParams.DownBorder - GetStep() <= FieldHeight + && objParams.DownBorder + GetStep() >= FieldHeight; + } + protected override void MoveToTarget() + { + ObjectParameters? objParams = GetObjectParameters; + if (objParams == null) + { + return; + } + int diffX = objParams.RightBorder - FieldWidth; + if (Math.Abs(diffX) > GetStep()) + { + if (diffX > 0) + { + MoveLeft(); + } + else + { + MoveRight(); + } + } + int diffY = objParams.DownBorder - FieldHeight; + if (Math.Abs(diffY) > GetStep()) + { + if (diffY > 0) + { + MoveUp(); + } + else + { + MoveDown(); + } + } + } +} diff --git a/Stormtrooper/Stormtrooper/MovementStrategy/MoveToCenter.cs b/Stormtrooper/Stormtrooper/MovementStrategy/MoveToCenter.cs new file mode 100644 index 0000000..8c6659a --- /dev/null +++ b/Stormtrooper/Stormtrooper/MovementStrategy/MoveToCenter.cs @@ -0,0 +1,52 @@ +namespace Stormtrooper.MovementStrategy; + +/// +/// Стратегия перемещения объекта в центр экрана +/// +public class MoveToCenter : AbstractStrategy +{ + protected override bool IsTargetDestinaion() + { + ObjectParameters? objParams = GetObjectParameters; + if (objParams == null) + { + return false; + } + return objParams.ObjectMiddleHorizontal - GetStep() <= FieldWidth / 2 + && objParams.ObjectMiddleHorizontal + GetStep() >= FieldWidth / 2 && + objParams.ObjectMiddleVertical - GetStep() <= FieldHeight / 2 + && objParams.ObjectMiddleVertical + GetStep() >= FieldHeight / 2; + } + protected override void MoveToTarget() + { + ObjectParameters? objParams = GetObjectParameters; + if (objParams == null) + { + return; + } + int diffX = objParams.ObjectMiddleHorizontal - FieldWidth / 2; + if (Math.Abs(diffX) > GetStep()) + { + if (diffX > 0) + { + MoveLeft(); + } + else + { + MoveRight(); + } + } + int diffY = objParams.ObjectMiddleVertical - FieldHeight / 2; + if (Math.Abs(diffY) > GetStep()) + { + if (diffY > 0) + { + MoveUp(); + } + else + { + MoveDown(); + } + } + } +} diff --git a/Stormtrooper/Stormtrooper/MovementStrategy/MoveableAircraft.cs b/Stormtrooper/Stormtrooper/MovementStrategy/MoveableAircraft.cs new file mode 100644 index 0000000..0ec0904 --- /dev/null +++ b/Stormtrooper/Stormtrooper/MovementStrategy/MoveableAircraft.cs @@ -0,0 +1,62 @@ +using Stormtrooper.Drawnings; +using Stormtrooper.MovementStrategy; + +namespace Aircraft.MovementStrategy; + +/// +/// класс реализация для IMoveableObject с использованием DrawningAircraft +/// +public class MoveableAircraft : IMoveableObject +{ + /// + /// Поле-объект класса DrawningAircraft или его наследника + /// + private readonly DrawningAircraft? _cruiser = null; + /// + /// Конструктор + /// + /// Объект класса DrawningAircraft + public MoveableAircraft(DrawningAircraft cruiser) + { + _cruiser = cruiser; + } + public ObjectParameters? GetObjectPosition + { + get + { + if (_cruiser == null || _cruiser.EntityAircraft == null || + !_cruiser.GetPosX.HasValue || !_cruiser.GetPosY.HasValue) + { + return null; + } + return new ObjectParameters(_cruiser.GetPosX.Value, + _cruiser.GetPosY.Value, _cruiser.GetWidth, _cruiser.GetHeight); + } + } + public int GetStep => (int)(_cruiser?.EntityAircraft?.Step ?? 0); + public bool TryMoveObject(MovementDirection direction) + { + if (_cruiser == null || _cruiser.EntityAircraft == null) + { + return false; + } + return _cruiser.MoveTransport(GetDirectionType(direction)); + } + /// + /// Конвертация из MovementDirection в DirectionType + /// + /// MovementDirection + /// DirectionType + private static DirectionType GetDirectionType(MovementDirection direction) + { + return direction switch + { + MovementDirection.Left => DirectionType.Left, + MovementDirection.Right => DirectionType.Right, + MovementDirection.Up => DirectionType.Up, + MovementDirection.Down => DirectionType.Down, + _ => DirectionType.Unknow, + }; + } +} + diff --git a/Stormtrooper/Stormtrooper/MovementStrategy/MovementDirection.cs b/Stormtrooper/Stormtrooper/MovementStrategy/MovementDirection.cs new file mode 100644 index 0000000..6815322 --- /dev/null +++ b/Stormtrooper/Stormtrooper/MovementStrategy/MovementDirection.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Stormtrooper.MovementStrategy; + +public enum MovementDirection +{ + /// + /// Вверх + /// + Up = 1, + /// + /// Вниз + /// + Down = 2, + /// + /// Влево + /// + Left = 3, + /// + /// Вправо + /// + Right = 4 +} diff --git a/Stormtrooper/Stormtrooper/MovementStrategy/ObjectParameters.cs b/Stormtrooper/Stormtrooper/MovementStrategy/ObjectParameters.cs new file mode 100644 index 0000000..d76ffd7 --- /dev/null +++ b/Stormtrooper/Stormtrooper/MovementStrategy/ObjectParameters.cs @@ -0,0 +1,64 @@ +namespace Stormtrooper.MovementStrategy +{ + /// + /// Параметры-координаты объекта + /// + public class ObjectParameters + { + /// + /// Координата X + /// + private readonly int _x; + /// + /// Координата Y + /// + private readonly int _y; + /// + /// Ширина объекта + /// + private readonly int _width; + /// + /// Высота объекта + /// + private readonly int _height; + /// + /// Левая граница + /// + public int LeftBorder => _x; + /// + /// Верхняя граница + /// + public int TopBorder => _y; + /// + /// Правая граница + /// + public int RightBorder => _x + _width; + /// + /// Нижняя граница + /// + public int DownBorder => _y + _height; + /// + /// Середина объекта + /// + public int ObjectMiddleHorizontal => _x + _width / 2; + /// + /// Середина объекта + /// + public int ObjectMiddleVertical => _y + _height / 2; + /// + /// Конструктор + /// + /// Координата X + /// Координата Y + /// Ширина объекта + /// Высота объекта + public ObjectParameters(int x, int y, int width, int height) + { + _x = x; + _y = y; + _width = width; + _height = height; + } + } + +} diff --git a/Stormtrooper/Stormtrooper/MovementStrategy/StrategyStatus.cs b/Stormtrooper/Stormtrooper/MovementStrategy/StrategyStatus.cs new file mode 100644 index 0000000..85ceccb --- /dev/null +++ b/Stormtrooper/Stormtrooper/MovementStrategy/StrategyStatus.cs @@ -0,0 +1,23 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Stormtrooper.MovementStrategy; + +public enum StrategyStatus +{ + /// + /// Все готово к началу + /// + NotInit, + /// + /// Выполняется + /// + InProgress, + /// + /// Завершено + /// + Finish +} -- 2.25.1