ПИбд-23 Тихоненков А.Е. Лабораторная 2 #3

Closed
YourDax wants to merge 5 commits from Lab2 into Lab1
14 changed files with 789 additions and 278 deletions

View File

@ -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];
/// <summary>
/// Класс-сущность
/// </summary>
public EntityAntiAircraftGun? AntiAircraftGun { get; private set; }
/// <summary>
/// Ширина окна
/// </summary>
private int _pictureWidth;
/// <summary>
/// Высота окна
/// </summary>
private int _pictureHeight;
/// <summary>
/// Левая координата прорисовки автомобиля
/// </summary>
private int _startPosX;
/// <summary>
/// Верхняя кооридната прорисовки автомобиля
/// </summary>
private int _startPosY;
/// <summary>
/// Ширина прорисовки автомобиля
/// </summary>
private readonly int _zenitWidth = 110;
/// <summary>
/// Высота прорисовки автомобиля
/// </summary>
private readonly int _zenitHeight = 60;
/// <summary>
/// Инициализация свойств
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес</param>
/// <param name="bodyColor">Цвет кузова</param>
/// <param name="width">Ширина картинки</param>
/// <param name="height">Высота картинки</param>
/// <returns>true - объект создан, false - проверка не пройдена,
///нельзя создать объект в этих размерах</returns>
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;
}
/// <summary>
/// Установка позиции
/// </summary>
/// <param name="x">Координата X</param>
/// <param name="y">Координата Y</param>
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;
}
}
/// <summary>
/// Изменение направления перемещения
/// </summary>
/// <param name="direction">Направление</param>
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;
}
}
/// <summary>
/// Прорисовка объекта
/// </summary>
/// <param name="g"></param>
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);
}
}
}
}

View File

@ -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);
}
}
}
}

View File

@ -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;
}
}
/// <summary>
/// Координата X объекта
/// </summary>
public int GetPosX => _startPosX;
/// <summary>
/// Координата Y объекта
/// </summary>
public int GetPosY => _startPosY;
/// <summary>
/// Ширина объекта
/// </summary>
public int GetWidth => _zenitWidth;
/// <summary>
/// Высота объекта
/// </summary>
public int GetHeight => _zenitHeight;
/// <summary>
/// Проверка, что объект может переместится по указанному направлению
/// </summary>
/// <param name="direction">Направление</param>
/// <returns>true - можно переместится по указанному направлению</returns>
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,
};
}
/// <summary>
/// Изменение направления перемещения
/// </summary>
/// <param name="direction">Направление</param>
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);
}
}
}
}

View File

@ -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
Outdated
Review

Имя класса не соответствует указанному в задании

Имя класса не соответствует указанному в задании
{
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;
}
}
}

View File

@ -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
{
/// <summary>
/// Скорость
/// </summary>
public int Speed { get; private set; }
/// Скорость
/// </summary>
public int Speed { get; private set; }
/// <summary>
/// Вес
/// </summary>
@ -21,7 +21,7 @@ namespace AntiAircraftGun
/// </summary>
public Color BodyColor { get; private set; }
/// <summary>
/// Дополнительный цвет (для опциональных элементов)
/// Шаг перемещения автомобиля
/// </summary>
public Color AdditionalColor { get; private set; }
/// <summary>
@ -29,33 +29,17 @@ namespace AntiAircraftGun
/// </summary>
public double Step => (double)Speed * 100 / Weight;
/// <summary>
/// Цвет для доп. деталей
/// </summary>
public Color DopColor { get; private set; }
/// <summary>
/// Ракета
/// </summary>
public bool Rocket { get; private set; }
/// <summary>
/// Радар
/// </summary>
public bool Radar { get; private set; }
/// <summary>
/// Инициализация полей объекта-класса спортивного автомобиля
/// Конструктор с параметрами
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес автомобиля</param>
/// <param name="bodyColor">Основной цвет</param>
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;
}
}
}

View File

@ -2,38 +2,41 @@
{
partial class FormAntiAirCraftGun
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
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
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
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;
}
}

View File

@ -1,29 +1,18 @@
using AntiAircraftGun.DrawingObjects;
using AntiAircraftGun.MovementStrategy;
namespace AntiAircraftGun
{
public partial class FormAntiAirCraftGun : Form
{
/// <summary>
/// Ïîëå-îáúåêò äëÿ ïðîðèñîâêè îáúåêòà
/// </summary>
private DrawingAntiAirCraftGun? _drawingAntiAirCraftGun;
public FormAntiAirCraftGun()
{
InitializeComponent();
}
/// <summary>
/// Ìåòîä ïðîðèñîâêè ìàøèíû
/// </summary>
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;
}
}
}
}

View File

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

View File

@ -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);
}
}

View File

@ -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
{
/// <summary>
/// Получение координаты X объекта
/// </summary>
ObjectParameters? GetObjectPosition { get; }
/// <summary>
/// Шаг объекта
/// </summary>
int GetStep { get; }
/// <summary>
/// Проверка, можно ли переместиться по нужному направлению
/// </summary>
/// <param name="direction"></param>
/// <returns></returns>
bool CheckCanMove(DirectionType direction);
/// <summary>
/// Изменение направления пермещения объекта
/// </summary>
/// <param name="direction">Направление</param>
void MoveObject(DirectionType direction);
}
}

View File

@ -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();
}
}
}
}
}

View File

@ -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();
}
}
}
}
}

View File

@ -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;
/// <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,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
}
}