Добавление стратегии
This commit is contained in:
parent
527d87741a
commit
b355d6f6ab
@ -5,6 +5,11 @@
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public enum DirectionType
|
public enum DirectionType
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Неизвестное направление
|
||||||
|
/// </summary>
|
||||||
|
Unknow = -1,
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Вверх
|
/// Вверх
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -37,12 +37,32 @@ public class DrawningTrolleyB
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Ширина прорисовки транспорта
|
/// Ширина прорисовки транспорта
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private readonly int _drawningVehicleWidth = 90;
|
private readonly int _drawningVehicleWidth = 89;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Высота прорисовки транспорта
|
/// Высота прорисовки транспорта
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private readonly int _drawningVehicleHeight = 33;
|
private readonly int _drawningVehicleHeight = 31;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Координата X объекта
|
||||||
|
/// </summary>
|
||||||
|
public int? GetPosX => _startPosX;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Координата Y объекта
|
||||||
|
/// </summary>
|
||||||
|
public int? GetPosY => _startPosY;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Ширина объекта
|
||||||
|
/// </summary>
|
||||||
|
public int GetWidth => _drawningVehicleWidth;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Высота объекта
|
||||||
|
/// </summary>
|
||||||
|
public int GetHight => _drawningVehicleHeight;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Пустой конструктор
|
/// Пустой конструктор
|
||||||
@ -204,44 +224,44 @@ public class DrawningTrolleyB
|
|||||||
|
|
||||||
//Колёса
|
//Колёса
|
||||||
pen.Width = 2;
|
pen.Width = 2;
|
||||||
g.DrawEllipse(pen, _startPosX.Value + 10, _startPosY.Value + 21, 12, 12);
|
g.DrawEllipse(pen, _startPosX.Value + 10, _startPosY.Value + 23, 12, 12);
|
||||||
g.DrawEllipse(pen, _startPosX.Value + 70, _startPosY.Value + 21, 12, 12);
|
g.DrawEllipse(pen, _startPosX.Value + 70, _startPosY.Value + 23, 12, 12);
|
||||||
pen.Width = 1;
|
pen.Width = 1;
|
||||||
Brush brBlackWheel = new SolidBrush(Color.Black);
|
Brush brBlackWheel = new SolidBrush(Color.Black);
|
||||||
g.FillEllipse(brBlackWheel, _startPosX.Value + 10, _startPosY.Value + 21, 12, 12);
|
g.FillEllipse(brBlackWheel, _startPosX.Value + 10, _startPosY.Value + 23, 12, 12);
|
||||||
g.FillEllipse(brBlackWheel, _startPosX.Value + 70, _startPosY.Value + 21, 12, 12);
|
g.FillEllipse(brBlackWheel, _startPosX.Value + 70, _startPosY.Value + 23, 12, 12);
|
||||||
Brush brYelWheel = new SolidBrush(Color.Goldenrod);
|
Brush brYelWheel = new SolidBrush(Color.Goldenrod);
|
||||||
g.FillEllipse(brYelWheel, _startPosX.Value + 12, _startPosY.Value + 23, 8, 8);
|
g.FillEllipse(brYelWheel, _startPosX.Value + 12, _startPosY.Value + 25, 8, 8);
|
||||||
g.FillEllipse(brYelWheel, _startPosX.Value + 72, _startPosY.Value + 23, 8, 8);
|
g.FillEllipse(brYelWheel, _startPosX.Value + 72, _startPosY.Value + 25, 8, 8);
|
||||||
|
|
||||||
//Кузов
|
//Кузов
|
||||||
Brush br = new SolidBrush(EntityTrolleyB.BodyColor);
|
Brush br = new SolidBrush(EntityTrolleyB.BodyColor);
|
||||||
g.FillRectangle(br, _startPosX.Value + 1, _startPosY.Value + 1, 89, 24);
|
g.FillRectangle(br, _startPosX.Value + 1, _startPosY.Value + 3, 89, 24);
|
||||||
|
|
||||||
//Границы Автомобиля
|
//Границы Автомобиля
|
||||||
g.DrawRectangle(pen, _startPosX.Value, _startPosY.Value, 90, 25);
|
g.DrawRectangle(pen, _startPosX.Value, _startPosY.Value + 2, 90, 25);
|
||||||
g.DrawRectangle(pen, _startPosX.Value + 26, _startPosY.Value + 10, 10, 15);
|
g.DrawRectangle(pen, _startPosX.Value + 26, _startPosY.Value + 12, 10, 15);
|
||||||
g.DrawRectangle(pen, _startPosX.Value + 78, _startPosY.Value + 4, 12, 12);
|
g.DrawRectangle(pen, _startPosX.Value + 78, _startPosY.Value + 6, 12, 12);
|
||||||
pen.Width = 2;
|
pen.Width = 2;
|
||||||
g.DrawEllipse(pen, _startPosX.Value + 2, _startPosY.Value + 5, 8, 10);
|
g.DrawEllipse(pen, _startPosX.Value + 2, _startPosY.Value + 7, 8, 10);
|
||||||
g.DrawEllipse(pen, _startPosX.Value + 14, _startPosY.Value + 5, 8, 10);
|
g.DrawEllipse(pen, _startPosX.Value + 14, _startPosY.Value + 7, 8, 10);
|
||||||
g.DrawEllipse(pen, _startPosX.Value + 40, _startPosY.Value + 5, 8, 10);
|
g.DrawEllipse(pen, _startPosX.Value + 40, _startPosY.Value + 7, 8, 10);
|
||||||
g.DrawEllipse(pen, _startPosX.Value + 52, _startPosY.Value + 5, 8, 10);
|
g.DrawEllipse(pen, _startPosX.Value + 52, _startPosY.Value + 7, 8, 10);
|
||||||
g.DrawEllipse(pen, _startPosX.Value + 64, _startPosY.Value + 5, 8, 10);
|
g.DrawEllipse(pen, _startPosX.Value + 64, _startPosY.Value + 7, 8, 10);
|
||||||
pen.Width = 1;
|
pen.Width = 1;
|
||||||
|
|
||||||
//Стекла и кабина
|
//Стекла и кабина
|
||||||
Brush brBlue = new SolidBrush(Color.LightBlue);
|
Brush brBlue = new SolidBrush(Color.LightBlue);
|
||||||
g.FillRectangle(brBlue, _startPosX.Value + 79, _startPosY.Value + 5, 11, 11);
|
g.FillRectangle(brBlue, _startPosX.Value + 79, _startPosY.Value + 7, 11, 11);
|
||||||
g.FillEllipse(brBlue, _startPosX.Value + 2, _startPosY.Value + 5, 8, 10);
|
g.FillEllipse(brBlue, _startPosX.Value + 2, _startPosY.Value + 7, 8, 10);
|
||||||
g.FillEllipse(brBlue, _startPosX.Value + 14, _startPosY.Value + 5, 8, 10);
|
g.FillEllipse(brBlue, _startPosX.Value + 14, _startPosY.Value + 7, 8, 10);
|
||||||
g.FillEllipse(brBlue, _startPosX.Value + 40, _startPosY.Value + 5, 8, 10);
|
g.FillEllipse(brBlue, _startPosX.Value + 40, _startPosY.Value + 7, 8, 10);
|
||||||
g.FillEllipse(brBlue, _startPosX.Value + 52, _startPosY.Value + 5, 8, 10);
|
g.FillEllipse(brBlue, _startPosX.Value + 52, _startPosY.Value + 7, 8, 10);
|
||||||
g.FillEllipse(brBlue, _startPosX.Value + 64, _startPosY.Value + 5, 8, 10);
|
g.FillEllipse(brBlue, _startPosX.Value + 64, _startPosY.Value + 7, 8, 10);
|
||||||
|
|
||||||
//Дверь
|
//Дверь
|
||||||
Brush brBlack = new SolidBrush(Color.LightGray);
|
Brush brBlack = new SolidBrush(Color.LightGray);
|
||||||
g.FillRectangle(brBlack, _startPosX.Value + 27, _startPosY.Value + 11, 9, 14);
|
g.FillRectangle(brBlack, _startPosX.Value + 27, _startPosY.Value + 13, 9, 14);
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -35,6 +35,8 @@
|
|||||||
buttonRight = new Button();
|
buttonRight = new Button();
|
||||||
buttonUp = new Button();
|
buttonUp = new Button();
|
||||||
buttonCreateTrolleyB = new Button();
|
buttonCreateTrolleyB = new Button();
|
||||||
|
comboBoxStrategy = new ComboBox();
|
||||||
|
buttonStrategyStep = new Button();
|
||||||
((System.ComponentModel.ISupportInitialize)pictureBoxTrolleybus).BeginInit();
|
((System.ComponentModel.ISupportInitialize)pictureBoxTrolleybus).BeginInit();
|
||||||
SuspendLayout();
|
SuspendLayout();
|
||||||
//
|
//
|
||||||
@ -117,11 +119,33 @@
|
|||||||
buttonCreateTrolleyB.UseVisualStyleBackColor = true;
|
buttonCreateTrolleyB.UseVisualStyleBackColor = true;
|
||||||
buttonCreateTrolleyB.Click += ButtonCreateTrolleyB_Click;
|
buttonCreateTrolleyB.Click += ButtonCreateTrolleyB_Click;
|
||||||
//
|
//
|
||||||
|
// comboBoxStrategy
|
||||||
|
//
|
||||||
|
comboBoxStrategy.DropDownStyle = ComboBoxStyle.DropDownList;
|
||||||
|
comboBoxStrategy.FormattingEnabled = true;
|
||||||
|
comboBoxStrategy.Items.AddRange(new object[] { "К центру", "К краю" });
|
||||||
|
comboBoxStrategy.Location = new Point(667, 12);
|
||||||
|
comboBoxStrategy.Name = "comboBoxStrategy";
|
||||||
|
comboBoxStrategy.Size = new Size(121, 23);
|
||||||
|
comboBoxStrategy.TabIndex = 7;
|
||||||
|
//
|
||||||
|
// buttonStrategyStep
|
||||||
|
//
|
||||||
|
buttonStrategyStep.Location = new Point(713, 41);
|
||||||
|
buttonStrategyStep.Name = "buttonStrategyStep";
|
||||||
|
buttonStrategyStep.Size = new Size(75, 23);
|
||||||
|
buttonStrategyStep.TabIndex = 8;
|
||||||
|
buttonStrategyStep.Text = "Шаг";
|
||||||
|
buttonStrategyStep.UseVisualStyleBackColor = true;
|
||||||
|
buttonStrategyStep.Click += buttonStrategyStep_Click;
|
||||||
|
//
|
||||||
// FormTrolleybus
|
// FormTrolleybus
|
||||||
//
|
//
|
||||||
AutoScaleDimensions = new SizeF(7F, 15F);
|
AutoScaleDimensions = new SizeF(7F, 15F);
|
||||||
AutoScaleMode = AutoScaleMode.Font;
|
AutoScaleMode = AutoScaleMode.Font;
|
||||||
ClientSize = new Size(800, 450);
|
ClientSize = new Size(800, 450);
|
||||||
|
Controls.Add(buttonStrategyStep);
|
||||||
|
Controls.Add(comboBoxStrategy);
|
||||||
Controls.Add(buttonCreateTrolleyB);
|
Controls.Add(buttonCreateTrolleyB);
|
||||||
Controls.Add(buttonUp);
|
Controls.Add(buttonUp);
|
||||||
Controls.Add(buttonRight);
|
Controls.Add(buttonRight);
|
||||||
@ -144,5 +168,7 @@
|
|||||||
private Button buttonRight;
|
private Button buttonRight;
|
||||||
private Button buttonUp;
|
private Button buttonUp;
|
||||||
private Button buttonCreateTrolleyB;
|
private Button buttonCreateTrolleyB;
|
||||||
|
private ComboBox comboBoxStrategy;
|
||||||
|
private Button buttonStrategyStep;
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,17 +1,36 @@
|
|||||||
using ProjectTrolleybus.Drawnings;
|
using ProjectTrolleybus.Drawnings;
|
||||||
using ProjectTrolleybus.Entities;
|
using ProjectTrolleybus.Entities;
|
||||||
|
using ProjectTrolleybus.MovementStrategy;
|
||||||
|
|
||||||
namespace ProjectTrolleybus;
|
namespace ProjectTrolleybus;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Форма работы с объектом "Троллейбус"
|
||||||
|
/// </summary>
|
||||||
public partial class FormTrolleybus : Form
|
public partial class FormTrolleybus : Form
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Поле-объект для прорисовки объекта
|
||||||
|
/// </summary>
|
||||||
private DrawningTrolleyB? _drawningTrolleyB;
|
private DrawningTrolleyB? _drawningTrolleyB;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Стратегия перемещения
|
||||||
|
/// </summary>
|
||||||
|
private AbstractStrategy? _strategy;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Конструктор формы
|
||||||
|
/// </summary>
|
||||||
public FormTrolleybus()
|
public FormTrolleybus()
|
||||||
{
|
{
|
||||||
InitializeComponent();
|
InitializeComponent();
|
||||||
|
_strategy = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Метод прорисовки транспорта
|
||||||
|
/// </summary>
|
||||||
private void Draw()
|
private void Draw()
|
||||||
{
|
{
|
||||||
Bitmap bmp = new(pictureBoxTrolleybus.Width, pictureBoxTrolleybus.Height);
|
Bitmap bmp = new(pictureBoxTrolleybus.Width, pictureBoxTrolleybus.Height);
|
||||||
@ -20,6 +39,11 @@ public partial class FormTrolleybus : Form
|
|||||||
pictureBoxTrolleybus.Image = bmp;
|
pictureBoxTrolleybus.Image = bmp;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Создание объекта класса-перемещения
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="type">Тип создания объекта</param>
|
||||||
private void CreateObject(string type)
|
private void CreateObject(string type)
|
||||||
{
|
{
|
||||||
Random random = new();
|
Random random = new();
|
||||||
@ -41,6 +65,9 @@ public partial class FormTrolleybus : Form
|
|||||||
|
|
||||||
_drawningTrolleyB.SetPictureSize(pictureBoxTrolleybus.Width, pictureBoxTrolleybus.Height);
|
_drawningTrolleyB.SetPictureSize(pictureBoxTrolleybus.Width, pictureBoxTrolleybus.Height);
|
||||||
_drawningTrolleyB.SetPosition(random.Next(10, 100), random.Next(10, 100), pictureBoxTrolleybus.Width, pictureBoxTrolleybus.Height);
|
_drawningTrolleyB.SetPosition(random.Next(10, 100), random.Next(10, 100), pictureBoxTrolleybus.Width, pictureBoxTrolleybus.Height);
|
||||||
|
_strategy = null;
|
||||||
|
comboBoxStrategy.Enabled = true;
|
||||||
|
|
||||||
Draw();
|
Draw();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -64,6 +91,11 @@ public partial class FormTrolleybus : Form
|
|||||||
CreateObject(nameof(DrawningTrolleyB));
|
CreateObject(nameof(DrawningTrolleyB));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Перемещение объекта по кнопке (нажатие кнопок навигации)
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="sender"></param>
|
||||||
|
/// <param name="e"></param>
|
||||||
private void ButtonMove_Click(object sender, EventArgs e)
|
private void ButtonMove_Click(object sender, EventArgs e)
|
||||||
{
|
{
|
||||||
if (_drawningTrolleyB == null)
|
if (_drawningTrolleyB == null)
|
||||||
@ -94,6 +126,42 @@ public partial class FormTrolleybus : Form
|
|||||||
Draw();
|
Draw();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void buttonStrategyStep_Click(object sender, EventArgs e)
|
||||||
|
{
|
||||||
|
if (_drawningTrolleyB == null)
|
||||||
|
{
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (comboBoxStrategy.Enabled)
|
||||||
|
{
|
||||||
|
_strategy = comboBoxStrategy.SelectedIndex switch
|
||||||
|
{
|
||||||
|
0 => new MoveToCenter(),
|
||||||
|
1 => new MoveToBorder(),
|
||||||
|
_ => null,
|
||||||
|
};
|
||||||
|
if (_strategy == null)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
_strategy.SetData(new MoveableTrolleyB(_drawningTrolleyB), pictureBoxTrolleybus.Width, pictureBoxTrolleybus.Height);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (_strategy == null)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
comboBoxStrategy.Enabled = false;
|
||||||
|
_strategy.MakeStep();
|
||||||
|
Draw();
|
||||||
|
|
||||||
|
if (_strategy.GetStatus() == StrategyStatus.Finish)
|
||||||
|
{
|
||||||
|
comboBoxStrategy.Enabled = true;
|
||||||
|
_strategy = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,123 @@
|
|||||||
|
namespace ProjectTrolleybus.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>
|
||||||
|
/// <returns></returns>
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void MakeStep()
|
||||||
|
{
|
||||||
|
if (_state != StrategyStatus.InProgress)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (IsTargetDestination())
|
||||||
|
{
|
||||||
|
_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;
|
||||||
|
|
||||||
|
protected int? GetStep()
|
||||||
|
{
|
||||||
|
if (_state != StrategyStatus.InProgress)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return _moveableObject?.GetStep;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Перемещение к цели
|
||||||
|
/// </summary>
|
||||||
|
protected abstract void MoveToTarget();
|
||||||
|
|
||||||
|
protected abstract bool IsTargetDestination();
|
||||||
|
|
||||||
|
protected bool MoveTo(MovementDirection movementDirection)
|
||||||
|
{
|
||||||
|
if (_state != StrategyStatus.InProgress)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return _moveableObject?.TryMoveObject(movementDirection) ?? false;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,24 @@
|
|||||||
|
namespace ProjectTrolleybus.MovementStrategy;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Интерфейс для работы с перемещаемым объектом
|
||||||
|
/// </summary>
|
||||||
|
public interface IMoveableObject
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Получение координаты объекта
|
||||||
|
/// </summary>
|
||||||
|
ObjectParameters? GetObjectPosition { get; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Шаг объекта
|
||||||
|
/// </summary>
|
||||||
|
int GetStep { get; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Попытка переместить объект в указанном направлении
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="direction"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
bool TryMoveObject(MovementDirection direction);
|
||||||
|
}
|
@ -0,0 +1,56 @@
|
|||||||
|
namespace ProjectTrolleybus.MovementStrategy;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Стратегия перемещения объекта к границам
|
||||||
|
/// </summary>
|
||||||
|
public class MoveToBorder : AbstractStrategy
|
||||||
|
{
|
||||||
|
protected override bool IsTargetDestination()
|
||||||
|
{
|
||||||
|
|
||||||
|
ObjectParameters? objParams = GetObjectParameters;
|
||||||
|
if (objParams == null)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return objParams.RightBorder <= FieldWidth && objParams.RightBorder + GetStep() >= FieldWidth &&
|
||||||
|
objParams.DownBorder <= FieldHeight &&
|
||||||
|
objParams.DownBorder + GetStep() >= FieldHeight;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override void MoveToTarget()
|
||||||
|
{
|
||||||
|
ObjectParameters? objParams = GetObjectParameters;
|
||||||
|
if (objParams == null)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
int diffX = objParams.RightBorder - FieldWidth;
|
||||||
|
if (Math.Abs(diffX) > GetStep())
|
||||||
|
{
|
||||||
|
if (diffX > 0)
|
||||||
|
{
|
||||||
|
MoveLeft();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
MoveRight();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int diffY = objParams.DownBorder - FieldHeight;
|
||||||
|
if (Math.Abs(diffY) > GetStep())
|
||||||
|
{
|
||||||
|
if (diffY > 0)
|
||||||
|
{
|
||||||
|
MoveUp();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
MoveDown();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,54 @@
|
|||||||
|
namespace ProjectTrolleybus.MovementStrategy;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Стратегия перемещения объекта к центру
|
||||||
|
/// </summary>
|
||||||
|
public class MoveToCenter : AbstractStrategy
|
||||||
|
{
|
||||||
|
protected override bool IsTargetDestination()
|
||||||
|
{
|
||||||
|
ObjectParameters? objParams = GetObjectParameters;
|
||||||
|
if (objParams == null)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return objParams.ObjectMiddleHorizontal - GetStep() <= FieldWidth / 2 && objParams.ObjectMiddleVertical + 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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,69 @@
|
|||||||
|
using ProjectTrolleybus.Drawnings;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace ProjectTrolleybus.MovementStrategy;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Класс-реализация IMoveableObject с использованием DrawningTrolleyB
|
||||||
|
/// </summary>
|
||||||
|
public class MoveableTrolleyB : IMoveableObject
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Поле-объект класса DrawningTrolleyB или его наследника
|
||||||
|
/// </summary>
|
||||||
|
private readonly DrawningTrolleyB? _trolleyB = null;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Конструктор
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="trolleyB">Объект класса DrawningTrolleyB</param>
|
||||||
|
public MoveableTrolleyB(DrawningTrolleyB trolleyB)
|
||||||
|
{
|
||||||
|
_trolleyB = trolleyB;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ObjectParameters? GetObjectPosition
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
if (_trolleyB == null || _trolleyB.EntityTrolleyB == null || !_trolleyB.GetPosX.HasValue || !_trolleyB.GetPosY.HasValue)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return new ObjectParameters(_trolleyB.GetPosX.Value, _trolleyB.GetPosY.Value, _trolleyB.GetWidth, _trolleyB.GetHight);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public int GetStep => (int)(_trolleyB?.EntityTrolleyB?.Step ?? 0);
|
||||||
|
|
||||||
|
public bool TryMoveObject(MovementDirection direction)
|
||||||
|
{
|
||||||
|
if (_trolleyB == null || _trolleyB.EntityTrolleyB == null)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return _trolleyB.MoveVehicle(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,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,27 @@
|
|||||||
|
namespace ProjectTrolleybus.MovementStrategy;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Направление перемещения
|
||||||
|
/// </summary>
|
||||||
|
public enum MovementDirection
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Вверх
|
||||||
|
/// </summary>
|
||||||
|
Up = 1,
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Вниз
|
||||||
|
/// </summary>
|
||||||
|
Down = 2,
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Влево
|
||||||
|
/// </summary>
|
||||||
|
Left = 3,
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Вправо
|
||||||
|
/// </summary>
|
||||||
|
Right = 4
|
||||||
|
}
|
@ -0,0 +1,62 @@
|
|||||||
|
namespace ProjectTrolleybus.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;
|
||||||
|
|
||||||
|
public ObjectParameters(int x, int y, int width, int height)
|
||||||
|
{
|
||||||
|
_x = x;
|
||||||
|
_y = y;
|
||||||
|
_width = width;
|
||||||
|
_height = height;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,19 @@
|
|||||||
|
namespace ProjectTrolleybus.MovementStrategy;
|
||||||
|
|
||||||
|
public enum StrategyStatus
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Все готово к началу
|
||||||
|
/// </summary>
|
||||||
|
NotInit,
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Выполняется
|
||||||
|
/// </summary>
|
||||||
|
InProgress,
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Завершено
|
||||||
|
/// </summary>
|
||||||
|
Finish
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user