PIbd-12_Alkin_D.V_AirBomber.../AirBomber/FormAirBomber.cs
2024-03-12 13:36:12 +04:00

148 lines
4.4 KiB
C#

using AirBomber.Drawnings;
using AirBomber.MovementStrategy;
namespace AirBomber;
/// <summary>
/// Ôîðìà äëÿ ðàáîòû ñ îáúåêòîì
/// </summary>
public partial class FormAirBomber : Form
{
/// <summary>
/// Ïîëå-îáúåêò äëÿ ïðîðèñîâêè îáúåêòà
/// </summary>
private DrawningAirPlane? _drawningAirPlane;
private AbstractStrategy? _strategy;
public FormAirBomber()
{
InitializeComponent();
_strategy = null;
}
private void Draw()
{
if (_drawningAirPlane == null)
{
return;
}
if (pictureBoxAirBomber.Width == 0 || pictureBoxAirBomber.Height == 0)
{
return;
}
Bitmap bpm = new(pictureBoxAirBomber.Width, pictureBoxAirBomber.Height);
Graphics gr = Graphics.FromImage(bpm);
_drawningAirPlane?.DrawTransport(gr);
pictureBoxAirBomber.Image = bpm;
}
private void CreateObject(string type)
{
Random random = new();
switch (type)
{
case nameof(DrawningAirPlane):
_drawningAirPlane = new DrawningAirPlane(random.Next(30, 100), random.Next(100, 500),
Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)));
break;
case nameof(DrawningAirBomber):
_drawningAirPlane = new DrawningAirBomber(random.Next(30, 100), random.Next(100, 500),
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:
return;
}
_drawningAirPlane.SetPictureSize(pictureBoxAirBomber.Width, pictureBoxAirBomber.Height);
_drawningAirPlane.SetPosition(random.Next(15, 100), random.Next(15, 100));
_strategy = null;
comboBoxStrategy.Enabled = true;
Draw();
}
/// <summary>
/// Îáðàáîòêà íàæàòèÿ êíîïêè "Ñîçäàòü áîìáàðäèðîâùèê"
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonCreateAirBomber_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningAirBomber));
/// <summary>
/// Îáðàáîòêà íàæàòèÿ êíîïêè "Ñîçäàòü âîåííûé ñàìîëåò"
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonCreateAirPlane_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningAirPlane));
private void ButtonMove_Click(object sender, EventArgs e)
{
if (_drawningAirPlane == null)
{
return;
}
string name = ((Button)sender)?.Name ?? string.Empty;
bool result = false;
switch (name)
{
case "ButtonUp":
result = _drawningAirPlane.MoveTransport(DirectionType.Up);
break;
case "ButtonDown":
result = _drawningAirPlane.MoveTransport(DirectionType.Down);
break;
case "ButtonLeft":
result = _drawningAirPlane.MoveTransport(DirectionType.Left);
break;
case "ButtonRight":
result = _drawningAirPlane.MoveTransport(DirectionType.Right);
break;
}
if (result)
{
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(new MoveableAirPlane(_drawningAirPlane), pictureBoxAirBomber.Width, pictureBoxAirBomber.Height);
}
if (_strategy == null)
{
return;
}
comboBoxStrategy.Enabled = false;
_strategy.MakeStep();
Draw();
if (_strategy.GetStatus() == StrategyStatus.Finish)
{
comboBoxStrategy.Enabled = true;
_strategy = null;
}
}
}