ISEbd-22_Alimova_M.S._AirBo.../AirBomber/AirBomber/FormAirBomber.cs

110 lines
3.9 KiB
C#

namespace AirBomber
{
public partial class FormAirBomber : Form
{
private DrawningAirPlane? _drawningAirPlane;
/// <summary>
/// Стратегия перемещения
/// </summary>
private AbstractStrategy? _abstractStrategy;
public FormAirBomber()
{
InitializeComponent();
}
private void Draw()
{
if (_drawningAirPlane == null)
{
return;
}
Bitmap bmp = new(pictureBoxAirBomber.Width, pictureBoxAirBomber.Height);
Graphics gr = Graphics.FromImage(bmp);
_drawningAirPlane.DrawPlane(gr);
pictureBoxAirBomber.Image = bmp;
}
private void buttonCreateAirBomber_Click(object sender, EventArgs e)
{
Random random = new();
_drawningAirPlane = new DrawningAirBomber(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)), Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)),
Convert.ToBoolean(random.Next(0, 2)),
pictureBoxAirBomber.Width, pictureBoxAirBomber.Height);
_drawningAirPlane.SetPosition(random.Next(10, 100), random.Next(10, 100));
Draw();
}
private void buttonCreateAirPlane_Click(object sender, EventArgs e)
{
Random random = new();
_drawningAirPlane = new DrawningAirPlane(random.Next(100, 300), random.Next(1000, 3000),
Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)),
pictureBoxAirBomber.Width, pictureBoxAirBomber.Height);
_drawningAirPlane.SetPosition(random.Next(10, 100), random.Next(10, 100));
Draw();
}
private void buttonMove_Click(object sender, EventArgs e)
{
if (_drawningAirPlane == null)
{
return;
}
string name = ((Button)sender)?.Name ?? string.Empty;
switch (name)
{
case "buttonUp":
_drawningAirPlane.MovePlane(DirectionType.Up);
break;
case "buttonDown":
_drawningAirPlane.MovePlane(DirectionType.Down);
break;
case "buttonLeft":
_drawningAirPlane.MovePlane(DirectionType.Left);
break;
case "buttonRight":
_drawningAirPlane.MovePlane(DirectionType.Right);
break;
}
Draw();
}
private void buttonStep_Click(object sender, EventArgs e)
{
if (_drawningAirPlane == null)
{
return;
}
if (comboBoxStrategy.Enabled)
{
_abstractStrategy = comboBoxStrategy.SelectedIndex
switch
{
0 => new MoveToCenter(),
1 => new MoveToBorder(),
_ => null,
};
if (_abstractStrategy == null)
{
return;
}
_abstractStrategy.SetData(new
DrawningObjectAirPlane(_drawningAirPlane), pictureBoxAirBomber.Width,
pictureBoxAirBomber.Height);
comboBoxStrategy.Enabled = false;
}
if (_abstractStrategy == null)
{
return;
}
_abstractStrategy.MakeStep();
Draw();
if (_abstractStrategy.GetStatus() == Status.Finish)
{
comboBoxStrategy.Enabled = true;
_abstractStrategy = null;
}
}
}
}