164 lines
5.6 KiB
C#
164 lines
5.6 KiB
C#
using ProjectMonorail.Scripts.Monorail.Drawnings;
|
||
using ProjectMonorail.Scripts.Monorail.MovementStrategy;
|
||
|
||
namespace ProjectMonorail
|
||
{
|
||
/// <summary>
|
||
/// Форма работы с объектом "Монорельс"
|
||
/// </summary>
|
||
public partial class FormMonorail : Form
|
||
{
|
||
/// <summary>
|
||
/// Поле-объект для прорисовки объекта
|
||
/// </summary>
|
||
private DrawingMonorail? _drawningMonorail;
|
||
|
||
/// <summary>
|
||
/// Стратегия перемещения
|
||
/// </summary>
|
||
private AbstractStrategy? _strategy;
|
||
|
||
/// <summary>
|
||
/// Конструктор формы
|
||
/// </summary>
|
||
public FormMonorail()
|
||
{
|
||
InitializeComponent();
|
||
_strategy = null;
|
||
}
|
||
|
||
/// <summary>
|
||
/// Метод для прорисовки автомобиля
|
||
/// </summary>
|
||
private void Draw()
|
||
{
|
||
if (_drawningMonorail == null) return;
|
||
|
||
Bitmap bmp = new(pictureBoxMonorail.Width, pictureBoxMonorail.Height);
|
||
Graphics gr = Graphics.FromImage(bmp);
|
||
_drawningMonorail.DrawTransport(gr);
|
||
pictureBoxMonorail.Image = bmp;
|
||
}
|
||
|
||
/// <summary>
|
||
/// Обработка кнопок перемешения
|
||
/// </summary>
|
||
private void ButtonMove_Click(object sender, EventArgs e)
|
||
{
|
||
if (_drawningMonorail == null) return;
|
||
|
||
string name = ((Button)sender).Name ?? string.Empty;
|
||
bool result = false;
|
||
switch (name)
|
||
{
|
||
case "buttonMove_Up":
|
||
result = _drawningMonorail.MoveTransport(DirectionType.Up);
|
||
break;
|
||
case "buttonMove_Down":
|
||
result = _drawningMonorail.MoveTransport(DirectionType.Down);
|
||
break;
|
||
case "buttonMove_Right":
|
||
result = _drawningMonorail.MoveTransport(DirectionType.Right);
|
||
break;
|
||
case "buttonMove_Left":
|
||
result = _drawningMonorail.MoveTransport(DirectionType.Left);
|
||
break;
|
||
}
|
||
|
||
if (result) Draw();
|
||
}
|
||
|
||
/// <summary>
|
||
/// Создание объекта класса-перемещения
|
||
/// </summary>
|
||
/// <param name="type">Тип создоваемого объекта</param>
|
||
private void CreateObject(string type)
|
||
{
|
||
Random random = new();
|
||
|
||
switch (type)
|
||
{
|
||
case nameof(DrawingMonorail):
|
||
_drawningMonorail = new DrawingMonorail(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(DrawingModernMonorail):
|
||
bool randomTrack = Convert.ToBoolean(random.Next(0, 2));
|
||
_drawningMonorail = new DrawingModernMonorail(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)),
|
||
randomTrack,
|
||
(randomTrack ? Convert.ToBoolean(random.Next(0, 2)) : true));
|
||
break;
|
||
default:
|
||
return;
|
||
}
|
||
|
||
_drawningMonorail.SetPictureSize(pictureBoxMonorail.Width, pictureBoxMonorail.Height);
|
||
_drawningMonorail.SetPosition(random.Next(10, 100), random.Next(10, 100));
|
||
_strategy = null;
|
||
comboBoxStrategy.Enabled = true;
|
||
|
||
Draw();
|
||
}
|
||
|
||
/// <summary>
|
||
/// Обработка нажатия кнопки "Создать современный монорельс"
|
||
/// </summary>
|
||
private void ButtonCreateModernMonorail_Click(object sender, EventArgs e)
|
||
{
|
||
CreateObject(nameof(DrawingModernMonorail));
|
||
}
|
||
|
||
/// <summary>
|
||
/// Обработка нажатия кнопки "Создать монорельс"
|
||
/// </summary>
|
||
/// <param name="sender"></param>
|
||
/// <param name="e"></param>
|
||
private void buttonCreateMonorail_Click(object sender, EventArgs e)
|
||
{
|
||
CreateObject(nameof(DrawingMonorail));
|
||
}
|
||
|
||
/// <summary>
|
||
///
|
||
/// </summary>
|
||
/// <param name="sender"></param>
|
||
/// <param name="e"></param>
|
||
private void buttonStrategyStep_Click(object sender, EventArgs e)
|
||
{
|
||
if (_drawningMonorail == null) return;
|
||
|
||
if (comboBoxStrategy.Enabled)
|
||
{
|
||
_strategy = comboBoxStrategy.SelectedIndex switch
|
||
{
|
||
0 => new MoveToCenter(),
|
||
1 => new MoveToRightDownBorder(),
|
||
_ => null,
|
||
};
|
||
if (_strategy == null)
|
||
{
|
||
return;
|
||
}
|
||
_strategy.SetData(new MoveableMonorail(_drawningMonorail), pictureBoxMonorail.Width, pictureBoxMonorail.Height);
|
||
}
|
||
|
||
if (_strategy == null)
|
||
{
|
||
return;
|
||
}
|
||
|
||
comboBoxStrategy.Enabled = false;
|
||
_strategy.MakeStep();
|
||
Draw();
|
||
|
||
if (_strategy.GetStatus() == StrategyStatus.Finish)
|
||
{
|
||
comboBoxStrategy.Enabled = true;
|
||
_strategy = null;
|
||
}
|
||
}
|
||
}
|
||
}
|