using ProjectMonorail.Scripts.Monorail.Drawnings;
using ProjectMonorail.Scripts.Monorail.MovementStrategy;
namespace ProjectMonorail
{
///
/// Форма работы с объектом "Монорельс"
///
public partial class FormMonorail : Form
{
///
/// Поле-объект для прорисовки объекта
///
private DrawingMonorail? _drawningMonorail;
///
/// Стратегия перемещения
///
private AbstractStrategy? _strategy;
public DrawingMonorail SetMonorail
{
set
{
_drawningMonorail = value;
_drawningMonorail.SetPictureSize(pictureBoxMonorail.Width, pictureBoxMonorail.Height);
Random random = new Random();
_drawningMonorail.SetPosition(random.Next(0, 100), random.Next(0, 100));
comboBoxStrategy.Enabled = true;
_strategy = null;
Draw();
}
}
///
/// Конструктор формы
///
public FormMonorail()
{
InitializeComponent();
_strategy = null;
}
///
/// Метод для прорисовки автомобиля
///
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;
}
///
/// Обработка кнопок перемешения
///
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();
}
///
///
///
///
///
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;
}
}
}
}