diff --git a/ProjectAirbus/ProjectAirbus/DirectionType.cs b/ProjectAirbus/ProjectAirbus/Drawning/DirectionType.cs
similarity index 74%
rename from ProjectAirbus/ProjectAirbus/DirectionType.cs
rename to ProjectAirbus/ProjectAirbus/Drawning/DirectionType.cs
index 6a5dcb5..0ff723b 100644
--- a/ProjectAirbus/ProjectAirbus/DirectionType.cs
+++ b/ProjectAirbus/ProjectAirbus/Drawning/DirectionType.cs
@@ -1,11 +1,15 @@
-
-namespace ProjectAirbus;
+namespace ProjectAirbus.Drawning;
///
/// Направление перемещения
///
public enum DirectionType
{
+ ///
+ /// Неизвестное направление
+ ///
+ Unknow = -1,
+
///
/// Вверх
///
diff --git a/ProjectAirbus/ProjectAirbus/Drawning/DrawningAirbus.cs b/ProjectAirbus/ProjectAirbus/Drawning/DrawningAirbus.cs
new file mode 100644
index 0000000..530c042
--- /dev/null
+++ b/ProjectAirbus/ProjectAirbus/Drawning/DrawningAirbus.cs
@@ -0,0 +1,69 @@
+using ProjectAirbus.Entities;
+
+namespace ProjectAirbus.Drawning;
+
+///
+/// Класс, отвечающий за прорисовку и перемещение объекта-сущности
+///
+public class DrawningAirbus : DrawningBus
+{
+
+ ///
+ /// Ширина прорисовки аэробуса
+ ///
+ private readonly int _drawningAirbusWidth = 133;
+
+ ///
+ /// Высота прорисовки аэробуса
+ ///
+ private readonly int _drawningAirbusHeight = 60;
+
+ ///
+ /// Конструтор
+ ///
+ /// Скорость
+ /// Вес
+ /// Основной цвет
+ /// Дополнительный цвет
+ /// Признак наличия дополнительного отсека для пассажиров
+ /// Признак наличия дополнительных двигателей
+ public DrawningAirbus(int speed, double weight, Color bodycolor, Color additionalcolor, bool compartment, bool engine) : base(133, 60)
+ {
+ EntityBus = new EntityAirbus(speed, weight, bodycolor, additionalcolor, compartment, engine);
+ }
+
+ public override void DrawTransport(Graphics g)
+ {
+ if (EntityBus == null || EntityBus is not EntityAirbus airbus || !_startPosX.HasValue || !_startPosY.HasValue)
+ {
+ return;
+ }
+
+ Pen pen = new(Color.Black);
+ Brush additionalBrush = new SolidBrush(airbus.AdditionalColor);
+
+ base.DrawTransport(g);
+
+ if (airbus.Engine)
+ {
+ g.FillRectangle(additionalBrush, _startPosX.Value + 30, _startPosY.Value + 33, 13, 5);
+ g.FillRectangle(additionalBrush, _startPosX.Value + 45, _startPosY.Value + 33, 15, 5);
+ }
+
+ Brush brBlue = new SolidBrush(Color.LightBlue);
+ if (airbus.Compartment)
+ {
+ //g.FillEllipse(brBlue, _startPosX.Value + 37, _startPosY.Value + 13, 49, 15);
+
+ g.FillPolygon(brBlue, new Point[]
+ {
+ new Point(_startPosX.Value + 35, _startPosY.Value + 20), new Point(_startPosX.Value + 60, _startPosY.Value + 12),
+ new Point(_startPosX.Value + 80, _startPosY.Value + 12), new Point(_startPosX.Value + 95, _startPosY.Value + 20)});
+
+ g.DrawLine(pen, _startPosX.Value + 35, _startPosY.Value + 20, _startPosX.Value + 60, _startPosY.Value + 12);
+ g.DrawLine(pen, _startPosX.Value + 60, _startPosY.Value + 12, _startPosX.Value + 80, _startPosY.Value + 12);
+ g.DrawLine(pen, _startPosX.Value + 80, _startPosY.Value + 12, _startPosX.Value + 95, _startPosY.Value + 20);
+ }
+ }
+}
+
diff --git a/ProjectAirbus/ProjectAirbus/DrawningAirbus.cs b/ProjectAirbus/ProjectAirbus/Drawning/DrawningBus.cs
similarity index 65%
rename from ProjectAirbus/ProjectAirbus/DrawningAirbus.cs
rename to ProjectAirbus/ProjectAirbus/Drawning/DrawningBus.cs
index 5d5ff2c..8b2d590 100644
--- a/ProjectAirbus/ProjectAirbus/DrawningAirbus.cs
+++ b/ProjectAirbus/ProjectAirbus/Drawning/DrawningBus.cs
@@ -1,15 +1,18 @@
-
-namespace ProjectAirbus;
+using ProjectAirbus.Entities;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
-///
-/// Класс, отвечающий за прорисовку и перемещение объекта-сущности
-///
-public class DrawningAirbus
+namespace ProjectAirbus.Drawning;
+
+public class DrawningBus
{
///
/// Класс-сущность
///
- public EntityAirbus? EntityAirbus { get; private set; }
+ public EntityBus? EntityBus { get; protected set; }
///
/// Ширина окна
@@ -24,42 +27,82 @@ public class DrawningAirbus
///
/// Левая координата прорисовки аэробуса
///
- private int? _startPosX;
+ protected int? _startPosX;
///
/// Верхняя кооридната прорисовки аэробуса
///
- private int? _startPosY;
+ protected int? _startPosY;
///
/// Ширина прорисовки аэробуса
///
- private readonly int _drawningAirbusWidth = 133;
+ private readonly int _drawningBusWidth = 135;
///
/// Высота прорисовки аэробуса
///
- private readonly int _drawningAirbusHeight = 60;
+ private readonly int _drawningBusHeight = 60;
///
- /// Инициализация свойств
+ /// Координата X объекта
+ ///
+ public int? GetPosX => _startPosX;
+
+ ///
+ /// Координата Y объекта
///
- /// Скорость
- /// Вес
- /// Основной цвет
- /// Дополнительный цвет
- /// Признак наличия дополнительного отсека для пассажиров
- /// Признак наличия дополнительных двигателей
- public void Init(int speed, double weight, Color bodycolor, Color additionalcolor, bool compartment, bool engine)
+ public int? GetPosY => _startPosY;
+
+ ///
+ /// Ширина объекта
+ ///
+ public int GetWidth => _drawningBusWidth;
+
+ ///
+ /// Высота объекта
+ ///
+ public int GetHeight => _drawningBusHeight;
+
+ ///
+ /// Пустой конструктор
+ ///
+ private DrawningBus()
{
- EntityAirbus = new EntityAirbus();
- EntityAirbus.Init(speed, weight, bodycolor, additionalcolor, compartment, engine);
_pictureWidth = null;
_pictureHeight = null;
_startPosX = null;
_startPosY = null;
}
+ ///
+ /// Конструктор
+ ///
+ /// Скорость
+ /// Вес
+ /// Основной цвет
+
+ public DrawningBus(int speed, double weight, Color bodycolor) : this()
+ {
+ EntityBus = new EntityBus(speed, weight, bodycolor);
+ _pictureWidth = null;
+ _pictureHeight = null;
+ _startPosX = null;
+ _startPosY = null;
+ }
+
+ ///
+ /// Конструктор для наследников
+ ///
+ /// Ширина прорисовки аэробуса
+ /// Высота прорисовки аэробуса
+
+ protected DrawningBus(int drawningAirbusWidth, int drawningAirbusHeight) : this()
+ {
+ _drawningBusWidth = drawningAirbusWidth;
+ _drawningBusHeight = drawningAirbusHeight;
+ }
+
///
/// Установка границ поля
///
@@ -71,7 +114,7 @@ public class DrawningAirbus
// TODO проверка, что объект "влезает" в размеры поля
// если влезает, сохраняем границы и корректируем позицию объекта,если она была уже установлена
- if (width >= _drawningAirbusWidth || height >= _drawningAirbusHeight)
+ if (width >= _drawningBusWidth || height >= _drawningBusHeight)
{
_pictureWidth = width;
_pictureHeight = height;
@@ -102,17 +145,17 @@ public class DrawningAirbus
{
x = 0;
}
- else if (x > _pictureWidth - _drawningAirbusWidth)
+ else if (x > _pictureWidth - _drawningBusWidth)
{
- x = _pictureWidth.Value - _drawningAirbusWidth;
+ x = _pictureWidth.Value - _drawningBusWidth;
}
if (y < 0)
{
y = 0;
}
- else if (y > _pictureHeight - _drawningAirbusHeight)
+ else if (y > _pictureHeight - _drawningBusHeight)
{
- y = _pictureHeight.Value - _drawningAirbusHeight;
+ y = _pictureHeight.Value - _drawningBusHeight;
}
_startPosX = x;
@@ -125,7 +168,7 @@ public class DrawningAirbus
///
public bool MoveTransport(DirectionType direction)
{
- if (EntityAirbus == null || !_startPosX.HasValue || !_startPosY.HasValue)
+ if (EntityBus == null || !_startPosX.HasValue || !_startPosY.HasValue)
{
return false;
}
@@ -133,33 +176,33 @@ public class DrawningAirbus
{
// Влево
case DirectionType.Left:
- if (_startPosX.Value - EntityAirbus.Step > 0)
+ if (_startPosX.Value - EntityBus.Step > 0)
{
- _startPosX -= (int)EntityAirbus.Step;
+ _startPosX -= (int)EntityBus.Step;
}
return true;
//Вверх
case DirectionType.Up:
- if (_startPosY.Value - EntityAirbus.Step > 0)
+ if (_startPosY.Value - EntityBus.Step > 0)
{
- _startPosY -= (int)EntityAirbus.Step;
+ _startPosY -= (int)EntityBus.Step;
}
return true;
//Вправо
case DirectionType.Right:
{
- if (_startPosX.Value + _drawningAirbusWidth + EntityAirbus.Step < _pictureWidth)
+ if (_startPosX.Value + _drawningBusWidth + EntityBus.Step < _pictureWidth)
{
- _startPosX += (int)EntityAirbus.Step;
+ _startPosX += (int)EntityBus.Step;
}
}
return true;
//Вниз
case DirectionType.Down:
{
- if (_startPosY.Value + _drawningAirbusHeight + EntityAirbus.Step < _pictureHeight)
+ if (_startPosY.Value + _drawningBusHeight + EntityBus.Step < _pictureHeight)
{
- _startPosY += (int)EntityAirbus.Step;
+ _startPosY += (int)EntityBus.Step;
}
}
return true;
@@ -171,23 +214,15 @@ public class DrawningAirbus
/// Прорисовка объекта
///
///
- public void DrawTransport(Graphics g)
+ public virtual void DrawTransport(Graphics g)
{
- if (EntityAirbus == null || !_startPosX.HasValue || !_startPosY.HasValue)
+ if (EntityBus == null || !_startPosX.HasValue || !_startPosY.HasValue)
{
return;
}
Pen pen = new(Color.Black);
- Brush bodyColorBrush = new SolidBrush(EntityAirbus.BodyColor);
- Brush additionalBrush = new SolidBrush(EntityAirbus.AdditionalColor);
-
- //Дополнительный отсек
- Brush brBlue = new SolidBrush(Color.LightBlue);
- if (EntityAirbus.Compartment)
- {
- g.FillEllipse(brBlue, _startPosX.Value + 37, _startPosY.Value + 13, 49, 15);
- }
+ Brush bodyColorBrush = new SolidBrush(EntityBus.BodyColor);
//корпус
g.FillRectangle(bodyColorBrush, _startPosX.Value, _startPosY.Value + 20, 100, 20);
@@ -208,7 +243,7 @@ public class DrawningAirbus
g.DrawLine(pen, _startPosX.Value, _startPosY.Value, _startPosX.Value + 20, _startPosY.Value + 20);
//заднее доп крыло
- g.FillEllipse(darkBrush, _startPosX.Value - 7, _startPosY.Value + 15, 20, 5);
+ g.FillEllipse(darkBrush, _startPosX.Value, _startPosY.Value + 15, 20, 5);
//нос самолёта
g.DrawLine(pen, _startPosX.Value + 100, _startPosY.Value + 20, _startPosX.Value + 130, _startPosY.Value + 30);
@@ -224,12 +259,5 @@ public class DrawningAirbus
g.DrawLine(pen, _startPosX.Value + 90, _startPosY.Value + 40, _startPosX.Value + 90, _startPosY.Value + 45);
g.FillEllipse(darkBrush, _startPosX.Value + 87, _startPosY.Value + 45, 6, 6);
- // Двигатели
- if (EntityAirbus.Engine)
- {
- g.FillRectangle(additionalBrush, _startPosX.Value + 30, _startPosY.Value + 33, 13, 5);
- g.FillRectangle(additionalBrush, _startPosX.Value + 45, _startPosY.Value + 33, 15, 5);
- }
}
}
-
diff --git a/ProjectAirbus/ProjectAirbus/EntityAirbus.cs b/ProjectAirbus/ProjectAirbus/Entities/EntityAirbus.cs
similarity index 71%
rename from ProjectAirbus/ProjectAirbus/EntityAirbus.cs
rename to ProjectAirbus/ProjectAirbus/Entities/EntityAirbus.cs
index a507c30..97d6ad5 100644
--- a/ProjectAirbus/ProjectAirbus/EntityAirbus.cs
+++ b/ProjectAirbus/ProjectAirbus/Entities/EntityAirbus.cs
@@ -1,26 +1,10 @@
-
-namespace ProjectAirbus;
+namespace ProjectAirbus.Entities;
///
/// Класс-сущность "Аэробус"
///
-public class EntityAirbus
+public class EntityAirbus : EntityBus
{
- ///
- /// Скорость
- ///
- public int Speed { get; private set; }
-
- ///
- /// Вес
- ///
- public double Weight { get; private set; }
-
- ///
- /// Основной цывет
- ///
- public Color BodyColor { get; private set; }
-
///
/// Дополнительный цвет
///
@@ -50,11 +34,8 @@ public class EntityAirbus
/// Дополнительный цвет
/// Признак наличия дополнительного отсека для пассажиров
/// Признак наличия дополнительных двигателей
- public void Init(int speed, double weight, Color bodyColor, Color additionalColor, bool compartment, bool engine)
+ public EntityAirbus(int speed, double weight, Color bodyColor, Color additionalColor, bool compartment, bool engine) : base (speed, weight, bodyColor)
{
- Speed = speed;
- Weight = weight;
- BodyColor = bodyColor;
AdditionalColor = additionalColor;
Compartment = compartment;
Engine = engine;
diff --git a/ProjectAirbus/ProjectAirbus/Entities/EntityBus.cs b/ProjectAirbus/ProjectAirbus/Entities/EntityBus.cs
new file mode 100644
index 0000000..4fa528e
--- /dev/null
+++ b/ProjectAirbus/ProjectAirbus/Entities/EntityBus.cs
@@ -0,0 +1,46 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace ProjectAirbus.Entities;
+
+///
+/// Класс-сущность "Аэробус2"
+///
+public class EntityBus
+{
+ ///
+ /// Скорость
+ ///
+ public int Speed { get; private set; }
+
+ ///
+ /// Вес
+ ///
+ public double Weight { get; private set; }
+
+ ///
+ /// Основной цывет
+ ///
+ public Color BodyColor { get; private set; }
+
+ ///
+ /// Шаг перемещения аэробуса
+ ///
+ public double Step => Speed * 100 / Weight;
+
+ ///
+ /// Конструктор сущности
+ ///
+ /// Скорость
+ /// Вес
+ /// Основной цветm>
+ public EntityBus(int speed, double weight, Color bodyColor)
+ {
+ Speed = speed;
+ Weight = weight;
+ BodyColor = bodyColor;
+ }
+}
diff --git a/ProjectAirbus/ProjectAirbus/FormAirbus.Designer.cs b/ProjectAirbus/ProjectAirbus/FormAirbus.Designer.cs
index ab25a76..d59f17c 100644
--- a/ProjectAirbus/ProjectAirbus/FormAirbus.Designer.cs
+++ b/ProjectAirbus/ProjectAirbus/FormAirbus.Designer.cs
@@ -29,11 +29,14 @@
private void InitializeComponent()
{
buttonCreateAirbus = new Button();
- ButtonLeft = new Button();
+ buttonLeft = new Button();
buttonDown = new Button();
buttonRight = new Button();
buttonUp = new Button();
pictureBoxAirbus = new PictureBox();
+ buttonCreateBus = new Button();
+ comboBoxStrategy = new ComboBox();
+ buttonStrategyStep = new Button();
((System.ComponentModel.ISupportInitialize)pictureBoxAirbus).BeginInit();
SuspendLayout();
//
@@ -42,23 +45,23 @@
buttonCreateAirbus.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
buttonCreateAirbus.Location = new Point(12, 498);
buttonCreateAirbus.Name = "buttonCreateAirbus";
- buttonCreateAirbus.Size = new Size(75, 23);
+ buttonCreateAirbus.Size = new Size(193, 23);
buttonCreateAirbus.TabIndex = 1;
- buttonCreateAirbus.Text = "Создать";
+ buttonCreateAirbus.Text = "Создать аэробус";
buttonCreateAirbus.UseVisualStyleBackColor = true;
buttonCreateAirbus.Click += ButtonCreateAirbus_Click;
//
- // ButtonLeft
+ // buttonLeft
//
- ButtonLeft.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
- ButtonLeft.BackgroundImage = Properties.Resources.free_icon_left_arrow_10696518;
- ButtonLeft.BackgroundImageLayout = ImageLayout.Stretch;
- ButtonLeft.Location = new Point(927, 491);
- ButtonLeft.Name = "buttonLeft";
- ButtonLeft.Size = new Size(35, 35);
- ButtonLeft.TabIndex = 2;
- ButtonLeft.UseVisualStyleBackColor = true;
- ButtonLeft.Click += ButtonMove_Click;
+ buttonLeft.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
+ buttonLeft.BackgroundImage = Properties.Resources.free_icon_left_arrow_10696518;
+ buttonLeft.BackgroundImageLayout = ImageLayout.Stretch;
+ buttonLeft.Location = new Point(927, 491);
+ buttonLeft.Name = "buttonLeft";
+ buttonLeft.Size = new Size(35, 35);
+ buttonLeft.TabIndex = 2;
+ buttonLeft.UseVisualStyleBackColor = true;
+ buttonLeft.Click += ButtonMove_Click;
//
// buttonDown
//
@@ -105,15 +108,49 @@
pictureBoxAirbus.TabIndex = 6;
pictureBoxAirbus.TabStop = false;
//
+ // buttonCreateBus
+ //
+ buttonCreateBus.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
+ buttonCreateBus.Location = new Point(229, 498);
+ buttonCreateBus.Name = "buttonCreateBus";
+ buttonCreateBus.Size = new Size(193, 23);
+ buttonCreateBus.TabIndex = 7;
+ buttonCreateBus.Text = "Создать базовый аэробус";
+ buttonCreateBus.UseVisualStyleBackColor = true;
+ buttonCreateBus.Click += ButtonCreateBus_Click;
+ //
+ // comboBoxStrategy
+ //
+ comboBoxStrategy.DropDownStyle = ComboBoxStyle.DropDownList;
+ comboBoxStrategy.FormattingEnabled = true;
+ comboBoxStrategy.Items.AddRange(new object[] { "К центру", "К краю" });
+ comboBoxStrategy.Location = new Point(927, 12);
+ comboBoxStrategy.Name = "comboBoxStrategy";
+ comboBoxStrategy.Size = new Size(117, 23);
+ comboBoxStrategy.TabIndex = 8;
+ //
+ // buttonStrategyStep
+ //
+ buttonStrategyStep.Location = new Point(969, 41);
+ buttonStrategyStep.Name = "buttonStrategyStep";
+ buttonStrategyStep.Size = new Size(75, 23);
+ buttonStrategyStep.TabIndex = 9;
+ buttonStrategyStep.Text = "Шаг";
+ buttonStrategyStep.UseVisualStyleBackColor = true;
+ buttonStrategyStep.Click += buttonStrategyStep_Click;
+ //
// FormAirbus
//
AutoScaleMode = AutoScaleMode.None;
ClientSize = new Size(1059, 533);
+ Controls.Add(buttonStrategyStep);
+ Controls.Add(comboBoxStrategy);
+ Controls.Add(buttonCreateBus);
Controls.Add(buttonCreateAirbus);
Controls.Add(buttonRight);
Controls.Add(buttonDown);
Controls.Add(buttonUp);
- Controls.Add(ButtonLeft);
+ Controls.Add(buttonLeft);
Controls.Add(pictureBoxAirbus);
Name = "FormAirbus";
Text = "Аэробус";
@@ -123,10 +160,13 @@
#endregion
private Button buttonCreateAirbus;
- private Button ButtonLeft;
+ private Button buttonLeft;
private Button buttonDown;
private Button buttonRight;
private Button buttonUp;
private PictureBox pictureBoxAirbus;
+ private Button buttonCreateBus;
+ private ComboBox comboBoxStrategy;
+ private Button buttonStrategyStep;
}
}
\ No newline at end of file
diff --git a/ProjectAirbus/ProjectAirbus/FormAirbus.cs b/ProjectAirbus/ProjectAirbus/FormAirbus.cs
index b047d21..8f6fc1b 100644
--- a/ProjectAirbus/ProjectAirbus/FormAirbus.cs
+++ b/ProjectAirbus/ProjectAirbus/FormAirbus.cs
@@ -1,4 +1,8 @@
+using ProjectAirbus.Drawning;
+using ProjectAirbus.MovementStrategy;
+using ProjectAirbus.MovementStrategyж;
+
namespace ProjectAirbus
{
///
@@ -9,14 +13,19 @@ namespace ProjectAirbus
///
/// Поле-объект для прорисовки объекта
///
- private DrawningAirbus? _drawningAirbus;
+ private DrawningBus? _drawningBus;
+ ///
+ /// Стратегия перемещения
+ ///
+ private AbstractStrategy? _strategy;
///
/// Конструктор формы
///
public FormAirbus()
{
InitializeComponent();
+ _strategy = null;
}
///
@@ -24,7 +33,7 @@ namespace ProjectAirbus
///
private void Draw()
{
- if (_drawningAirbus == null)
+ if (_drawningBus == null)
{
return;
}
@@ -32,28 +41,52 @@ namespace ProjectAirbus
Bitmap bmp = new(pictureBoxAirbus.Width,
pictureBoxAirbus.Height);
Graphics gr = Graphics.FromImage(bmp);
- _drawningAirbus.DrawTransport(gr);
+ _drawningBus.DrawTransport(gr);
pictureBoxAirbus.Image = bmp;
}
///
- /// Обработка нажатия кнопки "Создать"
+ /// Создание объекта класса-перемещения
+ ///
+ /// Тип создаваемого объекта
+ private void CreateObject(string type)
+ {
+ Random random = new();
+ switch (type)
+ {
+ case nameof(DrawningBus):
+ _drawningBus = new DrawningBus(random.Next(100, 300), random.Next(1000, 3000),
+ Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)));
+ break;
+ case nameof(DrawningAirbus):
+ _drawningBus = new DrawningAirbus(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)));
+ break;
+ default:
+ return;
+ }
+ _drawningBus.SetPictureSize(pictureBoxAirbus.Width, pictureBoxAirbus.Height);
+ _drawningBus.SetPosition(random.Next(10, 100), random.Next(10, 100));
+ _strategy = null;
+ comboBoxStrategy.Enabled = true;
+ Draw();
+ }
+
+ ///
+ /// Обработка нажатия кнопки "Создать Аэробус"
///
///
///
- private void ButtonCreateAirbus_Click(object sender, EventArgs e)
- {
- Random random = new();
- _drawningAirbus = new DrawningAirbus();
- _drawningAirbus.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)));
- _drawningAirbus.SetPictureSize(pictureBoxAirbus.Width, pictureBoxAirbus.Height);
- _drawningAirbus.SetPosition(random.Next(10, 100), random.Next(10, 100));
+ private void ButtonCreateAirbus_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningAirbus));
- Draw();
- }
+ ///
+ /// Обработка нажатия кнопки "Создать автобус"
+ ///
+ ///
+ ///
+ private void ButtonCreateBus_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningBus));
///
/// Перемещение объекта по форме (нажатие кнопок навигации)
@@ -62,7 +95,7 @@ namespace ProjectAirbus
///
private void ButtonMove_Click(object sender, EventArgs e)
{
- if (_drawningAirbus == null)
+ if (_drawningBus == null)
{
return;
}
@@ -72,19 +105,19 @@ namespace ProjectAirbus
{
case "buttonUp":
result =
- _drawningAirbus.MoveTransport(DirectionType.Up);
+ _drawningBus.MoveTransport(DirectionType.Up);
break;
case "buttonDown":
result =
- _drawningAirbus.MoveTransport(DirectionType.Down);
+ _drawningBus.MoveTransport(DirectionType.Down);
break;
case "buttonLeft":
result =
- _drawningAirbus.MoveTransport(DirectionType.Left);
+ _drawningBus.MoveTransport(DirectionType.Left);
break;
case "buttonRight":
result =
- _drawningAirbus.MoveTransport(DirectionType.Right);
+ _drawningBus.MoveTransport(DirectionType.Right);
break;
}
if (result)
@@ -92,5 +125,47 @@ namespace ProjectAirbus
Draw();
}
}
+
+ ///
+ ///
+ ///
+ ///
+ ///
+ private void buttonStrategyStep_Click(object sender, EventArgs e)
+ {
+ if (_drawningBus == null)
+ {
+ return;
+ }
+
+ if (comboBoxStrategy.Enabled)
+ {
+ _strategy = comboBoxStrategy.SelectedIndex switch
+ {
+ 0 => new MoveToCenter(),
+ 1 => new MoveToBorder(),
+ _ => null,
+ };
+ if (_strategy == null)
+ {
+ return;
+ }
+ _strategy.SetData(new MoveableBus(_drawningBus), pictureBoxAirbus.Width, pictureBoxAirbus.Height);
+ }
+
+ if (_strategy == null)
+ {
+ return;
+ }
+ comboBoxStrategy.Enabled = false;
+ _strategy.MakeStep();
+ Draw();
+
+ if (_strategy.GetStatus() == StrategyStatus.Finish)
+ {
+ comboBoxStrategy.Enabled = true;
+ _strategy = null;
+ }
+ }
}
}
diff --git a/ProjectAirbus/ProjectAirbus/MovementStrategy/AbstractStrategy.cs b/ProjectAirbus/ProjectAirbus/MovementStrategy/AbstractStrategy.cs
new file mode 100644
index 0000000..a2f20ed
--- /dev/null
+++ b/ProjectAirbus/ProjectAirbus/MovementStrategy/AbstractStrategy.cs
@@ -0,0 +1,143 @@
+using ProjectAirbus.MovementStrategyж;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace ProjectAirbus.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/ProjectAirbus/ProjectAirbus/MovementStrategy/IMoveableObject.cs b/ProjectAirbus/ProjectAirbus/MovementStrategy/IMoveableObject.cs
new file mode 100644
index 0000000..d768dc2
--- /dev/null
+++ b/ProjectAirbus/ProjectAirbus/MovementStrategy/IMoveableObject.cs
@@ -0,0 +1,27 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace ProjectAirbus.MovementStrategy;
+
+public interface IMoveableObject
+{
+ ///
+ /// Получение координаты объекта
+ ///
+ ObjectParameters? GetObjectPosition { get; }
+
+ ///
+ /// Шаг объекта
+ ///
+ int GetStep { get; }
+
+ ///
+ /// Попытка переместить объект в указанном направлении
+ ///
+ /// Направление
+ /// true - объект перемещен, false - перемещение невозможно
+ bool TryMoveObject(MovementDirection direction);
+}
diff --git a/ProjectAirbus/ProjectAirbus/MovementStrategy/MoveToBorder.cs b/ProjectAirbus/ProjectAirbus/MovementStrategy/MoveToBorder.cs
new file mode 100644
index 0000000..4494328
--- /dev/null
+++ b/ProjectAirbus/ProjectAirbus/MovementStrategy/MoveToBorder.cs
@@ -0,0 +1,34 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace ProjectAirbus.MovementStrategy;
+
+public class MoveToBorder : AbstractStrategy
+{
+ protected override bool IsTargetDestinaion()
+ {
+ ObjectParameters? objParams = GetObjectParameters;
+ if (objParams == null)
+ {
+ return false;
+ }
+ return objParams.LeftBorder - GetStep() <= 0 || objParams.RightBorder + GetStep() >= FieldWidth ||
+ objParams.TopBorder - GetStep() <= 0 || objParams.ObjectMiddleVertical + GetStep() >= FieldHeight;
+ }
+
+ protected override void MoveToTarget()
+ {
+ ObjectParameters? objParams = GetObjectParameters;
+ if (objParams == null)
+ {
+ return;
+ }
+ int x = objParams.RightBorder;
+ if (x + GetStep() < FieldWidth) MoveRight();
+ int y = objParams.DownBorder;
+ if (y + GetStep() < FieldHeight) MoveDown();
+ }
+}
diff --git a/ProjectAirbus/ProjectAirbus/MovementStrategy/MoveToCenter.cs b/ProjectAirbus/ProjectAirbus/MovementStrategy/MoveToCenter.cs
new file mode 100644
index 0000000..9bb2746
--- /dev/null
+++ b/ProjectAirbus/ProjectAirbus/MovementStrategy/MoveToCenter.cs
@@ -0,0 +1,56 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace ProjectAirbus.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/ProjectAirbus/ProjectAirbus/MovementStrategy/MoveableBus.cs b/ProjectAirbus/ProjectAirbus/MovementStrategy/MoveableBus.cs
new file mode 100644
index 0000000..4dc45fa
--- /dev/null
+++ b/ProjectAirbus/ProjectAirbus/MovementStrategy/MoveableBus.cs
@@ -0,0 +1,69 @@
+using ProjectAirbus.Drawning;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace ProjectAirbus.MovementStrategy;
+
+///
+/// Класс-реализация IMoveableObject с использованием DrawningBus
+///
+public class MoveableBus : IMoveableObject
+{
+ ///
+ /// Поле-объект класса DrawningAirplan или его наследника
+ ///
+ private readonly DrawningBus? _bus = null;
+
+ ///
+ /// Конструктор
+ ///
+ /// Объект класса DrawningAirplan
+ public MoveableBus(DrawningBus bus)
+ {
+ _bus = bus;
+ }
+
+ public ObjectParameters? GetObjectPosition
+ {
+ get
+ {
+ if (_bus == null || _bus.EntityBus == null || !_bus.GetPosX.HasValue || !_bus.GetPosY.HasValue)
+ {
+ return null;
+ }
+ return new ObjectParameters(_bus.GetPosX.Value, _bus.GetPosY.Value, _bus.GetWidth, _bus.GetHeight);
+ }
+ }
+
+ public int GetStep => (int)(_bus?.EntityBus?.Step ?? 0);
+
+ public bool TryMoveObject(MovementDirection direction)
+ {
+ if (_bus == null || _bus.EntityBus == null)
+ {
+ return false;
+ }
+
+ return _bus.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/ProjectAirbus/ProjectAirbus/MovementStrategy/MovementDirection.cs b/ProjectAirbus/ProjectAirbus/MovementStrategy/MovementDirection.cs
new file mode 100644
index 0000000..92ebd28
--- /dev/null
+++ b/ProjectAirbus/ProjectAirbus/MovementStrategy/MovementDirection.cs
@@ -0,0 +1,30 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace ProjectAirbus.MovementStrategy;
+
+///
+/// Направление перемещения
+///
+public enum MovementDirection
+{
+ ///
+ /// Вверх
+ ///
+ Up = 1,
+ ///
+ /// Вниз
+ ///
+ Down = 2,
+ ///
+ /// Влево
+ ///
+ Left = 3,
+ ///
+ /// Вправо
+ ///
+ Right = 4
+}
diff --git a/ProjectAirbus/ProjectAirbus/MovementStrategy/ObjectParameters.cs b/ProjectAirbus/ProjectAirbus/MovementStrategy/ObjectParameters.cs
new file mode 100644
index 0000000..6e9434f
--- /dev/null
+++ b/ProjectAirbus/ProjectAirbus/MovementStrategy/ObjectParameters.cs
@@ -0,0 +1,77 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace ProjectAirbus.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;
+ }
+}
+
\ No newline at end of file
diff --git a/ProjectAirbus/ProjectAirbus/MovementStrategy/StrategyStatus.cs b/ProjectAirbus/ProjectAirbus/MovementStrategy/StrategyStatus.cs
new file mode 100644
index 0000000..e653d37
--- /dev/null
+++ b/ProjectAirbus/ProjectAirbus/MovementStrategy/StrategyStatus.cs
@@ -0,0 +1,26 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace ProjectAirbus.MovementStrategyж
+{
+ public enum StrategyStatus
+ {
+ ///
+ /// Все готово к началу
+ ///
+ NotInit,
+
+ ///
+ /// Выполняется
+ ///
+ InProgress,
+
+ ///
+ /// Завершено
+ ///
+ Finish
+ }
+}