Compare commits
2 Commits
749f810416
...
d33425108d
Author | SHA1 | Date | |
---|---|---|---|
d33425108d | |||
a8c4022a95 |
@ -1,4 +1,4 @@
|
|||||||
namespace MotorBoat
|
namespace MotorBoat.Drawnings
|
||||||
{
|
{
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -6,6 +6,10 @@
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public enum DirectionType
|
public enum DirectionType
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Неизвестное направление
|
||||||
|
/// </summary>
|
||||||
|
Unknow = -1,
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Вверх
|
/// Вверх
|
||||||
/// </summary>
|
/// </summary>
|
@ -1,14 +1,18 @@
|
|||||||
namespace MotorBoat
|
using MotorBoat.Entities;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace MotorBoat.Drawnings
|
||||||
{
|
{
|
||||||
/// <summary>
|
public class DrawningBoat
|
||||||
/// Класс, отвечающий за прорисовку и перемещение объекта-сущности
|
|
||||||
/// </summary>
|
|
||||||
public class DrawningMotorBoat
|
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Класс-сущность
|
/// Класс-сущность
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public EntityMotorBoat? EntityMotorBoat { get; private set; }
|
public EntityBoat? EntityBoat { get; protected set; }
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Ширина окна
|
/// Ширина окна
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -20,39 +24,67 @@
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Левая координата прорисовки лодки
|
/// Левая координата прорисовки лодки
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private int? _startPosX;
|
protected int? _startPosX;
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Верхняя кооридната прорисовки лодки
|
/// Верхняя кооридната прорисовки лодки
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private int? _startPosY;
|
protected int? _startPosY;
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Ширина прорисовки лодки
|
/// Ширина прорисовки лодки
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private readonly int _drawningBoatWidth = 153;
|
private readonly int _drawningBoatWidth = 143;
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Высота прорисовки лодки
|
/// Высота прорисовки лодки
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private readonly int _drawningBoatHeight = 75;
|
private readonly int _drawningBoatHeight = 38;
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Инициализация свойств
|
/// Координата Х объекта
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="speed">Скорость</param>
|
public int? GetPosX => _startPosX;
|
||||||
/// <param name="weight">Вес</param>
|
/// <summary>
|
||||||
/// <param name="bodyColor">Основной цвет</param>
|
/// Координата Y объекта
|
||||||
/// <param name="additionalColor">Дополнительный цвет</param>
|
/// </summary>
|
||||||
/// <param name="protectiveGlass">Признак наличия защитного стекла</param>
|
public int? GetPosY => _startPosY;
|
||||||
/// <param name="engine">Признак наличия мотора</param>
|
/// <summary>
|
||||||
public void Init(int speed, double weight, Color bodyColor, Color additionalColor,
|
/// Ширина объекта
|
||||||
bool protectiveGlass, bool engine)
|
/// </summary>
|
||||||
|
public int GetWidth => _drawningBoatWidth;
|
||||||
|
/// <summary>
|
||||||
|
/// Высота объекта
|
||||||
|
/// </summary>
|
||||||
|
public int GetHeight => _drawningBoatHeight;
|
||||||
|
/// <summary>
|
||||||
|
/// Пустой конструктор
|
||||||
|
/// </summary>
|
||||||
|
private DrawningBoat()
|
||||||
{
|
{
|
||||||
EntityMotorBoat = new EntityMotorBoat();
|
|
||||||
EntityMotorBoat.Init(speed, weight, bodyColor, additionalColor, protectiveGlass, engine);
|
|
||||||
_pictureWidth = null;
|
_pictureWidth = null;
|
||||||
_pictureHeight = null;
|
_pictureHeight = null;
|
||||||
_startPosX = null;
|
_startPosX = null;
|
||||||
_startPosY = null;
|
_startPosY = null;
|
||||||
}
|
}
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
/// Конструктор
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="speed">Скорость</param>
|
||||||
|
/// <param name="weight">Вес</param>
|
||||||
|
/// <param name="bodyColor">Основной цвет</param>
|
||||||
|
public DrawningBoat(int speed, double weight, Color bodyColor) : this()
|
||||||
|
{
|
||||||
|
EntityBoat = new EntityBoat(speed, weight, bodyColor);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Конструктор для наследников
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="drawningBoatWidth">Ширина прорисовки лодки</param>
|
||||||
|
/// <param name="drawningBoatHeight">Высота прорисовки лодки</param>
|
||||||
|
protected DrawningBoat(int drawningBoatWidth, int drawningBoatHeight) : this()
|
||||||
|
{
|
||||||
|
_drawningBoatWidth = drawningBoatWidth;
|
||||||
|
_drawningBoatHeight = drawningBoatHeight;
|
||||||
|
}
|
||||||
|
/// <summary>
|
||||||
/// Установка границ поля
|
/// Установка границ поля
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="width">Ширина поля</param>
|
/// <param name="width">Ширина поля</param>
|
||||||
@ -126,7 +158,7 @@
|
|||||||
/// невозможно</returns>
|
/// невозможно</returns>
|
||||||
public bool MoveTransport(DirectionType direction)
|
public bool MoveTransport(DirectionType direction)
|
||||||
{
|
{
|
||||||
if (EntityMotorBoat == null || !_startPosX.HasValue || !_startPosY.HasValue)
|
if (EntityBoat == null || !_startPosX.HasValue || !_startPosY.HasValue)
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -134,30 +166,30 @@
|
|||||||
{
|
{
|
||||||
//влево
|
//влево
|
||||||
case DirectionType.Left:
|
case DirectionType.Left:
|
||||||
if (_startPosX.Value - EntityMotorBoat.Step > 0)
|
if (_startPosX.Value - EntityBoat.Step > 0)
|
||||||
{
|
{
|
||||||
_startPosX -= (int)EntityMotorBoat.Step;
|
_startPosX -= (int)EntityBoat.Step;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
//вверх
|
//вверх
|
||||||
case DirectionType.Up:
|
case DirectionType.Up:
|
||||||
if (_startPosY.Value - EntityMotorBoat.Step > 0)
|
if (_startPosY.Value - EntityBoat.Step > 0)
|
||||||
{
|
{
|
||||||
_startPosY -= (int)EntityMotorBoat.Step;
|
_startPosY -= (int)EntityBoat.Step;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
//вправо
|
//вправо
|
||||||
case DirectionType.Right:
|
case DirectionType.Right:
|
||||||
if (_startPosX.Value + _drawningBoatWidth + EntityMotorBoat.Step < _pictureWidth)
|
if (_startPosX.Value + _drawningBoatWidth + EntityBoat.Step < _pictureWidth)
|
||||||
{
|
{
|
||||||
_startPosX += (int)EntityMotorBoat.Step;
|
_startPosX += (int)EntityBoat.Step;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
//вниз
|
//вниз
|
||||||
case DirectionType.Down:
|
case DirectionType.Down:
|
||||||
if (_startPosY.Value + _drawningBoatHeight + EntityMotorBoat.Step < _pictureHeight)
|
if (_startPosY.Value + _drawningBoatHeight + EntityBoat.Step < _pictureHeight)
|
||||||
{
|
{
|
||||||
_startPosY += (int)EntityMotorBoat.Step;
|
_startPosY += (int)EntityBoat.Step;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
default:
|
default:
|
||||||
@ -169,26 +201,25 @@
|
|||||||
/// Прорисовка объекта
|
/// Прорисовка объекта
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="g"></param>
|
/// <param name="g"></param>
|
||||||
public void DrawTransport(Graphics g)
|
public virtual void DrawTransport(Graphics g)
|
||||||
{
|
{
|
||||||
if (EntityMotorBoat == null || !_startPosX.HasValue ||
|
if (EntityBoat == null || !_startPosX.HasValue ||
|
||||||
!_startPosY.HasValue)
|
!_startPosY.HasValue)
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
Pen pen = new(Color.Black);
|
Pen pen = new(Color.Black);
|
||||||
Brush brushBodyColor = new SolidBrush(EntityMotorBoat.BodyColor);
|
Brush brushBodyColor = new SolidBrush(EntityBoat.BodyColor);
|
||||||
Brush brushAdditionalColor = new SolidBrush(EntityMotorBoat.AdditionalColor);
|
|
||||||
|
|
||||||
Point[] body =
|
Point[] body =
|
||||||
{
|
{
|
||||||
new Point(_startPosX.Value + 10, _startPosY.Value + 25),
|
new Point(_startPosX.Value, _startPosY.Value),
|
||||||
new Point(_startPosX.Value + 110, _startPosY.Value + 25),
|
new Point(_startPosX.Value + 100, _startPosY.Value),
|
||||||
new Point(_startPosX.Value + 155, _startPosY.Value + 45),
|
new Point(_startPosX.Value + 145, _startPosY.Value + 20),
|
||||||
new Point(_startPosX.Value + 110, _startPosY.Value + 65),
|
new Point(_startPosX.Value + 100, _startPosY.Value + 40),
|
||||||
new Point(_startPosX.Value + 10, _startPosY.Value + 65),
|
new Point(_startPosX.Value, _startPosY.Value + 40),
|
||||||
new Point(_startPosX.Value + 10, _startPosY.Value + 25)
|
new Point(_startPosX.Value, _startPosY.Value)
|
||||||
};
|
};
|
||||||
|
|
||||||
// корпус
|
// корпус
|
||||||
@ -196,34 +227,11 @@
|
|||||||
g.DrawPolygon(pen, body);
|
g.DrawPolygon(pen, body);
|
||||||
|
|
||||||
//боковина лодки
|
//боковина лодки
|
||||||
g.DrawEllipse(pen, _startPosX.Value + 25, _startPosY.Value + 30, 30, 30);
|
g.DrawEllipse(pen, _startPosX.Value + 15, _startPosY.Value + 5, 30, 30);
|
||||||
g.DrawEllipse(pen, _startPosX.Value + 70, _startPosY.Value + 30, 30, 30);
|
g.DrawEllipse(pen, _startPosX.Value + 60, _startPosY.Value + 5, 30, 30);
|
||||||
g.DrawLine(pen, _startPosX.Value + 37, _startPosY.Value + 30, _startPosX.Value + 83, _startPosY.Value + 30);
|
g.DrawLine(pen, _startPosX.Value + 27, _startPosY.Value + 5, _startPosX.Value + 73, _startPosY.Value + 5);
|
||||||
g.DrawLine(pen, _startPosX.Value + 37, _startPosY.Value + 60, _startPosX.Value + 83, _startPosY.Value + 60);
|
g.DrawLine(pen, _startPosX.Value + 27, _startPosY.Value + 35, _startPosX.Value + 73, _startPosY.Value + 35);
|
||||||
g.FillRectangle(brushBodyColor, _startPosX.Value + 37, _startPosY.Value + 31, 50, 29);
|
g.FillRectangle(brushBodyColor, _startPosX.Value + 27, _startPosY.Value + 6, 50, 29);
|
||||||
|
|
||||||
|
|
||||||
//защитное стекло спереди
|
|
||||||
if (EntityMotorBoat.ProtectiveGlass)
|
|
||||||
{
|
|
||||||
Point[] glass =
|
|
||||||
{
|
|
||||||
new Point(_startPosX.Value + 105, _startPosY.Value + 25),
|
|
||||||
new Point(_startPosX.Value + 100, _startPosY.Value),
|
|
||||||
new Point(_startPosX.Value + 90, _startPosY.Value),
|
|
||||||
new Point(_startPosX.Value + 90, _startPosY.Value + 25),
|
|
||||||
new Point(_startPosX.Value + 105, _startPosY.Value + 25)
|
|
||||||
};
|
|
||||||
g.FillPolygon(brushAdditionalColor, glass);
|
|
||||||
g.DrawPolygon(pen, glass);
|
|
||||||
}
|
|
||||||
|
|
||||||
//мотор
|
|
||||||
if (EntityMotorBoat.Engine)
|
|
||||||
{
|
|
||||||
g.FillRectangle(brushAdditionalColor, _startPosX.Value, _startPosY.Value + 50, 10, 25);
|
|
||||||
g.DrawRectangle(pen, _startPosX.Value, _startPosY.Value + 50, 10, 25);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
65
MotorBoat/MotorBoat/Drawnings/DrawningMotorBoat.cs
Normal file
65
MotorBoat/MotorBoat/Drawnings/DrawningMotorBoat.cs
Normal file
@ -0,0 +1,65 @@
|
|||||||
|
using MotorBoat.Entities;
|
||||||
|
|
||||||
|
namespace MotorBoat.Drawnings
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Класс, отвечающий за прорисовку и перемещение объекта-сущности
|
||||||
|
/// </summary>
|
||||||
|
public class DrawningMotorBoat : DrawningBoat
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Конструктор
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="speed">Скорость</param>
|
||||||
|
/// <param name="weight">Вес</param>
|
||||||
|
/// <param name="bodyColor">Основной цвет</param>
|
||||||
|
/// <param name="additionalColor">Дополнительный цвет</param>
|
||||||
|
/// <param name="protectiveGlass">Признак наличия защитного стекла</param>
|
||||||
|
/// <param name="engine">Признак наличия мотора</param>
|
||||||
|
public DrawningMotorBoat(int speed, double weight, Color bodyColor, Color additionalColor,
|
||||||
|
bool protectiveGlass, bool engine) : base(153, 75)
|
||||||
|
{
|
||||||
|
EntityBoat = new EntityMotorBoat(speed, weight, bodyColor, additionalColor, protectiveGlass, engine);
|
||||||
|
}
|
||||||
|
/// <summary>
|
||||||
|
/// Прорисовка объекта
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="g"></param>
|
||||||
|
public override void DrawTransport(Graphics g)
|
||||||
|
{
|
||||||
|
if (EntityBoat == null || EntityBoat is not EntityMotorBoat motorBoat
|
||||||
|
|| !_startPosX.HasValue || !_startPosY.HasValue)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
Pen pen = new(Color.Black);
|
||||||
|
Brush brushAdditionalColor = new SolidBrush(motorBoat.AdditionalColor);
|
||||||
|
|
||||||
|
//защитное стекло спереди
|
||||||
|
if (motorBoat.ProtectiveGlass)
|
||||||
|
{
|
||||||
|
Point[] glass =
|
||||||
|
{
|
||||||
|
new Point(_startPosX.Value + 105, _startPosY.Value + 25),
|
||||||
|
new Point(_startPosX.Value + 100, _startPosY.Value),
|
||||||
|
new Point(_startPosX.Value + 90, _startPosY.Value),
|
||||||
|
new Point(_startPosX.Value + 90, _startPosY.Value + 25),
|
||||||
|
new Point(_startPosX.Value + 105, _startPosY.Value + 25)
|
||||||
|
};
|
||||||
|
g.FillPolygon(brushAdditionalColor, glass);
|
||||||
|
g.DrawPolygon(pen, glass);
|
||||||
|
}
|
||||||
|
_startPosX += 10;
|
||||||
|
_startPosY += 25;
|
||||||
|
base.DrawTransport(g);
|
||||||
|
_startPosX -= 10;
|
||||||
|
_startPosY -= 25;
|
||||||
|
//мотор
|
||||||
|
if (motorBoat.Engine)
|
||||||
|
{
|
||||||
|
g.FillRectangle(brushAdditionalColor, _startPosX.Value, _startPosY.Value + 50, 10, 25);
|
||||||
|
g.DrawRectangle(pen, _startPosX.Value, _startPosY.Value + 50, 10, 25);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
43
MotorBoat/MotorBoat/Entities/EntityBoat.cs
Normal file
43
MotorBoat/MotorBoat/Entities/EntityBoat.cs
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace MotorBoat.Entities
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Класс-сущность "Лодка"
|
||||||
|
/// </summary>
|
||||||
|
public class EntityBoat
|
||||||
|
{
|
||||||
|
/// <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 EntityBoat(int speed, double weight, Color bodyColor)
|
||||||
|
{
|
||||||
|
Speed = speed;
|
||||||
|
Weight = weight;
|
||||||
|
BodyColor = bodyColor;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,22 +1,10 @@
|
|||||||
namespace MotorBoat
|
namespace MotorBoat.Entities
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Класс-сущность "Моторная лодка"
|
/// Класс-сущность "Моторная лодка"
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class EntityMotorBoat
|
public class EntityMotorBoat : EntityBoat
|
||||||
{
|
{
|
||||||
/// <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>
|
||||||
/// Дополнительный цвет (для опциональных элементов)
|
/// Дополнительный цвет (для опциональных элементов)
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -42,12 +30,9 @@
|
|||||||
/// <param name="additionalColor">Дополнительный цвет</param>
|
/// <param name="additionalColor">Дополнительный цвет</param>
|
||||||
/// <param name="protectiveGlass">Признак наличия защитного стекла</param>
|
/// <param name="protectiveGlass">Признак наличия защитного стекла</param>
|
||||||
/// <param name="engine">Признак наличия мотора</param>
|
/// <param name="engine">Признак наличия мотора</param>
|
||||||
public void Init(int speed, double weight, Color bodyColor, Color additionalColor,
|
public EntityMotorBoat(int speed, double weight, Color bodyColor, Color additionalColor,
|
||||||
bool protectiveGlass, bool engine)
|
bool protectiveGlass, bool engine) : base(speed, weight, bodyColor)
|
||||||
{
|
{
|
||||||
Speed = speed;
|
|
||||||
Weight = weight;
|
|
||||||
BodyColor = bodyColor;
|
|
||||||
AdditionalColor = additionalColor;
|
AdditionalColor = additionalColor;
|
||||||
ProtectiveGlass = protectiveGlass;
|
ProtectiveGlass = protectiveGlass;
|
||||||
Engine = engine;
|
Engine = engine;
|
64
MotorBoat/MotorBoat/FormMotorBoat.Designer.cs
generated
64
MotorBoat/MotorBoat/FormMotorBoat.Designer.cs
generated
@ -29,11 +29,14 @@
|
|||||||
private void InitializeComponent()
|
private void InitializeComponent()
|
||||||
{
|
{
|
||||||
pictureBoxMotorBoat = new PictureBox();
|
pictureBoxMotorBoat = new PictureBox();
|
||||||
buttonCreate = new Button();
|
buttonCreateMotorBoat = new Button();
|
||||||
buttonLeft = new Button();
|
buttonLeft = new Button();
|
||||||
buttonDown = new Button();
|
buttonDown = new Button();
|
||||||
buttonRight = new Button();
|
buttonRight = new Button();
|
||||||
buttonUp = new Button();
|
buttonUp = new Button();
|
||||||
|
buttonCreateBoat = new Button();
|
||||||
|
comboBoxStrategy = new ComboBox();
|
||||||
|
buttonStrategyStep = new Button();
|
||||||
((System.ComponentModel.ISupportInitialize)pictureBoxMotorBoat).BeginInit();
|
((System.ComponentModel.ISupportInitialize)pictureBoxMotorBoat).BeginInit();
|
||||||
SuspendLayout();
|
SuspendLayout();
|
||||||
//
|
//
|
||||||
@ -46,16 +49,16 @@
|
|||||||
pictureBoxMotorBoat.TabIndex = 0;
|
pictureBoxMotorBoat.TabIndex = 0;
|
||||||
pictureBoxMotorBoat.TabStop = false;
|
pictureBoxMotorBoat.TabStop = false;
|
||||||
//
|
//
|
||||||
// buttonCreate
|
// buttonCreateMotorBoat
|
||||||
//
|
//
|
||||||
buttonCreate.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
|
buttonCreateMotorBoat.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
|
||||||
buttonCreate.Location = new Point(12, 412);
|
buttonCreateMotorBoat.Location = new Point(12, 412);
|
||||||
buttonCreate.Name = "buttonCreate";
|
buttonCreateMotorBoat.Name = "buttonCreateMotorBoat";
|
||||||
buttonCreate.Size = new Size(75, 23);
|
buttonCreateMotorBoat.Size = new Size(174, 23);
|
||||||
buttonCreate.TabIndex = 1;
|
buttonCreateMotorBoat.TabIndex = 1;
|
||||||
buttonCreate.Text = "Создать";
|
buttonCreateMotorBoat.Text = "Создать моторную лодку";
|
||||||
buttonCreate.UseVisualStyleBackColor = true;
|
buttonCreateMotorBoat.UseVisualStyleBackColor = true;
|
||||||
buttonCreate.Click += ButtonCreateMotorBoat_Click;
|
buttonCreateMotorBoat.Click += ButtonCreateMotorBoat_Click;
|
||||||
//
|
//
|
||||||
// buttonLeft
|
// buttonLeft
|
||||||
//
|
//
|
||||||
@ -105,16 +108,50 @@
|
|||||||
buttonUp.UseVisualStyleBackColor = true;
|
buttonUp.UseVisualStyleBackColor = true;
|
||||||
buttonUp.Click += ButtonMove_Click;
|
buttonUp.Click += ButtonMove_Click;
|
||||||
//
|
//
|
||||||
|
// buttonCreateBoat
|
||||||
|
//
|
||||||
|
buttonCreateBoat.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
|
||||||
|
buttonCreateBoat.Location = new Point(203, 412);
|
||||||
|
buttonCreateBoat.Name = "buttonCreateBoat";
|
||||||
|
buttonCreateBoat.Size = new Size(174, 23);
|
||||||
|
buttonCreateBoat.TabIndex = 6;
|
||||||
|
buttonCreateBoat.Text = "Создать лодку";
|
||||||
|
buttonCreateBoat.UseVisualStyleBackColor = true;
|
||||||
|
buttonCreateBoat.Click += ButtonCreateBoat_Click;
|
||||||
|
//
|
||||||
|
// comboBoxStrategy
|
||||||
|
//
|
||||||
|
comboBoxStrategy.DropDownStyle = ComboBoxStyle.DropDownList;
|
||||||
|
comboBoxStrategy.FormattingEnabled = true;
|
||||||
|
comboBoxStrategy.Items.AddRange(new object[] { "К центру", "К краю" });
|
||||||
|
comboBoxStrategy.Location = new Point(724, 12);
|
||||||
|
comboBoxStrategy.Name = "comboBoxStrategy";
|
||||||
|
comboBoxStrategy.Size = new Size(121, 23);
|
||||||
|
comboBoxStrategy.TabIndex = 7;
|
||||||
|
//
|
||||||
|
// buttonStrategyStep
|
||||||
|
//
|
||||||
|
buttonStrategyStep.Location = new Point(774, 41);
|
||||||
|
buttonStrategyStep.Name = "buttonStrategyStep";
|
||||||
|
buttonStrategyStep.Size = new Size(75, 23);
|
||||||
|
buttonStrategyStep.TabIndex = 8;
|
||||||
|
buttonStrategyStep.Text = "Шаг";
|
||||||
|
buttonStrategyStep.UseVisualStyleBackColor = true;
|
||||||
|
buttonStrategyStep.Click += buttonStrategyStep_Click;
|
||||||
|
//
|
||||||
// FormMotorBoat
|
// FormMotorBoat
|
||||||
//
|
//
|
||||||
AutoScaleDimensions = new SizeF(7F, 15F);
|
AutoScaleDimensions = new SizeF(7F, 15F);
|
||||||
AutoScaleMode = AutoScaleMode.Font;
|
AutoScaleMode = AutoScaleMode.Font;
|
||||||
ClientSize = new Size(861, 447);
|
ClientSize = new Size(861, 447);
|
||||||
|
Controls.Add(buttonStrategyStep);
|
||||||
|
Controls.Add(comboBoxStrategy);
|
||||||
|
Controls.Add(buttonCreateBoat);
|
||||||
Controls.Add(buttonUp);
|
Controls.Add(buttonUp);
|
||||||
Controls.Add(buttonRight);
|
Controls.Add(buttonRight);
|
||||||
Controls.Add(buttonDown);
|
Controls.Add(buttonDown);
|
||||||
Controls.Add(buttonLeft);
|
Controls.Add(buttonLeft);
|
||||||
Controls.Add(buttonCreate);
|
Controls.Add(buttonCreateMotorBoat);
|
||||||
Controls.Add(pictureBoxMotorBoat);
|
Controls.Add(pictureBoxMotorBoat);
|
||||||
Name = "FormMotorBoat";
|
Name = "FormMotorBoat";
|
||||||
Text = "Моторная лодка";
|
Text = "Моторная лодка";
|
||||||
@ -125,10 +162,13 @@
|
|||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
private PictureBox pictureBoxMotorBoat;
|
private PictureBox pictureBoxMotorBoat;
|
||||||
private Button buttonCreate;
|
private Button buttonCreateMotorBoat;
|
||||||
private Button buttonLeft;
|
private Button buttonLeft;
|
||||||
private Button buttonDown;
|
private Button buttonDown;
|
||||||
private Button buttonRight;
|
private Button buttonRight;
|
||||||
private Button buttonUp;
|
private Button buttonUp;
|
||||||
|
private Button buttonCreateBoat;
|
||||||
|
private ComboBox comboBoxStrategy;
|
||||||
|
private Button buttonStrategyStep;
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -7,54 +7,83 @@ using System.Linq;
|
|||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using System.Windows.Forms;
|
using System.Windows.Forms;
|
||||||
|
using MotorBoat.Drawnings;
|
||||||
|
using MotorBoat.MovementStrategy;
|
||||||
|
|
||||||
namespace MotorBoat
|
namespace MotorBoat
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Форма работы с объектом "Моторная лодка"
|
||||||
|
/// </summary>
|
||||||
public partial class FormMotorBoat : Form
|
public partial class FormMotorBoat : Form
|
||||||
{
|
{
|
||||||
private DrawningMotorBoat? _drawningMotorBoat;
|
/// <summary>
|
||||||
|
/// Поле для прорисовки объекта
|
||||||
|
/// </summary>
|
||||||
|
private DrawningBoat? _drawningBoat;
|
||||||
|
/// <summary>
|
||||||
|
/// Стратегия перемещения
|
||||||
|
/// </summary>
|
||||||
|
private AbstractStrategy? _strategy;
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Конструктор формы
|
/// Конструктор формы
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public FormMotorBoat()
|
public FormMotorBoat()
|
||||||
{
|
{
|
||||||
InitializeComponent();
|
InitializeComponent();
|
||||||
|
_strategy = null;
|
||||||
}
|
}
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Метод прорисовки катера
|
/// Метод прорисовки катера
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private void Draw()
|
private void Draw()
|
||||||
{
|
{
|
||||||
if (_drawningMotorBoat == null)
|
if (_drawningBoat == null)
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
Bitmap bmp = new(pictureBoxMotorBoat.Width,
|
Bitmap bmp = new(pictureBoxMotorBoat.Width,
|
||||||
pictureBoxMotorBoat.Height);
|
pictureBoxMotorBoat.Height);
|
||||||
Graphics gr = Graphics.FromImage(bmp);
|
Graphics gr = Graphics.FromImage(bmp);
|
||||||
_drawningMotorBoat.DrawTransport(gr);
|
_drawningBoat.DrawTransport(gr);
|
||||||
pictureBoxMotorBoat.Image = bmp;
|
pictureBoxMotorBoat.Image = bmp;
|
||||||
}
|
}
|
||||||
|
private void CreateObject(string type)
|
||||||
|
{
|
||||||
|
Random random = new Random();
|
||||||
|
switch (type)
|
||||||
|
{
|
||||||
|
case nameof(DrawningBoat):
|
||||||
|
_drawningBoat = new DrawningBoat(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(DrawningMotorBoat):
|
||||||
|
_drawningBoat = new DrawningMotorBoat(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;
|
||||||
|
}
|
||||||
|
_drawningBoat.SetPictureSize(pictureBoxMotorBoat.Width, pictureBoxMotorBoat.Height);
|
||||||
|
_drawningBoat.SetPosition(random.Next(10, 100), random.Next(10, 100));
|
||||||
|
_strategy = null;
|
||||||
|
comboBoxStrategy.Enabled = true;
|
||||||
|
Draw();
|
||||||
|
}
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Обработка нажатия кнопки "Создать"
|
/// Обработка нажатия кнопки "Создать моторную лодку"
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="sender"></param>
|
/// <param name="sender"></param>
|
||||||
/// <param name="e"></param>
|
/// <param name="e"></param>
|
||||||
private void ButtonCreateMotorBoat_Click(object sender, EventArgs e)
|
private void ButtonCreateMotorBoat_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningMotorBoat));
|
||||||
{
|
/// <summary>
|
||||||
Random random = new();
|
/// Обработка нажатия кнопки "Создать лодку"
|
||||||
_drawningMotorBoat = new DrawningMotorBoat();
|
/// </summary>
|
||||||
_drawningMotorBoat.Init(random.Next(100, 300), random.Next(1000, 3000),
|
/// <param name="sender"></param>
|
||||||
Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)),
|
/// <param name="e"></param>
|
||||||
Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)),
|
private void ButtonCreateBoat_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningBoat));
|
||||||
Convert.ToBoolean(random.Next(0, 2)),
|
|
||||||
Convert.ToBoolean(random.Next(0, 2)));
|
|
||||||
_drawningMotorBoat.SetPictureSize(pictureBoxMotorBoat.Width,
|
|
||||||
pictureBoxMotorBoat.Height);
|
|
||||||
_drawningMotorBoat.SetPosition(random.Next(10, 100), random.Next(10,
|
|
||||||
100));
|
|
||||||
Draw();
|
|
||||||
}
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Перемещение объекта по форме (нажатие кнопок навигации)
|
/// Перемещение объекта по форме (нажатие кнопок навигации)
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -62,7 +91,7 @@ namespace MotorBoat
|
|||||||
/// <param name="e"></param>
|
/// <param name="e"></param>
|
||||||
private void ButtonMove_Click(object sender, EventArgs e)
|
private void ButtonMove_Click(object sender, EventArgs e)
|
||||||
{
|
{
|
||||||
if (_drawningMotorBoat == null)
|
if (_drawningBoat == null)
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -72,19 +101,19 @@ namespace MotorBoat
|
|||||||
{
|
{
|
||||||
case "buttonUp":
|
case "buttonUp":
|
||||||
result =
|
result =
|
||||||
_drawningMotorBoat.MoveTransport(DirectionType.Up);
|
_drawningBoat.MoveTransport(DirectionType.Up);
|
||||||
break;
|
break;
|
||||||
case "buttonDown":
|
case "buttonDown":
|
||||||
result =
|
result =
|
||||||
_drawningMotorBoat.MoveTransport(DirectionType.Down);
|
_drawningBoat.MoveTransport(DirectionType.Down);
|
||||||
break;
|
break;
|
||||||
case "buttonLeft":
|
case "buttonLeft":
|
||||||
result =
|
result =
|
||||||
_drawningMotorBoat.MoveTransport(DirectionType.Left);
|
_drawningBoat.MoveTransport(DirectionType.Left);
|
||||||
break;
|
break;
|
||||||
case "buttonRight":
|
case "buttonRight":
|
||||||
result =
|
result =
|
||||||
_drawningMotorBoat.MoveTransport(DirectionType.Right);
|
_drawningBoat.MoveTransport(DirectionType.Right);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (result)
|
if (result)
|
||||||
@ -92,5 +121,46 @@ namespace MotorBoat
|
|||||||
Draw();
|
Draw();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
///
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="sender"></param>
|
||||||
|
/// <param name="e"></param>
|
||||||
|
private void buttonStrategyStep_Click(object sender, EventArgs e)
|
||||||
|
{
|
||||||
|
if (_drawningBoat == null)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (comboBoxStrategy.Enabled)
|
||||||
|
{
|
||||||
|
_strategy = comboBoxStrategy.SelectedIndex switch
|
||||||
|
{
|
||||||
|
0 => new MoveToCenter(),
|
||||||
|
1 => new MoveToBorder(),
|
||||||
|
_ => null,
|
||||||
|
};
|
||||||
|
if (_strategy == null)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
_strategy.SetData(new MoveableBoat(_drawningBoat),
|
||||||
|
pictureBoxMotorBoat.Width, pictureBoxMotorBoat.Height);
|
||||||
|
}
|
||||||
|
if (_strategy == null)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
comboBoxStrategy.Enabled = false;
|
||||||
|
_strategy.MakeStep();
|
||||||
|
Draw();
|
||||||
|
if (_strategy.GetStatus() == StrategyStatus.Finish)
|
||||||
|
{
|
||||||
|
comboBoxStrategy.Enabled = true;
|
||||||
|
_strategy = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
119
MotorBoat/MotorBoat/MovementStrategy/AbstractStrategy.cs
Normal file
119
MotorBoat/MotorBoat/MovementStrategy/AbstractStrategy.cs
Normal file
@ -0,0 +1,119 @@
|
|||||||
|
namespace MotorBoat.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;
|
||||||
|
}
|
||||||
|
}
|
27
MotorBoat/MotorBoat/MovementStrategy/IMoveableObject.cs
Normal file
27
MotorBoat/MotorBoat/MovementStrategy/IMoveableObject.cs
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace MotorBoat.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);
|
||||||
|
}
|
44
MotorBoat/MotorBoat/MovementStrategy/MoveToBorder.cs
Normal file
44
MotorBoat/MotorBoat/MovementStrategy/MoveToBorder.cs
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace MotorBoat.MovementStrategy;
|
||||||
|
/// <summary>
|
||||||
|
/// Стратегия перемещения объекта к краю экрана
|
||||||
|
/// </summary>
|
||||||
|
public class MoveToBorder : AbstractStrategy
|
||||||
|
{
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
57
MotorBoat/MotorBoat/MovementStrategy/MoveToCenter.cs
Normal file
57
MotorBoat/MotorBoat/MovementStrategy/MoveToCenter.cs
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace MotorBoat.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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
59
MotorBoat/MotorBoat/MovementStrategy/MoveableBoat.cs
Normal file
59
MotorBoat/MotorBoat/MovementStrategy/MoveableBoat.cs
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
using MotorBoat.Drawnings;
|
||||||
|
|
||||||
|
namespace MotorBoat.MovementStrategy;
|
||||||
|
/// <summary>
|
||||||
|
/// Класс-реализация IMoveableObject с использованием DrawningBoat
|
||||||
|
/// </summary>
|
||||||
|
public class MoveableBoat : IMoveableObject
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Поле-объект класса DrawningBoat или его наследника
|
||||||
|
/// </summary>
|
||||||
|
private readonly DrawningBoat? _boat = null;
|
||||||
|
/// <summary>
|
||||||
|
/// Конструктор
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="car">Объект класса DrawningBoat</param>
|
||||||
|
public MoveableBoat(DrawningBoat boat)
|
||||||
|
{
|
||||||
|
_boat = boat;
|
||||||
|
}
|
||||||
|
public ObjectParameters? GetObjectPosition
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
if (_boat == null || _boat.EntityBoat == null ||
|
||||||
|
!_boat.GetPosX.HasValue || !_boat.GetPosY.HasValue)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return new ObjectParameters(_boat.GetPosX.Value,
|
||||||
|
_boat.GetPosY.Value, _boat.GetWidth, _boat.GetHeight);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public int GetStep => (int)(_boat?.EntityBoat?.Step ?? 0);
|
||||||
|
public bool TryMoveObject(MovementDirection direction)
|
||||||
|
{
|
||||||
|
if (_boat == null || _boat.EntityBoat == null)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return _boat.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,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
24
MotorBoat/MotorBoat/MovementStrategy/MovementDirection.cs
Normal file
24
MotorBoat/MotorBoat/MovementStrategy/MovementDirection.cs
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
namespace MotorBoat.MovementStrategy;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Направление перемещения
|
||||||
|
/// </summary>
|
||||||
|
public enum MovementDirection
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Вверх
|
||||||
|
/// </summary>
|
||||||
|
Up = 1,
|
||||||
|
/// <summary>
|
||||||
|
/// Вниз
|
||||||
|
/// </summary>
|
||||||
|
Down = 2,
|
||||||
|
/// <summary>
|
||||||
|
/// Влево
|
||||||
|
/// </summary>
|
||||||
|
Left = 3,
|
||||||
|
/// <summary>
|
||||||
|
/// Вправо
|
||||||
|
/// </summary>
|
||||||
|
Right = 4
|
||||||
|
}
|
62
MotorBoat/MotorBoat/MovementStrategy/ObjectParameters.cs
Normal file
62
MotorBoat/MotorBoat/MovementStrategy/ObjectParameters.cs
Normal file
@ -0,0 +1,62 @@
|
|||||||
|
namespace MotorBoat.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
MotorBoat/MotorBoat/MovementStrategy/StrategyStatus.cs
Normal file
19
MotorBoat/MotorBoat/MovementStrategy/StrategyStatus.cs
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
namespace MotorBoat.MovementStrategy;
|
||||||
|
/// <summary>
|
||||||
|
/// Статус выполнения операции перемещения
|
||||||
|
/// </summary>
|
||||||
|
public enum StrategyStatus
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Все готово к началу
|
||||||
|
/// </summary>
|
||||||
|
NotInit,
|
||||||
|
/// <summary>
|
||||||
|
/// Выполняется
|
||||||
|
/// </summary>
|
||||||
|
InProgress,
|
||||||
|
/// <summary>
|
||||||
|
/// Завершено
|
||||||
|
/// </summary>
|
||||||
|
Finish
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user