PIbd-12_Alkin_D.V_AirBomber.../AirBomber/FormAirBomber.cs
2024-04-09 13:54:43 +04:00

122 lines
3.0 KiB
C#

using AirBomber.Drawnings;
using AirBomber.MovementStrategy;
namespace AirBomber;
/// <summary>
/// Ôîðìà äëÿ ðàáîòû ñ îáúåêòîì
/// </summary>
public partial class FormAirBomber : Form
{
/// <summary>
/// Ïîëå-îáúåêò äëÿ ïðîðèñîâêè îáúåêòà
/// </summary>
private DrawningAirPlane? _drawningAirPlane;
/// <summary>
/// Ñòðàòåãèÿ ïåðåìåùåíèÿ
/// </summary>
private AbstractStrategy? _strategy;
public DrawningAirPlane SetAirPlane
{
set
{
_drawningAirPlane = value;
_drawningAirPlane.SetPictureSize(pictureBoxAirBomber.Width, pictureBoxAirBomber.Height);
comboBoxStrategy.Enabled = true;
_strategy = null;
Draw();
}
}
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 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;
}
}
}