PIBD-14_Khalikova.A.R_Storm.../ProjectStormtrooper/FormStormtrooper.cs

129 lines
3.6 KiB
C#
Raw Normal View History

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