2023-12-13 02:50:15 +04:00

118 lines
4.0 KiB
C#

using AirFighter.DrawningObjects;
using AirFighter.Drawnings;
using AirFighter.MovementStrategy;
namespace AirFighter
{
public partial class FormAirFighter : Form
{
private DrawningAirFighter? _drawningAirFighter;
private AbstractStrategy? _abstractStrategy;
public FormAirFighter()
{
InitializeComponent();
}
private void Draw()
{
if (_drawningAirFighter == null)
{
return;
}
Bitmap bmp = new(pictureBoxAirFighter.Width, pictureBoxAirFighter.Height);
Graphics gr = Graphics.FromImage(bmp);
_drawningAirFighter.DrawTransport(gr);
pictureBoxAirFighter.Image = bmp;
}
private void buttonCreateAirFighterMilitary_Click(object sender, EventArgs e)
{
Random random = new();
_drawningAirFighter = new DrawningAirFighterMilitary(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)),
pictureBoxAirFighter.Width, pictureBoxAirFighter.Height);
_drawningAirFighter.SetPosition(random.Next(10, 100), random.Next(10,
100));
Draw();
}
private void ButtonCreateFighter_Click(object sender, EventArgs e)
{
Random random = new();
_drawningAirFighter = new DrawningAirFighter(random.Next(100, 300),
random.Next(1000, 3000),
Color.FromArgb(random.Next(0, 256), random.Next(0, 256),
random.Next(0, 256)),
pictureBoxAirFighter.Width, pictureBoxAirFighter.Height);
_drawningAirFighter.SetPosition(random.Next(10, 100), random.Next(10,
100));
Draw();
}
private void buttonMove_Click(object sender, EventArgs e)
{
if (_drawningAirFighter == null)
{
return;
}
string name = ((Button)sender)?.Name ?? string.Empty;
switch (name)
{
case "buttonUp":
_drawningAirFighter.MoveTransport(DirectionAirFighter.Up);
break;
case "buttonDown":
_drawningAirFighter.MoveTransport(DirectionAirFighter.Down);
break;
case "buttonLeft":
_drawningAirFighter.MoveTransport(DirectionAirFighter.Left);
break;
case "buttonRight":
_drawningAirFighter.MoveTransport(DirectionAirFighter.Right);
break;
}
Draw();
}
private void ButtonStep_Click(object sender, EventArgs e)
{
if (_drawningAirFighter == null)
{
return;
}
if (comboBoxAirFighter.Enabled)
{
_abstractStrategy = comboBoxAirFighter.SelectedIndex
switch
{
0 => new MoveToCenter(),
1 => new MoveToBorder(),
_ => null,
};
if (_abstractStrategy == null)
{
return;
}
_abstractStrategy.SetData(new
DrawningObjectAirFighter(_drawningAirFighter), pictureBoxAirFighter.Width,
pictureBoxAirFighter.Height);
comboBoxAirFighter.Enabled = false;
}
if (_abstractStrategy != null)
{
_abstractStrategy.MakeStep();
Draw();
if (_abstractStrategy.GetStatus() == Status.Finish)
{
comboBoxAirFighter.Enabled = true;
_abstractStrategy = null;
}
}
}
}
}