141 lines
3.7 KiB
C#
141 lines
3.7 KiB
C#
using ProjectStormtrooper.Drawnings;
|
||
using ProjectStormtrooper.MovementStrategy;
|
||
|
||
namespace ProjectStormtrooper;
|
||
|
||
/// <summary>
|
||
/// Форма работы с объектом "Штурмовик"
|
||
/// </summary>
|
||
public partial class FormStormtrooper : Form
|
||
{
|
||
/// <summary>
|
||
/// Поле-объект для прорисовки объекта
|
||
/// </summary>
|
||
private DrawningAirplane? _drawningAirplane;
|
||
|
||
/// <summary>
|
||
/// Стратегия перемещения
|
||
/// </summary>
|
||
private AbstractStrategy? _strategy;
|
||
|
||
/// <summary>
|
||
/// Получение объекта
|
||
/// </summary>
|
||
public DrawningAirplane SetAirplane
|
||
{
|
||
set
|
||
{
|
||
_drawningAirplane = value;
|
||
_drawningAirplane.SetPictureSize(pictureBoxStormtrooper.Width, pictureBoxStormtrooper.Height);
|
||
comboBoxStrategy.Enabled = true;
|
||
Draw();
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// Конструктор формы
|
||
/// </summary>
|
||
public FormStormtrooper()
|
||
{
|
||
InitializeComponent();
|
||
_strategy = null;
|
||
}
|
||
|
||
/// <summary>
|
||
/// Метод прорисовки машины
|
||
/// </summary>
|
||
private void Draw()
|
||
{
|
||
if (_drawningAirplane == null)
|
||
{
|
||
return;
|
||
}
|
||
Bitmap bmp = new(pictureBoxStormtrooper.Width, pictureBoxStormtrooper.Height);
|
||
Graphics gr = Graphics.FromImage(bmp);
|
||
_drawningAirplane.DrawTransport(gr);
|
||
pictureBoxStormtrooper.Image = bmp;
|
||
}
|
||
|
||
/// <summary>
|
||
/// Перемещение объекта по форме (нажатие кнопок навигации)
|
||
/// </summary>
|
||
/// <param name="sender"></param>
|
||
/// <param name="e"></param>
|
||
private void ButtonMove_Click(object sender, EventArgs e)
|
||
{
|
||
if (_drawningAirplane == null)
|
||
{
|
||
return;
|
||
}
|
||
string name = ((Button)sender)?.Name ?? string.Empty;
|
||
bool result = false;
|
||
switch (name)
|
||
{
|
||
case "buttonUp":
|
||
result =
|
||
_drawningAirplane.MoveTransport(DirectionType.Up);
|
||
break;
|
||
case "buttonDown":
|
||
result =
|
||
_drawningAirplane.MoveTransport(DirectionType.Down);
|
||
break;
|
||
case "buttonLeft":
|
||
result =
|
||
_drawningAirplane.MoveTransport(DirectionType.Left);
|
||
break;
|
||
case "buttonRight":
|
||
result =
|
||
_drawningAirplane.MoveTransport(DirectionType.Right);
|
||
break;
|
||
}
|
||
if (result)
|
||
{
|
||
Draw();
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// Обработка нажатия кнопки "Шаг"
|
||
/// </summary>
|
||
/// <param name="sender"></param>
|
||
/// <param name="e"></param>
|
||
///
|
||
|
||
private void ButtonStrategyStep_Click(object sender, EventArgs e)
|
||
{
|
||
if (_drawningAirplane == null)
|
||
{
|
||
return;
|
||
}
|
||
|
||
if (comboBoxStrategy.Enabled)
|
||
{
|
||
_strategy = comboBoxStrategy.SelectedIndex switch
|
||
{
|
||
0 => new MoveToCenter(),
|
||
1 => new MoveToBorder(),
|
||
_ => null,
|
||
};
|
||
if (_strategy == null)
|
||
{
|
||
return;
|
||
}
|
||
_strategy.SetData(new MoveableAirplane(_drawningAirplane), pictureBoxStormtrooper.Width, pictureBoxStormtrooper.Height);
|
||
}
|
||
|
||
if (_strategy == null)
|
||
{
|
||
return;
|
||
}
|
||
|
||
comboBoxStrategy.Enabled = false;
|
||
_strategy.MakeStep();
|
||
Draw();
|
||
if (_strategy.GetStatus == StrategyStatus.Finish)
|
||
{
|
||
comboBoxStrategy.Enabled = true;
|
||
_strategy = null;
|
||
}
|
||
}
|
||
}
|