Сделанная 2 лаба

This commit is contained in:
Табеев Александр 2023-10-20 21:30:00 +04:00
parent 4f2298a184
commit 32e13d828f
14 changed files with 688 additions and 154 deletions

View File

@ -1,124 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AntiAircraftGun
{
public class DrawingAntiAircraftGun
{
public EntityAntiAircraftGun? EntityAntiAircraftGun { get; private set; }
private int pictureWidth; // ширина
private int pictureHeight; // высота
private int startPosX; // левая координата
private int startPosY; // верхняя координата
private readonly int zenitkaWidth = 185;
private readonly int zenitkaHeight = 128;
public bool Init(int speed, double weight, Color bodyColor, Color additionalColor, bool luke, bool zenitkaGun, bool radar, int width, int height)
{
if (width < zenitkaWidth) { return false; }
if (height < zenitkaHeight) { return false; }
pictureWidth = width;
pictureHeight = height;
EntityAntiAircraftGun = new EntityAntiAircraftGun();
EntityAntiAircraftGun.Init(speed, weight, bodyColor, additionalColor, luke, zenitkaGun, radar);
return true;
}
public void SetPosition(int x, int y)
{
if (x < 0) { x = 0; }
else if (x > pictureWidth) { x = pictureWidth; }
if (y < 0) { y = 0; }
else if (y > pictureHeight) { y = pictureHeight; }
startPosX = x;
startPosY = y;
}
public void MoveTransport(DirectionType direction)
{
if (EntityAntiAircraftGun == null)
{
return;
}
switch (direction)
{
case DirectionType.Left:
if (startPosX - EntityAntiAircraftGun.Step > 0)
{
startPosX -= (int)EntityAntiAircraftGun.Step;
}
break;
case DirectionType.Up:
if (startPosY - EntityAntiAircraftGun.Step > 0)
{
startPosY -= (int)EntityAntiAircraftGun.Step;
}
break;
case DirectionType.Right:
if (startPosX + zenitkaWidth + EntityAntiAircraftGun.Step < pictureWidth)
{
startPosX += (int)EntityAntiAircraftGun.Step;
}
break;
case DirectionType.Down:
if (startPosY + zenitkaHeight + EntityAntiAircraftGun.Step < pictureHeight)
{
startPosY += (int)EntityAntiAircraftGun.Step;
}
break;
}
}
public void DrawZenitka(Graphics g)
{
if (EntityAntiAircraftGun == null)
{
return;
}
Pen pen = new(Color.Black);
pen.Width = 2;
Brush additionalBrush = new SolidBrush(EntityAntiAircraftGun.AdditionalColor);
// Гусеница
g.DrawEllipse(pen, startPosX, startPosY + 103, 150, 25);
g.DrawEllipse(pen, startPosX + 5, startPosY + 110, 10, 10);
g.DrawEllipse(pen, startPosX + 18, startPosY + 108, 15, 15);
g.DrawEllipse(pen, startPosX + 36, startPosY + 107, 17, 17);
g.DrawEllipse(pen, startPosX + 56, startPosY + 107, 17, 17);
g.DrawEllipse(pen, startPosX + 76, startPosY + 107, 17, 17);
g.DrawEllipse(pen, startPosX + 96, startPosY + 107, 17, 17);
g.DrawEllipse(pen, startPosX + 116, startPosY + 108, 15, 15);
g.DrawEllipse(pen, startPosX + 136, startPosY + 110, 10, 10);
// Корпус зенитки
Brush brGreen = new SolidBrush(Color.Green);
g.FillRectangle(brGreen, startPosX + 20, startPosY + 78, 110, 25);
// Башня зенитки
g.FillRectangle(additionalBrush, startPosX + 45, startPosY + 53, 60, 25);
// Два орудия у зенитки
if (EntityAntiAircraftGun.ZenitkaGun)
{
Pen penTolst = new(Color.Black);
penTolst.Width = 5;
g.DrawLine(penTolst, startPosX + 105, startPosY + 63, startPosX + 175, startPosY + 13);
g.DrawLine(penTolst, startPosX + 105, startPosY + 73, startPosX + 185, startPosY + 18);
}
// Лючек зенитки
if (EntityAntiAircraftGun.Luke)
{
Brush brBlack = new SolidBrush(Color.Black);
g.FillRectangle(brBlack, startPosX + 80, startPosY + 43, 25, 10);
}
// Радар зенитки
if (EntityAntiAircraftGun.Radar)
{
g.DrawLine(pen, startPosX + 35, startPosY + 78, startPosX + 35, startPosY + 26);
g.DrawLine(pen, startPosX + 60, startPosY + 53, startPosX + 38, startPosY + 23);
g.DrawLine(pen, startPosX + 18, startPosY + 31, startPosX + 52, startPosY + 18);
g.DrawLine(pen, startPosX + 18, startPosY + 31, startPosX, startPosY + 18);
g.DrawLine(pen, startPosX + 52, startPosY + 18, startPosX + 55, startPosY -2);
}
}
}
}

View File

@ -0,0 +1,57 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AntiAircraftGun.Entities;
namespace AntiAircraftGun.DrawningObjects
{
public class DrawingAntiAircraftGun : DrawningAircraftGun
{
// Конструктор
public DrawingAntiAircraftGun(int speed, double weight, Color bodyColor, Color additionalColor, bool luke, bool zenitkaGun, bool radar, int width, int height) :
base(speed, weight, bodyColor, width, height, 185, 128)
{
if (EntityAircraftGun != null)
{
EntityAircraftGun = new EntityAntiAircraftGun(speed, weight, bodyColor, additionalColor, luke, zenitkaGun, radar);
}
}
public override void DrawZenitka(Graphics g)
{
if (EntityAircraftGun is not EntityAntiAircraftGun aircraftGun)
{
return;
}
Pen pen = new(Color.Black);
pen.Width = 2;
Brush additionalBrush = new SolidBrush(aircraftGun.AdditionalColor);
// Два орудия у зенитки
if (aircraftGun.ZenitkaGun)
{
Pen penTolst = new(Color.Black);
penTolst.Width = 5;
g.DrawLine(penTolst, startPosX + 105, startPosY + 63, startPosX + 175, startPosY + 13);
g.DrawLine(penTolst, startPosX + 105, startPosY + 73, startPosX + 185, startPosY + 18);
}
base.DrawZenitka(g);
// Лючек зенитки
if (aircraftGun.Luke)
{
Brush brBlack = new SolidBrush(Color.Black);
g.FillRectangle(brBlack, startPosX + 80, startPosY + 43, 25, 10);
}
// Радар зенитки
if (aircraftGun.Radar)
{
g.DrawLine(pen, startPosX + 35, startPosY + 78, startPosX + 35, startPosY + 26);
g.DrawLine(pen, startPosX + 60, startPosY + 53, startPosX + 38, startPosY + 23);
g.DrawLine(pen, startPosX + 18, startPosY + 31, startPosX + 52, startPosY + 18);
g.DrawLine(pen, startPosX + 18, startPosY + 31, startPosX, startPosY + 18);
g.DrawLine(pen, startPosX + 52, startPosY + 18, startPosX + 55, startPosY - 2);
}
}
}
}

View File

@ -0,0 +1,148 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AntiAircraftGun.Entities;
namespace AntiAircraftGun.DrawningObjects
{
// Класс, отвечающий за прорисовку и перемещение объекта-сущности
public class DrawningAircraftGun
{
// Класс-сущность
public EntityAircraftGun? EntityAircraftGun { get; protected set; }
// Ширина окна
private int pictureWidth;
// Высота окна
private int pictureHeight;
// Левая координата прорисовки зенитки
protected int startPosX;
// Верхняя координата прорисовки зенитки
protected int startPosY;
// Ширина прорисоки зенитки
protected readonly int _zenitkaWidth = 185;
// Высота прорисовки зенитки
protected readonly int _zenitkaHeight = 128;
// Координата Х объекта
public int GetPosX => startPosX;
// Координата Y объекта
public int GetPosY => startPosY;
// Ширина объекта
public int GetWidth => _zenitkaWidth;
// Высота объекта
public int GetHeight => _zenitkaHeight;
// Конструктор
public DrawningAircraftGun(int speed, double weight, Color bodyColor, int width, int height)
{
if (width < _zenitkaWidth) { return; }
if (height < _zenitkaHeight) { return; }
pictureWidth = width;
pictureHeight = height;
EntityAircraftGun = new EntityAircraftGun(speed, weight, bodyColor);
}
// Конструктор
protected DrawningAircraftGun(int speed, double weight, Color bodyColor, int width, int height, int zenitkaWidth, int zenitkaHeight)
{
pictureWidth = width;
pictureHeight = height;
_zenitkaWidth = zenitkaWidth;
_zenitkaHeight = zenitkaHeight;
EntityAircraftGun = new EntityAircraftGun(speed, weight, bodyColor);
}
// Установка позиции
public void SetPosition(int x, int y)
{
if (x < 0) { x = 0; }
else if (x > pictureWidth) { x = pictureWidth; }
if (y < 0) { y = 0; }
else if (y > pictureHeight) { y = pictureHeight; }
startPosX = x;
startPosY = y;
}
// Проверка, что объект может переместиться по указанному направлению
public bool CanMove(DirectionType direction)
{
if (EntityAircraftGun == null)
{
return false;
}
return direction switch
{
// влево
DirectionType.Left => startPosX - EntityAircraftGun.Step > 0,
// вверх
DirectionType.Up => startPosY - EntityAircraftGun.Step > 0,
// вправо
DirectionType.Right => startPosX + EntityAircraftGun.Step + _zenitkaWidth < pictureWidth,
// вниз
DirectionType.Down => startPosY + EntityAircraftGun.Step + _zenitkaHeight < pictureHeight,
};
}
// Изменение направления перемещения
public void MoveTransport(DirectionType direction)
{
if (!CanMove(direction) || EntityAircraftGun == null)
{
return;
}
switch (direction)
{
// влево
case DirectionType.Left:
startPosX -= (int)EntityAircraftGun.Step;
break;
// вверх
case DirectionType.Up:
startPosY -= (int)EntityAircraftGun.Step;
break;
// вправо
case DirectionType.Right:
startPosX += (int)EntityAircraftGun.Step;
break;
// вниз
case DirectionType.Down:
startPosY += (int)EntityAircraftGun.Step;
break;
}
}
// Прорисовка объекта
public virtual void DrawZenitka(Graphics g)
{
if (EntityAircraftGun == null)
{
return;
}
Pen pen = new(Color.Black);
pen.Width = 2;
Brush additionalBrush = new SolidBrush(EntityAircraftGun.BodyColor);
// Гусеница
g.DrawEllipse(pen, startPosX, startPosY + 103, 150, 25);
g.DrawEllipse(pen, startPosX + 5, startPosY + 110, 10, 10);
g.DrawEllipse(pen, startPosX + 18, startPosY + 108, 15, 15);
g.DrawEllipse(pen, startPosX + 36, startPosY + 107, 17, 17);
g.DrawEllipse(pen, startPosX + 56, startPosY + 107, 17, 17);
g.DrawEllipse(pen, startPosX + 76, startPosY + 107, 17, 17);
g.DrawEllipse(pen, startPosX + 96, startPosY + 107, 17, 17);
g.DrawEllipse(pen, startPosX + 116, startPosY + 108, 15, 15);
g.DrawEllipse(pen, startPosX + 136, startPosY + 110, 10, 10);
// Корпус зенитки
Brush brGreen = new SolidBrush(Color.Green);
g.FillRectangle(brGreen, startPosX + 20, startPosY + 78, 110, 25);
// Башня зенитки
g.FillRectangle(additionalBrush, startPosX + 45, startPosY + 53, 60, 25);
}
}
}

View File

@ -0,0 +1,32 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AntiAircraftGun.Entities
{
// Класс-сущность "Зенитка"
public class EntityAircraftGun
{
// Скорость
public int Speed { get; private set; }
// Вес
public double Weight { get; private set; }
// Основной цвет
public Color BodyColor { get; private set; }
// Шаг перемещения зенитки
public double Step => (double)Speed * 100 / Weight;
// Конструктор с параметрами
public EntityAircraftGun(int speed, double weight, Color bodyColor)
{
Speed = speed;
Weight = weight;
BodyColor = bodyColor;
}
}
}

View File

@ -4,16 +4,11 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AntiAircraftGun
namespace AntiAircraftGun.Entities
{
public class EntityAntiAircraftGun
// Класс-сущность "Стреляющая зенитка"
public class EntityAntiAircraftGun : EntityAircraftGun
{
// Скорость
public int Speed { get; private set; }
// Вес
public double Weight { get; private set; }
// Главный цвет
public Color BodyColor { get; private set; }
// Доп. цвет
public Color AdditionalColor { get; private set; }
// Признак наличия люка
@ -22,14 +17,11 @@ namespace AntiAircraftGun
public bool ZenitkaGun { get; private set; }
// признак наличия радара
public bool Radar { get; private set; }
// Шаг перемещения зенитки
public double Step => (double)Speed * 100 / Weight;
public void Init(int speed, double weight, Color bodyColor, Color additionalColor,bool luke, bool zenitkaGun, bool radar)
// Инициализация полей объекта-класса продвинутой зенитки
public EntityAntiAircraftGun(int speed, double weight, Color bodyColor, Color additionalColor, bool luke, bool zenitkaGun, bool radar) :
base(speed, weight, bodyColor)
{
Speed = speed;
Weight = weight;
BodyColor = bodyColor;
AdditionalColor = additionalColor;
Luke = luke;
ZenitkaGun = zenitkaGun;

View File

@ -35,6 +35,9 @@
this.buttonRight = new System.Windows.Forms.Button();
this.buttonCreateAntiAircraftGun = new System.Windows.Forms.Button();
this.pictureBoxAntiAircraftGun = new System.Windows.Forms.PictureBox();
this.comboBoxStrategy = new System.Windows.Forms.ComboBox();
this.ButtonCreateAntiAircraftGun_Click = new System.Windows.Forms.Button();
this.ButtonStep_Click = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.pictureBoxAntiAircraftGun)).BeginInit();
this.SuspendLayout();
@ -99,11 +102,11 @@
// buttonCreateAntiAircraftGun
//
this.buttonCreateAntiAircraftGun.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.buttonCreateAntiAircraftGun.Location = new System.Drawing.Point(12, 406);
this.buttonCreateAntiAircraftGun.Location = new System.Drawing.Point(148, 406);
this.buttonCreateAntiAircraftGun.Name = "buttonCreateAntiAircraftGun";
this.buttonCreateAntiAircraftGun.Size = new System.Drawing.Size(86, 43);
this.buttonCreateAntiAircraftGun.Size = new System.Drawing.Size(186, 43);
this.buttonCreateAntiAircraftGun.TabIndex = 7;
this.buttonCreateAntiAircraftGun.Text = "Создать";
this.buttonCreateAntiAircraftGun.Text = "Создать стреляющую зенитку";
this.buttonCreateAntiAircraftGun.UseVisualStyleBackColor = true;
this.buttonCreateAntiAircraftGun.Click += new System.EventHandler(this.buttonCreate_Click);
//
@ -117,11 +120,47 @@
this.pictureBoxAntiAircraftGun.TabIndex = 6;
this.pictureBoxAntiAircraftGun.TabStop = false;
//
// 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(633, 12);
this.comboBoxStrategy.Name = "comboBoxStrategy";
this.comboBoxStrategy.Size = new System.Drawing.Size(239, 23);
this.comboBoxStrategy.TabIndex = 12;
//
// ButtonCreateAntiAircraftGun_Click
//
this.ButtonCreateAntiAircraftGun_Click.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.ButtonCreateAntiAircraftGun_Click.Location = new System.Drawing.Point(12, 406);
this.ButtonCreateAntiAircraftGun_Click.Name = "ButtonCreateAntiAircraftGun_Click";
this.ButtonCreateAntiAircraftGun_Click.Size = new System.Drawing.Size(111, 43);
this.ButtonCreateAntiAircraftGun_Click.TabIndex = 13;
this.ButtonCreateAntiAircraftGun_Click.Text = "Создать зенитку";
this.ButtonCreateAntiAircraftGun_Click.UseVisualStyleBackColor = true;
this.ButtonCreateAntiAircraftGun_Click.Click += new System.EventHandler(this.ButtonCreateAntiAircraftGun_Click_Click);
//
// ButtonStep_Click
//
this.ButtonStep_Click.Location = new System.Drawing.Point(812, 50);
this.ButtonStep_Click.Name = "ButtonStep_Click";
this.ButtonStep_Click.Size = new System.Drawing.Size(55, 42);
this.ButtonStep_Click.TabIndex = 14;
this.ButtonStep_Click.Text = "Шаг";
this.ButtonStep_Click.UseVisualStyleBackColor = true;
this.ButtonStep_Click.Click += new System.EventHandler(this.ButtonStep_Click_Click);
//
// FormAntiAircraftGun
//
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(884, 461);
this.Controls.Add(this.ButtonStep_Click);
this.Controls.Add(this.ButtonCreateAntiAircraftGun_Click);
this.Controls.Add(this.comboBoxStrategy);
this.Controls.Add(this.buttonUp);
this.Controls.Add(this.buttonLeft);
this.Controls.Add(this.buttonDown);
@ -147,5 +186,8 @@
private Button buttonRight;
private Button buttonCreateAntiAircraftGun;
private PictureBox pictureBoxAntiAircraftGun;
private ComboBox comboBoxStrategy;
private Button ButtonCreateAntiAircraftGun_Click;
private Button ButtonStep_Click;
}
}

View File

@ -1,8 +1,13 @@
using AntiAircraftGun.DrawningObjects;
using AntiAircraftGun.MovementStrategy;
namespace AntiAircraftGun
{
public partial class FormAntiAircraftGun : Form
{
private DrawingAntiAircraftGun? drawingAntiAircraftGun;
private DrawningAircraftGun? _drawningAircraftGun;
private AbstractStrategy? abstractStrategy;
public FormAntiAircraftGun()
{
InitializeComponent();
@ -10,28 +15,38 @@ namespace AntiAircraftGun
private void Draw()
{
if (drawingAntiAircraftGun == null)
if (_drawningAircraftGun == null)
{
return;
}
Bitmap bmp = new(pictureBoxAntiAircraftGun.Width, pictureBoxAntiAircraftGun.Height);
Graphics gr = Graphics.FromImage(bmp);
drawingAntiAircraftGun.DrawZenitka(gr);
_drawningAircraftGun.DrawZenitka(gr);
pictureBoxAntiAircraftGun.Image = bmp;
}
// Îáðàáîòêà íàæàòèÿ êíîïêè "Ñîçäàòü ñòðåëÿþùóþ çåíèòêó"
private void buttonCreate_Click(object sender, EventArgs e)
{
Random random = new();
drawingAntiAircraftGun = new DrawingAntiAircraftGun();
drawingAntiAircraftGun.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)), pictureBoxAntiAircraftGun.Width, pictureBoxAntiAircraftGun.Height);
drawingAntiAircraftGun.SetPosition(random.Next(10, 100), random.Next(10, 100));
_drawningAircraftGun = new DrawingAntiAircraftGun(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)), pictureBoxAntiAircraftGun.Width, pictureBoxAntiAircraftGun.Height);
_drawningAircraftGun.SetPosition(random.Next(10, 100), random.Next(10, 100));
Draw();
}
// Îáðàáîòêà íàæàòèÿ êíîïêè "Ñîçäàòü çåíèòêó"
private void ButtonCreateAntiAircraftGun_Click_Click(object sender, EventArgs e)
{
Random random = new();
_drawningAircraftGun = new DrawningAircraftGun(random.Next(100, 300), random.Next(1000, 3000), Color.FromArgb(random.Next(0, 256), random.Next(0, 256),
random.Next(0, 256)), pictureBoxAntiAircraftGun.Width, pictureBoxAntiAircraftGun.Height);
_drawningAircraftGun.SetPosition(random.Next(10, 100), random.Next(10, 100));
Draw();
}
private void buttonMove_Click(object sender, EventArgs e)
{
if (drawingAntiAircraftGun == null)
if (_drawningAircraftGun == null)
{
return;
}
@ -39,19 +54,57 @@ namespace AntiAircraftGun
switch (name)
{
case "buttonUp":
drawingAntiAircraftGun.MoveTransport(DirectionType.Up);
_drawningAircraftGun.MoveTransport(DirectionType.Up);
break;
case "buttonDown":
drawingAntiAircraftGun.MoveTransport(DirectionType.Down);
_drawningAircraftGun.MoveTransport(DirectionType.Down);
break;
case "buttonLeft":
drawingAntiAircraftGun.MoveTransport(DirectionType.Left);
_drawningAircraftGun.MoveTransport(DirectionType.Left);
break;
case "buttonRight":
drawingAntiAircraftGun.MoveTransport(DirectionType.Right);
_drawningAircraftGun.MoveTransport(DirectionType.Right);
break;
}
Draw();
}
// Îáðàáîòêà íàæàòèÿ êíîïêè "Øàã"
private void ButtonStep_Click_Click(object sender, EventArgs e)
{
if (_drawningAircraftGun == null)
{
return;
}
if (comboBoxStrategy.Enabled)
{
abstractStrategy = comboBoxStrategy.SelectedIndex
switch
{
0 => new MoveToCenter(),
1 => new MoveToBorder(),
_ => null,
};
if (abstractStrategy == null)
{
return;
}
abstractStrategy.SetData(new DrawningObjectAircraftGun(_drawningAircraftGun), 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,106 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AntiAircraftGun.DrawningObjects;
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;
}
}
}

View File

@ -0,0 +1,39 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AntiAircraftGun.DrawningObjects;
namespace AntiAircraftGun.MovementStrategy
{
// Реализация интерфейса IDrawningObject для работы с объектом DrawningCar (паттерн Adapter)
public class DrawningObjectAircraftGun : IMoveableObject
{
private readonly DrawningAircraftGun? _drawningAircraftGun = null;
public DrawningObjectAircraftGun(DrawningAircraftGun drawningAircraftGun)
{
_drawningAircraftGun = drawningAircraftGun;
}
public ObjectParameters? GetObjectPosition
{
get
{
if (_drawningAircraftGun == null || _drawningAircraftGun.EntityAircraftGun == null)
{
return null;
}
return new ObjectParameters(_drawningAircraftGun.GetPosX, _drawningAircraftGun.GetPosY, _drawningAircraftGun.GetWidth, _drawningAircraftGun.GetHeight);
}
}
public int GetStep => (int)(_drawningAircraftGun?.EntityAircraftGun?.Step ?? 0);
public bool CheckCanMove(DirectionType direction) => _drawningAircraftGun?.CanMove(direction) ?? false;
public void MoveObject(DirectionType direction) => _drawningAircraftGun?.MoveTransport(direction);
}
}

View File

@ -0,0 +1,25 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AntiAircraftGun.DrawningObjects;
namespace AntiAircraftGun.MovementStrategy
{
// Интерфейс для работы с перемещаемым объектом
public interface IMoveableObject
{
// Поулчение координаты X объекта
ObjectParameters? GetObjectPosition { get; }
// Шаг объекта
int GetStep { get; }
// Првоерка, можно ли переместиться по нужному направлению
bool CheckCanMove(DirectionType direction);
// Изменение направления перемещения объекта
void MoveObject(DirectionType direction);
}
}

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 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.ObjectMiddleHorizontal - FieldWidth;
if (Math.Abs(diffX) > GetStep())
{
if (diffX > 0)
{
MoveLeft();
}
else
{
MoveRight();
}
}
var diffY = objParams.ObjectMiddleVertical - FieldHeight;
if (Math.Abs(diffY) > GetStep())
{
if (diffY > 0)
{
MoveUp();
}
else
{
MoveDown();
}
}
}
}
}

View File

@ -0,0 +1,55 @@
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,39 @@
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;
//Конструктор
public ObjectParameters(int x, int y, int width, int height)
{
_x = x;
_y = y;
_width = width;
_height = height;
}
}
}

View File

@ -0,0 +1,16 @@
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
}
}