PIbd-22_Shabunov_O.A._AirBo.../AirBomber/BomberForm.cs

164 lines
4.8 KiB
C#
Raw Normal View History

2023-11-19 00:10:06 +04:00
using AirBomber.MovementStrategy;
using AirBomber.Rendering;
2023-11-18 22:48:03 +04:00
namespace AirBomber
{
public partial class BomberForm : Form
{
private BomberRendererBase? _bomberRenderer;
2023-11-19 00:10:06 +04:00
private AbstractStrategy? _strategy;
2023-11-18 22:48:03 +04:00
2023-11-19 17:11:10 +04:00
public BomberRendererBase? SelectedRenderer { get; private set; }
2023-11-18 22:48:03 +04:00
public BomberForm()
{
InitializeComponent();
2023-11-19 17:11:10 +04:00
_strategy = null;
SelectedRenderer = null;
2023-11-18 22:48:03 +04:00
}
private void Draw()
{
if (_bomberRenderer == null)
return;
Bitmap bmp = new Bitmap(BomberPictureBox.Width, BomberPictureBox.Height);
Graphics g = Graphics.FromImage(bmp);
_bomberRenderer.DrawEntity(g);
BomberPictureBox.Image = bmp;
}
2023-11-19 00:10:06 +04:00
private void ButtonCreateBomber_Click(object sender, EventArgs e)
2023-11-18 22:48:03 +04:00
{
Random random = new Random();
2023-11-19 17:11:10 +04:00
Color BodyColor = Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256));
Color AdditionalColor = Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256));
ColorDialog BodyColorPicker = new ColorDialog();
if (BodyColorPicker.ShowDialog() == DialogResult.OK)
BodyColor = BodyColorPicker.Color;
ColorDialog AdditionalColorPicker = new ColorDialog();
if (AdditionalColorPicker.ShowDialog() == DialogResult.OK)
AdditionalColor = AdditionalColorPicker.Color;
2023-11-19 00:10:06 +04:00
_bomberRenderer = new BomberRenderer(
2023-11-18 22:48:03 +04:00
random.Next(100, 300),
random.Next(1000, 3000),
2023-11-19 17:11:10 +04:00
BodyColor,
AdditionalColor,
2023-11-18 22:48:03 +04:00
true,
true,
BomberPictureBox.Width,
BomberPictureBox.Height
);
2023-11-19 00:10:06 +04:00
_bomberRenderer.SetPosition(random.Next(10, 60), random.Next(10, 60));
MovementStrategyComboBox.Enabled = true;
Draw();
}
private void ButtonCreateBomberBase_Click(object sender, EventArgs e)
{
Random random = new Random();
2023-11-19 17:11:10 +04:00
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;
}
2023-11-19 00:10:06 +04:00
_bomberRenderer = new BomberRendererBase(
random.Next(100, 200),
random.Next(1000, 1800),
2023-11-19 17:11:10 +04:00
color,
2023-11-19 00:10:06 +04:00
BomberPictureBox.Width,
BomberPictureBox.Height
);
2023-11-18 22:48:03 +04:00
_bomberRenderer.SetPosition(random.Next(20, 100), random.Next(20, 100));
2023-11-19 00:10:06 +04:00
MovementStrategyComboBox.Enabled = true;
2023-11-18 22:48:03 +04:00
Draw();
}
private void ButtonMove_Click(object sender, EventArgs e)
{
if (_bomberRenderer == null)
return;
string ButtonName = ((Button)sender)?.Name ?? string.Empty;
switch (ButtonName)
{
case "ButtonUp":
_bomberRenderer.MoveEntity(DirectionType.Up);
break;
case "ButtonDown":
_bomberRenderer.MoveEntity(DirectionType.Down);
break;
case "ButtonLeft":
_bomberRenderer.MoveEntity(DirectionType.Left);
break;
case "ButtonRight":
_bomberRenderer.MoveEntity(DirectionType.Right);
break;
}
Draw();
}
2023-11-19 00:10:06 +04:00
private void ButtonPerformStep_Click(object sender, EventArgs e)
{
if (_bomberRenderer is null)
return;
if (MovementStrategyComboBox.Enabled)
{
_strategy = MovementStrategyComboBox.SelectedIndex switch
{
0 => new MoveToCenterStrategy(),
1 => new MoveToDownRightStrategy(),
_ => null,
};
if (_strategy is null)
return;
_strategy.SetData(
2023-11-19 17:11:10 +04:00
_bomberRenderer.MovableObject,
2023-11-19 00:10:06 +04:00
BomberPictureBox.Width,
BomberPictureBox.Height
);
}
if (_strategy is null)
return;
2023-11-19 17:11:10 +04:00
MovementStrategyComboBox.Enabled = false;
2023-11-19 00:10:06 +04:00
_strategy.MakeStep();
Draw();
if (_strategy.GetStatus() == Status.Finish)
{
MovementStrategyComboBox.Enabled = true;
_strategy = null;
}
}
2023-11-19 17:11:10 +04:00
private void ButtonSelectEntity_Click(object sender, EventArgs e)
{
SelectedRenderer = _bomberRenderer;
DialogResult = DialogResult.OK;
}
2023-11-18 22:48:03 +04:00
}
}