using ProjectStormtrooper.Drawnings;
using ProjectStormtrooper.MovementStrategy;
namespace ProjectStormtrooper;
///
/// Форма работы с объектом "Штурмовик"
///
public partial class FormStormtrooper : Form
{///
/// Поле-объект для прорисовки объекта
///
private DrawningStormtrooperBase? _drawningStormtrooperBase;
///
/// Стратегия перемещения
///
private AbstractStrategy? _strategy;
///
/// Конструктор формы
///
public FormStormtrooper()
{
InitializeComponent();
_strategy = null;
}
///
/// Метод прорисовки штурмовика
///
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;
}
///
/// Создание объекта класса-перемещения
///
///
private void CreateObject(string type)
{
Random random = new();
switch (type)
{
case nameof(DrawingStormtrooper):
_drawningStormtrooperBase = new DrawingStormtrooper(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;
case nameof(DrawningStormtrooperBase):
_drawningStormtrooperBase = new DrawningStormtrooperBase(random.Next(100, 300), random.Next(1000, 3000),
Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)));
break;
default:
return;
}
_drawningStormtrooperBase.SetPictureSize(pictureBoxStormtrooper.Width, pictureBoxStormtrooper.Height);
_drawningStormtrooperBase.SetPosition(random.Next(10, 100), random.Next(10, 100));
_strategy = null;
comboBoxStrategy.Enabled = true;
Draw();
}
///
/// Обработка нажатия кнопки "Создать штурмовик"
///
///
///
private void ButtonCreateStormtrooper_Click(object sender, EventArgs e) => CreateObject(nameof(DrawingStormtrooper));
///
/// Обработка нажатия кнопки "Создать базовый штурмовик"
///
///
///
private void ButtonCreateStormtrooperBase_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningStormtrooperBase));
///
/// Перемещение объкта по форме(нажатие кнопок навигации)
///
///
///
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;
}
}
}