Добавление стратегии
This commit is contained in:
parent
4adc299af9
commit
36bd6dfbb9
@ -11,6 +11,10 @@ namespace AccordionBus.Drawnings
|
||||
/// </summary>
|
||||
public enum DirectionType
|
||||
{
|
||||
/// <summary>
|
||||
/// Неизвестное направление
|
||||
/// </summary>
|
||||
Unknow = -1,
|
||||
/// <summary>
|
||||
/// Вверх
|
||||
/// </summary>
|
||||
|
@ -39,6 +39,26 @@ namespace AccordionBus.Drawnings
|
||||
/// </summary>
|
||||
private readonly int _drawningBusHeight = 20;
|
||||
/// <summary>
|
||||
/// Координата Х объекта
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public int? GetPosX() => _startPosX;
|
||||
/// <summary>
|
||||
/// Координата Y объекта
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public int? GetPosY() => _startPosY;
|
||||
/// <summary>
|
||||
/// Ширина объекта
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public int GetWidth() => _drawningBusWeight;
|
||||
/// <summary>
|
||||
/// Высота объекта
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public int GetHeight() => _drawningBusHeight;
|
||||
/// <summary>
|
||||
/// Пустой конструктор
|
||||
/// </summary>
|
||||
private DrawningBus()
|
||||
|
@ -35,6 +35,8 @@
|
||||
ButtonLeft = new Button();
|
||||
ButtonDown = new Button();
|
||||
buttonCreateBus = new Button();
|
||||
comboBoxStratregy = new ComboBox();
|
||||
buttonStrategyStap = new Button();
|
||||
((System.ComponentModel.ISupportInitialize)pictureBoxAccordionBus).BeginInit();
|
||||
SuspendLayout();
|
||||
//
|
||||
@ -122,11 +124,33 @@
|
||||
buttonCreateBus.UseVisualStyleBackColor = true;
|
||||
buttonCreateBus.Click += ButtonCreateBus_Click;
|
||||
//
|
||||
// comboBoxStratregy
|
||||
//
|
||||
comboBoxStratregy.DropDownStyle = ComboBoxStyle.DropDownList;
|
||||
comboBoxStratregy.FormattingEnabled = true;
|
||||
comboBoxStratregy.Items.AddRange(new object[] { "К центру", "К краю" });
|
||||
comboBoxStratregy.Location = new Point(688, 12);
|
||||
comboBoxStratregy.Name = "comboBoxStratregy";
|
||||
comboBoxStratregy.Size = new Size(151, 28);
|
||||
comboBoxStratregy.TabIndex = 7;
|
||||
//
|
||||
// buttonStrategyStap
|
||||
//
|
||||
buttonStrategyStap.Location = new Point(761, 46);
|
||||
buttonStrategyStap.Name = "buttonStrategyStap";
|
||||
buttonStrategyStap.Size = new Size(78, 29);
|
||||
buttonStrategyStap.TabIndex = 8;
|
||||
buttonStrategyStap.Text = "Шаг";
|
||||
buttonStrategyStap.UseVisualStyleBackColor = true;
|
||||
buttonStrategyStap.Click += buttonStrategyStap_Click;
|
||||
//
|
||||
// FormAccordionBus
|
||||
//
|
||||
AutoScaleDimensions = new SizeF(8F, 20F);
|
||||
AutoScaleMode = AutoScaleMode.Font;
|
||||
ClientSize = new Size(882, 453);
|
||||
Controls.Add(buttonStrategyStap);
|
||||
Controls.Add(comboBoxStratregy);
|
||||
Controls.Add(buttonCreateBus);
|
||||
Controls.Add(ButtonDown);
|
||||
Controls.Add(ButtonLeft);
|
||||
@ -151,5 +175,7 @@
|
||||
private Button ButtonLeft;
|
||||
private Button ButtonDown;
|
||||
private Button buttonCreateBus;
|
||||
private ComboBox comboBoxStratregy;
|
||||
private Button buttonStrategyStap;
|
||||
}
|
||||
}
|
@ -8,15 +8,18 @@ using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Forms;
|
||||
using AccordionBus.Drawnings;
|
||||
using AccordionBus.MovementStrategy;
|
||||
|
||||
namespace AccordionBus
|
||||
{
|
||||
public partial class FormAccordionBus : Form
|
||||
{
|
||||
private DrawningBus? _drawningBus;
|
||||
private AbstractStrategy? _strategy;
|
||||
public FormAccordionBus()
|
||||
{
|
||||
InitializeComponent();
|
||||
_strategy = null;
|
||||
}
|
||||
|
||||
private void Draw()
|
||||
@ -43,15 +46,17 @@ namespace AccordionBus
|
||||
case nameof(DrawningAccordionBus):
|
||||
_drawningBus = new DrawningAccordionBus(random.Next(100, 300), random.Next(1000, 3000),
|
||||
Color.FromArgb(random.Next(0, 255), random.Next(0, 255), random.Next(0, 255)),
|
||||
Color.FromArgb(random.Next(0, 255), random.Next(0, 255), random.Next(0, 255)),
|
||||
Color.FromArgb(random.Next(0, 255), random.Next(0, 255), random.Next(0, 255)),
|
||||
Convert.ToBoolean(random.Next(0, 2)), Convert.ToBoolean(random.Next(0, 2)));
|
||||
break;
|
||||
default:
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
_drawningBus.SetPictureSize(pictureBoxAccordionBus.Width, pictureBoxAccordionBus.Height);
|
||||
_drawningBus.SetPosition(random.Next(50, 300), random.Next(50, 300));
|
||||
_strategy = null;
|
||||
comboBoxStratregy.Enabled = true;
|
||||
Draw();
|
||||
}
|
||||
|
||||
@ -94,5 +99,32 @@ namespace AccordionBus
|
||||
Draw();
|
||||
}
|
||||
}
|
||||
|
||||
private void buttonStrategyStap_Click(object sender, EventArgs e)
|
||||
{
|
||||
if (_drawningBus == null) return;
|
||||
if (comboBoxStratregy.Enabled)
|
||||
{
|
||||
_strategy = comboBoxStratregy.SelectedIndex switch
|
||||
{
|
||||
0 => new MoveToCenter(),
|
||||
1 => new MoveToBorder(),
|
||||
_ => null
|
||||
};
|
||||
if (_strategy == null) return;
|
||||
_strategy.SetData(new MoveableBus(_drawningBus), pictureBoxAccordionBus.Width, pictureBoxAccordionBus.Height);
|
||||
}
|
||||
|
||||
if (_strategy == null) return;
|
||||
comboBoxStratregy.Enabled = false;
|
||||
_strategy.MakeStap();
|
||||
Draw();
|
||||
|
||||
if (_strategy.GetStatus() == StrategyStatus.Finish)
|
||||
{
|
||||
comboBoxStratregy.Enabled = true;
|
||||
_strategy = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
125
AccordionBus/AccordionBus/MovementStrategy/AbstractStrategy.cs
Normal file
125
AccordionBus/AccordionBus/MovementStrategy/AbstractStrategy.cs
Normal file
@ -0,0 +1,125 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace AccordionBus.MovementStrategy
|
||||
{
|
||||
public abstract class AbstractStrategy
|
||||
{
|
||||
/// <summary>
|
||||
/// Объект
|
||||
/// </summary>
|
||||
private IMoveableObjects? _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(IMoveableObjects 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 MakeStap()
|
||||
{
|
||||
if (_state != StrategyStatus.InProgress) return;
|
||||
if (IsTargetDestination())
|
||||
{
|
||||
_state = StrategyStatus.Finish;
|
||||
return;
|
||||
}
|
||||
MoveToTarget();
|
||||
}
|
||||
/// <summary>
|
||||
/// Шаг влево
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
protected bool MoveLeft() => MoveTo(MovementDirection.Left);
|
||||
/// <summary>
|
||||
/// Шаг вправо
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
protected bool MoveRight() => MoveTo(MovementDirection.Right);
|
||||
/// <summary>
|
||||
/// Шаг вверх
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
protected bool MoveUp() => MoveTo(MovementDirection.Up);
|
||||
/// <summary>
|
||||
/// Шаг вниз
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
protected bool MoveDown() => MoveTo(MovementDirection.Down);
|
||||
/// <summary>
|
||||
/// Получение параметров
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
protected ObjectParameters? GetObjectParameters() => _moveableObject?.GetObjectPosition;
|
||||
/// <summary>
|
||||
/// Получение шага
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
protected int? GetStap()
|
||||
{
|
||||
if(_state != StrategyStatus.InProgress)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return _moveableObject?.GetStep;
|
||||
}
|
||||
/// <summary>
|
||||
/// Шаг к цели
|
||||
/// </summary>
|
||||
protected abstract void MoveToTarget();
|
||||
/// <summary>
|
||||
/// Достижение цели
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
protected abstract bool IsTargetDestination();
|
||||
/// <summary>
|
||||
/// Сделать шаг
|
||||
/// </summary>
|
||||
/// <param name="movementDirection"></param>
|
||||
/// <returns></returns>
|
||||
private bool MoveTo(MovementDirection movementDirection)
|
||||
{
|
||||
if(_state != StrategyStatus.InProgress)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return _moveableObject?.TryMoveObject(movementDirection) ?? false;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,29 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace AccordionBus.MovementStrategy
|
||||
{
|
||||
/// <summary>
|
||||
/// Интерфейс для работы с пперемещаемым объектом
|
||||
/// </summary>
|
||||
public interface IMoveableObjects
|
||||
{
|
||||
/// <summary>
|
||||
/// Получение координаты объекта
|
||||
/// </summary>
|
||||
ObjectParameters? GetObjectPosition { get; }
|
||||
/// <summary>
|
||||
/// Шаг объекта
|
||||
/// </summary>
|
||||
int GetStep { get; }
|
||||
/// <summary>
|
||||
/// попытка переместить объект в указаном направлении
|
||||
/// </summary>
|
||||
/// <param name="direction">Направление</param>
|
||||
/// <returns>true - перемещение удалось false - перемещение невозможно</returns>
|
||||
bool TryMoveObject(MovementDirection direction);
|
||||
}
|
||||
}
|
41
AccordionBus/AccordionBus/MovementStrategy/MoveToBorder.cs
Normal file
41
AccordionBus/AccordionBus/MovementStrategy/MoveToBorder.cs
Normal file
@ -0,0 +1,41 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace AccordionBus.MovementStrategy
|
||||
{
|
||||
public class MoveToBorder : AbstractStrategy
|
||||
{
|
||||
protected override bool IsTargetDestination()
|
||||
{
|
||||
ObjectParameters? objParams = GetObjectParameters();
|
||||
if (objParams == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return objParams.LeftBorder() == 0 || objParams.RightBorder() == FieldWidth ||
|
||||
objParams.TopBorder() == 0 || objParams.DownBorder() == FieldHeight;
|
||||
}
|
||||
|
||||
protected override void MoveToTarget()
|
||||
{
|
||||
ObjectParameters? objParams = GetObjectParameters();
|
||||
if (objParams == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
int _raznLeft = 0 - objParams.RightBorder();
|
||||
int _raznRight = objParams.LeftBorder() - FieldWidth;
|
||||
int _raznUp = 0 - objParams.TopBorder();
|
||||
int _raznDown = objParams.DownBorder() - FieldHeight;
|
||||
|
||||
if (_raznLeft > _raznRight && _raznLeft > _raznDown && _raznLeft > _raznUp) MoveLeft();
|
||||
if (_raznRight > _raznLeft && _raznRight > _raznDown && _raznRight > _raznUp) MoveRight();
|
||||
if (_raznUp > _raznRight && _raznUp > _raznLeft && _raznUp > _raznDown) MoveUp();
|
||||
if (_raznDown > _raznLeft && _raznDown > _raznRight && _raznDown > _raznUp) MoveDown();
|
||||
}
|
||||
}
|
||||
}
|
56
AccordionBus/AccordionBus/MovementStrategy/MoveToCenter.cs
Normal file
56
AccordionBus/AccordionBus/MovementStrategy/MoveToCenter.cs
Normal file
@ -0,0 +1,56 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace AccordionBus.MovementStrategy
|
||||
{
|
||||
public class MoveToCenter : AbstractStrategy
|
||||
{
|
||||
protected override bool IsTargetDestination()
|
||||
{
|
||||
ObjectParameters? objParams = GetObjectParameters();
|
||||
if (objParams == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return objParams.ObjectMiddleHorizontal() - GetStap() <= FieldWidth / 2
|
||||
&& objParams.ObjectMiddleHorizontal() + GetStap() >= FieldWidth / 2 &&
|
||||
objParams.ObjectMiddleVertical() - GetStap() <= FieldHeight / 2
|
||||
&& objParams.ObjectMiddleVertical() + GetStap() >= FieldHeight / 2;
|
||||
}
|
||||
protected override void MoveToTarget()
|
||||
{
|
||||
ObjectParameters? objParams = GetObjectParameters();
|
||||
if (objParams == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
int diffX = objParams.ObjectMiddleHorizontal() - FieldWidth / 2;
|
||||
if (Math.Abs(diffX) > GetStap())
|
||||
{
|
||||
if (diffX > 0)
|
||||
{
|
||||
MoveLeft();
|
||||
}
|
||||
else
|
||||
{
|
||||
MoveRight();
|
||||
}
|
||||
}
|
||||
int diffY = objParams.ObjectMiddleVertical() - FieldHeight / 2;
|
||||
if (Math.Abs(diffY) > GetStap())
|
||||
{
|
||||
if (diffY > 0)
|
||||
{
|
||||
MoveUp();
|
||||
}
|
||||
else
|
||||
{
|
||||
MoveDown();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
67
AccordionBus/AccordionBus/MovementStrategy/MoveableBus.cs
Normal file
67
AccordionBus/AccordionBus/MovementStrategy/MoveableBus.cs
Normal file
@ -0,0 +1,67 @@
|
||||
using AccordionBus.Drawnings;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace AccordionBus.MovementStrategy
|
||||
{
|
||||
/// <summary>
|
||||
/// Класс-реализация IMoveableObjects с использованием DrawningBus
|
||||
/// </summary>
|
||||
public class MoveableBus : IMoveableObjects
|
||||
{
|
||||
/// <summary>
|
||||
/// Поле-объект DrawningBus
|
||||
/// </summary>
|
||||
private readonly DrawningBus? _car;
|
||||
/// <summary>
|
||||
/// Конструктор
|
||||
/// </summary>
|
||||
/// <param name="car">Объект класса DrawningBus</param>
|
||||
public MoveableBus(DrawningBus? car)
|
||||
{
|
||||
_car = car;
|
||||
}
|
||||
|
||||
public ObjectParameters? GetObjectPosition
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_car == null || _car.EntityBus == null || _car.GetPosX() == null || _car.GetPosY() == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return new ObjectParameters(_car.GetPosX().Value, _car.GetPosY().Value, _car.GetWidth(), _car.GetHeight());
|
||||
}
|
||||
}
|
||||
|
||||
public int GetStep => (int)(_car?.EntityBus?.Step ?? 0);
|
||||
|
||||
public bool TryMoveObject(MovementDirection direction)
|
||||
{
|
||||
if (_car == null || _car.EntityBus == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return _car.MoveTransport(GetDirectionType(direction));
|
||||
}
|
||||
/// <summary>
|
||||
/// Конвертация из MovementDirection в DirectionType
|
||||
/// </summary>
|
||||
/// <param name="direction"></param>
|
||||
/// <returns></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,28 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace AccordionBus.MovementStrategy
|
||||
{
|
||||
public enum MovementDirection
|
||||
{
|
||||
/// <summary>
|
||||
/// Вверх
|
||||
/// </summary>
|
||||
Up = 1,
|
||||
/// <summary>
|
||||
/// Вниз
|
||||
/// </summary>
|
||||
Down = 2,
|
||||
/// <summary>
|
||||
/// Влево
|
||||
/// </summary>
|
||||
Left = 3,
|
||||
/// <summary>
|
||||
/// Вправо
|
||||
/// </summary>
|
||||
Right = 4
|
||||
}
|
||||
}
|
@ -0,0 +1,75 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace AccordionBus.MovementStrategy
|
||||
{
|
||||
/// <summary>
|
||||
/// Параметры-координаты объекта
|
||||
/// </summary>
|
||||
public class ObjectParameters
|
||||
{
|
||||
/// <summary>
|
||||
/// Координата Х
|
||||
/// </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>
|
||||
/// <returns></returns>
|
||||
public int LeftBorder() => _x;
|
||||
/// <summary>
|
||||
/// Верхняя граница
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public int TopBorder() => _y;
|
||||
/// <summary>
|
||||
/// Правая граница
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public int RightBorder() => _x + _width;
|
||||
/// <summary>
|
||||
/// Нижняя граница
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public int DownBorder() => _y + _height;
|
||||
/// <summary>
|
||||
/// Середина объекта
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public int ObjectMiddleHorizontal() => _x + _width / 2;
|
||||
/// <summary>
|
||||
/// Середина объекта
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public int ObjectMiddleVertical() => _y + _height / 2;
|
||||
/// <summary>
|
||||
/// Конструктор
|
||||
/// </summary>
|
||||
/// <param name="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;
|
||||
}
|
||||
}
|
||||
}
|
24
AccordionBus/AccordionBus/MovementStrategy/StrategyStatus.cs
Normal file
24
AccordionBus/AccordionBus/MovementStrategy/StrategyStatus.cs
Normal file
@ -0,0 +1,24 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace AccordionBus.MovementStrategy
|
||||
{
|
||||
public enum StrategyStatus
|
||||
{
|
||||
/// <summary>
|
||||
/// всё готово к началу
|
||||
/// </summary>
|
||||
NotInit,
|
||||
/// <summary>
|
||||
/// В процессе
|
||||
/// </summary>
|
||||
InProgress,
|
||||
/// <summary>
|
||||
/// Завершено
|
||||
/// </summary>
|
||||
Finish
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user