diff --git a/AntiAirCraftGun/AntiAirCraftGun/DrawingAntiAirCraftGun.cs b/AntiAirCraftGun/AntiAirCraftGun/DrawingAntiAirCraftGun.cs deleted file mode 100644 index dc404a8..0000000 --- a/AntiAirCraftGun/AntiAirCraftGun/DrawingAntiAirCraftGun.cs +++ /dev/null @@ -1,171 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Drawing.Drawing2D; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using static System.Windows.Forms.AxHost; - -namespace AntiAircraftGun -{ - public class DrawingAntiAirCraftGun - - { - private Point[] points = new Point[4]; - /// - /// Класс-сущность - /// - public EntityAntiAircraftGun? AntiAircraftGun { get; private set; } - /// - /// Ширина окна - /// - private int _pictureWidth; - /// - /// Высота окна - /// - private int _pictureHeight; - /// - /// Левая координата прорисовки автомобиля - /// - private int _startPosX; - /// - /// Верхняя кооридната прорисовки автомобиля - /// - private int _startPosY; - /// - /// Ширина прорисовки автомобиля - /// - private readonly int _zenitWidth = 110; - /// - /// Высота прорисовки автомобиля - /// - private readonly int _zenitHeight = 60; - /// - /// Инициализация свойств - /// - /// Скорость - /// Вес - /// Цвет кузова - - /// Ширина картинки - /// Высота картинки - /// true - объект создан, false - проверка не пройдена, - ///нельзя создать объект в этих размерах - public bool Init(int speed, double weight, Color bodyColor, Color - additionalColor, Color dopColor, bool rocket, bool radar, int width, int height) - { - if (width <= _zenitWidth || height <= _zenitHeight) return false; - - _pictureWidth = width; - _pictureHeight = height; - AntiAircraftGun = new EntityAntiAircraftGun(); - AntiAircraftGun.Init(speed, weight, bodyColor, additionalColor,dopColor,rocket,radar); - return true; - } - /// - /// Установка позиции - /// - /// Координата X - /// Координата Y - public void SetPosition(int x, int y) - { - if (AntiAircraftGun == null) return; - _startPosX = x; - _startPosY = y; - if (x < 0 || y < 0 ||x + _zenitWidth >= _pictureWidth || y + _zenitHeight>= _pictureHeight) - { - _startPosX = 1; - _startPosY = 1; - } - } - /// - /// Изменение направления перемещения - /// - /// Направление - public void MoveTransport(DirectionType direction) - { - if (AntiAircraftGun == null) - { - return; - } - switch (direction) - { - //влево - case DirectionType.Left: - if (_startPosX - AntiAircraftGun.Step > 0) - { - _startPosX -= (int)AntiAircraftGun.Step; - } - break; - //вверх - case DirectionType.Up: - if (_startPosY - AntiAircraftGun.Step > 0) - { - _startPosY -= (int)AntiAircraftGun.Step; - } - break; - // вправо - case DirectionType.Right: - if (_startPosX + AntiAircraftGun.Step + _zenitWidth < _pictureWidth) - { - _startPosX += (int)AntiAircraftGun.Step; - } - break; - //вниз - case DirectionType.Down: - if (_startPosY + AntiAircraftGun.Step + _zenitHeight < _pictureHeight) - { - _startPosY += (int)AntiAircraftGun.Step; - } - break; - } - } - /// - /// Прорисовка объекта - /// - /// - public void DrawTransport(Graphics g) - { - if (AntiAircraftGun == null) - { - return; - } - Pen pen = new(Color.Black); - Brush bodyBrush = new SolidBrush(AntiAircraftGun.BodyColor); - Brush additionalBrush = new SolidBrush(AntiAircraftGun.AdditionalColor); - - - g.FillEllipse(additionalBrush, _startPosX, _startPosY+40, 110, 10); - g.DrawEllipse(pen, _startPosX, _startPosY+40, 110, 10); - g.FillRectangle(bodyBrush, _startPosX, _startPosY+30, 110, 10); - g.DrawRectangle(pen, _startPosX, _startPosY+30, 110, 10); - g.FillRectangle(bodyBrush, _startPosX+80, _startPosY+10, 30, 20); - g.DrawRectangle(pen, _startPosX+80, _startPosY+10, 30, 20); - for (int i = 0; i < 4; i++) - { - Rectangle trackRect = new Rectangle(_startPosX+20 + i*19, _startPosY+40, 10, 10); - g.DrawEllipse(pen, trackRect); - g.FillEllipse(Brushes.Black, trackRect); - } - Brush dopBrush = new SolidBrush(AntiAircraftGun.DopColor); - Pen dopPen = new Pen(AntiAircraftGun.DopColor); - if (AntiAircraftGun.Rocket) - { - points[0] = new Point(_startPosX, _startPosY + 30); - points[1] = new Point(_startPosX + 95, _startPosY + 5); - points[2] = new Point(_startPosX + 92, _startPosY); - points[3] = new Point(_startPosX, _startPosY + 25); - g.FillPolygon(dopBrush, points); - g.DrawPolygon(pen, points); - } - if (AntiAircraftGun.Radar) - { - g.DrawLine(dopPen, _startPosX + 105, _startPosY + 20, _startPosX +105, _startPosY + 5); - g.FillPie(dopBrush, _startPosX + 81, _startPosY-15, 30, 30, -45, 180); - g.DrawLine(dopPen, _startPosX + 98, _startPosY, _startPosX + 93, _startPosY + 10); - g.FillEllipse(dopBrush, _startPosX + 88, _startPosY -10, 10, 10); - } - } - } -} - diff --git a/AntiAirCraftGun/AntiAirCraftGun/DrawingObjects/AdvancedDrawingAntiAirCraftGun.cs b/AntiAirCraftGun/AntiAirCraftGun/DrawingObjects/AdvancedDrawingAntiAirCraftGun.cs new file mode 100644 index 0000000..8d28d7e --- /dev/null +++ b/AntiAirCraftGun/AntiAirCraftGun/DrawingObjects/AdvancedDrawingAntiAirCraftGun.cs @@ -0,0 +1,54 @@ +using System; +using System.Collections.Generic; +using System.Drawing.Drawing2D; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using AntiAircraftGun.Enitites; +using static System.Windows.Forms.AxHost; + +namespace AntiAircraftGun.DrawingObjects +{ + public class AdvancedDrawingAntiAirCraftGun : BaseDrawingAntiAirCraftGun + { + private Point[] points = new Point[4]; + + public AdvancedDrawingAntiAirCraftGun(int speed, double weight, Color bodyColor, Color additionalColor, Color dopColor, bool rocket,bool radar, int width, int height) : + base(speed, weight, bodyColor, additionalColor, width, height) + { + AntiAirСraftGun = new EntityAdvancedAntiAirCraftGun(speed, weight, bodyColor, additionalColor, dopColor, rocket,radar); + } + + public override void DrawTransport(Graphics g) + { + if (AntiAirСraftGun is not EntityAdvancedAntiAirCraftGun advancedGun) + { + return; + } + + Pen pen = new Pen(Color.Black); + Brush bodyBrush = new SolidBrush(AntiAirСraftGun.BodyColor); + Brush additionalBrush = new SolidBrush(AntiAirСraftGun.AdditionalColor); + base.DrawTransport(g); + Brush dopBrush = new SolidBrush(advancedGun.DopColor); + Pen dopPen = new Pen(advancedGun.DopColor); + if (advancedGun.Rocket) + { + points[0] = new Point(_startPosX, _startPosY + 30); + points[1] = new Point(_startPosX + 95, _startPosY + 5); + points[2] = new Point(_startPosX + 92, _startPosY); + points[3] = new Point(_startPosX, _startPosY + 25); + g.FillPolygon(dopBrush, points); + g.DrawPolygon(pen, points); + } + if (advancedGun.Radar) + { + g.DrawLine(dopPen, _startPosX + 105, _startPosY + 20, _startPosX +105, _startPosY + 5); + g.FillPie(dopBrush, _startPosX + 81, _startPosY-15, 30, 30, -45, 180); + g.DrawLine(dopPen, _startPosX + 98, _startPosY , _startPosX + 93, _startPosY + 10); + g.FillEllipse(dopBrush, _startPosX + 88, _startPosY -10, 10, 10); + } + + } + } +} \ No newline at end of file diff --git a/AntiAirCraftGun/AntiAirCraftGun/DrawingObjects/BaseDrawingAntiAirCraftGun.cs b/AntiAirCraftGun/AntiAirCraftGun/DrawingObjects/BaseDrawingAntiAirCraftGun.cs new file mode 100644 index 0000000..5ab4d02 --- /dev/null +++ b/AntiAirCraftGun/AntiAirCraftGun/DrawingObjects/BaseDrawingAntiAirCraftGun.cs @@ -0,0 +1,142 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using AntiAircraftGun.Enitites; + +namespace AntiAircraftGun.DrawingObjects +{ + public class BaseDrawingAntiAirCraftGun + { + public EntityAntiAirCraftGun? AntiAirСraftGun { get; protected set; } + private readonly int _pictureWidth; + private readonly int _pictureHeight; + protected int _startPosX; + protected int _startPosY; + protected int _zenitWidth = 110; + protected int _zenitHeight = 60; + + public BaseDrawingAntiAirCraftGun(int speed, double weight, Color bodyColor, Color additionalColor, int width, int height) + { + _pictureWidth = width; + _pictureHeight = height; + AntiAirСraftGun = new EntityAntiAirCraftGun(speed, weight, bodyColor, additionalColor); + } + + protected BaseDrawingAntiAirCraftGun(int speed, double weight, Color bodyColor, Color additionalColor, int width, int height, int carWidth, int carHeight) + { + _pictureWidth = width; + _pictureHeight = height; + _zenitWidth = carWidth; + _zenitHeight = carHeight; + AntiAirСraftGun = new EntityAntiAirCraftGun(speed, weight, bodyColor, additionalColor); + } + + public void SetPosition(int x, int y) + { + if (AntiAirСraftGun == null) return; + _startPosX = x; + _startPosY = y; + if (x < 0 || y < 0 || x + _zenitWidth >= _pictureWidth || y + _zenitHeight >= _pictureHeight) + { + _startPosX = 1; + _startPosY = 1; + } + } + /// + /// Координата X объекта + /// + public int GetPosX => _startPosX; + /// + /// Координата Y объекта + /// + public int GetPosY => _startPosY; + /// + /// Ширина объекта + /// + public int GetWidth => _zenitWidth; + /// + /// Высота объекта + /// + public int GetHeight => _zenitHeight; + + /// + /// Проверка, что объект может переместится по указанному направлению + /// + /// Направление + /// true - можно переместится по указанному направлению + public bool CanMove(DirectionType direction) + { + if (AntiAirСraftGun == null) + { + return false; + } + return direction switch + { + //влево + DirectionType.Left => _startPosX - AntiAirСraftGun.Step > 0, + //вверх + DirectionType.Up => _startPosY - AntiAirСraftGun.Step > 0, + // вправо + DirectionType.Right =>_startPosX + AntiAirСraftGun.Step + _zenitWidth < _pictureWidth, + //вниз + DirectionType.Down => _startPosY + AntiAirСraftGun.Step + _zenitHeight < _pictureHeight, + _ => false, + }; + } + /// + /// Изменение направления перемещения + /// + /// Направление + public void MoveTransport(DirectionType direction) + { + if (!CanMove(direction) || AntiAirСraftGun == null) + { + return; + } + switch (direction) + { + //влево + case DirectionType.Left: + _startPosX -= (int)AntiAirСraftGun.Step; + break; + //вверх + case DirectionType.Up: + _startPosY -= (int)AntiAirСraftGun.Step; + break; + // вправо + case DirectionType.Right: + _startPosX += (int)AntiAirСraftGun.Step; + break; + //вниз + case DirectionType.Down: + _startPosY += (int)AntiAirСraftGun.Step; + break; + } + } + + public virtual void DrawTransport(Graphics g) + { + + + Pen pen = Pens.Black; + Brush bodyBrush = new SolidBrush(AntiAirСraftGun.BodyColor); + Brush additionalBrush = new SolidBrush(AntiAirСraftGun.AdditionalColor); + + g.FillEllipse(additionalBrush, _startPosX, _startPosY + 40, 110, 10); + g.DrawEllipse(pen, _startPosX, _startPosY + 40, 110, 10); + g.FillRectangle(bodyBrush, _startPosX, _startPosY + 30, 110, 10); + g.DrawRectangle(pen, _startPosX, _startPosY + 30, 110, 10); + g.FillRectangle(bodyBrush, _startPosX + 80, _startPosY + 10, 30, 20); + g.DrawRectangle(pen, _startPosX + 80, _startPosY + 10, 30, 20); + + for (int i = 0; i < 4; i++) + { + Rectangle trackRect = new Rectangle(_startPosX + 20 + i * 19, _startPosY + 40, 10, 10); + g.DrawEllipse(pen, trackRect); + g.FillEllipse(Brushes.Black, trackRect); + } + } + } +} diff --git a/AntiAirCraftGun/AntiAirCraftGun/Enitites/EntityAdvancedAntiAircraftGun.cs b/AntiAirCraftGun/AntiAirCraftGun/Enitites/EntityAdvancedAntiAircraftGun.cs new file mode 100644 index 0000000..31daa0d --- /dev/null +++ b/AntiAirCraftGun/AntiAirCraftGun/Enitites/EntityAdvancedAntiAircraftGun.cs @@ -0,0 +1,28 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AntiAircraftGun.Enitites +{ + public class EntityAdvancedAntiAirCraftGun : EntityAntiAirCraftGun + { + + public Color DopColor { get; private set; } + + public bool Rocket { get; private set; } + + public bool Radar { get; private set; } + + + + public EntityAdvancedAntiAirCraftGun(int speed, double weight, Color bodyColor, Color additionalColor,Color dopColor, bool rocket, bool radar) + : base(speed, weight, bodyColor, additionalColor) + { + DopColor = dopColor; + Rocket = rocket; + Radar = radar; + } + } +} diff --git a/AntiAirCraftGun/AntiAirCraftGun/EntityAntiAircraftGun.cs b/AntiAirCraftGun/AntiAirCraftGun/Enitites/EntityAntiAirCraftGun.cs similarity index 53% rename from AntiAirCraftGun/AntiAirCraftGun/EntityAntiAircraftGun.cs rename to AntiAirCraftGun/AntiAirCraftGun/Enitites/EntityAntiAirCraftGun.cs index c6c6b17..ce0e940 100644 --- a/AntiAirCraftGun/AntiAirCraftGun/EntityAntiAircraftGun.cs +++ b/AntiAirCraftGun/AntiAirCraftGun/Enitites/EntityAntiAirCraftGun.cs @@ -4,14 +4,14 @@ using System.Linq; using System.Text; using System.Threading.Tasks; -namespace AntiAircraftGun +namespace AntiAircraftGun.Enitites { - public class EntityAntiAircraftGun + public class EntityAntiAirCraftGun { /// - /// Скорость - /// - public int Speed { get; private set; } + /// Скорость + /// + public int Speed { get; private set; } /// /// Вес /// @@ -21,7 +21,7 @@ namespace AntiAircraftGun /// public Color BodyColor { get; private set; } /// - /// Дополнительный цвет (для опциональных элементов) + /// Шаг перемещения автомобиля /// public Color AdditionalColor { get; private set; } /// @@ -29,33 +29,17 @@ namespace AntiAircraftGun /// public double Step => (double)Speed * 100 / Weight; /// - /// Цвет для доп. деталей - /// - public Color DopColor { get; private set; } - /// - /// Ракета - /// - public bool Rocket { get; private set; } - /// - /// Радар - /// - public bool Radar { get; private set; } - /// - /// Инициализация полей объекта-класса спортивного автомобиля + /// Конструктор с параметрами /// /// Скорость /// Вес автомобиля /// Основной цвет - - public void Init(int speed, double weight, Color bodyColor, Color additionalColor, Color dopColor, bool rocket, bool radar) + public EntityAntiAirCraftGun(int speed, double weight, Color bodyColor, Color additionalColor) { Speed = speed; Weight = weight; BodyColor = bodyColor; AdditionalColor = additionalColor; - DopColor = dopColor; - Rocket = rocket; - Radar = radar; } } } diff --git a/AntiAirCraftGun/AntiAirCraftGun/FormAntiAirCraftGun.Designer.cs b/AntiAirCraftGun/AntiAirCraftGun/FormAntiAirCraftGun.Designer.cs index 347ee34..847b92d 100644 --- a/AntiAirCraftGun/AntiAirCraftGun/FormAntiAirCraftGun.Designer.cs +++ b/AntiAirCraftGun/AntiAirCraftGun/FormAntiAirCraftGun.Designer.cs @@ -2,38 +2,41 @@ { partial class FormAntiAirCraftGun { - /// - /// Required designer variable. - /// - private System.ComponentModel.IContainer components = null; + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; - /// - /// Clean up any resources being used. - /// - /// true if managed resources should be disposed; otherwise, false. - protected override void Dispose(bool disposing) - { - if (disposing && (components != null)) - { - components.Dispose(); - } - base.Dispose(disposing); - } + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } - #region Windows Form Designer generated code + #region Windows Form Designer generated code - /// - /// Required method for Designer support - do not modify - /// the contents of this method with the code editor. - /// - private void InitializeComponent() - { + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { this.pictureBoxAntiAircraftGun = new System.Windows.Forms.PictureBox(); - this.buttonCreate = new System.Windows.Forms.Button(); + this.CreateAdavancedAntiAirCraftGun = new System.Windows.Forms.Button(); this.buttonLeft = new System.Windows.Forms.Button(); this.buttonUp = new System.Windows.Forms.Button(); this.buttonRight = new System.Windows.Forms.Button(); this.buttonDown = new System.Windows.Forms.Button(); + this.comboBoxStrategy = new System.Windows.Forms.ComboBox(); + this.CreateAntiAirCraftGun = new System.Windows.Forms.Button(); + this.Step = new System.Windows.Forms.Button(); ((System.ComponentModel.ISupportInitialize)(this.pictureBoxAntiAircraftGun)).BeginInit(); this.SuspendLayout(); // @@ -47,16 +50,16 @@ this.pictureBoxAntiAircraftGun.TabIndex = 0; this.pictureBoxAntiAircraftGun.TabStop = false; // - // buttonCreate + // CreateAdavancedAntiAirCraftGun // - this.buttonCreate.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); - this.buttonCreate.Location = new System.Drawing.Point(32, 577); - this.buttonCreate.Name = "buttonCreate"; - this.buttonCreate.Size = new System.Drawing.Size(75, 23); - this.buttonCreate.TabIndex = 1; - this.buttonCreate.Text = "Создать"; - this.buttonCreate.UseVisualStyleBackColor = true; - this.buttonCreate.Click += new System.EventHandler(this.buttonCreate_Click); + this.CreateAdavancedAntiAirCraftGun.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); + this.CreateAdavancedAntiAirCraftGun.Location = new System.Drawing.Point(32, 577); + this.CreateAdavancedAntiAirCraftGun.Name = "CreateAdavancedAntiAirCraftGun"; + this.CreateAdavancedAntiAirCraftGun.Size = new System.Drawing.Size(183, 23); + this.CreateAdavancedAntiAirCraftGun.TabIndex = 1; + this.CreateAdavancedAntiAirCraftGun.Text = "Создать с дополнениями"; + this.CreateAdavancedAntiAirCraftGun.UseVisualStyleBackColor = true; + this.CreateAdavancedAntiAirCraftGun.Click += new System.EventHandler(this.CreateAdvancedAintiAirCraftGun_Click); // // buttonLeft // @@ -106,16 +109,52 @@ this.buttonDown.UseVisualStyleBackColor = true; this.buttonDown.Click += new System.EventHandler(this.ButtonMove_Click); // + // comboBoxStrategy + // + this.comboBoxStrategy.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; + this.comboBoxStrategy.FormattingEnabled = true; + this.comboBoxStrategy.Items.AddRange(new object[] { + "Двигаться в центер ", + "Двигаться в правый нижний угол"}); + this.comboBoxStrategy.Location = new System.Drawing.Point(1244, 28); + this.comboBoxStrategy.Name = "comboBoxStrategy"; + this.comboBoxStrategy.Size = new System.Drawing.Size(170, 23); + this.comboBoxStrategy.TabIndex = 6; + + // + // CreateAntiAirCraftGun + // + this.CreateAntiAirCraftGun.Location = new System.Drawing.Point(221, 577); + this.CreateAntiAirCraftGun.Name = "CreateAntiAirCraftGun"; + this.CreateAntiAirCraftGun.Size = new System.Drawing.Size(185, 23); + this.CreateAntiAirCraftGun.TabIndex = 7; + this.CreateAntiAirCraftGun.Text = "Создать обычный"; + this.CreateAntiAirCraftGun.UseVisualStyleBackColor = true; + this.CreateAntiAirCraftGun.Click += new System.EventHandler(this.CreateAntiAirCraftGun_Click); + // + // Step + // + this.Step.Location = new System.Drawing.Point(1323, 61); + this.Step.Name = "Step"; + this.Step.Size = new System.Drawing.Size(75, 23); + this.Step.TabIndex = 8; + this.Step.Text = "Шаг"; + this.Step.UseVisualStyleBackColor = true; + this.Step.Click += new System.EventHandler(this.ButtonStep_Click); + // // FormAntiAirCraftGun // this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(1491, 628); + this.Controls.Add(this.Step); + this.Controls.Add(this.CreateAntiAirCraftGun); + this.Controls.Add(this.comboBoxStrategy); this.Controls.Add(this.buttonDown); this.Controls.Add(this.buttonRight); this.Controls.Add(this.buttonUp); this.Controls.Add(this.buttonLeft); - this.Controls.Add(this.buttonCreate); + this.Controls.Add(this.CreateAdavancedAntiAirCraftGun); this.Controls.Add(this.pictureBoxAntiAircraftGun); this.Name = "FormAntiAirCraftGun"; this.Text = "Form1"; @@ -123,16 +162,18 @@ this.ResumeLayout(false); this.PerformLayout(); - } + } - #endregion - - private PictureBox pictureBoxAntiAircraftGun; - private Button buttonCreate; - private Button buttonLeft; - private Button buttonUp; - private Button buttonRight; - private Button buttonDown; + #endregion + private PictureBox pictureBoxAntiAircraftGun; + private Button CreateAdavancedAntiAirCraftGun; + private Button buttonLeft; + private Button buttonUp; + private Button buttonRight; + private Button buttonDown; + private ComboBox comboBoxStrategy; + private Button CreateAntiAirCraftGun; + private Button Step; } } \ No newline at end of file diff --git a/AntiAirCraftGun/AntiAirCraftGun/FormAntiAirCraftGun.cs b/AntiAirCraftGun/AntiAirCraftGun/FormAntiAirCraftGun.cs index c5c8fea..2321e34 100644 --- a/AntiAirCraftGun/AntiAirCraftGun/FormAntiAirCraftGun.cs +++ b/AntiAirCraftGun/AntiAirCraftGun/FormAntiAirCraftGun.cs @@ -1,29 +1,18 @@ +using AntiAircraftGun.DrawingObjects; +using AntiAircraftGun.MovementStrategy; + namespace AntiAircraftGun { - public partial class FormAntiAirCraftGun : Form - { - /// - /// - - /// - private DrawingAntiAirCraftGun? _drawingAntiAirCraftGun; - public FormAntiAirCraftGun() - { - InitializeComponent(); - } - /// - /// - /// - private void Draw() + public partial class FormAntiAirCraftGun : Form + { + private BaseDrawingAntiAirCraftGun _drawingAntiAirCraftGun; + + private AbstractStrategy? _abstractStrategy; + public FormAntiAirCraftGun() { - if (_drawingAntiAirCraftGun == null) - { - return; - } - Bitmap bmp = new(pictureBoxAntiAircraftGun.Width,pictureBoxAntiAircraftGun.Height); - Graphics gr = Graphics.FromImage(bmp); - _drawingAntiAirCraftGun.DrawTransport(gr); - pictureBoxAntiAircraftGun.Image = bmp; + InitializeComponent(); } + private void ButtonMove_Click(object sender, EventArgs e) { if (_drawingAntiAirCraftGun == null) @@ -48,28 +37,89 @@ namespace AntiAircraftGun } Draw(); } - private void buttonCreate_Click(object sender, EventArgs e) + private void Draw() { - Random random = new(); - _drawingAntiAirCraftGun = new DrawingAntiAirCraftGun(); - _drawingAntiAirCraftGun.Init(random.Next(100, 300),//c - 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)), - - Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)), // . 2 - Convert.ToBoolean(random.Next(2)), // Rocket - Convert.ToBoolean(random.Next(2)), // Radar - - pictureBoxAntiAircraftGun.Width, pictureBoxAntiAircraftGun.Height); - _drawingAntiAirCraftGun.SetPosition(random.Next(10, 100), - random.Next(10, 100)); + if (_drawingAntiAirCraftGun == null) + { + return; + } + Bitmap bmp = new Bitmap(pictureBoxAntiAircraftGun.Width, pictureBoxAntiAircraftGun.Height); + Graphics gr = Graphics.FromImage(bmp); + _drawingAntiAirCraftGun.DrawTransport(gr); + pictureBoxAntiAircraftGun.Image = bmp; + } + private void CreateAdvancedAintiAirCraftGun_Click(object sender, EventArgs e) + { + Random random = new Random(); + _drawingAntiAirCraftGun = new AdvancedDrawingAntiAirCraftGun( + 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)), // . + Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)), // . EntityAdvancedAntiAircraftGun + Convert.ToBoolean(random.Next(2)), // Rocket + Convert.ToBoolean(random.Next(2)), + pictureBoxAntiAircraftGun.Width, + pictureBoxAntiAircraftGun.Height + ); + _drawingAntiAirCraftGun.SetPosition(random.Next(10, 100), random.Next(10, 100)); Draw(); } - + + private void CreateAntiAirCraftGun_Click(object sender, EventArgs e) + { + Random random = new Random(); + _drawingAntiAirCraftGun = new BaseDrawingAntiAirCraftGun( + 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)), // . + pictureBoxAntiAircraftGun.Width, + pictureBoxAntiAircraftGun.Height + ); + + _drawingAntiAirCraftGun.SetPosition(random.Next(10, 100), random.Next(10, 100)); + Draw(); + } + + private void ButtonStep_Click(object sender, EventArgs e) + { + if (_drawingAntiAirCraftGun == null) + { + return; + } + if (comboBoxStrategy.Enabled) + { + _abstractStrategy = comboBoxStrategy.SelectedIndex + switch + { + 0 => new MoveToCenter(), + 1 => new MoveToBorder(), + _ => null, + }; + if (_abstractStrategy == null) + { + return; + } + _abstractStrategy.SetData(new + DrawingObjectAntiAirCraftGun(_drawingAntiAirCraftGun), pictureBoxAntiAircraftGun.Width, + pictureBoxAntiAircraftGun.Height); + comboBoxStrategy.Enabled = false; + } + if (_abstractStrategy == null) + { + return; + } + _abstractStrategy.MakeStep(); + Draw(); + if (_abstractStrategy.GetStatus() == Status.Finish) + { + comboBoxStrategy.Enabled = true; + _abstractStrategy = null; + } + + } } } \ No newline at end of file diff --git a/AntiAirCraftGun/AntiAirCraftGun/MovementStrategy/AbstractStrategy.cs b/AntiAirCraftGun/AntiAirCraftGun/MovementStrategy/AbstractStrategy.cs new file mode 100644 index 0000000..421bf13 --- /dev/null +++ b/AntiAirCraftGun/AntiAirCraftGun/MovementStrategy/AbstractStrategy.cs @@ -0,0 +1,132 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using static System.Windows.Forms.VisualStyles.VisualStyleElement; + +namespace AntiAircraftGun.MovementStrategy +{ + public abstract class AbstractStrategy + { + /// + /// Перемещаемый объект + /// + private IMoveableObject? _moveableObject; + /// + /// Статус перемещения + /// + private Status _state = Status.NotInit; + /// + /// Ширина поля + /// + protected int FieldWidth { get; private set; } + /// + /// Высота поля + /// + protected int FieldHeight { get; private set; } + /// + /// Статус перемещения + /// + public Status GetStatus() { return _state; } + /// + /// Установка данных + /// + /// Перемещаемый объект + /// Ширина поля + /// Высота поля + public void SetData(IMoveableObject moveableObject, int width, int + height) + { + if (moveableObject == null) + { + _state = Status.NotInit; + return; + } + _state = Status.InProgress; + _moveableObject = moveableObject; + FieldWidth = width; + FieldHeight = height; + } + /// + /// Шаг перемещения + /// + public void MakeStep() + { + if (_state != Status.InProgress) + { + return; + } + if (IsTargetDestinaion()) + { + _state = Status.Finish; + return; + } + MoveToTarget(); + } + /// + /// Перемещение влево + /// + /// Результат перемещения (true - удалось переместиться, false - неудача) + protected bool MoveLeft() => MoveTo(DirectionType.Left); + /// + /// Перемещение вправо + /// + /// Результат перемещения (true - удалось переместиться,false - неудача) + protected bool MoveRight() => MoveTo(DirectionType.Right); + /// + /// Перемещение вверх + /// + /// Результат перемещения (true - удалось переместиться, false - неудача) + protected bool MoveUp() => MoveTo(DirectionType.Up); + /// + /// Перемещение вниз + /// + /// Результат перемещения (true - удалось переместиться, false - неудача) + protected bool MoveDown() => MoveTo(DirectionType.Down); + /// + /// Параметры объекта + /// + protected ObjectParameters? GetObjectParameters => + _moveableObject?.GetObjectPosition; + /// + /// Шаг объекта + /// + /// + protected int? GetStep() + { + if (_state != Status.InProgress) + { + return null; + } + return _moveableObject?.GetStep; + } + /// + /// Перемещение к цели + /// + protected abstract void MoveToTarget(); + /// + /// Достигнута ли цель + /// + /// + protected abstract bool IsTargetDestinaion(); + /// + /// Попытка перемещения в требуемом направлении + /// + /// Направление + /// Результат попытки (true - удалось переместиться, false - неудача) + private bool MoveTo(DirectionType directionType) + { + if (_state != Status.InProgress) + { + return false; + } + if (_moveableObject?.CheckCanMove(directionType) ?? false) + { + _moveableObject.MoveObject(directionType); + return true; + } + return false; + } + } +} diff --git a/AntiAirCraftGun/AntiAirCraftGun/MovementStrategy/DrawingObjectAntiAirCraftGun.cs b/AntiAirCraftGun/AntiAirCraftGun/MovementStrategy/DrawingObjectAntiAirCraftGun.cs new file mode 100644 index 0000000..6a4948c --- /dev/null +++ b/AntiAirCraftGun/AntiAirCraftGun/MovementStrategy/DrawingObjectAntiAirCraftGun.cs @@ -0,0 +1,36 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using AntiAircraftGun.DrawingObjects; + +namespace AntiAircraftGun.MovementStrategy +{ + public class DrawingObjectAntiAirCraftGun : IMoveableObject + { + private readonly BaseDrawingAntiAirCraftGun? _drawningAntiAirCraftGun = null; + public DrawingObjectAntiAirCraftGun(BaseDrawingAntiAirCraftGun drawningCar) + { + _drawningAntiAirCraftGun = drawningCar; + } + public ObjectParameters? GetObjectPosition + { + get + { + if (_drawningAntiAirCraftGun == null || _drawningAntiAirCraftGun.AntiAirСraftGun == + null) + { + return null; + } + return new ObjectParameters(_drawningAntiAirCraftGun.GetPosX, + _drawningAntiAirCraftGun.GetPosY, _drawningAntiAirCraftGun.GetWidth, _drawningAntiAirCraftGun.GetHeight); + } + } + public int GetStep => (int)(_drawningAntiAirCraftGun?.AntiAirСraftGun?.Step ?? 0); + public bool CheckCanMove(DirectionType direction) => + _drawningAntiAirCraftGun?.CanMove(direction) ?? false; + public void MoveObject(DirectionType direction) => + _drawningAntiAirCraftGun?.MoveTransport(direction); + } +} diff --git a/AntiAirCraftGun/AntiAirCraftGun/MovementStrategy/IMoveableObject.cs b/AntiAirCraftGun/AntiAirCraftGun/MovementStrategy/IMoveableObject.cs new file mode 100644 index 0000000..3b96ec9 --- /dev/null +++ b/AntiAirCraftGun/AntiAirCraftGun/MovementStrategy/IMoveableObject.cs @@ -0,0 +1,31 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AntiAircraftGun.MovementStrategy +{ + public interface IMoveableObject + { + /// + /// Получение координаты X объекта + /// + ObjectParameters? GetObjectPosition { get; } + /// + /// Шаг объекта + /// + int GetStep { get; } + /// + /// Проверка, можно ли переместиться по нужному направлению + /// + /// + /// + bool CheckCanMove(DirectionType direction); + /// + /// Изменение направления пермещения объекта + /// + /// Направление + void MoveObject(DirectionType direction); + } +} diff --git a/AntiAirCraftGun/AntiAirCraftGun/MovementStrategy/MoveToBorder.cs b/AntiAirCraftGun/AntiAirCraftGun/MovementStrategy/MoveToBorder.cs new file mode 100644 index 0000000..6ce801f --- /dev/null +++ b/AntiAirCraftGun/AntiAirCraftGun/MovementStrategy/MoveToBorder.cs @@ -0,0 +1,59 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AntiAircraftGun.MovementStrategy +{ + public class MoveToBorder : AbstractStrategy + { + protected override bool IsTargetDestinaion() + { + var objParams = GetObjectParameters; + if (objParams == null) + { + return false; + } + return objParams.RightBorder <= FieldWidth && + objParams.RightBorder + GetStep() >= FieldWidth && + objParams.DownBorder <= FieldHeight && + objParams.DownBorder + GetStep() >= FieldHeight; + } + protected override void MoveToTarget() + { + var objParams = GetObjectParameters; + if (objParams == null) + { + return; + } + var diffX = objParams.RightBorder - FieldWidth; + if (Math.Abs(diffX) > GetStep()) + { + if (diffX > 0) + { + MoveLeft(); + } + else + { + MoveRight(); + } + } + var diffY = objParams.DownBorder - FieldHeight; + if (Math.Abs(diffY) > GetStep()) + { + if (diffY > 0) + { + MoveUp(); + } + else + { + MoveDown(); + } + } + } + } +} + + + diff --git a/AntiAirCraftGun/AntiAirCraftGun/MovementStrategy/MoveToCenter.cs b/AntiAirCraftGun/AntiAirCraftGun/MovementStrategy/MoveToCenter.cs new file mode 100644 index 0000000..1b16410 --- /dev/null +++ b/AntiAirCraftGun/AntiAirCraftGun/MovementStrategy/MoveToCenter.cs @@ -0,0 +1,56 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AntiAircraftGun.MovementStrategy +{ + public class MoveToCenter : AbstractStrategy + { + protected override bool IsTargetDestinaion() + { + var objParams = GetObjectParameters; + if (objParams == null) + { + return false; + } + return objParams.ObjectMiddleHorizontal <= FieldWidth / 2 && + objParams.ObjectMiddleHorizontal + GetStep() >= FieldWidth / 2 && + objParams.ObjectMiddleVertical <= FieldHeight / 2 && + objParams.ObjectMiddleVertical + GetStep() >= FieldHeight / 2; + } + protected override void MoveToTarget() + { + var objParams = GetObjectParameters; + if (objParams == null) + { + return; + } + var diffX = objParams.ObjectMiddleHorizontal - FieldWidth / 2; + if (Math.Abs(diffX) > GetStep()) + { + if (diffX > 0) + { + MoveLeft(); + } + else + { + MoveRight(); + } + } + var diffY = objParams.ObjectMiddleVertical - FieldHeight / 2; + if (Math.Abs(diffY) > GetStep()) + { + if (diffY > 0) + { + MoveUp(); + } + else + { + MoveDown(); + } + } + } + } +} diff --git a/AntiAirCraftGun/AntiAirCraftGun/MovementStrategy/ObjectParameters.cs b/AntiAirCraftGun/AntiAirCraftGun/MovementStrategy/ObjectParameters.cs new file mode 100644 index 0000000..2fca1cc --- /dev/null +++ b/AntiAirCraftGun/AntiAirCraftGun/MovementStrategy/ObjectParameters.cs @@ -0,0 +1,54 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AntiAircraftGun.MovementStrategy +{ + public class ObjectParameters + { + private readonly int _x; + 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/AntiAirCraftGun/AntiAirCraftGun/MovementStrategy/Status.cs b/AntiAirCraftGun/AntiAirCraftGun/MovementStrategy/Status.cs new file mode 100644 index 0000000..7c2fdbc --- /dev/null +++ b/AntiAirCraftGun/AntiAirCraftGun/MovementStrategy/Status.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AntiAircraftGun.MovementStrategy +{ + public enum Status + { + NotInit, + InProgress, + Finish + } +}