From 6c391ce4f7f24595c2128e57011f740a74641723 Mon Sep 17 00:00:00 2001
From: GokaPek <109132407+GokaPek@users.noreply.github.com>
Date: Sat, 7 Oct 2023 17:44:27 +0400
Subject: [PATCH 1/2] =?UTF-8?q?=D0=A1=D0=BE=D0=B7=D0=B4=D0=B0=D0=BD=D0=B8?=
=?UTF-8?q?=D0=B5=20=D0=BF=D0=BE=D1=82=D0=BE=D0=BC=D0=BA=D0=BE=D0=B2?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../SelfPropelledArtilleryUnit/DrawingSPAU.cs | 34 ++++++++-----------
.../DrawingSPAUchild.cs | 33 ++++++++++++++++++
.../SelfPropelledArtilleryUnit/EntitySPAU.cs | 19 ++---------
.../EntitySPAUchild.cs | 27 +++++++++++++++
.../SelfPropelledArtilleryUnit/Form.cs | 3 +-
5 files changed, 77 insertions(+), 39 deletions(-)
create mode 100644 SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/DrawingSPAUchild.cs
create mode 100644 SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/EntitySPAUchild.cs
diff --git a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/DrawingSPAU.cs b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/DrawingSPAU.cs
index 9b74014..2bcd6ce 100644
--- a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/DrawingSPAU.cs
+++ b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/DrawingSPAU.cs
@@ -27,11 +27,11 @@ namespace SelfPropelledArtilleryUnit
///
/// Левая координата прорисовки автомобиля
///
- private int _startPosX;
+ protected int _startPosX;
///
/// Верхняя кооридната прорисовки автомобиля
///
- private int _startPosY;
+ protected int _startPosY;
///
/// Ширина прорисовки автомобиля
///
@@ -46,28 +46,22 @@ namespace SelfPropelledArtilleryUnit
/// Скорость
/// Вес
/// Цвет кузова
- /// Дополнительный цвет
- /// Признак наличия залповой установки
/// Ширина картинки
/// Высота картинки
/// true - объект создан, false - проверка не пройдена,нельзя создать объект в этих размерах
- public bool Init(int speed, double weight, Color bodyColor, Color
- additionalColor, bool bodyKit, int width, int height)
+ public DrawingSPAU(int speed, double weight, Color bodyColor, int width, int height)
{
_pictureWidth = width;
_pictureHeight = height;
if (_carHeight >= height)
{
- return false;
+ return;
}
if (_carWidth >= width)
{
- return false;
+ return;
}
- EntitySPAU = new EntitySPAU();
- EntitySPAU.Init(speed, weight, bodyColor, additionalColor,
- bodyKit);
- return true;
+ EntitySPAU = new EntitySPAU(speed, weight, bodyColor);
}
///
/// Установка позиции
@@ -132,6 +126,11 @@ namespace SelfPropelledArtilleryUnit
///
/// Прорисовка объекта
///
+ public virtual void DrawingZU(Graphics g)
+ {}
+ ///
+ /// Прорисовка объекта
+ ///
///
public void DrawTransport(Graphics g)
{
@@ -140,14 +139,9 @@ namespace SelfPropelledArtilleryUnit
return;
}
Pen penBlack = new(Color.Black);
- Brush additionalBrush = new SolidBrush(EntitySPAU.AdditionalColor);
- // обвесы
- if (EntitySPAU.BodyKit)
- {
- //залповая усутановка
- g.FillRectangle(additionalBrush, _startPosX + 15, _startPosY + 20, 20, 40);
- g.DrawLine(penBlack, _startPosX + 5, _startPosY + 20, _startPosX + 15, _startPosY + 25);
- }
+
+ DrawingZU(g);
+
//гусеницы
Brush brBlack = new SolidBrush(Color.Black);
Brush brBody = new SolidBrush(EntitySPAU.BodyColor);
diff --git a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/DrawingSPAUchild.cs b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/DrawingSPAUchild.cs
new file mode 100644
index 0000000..dcc0142
--- /dev/null
+++ b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/DrawingSPAUchild.cs
@@ -0,0 +1,33 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace SelfPropelledArtilleryUnit
+{
+ public class DrawingSPAUchild : DrawingSPAU
+ {
+ public new EntitySPAUchild EntitySPAU;
+ /// Дополнительный цвет
+ /// Признак наличия залповой установки
+
+ public override void DrawingZU(Graphics g)
+ {
+ base.DrawingZU(g);
+ Pen penBlack = new Pen(Color.Black);
+ Brush additionalBrush = new SolidBrush(EntitySPAU.AdditionalColor);
+ // обвесы
+ if (EntitySPAU.BodyKit)
+ {
+ //залповая усутановка
+ g.FillRectangle(additionalBrush, _startPosX + 15, _startPosY + 20, 20, 40);
+ g.DrawLine(penBlack, _startPosX + 5, _startPosY + 20, _startPosX + 15, _startPosY + 25);
+ }
+ }
+ public DrawingSPAUchild(int speed, double weight, Color bodyColor, Color additionalColor, bool bodyKit, int width, int height) : base(speed, weight, bodyColor, width, height)
+ {
+ EntitySPAU = new EntitySPAUchild(speed, weight, bodyColor, additionalColor, bodyKit);
+ }
+ }
+}
diff --git a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/EntitySPAU.cs b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/EntitySPAU.cs
index 97c239b..7723ea1 100644
--- a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/EntitySPAU.cs
+++ b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/EntitySPAU.cs
@@ -2,6 +2,7 @@
{
public class EntitySPAU
{
+
///
/// Скорость
///
@@ -15,14 +16,6 @@
///
public Color BodyColor { get; private set; }
///
- /// Дополнительный цвет (для опциональных элементов)
- ///
- public Color AdditionalColor { get; private set; }
- ///
- /// Признак (опция) наличия залповой установки
- ///
- public bool BodyKit { get; private set; }
- ///
/// Шаг перемещения САУ
///
public double Step => (double)Speed * 100 / Weight;
@@ -32,18 +25,10 @@
/// Скорость
/// Вес автомобиля
/// Основной цвет
- /// Дополнительный цвет
- /// Признак наличия обвеса
- /// Признак наличия антикрыла
- /// Признак наличия гоночной полосы
- public void Init(int speed, double weight, Color bodyColor, Color
- additionalColor, bool bodyKit)
- {
+ public EntitySPAU(int speed, double weight, Color bodyColor) {
Speed = speed;
Weight = weight;
BodyColor = bodyColor;
- AdditionalColor = additionalColor;
- BodyKit = bodyKit;
}
}
}
diff --git a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/EntitySPAUchild.cs b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/EntitySPAUchild.cs
new file mode 100644
index 0000000..f86eda6
--- /dev/null
+++ b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/EntitySPAUchild.cs
@@ -0,0 +1,27 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace SelfPropelledArtilleryUnit
+{
+ public class EntitySPAUchild : EntitySPAU
+ {
+ ///
+ /// Дополнительный цвет (для опциональных элементов)
+ ///
+ public Color AdditionalColor { get; private set; }
+ ///
+ /// Признак (опция) наличия залповой установки
+ ///
+ public bool BodyKit { get; private set; }
+ /// Дополнительный цвет
+ /// Признак наличия залповой установки
+ public EntitySPAUchild(int speed, double weight, Color bodyColor, Color additionalColor, bool bodyKit) : base(speed, weight, bodyColor)
+ {
+ AdditionalColor = additionalColor;
+ BodyKit = bodyKit;
+ }
+ }
+}
diff --git a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/Form.cs b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/Form.cs
index 935f790..23d8440 100644
--- a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/Form.cs
+++ b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/Form.cs
@@ -38,8 +38,7 @@ namespace SelfPropelledArtilleryUnit
private void buttonCreate_Click(object sender, EventArgs e)
{
Random random = new();
- _drawningSPAU = new DrawingSPAU();
- _drawningSPAU.Init(random.Next(100, 300),
+ _drawningSPAU = new DrawingSPAUchild(random.Next(100, 300),
random.Next(1000, 3000),
Color.FromArgb(random.Next(0, 256), random.Next(0, 256),
random.Next(0, 256)),
--
2.25.1
From 60f2e38cf7d9c04bf5e47ffa2d52e943fc5ebc77 Mon Sep 17 00:00:00 2001
From: GokaPek <109132407+GokaPek@users.noreply.github.com>
Date: Mon, 9 Oct 2023 20:56:25 +0400
Subject: [PATCH 2/2] =?UTF-8?q?=D0=9D=D0=B0=D1=81=D0=BB=D0=B5=D0=B4=D0=BE?=
=?UTF-8?q?=D0=B2=D0=B0=D0=BD=D0=B8=D0=B5?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../AbstractStrategy.cs | 133 ++++++++++++++++++
.../DrawingSPAUchild.cs | 33 -----
.../DrawningObjectSPAU.cs | 39 +++++
.../{DrawingSPAU.cs => DrawningSPAU.cs} | 93 ++++++++----
.../DrawningSPAUchild.cs | 40 ++++++
.../SelfPropelledArtilleryUnit/EntitySPAU.cs | 2 +-
.../EntitySPAUchild.cs | 2 +-
.../Form.Designer.cs | 45 +++++-
.../SelfPropelledArtilleryUnit/Form.cs | 73 ++++++++--
.../IMoveableObject.cs | 36 +++++
.../MoveToBorder.cs | 59 ++++++++
.../MoveToCenter.cs | 59 ++++++++
.../ObjectParameters.cs | 57 ++++++++
.../SelfPropelledArtilleryUnit/Status.cs | 18 +++
14 files changed, 616 insertions(+), 73 deletions(-)
create mode 100644 SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/AbstractStrategy.cs
delete mode 100644 SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/DrawingSPAUchild.cs
create mode 100644 SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/DrawningObjectSPAU.cs
rename SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/{DrawingSPAU.cs => DrawningSPAU.cs} (68%)
create mode 100644 SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/DrawningSPAUchild.cs
create mode 100644 SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/IMoveableObject.cs
create mode 100644 SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/MoveToBorder.cs
create mode 100644 SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/MoveToCenter.cs
create mode 100644 SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/ObjectParameters.cs
create mode 100644 SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/Status.cs
diff --git a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/AbstractStrategy.cs b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/AbstractStrategy.cs
new file mode 100644
index 0000000..e77fd1d
--- /dev/null
+++ b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/AbstractStrategy.cs
@@ -0,0 +1,133 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using SelfPropelledArtilleryUnit.DrawningObjects;
+
+namespace SelfPropelledArtilleryUnit.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;
+ }
+ }
+}
\ No newline at end of file
diff --git a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/DrawingSPAUchild.cs b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/DrawingSPAUchild.cs
deleted file mode 100644
index dcc0142..0000000
--- a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/DrawingSPAUchild.cs
+++ /dev/null
@@ -1,33 +0,0 @@
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
-using System.Threading.Tasks;
-
-namespace SelfPropelledArtilleryUnit
-{
- public class DrawingSPAUchild : DrawingSPAU
- {
- public new EntitySPAUchild EntitySPAU;
- /// Дополнительный цвет
- /// Признак наличия залповой установки
-
- public override void DrawingZU(Graphics g)
- {
- base.DrawingZU(g);
- Pen penBlack = new Pen(Color.Black);
- Brush additionalBrush = new SolidBrush(EntitySPAU.AdditionalColor);
- // обвесы
- if (EntitySPAU.BodyKit)
- {
- //залповая усутановка
- g.FillRectangle(additionalBrush, _startPosX + 15, _startPosY + 20, 20, 40);
- g.DrawLine(penBlack, _startPosX + 5, _startPosY + 20, _startPosX + 15, _startPosY + 25);
- }
- }
- public DrawingSPAUchild(int speed, double weight, Color bodyColor, Color additionalColor, bool bodyKit, int width, int height) : base(speed, weight, bodyColor, width, height)
- {
- EntitySPAU = new EntitySPAUchild(speed, weight, bodyColor, additionalColor, bodyKit);
- }
- }
-}
diff --git a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/DrawningObjectSPAU.cs b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/DrawningObjectSPAU.cs
new file mode 100644
index 0000000..1c582c3
--- /dev/null
+++ b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/DrawningObjectSPAU.cs
@@ -0,0 +1,39 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using SelfPropelledArtilleryUnit.DrawningObjects;
+
+///
+/// Реализация интерфейса IDrawningObject для работы с объектом DrawningCar (паттерн Adapter)
+///
+
+namespace SelfPropelledArtilleryUnit.MovementStrategy
+{
+ public class DrawningObjectSPAU : IMoveableObject
+ {
+ private readonly DrawningSPAU? _drawningCar = null;
+ public DrawningObjectSPAU(DrawningSPAU drawningCar)
+ {
+ _drawningCar = drawningCar;
+ }
+ public ObjectParameters? GetObjectPosition
+ {
+ get
+ {
+ if (_drawningCar == null || _drawningCar.EntitySPAU == null)
+ {
+ return null;
+ }
+ return new ObjectParameters(_drawningCar.GetPosX,
+ _drawningCar.GetPosY, _drawningCar.GetWidth, _drawningCar.GetHeight);
+ }
+ }
+ public int GetStep => (int)(_drawningCar?.EntitySPAU?.Step ?? 0);
+ public bool CheckCanMove(DirectionType direction) =>
+ _drawningCar?.CanMove(direction) ?? false;
+ public void MoveObject(DirectionType direction) =>
+ _drawningCar?.MoveTransport(direction);
+ }
+}
diff --git a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/DrawingSPAU.cs b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/DrawningSPAU.cs
similarity index 68%
rename from SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/DrawingSPAU.cs
rename to SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/DrawningSPAU.cs
index 2bcd6ce..9b0ef7d 100644
--- a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/DrawingSPAU.cs
+++ b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/DrawningSPAU.cs
@@ -4,18 +4,19 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
+using SelfPropelledArtilleryUnit.Entities;
-namespace SelfPropelledArtilleryUnit
+namespace SelfPropelledArtilleryUnit.DrawningObjects
{
///
/// Класс, отвечающий за прорисовку и перемещение объекта-сущности
///
- public class DrawingSPAU
+ public class DrawningSPAU
{
///
/// Класс-сущность
///
- public EntitySPAU? EntitySPAU { get; private set; }
+ public EntitySPAU? EntitySPAU { get; protected set; }
///
/// Ширина окна
///
@@ -49,7 +50,7 @@ namespace SelfPropelledArtilleryUnit
/// Ширина картинки
/// Высота картинки
/// true - объект создан, false - проверка не пройдена,нельзя создать объект в этих размерах
- public DrawingSPAU(int speed, double weight, Color bodyColor, int width, int height)
+ public DrawningSPAU(int speed, double weight, Color bodyColor, int width, int height)
{
_pictureWidth = width;
_pictureHeight = height;
@@ -63,6 +64,22 @@ namespace SelfPropelledArtilleryUnit
}
EntitySPAU = new EntitySPAU(speed, weight, bodyColor);
}
+ public DrawningSPAU(int speed, double weight, Color bodyColor, int width, int height, int carWidth, int carHeight)
+ {
+ _pictureWidth = width;
+ _pictureHeight = height;
+ _carWidth = carWidth;
+ _carHeight = carHeight;
+ if (_carHeight >= height)
+ {
+ return;
+ }
+ if (_carWidth >= width)
+ {
+ return;
+ }
+ EntitySPAU = new EntitySPAU(speed, weight, bodyColor);
+ }
///
/// Установка позиции
///
@@ -87,7 +104,7 @@ namespace SelfPropelledArtilleryUnit
/// Направление
public void MoveTransport(DirectionType direction)
{
- if (EntitySPAU == null)
+ if (!CanMove(direction) || EntitySPAU == null)
{
return;
}
@@ -95,44 +112,30 @@ namespace SelfPropelledArtilleryUnit
{
//влево
case DirectionType.Left:
- if (_startPosX - EntitySPAU.Step > 0)
- {
- _startPosX -= (int)EntitySPAU.Step;
- }
+ _startPosX -= (int)EntitySPAU.Step;
break;
//вверх
case DirectionType.Up:
- if (_startPosY - EntitySPAU.Step > 0)
- {
- _startPosY -= (int)EntitySPAU.Step;
- }
+ _startPosY -= (int)EntitySPAU.Step;
break;
// вправо
case DirectionType.Right:
- if (_startPosX + EntitySPAU.Step < _pictureWidth - _carWidth)
- {
- _startPosX += (int)EntitySPAU.Step;
- }
+ _startPosX += (int)EntitySPAU.Step;
break;
//вниз
case DirectionType.Down:
- if (_startPosY + EntitySPAU.Step < _pictureHeight - _carHeight)
- {
- _startPosY += (int)EntitySPAU.Step;
- }
+ _startPosY += (int)EntitySPAU.Step;
break;
}
}
///
/// Прорисовка объекта
///
- public virtual void DrawingZU(Graphics g)
- {}
///
/// Прорисовка объекта
///
///
- public void DrawTransport(Graphics g)
+ public virtual void DrawTransport(Graphics g)
{
if (EntitySPAU == null)
{
@@ -140,8 +143,6 @@ namespace SelfPropelledArtilleryUnit
}
Pen penBlack = new(Color.Black);
- DrawingZU(g);
-
//гусеницы
Brush brBlack = new SolidBrush(Color.Black);
Brush brBody = new SolidBrush(EntitySPAU.BodyColor);
@@ -176,5 +177,45 @@ namespace SelfPropelledArtilleryUnit
g.FillPolygon(brBody, pointsHead);
g.DrawPolygon(penBlack, pointsHead);
}
+ ///
+ /// Координата X объекта
+ ///
+ public int GetPosX => _startPosX;
+ ///
+ /// Координата Y объекта
+ /// ///
+ public int GetPosY => _startPosY;
+ ///
+ /// Ширина объекта
+ ///
+ public int GetWidth => _carWidth;
+ ///
+ /// Высота объекта
+ ///
+ public int GetHeight => _carHeight;
+ ///
+ /// Проверка, что объект может переместится по указанному направлению
+ ///
+ /// Направление
+ /// true - можно переместится по указанному направлению
+ public bool CanMove(DirectionType direction)
+ {
+ if (EntitySPAU == null)
+ {
+ return false;
+ }
+ return direction switch
+ {
+ //влево
+ DirectionType.Left => _startPosX - EntitySPAU.Step > 0,
+ //вверх
+ DirectionType.Up => _startPosY - EntitySPAU.Step > 0,
+ // вправо
+ DirectionType.Right => _startPosX + EntitySPAU.Step < _pictureWidth - _carWidth,
+ //вниз
+ DirectionType.Down => _startPosY + EntitySPAU.Step < _pictureHeight - _carHeight,
+ _ => false,
+ };
+ }
}
}
diff --git a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/DrawningSPAUchild.cs b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/DrawningSPAUchild.cs
new file mode 100644
index 0000000..4ae1dad
--- /dev/null
+++ b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/DrawningSPAUchild.cs
@@ -0,0 +1,40 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using SelfPropelledArtilleryUnit.Entities;
+
+namespace SelfPropelledArtilleryUnit.DrawningObjects
+{
+ public class DrawningSPAUchild : DrawningSPAU
+ {
+ /// Дополнительный цвет
+ /// Признак наличия залповой установки
+ public DrawningSPAUchild(int speed, double weight, Color bodyColor, Color additionalColor, bool bodyKit, int width, int height) : base(speed, weight, bodyColor, width, height)
+ {
+ if (EntitySPAU != null)
+ {
+ EntitySPAU = new EntitySPAUchild(speed, weight, bodyColor, additionalColor, bodyKit);
+ }
+ }
+ public override void DrawTransport(Graphics g)
+ {
+ if (EntitySPAU is not EntitySPAUchild entitySPAUchild)
+ {
+ return;
+ }
+ Pen penBlack = new Pen(Color.Black);
+ Brush additionalBrush = new SolidBrush(entitySPAUchild.AdditionalColor);
+ // обвесы
+ if (entitySPAUchild.BodyKit)
+ {
+ //залповая усутановка
+ g.FillRectangle(additionalBrush, _startPosX + 15, _startPosY + 20, 20, 40);
+ g.DrawLine(penBlack, _startPosX + 5, _startPosY + 20, _startPosX + 15, _startPosY + 25);
+ }
+ base.DrawTransport(g);
+ }
+
+ }
+}
diff --git a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/EntitySPAU.cs b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/EntitySPAU.cs
index 7723ea1..2ac7543 100644
--- a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/EntitySPAU.cs
+++ b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/EntitySPAU.cs
@@ -1,4 +1,4 @@
-namespace SelfPropelledArtilleryUnit
+namespace SelfPropelledArtilleryUnit.Entities
{
public class EntitySPAU
{
diff --git a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/EntitySPAUchild.cs b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/EntitySPAUchild.cs
index f86eda6..25dfa58 100644
--- a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/EntitySPAUchild.cs
+++ b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/EntitySPAUchild.cs
@@ -4,7 +4,7 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
-namespace SelfPropelledArtilleryUnit
+namespace SelfPropelledArtilleryUnit.Entities
{
public class EntitySPAUchild : EntitySPAU
{
diff --git a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/Form.Designer.cs b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/Form.Designer.cs
index 4d9efe6..25d4253 100644
--- a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/Form.Designer.cs
+++ b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/Form.Designer.cs
@@ -34,6 +34,9 @@
buttonDown = new Button();
buttonLeft = new Button();
buttonRight = new Button();
+ buttonCreateParent = new Button();
+ buttonStep = new Button();
+ comboBoxStrategy = new ComboBox();
((System.ComponentModel.ISupportInitialize)pictureBoxSPAU).BeginInit();
SuspendLayout();
//
@@ -49,11 +52,11 @@
// buttonCreate
//
buttonCreate.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
- buttonCreate.Location = new Point(22, 410);
+ buttonCreate.Location = new Point(134, 412);
buttonCreate.Name = "buttonCreate";
- buttonCreate.Size = new Size(82, 29);
+ buttonCreate.Size = new Size(301, 29);
buttonCreate.TabIndex = 1;
- buttonCreate.Text = "Создать";
+ buttonCreate.Text = "Создать САУ с залповой установкой";
buttonCreate.UseVisualStyleBackColor = true;
buttonCreate.Click += buttonCreate_Click;
//
@@ -101,11 +104,44 @@
buttonRight.UseVisualStyleBackColor = true;
buttonRight.Click += buttonMove_Click;
//
+ // buttonCreateParent
+ //
+ buttonCreateParent.Location = new Point(12, 409);
+ buttonCreateParent.Name = "buttonCreateParent";
+ buttonCreateParent.Size = new Size(116, 30);
+ buttonCreateParent.TabIndex = 6;
+ buttonCreateParent.Text = "Создать САУ";
+ buttonCreateParent.UseVisualStyleBackColor = true;
+ buttonCreateParent.Click += buttonCreateParent_Click;
+ //
+ // buttonStep
+ //
+ buttonStep.Location = new Point(794, 65);
+ buttonStep.Name = "buttonStep";
+ buttonStep.Size = new Size(76, 30);
+ buttonStep.TabIndex = 7;
+ buttonStep.Text = "Шаг";
+ buttonStep.UseVisualStyleBackColor = true;
+ buttonStep.Click += buttonStep_Click;
+ //
+ // comboBoxStrategy
+ //
+ comboBoxStrategy.DropDownStyle = ComboBoxStyle.DropDownList;
+ comboBoxStrategy.FormattingEnabled = true;
+ comboBoxStrategy.Items.AddRange(new object[] { "0", "1" });
+ comboBoxStrategy.Location = new Point(767, 12);
+ comboBoxStrategy.Name = "comboBoxStrategy";
+ comboBoxStrategy.Size = new Size(103, 28);
+ comboBoxStrategy.TabIndex = 8;
+ //
// Form
//
AutoScaleDimensions = new SizeF(8F, 20F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(882, 453);
+ Controls.Add(comboBoxStrategy);
+ Controls.Add(buttonStep);
+ Controls.Add(buttonCreateParent);
Controls.Add(buttonRight);
Controls.Add(buttonLeft);
Controls.Add(buttonDown);
@@ -127,5 +163,8 @@
private Button buttonDown;
private Button buttonLeft;
private Button buttonRight;
+ private Button buttonCreateParent;
+ private Button buttonStep;
+ private ComboBox comboBoxStrategy;
}
}
\ No newline at end of file
diff --git a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/Form.cs b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/Form.cs
index 23d8440..757860f 100644
--- a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/Form.cs
+++ b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/Form.cs
@@ -1,3 +1,6 @@
+using SelfPropelledArtilleryUnit.DrawningObjects;
+using SelfPropelledArtilleryUnit.MovementStrategy;
+
namespace SelfPropelledArtilleryUnit
{
public partial class Form : System.Windows.Forms.Form
@@ -5,7 +8,13 @@ namespace SelfPropelledArtilleryUnit
///
/// -
///
- private DrawingSPAU? _drawningSPAU;
+ private DrawningSPAU? _drawningSPAU;
+
+ ///
+ ///
+ ///
+ private AbstractStrategy? _abstractStrategy;
+
///
///
///
@@ -22,14 +31,12 @@ namespace SelfPropelledArtilleryUnit
{
return;
}
- Bitmap bmp = new(pictureBoxSPAU.Width,
- pictureBoxSPAU.Height);
+ Bitmap bmp = new(pictureBoxSPAU.Width, pictureBoxSPAU.Height);
Graphics gr = Graphics.FromImage(bmp);
_drawningSPAU.DrawTransport(gr);
pictureBoxSPAU.Image = bmp;
}
-
///
/// ""
///
@@ -38,22 +45,32 @@ namespace SelfPropelledArtilleryUnit
private void buttonCreate_Click(object sender, EventArgs e)
{
Random random = new();
- _drawningSPAU = new DrawingSPAUchild(random.Next(100, 300),
+ _drawningSPAU = new DrawningSPAUchild(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)),
+ random.Next(0, 256)), true,
+ pictureBoxSPAU.Width, pictureBoxSPAU.Height);
+ _drawningSPAU.SetPosition(random.Next(10, 100),
+ random.Next(10, 100));
+ Draw();
+ }
+ private void buttonCreateParent_Click(object sender, EventArgs e)
+ {
+ Random random = new();
+ _drawningSPAU = new DrawningSPAU(random.Next(100, 300),
+ random.Next(1000, 3000),
+ Color.FromArgb(random.Next(0, 256), random.Next(0, 256),
+ random.Next(0, 256)),
pictureBoxSPAU.Width, pictureBoxSPAU.Height);
_drawningSPAU.SetPosition(random.Next(10, 100),
random.Next(10, 100));
Draw();
}
-
private void buttonMove_Click(object sender, EventArgs e)
{
- if(_drawningSPAU == null)
+ if (_drawningSPAU == null)
{
return;
}
@@ -75,5 +92,43 @@ namespace SelfPropelledArtilleryUnit
}
Draw();
}
+
+ private void buttonStep_Click(object sender, EventArgs e)
+ {
+ if (_drawningSPAU == null)
+ {
+ return;
+ }
+ if (comboBoxStrategy.Enabled)
+ {
+ _abstractStrategy = comboBoxStrategy.SelectedIndex
+ switch
+ {
+ 0 => new MoveToCenter(),
+ 1 => new MoveToBorder(),
+ _ => null,
+ };
+ if (_abstractStrategy == null)
+ {
+ return;
+ }
+ _abstractStrategy.SetData(new
+ DrawningObjectSPAU(_drawningSPAU), pictureBoxSPAU.Width,
+ pictureBoxSPAU.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/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/IMoveableObject.cs b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/IMoveableObject.cs
new file mode 100644
index 0000000..7582af8
--- /dev/null
+++ b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/IMoveableObject.cs
@@ -0,0 +1,36 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using SelfPropelledArtilleryUnit.DrawningObjects;
+
+namespace SelfPropelledArtilleryUnit.MovementStrategy
+{
+ ///
+ /// Интерфейс для работы с перемещаемым объектом
+ ///
+ public interface IMoveableObject
+ {
+ ///
+ /// Получение координаты X объекта
+ ///
+ ObjectParameters? GetObjectPosition { get; }
+ ///
+ /// Шаг объекта
+ ///
+ int GetStep { get; }
+ ///
+ /// Проверка, можно ли переместиться по нужному направлению
+ ///
+ ///
+ ///
+ bool CheckCanMove(DirectionType direction);
+ ///
+ /// Изменение направления пермещения объекта
+ ///
+ /// Направление
+ void MoveObject(DirectionType direction);
+
+ }
+}
diff --git a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/MoveToBorder.cs b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/MoveToBorder.cs
new file mode 100644
index 0000000..cd9e948
--- /dev/null
+++ b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/MoveToBorder.cs
@@ -0,0 +1,59 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace SelfPropelledArtilleryUnit.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/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/MoveToCenter.cs b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/MoveToCenter.cs
new file mode 100644
index 0000000..71fa9fa
--- /dev/null
+++ b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/MoveToCenter.cs
@@ -0,0 +1,59 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace SelfPropelledArtilleryUnit.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();
+ }
+ }
+ }
+ }
+}
\ No newline at end of file
diff --git a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/ObjectParameters.cs b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/ObjectParameters.cs
new file mode 100644
index 0000000..c02860a
--- /dev/null
+++ b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/ObjectParameters.cs
@@ -0,0 +1,57 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace SelfPropelledArtilleryUnit.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/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/Status.cs b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/Status.cs
new file mode 100644
index 0000000..7b57382
--- /dev/null
+++ b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/Status.cs
@@ -0,0 +1,18 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace SelfPropelledArtilleryUnit.MovementStrategy
+{
+ ///
+ /// Статус выполнения операции перемещения
+ ///
+ public enum Status
+ {
+ NotInit,
+ InProgress,
+ Finish
+ }
+}
--
2.25.1