ISEbd-11_Hairullov_B.A._LabWork02_Simple #2
@ -4,12 +4,17 @@ using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace monorail;
|
||||
namespace Monorail.Drawnings;
|
||||
/// <summary>
|
||||
/// Направление перемещения
|
||||
/// </summary>
|
||||
public enum DirectionType
|
||||
{
|
||||
/// <summary>
|
||||
/// Неизвестное направление
|
||||
/// </summary>
|
||||
Unknow = -1,
|
||||
|
||||
/// <summary>
|
||||
/// Вверх
|
||||
/// </summary>
|
@ -1,15 +1,21 @@
|
||||
|
||||
using Monorail.Entities;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Monorail.Drawnings;
|
||||
|
||||
namespace monorail;
|
||||
/// <summary>
|
||||
/// Класс отвечающий за прорисовку и перемещение объекта-сущности
|
||||
/// Класс, отвечающий за прорисовку и перемещение базового объекта-сущности
|
||||
/// </summary>
|
||||
public class DrawningMonorail
|
||||
public class DrawningLocomotive
|
||||
{
|
||||
/// <summary>
|
||||
/// Класс-сущность (объект)
|
||||
/// </summary>
|
||||
public EntityMonorail? EntityMonorail { get; private set; }
|
||||
public EntityLocomotive? EntityLocomotive { get; protected set; }
|
||||
|
||||
/// <summary>
|
||||
/// Ширина окна
|
||||
@ -24,42 +30,84 @@ public class DrawningMonorail
|
||||
/// <summary>
|
||||
/// Левая координата прорисовки монорельса
|
||||
/// </summary>
|
||||
private int? _startPosX;
|
||||
protected int? _startPosX;
|
||||
|
||||
/// <summary>
|
||||
/// Верхняя координата прорисовки монорельса
|
||||
/// </summary>
|
||||
private int? _startPosY;
|
||||
protected int? _startPosY;
|
||||
/// <summary>
|
||||
/// Ширина прорисовки монорельса (размер объекта)
|
||||
/// </summary>
|
||||
private readonly int _drawningMonorailWidth = 95;
|
||||
private readonly int _drawningMonorailWidth = 90;
|
||||
/// <summary>
|
||||
/// Высота прорисовки монорельса (размер объекта)
|
||||
/// </summary>
|
||||
private readonly int _drawingMonorailHeight = 40;
|
||||
|
||||
/// <summary>
|
||||
/// Инициализация свойства
|
||||
/// Координата X объекта
|
||||
/// </summary>
|
||||
/// <param name="speed">Скорость</param>
|
||||
/// <param name="weight">Вес</param>
|
||||
/// <param name="bodyColor">Основной цвет</param>
|
||||
/// <param name="additionalColor">Дополнительный цвет</param>
|
||||
/// <param name="magneticRail">магнитный рельс</param>
|
||||
/// <param name="secondCabin">Вторая кабинка в задней части</param>
|
||||
public int? GetPosX => _startPosX;
|
||||
|
||||
public void Init(int speed, double weight, Color bodyColor, Color additionalColor, bool magneticRail, bool secondCabin)
|
||||
/// <summary>
|
||||
/// Координата Y объекта
|
||||
/// </summary>
|
||||
public int? GetPosY => _startPosY;
|
||||
|
||||
/// <summary>
|
||||
/// Ширина объекта
|
||||
/// </summary>
|
||||
public int GetWidth => _drawningMonorailWidth;
|
||||
|
||||
/// <summary>
|
||||
/// Высота объекта
|
||||
/// </summary>
|
||||
public int GetHeight => _drawingMonorailHeight;
|
||||
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Пустой конструктор
|
||||
/// </summary>
|
||||
private DrawningLocomotive()
|
||||
{
|
||||
EntityMonorail = new EntityMonorail();
|
||||
EntityMonorail.Init(speed, weight, bodyColor, additionalColor, magneticRail, secondCabin);
|
||||
_pictureWidth = null;
|
||||
_pictureHeight = null;
|
||||
_startPosX = null;
|
||||
_startPosY = null;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Конструтор
|
||||
/// </summary>
|
||||
/// <param name="speed">Скорость</param>
|
||||
/// <param name="weight">Вес</param>
|
||||
/// <param name="bodyColor">Основной цвет</param>
|
||||
|
||||
|
||||
public DrawningLocomotive(int speed, double weight, Color bodyColor) : this()
|
||||
{
|
||||
EntityLocomotive = new EntityLocomotive(speed, weight, bodyColor);
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Конструтор для наследников
|
||||
/// </summary>
|
||||
/// <param name="drawningMonorailWidth">Ширина прорисовки монорельса (размер объекта)</param>
|
||||
/// <param name="drawingMonorailHeight">Высота прорисовки монорельса (размер объекта)</param>
|
||||
|
||||
|
||||
protected DrawningLocomotive(int drawningMonorailWidth, int drawingMonorailHeight) : this()
|
||||
{
|
||||
_drawningMonorailWidth = drawningMonorailWidth;
|
||||
_drawingMonorailHeight = drawingMonorailHeight;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Установка границ поля
|
||||
/// </summary>
|
||||
@ -141,7 +189,7 @@ public class DrawningMonorail
|
||||
/// <returns>true - перемещение выполнено, false - перемещение невозможно</returns>
|
||||
public bool MoveTransport(DirectionType direction)
|
||||
{
|
||||
if (EntityMonorail == null || !_startPosX.HasValue || !_startPosY.HasValue)
|
||||
if (EntityLocomotive == null || !_startPosX.HasValue || !_startPosY.HasValue)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
@ -149,33 +197,33 @@ public class DrawningMonorail
|
||||
{
|
||||
//влево
|
||||
case DirectionType.Left:
|
||||
if (_startPosX.Value - EntityMonorail.Step > 0)
|
||||
if (_startPosX.Value - EntityLocomotive.Step > 0)
|
||||
{
|
||||
_startPosX -= (int)EntityMonorail.Step;
|
||||
_startPosX -= (int)EntityLocomotive.Step;
|
||||
}
|
||||
return true;
|
||||
//вверх
|
||||
case DirectionType.Up:
|
||||
if (_startPosY.Value - EntityMonorail.Step > 0)
|
||||
if (_startPosY.Value - EntityLocomotive.Step > 0)
|
||||
{
|
||||
_startPosY -= (int)EntityMonorail.Step;
|
||||
_startPosY -= (int)EntityLocomotive.Step;
|
||||
}
|
||||
return true;
|
||||
// вправо
|
||||
case DirectionType.Right:
|
||||
// TODO прописать логику сдвига в право
|
||||
if (_startPosX.Value + EntityMonorail.Step + _drawningMonorailWidth < _pictureWidth)
|
||||
if (_startPosX.Value + EntityLocomotive.Step + _drawningMonorailWidth < _pictureWidth)
|
||||
{
|
||||
_startPosX += (int)EntityMonorail.Step;
|
||||
_startPosX += (int)EntityLocomotive.Step;
|
||||
}
|
||||
|
||||
return true;
|
||||
//вниз
|
||||
case DirectionType.Down:
|
||||
//TODO прописать логику сдвига в вниз
|
||||
if (_startPosY.Value + EntityMonorail.Step + _drawingMonorailHeight < _pictureHeight)
|
||||
if (_startPosY.Value + EntityLocomotive.Step + _drawingMonorailHeight < _pictureHeight)
|
||||
{
|
||||
_startPosY += (int)EntityMonorail.Step;
|
||||
_startPosY += (int)EntityLocomotive.Step;
|
||||
}
|
||||
return true;
|
||||
default:
|
||||
@ -189,43 +237,20 @@ public class DrawningMonorail
|
||||
/// </summary>
|
||||
/// <param name="g"></param>
|
||||
///
|
||||
public void DrawTransport(Graphics g)
|
||||
public virtual void DrawTransport(Graphics g)
|
||||
{
|
||||
if (EntityMonorail == null || !_startPosX.HasValue || !_startPosY.HasValue)
|
||||
if (EntityLocomotive == null || !_startPosX.HasValue || !_startPosY.HasValue)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Pen pen = new(Color.Black);
|
||||
Brush additionalBrush = new SolidBrush(EntityMonorail.AdditionalColor);
|
||||
|
||||
//магнитная рельса
|
||||
if (EntityMonorail.MagneticRail)
|
||||
{
|
||||
g.FillRectangle(additionalBrush, _startPosX.Value, _startPosY.Value + 40, 90, 3);
|
||||
}
|
||||
|
||||
//вторая часть монорельса
|
||||
|
||||
if (EntityMonorail.SecondCabin)
|
||||
{
|
||||
Point[] points_second_cabin = {
|
||||
new Point(_startPosX.Value + 85, _startPosY.Value),
|
||||
new Point(_startPosX.Value + 95, _startPosY.Value + 15),
|
||||
new Point(_startPosX.Value + 95, _startPosY.Value + 30),
|
||||
new Point(_startPosX.Value + 85, _startPosY.Value + 30),
|
||||
new Point(_startPosX.Value + 85, _startPosY.Value + 15),
|
||||
new Point(_startPosX.Value + 95, _startPosY.Value + 15)
|
||||
|
||||
};
|
||||
g.DrawPolygon(pen, points_second_cabin);
|
||||
g.FillPolygon(additionalBrush, points_second_cabin);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
//границы Монорельса
|
||||
Brush br = new SolidBrush(EntityMonorail.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),
|
||||
@ -244,7 +269,7 @@ public class DrawningMonorail
|
||||
new Point(_startPosX.Value + 84, _startPosY.Value + 1),
|
||||
new Point(_startPosX.Value + 84, _startPosY.Value + 14),
|
||||
new Point(_startPosX.Value + 6, _startPosY.Value + 14)
|
||||
};
|
||||
};
|
||||
|
||||
g.FillPolygon(br, points2);
|
||||
|
||||
@ -266,7 +291,7 @@ public class DrawningMonorail
|
||||
//1-ый держатель
|
||||
|
||||
Point[] points_cart1 = {
|
||||
new Point(_startPosX.Value, _startPosY.Value + 35),
|
||||
new Point(_startPosX.Value + 5, _startPosY.Value + 30),
|
||||
new Point(_startPosX.Value + 15, _startPosY.Value + 35),
|
||||
new Point(_startPosX.Value + 15, _startPosY.Value + 30),
|
||||
new Point(_startPosX.Value + 5, _startPosY.Value + 30)
|
||||
@ -330,4 +355,4 @@ public class DrawningMonorail
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}
|
62
Monorail/Monorail/Drawnings/DrawningMonorail.cs
Normal file
62
Monorail/Monorail/Drawnings/DrawningMonorail.cs
Normal file
@ -0,0 +1,62 @@
|
||||
using Monorail.Entities;
|
||||
|
||||
namespace Monorail.Drawnings;
|
||||
/// <summary>
|
||||
/// Класс отвечающий за прорисовку и перемещение объекта-сущности
|
||||
/// </summary>
|
||||
public class DrawningMonorail : DrawningLocomotive
|
||||
{
|
||||
/// <summary>
|
||||
/// Конструктор
|
||||
/// </summary>
|
||||
/// <param name="speed">Скорость</param>
|
||||
/// <param name="weight">Вес</param>
|
||||
/// <param name="bodyColor">Основной цвет</param>
|
||||
/// <param name="additionalColor">Дополнительный цвет</param>
|
||||
/// <param name="magneticRail">магнитный рельс</param>
|
||||
/// <param name="secondCabin">Вторая кабинка в задней части</param>
|
||||
|
||||
public DrawningMonorail(int speed, double weight, Color bodyColor, Color additionalColor, bool magneticRail, bool secondCabin) : base(90, 40)
|
||||
{
|
||||
EntityLocomotive = new EntityMonorail(speed, weight, bodyColor, additionalColor, magneticRail, secondCabin);
|
||||
}
|
||||
|
||||
public override void DrawTransport(Graphics g)
|
||||
{
|
||||
if (EntityLocomotive == null || EntityLocomotive is not EntityMonorail monorail || !_startPosX.HasValue || !_startPosY.HasValue)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
Pen pen = new(Color.Black);
|
||||
Brush additionalBrush = new SolidBrush(monorail.AdditionalColor);
|
||||
|
||||
//магнитная рельса
|
||||
if (monorail.MagneticRail)
|
||||
{
|
||||
g.FillRectangle(additionalBrush, _startPosX.Value, _startPosY.Value + 40, 90, 3);
|
||||
}
|
||||
|
||||
//вторая часть монорельса
|
||||
|
||||
if (monorail.SecondCabin)
|
||||
{
|
||||
Point[] points_second_cabin = {
|
||||
new Point(_startPosX.Value + 85, _startPosY.Value),
|
||||
new Point(_startPosX.Value + 95, _startPosY.Value + 15),
|
||||
new Point(_startPosX.Value + 95, _startPosY.Value + 30),
|
||||
new Point(_startPosX.Value + 85, _startPosY.Value + 30),
|
||||
new Point(_startPosX.Value + 85, _startPosY.Value + 15),
|
||||
new Point(_startPosX.Value + 95, _startPosY.Value + 15)
|
||||
|
||||
};
|
||||
g.DrawPolygon(pen, points_second_cabin);
|
||||
g.FillPolygon(additionalBrush, points_second_cabin);
|
||||
|
||||
}
|
||||
|
||||
base.DrawTransport(g);
|
||||
|
||||
}
|
||||
}
|
48
Monorail/Monorail/Entities/EntityLocomotive.cs
Normal file
48
Monorail/Monorail/Entities/EntityLocomotive.cs
Normal file
@ -0,0 +1,48 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Monorail.Entities;
|
||||
|
||||
/// <summary>
|
||||
/// Класс-сущносить "Локомотив"
|
||||
/// </summary>
|
||||
public class EntityLocomotive
|
||||
{
|
||||
/// <summary>
|
||||
/// скорость
|
||||
/// </summary>
|
||||
public int Speed { get; set; }
|
||||
/// <summary>
|
||||
/// Вес
|
||||
/// </summary>
|
||||
public double Weight { get; set; }
|
||||
/// <summary>
|
||||
/// основной цвет
|
||||
/// </summary>
|
||||
public Color BodyColor { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// шаг
|
||||
/// </summary>
|
||||
public double Step => Speed * 100 / Weight;
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Конструктор сущности
|
||||
/// </summary>
|
||||
/// <param name="speed">Скорость</param>
|
||||
/// <param name="weight">Вес</param>
|
||||
/// <param name="bodyColor">Основной цвет</param>
|
||||
|
||||
public EntityLocomotive(int speed, double weight, Color bodyColor)
|
||||
{
|
||||
Speed = speed;
|
||||
Weight = weight;
|
||||
BodyColor = bodyColor;
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -1,21 +1,9 @@
|
||||
namespace monorail;
|
||||
namespace Monorail.Entities;
|
||||
/// <summary>
|
||||
/// Класс-сущность "Монорельс"
|
||||
/// </summary>
|
||||
public class EntityMonorail
|
||||
public class EntityMonorail : EntityLocomotive
|
||||
{
|
||||
/// <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>
|
||||
@ -30,11 +18,7 @@ public class EntityMonorail
|
||||
public bool SecondCabin { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// шаг
|
||||
/// </summary>
|
||||
public double Step => Speed * 100 / Weight;
|
||||
/// <summary>
|
||||
/// Инициализация полей объекта-класса монорельса
|
||||
/// Конструктор
|
||||
/// </summary>
|
||||
/// <param name="speed">Скорость</param>
|
||||
/// <param name="weight">Вес</param>
|
||||
@ -42,7 +26,8 @@ public class EntityMonorail
|
||||
/// <param name="additionalColor">Дополнительный цвет</param>
|
||||
/// <param name="magneticRail">признак наличие рельса</param>
|
||||
/// <param name="secondCabin">признак вторая кабинка</param>
|
||||
public void Init(int speed, double weight, Color bodyColor, Color additionalColor, bool magneticRail, bool secondCabin)
|
||||
|
||||
public EntityMonorail(int speed, double weight, Color bodyColor, Color additionalColor, bool magneticRail, bool secondCabin) : base(speed, weight, bodyColor)
|
||||
{
|
||||
Speed = speed;
|
||||
Weight = weight;
|
||||
@ -50,5 +35,6 @@ public class EntityMonorail
|
||||
AdditionalColor = additionalColor;
|
||||
MagneticRail = magneticRail;
|
||||
SecondCabin = secondCabin;
|
||||
|
||||
}
|
||||
}
|
44
Monorail/Monorail/FormMonorail.Designer.cs
generated
44
Monorail/Monorail/FormMonorail.Designer.cs
generated
@ -34,6 +34,9 @@
|
||||
buttonDown = new Button();
|
||||
buttonRight = new Button();
|
||||
buttonUp = new Button();
|
||||
buttonCreateLocomotive = new Button();
|
||||
comboBoxStrategy = new ComboBox();
|
||||
buttonStrategyStep = new Button();
|
||||
((System.ComponentModel.ISupportInitialize)pictureBox1Monorail).BeginInit();
|
||||
SuspendLayout();
|
||||
//
|
||||
@ -51,9 +54,9 @@
|
||||
buttonCreate.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
|
||||
buttonCreate.Location = new Point(10, 358);
|
||||
buttonCreate.Name = "buttonCreate";
|
||||
buttonCreate.Size = new Size(75, 23);
|
||||
buttonCreate.Size = new Size(221, 23);
|
||||
buttonCreate.TabIndex = 1;
|
||||
buttonCreate.Text = "создать";
|
||||
buttonCreate.Text = "создать Monorail";
|
||||
buttonCreate.UseVisualStyleBackColor = true;
|
||||
buttonCreate.Click += buttonCreate_Click;
|
||||
//
|
||||
@ -105,11 +108,45 @@
|
||||
buttonUp.UseVisualStyleBackColor = true;
|
||||
buttonUp.Click += buttonMove_Click;
|
||||
//
|
||||
// buttonCreateLocomotive
|
||||
//
|
||||
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(buttonStrategyStep);
|
||||
Controls.Add(comboBoxStrategy);
|
||||
Controls.Add(buttonCreateLocomotive);
|
||||
Controls.Add(buttonUp);
|
||||
Controls.Add(buttonRight);
|
||||
Controls.Add(buttonDown);
|
||||
@ -130,5 +167,8 @@
|
||||
private Button buttonDown;
|
||||
private Button buttonRight;
|
||||
private Button buttonUp;
|
||||
private Button buttonCreateLocomotive;
|
||||
private ComboBox comboBoxStrategy;
|
||||
private Button buttonStrategyStep;
|
||||
}
|
||||
}
|
@ -1,4 +1,6 @@
|
||||
using monorail;
|
||||
using Monorail.Drawnings;
|
||||
using Monorail.MovementStrategy;
|
||||
|
||||
namespace Monorail;
|
||||
/// <summary>
|
||||
/// Форма работы с объектом "Монорельс"
|
||||
@ -8,59 +10,93 @@ public partial class FormMonorail : Form
|
||||
/// <summary>
|
||||
/// Поле-объект для прорисовки объекта
|
||||
/// </summary>
|
||||
private DrawningMonorail? _drawningMonorail;
|
||||
private DrawningLocomotive? _drawningLocomotive;
|
||||
|
||||
/// <summary>
|
||||
/// Стратегия перемещения
|
||||
/// </summary>
|
||||
private AbstractStrategy? _strategy;
|
||||
|
||||
/// <summary>
|
||||
/// конструктор формы
|
||||
/// </summary>
|
||||
public FormMonorail()
|
||||
{
|
||||
InitializeComponent();
|
||||
_strategy = null;
|
||||
}
|
||||
/// <summary>
|
||||
/// Метод прорисовки машины
|
||||
/// </summary>
|
||||
private void Draw()
|
||||
{
|
||||
if (_drawningMonorail == null)
|
||||
if (_drawningLocomotive == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Bitmap bmp = new(pictureBox1Monorail.Width, pictureBox1Monorail.Height);
|
||||
Graphics gr = Graphics.FromImage(bmp);
|
||||
_drawningMonorail.DrawTransport(gr);
|
||||
_drawningLocomotive.DrawTransport(gr);
|
||||
pictureBox1Monorail.Image = bmp;
|
||||
}
|
||||
|
||||
|
||||
|
||||
private void FormMonorail_Load(object sender, EventArgs e)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Обработка нажатия кнопки "Создать"
|
||||
/// Создание объекта класса-перемещения
|
||||
/// </summary>
|
||||
/// <param name="type">Тип создаваемого объекта</param>
|
||||
private void CreateObject(string type)
|
||||
{
|
||||
Random random = new();
|
||||
switch (type)
|
||||
{
|
||||
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):
|
||||
_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)));
|
||||
break;
|
||||
default:
|
||||
return;
|
||||
}
|
||||
|
||||
_drawningLocomotive.SetPictureSize(pictureBox1Monorail.Width, pictureBox1Monorail.Height);
|
||||
_drawningLocomotive.SetPosition(random.Next(10, 100), random.Next(10, 100));
|
||||
_strategy = null;
|
||||
comboBoxStrategy.Enabled = true;
|
||||
Draw();
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Обработка нажатия кнопки "Создать Монорельс"
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="e"></param>
|
||||
|
||||
private void buttonCreate_Click(object sender, EventArgs e)
|
||||
{
|
||||
Random random = new();
|
||||
_drawningMonorail = new DrawningMonorail();
|
||||
_drawningMonorail.Init(random.Next(100, 300), random.Next(1000, 3000),
|
||||
Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)),
|
||||
Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)),
|
||||
Convert.ToBoolean(random.Next(0, 2)), Convert.ToBoolean(random.Next(0, 2)));
|
||||
_drawningMonorail.SetPictureSize(pictureBox1Monorail.Width, pictureBox1Monorail.Height);
|
||||
_drawningMonorail.SetPosition(random.Next(10, 100), random.Next(10, 100));
|
||||
private void buttonCreate_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningMonorail));
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Обработка нажатия кнопки "Создать _Монорельс"
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="e"></param>
|
||||
private void ButtonCreateLocomotive_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningLocomotive));
|
||||
|
||||
Bitmap bmp = new(pictureBox1Monorail.Width, pictureBox1Monorail.Height);
|
||||
Graphics gr = Graphics.FromImage(bmp);
|
||||
_drawningMonorail.DrawTransport(gr);
|
||||
pictureBox1Monorail.Image = bmp;
|
||||
Draw();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Перемещение объекта по форме (нажатие кнопок навигации)
|
||||
@ -69,7 +105,7 @@ public partial class FormMonorail : Form
|
||||
/// <param name="e"></param>
|
||||
private void buttonMove_Click(object sender, EventArgs e)
|
||||
{
|
||||
if (_drawningMonorail == null)
|
||||
if (_drawningLocomotive == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
@ -79,16 +115,16 @@ public partial class FormMonorail : Form
|
||||
switch (name)
|
||||
{
|
||||
case "buttonUp":
|
||||
result = _drawningMonorail.MoveTransport(DirectionType.Up);
|
||||
result = _drawningLocomotive.MoveTransport(DirectionType.Up);
|
||||
break;
|
||||
case "buttonDown":
|
||||
result = _drawningMonorail.MoveTransport(DirectionType.Down);
|
||||
result = _drawningLocomotive.MoveTransport(DirectionType.Down);
|
||||
break;
|
||||
case "buttonLeft":
|
||||
result = _drawningMonorail.MoveTransport(DirectionType.Left);
|
||||
result = _drawningLocomotive.MoveTransport(DirectionType.Left);
|
||||
break;
|
||||
case "buttonRight":
|
||||
result = _drawningMonorail.MoveTransport(DirectionType.Right);
|
||||
result = _drawningLocomotive.MoveTransport(DirectionType.Right);
|
||||
break;
|
||||
}
|
||||
if (result)
|
||||
@ -97,4 +133,47 @@ 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