PIBD-13_Fomichev_V.S._Simple_/AntiAircraftGun/FormAntiAircraftGun.cs

155 lines
5.1 KiB
C#
Raw Normal View History

using AntiAircraftGun.Drawnings;
2024-03-04 16:16:41 +04:00
using AntiAircraftGun.MovementStrategy;
namespace AntiAircraftGun;
public partial class FormAntiAircraftGun : Form
2024-02-21 19:44:26 +04:00
{
/// <summary>
/// Поле объект для прорисовки объекта
/// </summary>
private DrawningAircraftGun? _drawningAircraftGun;
/// <summary>
/// Стратегия перемещения
/// </summary>
private AbstractStrategy? _strategy;
/// <summary>
/// конструктор формы
/// </summary>
public FormAntiAircraftGun()
{
InitializeComponent();
_strategy = null;
}
/// <summary>
/// Метод прорисовки транспорта
/// </summary>
private void Draw()
2024-02-21 19:44:26 +04:00
{
if (_drawningAircraftGun == null)
2024-02-21 19:44:26 +04:00
{
return;
2024-02-21 19:44:26 +04:00
}
Bitmap bmp = new(pictureBoxAntiAircraftGun.Width, pictureBoxAntiAircraftGun.Height);
Graphics gr = Graphics.FromImage(bmp);
_drawningAircraftGun.DrawTransport(gr);
pictureBoxAntiAircraftGun.Image = bmp;
}
/// <summary>
/// Метод создания объекта
/// </summary>
/// <param name="type"></param>
private void CreateObject(string type)
{
Random random = new();
switch (type)
2024-02-21 19:44:26 +04:00
{
case nameof(DrawningAircraftGun):
_drawningAircraftGun = new DrawningAircraftGun(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(DrawningAntiAircraftGun):
_drawningAircraftGun = new DrawningAntiAircraftGun(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;
default:
2024-02-21 19:44:26 +04:00
return;
}
_drawningAircraftGun.SetPictureSize(pictureBoxAntiAircraftGun.Width, pictureBoxAntiAircraftGun.Height);
_drawningAircraftGun.SetPosition(random.Next(10, 100), random.Next(10, 100));
_strategy = null;
comboBoxStrategy.Enabled = true;
Draw();
}
/// <summary>
/// Обработка кнопик Создать Зенитную установку
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonCreate_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningAntiAircraftGun));
/// <summary>
/// Обработка кнопик Создать установку
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonCreatAircraftGun_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningAircraftGun));
2024-03-04 16:16:41 +04:00
/// <summary>
/// Перемещение объекта по форме
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonMove_Click(object sender, EventArgs e)
{
if (_drawningAircraftGun == null)
2024-02-21 19:44:26 +04:00
{
return;
2024-02-21 19:44:26 +04:00
}
string name = ((Button)sender)?.Name ?? string.Empty;
bool result = false;
switch (name)
2024-03-04 16:16:41 +04:00
{
case "buttonUp":
result = _drawningAircraftGun.MoveTransport(DirectionType.Up);
break;
case "buttonDown":
result = _drawningAircraftGun.MoveTransport(DirectionType.Down);
break;
case "buttonRight":
result = _drawningAircraftGun.MoveTransport(DirectionType.Right);
break;
case "buttonLeft":
result = _drawningAircraftGun.MoveTransport(DirectionType.Left);
break;
}
if (result)
{
Draw();
}
}
/// <summary>
/// Метод выбора стратегии
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonStrategyStep_Click(object sender, EventArgs e)
{
if (_drawningAircraftGun == null)
{
return;
}
2024-03-04 16:16:41 +04:00
if (comboBoxStrategy.Enabled)
{
_strategy = comboBoxStrategy.SelectedIndex switch
2024-03-04 16:16:41 +04:00
{
0 => new MoveToCenter(),
1 => new MoveToBorder(),
_ => null,
};
2024-03-04 16:16:41 +04:00
if (_strategy == null)
{
return;
}
_strategy.SetData(new MoveableAircraftGun(_drawningAircraftGun), pictureBoxAntiAircraftGun.Width, pictureBoxAntiAircraftGun.Height);
}
2024-03-04 16:16:41 +04:00
if (_strategy == null)
{
return;
}
2024-03-04 16:16:41 +04:00
comboBoxStrategy.Enabled = false;
_strategy.MakeStep();
Draw();
if (_strategy.GetStatus() == StrategyStatus.Finish)
{
comboBoxStrategy.Enabled = true;
_strategy = null;
2024-03-04 16:16:41 +04:00
}
2024-02-21 19:44:26 +04:00
}
}