Pibd-13_Garifullin_F.M_LabWork02 #11
@ -3,7 +3,7 @@ Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
# Visual Studio Version 17
|
||||
VisualStudioVersion = 17.8.34316.72
|
||||
MinimumVisualStudioVersion = 10.0.40219.1
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ProjectTank", "ProjectTank\ProjectTank.csproj", "{65B41360-B289-4A4E-8BA7-E53F4AF9336A}"
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ProjectTank", "ProjectTank\ProjectTank.csproj", "{65B41360-B289-4A4E-8BA7-E53F4AF9336A}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
|
@ -1,10 +1,14 @@
|
||||
namespace ProjectTank
|
||||
namespace ProjectTank.Drawning
|
||||
{
|
||||
/// <summary>
|
||||
/// направление перемещенния
|
||||
/// </summary>
|
||||
public enum DirectionType
|
||||
{
|
||||
/// <summary>
|
||||
/// Неизвестное направление
|
||||
/// </summary>
|
||||
Unknow = -1,
|
||||
/// <summary>
|
||||
/// Вверх
|
||||
/// </summary>
|
||||
@ -21,5 +25,6 @@
|
||||
/// Вправо
|
||||
/// </summary>
|
||||
Right = 4,
|
||||
|
||||
}
|
||||
}
|
64
ProjectTank/ProjectTank/Drawning/DrawningBattleTank.cs
Normal file
64
ProjectTank/ProjectTank/Drawning/DrawningBattleTank.cs
Normal file
@ -0,0 +1,64 @@
|
||||
namespace ProjectTank.Drawning;
|
||||
/// <summary>
|
||||
/// Класс, отвечающий за прорисовку и перемещение объекта-сущности
|
||||
/// </summary>
|
||||
public class DrawningBattleTank :DrawningTank
|
||||
{
|
||||
/// <summary>
|
||||
/// Конструктор
|
||||
/// <summary>
|
||||
/// <param name="speed">Скорость</param>
|
||||
/// <param name="weight">Вес автомобиля</param>
|
||||
/// <param name="bodyColor">Основной цвет</param>
|
||||
/// <param name="additionalColor">Дополнительный цвет</param>
|
||||
/// <param name="gun">Признак наличия пушки</param>
|
||||
/// <param name="machinGun">признак наличия пулемета</param>
|
||||
///
|
||||
public DrawningBattleTank(int speed, double weight, Color bodyColor, Color additionalColor, bool gun, bool machinGun) : base(150, 87)
|
||||
{
|
||||
EntityTank = new EntityBattleTank(speed, weight, bodyColor, additionalColor, gun, machinGun);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Прорисовка объекта
|
||||
/// </summary>
|
||||
/// <param name="g"></param>
|
||||
public override void DrawTransport(Graphics g)
|
||||
{
|
||||
if (EntityTank == null || EntityTank is not EntityBattleTank battleTank || !_startPosX.HasValue || !_startPosY.HasValue)
|
||||
{
|
||||
return;
|
||||
}
|
||||
Pen pen = new(Color.Black);
|
||||
Brush additionalBrush = new SolidBrush(battleTank.AdditionalColor);
|
||||
|
||||
if (battleTank.Gun)
|
||||
{
|
||||
g.DrawRectangle(pen, _startPosX.Value, _startPosY.Value + 30, 45, 5);
|
||||
|
||||
g.FillRectangle(additionalBrush, _startPosX.Value, _startPosY.Value + 30, 45, 5);
|
||||
}
|
||||
|
||||
//пулемет
|
||||
if (battleTank.MachinGun)
|
||||
{
|
||||
g.DrawRectangle(pen, _startPosX.Value + 65, _startPosY.Value + 15, 16, 5);
|
||||
g.DrawRectangle(pen, _startPosX.Value + 72, _startPosY.Value + 10, 5, 5);
|
||||
g.DrawRectangle(pen, _startPosX.Value + 71, _startPosY.Value + 3, 7, 7);
|
||||
g.DrawRectangle(pen, _startPosX.Value + 55, _startPosY.Value + 5, 15, 3);
|
||||
|
||||
g.FillRectangle(additionalBrush, _startPosX.Value + 65, _startPosY.Value + 15, 16, 5);
|
||||
g.FillRectangle(additionalBrush, _startPosX.Value + 72, _startPosY.Value + 10, 5, 5);
|
||||
g.FillRectangle(additionalBrush, _startPosX.Value + 71, _startPosY.Value + 3, 7, 7);
|
||||
g.FillRectangle(additionalBrush, _startPosX.Value + 55, _startPosY.Value + 5, 15, 3);
|
||||
}
|
||||
_startPosY += 20;
|
||||
base.DrawTransport(g);
|
||||
_startPosY -= 20;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
239
ProjectTank/ProjectTank/Drawning/DrawningTank.cs
Normal file
239
ProjectTank/ProjectTank/Drawning/DrawningTank.cs
Normal file
@ -0,0 +1,239 @@
|
||||
using ProjectTank.Entities;
|
||||
namespace ProjectTank.Drawning
|
||||
{
|
||||
public class DrawningTank
|
||||
|
||||
{
|
||||
/// <summary>
|
||||
/// класс - сущность
|
||||
/// </summary>
|
||||
public EntityTank? EntityTank { get; protected set; }
|
||||
|
||||
/// <summary>
|
||||
/// ширина окна
|
||||
/// </summary>
|
||||
private int? _pictureWidth;
|
||||
|
||||
/// <summary>
|
||||
/// высота окна
|
||||
/// </summary>
|
||||
private int? _pictureHeight;
|
||||
|
||||
/// <summary>
|
||||
/// Левая координата прорисовки танка
|
||||
/// </summary>
|
||||
protected int? _startPosX;
|
||||
|
||||
/// <summary>
|
||||
/// Верхняя координата прорисовки танка
|
||||
/// </summary>
|
||||
protected int? _startPosY;
|
||||
|
||||
/// <summary>
|
||||
/// ширина прорисовки танка
|
||||
/// /// </summary>
|
||||
private readonly int _drawningTankWidth = 150;
|
||||
|
||||
/// <summary>
|
||||
/// высота прорисовки танка
|
||||
/// </summary>
|
||||
private readonly int _drawningTankHeight = 67;
|
||||
|
||||
/// <summary>
|
||||
/// Координата X объекта
|
||||
/// </summary>
|
||||
public int? GetPosX => _startPosX;
|
||||
|
||||
/// <summary>
|
||||
/// Координата Y объекта
|
||||
/// </summary>
|
||||
public int? GetPosY => _startPosY;
|
||||
|
||||
/// <summary>
|
||||
/// Ширина объекта
|
||||
/// </summary>
|
||||
public int GetWidth => _drawningTankWidth;
|
||||
|
||||
/// <summary>
|
||||
/// Высота объекта
|
||||
/// </summary>
|
||||
public int GetHeight => _drawningTankHeight;
|
||||
|
||||
/// <summary>
|
||||
/// Пустой конструктор
|
||||
/// </summary>
|
||||
private DrawningTank()
|
||||
{
|
||||
_pictureWidth = null;
|
||||
_pictureHeight = null;
|
||||
_startPosX = null;
|
||||
_startPosY = null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Конструктор
|
||||
/// <summary>
|
||||
/// <param name="speed">Скорость</param>
|
||||
/// <param name="weight">Вес танка</param>
|
||||
/// <param name="bodyColor">Основной цвет</param>
|
||||
public DrawningTank(int speed, double weight, Color bodyColor) : this()
|
||||
{
|
||||
EntityTank = new EntityTank(speed, weight, bodyColor);
|
||||
}
|
||||
/// <summary>
|
||||
/// Конструктор
|
||||
/// <summary>
|
||||
/// <param name="drawingBusWidth">Ширина прорисовки танка</param>
|
||||
/// <param name="drawingBusHeight">Высота прорисовки танка</param>
|
||||
protected DrawningTank(int drawningTankWidth, int drawningTankHeight) : this()
|
||||
{
|
||||
_drawningTankWidth = drawningTankWidth;
|
||||
_drawningTankHeight = drawningTankHeight;
|
||||
}
|
||||
/// <summary>
|
||||
/// Установка границ поля
|
||||
/// </summary>
|
||||
/// <param name="width">Ширина поля</param>
|
||||
/// <param name="height">Высота поля</param>
|
||||
/// <returns>true - границы заданы, false - проверка не пройдена , нельзя разместить объект в этих размерах</returns>
|
||||
public bool SetPictureSize(int width, int height)
|
||||
{
|
||||
|
||||
if (height < _drawningTankHeight || width < _drawningTankWidth)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
_pictureHeight = height;
|
||||
_pictureWidth = width;
|
||||
if (_startPosX != null && _startPosY != null)
|
||||
{
|
||||
if (_startPosX < 0) _startPosX = 0;
|
||||
if (_startPosY < 0) _startPosY = 0;
|
||||
if (_pictureHeight.Value < _startPosY + _drawningTankHeight)
|
||||
{
|
||||
_startPosY = _pictureHeight - _drawningTankHeight;
|
||||
}
|
||||
if (_pictureWidth.Value < _startPosX + _drawningTankWidth)
|
||||
{
|
||||
_startPosX = _pictureWidth - _drawningTankWidth;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
/// <summary>
|
||||
/// Установка позиции
|
||||
/// </summary>
|
||||
/// <param name="x">Координата X</param>
|
||||
/// <param name="y">Координата Y</param>
|
||||
public void SetPosition(int x, int y)
|
||||
{
|
||||
if (!_pictureWidth.HasValue || !_pictureHeight.HasValue)
|
||||
{
|
||||
return;
|
||||
}
|
||||
_startPosX = x;
|
||||
_startPosY = y;
|
||||
if (_startPosX < 0) _startPosX = 0;
|
||||
if (_startPosY < 0) _startPosY = 0;
|
||||
if (_pictureHeight.Value < _startPosY + _drawningTankHeight)
|
||||
{
|
||||
_startPosY = _pictureHeight - _drawningTankHeight;
|
||||
}
|
||||
if (_pictureWidth.Value < _startPosX + _drawningTankWidth)
|
||||
{
|
||||
_startPosX = _pictureWidth - _drawningTankWidth;
|
||||
}
|
||||
|
||||
}
|
||||
/// <summary>
|
||||
/// Изменение направления перемещения
|
||||
/// </summary>
|
||||
/// <param name="dicretion">направление</param>
|
||||
/// <returns></returns>
|
||||
public bool MoveTransport(DirectionType dicretion)
|
||||
{
|
||||
if (EntityTank == null || _startPosX == null || _startPosY == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
switch (dicretion)
|
||||
{
|
||||
//влево
|
||||
case DirectionType.Left:
|
||||
if (_startPosX.Value - EntityTank.Step > 0)
|
||||
{
|
||||
_startPosX -= (int)EntityTank.Step;
|
||||
}
|
||||
return true;
|
||||
//вправо
|
||||
case DirectionType.Right:
|
||||
if (_startPosX.Value + _drawningTankWidth + EntityTank.Step < _pictureWidth)
|
||||
{
|
||||
_startPosX += (int)EntityTank.Step;
|
||||
}
|
||||
return true;
|
||||
//вверх
|
||||
case DirectionType.Up:
|
||||
if (_startPosY.Value - EntityTank.Step > 0)
|
||||
{
|
||||
_startPosY -= (int)EntityTank.Step;
|
||||
}
|
||||
return true;
|
||||
//вниз
|
||||
case DirectionType.Down:
|
||||
if (_startPosY + EntityTank.Step + _drawningTankHeight < _pictureHeight)
|
||||
{
|
||||
_startPosY += (int)EntityTank.Step;
|
||||
}
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// прорисовка танка
|
||||
/// </summary>
|
||||
/// <param name="g"></param>
|
||||
public virtual void DrawTransport(Graphics g)
|
||||
{
|
||||
if (EntityTank == null || !_startPosX.HasValue || !_startPosY.HasValue)
|
||||
{
|
||||
return;
|
||||
}
|
||||
Pen pen = new(Color.Black);
|
||||
Brush Br = new SolidBrush(EntityTank.BodyColor);
|
||||
//основная часть танка
|
||||
g.DrawRectangle(pen, _startPosX.Value + 6, _startPosY.Value + 20, 135, 20);
|
||||
g.DrawRectangle(pen, _startPosX.Value + 45, _startPosY.Value, 55, 20);
|
||||
|
||||
g.FillRectangle(Br, _startPosX.Value + 6, _startPosY.Value + 20, 135, 20);
|
||||
g.FillRectangle(Br, _startPosX.Value + 45, _startPosY.Value, 55, 20);
|
||||
|
||||
//гусеницы
|
||||
|
||||
g.DrawArc(pen, _startPosX.Value, _startPosY.Value + 40, 15, 30, 100, 170);
|
||||
g.DrawArc(pen, _startPosX.Value + 135, _startPosY.Value + 40, 15, 30, -90, 170);
|
||||
g.DrawLine(pen, _startPosX.Value + 5, _startPosY.Value + 40, _startPosX.Value + 145, _startPosY.Value + 40);
|
||||
g.DrawLine(pen, _startPosX.Value + 5, _startPosY.Value + 70, _startPosX.Value + 145, _startPosY.Value + 70);
|
||||
Pen WidePen = new(Color.Black);
|
||||
WidePen.Width = 2;
|
||||
g.DrawEllipse(WidePen, _startPosX.Value, _startPosY.Value + 40, 24, 24);
|
||||
g.DrawEllipse(WidePen, _startPosX.Value + 125, _startPosY.Value + 40, 24, 24);
|
||||
|
||||
//катки
|
||||
g.DrawEllipse(pen, _startPosX.Value + 26, _startPosY.Value + 50, 20, 20);
|
||||
g.DrawEllipse(pen, _startPosX.Value + 52, _startPosY.Value + 50, 20, 20);
|
||||
g.DrawEllipse(pen, _startPosX.Value + 78, _startPosY.Value + 50, 20, 20);
|
||||
g.DrawEllipse(pen, _startPosX.Value + 104, _startPosY.Value + 50, 20, 20);
|
||||
|
||||
|
||||
g.DrawEllipse(pen, _startPosX.Value + 49, _startPosY.Value + 40, 5, 5);
|
||||
g.DrawEllipse(pen, _startPosX.Value + 75, _startPosY.Value + 40, 5, 5);
|
||||
g.DrawEllipse(pen, _startPosX.Value + 101, _startPosY.Value + 40, 5, 5);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@ -1,231 +0,0 @@
|
||||
namespace ProjectTank;
|
||||
/// <summary>
|
||||
/// Класс, отвечающий за прорисовку и перемещение объекта-сущности
|
||||
/// </summary>
|
||||
public class DrawningBattleTank
|
||||
{
|
||||
/// <summary>
|
||||
/// класс - сущность
|
||||
/// </summary>
|
||||
public EntityBattleTank? EntityBattleTank { 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 _drawningTankWidth = 150;
|
||||
|
||||
/// <summary>
|
||||
/// высота прорисовки танка
|
||||
/// </summary>
|
||||
private readonly int _drawingTankHeight = 87;
|
||||
|
||||
/// <summary>
|
||||
/// Инициализация свойств
|
||||
/// </summary>
|
||||
/// <param name="speed"> Скорость </param>
|
||||
/// <param name="weight">Вес </param>
|
||||
/// <param name="bodyColor">Основной цвет </param>
|
||||
/// <param name="additionalColor"> Дополнительный цвет</param>
|
||||
/// <param name="gun"> орудие</param>
|
||||
/// <param name="machinGun"> пулемет на башне</param>
|
||||
public void Init(EntityBattleTank entityBattleTank)
|
||||
{
|
||||
EntityBattleTank = entityBattleTank;
|
||||
_pictureHeight = null;
|
||||
_pictureWidth = null;
|
||||
_startPosX = null;
|
||||
_startPosY = null;
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Установка границ поля
|
||||
/// </summary>
|
||||
/// <param name="width">Ширина поля</param>
|
||||
/// <param name="height">Высота поля</param>
|
||||
/// <returns>true - границы заданы, false - проверка не пройдена , нельзя разместить объект в этих размерах</returns>
|
||||
public bool SetPictureSize(int width, int height)
|
||||
{
|
||||
|
||||
if (height < _drawingTankHeight || width < _drawningTankWidth)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
_pictureHeight = height;
|
||||
_pictureWidth = width;
|
||||
if (_startPosX != null && _startPosY != null)
|
||||
{
|
||||
if (_startPosX < 0) _startPosX = 0;
|
||||
if (_startPosY < 0) _startPosY = 0;
|
||||
if (_pictureHeight.Value < (_startPosY + _drawingTankHeight))
|
||||
{
|
||||
_startPosY = _pictureHeight - _drawingTankHeight;
|
||||
}
|
||||
if (_pictureWidth.Value < (_startPosX + _drawningTankWidth))
|
||||
{
|
||||
_startPosX = _pictureWidth - _drawningTankWidth;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
/// <summary>
|
||||
/// Установка позиции
|
||||
/// </summary>
|
||||
/// <param name="x">Координата X</param>
|
||||
/// <param name="y">Координата Y</param>
|
||||
public void SetPosition(int x, int y)
|
||||
{
|
||||
if (!_pictureWidth.HasValue || !_pictureHeight.HasValue)
|
||||
{
|
||||
return;
|
||||
}
|
||||
_startPosX = x;
|
||||
_startPosY = y;
|
||||
if (_startPosX < 0) _startPosX = 0;
|
||||
if (_startPosY < 0) _startPosY = 0;
|
||||
if (_pictureHeight.Value < (_startPosY + _drawingTankHeight))
|
||||
{
|
||||
_startPosY = _pictureHeight - _drawingTankHeight;
|
||||
}
|
||||
if (_pictureWidth.Value < (_startPosX + _drawningTankWidth))
|
||||
{
|
||||
_startPosX = _pictureWidth - _drawningTankWidth;
|
||||
}
|
||||
|
||||
}
|
||||
/// <summary>
|
||||
/// Изменение направления перемещения
|
||||
/// </summary>
|
||||
/// <param name="dicretion">направление</param>
|
||||
/// <returns></returns>
|
||||
public bool MoveTransport(DirectionType dicretion)
|
||||
{
|
||||
if (EntityBattleTank == null || _startPosX == null || _startPosY == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
switch (dicretion)
|
||||
{
|
||||
//влево
|
||||
case DirectionType.Left:
|
||||
if (_startPosX.Value - EntityBattleTank.Step > 0)
|
||||
{
|
||||
_startPosX -= (int)EntityBattleTank.Step;
|
||||
}
|
||||
return true;
|
||||
//вправо
|
||||
case DirectionType.Right:
|
||||
if (_startPosX.Value + _drawningTankWidth + EntityBattleTank.Step < _pictureWidth)
|
||||
{
|
||||
_startPosX += (int)EntityBattleTank.Step;
|
||||
}
|
||||
return true;
|
||||
//вверх
|
||||
case DirectionType.Up:
|
||||
if (_startPosY.Value - EntityBattleTank.Step > 0)
|
||||
{
|
||||
_startPosY -= (int)EntityBattleTank.Step;
|
||||
}
|
||||
return true;
|
||||
//вниз
|
||||
case DirectionType.Down:
|
||||
if (_startPosY + EntityBattleTank.Step + _drawingTankHeight < _pictureHeight)
|
||||
{
|
||||
_startPosY += (int)EntityBattleTank.Step;
|
||||
}
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// прорисовка танка
|
||||
/// </summary>
|
||||
/// <param name="g"></param>
|
||||
public void DrawTransport(Graphics g)
|
||||
{
|
||||
if (EntityBattleTank == null || !_startPosX.HasValue || !_startPosY.HasValue)
|
||||
{
|
||||
return;
|
||||
}
|
||||
Pen pen = new(Color.Black);
|
||||
Brush addititionalBrush = new SolidBrush(EntityBattleTank.AdditionalColor);
|
||||
Brush Br = new SolidBrush(EntityBattleTank.BodyColor);
|
||||
//основная часть танка
|
||||
g.DrawRectangle(pen, _startPosX.Value + 6, _startPosY.Value + 40, 135, 20);
|
||||
g.DrawRectangle(pen, _startPosX.Value + 45, _startPosY.Value + 20, 55, 20);
|
||||
|
||||
g.FillRectangle(Br, _startPosX.Value + 6, _startPosY.Value + 40, 135, 20);
|
||||
g.FillRectangle(Br, _startPosX.Value + 45, _startPosY.Value + 20, 55, 20);
|
||||
|
||||
//гусеницы
|
||||
|
||||
g.DrawArc(pen, _startPosX.Value, _startPosY.Value + 60, 15, 30, 100, 170);
|
||||
g.DrawArc(pen, _startPosX.Value + 135, _startPosY.Value + 60, 15, 30, -90, 170);
|
||||
g.DrawLine(pen, _startPosX.Value + 5, _startPosY.Value + 60, _startPosX.Value + 145, _startPosY.Value + 60);
|
||||
g.DrawLine(pen, _startPosX.Value + 5, _startPosY.Value + 90, _startPosX.Value + 145, _startPosY.Value + 90);
|
||||
Pen WidePen = new(Color.Black);
|
||||
WidePen.Width = 2;
|
||||
g.DrawEllipse(WidePen, _startPosX.Value, _startPosY.Value + 60, 24, 24);
|
||||
g.DrawEllipse(WidePen, _startPosX.Value + 125, _startPosY.Value + 60, 24, 24);
|
||||
|
||||
//катки
|
||||
g.DrawEllipse(pen, _startPosX.Value + 26, _startPosY.Value + 70, 20, 20);
|
||||
g.DrawEllipse(pen, _startPosX.Value + 52, _startPosY.Value + 70, 20, 20);
|
||||
g.DrawEllipse(pen, _startPosX.Value + 78, _startPosY.Value + 70, 20, 20);
|
||||
g.DrawEllipse(pen, _startPosX.Value + 104, _startPosY.Value + 70, 20, 20);
|
||||
|
||||
|
||||
g.DrawEllipse(pen, _startPosX.Value + 49, _startPosY.Value + 60, 5, 5);
|
||||
g.DrawEllipse(pen, _startPosX.Value + 75, _startPosY.Value + 60, 5, 5);
|
||||
g.DrawEllipse(pen, _startPosX.Value + 101, _startPosY.Value + 60, 5, 5);
|
||||
|
||||
//пушка
|
||||
Brush AdditionalBrush = new SolidBrush(EntityBattleTank.AdditionalColor);
|
||||
if (EntityBattleTank.Gun)
|
||||
{
|
||||
g.DrawRectangle(pen, _startPosX.Value, _startPosY.Value + 30, 45, 5);
|
||||
|
||||
g.FillRectangle(AdditionalBrush, _startPosX.Value, _startPosY.Value + 30, 45, 5);
|
||||
}
|
||||
|
||||
//пулемет
|
||||
if (EntityBattleTank.MachinGun)
|
||||
{
|
||||
g.DrawRectangle(pen, _startPosX.Value + 65, _startPosY.Value + 15, 16, 5);
|
||||
g.DrawRectangle(pen, _startPosX.Value + 72, _startPosY.Value + 10, 5, 5);
|
||||
g.DrawRectangle(pen, _startPosX.Value + 71, _startPosY.Value + 3, 7, 7);
|
||||
g.DrawRectangle(pen, _startPosX.Value + 55, _startPosY.Value + 5, 15, 3);
|
||||
|
||||
g.FillRectangle(AdditionalBrush, _startPosX.Value + 65, _startPosY.Value + 15, 16, 5);
|
||||
g.FillRectangle(AdditionalBrush, _startPosX.Value + 72, _startPosY.Value + 10, 5, 5);
|
||||
g.FillRectangle(AdditionalBrush, _startPosX.Value + 71, _startPosY.Value + 3, 7, 7);
|
||||
g.FillRectangle(AdditionalBrush, _startPosX.Value + 55, _startPosY.Value + 5, 15, 3);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
41
ProjectTank/ProjectTank/Entities/EntityTank.cs
Normal file
41
ProjectTank/ProjectTank/Entities/EntityTank.cs
Normal file
@ -0,0 +1,41 @@
|
||||
namespace ProjectTank.Entities
|
||||
{
|
||||
public class EntityTank
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// Скорость
|
||||
/// </summary>
|
||||
public int Speed { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Вес
|
||||
/// </summary>
|
||||
public double Weight { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Основной цвет
|
||||
/// </summary>
|
||||
public Color BodyColor { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Шаг перемещения танка
|
||||
/// </summary>
|
||||
public double Step => Speed * 100 / Weight;
|
||||
|
||||
/// <summary>
|
||||
/// Конструктор сущности
|
||||
/// </summary>
|
||||
/// <param name="speed">Скорость</param>
|
||||
/// <param name="weight">Веc</param>
|
||||
/// <param name="bodyColor">Основной цвет</param>
|
||||
|
||||
public EntityTank(int speed, double weight, Color bodyColor)
|
||||
{
|
||||
Speed = speed;
|
||||
Weight = weight;
|
||||
BodyColor = bodyColor;
|
||||
|
||||
}
|
||||
}
|
||||
}
|
44
ProjectTank/ProjectTank/Entities/EntiyBattleTank.cs
Normal file
44
ProjectTank/ProjectTank/Entities/EntiyBattleTank.cs
Normal file
@ -0,0 +1,44 @@
|
||||
using ProjectTank.Entities;
|
||||
|
||||
namespace ProjectTank
|
||||
{
|
||||
/// <summary>
|
||||
/// Класс-сущность танк
|
||||
/// </summary>
|
||||
public class EntityBattleTank : EntityTank
|
||||
|
||||
{
|
||||
/// <summary>
|
||||
/// Дополнительный цвет (для опциональных элементов)
|
||||
/// </summary>
|
||||
public Color AdditionalColor { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Признак (опция) наличия пушки
|
||||
/// </summary>
|
||||
public bool Gun { get; private set; }
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Признак (опция) наличия Пулемета на башне
|
||||
/// /// </summary>
|
||||
public bool MachinGun { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Конструктор объекта-класса Боевого Танка
|
||||
/// </summary>
|
||||
/// <param name="speed">Скорость</param>
|
||||
/// <param name="weight">Вес</param>
|
||||
/// <param name="bodyColor">Основной цвет</param>
|
||||
/// <param name="additionalColor">Дополнительный цвет</param>
|
||||
/// <param name="gun">Пушка танка</param>
|
||||
/// <param name="machinGun">Пклемет на башне</param>
|
||||
public EntityBattleTank(int speed, double weight, Color bodyColor, Color additionalColor, bool gun, bool machinGun) : base(speed, weight, bodyColor)
|
||||
{
|
||||
AdditionalColor = additionalColor;
|
||||
MachinGun= machinGun;
|
||||
Gun = gun;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@ -1,59 +0,0 @@
|
||||
namespace ProjectTank
|
||||
{
|
||||
/// <summary>
|
||||
/// Класс-сущность танк
|
||||
/// </summary>
|
||||
public class EntityBattleTank
|
||||
{
|
||||
/// <summary>
|
||||
/// скорость
|
||||
/// </summary>
|
||||
public int Speed { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// вес
|
||||
/// </summary>
|
||||
public double Weight { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// основный цвет
|
||||
/// </summary>
|
||||
public Color BodyColor { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// дополнительный цвет
|
||||
/// </summary>
|
||||
public Color AdditionalColor { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Признак (опция) наличия пушки
|
||||
/// </summary>
|
||||
public bool Gun { get; private set; }
|
||||
/// <summary>
|
||||
/// Признак (опция) наличия пулемета
|
||||
/// </summary>
|
||||
public bool MachinGun { get; private set; }
|
||||
/// <summary>
|
||||
/// шаг
|
||||
/// </summary>
|
||||
public double Step => Speed * 100 / Weight;
|
||||
/// <summary>
|
||||
/// Инициализация полей объекта-класса танк
|
||||
/// </summary>
|
||||
/// <param name="speed"> Скорость </param>
|
||||
/// <param name="weight">Вес </param>
|
||||
/// <param name="bodyColor">Основной цвет </param>
|
||||
/// <param name="additionalColor"> Дополнительный цвет</param>
|
||||
/// <param name="gun"> орудие</param>
|
||||
/// <param name="machinGun"> пулемет на башне</param>
|
||||
public void Init(int speed, double weight, Color bodyColor, Color additionalColor, bool gun, bool machinGun)
|
||||
{
|
||||
Speed = speed;
|
||||
Weight = weight;
|
||||
BodyColor = bodyColor;
|
||||
AdditionalColor = additionalColor;
|
||||
Gun = gun;
|
||||
MachinGun = machinGun;
|
||||
}
|
||||
}
|
||||
}
|
64
ProjectTank/ProjectTank/FormBattleTank.Designer.cs
generated
64
ProjectTank/ProjectTank/FormBattleTank.Designer.cs
generated
@ -29,11 +29,14 @@
|
||||
private void InitializeComponent()
|
||||
{
|
||||
pictureBoxBattleTank = new PictureBox();
|
||||
Создать = new Button();
|
||||
ButtonCreateBattkeTank = new Button();
|
||||
buttonLeft = new Button();
|
||||
buttonUp = new Button();
|
||||
buttonRight = new Button();
|
||||
buttonDown = new Button();
|
||||
ButtonCreateTank = new Button();
|
||||
СomboBoxStrategy = new ComboBox();
|
||||
buttonStrategyStep = new Button();
|
||||
((System.ComponentModel.ISupportInitialize)pictureBoxBattleTank).BeginInit();
|
||||
SuspendLayout();
|
||||
//
|
||||
@ -47,16 +50,16 @@
|
||||
pictureBoxBattleTank.TabStop = false;
|
||||
pictureBoxBattleTank.Click += ButtonMove_Click;
|
||||
//
|
||||
// Создать
|
||||
// ButtonCreateBattkeTank
|
||||
//
|
||||
Создать.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
|
||||
Создать.Location = new Point(12, 443);
|
||||
Создать.Name = "Создать";
|
||||
Создать.Size = new Size(75, 23);
|
||||
Создать.TabIndex = 1;
|
||||
Создать.Text = "Создать";
|
||||
Создать.UseVisualStyleBackColor = true;
|
||||
Создать.Click += ButtonCreate_Click;
|
||||
ButtonCreateBattkeTank.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
|
||||
ButtonCreateBattkeTank.Location = new Point(12, 443);
|
||||
ButtonCreateBattkeTank.Name = "ButtonCreateBattkeTank";
|
||||
ButtonCreateBattkeTank.Size = new Size(150, 23);
|
||||
ButtonCreateBattkeTank.TabIndex = 1;
|
||||
ButtonCreateBattkeTank.Text = "Создать Боевой Танк";
|
||||
ButtonCreateBattkeTank.UseVisualStyleBackColor = true;
|
||||
ButtonCreateBattkeTank.Click += ButtonCreateButtleTank_Ckick;
|
||||
//
|
||||
// buttonLeft
|
||||
//
|
||||
@ -106,16 +109,50 @@
|
||||
buttonDown.UseVisualStyleBackColor = true;
|
||||
buttonDown.Click += ButtonMove_Click;
|
||||
//
|
||||
// ButtonCreateTank
|
||||
//
|
||||
ButtonCreateTank.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
|
||||
ButtonCreateTank.Location = new Point(168, 443);
|
||||
ButtonCreateTank.Name = "ButtonCreateTank";
|
||||
ButtonCreateTank.Size = new Size(150, 23);
|
||||
ButtonCreateTank.TabIndex = 6;
|
||||
ButtonCreateTank.Text = "Создать Танк";
|
||||
ButtonCreateTank.UseVisualStyleBackColor = true;
|
||||
ButtonCreateTank.Click += ButtonCreateTank_Ckick;
|
||||
//
|
||||
// СomboBoxStrategy
|
||||
//
|
||||
СomboBoxStrategy.DropDownStyle = ComboBoxStyle.DropDownList;
|
||||
СomboBoxStrategy.FormattingEnabled = true;
|
||||
СomboBoxStrategy.Items.AddRange(new object[] { "К центру", "К краю" });
|
||||
СomboBoxStrategy.Location = new Point(698, 12);
|
||||
СomboBoxStrategy.Name = "СomboBoxStrategy";
|
||||
СomboBoxStrategy.Size = new Size(121, 23);
|
||||
СomboBoxStrategy.TabIndex = 7;
|
||||
//
|
||||
// buttonStrategyStep
|
||||
//
|
||||
buttonStrategyStep.Location = new Point(744, 50);
|
||||
buttonStrategyStep.Name = "buttonStrategyStep";
|
||||
buttonStrategyStep.Size = new Size(75, 23);
|
||||
buttonStrategyStep.TabIndex = 8;
|
||||
buttonStrategyStep.Text = "шаг";
|
||||
buttonStrategyStep.UseVisualStyleBackColor = true;
|
||||
buttonStrategyStep.Click += ButtonStrategyStep_Ckick;
|
||||
//
|
||||
// FormBattleTank
|
||||
//
|
||||
AutoScaleDimensions = new SizeF(7F, 15F);
|
||||
AutoScaleMode = AutoScaleMode.Font;
|
||||
ClientSize = new Size(831, 478);
|
||||
Controls.Add(buttonStrategyStep);
|
||||
Controls.Add(СomboBoxStrategy);
|
||||
Controls.Add(ButtonCreateTank);
|
||||
Controls.Add(buttonDown);
|
||||
Controls.Add(buttonRight);
|
||||
Controls.Add(buttonUp);
|
||||
Controls.Add(buttonLeft);
|
||||
Controls.Add(Создать);
|
||||
Controls.Add(ButtonCreateBattkeTank);
|
||||
Controls.Add(pictureBoxBattleTank);
|
||||
Name = "FormBattleTank";
|
||||
Text = "FormBattleTank";
|
||||
@ -126,10 +163,13 @@
|
||||
#endregion
|
||||
|
||||
private PictureBox pictureBoxBattleTank;
|
||||
private Button Создать;
|
||||
private Button ButtonCreateBattkeTank;
|
||||
private Button buttonLeft;
|
||||
private Button buttonUp;
|
||||
private Button buttonRight;
|
||||
private Button buttonDown;
|
||||
private Button ButtonCreateTank;
|
||||
private ComboBox СomboBoxStrategy;
|
||||
private Button buttonStrategyStep;
|
||||
}
|
||||
}
|
@ -1,4 +1,8 @@
|
||||
namespace ProjectTank
|
||||
using ProjectTank.Drawning;
|
||||
using ProjectTank.Entities;
|
||||
using ProjectTank.MovementStrategy;
|
||||
|
||||
namespace ProjectTank
|
||||
{
|
||||
/// <summary>
|
||||
/// Форма работы с объектом "Танк"
|
||||
@ -8,55 +12,72 @@
|
||||
/// <summary>
|
||||
/// Поле-объект для прорисовки объекта
|
||||
/// </summary>
|
||||
private DrawningBattleTank? _drawningBattleTank;
|
||||
private DrawningTank? _drawningTank;
|
||||
|
||||
/// <summary>
|
||||
/// класс - сущность
|
||||
/// Стратегия перемещения
|
||||
/// </summary>
|
||||
private EntityBattleTank? _entityBattleTank;
|
||||
|
||||
private AbstractStrategy? _strategy;
|
||||
/// <summary>
|
||||
/// Конструктор формы
|
||||
/// </summary>
|
||||
public FormBattleTank()
|
||||
{
|
||||
InitializeComponent();
|
||||
_strategy = null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Метод прорисовки танкаx
|
||||
/// Метод прорисовки танка
|
||||
/// </summary>
|
||||
private void Draw()
|
||||
{
|
||||
if (_drawningBattleTank == null)
|
||||
if (_drawningTank == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
Bitmap bmp = new(pictureBoxBattleTank.Width, pictureBoxBattleTank.Height);
|
||||
Graphics gr = Graphics.FromImage(bmp);
|
||||
_drawningBattleTank.DrawTransport(gr);
|
||||
_drawningTank.DrawTransport(gr);
|
||||
pictureBoxBattleTank.Image = bmp;
|
||||
}
|
||||
/// <summary>
|
||||
/// Создание объекта класса-перемещения
|
||||
/// </summary>
|
||||
/// <param name="type">Тип создаваемого объекта</param>
|
||||
private void CreateObject(string type)
|
||||
{
|
||||
Random random = new Random();
|
||||
switch (type)
|
||||
{
|
||||
case nameof(DrawningTank):
|
||||
_drawningTank = new DrawningTank(random.Next(100, 300), random.Next(1000, 3000),
|
||||
Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)));
|
||||
break;
|
||||
case nameof(DrawningBattleTank):
|
||||
_drawningTank = new DrawningBattleTank(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)));
|
||||
break;
|
||||
default:
|
||||
return;
|
||||
}
|
||||
_drawningTank.SetPictureSize(pictureBoxBattleTank.Width, pictureBoxBattleTank.Height);
|
||||
_drawningTank.SetPosition(random.Next(10, 100), random.Next(10, 100));
|
||||
_strategy = null;
|
||||
СomboBoxStrategy.Enabled = true;
|
||||
Draw();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Обработка нажатия кнопки "Создать"
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="e"></param>
|
||||
private void ButtonCreate_Click(object sender, EventArgs e)
|
||||
{
|
||||
Random random = new Random();
|
||||
_drawningBattleTank = new DrawningBattleTank();
|
||||
_entityBattleTank = new EntityBattleTank();
|
||||
_entityBattleTank.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)));
|
||||
_drawningBattleTank.Init(_entityBattleTank);
|
||||
_drawningBattleTank.SetPictureSize(pictureBoxBattleTank.Width, pictureBoxBattleTank.Height);
|
||||
_drawningBattleTank.SetPosition(random.Next(10, 100), random.Next(10, 100));
|
||||
Draw();
|
||||
}
|
||||
|
||||
private void ButtonCreateButtleTank_Ckick(object sender, EventArgs e) => CreateObject(nameof(DrawningBattleTank));
|
||||
private void ButtonCreateTank_Ckick(object sender, EventArgs e) => CreateObject(nameof(DrawningTank));
|
||||
|
||||
/// <summary>
|
||||
/// Перемещение объекта по форме (нажатие кнопок навигации)
|
||||
/// </summary>
|
||||
@ -64,7 +85,7 @@
|
||||
/// <param name="e"></param>
|
||||
private void ButtonMove_Click(object sender, EventArgs e)
|
||||
{
|
||||
if (_drawningBattleTank == null)
|
||||
if (_drawningTank == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
@ -73,16 +94,16 @@
|
||||
switch (name)
|
||||
{
|
||||
case "buttonUp":
|
||||
result = _drawningBattleTank.MoveTransport(DirectionType.Up);
|
||||
result = _drawningTank.MoveTransport(DirectionType.Up);
|
||||
break;
|
||||
case "buttonLeft":
|
||||
result = _drawningBattleTank.MoveTransport(DirectionType.Left);
|
||||
result = _drawningTank.MoveTransport(DirectionType.Left);
|
||||
break;
|
||||
case "buttonRight":
|
||||
result = _drawningBattleTank.MoveTransport(DirectionType.Right);
|
||||
result = _drawningTank.MoveTransport(DirectionType.Right);
|
||||
break;
|
||||
case "buttonDown":
|
||||
result = _drawningBattleTank.MoveTransport(DirectionType.Down);
|
||||
result = _drawningTank.MoveTransport(DirectionType.Down);
|
||||
break;
|
||||
}
|
||||
if (result)
|
||||
@ -90,5 +111,45 @@
|
||||
Draw();
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// Обработка нажатия кнопки "Шаг"
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="e"></param>
|
||||
private void ButtonStrategyStep_Ckick(object sender, EventArgs e)
|
||||
{
|
||||
if (_drawningTank == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (СomboBoxStrategy.Enabled)
|
||||
{
|
||||
_strategy = СomboBoxStrategy.SelectedIndex switch
|
||||
{
|
||||
0 => new MoveToCenter(),
|
||||
1 => new MoveToBorder(),
|
||||
_ => null,
|
||||
};
|
||||
if (_strategy == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
_strategy.SetData(new MoveableTank(_drawningTank),
|
||||
pictureBoxBattleTank.Width, pictureBoxBattleTank.Height);
|
||||
}
|
||||
if (_strategy == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
СomboBoxStrategy.Enabled = false;
|
||||
_strategy.MakeStep();
|
||||
Draw();
|
||||
if (_strategy.GetStatus() == StrategyStatus.Finish)
|
||||
{
|
||||
СomboBoxStrategy.Enabled = true;
|
||||
_strategy = null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
118
ProjectTank/ProjectTank/MovementStrategy/AbstractStrategy.cs
Normal file
118
ProjectTank/ProjectTank/MovementStrategy/AbstractStrategy.cs
Normal file
@ -0,0 +1,118 @@
|
||||
namespace ProjectTank.MovementStrategy
|
||||
{
|
||||
public abstract class AbstractStrategy
|
||||
{
|
||||
/// <summary>
|
||||
/// Перемещаемый объект
|
||||
/// </summary>
|
||||
private IMoveableObject? _moveableObject;
|
||||
/// <summary>
|
||||
/// Статус перемещения
|
||||
/// </summary>
|
||||
private StrategyStatus _state = StrategyStatus.NotInit;
|
||||
/// <summary>
|
||||
/// Ширина поля
|
||||
/// </summary>
|
||||
protected int FieldWidth { get; private set; }
|
||||
/// <summary>
|
||||
/// Высота поля
|
||||
/// </summary>
|
||||
protected int FieldHeight { get; private set; }
|
||||
/// <summary>
|
||||
/// Статус перемещения
|
||||
/// </summary>
|
||||
public StrategyStatus GetStatus() { return _state; }
|
||||
/// <summary>
|
||||
/// Установка данных
|
||||
/// </summary>
|
||||
/// <param name="moveableObject">Перемещаемый объект</param>
|
||||
/// <param name="width">Ширина поля</param>
|
||||
/// <param name="height">Высота поля</param>
|
||||
public void SetData(IMoveableObject moveableObject, int width, int height)
|
||||
{
|
||||
if (moveableObject == null)
|
||||
{
|
||||
_state = StrategyStatus.NotInit;
|
||||
return;
|
||||
}
|
||||
_state = StrategyStatus.InProgress;
|
||||
_moveableObject = moveableObject;
|
||||
FieldWidth = width;
|
||||
FieldHeight = height;
|
||||
}
|
||||
/// <summary>
|
||||
/// Шаг перемещения
|
||||
/// </summary>
|
||||
public void MakeStep()
|
||||
{
|
||||
if (_state != StrategyStatus.InProgress)
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (IsTargetDestinaion())
|
||||
{
|
||||
_state = StrategyStatus.Finish;
|
||||
return;
|
||||
}
|
||||
MoveToTarget();
|
||||
}
|
||||
/// <summary>
|
||||
/// Перемещение влево
|
||||
/// </summary>
|
||||
/// <returns>Результат перемещения (true - удалось переместиться, false - неудача)</returns>
|
||||
protected bool MoveLeft() => MoveTo(MovementDirection.Left);
|
||||
/// <summary>
|
||||
/// Перемещение вправо
|
||||
/// </summary>
|
||||
/// <returns>Результат перемещения (true - удалось переместиться, false - неудача)</returns>
|
||||
protected bool MoveRight() => MoveTo(MovementDirection.Right);
|
||||
/// <summary>
|
||||
/// Перемещение вверх
|
||||
/// </summary>
|
||||
/// <returns>Результат перемещения (true - удалось переместиться, false - неудача)</returns>
|
||||
protected bool MoveUp() => MoveTo(MovementDirection.Up);
|
||||
/// <summary>
|
||||
/// Перемещение вниз
|
||||
/// </summary>
|
||||
/// <returns>Результат перемещения (true - удалось переместиться, false - неудача)</returns>
|
||||
protected bool MoveDown() => MoveTo(MovementDirection.Down);
|
||||
/// <summary>
|
||||
/// Параметры объекта
|
||||
/// </summary>
|
||||
protected ObjectParameters? GetObjectParameters => _moveableObject?.GetObjectPosition;
|
||||
/// <summary>
|
||||
/// Шаг объекта
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
protected int? GetStep()
|
||||
{
|
||||
if (_state != StrategyStatus.InProgress)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return _moveableObject?.GetStep;
|
||||
}
|
||||
/// <summary>
|
||||
/// Перемещение к цели
|
||||
/// </summary>
|
||||
protected abstract void MoveToTarget();
|
||||
/// <summary>
|
||||
/// Достигнута ли цель
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
protected abstract bool IsTargetDestinaion();
|
||||
/// <summary>
|
||||
/// Попытка перемещения в требуемом направлении
|
||||
/// </summary>
|
||||
/// <param name="movementDirection">Направление</param>
|
||||
/// <returns>Результат попытки (true - удалось переместиться, false - неудача)</returns>
|
||||
private bool MoveTo(MovementDirection movementDirection)
|
||||
{
|
||||
if (_state != StrategyStatus.InProgress)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return _moveableObject?.TryMoveObject(movementDirection) ?? false;
|
||||
}
|
||||
}
|
||||
}
|
22
ProjectTank/ProjectTank/MovementStrategy/IMoveableObject.cs
Normal file
22
ProjectTank/ProjectTank/MovementStrategy/IMoveableObject.cs
Normal file
@ -0,0 +1,22 @@
|
||||
namespace ProjectTank.MovementStrategy
|
||||
{
|
||||
public interface IMoveableObject
|
||||
{
|
||||
/// <summary>
|
||||
/// Получение координаты объекта
|
||||
/// </summary>
|
||||
|
||||
ObjectParameters? GetObjectPosition { get; }
|
||||
/// <summary>
|
||||
/// Шаг объекта
|
||||
/// </summary>
|
||||
int GetStep { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Попытка переместить объект в указанном направлении
|
||||
/// </summary>
|
||||
/// <param name="direction">Направление</param>
|
||||
/// <returns>true - объект перемещен, false - перемещение невозможно</returns>
|
||||
bool TryMoveObject(MovementDirection direction);
|
||||
}
|
||||
}
|
38
ProjectTank/ProjectTank/MovementStrategy/MoveToBorder.cs
Normal file
38
ProjectTank/ProjectTank/MovementStrategy/MoveToBorder.cs
Normal file
@ -0,0 +1,38 @@
|
||||
namespace ProjectTank.MovementStrategy;
|
||||
|
||||
internal class MoveToBorder : AbstractStrategy
|
||||
{
|
||||
/// <summary>
|
||||
/// Стратегия перемещения объекта к Краю экрана
|
||||
/// </summary>
|
||||
protected override bool IsTargetDestinaion()
|
||||
{
|
||||
ObjectParameters? objParams = GetObjectParameters;
|
||||
if (objParams == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return objParams.RightBorder + GetStep() >= FieldWidth &&
|
||||
objParams.DownBorder + GetStep() >= FieldHeight;
|
||||
}
|
||||
protected override void MoveToTarget()
|
||||
{
|
||||
ObjectParameters? objParams = GetObjectParameters;
|
||||
if (objParams == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
int diffX = objParams.RightBorder - FieldWidth;
|
||||
if (Math.Abs(diffX) > GetStep())
|
||||
{
|
||||
MoveRight();
|
||||
}
|
||||
|
||||
int diffY = objParams.DownBorder - FieldHeight;
|
||||
if (Math.Abs(diffY) > GetStep())
|
||||
{
|
||||
MoveDown();
|
||||
}
|
||||
}
|
||||
}
|
53
ProjectTank/ProjectTank/MovementStrategy/MoveToCenter.cs
Normal file
53
ProjectTank/ProjectTank/MovementStrategy/MoveToCenter.cs
Normal file
@ -0,0 +1,53 @@
|
||||
namespace ProjectTank.MovementStrategy
|
||||
{
|
||||
/// <summary>
|
||||
/// Стратегия перемещения объекта в центр экрана
|
||||
/// </summary>
|
||||
public class MoveToCenter : AbstractStrategy
|
||||
{
|
||||
protected override bool IsTargetDestinaion()
|
||||
{
|
||||
ObjectParameters? objParams = GetObjectParameters;
|
||||
if (objParams == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return objParams.ObjectMiddleHorizontal - GetStep() <= FieldWidth / 2
|
||||
&& objParams.ObjectMiddleHorizontal + GetStep() >= FieldWidth / 2 &&
|
||||
objParams.ObjectMiddleVertical - GetStep() <= FieldHeight / 2
|
||||
&& objParams.ObjectMiddleVertical + GetStep() >= FieldHeight / 2;
|
||||
}
|
||||
protected override void MoveToTarget()
|
||||
{
|
||||
ObjectParameters? objParams = GetObjectParameters;
|
||||
if (objParams == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
int diffX = objParams.ObjectMiddleHorizontal - FieldWidth / 2;
|
||||
if (Math.Abs(diffX) > GetStep())
|
||||
{
|
||||
if (diffX > 0)
|
||||
{
|
||||
MoveLeft();
|
||||
}
|
||||
else
|
||||
{
|
||||
MoveRight();
|
||||
}
|
||||
}
|
||||
int diffY = objParams.ObjectMiddleVertical - FieldHeight / 2;
|
||||
if (Math.Abs(diffY) > GetStep())
|
||||
{
|
||||
if (diffY > 0)
|
||||
{
|
||||
MoveUp();
|
||||
}
|
||||
else
|
||||
{
|
||||
MoveDown();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
58
ProjectTank/ProjectTank/MovementStrategy/MoveableTank.cs
Normal file
58
ProjectTank/ProjectTank/MovementStrategy/MoveableTank.cs
Normal file
@ -0,0 +1,58 @@
|
||||
using ProjectTank.Drawning;
|
||||
|
||||
namespace ProjectTank.MovementStrategy
|
||||
{
|
||||
public class MoveableTank : IMoveableObject
|
||||
{
|
||||
/// <summary>
|
||||
/// Поле-объект класса или его DrawningTank наследника
|
||||
/// </summary>
|
||||
private readonly DrawningTank? _tank = null;
|
||||
/// <summary>
|
||||
/// конструктор
|
||||
/// </summary>
|
||||
/// <param name="tank">Объект класса DrawningTank</param>
|
||||
public MoveableTank(DrawningTank tank)
|
||||
{
|
||||
_tank = tank;
|
||||
}
|
||||
public ObjectParameters? GetObjectPosition
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_tank == null || _tank.EntityTank == null ||
|
||||
!_tank.GetPosX.HasValue || !_tank.GetPosY.HasValue)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return new ObjectParameters(_tank.GetPosX.Value,
|
||||
_tank.GetPosY.Value, _tank.GetWidth, _tank.GetHeight);
|
||||
}
|
||||
}
|
||||
public int GetStep => (int)(_tank?.EntityTank?.Step ?? 0);
|
||||
public bool TryMoveObject(MovementDirection direction)
|
||||
{
|
||||
if (_tank == null || _tank.EntityTank == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return _tank.MoveTransport(GetDirectionType(direction));
|
||||
}
|
||||
/// <summary>
|
||||
/// Конвертация из MovementDirection в DirectionType
|
||||
/// </summary>
|
||||
/// <param name="direction">MovementDirection</param>
|
||||
/// <re turns>DirectionType</returns>
|
||||
private static DirectionType GetDirectionType(MovementDirection direction)
|
||||
{
|
||||
return direction switch
|
||||
{
|
||||
MovementDirection.Left => DirectionType.Left,
|
||||
MovementDirection.Right => DirectionType.Right,
|
||||
MovementDirection.Up => DirectionType.Up,
|
||||
MovementDirection.Down => DirectionType.Down,
|
||||
_ => DirectionType.Unknow,
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
22
ProjectTank/ProjectTank/MovementStrategy/MovementDirecton.cs
Normal file
22
ProjectTank/ProjectTank/MovementStrategy/MovementDirecton.cs
Normal file
@ -0,0 +1,22 @@
|
||||
namespace ProjectTank.MovementStrategy
|
||||
{
|
||||
public enum MovementDirection
|
||||
{
|
||||
/// <summary>
|
||||
/// Вверх
|
||||
/// </summary>
|
||||
Up = 1,
|
||||
/// <summary>
|
||||
/// Вниз
|
||||
/// </summary>
|
||||
Down = 2,
|
||||
/// <summary>
|
||||
/// Влево
|
||||
/// </summary>
|
||||
Left = 3,
|
||||
/// <summary>
|
||||
/// Вправо
|
||||
/// </summary>
|
||||
Right = 4,
|
||||
}
|
||||
}
|
60
ProjectTank/ProjectTank/MovementStrategy/ObjectParameters.cs
Normal file
60
ProjectTank/ProjectTank/MovementStrategy/ObjectParameters.cs
Normal file
@ -0,0 +1,60 @@
|
||||
namespace ProjectTank.MovementStrategy
|
||||
{
|
||||
public class ObjectParameters
|
||||
{
|
||||
/// <summary>
|
||||
/// Координата X
|
||||
/// </summary>
|
||||
private readonly int _x;
|
||||
/// <summary>
|
||||
/// Координата Y
|
||||
/// </summary>
|
||||
private readonly int _y;
|
||||
/// <summary>
|
||||
/// Ширина объекта
|
||||
/// </summary>
|
||||
private readonly int _width;
|
||||
/// <summary>
|
||||
/// Высота объекта
|
||||
/// </summary>
|
||||
private readonly int _height;
|
||||
/// <summary>
|
||||
/// Левая граница
|
||||
/// </summary>
|
||||
public int LeftBorder => _x;
|
||||
/// <summary>
|
||||
/// Верхняя граница
|
||||
/// </summary>
|
||||
public int TopBorder => _y;
|
||||
/// <summary>
|
||||
/// Правая граница
|
||||
/// </summary>
|
||||
public int RightBorder => _x + _width;
|
||||
/// <summary>
|
||||
/// Нижняя граница
|
||||
/// </summary>
|
||||
public int DownBorder => _y + _height;
|
||||
/// <summary>
|
||||
/// Середина объекта
|
||||
/// </summary>
|
||||
public int ObjectMiddleHorizontal => _x + _width / 2;
|
||||
/// <summary>
|
||||
/// Середина объекта
|
||||
/// </summary>
|
||||
public int ObjectMiddleVertical => _y + _height / 2;
|
||||
/// <summary>
|
||||
/// Конструктор
|
||||
/// </summary>
|
||||
/// <param name="x">Координата X</param>
|
||||
/// <param name="y">Координата Y</param>
|
||||
/// <param name="width">Ширина объекта</param>
|
||||
/// <param name="height">Высота объекта</param>
|
||||
public ObjectParameters(int x, int y, int width, int height)
|
||||
{
|
||||
_x = x;
|
||||
_y = y;
|
||||
_width = width;
|
||||
_height = height;
|
||||
}
|
||||
}
|
||||
}
|
18
ProjectTank/ProjectTank/MovementStrategy/StrategyStatus.cs
Normal file
18
ProjectTank/ProjectTank/MovementStrategy/StrategyStatus.cs
Normal file
@ -0,0 +1,18 @@
|
||||
namespace ProjectTank.MovementStrategy
|
||||
{
|
||||
public enum StrategyStatus
|
||||
{
|
||||
/// <summary>
|
||||
/// Все готово к началу
|
||||
/// </summary>
|
||||
NotInit,
|
||||
/// <summary>
|
||||
/// Выполняется
|
||||
/// </summary>
|
||||
InProgress,
|
||||
/// <summary>
|
||||
/// Завершино
|
||||
/// </summary>
|
||||
Finish
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user
Имя элемента проекта не соответствует указанному в задании