diff --git a/ProjectAirPlane/ProjectAirPlane/Drawnings/DirectionType.cs b/ProjectAirPlane/ProjectAirPlane/Drawnings/DirectionType.cs new file mode 100644 index 0000000..ebe8ff4 --- /dev/null +++ b/ProjectAirPlane/ProjectAirPlane/Drawnings/DirectionType.cs @@ -0,0 +1,32 @@ +namespace ProjectAirPlane.Drawnings; + +/// +/// Направление перемещения +/// +public enum DirectionType +{ + /// + /// Неизвестное направление + /// + Unknow = -1, + + /// + /// Вверх + /// + Up = 1, + + /// + /// Вниз + /// + Down = 2, + + /// + /// Влево + /// + Left = 3, + + /// + /// Вправо + /// + Right = 4 +} \ No newline at end of file diff --git a/ProjectAirPlane/ProjectAirPlane/Drawnings/DrawningAirPlane.cs b/ProjectAirPlane/ProjectAirPlane/Drawnings/DrawningAirPlane.cs new file mode 100644 index 0000000..5b24d56 --- /dev/null +++ b/ProjectAirPlane/ProjectAirPlane/Drawnings/DrawningAirPlane.cs @@ -0,0 +1,83 @@ +using ProjectAirPlane.Entites; + +namespace ProjectAirPlane.Drawnings; + +/// +/// Класс, отвечающий за прорисовку и перемещение объекта-сущности +/// +public class DrawningAirPlane : DrawningPlane +{ + + /// + /// Конструктор + /// + /// Скорость + /// Вес + /// Основной цвет + /// Дополнительный цвет + /// Признак наличия обвеса + /// Признак наличия антикрыла + /// Признак наличия гоночной полосы + public DrawningAirPlane(int speed, double weight, Color bodyColor, Color additionalColor, bool radar, bool dopBak, bool chassi) : base(195, 70) + { + EntityPlane = new EntityAirPlane(speed, weight, bodyColor, additionalColor, radar, dopBak, chassi); + + + } + + + public override void DrawTransport(Graphics g) + { + if (EntityPlane == null || EntityPlane is not EntityAirPlane airPlane || !_startPosX.HasValue || !_startPosY.HasValue) + { + return; + } + Pen pen = new(Color.Black); + Brush additionalBrush = new SolidBrush(EntityPlane.BodyColor); + + if (airPlane.Chassi) + { + //шасси + g.DrawLine(pen, _startPosX.Value + 50, _startPosY.Value + 55, _startPosX.Value + 50, _startPosY.Value + 70); + g.DrawLine(pen, _startPosX.Value + 150, _startPosY.Value + 55, _startPosX.Value + 150, _startPosY.Value + 70); + g.FillEllipse(additionalBrush, _startPosX.Value + 40, _startPosY.Value + 65, 10, 10); + g.FillEllipse(additionalBrush, _startPosX.Value + 50, _startPosY.Value + 65, 10, 10); + g.FillEllipse(additionalBrush, _startPosX.Value + 145, _startPosY.Value + 65, 10, 10); + + } + + + base.DrawTransport(g); + + + + if (airPlane.DopBak) + { + //бак + g.FillEllipse(additionalBrush, _startPosX.Value, _startPosY.Value + 45, 40, 20); + g.DrawEllipse(pen, _startPosX.Value, _startPosY.Value + 45, 40, 20); + + } + + if (airPlane.Radar) + { + //радар + g.DrawLine(pen, _startPosX.Value + 60, _startPosY.Value + 25, _startPosX.Value + 60, _startPosY.Value + 15); + g.DrawLine(pen, _startPosX.Value + 60, _startPosY.Value + 15, _startPosX.Value + 67, _startPosY.Value + 11); + Point point7 = new Point(_startPosX.Value + 60, _startPosY.Value + 15); + Point point8 = new Point(_startPosX.Value + 60, _startPosY.Value + 5); + Point point9 = new Point(_startPosX.Value + 70, _startPosY.Value + 25); + Point[] curvePoints3 = + { + point7, + point8, + point9, + }; + g.FillPolygon(additionalBrush, curvePoints3); + + } + + } +} + + diff --git a/ProjectAirPlane/ProjectAirPlane/DrawningAirPlane.cs b/ProjectAirPlane/ProjectAirPlane/Drawnings/DrawningPlane.cs similarity index 57% rename from ProjectAirPlane/ProjectAirPlane/DrawningAirPlane.cs rename to ProjectAirPlane/ProjectAirPlane/Drawnings/DrawningPlane.cs index 39362e4..ffcadd5 100644 --- a/ProjectAirPlane/ProjectAirPlane/DrawningAirPlane.cs +++ b/ProjectAirPlane/ProjectAirPlane/Drawnings/DrawningPlane.cs @@ -1,65 +1,104 @@ -namespace ProjectAirPlane; +using ProjectAirPlane.Entites; -/// -/// Класс, отвечающий за прорисовку и перемещение объекта-сущности -/// -public class DrawningAirPlane +namespace ProjectAirPlane.Drawnings; + +public class DrawningPlane { /// /// Класс-сущность /// - public EntityAirPlane? EntityAirPlane { get; private set; } + public EntityPlane? EntityPlane { get; protected set; } /// - /// Ширина + /// Ширина окна /// private int? _pictureWidth; /// - /// Высота + /// Высота окна /// private int? _pictureHeight; /// - /// Левая координата прорисовки самолёта + /// Левая координата прорисовки автомобиля /// - private int? _startPosX; + protected int? _startPosX; /// - /// Верхняя кооридната прорисовки самолёта + /// Верхняя кооридната прорисовки автомобиля /// - private int? _startPosY; + protected int? _startPosY; /// /// Ширина прорисовки самолёта /// - private readonly int _drawningAirPlaneWidth = 197; + private readonly int _drawningAirPlaneWidth = 195; /// /// Высота прорисовки самолёта /// - private readonly int _drawningAirPlaneHeight = 65; + private readonly int _drawningAirPlaneHeight = 70; /// - /// Инициализация свойств + /// Координата X объекта + /// + public int? GetPosX => _startPosX; + + /// + /// Координата Y объекта /// - /// Скорость - /// Вес - /// Основной цвет - /// Дополнительный цвет - /// Признак наличия обвеса - /// Признак наличия доп бака - /// Признак наличия шасси - public void Init(int speed, double weight, Color bodyColor, Color additionalColor, bool radar, bool dopBak, bool chassi) + public int? GetPosY => _startPosY; + + /// + /// Ширина объекта + /// + public int GetWidth => _drawningAirPlaneWidth; + + /// + /// Высота объекта + /// + public int GetHeight => _drawningAirPlaneHeight; + + /// + /// Пустой конструктор + /// + private DrawningPlane() { - EntityAirPlane = new EntityAirPlane(); - EntityAirPlane.Init(speed, weight, bodyColor, additionalColor, radar, dopBak, chassi); _pictureWidth = null; _pictureHeight = null; _startPosX = null; _startPosY = null; } + /// + /// Конструктор + /// + /// Скорость + /// Вес + /// Основной цвет + + public DrawningPlane(int speed, double weight, Color bodyColor) : this() + { + + EntityPlane = new EntityPlane(speed, weight, bodyColor); + + } + + /// + /// Конструктор для наследования + /// + /// Ширина прорисовки самолёта + /// Высота прорисовки самолёта + + + protected DrawningPlane(int drawningPlaneWidth, int drawningPlaneHeight) : this() + { + _drawningAirPlaneWidth = drawningPlaneWidth; + _drawningAirPlaneHeight = drawningPlaneHeight; + + } + + /// /// Установка границ поля /// @@ -69,6 +108,7 @@ public class DrawningAirPlane public bool SetPictureSize(int width, int height) { // TODO проверка, что объект "влезает" в размеры поля + if (_drawningAirPlaneWidth > width || _drawningAirPlaneHeight > height) { return false; @@ -90,7 +130,6 @@ public class DrawningAirPlane else if (_startPosY < 0) _startPosY = 0; } return true; - } /// @@ -107,20 +146,15 @@ public class DrawningAirPlane // TODO если при установке объекта в эти координаты, он будет "выходить" за границы формы - if (x + _drawningAirPlaneWidth > _pictureWidth) + if (x + _drawningAirPlaneWidth >= _pictureWidth || y + _drawningAirPlaneHeight >= _pictureHeight) { - _startPosX = _pictureWidth - _drawningAirPlaneWidth; + x = y = 10; + _startPosX = x; + _startPosY = y; } - else if (x < 0) _startPosX = 0; - else _startPosX = x; - - if (x + _drawningAirPlaneHeight > _pictureHeight) - { - _startPosY = _pictureHeight - _drawningAirPlaneHeight; - } - else if (y < 0) _startPosX = 0; + // то надо изменить координаты, чтобы он оставался в этих границах + _startPosX = x; _startPosY = y; - } /// @@ -130,7 +164,7 @@ public class DrawningAirPlane /// true - перемещене выполнено, false - перемещение невозможно public bool MoveTransport(DirectionType direction) { - if (EntityAirPlane == null || !_startPosX.HasValue || !_startPosY.HasValue) + if (EntityPlane == null || !_startPosX.HasValue || !_startPosY.HasValue) { return false; } @@ -139,32 +173,32 @@ public class DrawningAirPlane { //влево case DirectionType.Left: - if (_startPosX.Value - EntityAirPlane.Step > 0) + if (_startPosX.Value - EntityPlane.Step > 0) { - _startPosX -= (int)EntityAirPlane.Step; + _startPosX -= (int)EntityPlane.Step; } return true; //вверх case DirectionType.Up: - if (_startPosY.Value - EntityAirPlane.Step > 0) + if (_startPosY.Value - EntityPlane.Step > 0) { - _startPosY -= (int)EntityAirPlane.Step; + _startPosY -= (int)EntityPlane.Step; } return true; // вправо case DirectionType.Right: //TODO прописать логику сдвига вправо - if (_startPosX + EntityAirPlane.Step + _drawningAirPlaneWidth < _pictureWidth) + if (_startPosX + EntityPlane.Step + _drawningAirPlaneWidth < _pictureWidth) { - _startPosX += (int)EntityAirPlane.Step; + _startPosX += (int)EntityPlane.Step; } return true; //вниз case DirectionType.Down: //TODO прописать логику сдвига ввниз - if (_startPosY + EntityAirPlane.Step + _drawningAirPlaneHeight < _pictureHeight) + if (_startPosY + EntityPlane.Step + _drawningAirPlaneHeight < _pictureHeight) { - _startPosY += (int)EntityAirPlane.Step; + _startPosY += (int)EntityPlane.Step; } return true; default: @@ -176,18 +210,25 @@ public class DrawningAirPlane /// Прорисовка объекта /// /// - public void DrawTransport(Graphics g) + public virtual void DrawTransport(Graphics g) { - if (EntityAirPlane == null || !_startPosX.HasValue || !_startPosY.HasValue) + if (EntityPlane == null || !_startPosX.HasValue || !_startPosY.HasValue) { return; } Pen pen = new(Color.Black); - Brush additionalBrush = new SolidBrush(EntityAirPlane.AdditionalColor); + Brush additionalBrush = new SolidBrush(EntityPlane.BodyColor); + + //шасси + g.DrawLine(pen, _startPosX.Value + 50, _startPosY.Value + 55, _startPosX.Value + 50, _startPosY.Value + 70); + g.DrawLine(pen, _startPosX.Value + 150, _startPosY.Value + 55, _startPosX.Value + 150, _startPosY.Value + 70); + g.FillEllipse(additionalBrush, _startPosX.Value + 40, _startPosY.Value + 65, 10, 10); + g.FillEllipse(additionalBrush, _startPosX.Value + 50, _startPosY.Value + 65, 10, 10); + g.FillEllipse(additionalBrush, _startPosX.Value + 145, _startPosY.Value + 65, 10, 10); // корпус - Brush br = new SolidBrush(EntityAirPlane.BodyColor); + Brush br = new SolidBrush(EntityPlane.BodyColor); g.DrawRectangle(pen, _startPosX.Value, _startPosY.Value + 25, 170, 30); g.FillRectangle(br, _startPosX.Value, _startPosY.Value + 25, 170, 30); // бак @@ -209,45 +250,13 @@ public class DrawningAirPlane g.DrawPolygon(blackPen, curvePoints); g.DrawLine(blackPen, _startPosX.Value + 170, _startPosY.Value + 40, _startPosX.Value + 200, _startPosY.Value + 40); // хвост - Brush bw = new SolidBrush(EntityAirPlane.BodyColor); + Brush bw = new SolidBrush(EntityPlane.BodyColor); g.FillPolygon(bw, new Point[] { new Point((int) _startPosX, (int) _startPosY + 40 ), new Point((int) _startPosX + 80, (int) _startPosY + 30), new Point((int) _startPosX, (int) _startPosY), new Point((int) _startPosX , (int) _startPosY + 40), - - - } ); - // шасси - g.DrawLine(blackPen, _startPosX.Value + 50, _startPosY.Value + 55, _startPosX.Value + 50, _startPosY.Value + 70); - g.DrawLine(blackPen, _startPosX.Value + 150, _startPosY.Value + 55, _startPosX.Value + 150, _startPosY.Value + 70); - g.FillEllipse(blackBrush, _startPosX.Value + 40, _startPosY.Value + 65, 10, 10); - g.FillEllipse(blackBrush, _startPosX.Value + 50, _startPosY.Value + 65, 10, 10); - g.FillEllipse(blackBrush, _startPosX.Value + 145, _startPosY.Value + 65, 10, 10); - if (EntityAirPlane.DopBak) - { - //бак - g.FillEllipse(additionalBrush, _startPosX.Value, _startPosY.Value + 45, 40, 20); - g.DrawEllipse(blackPen, _startPosX.Value, _startPosY.Value + 45, 40, 20); - } - if (EntityAirPlane.Radar) - { - //радар - g.DrawLine(blackPen, _startPosX.Value + 60, _startPosY.Value + 25, _startPosX.Value + 60, _startPosY.Value + 15); - g.DrawLine(blackPen, _startPosX.Value + 60, _startPosY.Value + 15, _startPosX.Value + 67, _startPosY.Value + 11); - Point point7 = new Point(_startPosX.Value + 60, _startPosY.Value + 15); - Point point8 = new Point(_startPosX.Value + 60, _startPosY.Value + 5); - Point point9 = new Point(_startPosX.Value + 70, _startPosY.Value + 25); - Point[] curvePoints3 = - { - point7, - point8, - point9, - }; - g.FillPolygon(additionalBrush, curvePoints3); - - - } } -} \ No newline at end of file +} + diff --git a/ProjectAirPlane/ProjectAirPlane/Entites/EntityAirPlane.cs b/ProjectAirPlane/ProjectAirPlane/Entites/EntityAirPlane.cs new file mode 100644 index 0000000..c560690 --- /dev/null +++ b/ProjectAirPlane/ProjectAirPlane/Entites/EntityAirPlane.cs @@ -0,0 +1,52 @@ +namespace ProjectAirPlane.Entites; + +/// +/// Класс-сущность "Спортивный самолёт" +/// +public class EntityAirPlane : EntityPlane +{ + /// + /// Дополнительный цвет (для опциональных элементов) + /// + public Color AdditionalColor { get; private set; } + + /// + /// Признак (опция) наличия обвеса + /// + public bool Radar { get; private set; } + + /// + /// Признак (опция) наличия антикрыла + /// + public bool DopBak { get; private set; } + + /// + /// Признак (опция) наличия гоночной полосы + /// + public bool Chassi { get; private set; } + + /// + /// Шаг перемещения автомобиля + /// + + + /// + /// Инициализация полей объекта-класса спортивного автомобиля + /// + /// Скорость + /// Вес автомобиля + /// Основной цвет + /// Дополнительный цвет + /// Признак наличия обвеса + /// Признак наличия антикрыла + /// Признак наличия гоночной полосы + public EntityAirPlane(int speed, double weight, Color bodyColor, Color additionalColor, bool radar, bool dopBak, bool chassi) : base(speed, weight, bodyColor) + { + + AdditionalColor = additionalColor; + Radar = radar; + DopBak = dopBak; + Chassi = chassi; + + } +} \ No newline at end of file diff --git a/ProjectAirPlane/ProjectAirPlane/Entites/EntityPlane.cs b/ProjectAirPlane/ProjectAirPlane/Entites/EntityPlane.cs new file mode 100644 index 0000000..5b0f796 --- /dev/null +++ b/ProjectAirPlane/ProjectAirPlane/Entites/EntityPlane.cs @@ -0,0 +1,43 @@ +namespace ProjectAirPlane.Entites; + +/// +/// Класс-сущность "Обычный самолёт" +/// +public class EntityPlane +{ + /// + /// Скорость + /// + 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 EntityPlane(int speed, double weight, Color bodyColor) + { + Speed = speed; + Weight = weight; + BodyColor = bodyColor; + + + } +} diff --git a/ProjectAirPlane/ProjectAirPlane/EntityAirPlane.cs b/ProjectAirPlane/ProjectAirPlane/EntityAirPlane.cs deleted file mode 100644 index 5927f6c..0000000 --- a/ProjectAirPlane/ProjectAirPlane/EntityAirPlane.cs +++ /dev/null @@ -1,69 +0,0 @@ -namespace ProjectAirPlane; - -/// -/// Класс-сущность "Самолёт" -/// -public class EntityAirPlane -{ - /// - /// Скорость - /// - 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 Radar { get; private set; } - - /// - /// Признак (опция) наличия доп бака - /// - public bool DopBak { get; private set; } - - /// - /// Признак (опция) наличия шасси - /// - public bool Chassi { get; private set; } - - /// - /// Шаг перемещения автомобиля - /// - public double Step => Speed * 100 / Weight; - - /// - /// Инициализация полей объекта-класса спортивного автомобиля - /// - /// Скорость - /// Вес самолёта - /// Основной цвет - /// Дополнительный цвет - /// Признак наличия радара - /// Признак наличия доп бака - /// Признак наличия шасси - public void Init(int speed, double weight, Color bodyColor, Color additionalColor, bool radar, bool dopBak, bool chassi) - { - Speed = speed; - Weight = weight; - BodyColor = bodyColor; - AdditionalColor = additionalColor; - Radar = radar; - DopBak = dopBak; - Chassi = chassi; - - } -} \ No newline at end of file diff --git a/ProjectAirPlane/ProjectAirPlane/FormAirPlane.Designer.cs b/ProjectAirPlane/ProjectAirPlane/FormAirPlane.Designer.cs index c841c7e..aecfeea 100644 --- a/ProjectAirPlane/ProjectAirPlane/FormAirPlane.Designer.cs +++ b/ProjectAirPlane/ProjectAirPlane/FormAirPlane.Designer.cs @@ -35,6 +35,9 @@ buttonUp = new Button(); buttonDown = new Button(); buttonRight = new Button(); + ButtonCreatePlane = new Button(); + comboBoxStrategy = new ComboBox(); + buttonStrategyStepS = new Button(); ((System.ComponentModel.ISupportInitialize)pictureBoxAirPlane).BeginInit(); SuspendLayout(); // @@ -52,9 +55,9 @@ buttonCreateAirPlane.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; buttonCreateAirPlane.Location = new Point(12, 488); buttonCreateAirPlane.Name = "buttonCreateAirPlane"; - buttonCreateAirPlane.Size = new Size(75, 23); + buttonCreateAirPlane.Size = new Size(180, 23); buttonCreateAirPlane.TabIndex = 1; - buttonCreateAirPlane.Text = "Создать"; + buttonCreateAirPlane.Text = "Создать самолёт с радаром"; buttonCreateAirPlane.UseVisualStyleBackColor = true; buttonCreateAirPlane.Click += ButtonCreateAirPlane_Click; // @@ -106,11 +109,45 @@ buttonRight.UseVisualStyleBackColor = true; buttonRight.Click += ButtonMove_Click; // + // ButtonCreatePlane + // + ButtonCreatePlane.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; + ButtonCreatePlane.Location = new Point(198, 488); + ButtonCreatePlane.Name = "ButtonCreatePlane"; + ButtonCreatePlane.Size = new Size(180, 23); + ButtonCreatePlane.TabIndex = 6; + ButtonCreatePlane.Text = "Создать самолёт"; + ButtonCreatePlane.UseVisualStyleBackColor = true; + ButtonCreatePlane.Click += ButtonCreatePlane_Click; + // + // comboBoxStrategy + // + comboBoxStrategy.DropDownStyle = ComboBoxStyle.DropDownList; + comboBoxStrategy.FormattingEnabled = true; + comboBoxStrategy.Items.AddRange(new object[] { "К центру", "К краю" }); + comboBoxStrategy.Location = new Point(628, 12); + comboBoxStrategy.Name = "comboBoxStrategy"; + comboBoxStrategy.Size = new Size(121, 23); + comboBoxStrategy.TabIndex = 7; + // + // buttonStrategyStepS + // + buttonStrategyStepS.Location = new Point(654, 41); + buttonStrategyStepS.Name = "buttonStrategyStepS"; + buttonStrategyStepS.Size = new Size(75, 23); + buttonStrategyStepS.TabIndex = 8; + buttonStrategyStepS.Text = "Шаг"; + buttonStrategyStepS.UseVisualStyleBackColor = true; + buttonStrategyStepS.Click += ButtonStrategyStep_Click; + // // FormAirPlane // AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(749, 523); + Controls.Add(buttonStrategyStepS); + Controls.Add(comboBoxStrategy); + Controls.Add(ButtonCreatePlane); Controls.Add(buttonRight); Controls.Add(buttonDown); Controls.Add(buttonUp); @@ -131,5 +168,8 @@ private Button buttonUp; private Button buttonDown; private Button buttonRight; + private Button ButtonCreatePlane; + private ComboBox comboBoxStrategy; + private Button buttonStrategyStepS; } } \ No newline at end of file diff --git a/ProjectAirPlane/ProjectAirPlane/FormAirPlane.cs b/ProjectAirPlane/ProjectAirPlane/FormAirPlane.cs index 531826e..355bcff 100644 --- a/ProjectAirPlane/ProjectAirPlane/FormAirPlane.cs +++ b/ProjectAirPlane/ProjectAirPlane/FormAirPlane.cs @@ -1,14 +1,23 @@ +using ProjectAirPlane.Drawnings; +using ProjectAirPlane.MovementStrategy; + + namespace ProjectAirPlane; /// -/// "" +/// " " /// public partial class FormAirPlane : Form { /// /// - /// - private DrawningAirPlane? _drawningAirPlane; + private DrawningPlane? _drawningPlane; + + /// + /// + /// + private AbstractStrategy? _strategy; /// /// @@ -16,41 +25,70 @@ public partial class FormAirPlane : Form public FormAirPlane() { InitializeComponent(); + _strategy = null; } /// - /// + /// /// private void Draw() { - if (_drawningAirPlane == null) + if (_drawningPlane == null) { return; } Bitmap bmp = new(pictureBoxAirPlane.Width, pictureBoxAirPlane.Height); Graphics gr = Graphics.FromImage(bmp); - _drawningAirPlane.DrawTransport(gr); + _drawningPlane.DrawTransport(gr); pictureBoxAirPlane.Image = bmp; } /// - /// "" + /// - + /// + /// + private void CreateObject(string type) + { + Random random = new(); + switch (type) + { + case nameof(DrawningPlane): + _drawningPlane = new DrawningPlane(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(DrawningAirPlane): + _drawningPlane = new DrawningAirPlane(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; + } + + _drawningPlane.SetPictureSize(pictureBoxAirPlane.Width, pictureBoxAirPlane.Height); + _drawningPlane.SetPosition(random.Next(10, 100), random.Next(10, 100)); + _strategy = null; + comboBoxStrategy.Enabled = true; + Draw(); + } + + /// + /// " " /// /// /// - private void ButtonCreateAirPlane_Click(object sender, EventArgs e) - { - Random random = new(); - _drawningAirPlane = new DrawningAirPlane(); - _drawningAirPlane.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))); - _drawningAirPlane.SetPictureSize(pictureBoxAirPlane.Width, pictureBoxAirPlane.Height); - _drawningAirPlane.SetPosition(random.Next(10, 100), random.Next(10, 100)); - Draw(); - } + private void ButtonCreateAirPlane_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningAirPlane)); + + /// + /// " " + /// + /// + /// + private void ButtonCreatePlane_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningPlane)); + + /// /// ( ) @@ -59,7 +97,7 @@ public partial class FormAirPlane : Form /// private void ButtonMove_Click(object sender, EventArgs e) { - if (_drawningAirPlane == null) + if (_drawningPlane == null) { return; } @@ -69,16 +107,16 @@ public partial class FormAirPlane : Form switch (name) { case "buttonUp": - result = _drawningAirPlane.MoveTransport(DirectionType.Up); + result = _drawningPlane.MoveTransport(DirectionType.Up); break; case "buttonDown": - result = _drawningAirPlane.MoveTransport(DirectionType.Down); + result = _drawningPlane.MoveTransport(DirectionType.Down); break; case "buttonLeft": - result = _drawningAirPlane.MoveTransport(DirectionType.Left); + result = _drawningPlane.MoveTransport(DirectionType.Left); break; case "buttonRight": - result = _drawningAirPlane.MoveTransport(DirectionType.Right); + result = _drawningPlane.MoveTransport(DirectionType.Right); break; } @@ -87,4 +125,46 @@ public partial class FormAirPlane : Form Draw(); } } + + /// + /// "" + /// + /// + /// + private void ButtonStrategyStep_Click(object sender, EventArgs e) + { + if (_drawningPlane == null) return; + + if (comboBoxStrategy.Enabled) + { + _strategy = comboBoxStrategy.SelectedIndex switch + { + 0 => new MoveToCenter(), + 1 => new MoveToBorder(), + _ => null, + }; + if (_strategy == null) + { + return; + } + _strategy.SetData(new MoveablePlane(_drawningPlane), pictureBoxAirPlane.Width, pictureBoxAirPlane.Height); + } + + if (_strategy == null) + { + return; + } + + comboBoxStrategy.Enabled = false; + _strategy.MakeStep(); + Draw(); + + if (_strategy.GetStatus() == StrategyStatus.Finish) + { + comboBoxStrategy.Enabled = true; + _strategy = null; + } + } + + } \ No newline at end of file diff --git a/ProjectAirPlane/ProjectAirPlane/FormAirPlane.resx b/ProjectAirPlane/ProjectAirPlane/FormAirPlane.resx index 8014d25..f8be0c1 100644 --- a/ProjectAirPlane/ProjectAirPlane/FormAirPlane.resx +++ b/ProjectAirPlane/ProjectAirPlane/FormAirPlane.resx @@ -121,7 +121,7 @@ iVBORw0KGgoAAAANSUhEUgAAAgAAAAIACAYAAAD0eNT6AAAABGdBTUEAALGPC/xhBQAAAAlwSFlzAAAP - RQAAD0UBxeoB7wAAABl0RVh0U29mdHdhcmUAd3d3Lmlua3NjYXBlLm9yZ5vuPBoAABSgSURBVHhe7d1N + RAAAD0QBF63jCwAAABl0RVh0U29mdHdhcmUAd3d3Lmlua3NjYXBlLm9yZ5vuPBoAABSgSURBVHhe7d1N yK95XQbwMxM1hINpJAjqSJb7IDB6A4NWtSlaWG1aFbSKZhMSFFQUmLqMWggVJRkSSdSmpIJIJSJJEsFX TI20RkNBGsPp+s95mf955jrnPC////2779/9ueCzON9n5jz3OXMP13Vm5jxz67nnngMAdqYeAYC51SMA MLd6BADmVo8AwNzqEQCYWz0CAHOrRwBgbvUIAMytHgGAudUjADC3egQA5laPAMDc6hEAmFs9AgBzq0cA @@ -216,7 +216,7 @@ iVBORw0KGgoAAAANSUhEUgAAAgAAAAIACAYAAAD0eNT6AAAABGdBTUEAALGPC/xhBQAAAAlwSFlzAAAP - RQAAD0UBxeoB7wAAABl0RVh0U29mdHdhcmUAd3d3Lmlua3NjYXBlLm9yZ5vuPBoAABrcSURBVHhe7d1N + RAAAD0QBF63jCwAAABl0RVh0U29mdHdhcmUAd3d3Lmlua3NjYXBlLm9yZ5vuPBoAABrcSURBVHhe7d1N 6LbbdRbwnBQN0hDTUqGQ5hQ/OhcEpX5AhI50ojjwY+JIwZGYiRRBQUWhth2KDgoqKipFLKITG1QQWxGx WCyF2laqrdTaGEmh2Erj2p4ds3PO9Z73//GsZz/3ff8W/CbXICfw3HutKyfn4yNf+tKXgJOr+Zryu8pf KZ8rP1K+UH6h/Gj5Z+W7y2fK16T/DOBcYgicQ80nyl8oP1dG8BQ/X0ZR+Lr0nwmcQwyBY6t5p/zJ8j/K @@ -337,7 +337,7 @@ iVBORw0KGgoAAAANSUhEUgAAAgAAAAIACAYAAAD0eNT6AAAABGdBTUEAALGPC/xhBQAAAAlwSFlzAAAP - RQAAD0UBxeoB7wAAABl0RVh0U29mdHdhcmUAd3d3Lmlua3NjYXBlLm9yZ5vuPBoAABrPSURBVHhe7d1f + RAAAD0QBF63jCwAAABl0RVh0U29mdHdhcmUAd3d3Lmlua3NjYXBlLm9yZ5vuPBoAABrPSURBVHhe7d1f yLZrWhbwWSM1hMM0SoEwzpL+7QdBYSlM4FbtFG1Y7bRV0FY0OyFBQUWCqZthG4JFRYVEErWTQwWRSkSS JIKpYWmYNU0oSGM4nVdzTetaax3fn/d9n+e8nvu+fyf8dg5hRnju6zyPWbPmWx/50pe+BA+t5p3yjeU7 yufKj5cvlPF/hN2+WH62/Ej5++VPlK9N3zI8khjCI6j56vLny8+XdeHCo/vV8k/K70rfNjyCGMJuNX+y @@ -458,7 +458,7 @@ iVBORw0KGgoAAAANSUhEUgAAAgAAAAIACAYAAAD0eNT6AAAABGdBTUEAALGPC/xhBQAAAAlwSFlzAAAP - RQAAD0UBxeoB7wAAABl0RVh0U29mdHdhcmUAd3d3Lmlua3NjYXBlLm9yZ5vuPBoAABUVSURBVHhe7d1d + RAAAD0QBF63jCwAAABl0RVh0U29mdHdhcmUAd3d3Lmlua3NjYXBlLm9yZ5vuPBoAABUVSURBVHhe7d1d yK15WQbwNRM1hMM0SoKgjvTheRAYfYFBR3VSdGB10lFBR9GchAQFFQXjx2HUgVBRUiGRRJ3UUEGkEZEU ieAnpkZa44SCpOF03+6le+3/vvfe78d67vU8/+d3wW+Yud72u9Z69rjva8rZHV566SUAYGfKEgCYW1kC AHMrSwBgbmUJAMytLAGAuZUlADC3sgQA5laWAMDcyhIAmFtZAgBzK0sAYG5lCQDMrSwBgLmVJQAwt7IE diff --git a/ProjectAirPlane/ProjectAirPlane/MovementStrategy/AbstractStrategy.cs b/ProjectAirPlane/ProjectAirPlane/MovementStrategy/AbstractStrategy.cs new file mode 100644 index 0000000..a81c6fe --- /dev/null +++ b/ProjectAirPlane/ProjectAirPlane/MovementStrategy/AbstractStrategy.cs @@ -0,0 +1,139 @@ +namespace ProjectAirPlane.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; + } +} \ No newline at end of file diff --git a/ProjectAirPlane/ProjectAirPlane/MovementStrategy/IMoveableObject.cs b/ProjectAirPlane/ProjectAirPlane/MovementStrategy/IMoveableObject.cs new file mode 100644 index 0000000..51bec7c --- /dev/null +++ b/ProjectAirPlane/ProjectAirPlane/MovementStrategy/IMoveableObject.cs @@ -0,0 +1,26 @@ +namespace ProjectAirPlane.MovementStrategy; + +/// +/// Интерфейс для работы с перемещаемым объектом +/// +public interface IMoveableObject +{ + /// + /// Получение координаты объекта + /// + ObjectParameters? GetObjectPosition { get; } + + /// + /// Шаг объекта + /// + int GetStep { get; } + + /// + /// Попытка переместить объект в указанном направлении + /// + /// Направление + /// true - объект перемещен, false - перемещение невозможно + bool TryMoveObject(MovementDirection direction); + + protected void Move(); +} \ No newline at end of file diff --git a/ProjectAirPlane/ProjectAirPlane/MovementStrategy/MoveToBorder.cs b/ProjectAirPlane/ProjectAirPlane/MovementStrategy/MoveToBorder.cs new file mode 100644 index 0000000..039e6ed --- /dev/null +++ b/ProjectAirPlane/ProjectAirPlane/MovementStrategy/MoveToBorder.cs @@ -0,0 +1,31 @@ +namespace ProjectAirPlane.MovementStrategy; + +public class MoveToBorder : AbstractStrategy +{ + protected override bool IsTargetDestinaion() + { + ObjectParameters? objParams = GetObjectParameters; + if (objParams == null) return false; + + return objParams.RightBorder + GetStep() >= FieldWidth + && objParams.DownBorder + GetStep() >= FieldHeight; + } + + protected override void MoveToTarget() + { + ObjectParameters? objParams = GetObjectParameters; + if (objParams == null) return; + + int diffX = objParams.LeftBorder - FieldWidth; + if (Math.Abs(diffX) > GetStep()) + { + MoveRight(); + } + + int diffY = objParams.DownBorder - FieldHeight; + if (Math.Abs(diffY) > GetStep()) + { + MoveDown(); + } + } +} diff --git a/ProjectAirPlane/ProjectAirPlane/MovementStrategy/MoveToCenter.cs b/ProjectAirPlane/ProjectAirPlane/MovementStrategy/MoveToCenter.cs new file mode 100644 index 0000000..f2dfc0d --- /dev/null +++ b/ProjectAirPlane/ProjectAirPlane/MovementStrategy/MoveToCenter.cs @@ -0,0 +1,52 @@ + +namespace ProjectAirPlane.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/ProjectAirPlane/ProjectAirPlane/MovementStrategy/MoveablePlane.cs b/ProjectAirPlane/ProjectAirPlane/MovementStrategy/MoveablePlane.cs new file mode 100644 index 0000000..343743b --- /dev/null +++ b/ProjectAirPlane/ProjectAirPlane/MovementStrategy/MoveablePlane.cs @@ -0,0 +1,66 @@ +using ProjectAirPlane.Drawnings; + +namespace ProjectAirPlane.MovementStrategy; + +public class MoveablePlane : IMoveableObject +{ + /// + /// Поле-объект класса DrawningPlane или его наследника + /// + private readonly DrawningPlane? _plane = null; + + /// + /// Конструктор + /// + /// Объект класса DrawningPlane + public MoveablePlane(DrawningPlane plane) + { + _plane = plane; + } + + public ObjectParameters? GetObjectPosition + { + get + { + if (_plane == null || _plane.EntityPlane == null || !_plane.GetPosX.HasValue || !_plane.GetPosY.HasValue) + { + return null; + } + return new ObjectParameters(_plane.GetPosX.Value, _plane.GetPosY.Value, _plane.GetWidth, _plane.GetHeight); + } + } + + public int GetStep => (int)(_plane?.EntityPlane?.Step ?? 0); + + public bool TryMoveObject(MovementDirection direction) + { + if (_plane == null || _plane.EntityPlane == null) + { + return false; + } + + return _plane.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, + }; + } + + void IMoveableObject.Move() + { + throw new NotImplementedException(); + } +} diff --git a/ProjectAirPlane/ProjectAirPlane/DirectionType.cs b/ProjectAirPlane/ProjectAirPlane/MovementStrategy/MovementDirection.cs similarity index 82% rename from ProjectAirPlane/ProjectAirPlane/DirectionType.cs rename to ProjectAirPlane/ProjectAirPlane/MovementStrategy/MovementDirection.cs index 6bd8f4c..df16368 100644 --- a/ProjectAirPlane/ProjectAirPlane/DirectionType.cs +++ b/ProjectAirPlane/ProjectAirPlane/MovementStrategy/MovementDirection.cs @@ -1,10 +1,11 @@ -namespace ProjectAirPlane; +namespace ProjectAirPlane.MovementStrategy; /// /// Направление перемещения /// -public enum DirectionType +public enum MovementDirection { + /// /// Вверх /// diff --git a/ProjectAirPlane/ProjectAirPlane/MovementStrategy/ObjectParameters.cs b/ProjectAirPlane/ProjectAirPlane/MovementStrategy/ObjectParameters.cs new file mode 100644 index 0000000..2f4872b --- /dev/null +++ b/ProjectAirPlane/ProjectAirPlane/MovementStrategy/ObjectParameters.cs @@ -0,0 +1,71 @@ +namespace ProjectAirPlane.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/ProjectAirPlane/ProjectAirPlane/MovementStrategy/StrategyStatus.cs b/ProjectAirPlane/ProjectAirPlane/MovementStrategy/StrategyStatus.cs new file mode 100644 index 0000000..ae076a8 --- /dev/null +++ b/ProjectAirPlane/ProjectAirPlane/MovementStrategy/StrategyStatus.cs @@ -0,0 +1,22 @@ +namespace ProjectAirPlane.MovementStrategy; + +/// +/// Статус выполнения операции перемещения +/// +public enum StrategyStatus +{ + /// + /// Все готово к началу + /// + NotInit, + + /// + /// Выполняется + /// + InProgress, + + /// + /// Завершено + /// + Finish +} diff --git a/ProjectAirPlane/ProjectAirPlane/Resources/arrowDown.jpg b/ProjectAirPlane/ProjectAirPlane/Resources/arrowDown.jpg new file mode 100644 index 0000000..f21002e Binary files /dev/null and b/ProjectAirPlane/ProjectAirPlane/Resources/arrowDown.jpg differ diff --git a/ProjectAirPlane/ProjectAirPlane/Resources/arrowLeft.jpg b/ProjectAirPlane/ProjectAirPlane/Resources/arrowLeft.jpg new file mode 100644 index 0000000..61b8dae Binary files /dev/null and b/ProjectAirPlane/ProjectAirPlane/Resources/arrowLeft.jpg differ diff --git a/ProjectAirPlane/ProjectAirPlane/Resources/arrowRight.jpg b/ProjectAirPlane/ProjectAirPlane/Resources/arrowRight.jpg new file mode 100644 index 0000000..b440197 Binary files /dev/null and b/ProjectAirPlane/ProjectAirPlane/Resources/arrowRight.jpg differ diff --git a/ProjectAirPlane/ProjectAirPlane/Resources/arrowUp.jpg b/ProjectAirPlane/ProjectAirPlane/Resources/arrowUp.jpg new file mode 100644 index 0000000..e630cea Binary files /dev/null and b/ProjectAirPlane/ProjectAirPlane/Resources/arrowUp.jpg differ