LabWork2
This commit is contained in:
parent
eb93d49c6b
commit
03402dd7cf
127
ProjectBattleship/ProjectBattleship/AbstractStrategy.cs
Normal file
127
ProjectBattleship/ProjectBattleship/AbstractStrategy.cs
Normal file
@ -0,0 +1,127 @@
|
||||
using ProjectBattleship.MovementStrategy;
|
||||
|
||||
namespace ProjectBattleship.MovementStrategy;
|
||||
/// <summary>
|
||||
/// Класс-стратегия перемещения объекта
|
||||
/// </summary>
|
||||
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;
|
||||
}
|
||||
}
|
@ -4,6 +4,10 @@
|
||||
/// </summary>
|
||||
public enum DirectionType
|
||||
{
|
||||
/// <summary>
|
||||
/// Неизвестное направление
|
||||
/// </summary>
|
||||
Unknow = -1,
|
||||
/// <summary>
|
||||
/// Вверх
|
||||
/// </summary>
|
||||
|
@ -1,228 +1,68 @@
|
||||
namespace ProjectBattleship;
|
||||
using ProjectBattleship.Entities;
|
||||
namespace ProjectBattleship.DrawingObject;
|
||||
/// <summary>
|
||||
/// Класс, отвечающий за прорисовку и перемещение объекта-сущности
|
||||
/// </summary>
|
||||
public class DrawingBattleship
|
||||
public class DrawingBattleship : DrawingWarship
|
||||
{
|
||||
/// <summary>
|
||||
/// Класс-сущность
|
||||
/// </summary>
|
||||
public EntityBattleship? EntityBattleship { 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 _drawingWarshipWidth = 150;
|
||||
/// <summary>
|
||||
/// Высота прорисовки корабля
|
||||
/// </summary>
|
||||
private readonly int _drawingWarshipHeight = 50;
|
||||
/// <summary>
|
||||
/// Инициализация свойств
|
||||
/// Конструктор
|
||||
/// </summary>
|
||||
/// <param name="speed">Скорость</param>
|
||||
/// <param name="weight">Вес</param>
|
||||
/// <param name="bodyColor">Основной цвет</param>
|
||||
/// <param name="additionalColor">Дополнительный цвет</param>
|
||||
/// <param name="turret">Признак наличия отсека под ракеты</param>
|
||||
/// <param name="rocketCompartment">Признак наличия орудийной башни</param>
|
||||
public void Init(int speed, double weight, Color bodyColor, Color
|
||||
additionalColor, bool turret, bool rocketCompartment)
|
||||
/// <param name="bodyKit">Признак наличия обвеса</param>
|
||||
/// <param name="wing">Признак наличия антикрыла</param>
|
||||
/// <param name="sportLine">Признак наличия гоночной полосы</param>
|
||||
public DrawingBattleship(int speed, double weight, Color bodyColor, Color
|
||||
additionalColor, bool turret, bool rocketCompartment) : base(110, 60)
|
||||
{
|
||||
EntityBattleship = new EntityBattleship();
|
||||
EntityBattleship.Init(speed, weight, bodyColor, additionalColor,
|
||||
turret, rocketCompartment);
|
||||
_pictureWidth = null;
|
||||
_pictureHeight = null;
|
||||
_startPosX = null;
|
||||
_startPosY = null;
|
||||
EntityWarship = new EntityBattleship(speed, weight, bodyColor, additionalColor,
|
||||
turret, rocketCompartment);
|
||||
}
|
||||
/// <summary>
|
||||
/// Установка границ поля
|
||||
/// </summary>
|
||||
/// <param name="width">Ширина поля</param>
|
||||
/// <param name="height">Высота поля</param>
|
||||
/// <returns>true - границы заданы, false - проверка не пройдена,
|
||||
/// нельзя разместить объект в этих размерах</returns>
|
||||
public bool SetPictureSize(int width, int height)
|
||||
public override void DrawTransport(Graphics g)
|
||||
{
|
||||
if (_drawingWarshipWidth < width && _drawingWarshipHeight < height)
|
||||
{
|
||||
_pictureWidth = width;
|
||||
_pictureHeight = height;
|
||||
if (_startPosX.HasValue && _startPosY.HasValue)
|
||||
{
|
||||
SetPosition(_startPosX.Value, _startPosY.Value);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Установка позиции
|
||||
/// </summary>
|
||||
/// <param name="x">Координата X</param>
|
||||
/// <param name="y">Координата Y</param>
|
||||
public void SetPosition(int x, int y)
|
||||
{
|
||||
if (!_pictureHeight.HasValue || !_pictureWidth.HasValue)
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (x > 0 && y > 0 && x + _drawingWarshipWidth < _pictureWidth
|
||||
&& y + _drawingWarshipHeight < _pictureHeight)
|
||||
{
|
||||
_startPosX = x;
|
||||
_startPosY = y;
|
||||
}
|
||||
else
|
||||
{
|
||||
Random rnd = new();
|
||||
_startPosX = rnd.Next(0, _pictureWidth.Value -
|
||||
_drawingWarshipWidth);
|
||||
_startPosY = rnd.Next(0, _pictureHeight.Value -
|
||||
_drawingWarshipHeight);
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// Изменение направления перемещения
|
||||
/// </summary>
|
||||
/// <param name="direction">Направление</param>
|
||||
/// <returns>true - перемещене выполнено, false - перемещение невозможно</returns>
|
||||
public bool MoveTransport(DirectionType direction)
|
||||
{
|
||||
if (EntityBattleship == null || !_startPosX.HasValue ||
|
||||
!_startPosY.HasValue)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
switch (direction)
|
||||
{
|
||||
//влево
|
||||
case DirectionType.Left:
|
||||
if (_startPosX - EntityBattleship.Step > 0)
|
||||
{
|
||||
_startPosX -= (int)EntityBattleship.Step;
|
||||
}
|
||||
return true;
|
||||
//вверх
|
||||
case DirectionType.Up:
|
||||
if (_startPosY - EntityBattleship.Step > 0)
|
||||
{
|
||||
_startPosY -= (int)EntityBattleship.Step;
|
||||
}
|
||||
return true;
|
||||
//вправо
|
||||
case DirectionType.Right:
|
||||
if (_startPosX + _drawingWarshipWidth + EntityBattleship.Step < _pictureWidth)
|
||||
{
|
||||
_startPosX += (int)EntityBattleship.Step;
|
||||
}
|
||||
return true;
|
||||
//вниз
|
||||
case DirectionType.Down:
|
||||
if (_startPosY + _drawingWarshipHeight + EntityBattleship.Step < _pictureHeight)
|
||||
{
|
||||
_startPosY += (int)EntityBattleship.Step;
|
||||
}
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// Прорисовка объекта
|
||||
/// </summary>
|
||||
/// <param name="g"></param>
|
||||
public void DrawTransport(Graphics g)
|
||||
{
|
||||
if (EntityBattleship == null || !_startPosX.HasValue ||
|
||||
!_startPosY.HasValue)
|
||||
if (EntityWarship == null || EntityWarship is not EntityBattleship sportWarship ||
|
||||
!_startPosX.HasValue || !_startPosY.HasValue)
|
||||
{
|
||||
return;
|
||||
}
|
||||
Pen pen = new(Color.Black);
|
||||
Brush bodyBrush = new SolidBrush(EntityBattleship.BodyColor);
|
||||
Brush additionalBrush = new
|
||||
SolidBrush(EntityBattleship.AdditionalColor);
|
||||
//основная часть
|
||||
Point[] body = new Point[] {new Point(_startPosX.Value + 5,
|
||||
_startPosY.Value), new Point(_startPosX.Value + 100,
|
||||
_startPosY.Value), new Point(_startPosX.Value + 150,
|
||||
_startPosY.Value + 25), new Point(_startPosX.Value + 100,
|
||||
_startPosY.Value + 50), new Point(_startPosX.Value + 5,
|
||||
_startPosY.Value + 50)};
|
||||
g.FillPolygon(bodyBrush, body);
|
||||
g.DrawPolygon(pen, body);
|
||||
Brush brBlack = new SolidBrush(Color.Black);
|
||||
g.FillRectangle(brBlack, _startPosX.Value,
|
||||
_startPosY.Value + 6, 5, 13);
|
||||
g.FillRectangle(brBlack, _startPosX.Value,
|
||||
_startPosY.Value + 31, 5, 13);
|
||||
Brush brDark = new SolidBrush(Color.DarkGray);
|
||||
g.FillRectangle(brDark, _startPosX.Value + 39,
|
||||
_startPosY.Value + 20, 40, 10);
|
||||
g.DrawRectangle(pen, _startPosX.Value + 39,
|
||||
_startPosY.Value + 20, 40, 10);
|
||||
g.FillRectangle(brDark, _startPosX.Value + 70,
|
||||
_startPosY.Value + 12, 18, 26);
|
||||
g.DrawRectangle(pen, _startPosX.Value + 70,
|
||||
_startPosY.Value + 12, 18, 26);
|
||||
g.FillEllipse(brBlack, _startPosX.Value + 94,
|
||||
_startPosY.Value + 19, 12, 12);
|
||||
Brush additionalBrush = new SolidBrush(sportWarship.AdditionalColor);
|
||||
//отсек под ракеты
|
||||
if (EntityBattleship.RocketCompartment)
|
||||
{
|
||||
g.FillRectangle(additionalBrush, _startPosX.Value + 14,
|
||||
g.FillRectangle(additionalBrush, _startPosX.Value + 14,
|
||||
_startPosY.Value + 14, 10, 10);
|
||||
g.FillRectangle(additionalBrush, _startPosX.Value + 26,
|
||||
g.FillRectangle(additionalBrush, _startPosX.Value + 26,
|
||||
_startPosY.Value + 14, 10, 10);
|
||||
g.FillRectangle(additionalBrush, _startPosX.Value + 14,
|
||||
g.FillRectangle(additionalBrush, _startPosX.Value + 14,
|
||||
_startPosY.Value + 26, 10, 10);
|
||||
g.FillRectangle(additionalBrush, _startPosX.Value + 26,
|
||||
g.FillRectangle(additionalBrush, _startPosX.Value + 26,
|
||||
_startPosY.Value + 26, 10, 10);
|
||||
g.DrawRectangle(pen, _startPosX.Value + 14,
|
||||
g.DrawRectangle(pen, _startPosX.Value + 14,
|
||||
_startPosY.Value + 14, 10, 10);
|
||||
g.DrawRectangle(pen, _startPosX.Value + 26,
|
||||
g.DrawRectangle(pen, _startPosX.Value + 26,
|
||||
_startPosY.Value + 14, 10, 10);
|
||||
g.DrawRectangle(pen, _startPosX.Value + 14,
|
||||
g.DrawRectangle(pen, _startPosX.Value + 14,
|
||||
_startPosY.Value + 26, 10, 10);
|
||||
g.DrawRectangle(pen, _startPosX.Value + 26,
|
||||
g.DrawRectangle(pen, _startPosX.Value + 26,
|
||||
_startPosY.Value + 26, 10, 10);
|
||||
}
|
||||
//орудийная башня
|
||||
if (EntityBattleship.Turret)
|
||||
{
|
||||
Point[] turret = new Point[] {new Point(_startPosX.Value + 112,
|
||||
_startPosY.Value + 19), new Point(_startPosX.Value + 112,
|
||||
_startPosY.Value + 31), new Point(_startPosX.Value + 119,
|
||||
_startPosY.Value + 28), new Point(_startPosX.Value + 119,
|
||||
Point[] turret = new Point[] {new Point(_startPosX.Value + 112,
|
||||
_startPosY.Value + 19), new Point(_startPosX.Value + 112,
|
||||
_startPosY.Value + 31), new Point(_startPosX.Value + 119,
|
||||
_startPosY.Value + 28), new Point(_startPosX.Value + 119,
|
||||
_startPosY.Value + 22)};
|
||||
g.FillPolygon(additionalBrush, turret);
|
||||
g.FillRectangle(additionalBrush, _startPosX.Value + 119,
|
||||
g.FillRectangle(additionalBrush, _startPosX.Value + 119,
|
||||
_startPosY.Value + 24, 12, 2);
|
||||
g.DrawPolygon(pen, turret);
|
||||
g.DrawRectangle(pen, _startPosX.Value + 119,
|
||||
g.DrawRectangle(pen, _startPosX.Value + 119,
|
||||
_startPosY.Value + 24, 12, 2);
|
||||
}
|
||||
}
|
||||
|
197
ProjectBattleship/ProjectBattleship/DrawingWarship.cs
Normal file
197
ProjectBattleship/ProjectBattleship/DrawingWarship.cs
Normal file
@ -0,0 +1,197 @@
|
||||
using ProjectBattleship;
|
||||
using ProjectBattleship.Entities;
|
||||
namespace ProjectBattleship.DrawingObject;
|
||||
/// <summary>
|
||||
/// Класс, отвечающий за прорисовку и перемещение базового объекта-сущности
|
||||
/// </summary>
|
||||
public class DrawingWarship
|
||||
{
|
||||
/// <summary>
|
||||
/// Класс-сущность
|
||||
/// </summary>
|
||||
public EntityWarship? EntityWarship { 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 _drawningWarshipWidth = 90;
|
||||
/// <summary>
|
||||
/// Высота прорисовки военного корабля
|
||||
/// </summary>
|
||||
private readonly int _drawningWarshipHeight = 50;
|
||||
/// <summary>
|
||||
/// Пустой конструктор
|
||||
/// </summary>
|
||||
private DrawingWarship()
|
||||
{
|
||||
_pictureWidth = null;
|
||||
_pictureHeight = null;
|
||||
_startPosX = null;
|
||||
_startPosY = null;
|
||||
}
|
||||
/// <summary>
|
||||
/// Конструктор
|
||||
/// </summary>
|
||||
/// <param name="speed">Скорость</param>
|
||||
/// <param name="weight">Вес</param>
|
||||
/// <param name="bodyColor">Основной цвет</param>
|
||||
public DrawingWarship(int speed, double weight, Color bodyColor) : this()
|
||||
{
|
||||
EntityWarship = new EntityWarship(speed, weight, bodyColor);
|
||||
}
|
||||
/// <summary>
|
||||
/// Конструктор для наследников
|
||||
/// </summary>
|
||||
/// <param name="drawningWarshipWidth">Ширина прорисовки военного корабля</param>
|
||||
/// <param name="drawningWarshipHeight">Высота прорисовки военного корабля</param>
|
||||
protected DrawingWarship(int drawningWarshipWidth, int drawningWarshipHeight) : this()
|
||||
{
|
||||
_drawningWarshipWidth = drawningWarshipWidth;
|
||||
_pictureHeight = drawningWarshipHeight;
|
||||
}
|
||||
/// <summary>
|
||||
/// Установка границ поля
|
||||
/// </summary>
|
||||
/// <param name="width">Ширина поля</param>
|
||||
/// <param name="height">Высота поля</param>
|
||||
/// <returns>true - границы заданы, false - проверка не пройдена, нельзя
|
||||
//разместить объект в этих размерах</returns>
|
||||
public bool SetPictureSize(int width, int height)
|
||||
{
|
||||
// TODO проверка, что объект "влезает" в размеры поля
|
||||
// если влезает, сохраняем границы и корректируем позицию объекта,
|
||||
//если она была уже установлена
|
||||
_pictureWidth = width;
|
||||
_pictureHeight = height;
|
||||
return true;
|
||||
}
|
||||
/// <summary>
|
||||
/// Установка позиции
|
||||
/// </summary>
|
||||
/// <param name="x">Координата X</param>
|
||||
/// <param name="y">Координата Y</param>
|
||||
public void SetPosition(int x, int y)
|
||||
{
|
||||
if (!_pictureHeight.HasValue || !_pictureWidth.HasValue)
|
||||
{
|
||||
return;
|
||||
}
|
||||
// TODO если при установке объекта в эти координаты, он будет
|
||||
//"выходить" за границы формы
|
||||
// то надо изменить координаты, чтобы он оставался в этих границах
|
||||
_startPosX = x;
|
||||
_startPosY = y;
|
||||
}
|
||||
/// <summary>
|
||||
/// Изменение направления перемещения
|
||||
/// </summary>
|
||||
/// <param name="direction">Направление</param>
|
||||
/// <returns>true - перемещене выполнено, false - перемещение
|
||||
///невозможно</returns>
|
||||
public bool MoveTransport(DirectionType direction)
|
||||
{
|
||||
if (EntityWarship == null || !_startPosX.HasValue ||
|
||||
!_startPosY.HasValue)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
switch (direction)
|
||||
{
|
||||
//влево
|
||||
case DirectionType.Left:
|
||||
if (_startPosX.Value - EntityWarship.Step > 0)
|
||||
{
|
||||
_startPosX -= (int)EntityWarship.Step;
|
||||
}
|
||||
return true;
|
||||
//вверх
|
||||
case DirectionType.Up:
|
||||
if (_startPosY.Value - EntityWarship.Step > 0)
|
||||
{
|
||||
_startPosY -= (int)EntityWarship.Step;
|
||||
}
|
||||
return true;
|
||||
// вправо
|
||||
case DirectionType.Right:
|
||||
//TODO прописать логику сдвига в право
|
||||
return true;
|
||||
//вниз
|
||||
case DirectionType.Down:
|
||||
//TODO прописать логику сдвига в вниз
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// Прорисовка объекта
|
||||
/// </summary>
|
||||
/// <param name="g"></param>
|
||||
public virtual void DrawTransport(Graphics g)
|
||||
{
|
||||
if (EntityWarship == null || !_startPosX.HasValue ||
|
||||
!_startPosY.HasValue)
|
||||
{
|
||||
return;
|
||||
}
|
||||
Pen pen = new(Color.Black);
|
||||
Brush bodyBrush = new SolidBrush(EntityWarship.BodyColor);
|
||||
//основная часть
|
||||
Point[] body = new Point[] {new Point(_startPosX.Value + 5,
|
||||
_startPosY.Value), new Point(_startPosX.Value + 100,
|
||||
_startPosY.Value), new Point(_startPosX.Value + 150,
|
||||
_startPosY.Value + 25), new Point(_startPosX.Value + 100,
|
||||
_startPosY.Value + 50), new Point(_startPosX.Value + 5,
|
||||
_startPosY.Value + 50)};
|
||||
g.FillPolygon(bodyBrush, body);
|
||||
g.DrawPolygon(pen, body);
|
||||
Brush brBlack = new SolidBrush(Color.Black);
|
||||
g.FillRectangle(brBlack, _startPosX.Value,
|
||||
_startPosY.Value + 6, 5, 13);
|
||||
g.FillRectangle(brBlack, _startPosX.Value,
|
||||
_startPosY.Value + 31, 5, 13);
|
||||
Brush brDark = new SolidBrush(Color.DarkGray);
|
||||
g.FillRectangle(brDark, _startPosX.Value + 39,
|
||||
_startPosY.Value + 20, 40, 10);
|
||||
g.DrawRectangle(pen, _startPosX.Value + 39,
|
||||
_startPosY.Value + 20, 40, 10);
|
||||
g.FillRectangle(brDark, _startPosX.Value + 70,
|
||||
_startPosY.Value + 12, 18, 26);
|
||||
g.DrawRectangle(pen, _startPosX.Value + 70,
|
||||
_startPosY.Value + 12, 18, 26);
|
||||
g.FillEllipse(brBlack, _startPosX.Value + 94,
|
||||
_startPosY.Value + 19, 12, 12);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Координата X объекта
|
||||
/// </summary>
|
||||
public int? GetPosX => _startPosX;
|
||||
/// <summary>
|
||||
/// Координата Y объекта
|
||||
/// </summary>
|
||||
public int? GetPosY => _startPosY;
|
||||
/// <summary>
|
||||
/// Ширина объекта
|
||||
/// </summary>
|
||||
public int GetWidth => _drawningWarshipWidth;
|
||||
/// <summary>
|
||||
/// Высота объекта
|
||||
/// </summary>
|
||||
public int GetHeight => _drawningWarshipHeight;
|
||||
}
|
@ -1,4 +1,6 @@
|
||||
namespace ProjectBattleship;
|
||||
using ProjectBattleship.Entities;
|
||||
|
||||
namespace ProjectBattleship.Entities;
|
||||
/// <summary>
|
||||
/// Класс-сущность "Линкор"
|
||||
/// </summary>
|
||||
|
35
ProjectBattleship/ProjectBattleship/EntityWarship.cs
Normal file
35
ProjectBattleship/ProjectBattleship/EntityWarship.cs
Normal file
@ -0,0 +1,35 @@
|
||||
namespace ProjectBattleship.Entities;
|
||||
/// <summary>
|
||||
/// Класс-сущность "Автомобиль"
|
||||
/// </summary>
|
||||
public class EntityWarship
|
||||
{
|
||||
/// <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">Вес автомобиля</param>
|
||||
/// <param name="bodyColor">Основной цвет</param>
|
||||
public EntityWarship(int speed, double weight, Color bodyColor)
|
||||
{
|
||||
Speed = speed;
|
||||
Weight = weight;
|
||||
BodyColor = bodyColor;
|
||||
}
|
||||
}
|
@ -29,11 +29,14 @@
|
||||
private void InitializeComponent()
|
||||
{
|
||||
pictureBoxBattleship = new PictureBox();
|
||||
buttonCreate = new Button();
|
||||
buttonCreateBattleship = new Button();
|
||||
buttonLeft = new Button();
|
||||
buttonDown = new Button();
|
||||
buttonRight = new Button();
|
||||
buttonUp = new Button();
|
||||
comboBoxStrategy = new ComboBox();
|
||||
button1 = new Button();
|
||||
buttonCreateWarship = new Button();
|
||||
((System.ComponentModel.ISupportInitialize)pictureBoxBattleship).BeginInit();
|
||||
SuspendLayout();
|
||||
//
|
||||
@ -41,33 +44,33 @@
|
||||
//
|
||||
pictureBoxBattleship.Dock = DockStyle.Fill;
|
||||
pictureBoxBattleship.Location = new Point(0, 0);
|
||||
pictureBoxBattleship.Margin = new Padding(2, 2, 2, 2);
|
||||
pictureBoxBattleship.Margin = new Padding(2);
|
||||
pictureBoxBattleship.Name = "pictureBoxBattleship";
|
||||
pictureBoxBattleship.Size = new Size(730, 363);
|
||||
pictureBoxBattleship.Size = new Size(876, 436);
|
||||
pictureBoxBattleship.TabIndex = 0;
|
||||
pictureBoxBattleship.TabStop = false;
|
||||
//
|
||||
// buttonCreate
|
||||
// buttonCreateBattleship
|
||||
//
|
||||
buttonCreate.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
|
||||
buttonCreate.Location = new Point(10, 320);
|
||||
buttonCreate.Margin = new Padding(2, 2, 2, 2);
|
||||
buttonCreate.Name = "buttonCreate";
|
||||
buttonCreate.Size = new Size(109, 33);
|
||||
buttonCreate.TabIndex = 1;
|
||||
buttonCreate.Text = "Создать ";
|
||||
buttonCreate.UseVisualStyleBackColor = true;
|
||||
buttonCreate.Click += ButtonCreateBattleship_Click;
|
||||
buttonCreateBattleship.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
|
||||
buttonCreateBattleship.Location = new Point(11, 384);
|
||||
buttonCreateBattleship.Margin = new Padding(2);
|
||||
buttonCreateBattleship.Name = "buttonCreateBattleship";
|
||||
buttonCreateBattleship.Size = new Size(201, 40);
|
||||
buttonCreateBattleship.TabIndex = 1;
|
||||
buttonCreateBattleship.Text = "Создать Линкор";
|
||||
buttonCreateBattleship.UseVisualStyleBackColor = true;
|
||||
buttonCreateBattleship.Click += ButtonCreateBattleship_Click;
|
||||
//
|
||||
// buttonLeft
|
||||
//
|
||||
buttonLeft.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
|
||||
buttonLeft.BackgroundImage = Properties.Resources.arrowLeft;
|
||||
buttonLeft.BackgroundImageLayout = ImageLayout.Stretch;
|
||||
buttonLeft.Location = new Point(616, 324);
|
||||
buttonLeft.Margin = new Padding(2, 2, 2, 2);
|
||||
buttonLeft.Location = new Point(739, 389);
|
||||
buttonLeft.Margin = new Padding(2);
|
||||
buttonLeft.Name = "buttonLeft";
|
||||
buttonLeft.Size = new Size(29, 29);
|
||||
buttonLeft.Size = new Size(35, 35);
|
||||
buttonLeft.TabIndex = 2;
|
||||
buttonLeft.UseVisualStyleBackColor = true;
|
||||
buttonLeft.Click += ButtonMove_Click;
|
||||
@ -77,10 +80,10 @@
|
||||
buttonDown.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
|
||||
buttonDown.BackgroundImage = Properties.Resources.arrowDown;
|
||||
buttonDown.BackgroundImageLayout = ImageLayout.Stretch;
|
||||
buttonDown.Location = new Point(650, 324);
|
||||
buttonDown.Margin = new Padding(2, 2, 2, 2);
|
||||
buttonDown.Location = new Point(780, 389);
|
||||
buttonDown.Margin = new Padding(2);
|
||||
buttonDown.Name = "buttonDown";
|
||||
buttonDown.Size = new Size(29, 29);
|
||||
buttonDown.Size = new Size(35, 35);
|
||||
buttonDown.TabIndex = 3;
|
||||
buttonDown.UseVisualStyleBackColor = true;
|
||||
buttonDown.Click += ButtonMove_Click;
|
||||
@ -90,10 +93,10 @@
|
||||
buttonRight.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
|
||||
buttonRight.BackgroundImage = Properties.Resources.arrowRight;
|
||||
buttonRight.BackgroundImageLayout = ImageLayout.Stretch;
|
||||
buttonRight.Location = new Point(684, 324);
|
||||
buttonRight.Margin = new Padding(2, 2, 2, 2);
|
||||
buttonRight.Location = new Point(821, 389);
|
||||
buttonRight.Margin = new Padding(2);
|
||||
buttonRight.Name = "buttonRight";
|
||||
buttonRight.Size = new Size(29, 29);
|
||||
buttonRight.Size = new Size(35, 35);
|
||||
buttonRight.TabIndex = 4;
|
||||
buttonRight.UseVisualStyleBackColor = true;
|
||||
buttonRight.Click += ButtonMove_Click;
|
||||
@ -103,26 +106,56 @@
|
||||
buttonUp.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
|
||||
buttonUp.BackgroundImage = Properties.Resources.arrowUp;
|
||||
buttonUp.BackgroundImageLayout = ImageLayout.Stretch;
|
||||
buttonUp.Location = new Point(650, 290);
|
||||
buttonUp.Margin = new Padding(2, 2, 2, 2);
|
||||
buttonUp.Location = new Point(780, 348);
|
||||
buttonUp.Margin = new Padding(2);
|
||||
buttonUp.Name = "buttonUp";
|
||||
buttonUp.Size = new Size(29, 29);
|
||||
buttonUp.Size = new Size(35, 35);
|
||||
buttonUp.TabIndex = 5;
|
||||
buttonUp.UseVisualStyleBackColor = true;
|
||||
buttonUp.Click += ButtonMove_Click;
|
||||
//
|
||||
// comboBoxStrategy
|
||||
//
|
||||
comboBoxStrategy.FormattingEnabled = true;
|
||||
comboBoxStrategy.Location = new Point(652, 12);
|
||||
comboBoxStrategy.Name = "comboBoxStrategy";
|
||||
comboBoxStrategy.Size = new Size(212, 38);
|
||||
comboBoxStrategy.TabIndex = 6;
|
||||
//
|
||||
// button1
|
||||
//
|
||||
button1.Location = new Point(752, 72);
|
||||
button1.Name = "button1";
|
||||
button1.Size = new Size(104, 43);
|
||||
button1.TabIndex = 7;
|
||||
button1.Text = "шаг";
|
||||
button1.TextAlign = ContentAlignment.TopCenter;
|
||||
button1.UseVisualStyleBackColor = true;
|
||||
//
|
||||
// buttonCreateWarship
|
||||
//
|
||||
buttonCreateWarship.Location = new Point(217, 384);
|
||||
buttonCreateWarship.Name = "buttonCreateWarship";
|
||||
buttonCreateWarship.Size = new Size(200, 40);
|
||||
buttonCreateWarship.TabIndex = 8;
|
||||
buttonCreateWarship.Text = "Создать корабль";
|
||||
buttonCreateWarship.UseVisualStyleBackColor = true;
|
||||
//
|
||||
// FormBattleship
|
||||
//
|
||||
AutoScaleDimensions = new SizeF(10F, 25F);
|
||||
AutoScaleDimensions = new SizeF(12F, 30F);
|
||||
AutoScaleMode = AutoScaleMode.Font;
|
||||
ClientSize = new Size(730, 363);
|
||||
ClientSize = new Size(876, 436);
|
||||
Controls.Add(buttonCreateWarship);
|
||||
Controls.Add(button1);
|
||||
Controls.Add(comboBoxStrategy);
|
||||
Controls.Add(buttonUp);
|
||||
Controls.Add(buttonRight);
|
||||
Controls.Add(buttonDown);
|
||||
Controls.Add(buttonLeft);
|
||||
Controls.Add(buttonCreate);
|
||||
Controls.Add(buttonCreateBattleship);
|
||||
Controls.Add(pictureBoxBattleship);
|
||||
Margin = new Padding(2, 2, 2, 2);
|
||||
Margin = new Padding(2);
|
||||
Name = "FormBattleship";
|
||||
StartPosition = FormStartPosition.CenterScreen;
|
||||
Text = "Линкор";
|
||||
@ -134,10 +167,13 @@
|
||||
#endregion
|
||||
|
||||
private PictureBox pictureBoxBattleship;
|
||||
private Button buttonCreate;
|
||||
private Button buttonCreateBattleship;
|
||||
private Button buttonLeft;
|
||||
private Button buttonDown;
|
||||
private Button buttonRight;
|
||||
private Button buttonUp;
|
||||
private ComboBox comboBoxStrategy;
|
||||
private Button button1;
|
||||
private Button buttonCreateWarship;
|
||||
}
|
||||
}
|
@ -1,55 +1,93 @@
|
||||
using ProjectBattleship.MovementStrategy;
|
||||
using ProjectBattleship;
|
||||
using ProjectBattleship.DrawingObject;
|
||||
using ProjectBattleship.MovementStrategy;
|
||||
namespace ProjectBattleship;
|
||||
/// <summary>
|
||||
/// Ôîðìà ðàáîòû ñ îáúåêòîì "Âîåííûé êîðàáëü"
|
||||
/// Ôîðìà ðàáîòû ñ îáúåêòîì "Ñïîðòèâíûé àâòîìîáèëü"
|
||||
/// </summary>
|
||||
public partial class FormBattleship : Form
|
||||
{
|
||||
/// <summary>
|
||||
/// Ïîëå-îáúåêò äëÿ ïðîðèñîâêè îáúåêòà
|
||||
/// </summary>
|
||||
private DrawingBattleship? _drawingBattleship;
|
||||
private DrawingWarship? _drawningWarship;
|
||||
/// <summary>
|
||||
/// Ñòðàòåãèÿ ïåðåìåùåíèÿ
|
||||
/// </summary>
|
||||
private AbstractStrategy? _strategy;
|
||||
/// <summary>
|
||||
/// Êîíñòðóêòîð ôîðìû
|
||||
/// </summary>
|
||||
public FormBattleship()
|
||||
{
|
||||
InitializeComponent();
|
||||
_strategy = null;
|
||||
}
|
||||
/// <summary>
|
||||
/// Ìåòîä ïðîðèñîâêè êîðàáëÿ
|
||||
/// Ìåòîä ïðîðèñîâêè ìàøèíû
|
||||
/// </summary>
|
||||
private void Draw()
|
||||
{
|
||||
if (_drawingBattleship == null)
|
||||
if (_drawningWarship == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
Bitmap bmp = new(pictureBoxBattleship.Width,
|
||||
pictureBoxBattleship.Height);
|
||||
pictureBoxBattleship.Height);
|
||||
Graphics gr = Graphics.FromImage(bmp);
|
||||
_drawingBattleship.DrawTransport(gr);
|
||||
_drawningWarship.DrawTransport(gr);
|
||||
pictureBoxBattleship.Image = bmp;
|
||||
}
|
||||
/// <summary>
|
||||
/// Îáðàáîòêà íàæàòèÿ êíîïêè "Ñîçäàòü"
|
||||
/// Ñîçäàíèå îáúåêòà êëàññà-ïåðåìåùåíèÿ
|
||||
/// </summary>
|
||||
/// <param name="type">Òèï ñîçäàâàåìîãî îáúåêòà</param>
|
||||
private void CreateObject(string type)
|
||||
{
|
||||
Random random = new();
|
||||
switch (type)
|
||||
{
|
||||
case nameof(DrawingWarship):
|
||||
_drawningWarship = new DrawingWarship(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(DrawingBattleship):
|
||||
_drawningWarship = new DrawingBattleship(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;
|
||||
}
|
||||
_drawningWarship.SetPictureSize(pictureBoxBattleship.Width,
|
||||
pictureBoxBattleship.Height);
|
||||
_drawningWarship.SetPosition(random.Next(10, 100), random.Next(10, 100));
|
||||
_strategy = null;
|
||||
comboBoxStrategy.Enabled = true;
|
||||
Draw();
|
||||
}
|
||||
/// <summary>
|
||||
/// Îáðàáîòêà íàæàòèÿ êíîïêè "Ñîçäàòü ñïîðòèâíûé àâòîìîáèëü"
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="e"></param>
|
||||
private void ButtonCreateBattleship_Click(object sender, EventArgs e)
|
||||
{
|
||||
Random random = new();
|
||||
_drawingBattleship = new DrawingBattleship();
|
||||
_drawingBattleship.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)));
|
||||
_drawingBattleship.SetPictureSize(pictureBoxBattleship.Width,
|
||||
pictureBoxBattleship.Height);
|
||||
_drawingBattleship.SetPosition(random.Next(10, 100),
|
||||
random.Next(10, 100));
|
||||
Draw();
|
||||
}
|
||||
private void ButtonCreateBattleship_Click(object sender, EventArgs e) =>
|
||||
CreateObject(nameof(DrawingBattleship));
|
||||
/// <summary>
|
||||
/// Îáðàáîòêà íàæàòèÿ êíîïêè "Ñîçäàòü àâòîìîáèëü"
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="e"></param>
|
||||
private void ButtonCreateWarship_Click(object sender, EventArgs e) =>
|
||||
CreateObject(nameof(DrawingWarship));
|
||||
/// <summary>
|
||||
/// Ïåðåìåùåíèå îáúåêòà ïî ôîðìå (íàæàòèå êíîïîê íàâèãàöèè)
|
||||
/// </summary>
|
||||
@ -57,7 +95,7 @@ public partial class FormBattleship : Form
|
||||
/// <param name="e"></param>
|
||||
private void ButtonMove_Click(object sender, EventArgs e)
|
||||
{
|
||||
if (_drawingBattleship == null)
|
||||
if (_drawningWarship == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
@ -66,20 +104,17 @@ public partial class FormBattleship : Form
|
||||
switch (name)
|
||||
{
|
||||
case "buttonUp":
|
||||
result =
|
||||
_drawingBattleship.MoveTransport(DirectionType.Up);
|
||||
result = _drawningWarship.MoveTransport(DirectionType.Up);
|
||||
break;
|
||||
case "buttonDown":
|
||||
result =
|
||||
_drawingBattleship.MoveTransport(DirectionType.Down);
|
||||
result = _drawningWarship.MoveTransport(DirectionType.Down);
|
||||
break;
|
||||
case "buttonLeft":
|
||||
result =
|
||||
_drawingBattleship.MoveTransport(DirectionType.Left);
|
||||
result = _drawningWarship.MoveTransport(DirectionType.Left);
|
||||
break;
|
||||
case "buttonRight":
|
||||
result =
|
||||
_drawingBattleship.MoveTransport(DirectionType.Right);
|
||||
_drawningWarship.MoveTransport(DirectionType.Right);
|
||||
break;
|
||||
}
|
||||
if (result)
|
||||
@ -87,4 +122,45 @@ public partial class FormBattleship : Form
|
||||
Draw();
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// Îáðàáîòêà íàæàòèÿ êíîïêè "Øàã"
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="e"></param>
|
||||
private void ButtonStrategyStep_Click(object sender, EventArgs e)
|
||||
{
|
||||
if (_drawningWarship == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (comboBoxStrategy.Enabled)
|
||||
{
|
||||
_strategy = comboBoxStrategy.SelectedIndex switch
|
||||
{
|
||||
0 => new MoveToCenter(),
|
||||
1 => new MoveToBorder(),
|
||||
_ => null,
|
||||
};
|
||||
if (_strategy == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
_strategy.SetData(new MoveableWarship(_drawningWarship),
|
||||
pictureBoxBattleship.Width, pictureBoxBattleship.Height);
|
||||
}
|
||||
if (_strategy == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
comboBoxStrategy.Enabled = false;
|
||||
_strategy.MakeStep();
|
||||
Draw();
|
||||
if (_strategy.GetStatus() == StrategyStatus.Finish)
|
||||
{
|
||||
comboBoxStrategy.Enabled = true;
|
||||
_strategy = null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
24
ProjectBattleship/ProjectBattleship/IMoveableObject.cs
Normal file
24
ProjectBattleship/ProjectBattleship/IMoveableObject.cs
Normal file
@ -0,0 +1,24 @@
|
||||
using ProjectBattleship.MovementStrategy;
|
||||
|
||||
namespace ProjectBattleship.MovementStrategy;
|
||||
/// <summary>
|
||||
/// Интерфейс для работы с перемещаемым объектом
|
||||
/// </summary>
|
||||
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);
|
||||
}
|
53
ProjectBattleship/ProjectBattleship/MoveToCenter.cs
Normal file
53
ProjectBattleship/ProjectBattleship/MoveToCenter.cs
Normal file
@ -0,0 +1,53 @@
|
||||
using ProjectBattleship.MovementStrategy;
|
||||
|
||||
namespace ProjectBattleship.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
ProjectBattleship/ProjectBattleship/MoveableWarship.cs
Normal file
58
ProjectBattleship/ProjectBattleship/MoveableWarship.cs
Normal file
@ -0,0 +1,58 @@
|
||||
using ProjectBattleship.DrawingObject;
|
||||
namespace ProjectBattleship.MovementStrategy;
|
||||
/// <summary>
|
||||
/// Класс-реализация IMoveableObject с использованием DrawingWarship
|
||||
/// </summary>
|
||||
public class MoveableWarship : IMoveableObject
|
||||
{
|
||||
/// <summary>
|
||||
/// Поле-объект класса DrawingWarship или его наследника
|
||||
/// </summary>
|
||||
private readonly DrawingWarship? _warship = null;
|
||||
/// <summary>
|
||||
/// Конструктор
|
||||
/// </summary>
|
||||
/// <param name="warship">Объект класса DrawingWarship</param>
|
||||
public MoveableWarship(DrawingWarship warship)
|
||||
{
|
||||
_warship = warship;
|
||||
}
|
||||
public ObjectParameters? GetObjectPosition
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_warship == null || _warship.EntityWarship == null ||
|
||||
!_warship.GetPosX.HasValue || !_warship.GetPosY.HasValue)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return new ObjectParameters(_warship.GetPosX.Value,
|
||||
_warship.GetPosY.Value, _warship.GetWidth, _warship.GetHeight);
|
||||
}
|
||||
}
|
||||
public int GetStep => (int)(_warship?.EntityWarship?.Step ?? 0);
|
||||
public bool TryMoveObject(MovementDirection direction)
|
||||
{
|
||||
if (_warship == null || _warship.EntityWarship == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return _warship.MoveTransport(GetDirectionType(direction));
|
||||
}
|
||||
/// <summary>
|
||||
/// Конвертация из MovementDirection в DirectionType
|
||||
/// </summary>
|
||||
/// <param name="direction">MovementDirection</param>
|
||||
/// <returns>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,
|
||||
};
|
||||
}
|
||||
}
|
23
ProjectBattleship/ProjectBattleship/MovementDirection.cs
Normal file
23
ProjectBattleship/ProjectBattleship/MovementDirection.cs
Normal file
@ -0,0 +1,23 @@
|
||||
namespace ProjectBattleship.MovementStrategy;
|
||||
/// <summary>
|
||||
/// Направление перемещения
|
||||
/// </summary>
|
||||
public enum MovementDirection
|
||||
{
|
||||
/// <summary>
|
||||
/// Вверх
|
||||
/// </summary>
|
||||
Up = 1,
|
||||
/// <summary>
|
||||
/// Вниз
|
||||
/// </summary>
|
||||
Down = 2,
|
||||
/// <summary>
|
||||
/// Влево
|
||||
/// </summary>
|
||||
Left = 3,
|
||||
/// <summary>
|
||||
/// Вправо
|
||||
/// </summary>
|
||||
Right = 4
|
||||
}
|
61
ProjectBattleship/ProjectBattleship/ObjectParameters.cs
Normal file
61
ProjectBattleship/ProjectBattleship/ObjectParameters.cs
Normal file
@ -0,0 +1,61 @@
|
||||
namespace ProjectBattleship.MovementStrategy;
|
||||
/// <summary>
|
||||
/// Параметры-координаты объекта
|
||||
/// </summary>
|
||||
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;
|
||||
}
|
||||
}
|
19
ProjectBattleship/ProjectBattleship/StrategyStatus.cs
Normal file
19
ProjectBattleship/ProjectBattleship/StrategyStatus.cs
Normal file
@ -0,0 +1,19 @@
|
||||
namespace ProjectBattleship.MovementStrategy;
|
||||
/// <summary>
|
||||
/// Статус выполнения операции перемещения
|
||||
/// </summary>
|
||||
public enum StrategyStatus
|
||||
{
|
||||
/// <summary>
|
||||
/// Все готово к началу
|
||||
/// </summary>
|
||||
NotInit,
|
||||
/// <summary>
|
||||
/// Выполняется
|
||||
/// </summary>
|
||||
InProgress,
|
||||
/// <summary>
|
||||
/// Завершено
|
||||
/// </summary>
|
||||
Finish
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user