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

142 lines
5.0 KiB
C#

using System.Windows.Forms;
namespace AirBomber
{
public partial class FormAirBomber : Form
{
private DrawningAirPlane? _drawningAirPlane;
/// <summary>
/// Стратегия перемещения
/// </summary>
private AbstractStrategy? _strategy;
/// <summary>
/// Выбранный самолет
/// </summary>
public DrawningAirPlane? SelectedPlane { get; private set; }
public FormAirBomber()
{
InitializeComponent();
_strategy = null;
SelectedPlane = null;
}
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();
Color color = Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256));
ColorDialog colorDialog = new ColorDialog();
//TODO выбор основного цвета DONE
if (colorDialog.ShowDialog() == DialogResult.OK)
{
color = colorDialog.Color;
}
Color dopColor = Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256));
//TODO выбор дополнительного цвета DONE
if (colorDialog.ShowDialog() == DialogResult.OK)
{
dopColor = colorDialog.Color;
}
_drawningAirPlane = new DrawningAirBomber(random.Next(100, 300), random.Next(1000, 3000), color, dopColor,
Convert.ToBoolean(random.Next(0, 2)), 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();
Color color = Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256));
ColorDialog dialog = new();
if (dialog.ShowDialog() == DialogResult.OK)
{
color = dialog.Color;
}
_drawningAirPlane = new DrawningAirPlane(random.Next(100, 300), random.Next(1000, 3000), color, 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 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(_drawningAirPlane.GetMoveableObject,
pictureBoxAirBomber.Width, pictureBoxAirBomber.Height);
}
if (_strategy == null)
{
return;
}
comboBoxStrategy.Enabled = false;
_strategy.MakeStep();
Draw();
if (_strategy.GetStatus() == Status.Finish)
{
comboBoxStrategy.Enabled = true;
_strategy = null;
}
}
/// <summary>
/// Выбор самолета
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonSelectPlane_Click(object sender, EventArgs e)
{
SelectedPlane = _drawningAirPlane;
DialogResult = DialogResult.OK;
}
}
}