Добавление стратегии
This commit is contained in:
parent
b10750e801
commit
eeb2f04169
@ -10,6 +10,11 @@ namespace Monorail.Drawnings;
|
||||
/// </summary>
|
||||
public enum DirectionType
|
||||
{
|
||||
/// <summary>
|
||||
/// Неизвестное направление
|
||||
/// </summary>
|
||||
Unknow = -1,
|
||||
|
||||
/// <summary>
|
||||
/// Вверх
|
||||
/// </summary>
|
||||
|
@ -7,12 +7,15 @@ using System.Threading.Tasks;
|
||||
|
||||
namespace Monorail.Drawnings;
|
||||
|
||||
public class Drawning_Monorail
|
||||
/// <summary>
|
||||
/// Класс, отвечающий за прорисовку и перемещение базового объекта-сущности
|
||||
/// </summary>
|
||||
public class DrawningLocomotive
|
||||
{
|
||||
/// <summary>
|
||||
/// Класс-сущность (объект)
|
||||
/// </summary>
|
||||
public Entity_Monorail? Entity_Monorail { get; protected set; }
|
||||
public EntityLocomotive? EntityLocomotive { get; protected set; }
|
||||
|
||||
/// <summary>
|
||||
/// Ширина окна
|
||||
@ -42,11 +45,33 @@ public class Drawning_Monorail
|
||||
/// </summary>
|
||||
private readonly int _drawingMonorailHeight = 40;
|
||||
|
||||
/// <summary>
|
||||
/// Координата X объекта
|
||||
/// </summary>
|
||||
public int? GetPosX => _startPosX;
|
||||
|
||||
/// <summary>
|
||||
/// Координата Y объекта
|
||||
/// </summary>
|
||||
public int? GetPosY => _startPosY;
|
||||
|
||||
/// <summary>
|
||||
/// Ширина объекта
|
||||
/// </summary>
|
||||
public int GetWidth => _drawningMonorailWidth;
|
||||
|
||||
/// <summary>
|
||||
/// Высота объекта
|
||||
/// </summary>
|
||||
public int GetHeight => _drawingMonorailHeight;
|
||||
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Пустой конструктор
|
||||
/// </summary>
|
||||
private Drawning_Monorail()
|
||||
private DrawningLocomotive()
|
||||
{
|
||||
_pictureWidth = null;
|
||||
_pictureHeight = null;
|
||||
@ -63,9 +88,9 @@ public class Drawning_Monorail
|
||||
/// <param name="bodyColor">Основной цвет</param>
|
||||
|
||||
|
||||
public Drawning_Monorail(int speed, double weight, Color bodyColor) : this()
|
||||
public DrawningLocomotive(int speed, double weight, Color bodyColor) : this()
|
||||
{
|
||||
Entity_Monorail = new Entity_Monorail(speed, weight, bodyColor);
|
||||
EntityLocomotive = new EntityLocomotive(speed, weight, bodyColor);
|
||||
|
||||
}
|
||||
|
||||
@ -76,7 +101,7 @@ public class Drawning_Monorail
|
||||
/// <param name="drawingMonorailHeight">Высота прорисовки монорельса (размер объекта)</param>
|
||||
|
||||
|
||||
protected Drawning_Monorail(int drawningMonorailWidth, int drawingMonorailHeight) : this()
|
||||
protected DrawningLocomotive(int drawningMonorailWidth, int drawingMonorailHeight) : this()
|
||||
{
|
||||
_drawningMonorailWidth = drawningMonorailWidth;
|
||||
_drawingMonorailHeight = drawingMonorailHeight;
|
||||
@ -164,7 +189,7 @@ public class Drawning_Monorail
|
||||
/// <returns>true - перемещение выполнено, false - перемещение невозможно</returns>
|
||||
public bool MoveTransport(DirectionType direction)
|
||||
{
|
||||
if (Entity_Monorail == null || !_startPosX.HasValue || !_startPosY.HasValue)
|
||||
if (EntityLocomotive == null || !_startPosX.HasValue || !_startPosY.HasValue)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
@ -172,33 +197,33 @@ public class Drawning_Monorail
|
||||
{
|
||||
//влево
|
||||
case DirectionType.Left:
|
||||
if (_startPosX.Value - Entity_Monorail.Step > 0)
|
||||
if (_startPosX.Value - EntityLocomotive.Step > 0)
|
||||
{
|
||||
_startPosX -= (int)Entity_Monorail.Step;
|
||||
_startPosX -= (int)EntityLocomotive.Step;
|
||||
}
|
||||
return true;
|
||||
//вверх
|
||||
case DirectionType.Up:
|
||||
if (_startPosY.Value - Entity_Monorail.Step > 0)
|
||||
if (_startPosY.Value - EntityLocomotive.Step > 0)
|
||||
{
|
||||
_startPosY -= (int)Entity_Monorail.Step;
|
||||
_startPosY -= (int)EntityLocomotive.Step;
|
||||
}
|
||||
return true;
|
||||
// вправо
|
||||
case DirectionType.Right:
|
||||
// TODO прописать логику сдвига в право
|
||||
if (_startPosX.Value + Entity_Monorail.Step + _drawningMonorailWidth < _pictureWidth)
|
||||
if (_startPosX.Value + EntityLocomotive.Step + _drawningMonorailWidth < _pictureWidth)
|
||||
{
|
||||
_startPosX += (int)Entity_Monorail.Step;
|
||||
_startPosX += (int)EntityLocomotive.Step;
|
||||
}
|
||||
|
||||
return true;
|
||||
//вниз
|
||||
case DirectionType.Down:
|
||||
//TODO прописать логику сдвига в вниз
|
||||
if (_startPosY.Value + Entity_Monorail.Step + _drawingMonorailHeight < _pictureHeight)
|
||||
if (_startPosY.Value + EntityLocomotive.Step + _drawingMonorailHeight < _pictureHeight)
|
||||
{
|
||||
_startPosY += (int)Entity_Monorail.Step;
|
||||
_startPosY += (int)EntityLocomotive.Step;
|
||||
}
|
||||
return true;
|
||||
default:
|
||||
@ -214,7 +239,7 @@ public class Drawning_Monorail
|
||||
///
|
||||
public virtual void DrawTransport(Graphics g)
|
||||
{
|
||||
if (Entity_Monorail == null || !_startPosX.HasValue || !_startPosY.HasValue)
|
||||
if (EntityLocomotive == null || !_startPosX.HasValue || !_startPosY.HasValue)
|
||||
{
|
||||
return;
|
||||
}
|
||||
@ -225,7 +250,7 @@ public class Drawning_Monorail
|
||||
|
||||
|
||||
//границы Монорельса
|
||||
Brush br = new SolidBrush(Entity_Monorail.BodyColor);
|
||||
Brush br = new SolidBrush(EntityLocomotive.BodyColor);
|
||||
g.DrawRectangle(pen, _startPosX.Value + 5, _startPosY.Value + 15, 80, 15);
|
||||
Point[] points1 = {
|
||||
new Point(_startPosX.Value + 15, _startPosY.Value),
|
@ -4,7 +4,7 @@ namespace Monorail.Drawnings;
|
||||
/// <summary>
|
||||
/// Класс отвечающий за прорисовку и перемещение объекта-сущности
|
||||
/// </summary>
|
||||
public class DrawningMonorail : Drawning_Monorail
|
||||
public class DrawningMonorail : DrawningLocomotive
|
||||
{
|
||||
/// <summary>
|
||||
/// Конструктор
|
||||
@ -16,14 +16,14 @@ public class DrawningMonorail : Drawning_Monorail
|
||||
/// <param name="magneticRail">магнитный рельс</param>
|
||||
/// <param name="secondCabin">Вторая кабинка в задней части</param>
|
||||
|
||||
public DrawningMonorail(int speed, double weight, Color bodyColor, Color additionalColor, bool magneticRail, bool secondCabin) : base(95, 40)
|
||||
public DrawningMonorail(int speed, double weight, Color bodyColor, Color additionalColor, bool magneticRail, bool secondCabin) : base(90, 40)
|
||||
{
|
||||
Entity_Monorail = new EntityMonorail(speed, weight, bodyColor, additionalColor, magneticRail, secondCabin);
|
||||
EntityLocomotive = new EntityMonorail(speed, weight, bodyColor, additionalColor, magneticRail, secondCabin);
|
||||
}
|
||||
|
||||
public override void DrawTransport(Graphics g)
|
||||
{
|
||||
if (Entity_Monorail == null || Entity_Monorail is not EntityMonorail monorail || !_startPosX.HasValue || !_startPosY.HasValue)
|
||||
if (EntityLocomotive == null || EntityLocomotive is not EntityMonorail monorail || !_startPosX.HasValue || !_startPosY.HasValue)
|
||||
{
|
||||
return;
|
||||
}
|
||||
@ -56,12 +56,7 @@ public class DrawningMonorail : Drawning_Monorail
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
//_startPosX += 10;
|
||||
//_startPosY += 5;
|
||||
base.DrawTransport(g);
|
||||
//_startPosX -= 10;
|
||||
//_startPosY -= 5;
|
||||
|
||||
}
|
||||
}
|
@ -7,9 +7,9 @@ using System.Threading.Tasks;
|
||||
namespace Monorail.Entities;
|
||||
|
||||
/// <summary>
|
||||
/// Класс-сущносить "Монорельс"
|
||||
/// Класс-сущносить "Локомотив"
|
||||
/// </summary>
|
||||
public class Entity_Monorail
|
||||
public class EntityLocomotive
|
||||
{
|
||||
/// <summary>
|
||||
/// скорость
|
||||
@ -37,7 +37,7 @@ public class Entity_Monorail
|
||||
/// <param name="weight">Вес</param>
|
||||
/// <param name="bodyColor">Основной цвет</param>
|
||||
|
||||
public Entity_Monorail(int speed, double weight, Color bodyColor)
|
||||
public EntityLocomotive(int speed, double weight, Color bodyColor)
|
||||
{
|
||||
Speed = speed;
|
||||
Weight = weight;
|
@ -2,7 +2,7 @@
|
||||
/// <summary>
|
||||
/// Класс-сущность "Монорельс"
|
||||
/// </summary>
|
||||
public class EntityMonorail : Entity_Monorail
|
||||
public class EntityMonorail : EntityLocomotive
|
||||
{
|
||||
/// <summary>
|
||||
/// дополнительный цвет
|
||||
@ -37,11 +37,4 @@ public class EntityMonorail : Entity_Monorail
|
||||
SecondCabin = secondCabin;
|
||||
|
||||
}
|
||||
|
||||
public void Init(int speed, double weight, Color bodyColor, Color additionalColor, bool magneticRail, bool secondCabin)
|
||||
{
|
||||
AdditionalColor = additionalColor;
|
||||
MagneticRail = magneticRail;
|
||||
SecondCabin = secondCabin;
|
||||
}
|
||||
}
|
51
Monorail/Monorail/FormMonorail.Designer.cs
generated
51
Monorail/Monorail/FormMonorail.Designer.cs
generated
@ -34,7 +34,9 @@
|
||||
buttonDown = new Button();
|
||||
buttonRight = new Button();
|
||||
buttonUp = new Button();
|
||||
buttonCreateMonorail = new Button();
|
||||
buttonCreateLocomotive = new Button();
|
||||
comboBoxStrategy = new ComboBox();
|
||||
buttonStrategyStep = new Button();
|
||||
((System.ComponentModel.ISupportInitialize)pictureBox1Monorail).BeginInit();
|
||||
SuspendLayout();
|
||||
//
|
||||
@ -54,7 +56,7 @@
|
||||
buttonCreate.Name = "buttonCreate";
|
||||
buttonCreate.Size = new Size(221, 23);
|
||||
buttonCreate.TabIndex = 1;
|
||||
buttonCreate.Text = "создать монорельс";
|
||||
buttonCreate.Text = "создать Monorail";
|
||||
buttonCreate.UseVisualStyleBackColor = true;
|
||||
buttonCreate.Click += buttonCreate_Click;
|
||||
//
|
||||
@ -106,22 +108,45 @@
|
||||
buttonUp.UseVisualStyleBackColor = true;
|
||||
buttonUp.Click += buttonMove_Click;
|
||||
//
|
||||
// buttonCreateMonorail
|
||||
// buttonCreateLocomotive
|
||||
//
|
||||
buttonCreateMonorail.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
|
||||
buttonCreateMonorail.Location = new Point(247, 358);
|
||||
buttonCreateMonorail.Name = "buttonCreateMonorail";
|
||||
buttonCreateMonorail.Size = new Size(221, 23);
|
||||
buttonCreateMonorail.TabIndex = 6;
|
||||
buttonCreateMonorail.Text = "создать monorail";
|
||||
buttonCreateMonorail.UseVisualStyleBackColor = true;
|
||||
buttonCreateLocomotive.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
|
||||
buttonCreateLocomotive.Location = new Point(247, 358);
|
||||
buttonCreateLocomotive.Name = "buttonCreateLocomotive";
|
||||
buttonCreateLocomotive.Size = new Size(221, 23);
|
||||
buttonCreateLocomotive.TabIndex = 6;
|
||||
buttonCreateLocomotive.Text = "создать Locomotive";
|
||||
buttonCreateLocomotive.UseVisualStyleBackColor = true;
|
||||
buttonCreateLocomotive.Click += ButtonCreateLocomotive_Click;
|
||||
//
|
||||
// comboBoxStrategy
|
||||
//
|
||||
comboBoxStrategy.DropDownStyle = ComboBoxStyle.DropDownList;
|
||||
comboBoxStrategy.FormattingEnabled = true;
|
||||
comboBoxStrategy.Items.AddRange(new object[] { "К центру", "К краю" });
|
||||
comboBoxStrategy.Location = new Point(625, 12);
|
||||
comboBoxStrategy.Name = "comboBoxStrategy";
|
||||
comboBoxStrategy.Size = new Size(121, 23);
|
||||
comboBoxStrategy.TabIndex = 7;
|
||||
//
|
||||
// buttonStrategyStep
|
||||
//
|
||||
buttonStrategyStep.Location = new Point(685, 41);
|
||||
buttonStrategyStep.Name = "buttonStrategyStep";
|
||||
buttonStrategyStep.Size = new Size(61, 23);
|
||||
buttonStrategyStep.TabIndex = 8;
|
||||
buttonStrategyStep.Text = "Шаг";
|
||||
buttonStrategyStep.UseVisualStyleBackColor = true;
|
||||
buttonStrategyStep.Click += buttonStrategyStep_Click;
|
||||
//
|
||||
// FormMonorail
|
||||
//
|
||||
AutoScaleDimensions = new SizeF(7F, 15F);
|
||||
AutoScaleMode = AutoScaleMode.Font;
|
||||
ClientSize = new Size(755, 393);
|
||||
Controls.Add(buttonCreateMonorail);
|
||||
Controls.Add(buttonStrategyStep);
|
||||
Controls.Add(comboBoxStrategy);
|
||||
Controls.Add(buttonCreateLocomotive);
|
||||
Controls.Add(buttonUp);
|
||||
Controls.Add(buttonRight);
|
||||
Controls.Add(buttonDown);
|
||||
@ -142,6 +167,8 @@
|
||||
private Button buttonDown;
|
||||
private Button buttonRight;
|
||||
private Button buttonUp;
|
||||
private Button buttonCreateMonorail;
|
||||
private Button buttonCreateLocomotive;
|
||||
private ComboBox comboBoxStrategy;
|
||||
private Button buttonStrategyStep;
|
||||
}
|
||||
}
|
@ -1,4 +1,5 @@
|
||||
using Monorail.Drawnings;
|
||||
using Monorail.MovementStrategy;
|
||||
|
||||
namespace Monorail;
|
||||
/// <summary>
|
||||
@ -9,7 +10,12 @@ public partial class FormMonorail : Form
|
||||
/// <summary>
|
||||
/// Поле-объект для прорисовки объекта
|
||||
/// </summary>
|
||||
private Drawning_Monorail? _drawning_Monorail;
|
||||
private DrawningLocomotive? _drawningLocomotive;
|
||||
|
||||
/// <summary>
|
||||
/// Стратегия перемещения
|
||||
/// </summary>
|
||||
private AbstractStrategy? _strategy;
|
||||
|
||||
/// <summary>
|
||||
/// конструктор формы
|
||||
@ -17,20 +23,21 @@ public partial class FormMonorail : Form
|
||||
public FormMonorail()
|
||||
{
|
||||
InitializeComponent();
|
||||
_strategy = null;
|
||||
}
|
||||
/// <summary>
|
||||
/// Метод прорисовки машины
|
||||
/// </summary>
|
||||
private void Draw()
|
||||
{
|
||||
if (_drawning_Monorail == null)
|
||||
if (_drawningLocomotive == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Bitmap bmp = new(pictureBox1Monorail.Width, pictureBox1Monorail.Height);
|
||||
Graphics gr = Graphics.FromImage(bmp);
|
||||
_drawning_Monorail.DrawTransport(gr);
|
||||
_drawningLocomotive.DrawTransport(gr);
|
||||
pictureBox1Monorail.Image = bmp;
|
||||
}
|
||||
|
||||
@ -42,18 +49,21 @@ public partial class FormMonorail : Form
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Создание объекта класса-перемещения
|
||||
/// </summary>
|
||||
/// <param name="type">Тип создаваемого объекта</param>
|
||||
private void CreateObject(string type)
|
||||
{
|
||||
Random random = new();
|
||||
switch (type)
|
||||
{
|
||||
case nameof(Drawning_Monorail):
|
||||
_drawning_Monorail = new Drawning_Monorail(random.Next(100, 300), random.Next(1000, 3000),
|
||||
case nameof(DrawningLocomotive):
|
||||
_drawningLocomotive = new DrawningLocomotive(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(DrawningMonorail):
|
||||
_drawning_Monorail = new DrawningMonorail(random.Next(100, 300), random.Next(1000, 3000),
|
||||
_drawningLocomotive = new DrawningMonorail(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)));
|
||||
@ -62,10 +72,10 @@ public partial class FormMonorail : Form
|
||||
return;
|
||||
}
|
||||
|
||||
_drawning_Monorail.SetPictureSize(pictureBox1Monorail.Width, pictureBox1Monorail.Height);
|
||||
_drawning_Monorail.SetPosition(random.Next(10, 100), random.Next(10, 100));
|
||||
//_strategy = null;
|
||||
//comboBoxStrategy.Enabled = true;
|
||||
_drawningLocomotive.SetPictureSize(pictureBox1Monorail.Width, pictureBox1Monorail.Height);
|
||||
_drawningLocomotive.SetPosition(random.Next(10, 100), random.Next(10, 100));
|
||||
_strategy = null;
|
||||
comboBoxStrategy.Enabled = true;
|
||||
Draw();
|
||||
}
|
||||
|
||||
@ -85,7 +95,7 @@ public partial class FormMonorail : Form
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="e"></param>
|
||||
private void ButtonCreateMonorail_Click(object sender, EventArgs e) => CreateObject(nameof(Drawning_Monorail));
|
||||
private void ButtonCreateLocomotive_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningLocomotive));
|
||||
|
||||
|
||||
/// <summary>
|
||||
@ -95,7 +105,7 @@ public partial class FormMonorail : Form
|
||||
/// <param name="e"></param>
|
||||
private void buttonMove_Click(object sender, EventArgs e)
|
||||
{
|
||||
if (_drawning_Monorail == null)
|
||||
if (_drawningLocomotive == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
@ -105,16 +115,16 @@ public partial class FormMonorail : Form
|
||||
switch (name)
|
||||
{
|
||||
case "buttonUp":
|
||||
result = _drawning_Monorail.MoveTransport(DirectionType.Up);
|
||||
result = _drawningLocomotive.MoveTransport(DirectionType.Up);
|
||||
break;
|
||||
case "buttonDown":
|
||||
result = _drawning_Monorail.MoveTransport(DirectionType.Down);
|
||||
result = _drawningLocomotive.MoveTransport(DirectionType.Down);
|
||||
break;
|
||||
case "buttonLeft":
|
||||
result = _drawning_Monorail.MoveTransport(DirectionType.Left);
|
||||
result = _drawningLocomotive.MoveTransport(DirectionType.Left);
|
||||
break;
|
||||
case "buttonRight":
|
||||
result = _drawning_Monorail.MoveTransport(DirectionType.Right);
|
||||
result = _drawningLocomotive.MoveTransport(DirectionType.Right);
|
||||
break;
|
||||
}
|
||||
if (result)
|
||||
@ -124,5 +134,46 @@ public partial class FormMonorail : Form
|
||||
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="e"></param>
|
||||
private void buttonStrategyStep_Click(object sender, EventArgs e)
|
||||
{
|
||||
if (_drawningLocomotive == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (comboBoxStrategy.Enabled)
|
||||
{
|
||||
_strategy = comboBoxStrategy.SelectedIndex switch
|
||||
{
|
||||
0 => new MoveToCenter(),
|
||||
1 => new MoveToBorder(),
|
||||
_ => null,
|
||||
};
|
||||
if (_strategy == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
_strategy.SetData(new MoveableLocomotive(_drawningLocomotive), pictureBox1Monorail.Width, pictureBox1Monorail.Height);
|
||||
}
|
||||
|
||||
if (_strategy == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
comboBoxStrategy.Enabled = false;
|
||||
_strategy.MakeStep();
|
||||
Draw();
|
||||
|
||||
if (_strategy.GetStatus() == StrategyStatus.Finish)
|
||||
{
|
||||
comboBoxStrategy.Enabled = true;
|
||||
_strategy = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
142
Monorail/Monorail/MovementStrategy/AbstractStrategy.cs
Normal file
142
Monorail/Monorail/MovementStrategy/AbstractStrategy.cs
Normal file
@ -0,0 +1,142 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Monorail.MovementStrategy;
|
||||
|
||||
public abstract class AbstractStrategy
|
||||
{
|
||||
/// <summary>
|
||||
/// Перемещаемый объект
|
||||
/// </summary>
|
||||
private IMoveableObject? _moveableObject;
|
||||
|
||||
/// <summary>
|
||||
/// Статус перемещения
|
||||
/// </summary>
|
||||
private StrategyStatus _state = StrategyStatus.NotInit;
|
||||
|
||||
/// <summary>
|
||||
/// Ширина поля
|
||||
/// </summary>
|
||||
protected int FieldWidth { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Высота поля
|
||||
/// </summary>
|
||||
protected int FieldHeight { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Статус перемещения
|
||||
/// </summary>
|
||||
public StrategyStatus GetStatus() { return _state; }
|
||||
|
||||
/// <summary>
|
||||
/// Установка данных
|
||||
/// </summary>
|
||||
/// <param name="moveableObject">Перемещаемый объект</param>
|
||||
/// <param name="width">Ширина поля</param>
|
||||
/// <param name="height">Высота поля</param>
|
||||
public void SetData(IMoveableObject moveableObject, int width, int height)
|
||||
{
|
||||
if (moveableObject == null)
|
||||
{
|
||||
_state = StrategyStatus.NotInit;
|
||||
return;
|
||||
}
|
||||
|
||||
_state = StrategyStatus.InProgress;
|
||||
_moveableObject = moveableObject;
|
||||
FieldWidth = width;
|
||||
FieldHeight = height;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Шаг перемещения
|
||||
/// </summary>
|
||||
public void MakeStep()
|
||||
{
|
||||
if (_state != StrategyStatus.InProgress)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (IsTargetDestinaion())
|
||||
{
|
||||
_state = StrategyStatus.Finish;
|
||||
return;
|
||||
}
|
||||
|
||||
MoveToTarget();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Перемещение влево
|
||||
/// </summary>
|
||||
/// <returns>Результат перемещения (true - удалось переместиться, false - неудача)</returns>
|
||||
protected bool MoveLeft() => MoveTo(MovementDirection.Left);
|
||||
|
||||
/// <summary>
|
||||
/// Перемещение вправо
|
||||
/// </summary>
|
||||
/// <returns>Результат перемещения (true - удалось переместиться, false - неудача)</returns>
|
||||
protected bool MoveRight() => MoveTo(MovementDirection.Right);
|
||||
|
||||
/// <summary>
|
||||
/// Перемещение вверх
|
||||
/// </summary>
|
||||
/// <returns>Результат перемещения (true - удалось переместиться, false - неудача)</returns>
|
||||
protected bool MoveUp() => MoveTo(MovementDirection.Up);
|
||||
|
||||
/// <summary>
|
||||
/// Перемещение вниз
|
||||
/// </summary>
|
||||
/// <returns>Результат перемещения (true - удалось переместиться, false - неудача)</returns>
|
||||
protected bool MoveDown() => MoveTo(MovementDirection.Down);
|
||||
|
||||
/// <summary>
|
||||
/// Параметры объекта
|
||||
/// </summary>
|
||||
protected ObjectParameters? GetObjectParameters => _moveableObject?.GetObjectPosition;
|
||||
|
||||
/// <summary>
|
||||
/// Шаг объекта
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
protected int? GetStep()
|
||||
{
|
||||
if (_state != StrategyStatus.InProgress)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return _moveableObject?.GetStep;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Перемещение к цели
|
||||
/// </summary>
|
||||
protected abstract void MoveToTarget();
|
||||
|
||||
/// <summary>
|
||||
/// Достигнута ли цель
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
protected abstract bool IsTargetDestinaion();
|
||||
|
||||
/// <summary>
|
||||
/// Попытка перемещения в требуемом направлении
|
||||
/// </summary>
|
||||
/// <param name="movementDirection">Направление</param>
|
||||
/// <returns>Результат попытки (true - удалось переместиться, false - неудача)</returns>
|
||||
private bool MoveTo(MovementDirection movementDirection)
|
||||
{
|
||||
if (_state != StrategyStatus.InProgress)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return _moveableObject?.TryMoveObject(movementDirection) ?? false;
|
||||
}
|
||||
}
|
30
Monorail/Monorail/MovementStrategy/IMoveableObject.cs
Normal file
30
Monorail/Monorail/MovementStrategy/IMoveableObject.cs
Normal file
@ -0,0 +1,30 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Monorail.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);
|
||||
}
|
37
Monorail/Monorail/MovementStrategy/MoveToBorder.cs
Normal file
37
Monorail/Monorail/MovementStrategy/MoveToBorder.cs
Normal file
@ -0,0 +1,37 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Monorail.MovementStrategy;
|
||||
|
||||
public class MoveToBorder : AbstractStrategy
|
||||
{
|
||||
protected override bool IsTargetDestinaion()
|
||||
{
|
||||
ObjectParameters? objParams = GetObjectParameters;
|
||||
if (objParams == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return objParams.LeftBorder - GetStep() <= 0 ||
|
||||
objParams.RightBorder + GetStep() >= FieldWidth ||
|
||||
objParams.TopBorder - GetStep() <= 0
|
||||
|| objParams.ObjectMiddleVertical + GetStep() >= FieldHeight;
|
||||
}
|
||||
|
||||
protected override void MoveToTarget()
|
||||
{
|
||||
ObjectParameters? objParams = GetObjectParameters;
|
||||
if (objParams == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
//реализация в правый нижний угол
|
||||
int x = objParams.RightBorder;
|
||||
if (x + GetStep() < FieldWidth) MoveRight();
|
||||
int y = objParams.DowBorder;
|
||||
if (y + GetStep() < FieldHeight) MoveDown();
|
||||
}
|
||||
}
|
60
Monorail/Monorail/MovementStrategy/MoveToCenter.cs
Normal file
60
Monorail/Monorail/MovementStrategy/MoveToCenter.cs
Normal file
@ -0,0 +1,60 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Monorail.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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
71
Monorail/Monorail/MovementStrategy/MoveableLocomotive.cs
Normal file
71
Monorail/Monorail/MovementStrategy/MoveableLocomotive.cs
Normal file
@ -0,0 +1,71 @@
|
||||
using Monorail.Drawnings;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Monorail.MovementStrategy;
|
||||
|
||||
/// <summary>
|
||||
/// Класс-реализация IMoveableObject с использованием Drawning_Monorail
|
||||
/// </summary>
|
||||
public class MoveableLocomotive : IMoveableObject
|
||||
{
|
||||
/// <summary>
|
||||
/// Поле-объект класса Drawning_Monorail или его наследника
|
||||
/// </summary>
|
||||
private readonly DrawningLocomotive? Locomotive = null;
|
||||
|
||||
/// <summary>
|
||||
/// Конструктор
|
||||
/// </summary>
|
||||
/// <param name="locomotive">Объект класса DrawningCar</param>
|
||||
public MoveableLocomotive(DrawningLocomotive locomotive)
|
||||
{
|
||||
Locomotive = locomotive;
|
||||
}
|
||||
|
||||
public ObjectParameters? GetObjectPosition
|
||||
{
|
||||
get
|
||||
{
|
||||
if ((Locomotive == null || Locomotive.EntityLocomotive == null || !Locomotive.GetPosX.HasValue || !Locomotive.GetPosY.HasValue))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return new ObjectParameters(Locomotive.GetPosX.Value, Locomotive.GetPosY.Value, Locomotive.GetWidth, Locomotive.GetHeight);
|
||||
}
|
||||
}
|
||||
|
||||
public int GetStep => (int)(Locomotive?.EntityLocomotive?.Step ?? 0);
|
||||
|
||||
public bool TryMoveObject(MovementDirection direction)
|
||||
{
|
||||
if (Locomotive == null || Locomotive.EntityLocomotive == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return Locomotive.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,
|
||||
};
|
||||
}
|
||||
|
||||
}
|
28
Monorail/Monorail/MovementStrategy/MovementDirection.cs
Normal file
28
Monorail/Monorail/MovementStrategy/MovementDirection.cs
Normal file
@ -0,0 +1,28 @@
|
||||
|
||||
namespace Monorail.MovementStrategy;
|
||||
|
||||
/// <summary>
|
||||
/// Направление перемещения
|
||||
/// </summary>
|
||||
public enum MovementDirection
|
||||
{
|
||||
/// <summary>
|
||||
/// Вверх
|
||||
/// </summary>
|
||||
Up = 1,
|
||||
|
||||
/// <summary>
|
||||
/// Вниз
|
||||
/// </summary>
|
||||
Down = 2,
|
||||
|
||||
/// <summary>
|
||||
/// Влево
|
||||
/// </summary>
|
||||
Left = 3,
|
||||
|
||||
/// <summary>
|
||||
/// Вправо
|
||||
/// </summary>
|
||||
Right = 4
|
||||
}
|
75
Monorail/Monorail/MovementStrategy/ObjectParameters.cs
Normal file
75
Monorail/Monorail/MovementStrategy/ObjectParameters.cs
Normal file
@ -0,0 +1,75 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Monorail.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 DowBorder => _y + _height;
|
||||
|
||||
/// <summary>
|
||||
/// Серидина объекта
|
||||
/// </summary>
|
||||
public int ObjectMiddleHorizontal => _x + _width / 2;
|
||||
|
||||
/// <summary>
|
||||
/// Серидина объекта
|
||||
/// </summary>
|
||||
public int ObjectMiddleVertical => _y + _height / 2;
|
||||
|
||||
/// <summary>
|
||||
/// Конструктор
|
||||
/// </summary>
|
||||
/// <param name="x"></param>
|
||||
/// <param name="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;
|
||||
}
|
||||
}
|
25
Monorail/Monorail/MovementStrategy/StrategyStatus.cs
Normal file
25
Monorail/Monorail/MovementStrategy/StrategyStatus.cs
Normal file
@ -0,0 +1,25 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Monorail.MovementStrategy;
|
||||
|
||||
public enum StrategyStatus
|
||||
{
|
||||
/// <summary>
|
||||
/// Все готово к началу
|
||||
/// </summary>
|
||||
NotInit,
|
||||
|
||||
/// <summary>
|
||||
/// Выполняется
|
||||
/// </summary>
|
||||
InProgress,
|
||||
|
||||
/// <summary>
|
||||
/// Завершено
|
||||
/// </summary>
|
||||
Finish
|
||||
}
|
Loading…
Reference in New Issue
Block a user