This commit is contained in:
Слава 2024-06-03 02:24:36 +04:00
parent 7f0d75e12e
commit 02951365a9
17 changed files with 835 additions and 195 deletions

View File

@ -1,23 +0,0 @@
namespace ProjectPlane;
/// <summary>
/// Направление перемещения
/// </summary>
public enum DirectionType
{
/// <summary>
/// Вверх
/// </summary>
Up = 1,
/// <summary>
/// Вниз
/// </summary>
Down = 2,
/// <summary>
/// Влево
/// </summary>
Left = 3,
/// <summary>
/// Вправо
/// </summary>
Right = 4
}

View File

@ -0,0 +1,27 @@
namespace ProjectPlane.Drawnings;
/// <summary>
/// Направление перемещения
/// </summary>
public enum DirectionType
{
/// <summary>
/// Неизвестное направление
/// </summary>
Unknow = -1,
/// <summary>
/// Вверх
/// </summary>
Up = 1,
/// <summary>
/// Вниз
/// </summary>
Down = 2,
/// <summary>
/// Влево
/// </summary>
Left = 3,
/// <summary>
/// Вправо
/// </summary>
Right = 4
}

View File

@ -1,8 +1,7 @@
using ProjectPlane;
using ProjectPlane.Entities;
using System.Drawing.Drawing2D;
using System.Security.Cryptography.Xml;
namespace ProjectPlane;
namespace ProjectPlane.Drawnings;
/// <summary>
/// Класс, отвечающий за прорисовку и перемещение объекта-сущности
/// </summary>
@ -11,7 +10,7 @@ public class DrawningPlane
/// <summary>
/// Класс-сущность
/// </summary>
public EntityPlane? EntityPlane { get; private set; }
public EntityPlane? EntityPlane { get; protected set; }
/// <summary>
/// Ширина окна
/// </summary>
@ -23,11 +22,11 @@ public class DrawningPlane
/// <summary>
/// Левая координата прорисовки автомобиля
/// </summary>
private int? _startPosX;
protected int? _startPosX;
/// <summary>
/// Верхняя кооридната прорисовки автомобиля
/// </summary>
private int? _startPosY;
protected int? _startPosY;
/// <summary>
/// Ширина прорисовки самолета
@ -38,28 +37,56 @@ public class DrawningPlane
/// </summary>
private readonly int _drawningPlaneHeight = 50;
private readonly int _drawningEnginesWidth = 3;
/// <summary>
/// Инициализация свойств
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес</param>
/// <param name="bodyColor">Основной цвет</param>
/// <param name="additionalColor">Дополнительный цвет</param>
/// <param name="line">Признак наличия вертолетной площадки</param>
/// <param name="floats">Признак наличия шлюпок</param>
/// <param name="boat">Признак наличия пушки</param>
public void Init(int speed, double weight, Color bodyColor, Color
additionalColor, bool line, bool floats, bool boat)
/// <summary>
/// Координата X объекта
/// </summary>
public int? GetPosX => _startPosX;
/// <summary>
/// Координата Y объекта
/// </summary>
public int? GetPosY => _startPosY;
/// <summary>
/// Ширина объекта
/// </summary>
public int GetWidth => _drawningPlaneWidth;
/// <summary>
/// Высота объекта
/// </summary>
public int GetHeight => _drawningPlaneHeight;
/// <summary>
/// Пустой онструктор
/// </summary>
private DrawningPlane()
{
EntityPlane = new EntityPlane();
EntityPlane.Init(speed, weight, bodyColor, additionalColor,
line, floats, boat);
_pictureWidth = null;
_pictureHeight = null;
_startPosX = null;
_startPosY = null;
}
/// <summary>
/// Конструктор
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес</param>
/// <param name="bodyColor">Основной цвет</param>
public DrawningPlane(int speed, double weight, Color bodyColor) : this()
{
EntityPlane = new EntityPlane(speed, weight, bodyColor);
}
/// <summary>
/// Конструктор для наследников
/// </summary>
/// <param name="drawningCarWidth">Ширина прорисовки автомобиля</param>
/// <param name="drawningCarHeight">Высота прорисовки автомобиля</param>
protected DrawningPlane(int drawningCarWidth, int drawningCarHeight) : this()
{
_drawningPlaneWidth = drawningCarWidth;
_pictureHeight = drawningCarHeight;
}
/// <summary>
/// Установка границ поля
/// </summary>
@ -161,7 +188,7 @@ public class DrawningPlane
/// Прорисовка объекта
/// </summary>
/// <param name="g"></param>
public void DrawTransport(Graphics g)
public virtual void DrawTransport(Graphics g)
{
if (EntityPlane == null || !_startPosX.HasValue ||
!_startPosY.HasValue)
@ -177,9 +204,9 @@ public class DrawningPlane
Brush Brush = new SolidBrush(EntityPlane.BodyColor);
Brush Brush2 = new SolidBrush(Color.Black);
Brush glassBrush = new SolidBrush(Color.SkyBlue);
Brush glassBrush2 = new SolidBrush(EntityPlane.AdditionalColor);
Brush boatBrush = new HatchBrush(HatchStyle.ZigZag, EntityPlane.AdditionalColor, Color.FromArgb(163, 163, 163));
Brush additionalBrush = new SolidBrush(EntityPlane.AdditionalColor);
//Brush glassBrush2 = new SolidBrush(EntityPlane.AdditionalColor);
//Brush boatBrush = new HatchBrush(HatchStyle.ZigZag, EntityPlane.AdditionalColor, Color.FromArgb(163, 163, 163));
//Brush additionalBrush = new SolidBrush(EntityPlane.AdditionalColor);
//границы самолета
@ -188,15 +215,15 @@ public class DrawningPlane
g.DrawPolygon(pen, points);
//стёкла
Point[] glass1 = { new Point(_startPosX.Value + 35, _startPosY.Value + 15), new Point(_startPosX.Value + 50, _startPosY.Value), new Point(_startPosX.Value + 42, _startPosY.Value + 15)};
Point[] glass1 = { new Point(_startPosX.Value + 35, _startPosY.Value + 15), new Point(_startPosX.Value + 50, _startPosY.Value), new Point(_startPosX.Value + 42, _startPosY.Value + 15) };
g.FillPolygon(glassBrush, glass1);
g.DrawPolygon(pen, glass1);
Point[] glass2 = { new Point(_startPosX.Value + 47, _startPosY.Value + 15), new Point(_startPosX.Value + 55, _startPosY.Value), new Point(_startPosX.Value + 55, _startPosY.Value + 15)};
Point[] glass2 = { new Point(_startPosX.Value + 47, _startPosY.Value + 15), new Point(_startPosX.Value + 55, _startPosY.Value), new Point(_startPosX.Value + 55, _startPosY.Value + 15) };
g.FillPolygon(glassBrush, glass2);
g.DrawPolygon(pen, glass2);
Point[] glass3 = { new Point(_startPosX.Value + 60, _startPosY.Value + 15), new Point(_startPosX.Value + 65, _startPosY.Value + 7), new Point(_startPosX.Value + 70, _startPosY.Value + 7), new Point(_startPosX.Value + 75, _startPosY.Value + 15)};
Point[] glass3 = { new Point(_startPosX.Value + 60, _startPosY.Value + 15), new Point(_startPosX.Value + 65, _startPosY.Value + 7), new Point(_startPosX.Value + 70, _startPosY.Value + 7), new Point(_startPosX.Value + 75, _startPosY.Value + 15) };
g.FillPolygon(glassBrush, glass3);
g.DrawPolygon(pen, glass3);
@ -208,7 +235,7 @@ public class DrawningPlane
//пропелер
Point[] points2 = { new Point(_startPosX.Value + 10, _startPosY.Value + 20), new Point(_startPosX.Value + 10, _startPosY.Value + 25), new Point(_startPosX.Value + 3, _startPosY.Value + 22)};
Point[] points2 = { new Point(_startPosX.Value + 10, _startPosY.Value + 20), new Point(_startPosX.Value + 10, _startPosY.Value + 25), new Point(_startPosX.Value + 3, _startPosY.Value + 22) };
g.DrawPolygon(pen, points2);
g.FillEllipse(Brush2, _startPosX.Value + 1, _startPosY.Value + 10, 5, 13);
g.FillEllipse(Brush2, _startPosX.Value + 1, _startPosY.Value + 21, 5, 13);
@ -224,41 +251,5 @@ public class DrawningPlane
g.DrawLine(pen, _startPosX.Value + 90, _startPosY.Value + 40, _startPosX.Value + 90, _startPosY.Value + 50);
g.FillEllipse(Brush2, _startPosX.Value + 7, _startPosY.Value + 43, 8, 8);
g.FillEllipse(Brush2, _startPosX.Value + 85, _startPosY.Value + 43, 8, 8);
//внутренности самолета
//g.DrawRectangle(pen, _startPosX.Value + 25, _startPosY.Value + 10, 80, 30);
//g.FillRectangle(additionalBrush, _startPosX.Value + 25, _startPosY.Value + 10, 80, 30);
if (EntityPlane.Line)
{
Point[] points3 = { new Point(_startPosX.Value + 35, _startPosY.Value + 15), new Point(_startPosX.Value + 20, _startPosY.Value + 15), new Point(_startPosX.Value + 10, _startPosY.Value + 20), new Point(_startPosX.Value + 10, _startPosY.Value + 25), new Point(_startPosX.Value + 15, _startPosY.Value + 30), new Point(_startPosX.Value + 145, _startPosY.Value + 20), new Point(_startPosX.Value + 140, _startPosY.Value + 20), new Point(_startPosX.Value + 140, _startPosY.Value + 10), new Point(_startPosX.Value + 135, _startPosY.Value + 20), new Point(_startPosX.Value + 30, _startPosY.Value + 20)};
g.FillPolygon(additionalBrush, points3);
g.DrawPolygon(pen6, points3);
}
if (EntityPlane.Floats)
{
Point[] points4 = { new Point(_startPosX.Value + 10, _startPosY.Value + 40), new Point(_startPosX.Value + 110, _startPosY.Value + 40), new Point(_startPosX.Value + 110, _startPosY.Value + 41), new Point(_startPosX.Value + 70, _startPosY.Value + 50), new Point(_startPosX.Value + 30, _startPosY.Value + 50)};
g.FillPolygon(additionalBrush, points4);
g.DrawPolygon(pen, points4);
g.DrawLine(pen, _startPosX.Value + 30, _startPosY.Value + 45, _startPosX.Value + 80, _startPosY.Value + 45);
}
if (EntityPlane.Boat)
{
g.DrawRectangle(pen, _startPosX.Value + 85, _startPosY.Value + 15, 25, 10);
g.FillRectangle(boatBrush, _startPosX.Value + 85, _startPosY.Value + 15, 25, 10);
Point[] points5 = { new Point(_startPosX.Value + 80, _startPosY.Value + 15), new Point(_startPosX.Value + 85, _startPosY.Value + 10), new Point(_startPosX.Value + 115, _startPosY.Value + 10), new Point(_startPosX.Value + 115, _startPosY.Value + 15), new Point(_startPosX.Value + 85, _startPosY.Value + 15), new Point(_startPosX.Value + 85, _startPosY.Value + 25), new Point(_startPosX.Value + 115, _startPosY.Value + 25), new Point(_startPosX.Value + 115, _startPosY.Value + 30), new Point(_startPosX.Value + 85, _startPosY.Value + 30), new Point(_startPosX.Value + 80, _startPosY.Value + 25)};
g.FillPolygon(additionalBrush, points5);
g.DrawPolygon(pen, points5);
}
}
}
}

View File

@ -0,0 +1,87 @@
using System.Drawing.Drawing2D;
using ProjectPlane.Entities;
namespace ProjectPlane.Drawnings
{
public class DrawningSeaPlane : DrawningPlane
{
/// <summary>
/// Конструктор
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес</param>
/// <param name="bodyColor">Основной цвет</param>
/// <param name="additionalColor">Дополнительный цвет</param>
/// <param name="line">Признак наличия вертолетной площадки</param>
/// <param name="boat">Признак наличия шлюпок</param>
/// <param name="floats">Признак наличия пушки</param>
public DrawningSeaPlane(int speed, double weight, Color bodyColor, Color additionalColor, bool line, bool boat, bool floats)
: base(150, 50)
{
EntityPlane = new EntitySeaPlane(speed, weight, bodyColor, additionalColor, line, boat, floats);
}
public override void DrawTransport(Graphics g)
{
if (EntityPlane == null || EntityPlane is not EntitySeaPlane entitySeaPlane || !_startPosX.HasValue ||
!_startPosY.HasValue)
{
return;
}
Pen pen3 = new(EntityPlane.BodyColor, 2);
Pen pen = new(Color.Black, 2);
Pen pen5 = new(Color.Black, 4);
Pen pen2 = new(Color.Black, 6);
Pen pen4 = new(Color.White, 4);
Pen pen6 = new(Color.Black, 1);
Brush Brush = new SolidBrush(EntityPlane.BodyColor);
Brush Brush2 = new SolidBrush(Color.Black);
Brush glassBrush = new SolidBrush(Color.SkyBlue);
Brush glassBrush2 = new SolidBrush(entitySeaPlane.AdditionalColor);
Brush boatBrush = new HatchBrush(HatchStyle.ZigZag, entitySeaPlane.AdditionalColor, Color.FromArgb(163, 163, 163));
Brush additionalBrush = new SolidBrush(entitySeaPlane.AdditionalColor);
base.DrawTransport(g);
//внутренности самолета
//g.DrawRectangle(pen, _startPosX.Value + 25, _startPosY.Value + 10, 80, 30);
//g.FillRectangle(additionalBrush, _startPosX.Value + 25, _startPosY.Value + 10, 80, 30);
if (entitySeaPlane.Line)
{
Point[] points3 = { new Point(_startPosX.Value + 35, _startPosY.Value + 15), new Point(_startPosX.Value + 20, _startPosY.Value + 15), new Point(_startPosX.Value + 10, _startPosY.Value + 20), new Point(_startPosX.Value + 10, _startPosY.Value + 25), new Point(_startPosX.Value + 15, _startPosY.Value + 30), new Point(_startPosX.Value + 145, _startPosY.Value + 20), new Point(_startPosX.Value + 140, _startPosY.Value + 20), new Point(_startPosX.Value + 140, _startPosY.Value + 10), new Point(_startPosX.Value + 135, _startPosY.Value + 20), new Point(_startPosX.Value + 30, _startPosY.Value + 20) };
g.FillPolygon(additionalBrush, points3);
g.DrawPolygon(pen6, points3);
}
if (entitySeaPlane.Floats)
{
Point[] points4 = { new Point(_startPosX.Value + 10, _startPosY.Value + 40), new Point(_startPosX.Value + 110, _startPosY.Value + 40), new Point(_startPosX.Value + 110, _startPosY.Value + 41), new Point(_startPosX.Value + 70, _startPosY.Value + 50), new Point(_startPosX.Value + 30, _startPosY.Value + 50) };
g.FillPolygon(additionalBrush, points4);
g.DrawPolygon(pen, points4);
g.DrawLine(pen, _startPosX.Value + 30, _startPosY.Value + 45, _startPosX.Value + 80, _startPosY.Value + 45);
}
if (entitySeaPlane.Boat)
{
g.DrawRectangle(pen, _startPosX.Value + 85, _startPosY.Value + 15, 25, 10);
g.FillRectangle(boatBrush, _startPosX.Value + 85, _startPosY.Value + 15, 25, 10);
Point[] points5 = { new Point(_startPosX.Value + 80, _startPosY.Value + 15), new Point(_startPosX.Value + 85, _startPosY.Value + 10), new Point(_startPosX.Value + 115, _startPosY.Value + 10), new Point(_startPosX.Value + 115, _startPosY.Value + 15), new Point(_startPosX.Value + 85, _startPosY.Value + 15), new Point(_startPosX.Value + 85, _startPosY.Value + 25), new Point(_startPosX.Value + 115, _startPosY.Value + 25), new Point(_startPosX.Value + 115, _startPosY.Value + 30), new Point(_startPosX.Value + 85, _startPosY.Value + 30), new Point(_startPosX.Value + 80, _startPosY.Value + 25) };
g.FillPolygon(additionalBrush, points5);
g.DrawPolygon(pen, points5);
}
}
}
}

View File

@ -0,0 +1,38 @@
namespace ProjectPlane.Entities;
/// <summary>
/// Класс-сущность "самолет"
/// </summary>
public class EntityPlane
{
//свойства
/// <summary>
/// Скорость
/// </summary>
public int Speed { get; private set; }
/// <summary>
/// Вес
/// </summary>
public double Weight { get; private set; }
/// <summary>
/// Основной цвет
/// </summary>
public Color BodyColor { get; private set; }
/// <summary>
/// Шаг перемещения автомобиля
/// </summary>
public double Step => Speed * 100 / Weight;
/// <summary>
/// Инициализация полей объекта-класса самолета
/// </summary>
/// <param name="speed">скорость</param>
/// <param name="weight">вес</param>
/// <param name="bodyColor">основной цвет</param>
public EntityPlane(int speed, double weight, Color bodyColor)
{
Speed = speed;
Weight = weight;
BodyColor = bodyColor;
}
}

View File

@ -0,0 +1,30 @@
namespace ProjectPlane.Entities
{
internal class EntitySeaPlane : EntityPlane
{
/// <summary>
/// Признак (опция) наличие линии
/// </summary>
public bool Line { get; private set; }
/// <summary>
/// Признак (опция) наличие шлюпки
/// </summary>
public bool Boat { get; private set; }
/// <summary>
/// Признак (опция) наличие поплавков
/// </summary>
public bool Floats { get; private set; }
/// <summary>
/// Дополнительный цвет (для опциональных элементов)
/// </summary>
public Color AdditionalColor { get; private set; }
public EntitySeaPlane(int speed, double weight, Color bodyColor, Color additionalColor, bool line, bool boat, bool floats) : base(speed, weight, bodyColor)
{
AdditionalColor = additionalColor;
Line = line;
Boat = boat;
Floats = floats;
}
}
}

View File

@ -1,62 +0,0 @@
namespace ProjectPlane;
/// <summary>
/// Класс-сущность "самолет"
/// </summary>
public class EntityPlane
{
//свойства
/// <summary>
/// Скорость
/// </summary>
public int Speed { get; private set; }
/// <summary>
/// Вес
/// </summary>
public double Weight { get; private set; }
/// <summary>
/// Основной цвет
/// </summary>
public Color BodyColor { get; private set; }
/// <summary>
/// Дополнительный цвет (для опциональных элементов)
/// </summary>
public Color AdditionalColor { get; private set; }
/// <summary>
/// Признак (опция) наличие линии
/// </summary>
public bool Line { get; private set; }
/// <summary>
/// Признак (опция) пополавков
/// </summary>
public bool Floats { get; private set; }
/// <summary>
/// Признак (опция) наличие лодки
/// </summary>
public bool Boat { get; private set; }
//поле класса
/// <summary>
/// Шаг перемещения автомобиля
/// </summary>
public double Step => Speed * 100 / Weight;
/// <summary>
/// Инициализация полей объекта-класса самолета
/// </summary>
/// <param name="speed">скорость</param>
/// <param name="weight">вес</param>
/// <param name="bodyColor">основной цвет</param>
/// <param name="additionalColor">дополнительный цвет</param>
/// <param name="line">линия</param>
/// <param name="floats">поплавки</param>
/// <param name="boat">лодка фар</param>
public void Init(int speed, double weight, Color bodyColor, Color
additionalColor, bool line, bool floats, bool boat)
{
Speed = speed;
Weight = weight;
BodyColor = bodyColor;
AdditionalColor = additionalColor;
Line = line;
Floats = floats;
Boat = boat;
}
}

View File

@ -1,5 +1,4 @@

namespace ProjectPlane
namespace ProjectPlane
{
partial class FormPlane
{
@ -30,26 +29,29 @@ namespace ProjectPlane
private void InitializeComponent()
{
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(FormPlane));
pictureBox1 = new PictureBox();
pictureBoxPlane = new PictureBox();
button1 = new Button();
buttonUp = new Button();
buttonDown = new Button();
buttonRight = new Button();
buttonLeft = new Button();
((System.ComponentModel.ISupportInitialize)pictureBox1).BeginInit();
comboBoxStrategy = new ComboBox();
buttonCretaePlane = new Button();
buttonStrategyStep = new Button();
((System.ComponentModel.ISupportInitialize)pictureBoxPlane).BeginInit();
SuspendLayout();
//
// pictureBox1
// pictureBoxPlane
//
pictureBox1.Dock = DockStyle.Fill;
pictureBox1.Location = new Point(0, 0);
pictureBox1.Margin = new Padding(3, 2, 3, 2);
pictureBox1.Name = "pictureBox1";
pictureBox1.Size = new Size(700, 338);
pictureBox1.SizeMode = PictureBoxSizeMode.AutoSize;
pictureBox1.TabIndex = 0;
pictureBox1.TabStop = false;
pictureBox1.Click += pictureBox1_Click;
pictureBoxPlane.Dock = DockStyle.Fill;
pictureBoxPlane.Location = new Point(0, 0);
pictureBoxPlane.Margin = new Padding(3, 2, 3, 2);
pictureBoxPlane.Name = "pictureBoxPlane";
pictureBoxPlane.Size = new Size(700, 338);
pictureBoxPlane.SizeMode = PictureBoxSizeMode.AutoSize;
pictureBoxPlane.TabIndex = 0;
pictureBoxPlane.TabStop = false;
pictureBoxPlane.Click += pictureBoxPlane_Click;
//
// button1
//
@ -57,9 +59,9 @@ namespace ProjectPlane
button1.Location = new Point(10, 307);
button1.Margin = new Padding(3, 2, 3, 2);
button1.Name = "button1";
button1.Size = new Size(82, 22);
button1.Size = new Size(186, 22);
button1.TabIndex = 1;
button1.Text = "создать";
button1.Text = "создать гидроплан";
button1.UseVisualStyleBackColor = true;
button1.Click += ButtonCreatePlane_Click;
//
@ -115,33 +117,74 @@ namespace ProjectPlane
buttonLeft.UseVisualStyleBackColor = true;
buttonLeft.Click += ButtonMove_Click;
//
// comboBoxStrategy
//
comboBoxStrategy.Anchor = AnchorStyles.Top | AnchorStyles.Right;
comboBoxStrategy.DropDownStyle = ComboBoxStyle.DropDownList;
comboBoxStrategy.FormattingEnabled = true;
comboBoxStrategy.Items.AddRange(new object[] { "к центру", "к краю" });
comboBoxStrategy.Location = new Point(557, 9);
comboBoxStrategy.Margin = new Padding(3, 2, 3, 2);
comboBoxStrategy.Name = "comboBoxStrategy";
comboBoxStrategy.Size = new Size(133, 23);
comboBoxStrategy.TabIndex = 6;
//
// buttonCretaePlane
//
buttonCretaePlane.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
buttonCretaePlane.Location = new Point(210, 308);
buttonCretaePlane.Margin = new Padding(3, 2, 3, 2);
buttonCretaePlane.Name = "buttonCretaePlane";
buttonCretaePlane.Size = new Size(186, 22);
buttonCretaePlane.TabIndex = 7;
buttonCretaePlane.Text = "создать самолет";
buttonCretaePlane.UseVisualStyleBackColor = true;
buttonCretaePlane.Click += buttonCretaePlane_Click;
//
// buttonStrategyStep
//
buttonStrategyStep.Location = new Point(607, 34);
buttonStrategyStep.Margin = new Padding(3, 2, 3, 2);
buttonStrategyStep.Name = "buttonStrategyStep";
buttonStrategyStep.Size = new Size(82, 22);
buttonStrategyStep.TabIndex = 8;
buttonStrategyStep.Text = "шаг";
buttonStrategyStep.UseVisualStyleBackColor = true;
buttonStrategyStep.Click += ButtonStrategyStep_Click;
//
// FormPlane
//
AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(700, 338);
Controls.Add(buttonStrategyStep);
Controls.Add(buttonCretaePlane);
Controls.Add(comboBoxStrategy);
Controls.Add(buttonLeft);
Controls.Add(buttonRight);
Controls.Add(buttonDown);
Controls.Add(buttonUp);
Controls.Add(button1);
Controls.Add(pictureBox1);
Controls.Add(pictureBoxPlane);
Margin = new Padding(3, 2, 3, 2);
Name = "FormPlane";
Text = "FormPlane";
Click += ButtonMove_Click;
((System.ComponentModel.ISupportInitialize)pictureBox1).EndInit();
((System.ComponentModel.ISupportInitialize)pictureBoxPlane).EndInit();
ResumeLayout(false);
PerformLayout();
}
#endregion
private PictureBox pictureBox1;
private PictureBox pictureBoxPlane;
private Button button1;
private Button buttonUp;
private Button buttonDown;
private Button buttonRight;
private Button buttonLeft;
private ComboBox comboBoxStrategy;
private Button buttonCretaePlane;
private Button buttonStrategyStep;
}
}

View File

@ -1,4 +1,7 @@
namespace ProjectPlane
using ProjectPlane.Drawnings;
using ProjectPlane.MovementStrategy;
namespace ProjectPlane
{
public partial class FormPlane : Form
{
@ -6,14 +9,20 @@
/// Поле-объект для прорисовки объекта
/// </summary>
private DrawningPlane? _drawningPlane;
/// <summary>
/// Стратегия перемещения
/// </summary>
private AbstractStrategy? _strategy;
public FormPlane()
{
InitializeComponent();
_strategy = null;
}
/// <summary>
/// Метод прорисовки самолета
/// Метод прорисовки круисера
/// </summary>
private void Draw()
{
@ -21,35 +30,64 @@
{
return;
}
Bitmap bmp = new(pictureBox1.Width,
pictureBox1.Height);
Bitmap bmp = new(pictureBoxPlane.Width,
pictureBoxPlane.Height);
Graphics gr = Graphics.FromImage(bmp);
_drawningPlane.DrawTransport(gr);
pictureBox1.Image = bmp;
pictureBoxPlane.Image = bmp;
}
/// <summary>
/// Обработка нажатия кнопки "Создать"
/// Создание объекта класса-перемещения
/// </summary>
/// <param name="type">Тип создаваемого объекта</param>
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(DrawningSeaPlane):
_drawningPlane = new DrawningSeaPlane(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(pictureBoxPlane.Width,
pictureBoxPlane.Height);
_drawningPlane.SetPosition(random.Next(10, 100), random.Next(10, 100));
_strategy = null;
comboBoxStrategy.Enabled = true;
Draw();
}
/// <summary>
/// Обработка нажатия кнопки "Создать военный самолет"
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonCreatePlane_Click(object sender, EventArgs e)
{
Random random = new();
_drawningPlane = new DrawningPlane();
_drawningPlane.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)));
_drawningPlane.SetPictureSize(pictureBox1.Width,
pictureBox1.Height);
private void ButtonCreatePlane_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningSeaPlane));
/// <summary>
/// Обработка нажатия кнопки "Создать самолет"
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonCretaePlane_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningPlane));
//начальное положение самолета
_drawningPlane.SetPosition(random.Next(10, 100), random.Next(10, 100));
Draw();
}
/// <summary>
/// Перемещение объекта по форме (нажатие кнопок навигации)
/// </summary>
@ -88,7 +126,51 @@
}
}
private void pictureBox1_Click(object sender, EventArgs e)
/// <summary>
/// обработка нажатия кнопки шаг
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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), pictureBoxPlane.Width, pictureBoxPlane.Height);
}
if (_strategy == null)
{
return;
}
comboBoxStrategy.Enabled = false;
_strategy.MakeStep();
Draw();
if (_strategy.GetStatus() == StrategyStatus.Finish)
{
comboBoxStrategy.Enabled = true;
_strategy = null;
}
}
private void pictureBoxPlane_Click(object sender, EventArgs e)
{
}

View File

@ -0,0 +1,123 @@
namespace ProjectPlane.MovementStrategy
{
/// <summary>
/// Класс-стратегия перемещения объекта
/// </summary>
public abstract class AbstractStrategy
{
/// <summary>
/// Перемещаемый объект
/// </summary>
private IMoveableObject? _moveableObject;
/// <summary>
/// Статус перемещения
/// </summary>
private StrategyStatus _state = StrategyStatus.NotInit;
/// <summary>
/// Ширина поля
/// </summary>
protected int FieldWidth { get; private set; }
/// <summary>
/// Высота поля
/// </summary>
protected int FieldHeight { get; private set; }
/// <summary>
/// Статус перемещения
/// </summary>
public StrategyStatus GetStatus() { return _state; }
/// <summary>
/// Установка данных
/// </summary>
/// <param name="moveableObject">Перемещаемый объект</param>
/// <param name="width">Ширина поля</param>
/// <param name="height">Высота поля</param>
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;
}
/// <summary>
/// Шаг перемещения
/// </summary>
public void MakeStep()
{
if (_state != StrategyStatus.InProgress)
{
return;
}
if (IsTargetDestinaion())
{
_state = StrategyStatus.Finish;
return;
}
MoveToTarget();
}
/// <summary>
/// Перемещение влево
/// </summary>
/// <returns>Результат перемещения (true - удалось переместиться, false - неудача)</returns>
protected bool MoveLeft() => MoveTo(MovementDirection.Left);
/// <summary>
/// Перемещение вправо
/// </summary>
/// <returns>Результат перемещения (true - удалось переместиться, false - неудача)</returns>
protected bool MoveRight() => MoveTo(MovementDirection.Right);
/// <summary>
/// Перемещение вверх
/// </summary>
/// <returns>Результат перемещения (true - удалось переместиться, false - неудача)</returns>
protected bool MoveUp() => MoveTo(MovementDirection.Up);
/// <summary>
/// Перемещение вниз
/// </summary>
/// <returns>Результат перемещения (true - удалось переместиться, false - неудача)</returns>
protected bool MoveDown() => MoveTo(MovementDirection.Down);
/// <summary>
/// Параметры объекта
/// </summary>
protected ObjectParameters? GetObjectParameters =>
_moveableObject?.GetObjectPosition;
/// <summary>
/// Шаг объекта
/// </summary>
/// <returns></returns>
protected int? GetStep()
{
if (_state != StrategyStatus.InProgress)
{
return null;
}
return _moveableObject?.GetStep;
}
/// <summary>
/// Перемещение к цели
/// </summary>
protected abstract void MoveToTarget();
/// <summary>
/// Достигнута ли цель
/// </summary>
/// <returns></returns>
protected abstract bool IsTargetDestinaion();
/// <summary>
/// Попытка перемещения в требуемом направлении
/// </summary>
/// <param name="movementDirection">Направление</param>
/// <returns>Результат попытки (true - удалось переместиться, false - неудача)</returns>
private bool MoveTo(MovementDirection movementDirection)
{
if (_state != StrategyStatus.InProgress)
{
return false;
}
return _moveableObject?.TryMoveObject(movementDirection) ?? false;
}
}
}

View File

@ -0,0 +1,24 @@
namespace ProjectPlane.MovementStrategy
{
/// <summary>
/// Интерфейс для работы с перемещаемым объектом
/// </summary>
public interface IMoveableObject
{
/// <summary>
/// Получение координаты объекта
/// </summary>
ObjectParameters? GetObjectPosition { get; }
/// <summary>
/// Шаг объекта
/// </summary>
int GetStep { get; }
/// <summary>
/// Попытка переместить объект в указанном направлении
/// </summary>
/// <param name="direction">Направление</param>
/// <returns>true - объект перемещен, false - перемещение невозможно</returns>
bool TryMoveObject(MovementDirection direction);
}
}

View File

@ -0,0 +1,53 @@
namespace ProjectPlane.MovementStrategy
{
/// <summary>
/// Стратегия перемещения объекта к краю экрана
/// </summary>
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();
}
}
}
}
}

View File

@ -0,0 +1,53 @@
namespace ProjectPlane.MovementStrategy
{
/// <summary>
/// Стратегия перемещения объекта в центр экрана
/// </summary>
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();
}
}
}
}
}

View File

@ -0,0 +1,62 @@
using ProjectPlane.Drawnings;
namespace ProjectPlane.MovementStrategy
{
/// <summary>
/// класс реалтзация для IMoveableObject с использованием DrawningPlane
/// </summary>
public class MoveablePlane : IMoveableObject
{
/// <summary>
/// Поле-объект класса DrawningPlane или его наследника
/// </summary>
private readonly DrawningPlane? _cruiser = null;
/// <summary>
/// Конструктор
/// </summary>
/// <param name="cruiser">Объект класса DrawningPlane</param>
public MoveablePlane(DrawningPlane cruiser)
{
_cruiser = cruiser;
}
public ObjectParameters? GetObjectPosition
{
get
{
if (_cruiser == null || _cruiser.EntityPlane == 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?.EntityPlane?.Step ?? 0);
public bool TryMoveObject(MovementDirection direction)
{
if (_cruiser == null || _cruiser.EntityPlane == null)
{
return false;
}
return _cruiser.MoveTransport(GetDirectionType(direction));
}
/// <summary>
/// Конвертация из MovementDirection в DirectionType
/// </summary>
/// <param name="direction">MovementDirection</param>
/// <returns>DirectionType</returns>
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,
};
}
}
}

View File

@ -0,0 +1,26 @@
namespace ProjectPlane.MovementStrategy
{
/// <summary>
/// Направление перемещения
/// </summary>
public enum MovementDirection
{
/// <summary>
/// Вверх
/// </summary>
Up = 1,
/// <summary>
/// Вниз
/// </summary>
Down = 2,
/// <summary>
/// Влево
/// </summary>
Left = 3,
/// <summary>
/// Вправо
/// </summary>
Right = 4
}
}

View File

@ -0,0 +1,64 @@
namespace ProjectPlane.MovementStrategy
{
/// <summary>
/// Параметры-координаты объекта
/// </summary>
public class ObjectParameters
{
/// <summary>
/// Координата X
/// </summary>
private readonly int _x;
/// <summary>
/// Координата Y
/// </summary>
private readonly int _y;
/// <summary>
/// Ширина объекта
/// </summary>
private readonly int _width;
/// <summary>
/// Высота объекта
/// </summary>
private readonly int _height;
/// <summary>
/// Левая граница
/// </summary>
public int LeftBorder => _x;
/// <summary>
/// Верхняя граница
/// </summary>
public int TopBorder => _y;
/// <summary>
/// Правая граница
/// </summary>
public int RightBorder => _x + _width;
/// <summary>
/// Нижняя граница
/// </summary>
public int DownBorder => _y + _height;
/// <summary>
/// Середина объекта
/// </summary>
public int ObjectMiddleHorizontal => _x + _width / 2;
/// <summary>
/// Середина объекта
/// </summary>
public int ObjectMiddleVertical => _y + _height / 2;
/// <summary>
/// Конструктор
/// </summary>
/// <param name="x">Координата X</param>
/// <param name="y">Координата Y</param>
/// <param name="width">Ширина объекта</param>
/// <param name="height">Высота объекта</param>
public ObjectParameters(int x, int y, int width, int height)
{
_x = x;
_y = y;
_width = width;
_height = height;
}
}
}

View File

@ -0,0 +1,22 @@
namespace ProjectPlane.MovementStrategy
{
/// <summary>
/// Статус выполнения операции перемещения
/// </summary>
public enum StrategyStatus
{
/// <summary>
/// Все готово к началу
/// </summary>
NotInit,
/// <summary>
/// Выполняется
/// </summary>
InProgress,
/// <summary>
/// Завершено
/// </summary>
Finish
}
}