PIbd22_NikiforovaMV_Contain.../ContainerShip/FormShips.cs

123 lines
3.9 KiB
C#
Raw Permalink Normal View History

2023-11-25 12:44:21 +04:00
using ContainerShip.DrawningObjects;
using ContainerShip.MovementStrategy;
2023-11-25 12:27:56 +04:00
namespace ContainerShip
{
public partial class FormShips : Form
{
2023-11-25 12:44:21 +04:00
private DrawningShip? _drawningShip;
private AbstractStrategy? _abstractStrategy;
2023-11-25 12:27:56 +04:00
public FormShips()
{
InitializeComponent();
}
private void Draw()
{
if (_drawningShip == null)
{
return;
}
Bitmap bmp = new(pictureBoxShips.Width,
pictureBoxShips.Height);
Graphics gr = Graphics.FromImage(bmp);
_drawningShip.DrawTransport(gr);
pictureBoxShips.Image = bmp;
}
2023-11-25 12:44:21 +04:00
private void ButtonCreateContainerShip_Click(object sender, EventArgs e)
{
Random random = new();
_drawningShip = new DrawingContainerShip(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)),
pictureBoxShips.Width, pictureBoxShips.Height);
_drawningShip.SetPosition(random.Next(10, 100), random.Next(10,
100));
Draw();
}
private void ButtonCreateShip_Click(object sender, EventArgs e)
{
Random random = new();
_drawningShip = new DrawningShip(random.Next(100, 300),
random.Next(1000, 3000),
Color.FromArgb(random.Next(0, 256), random.Next(0, 256),
random.Next(0, 256)),
pictureBoxShips.Width, pictureBoxShips.Height);
_drawningShip.SetPosition(random.Next(10, 100), random.Next(10,
100));
Draw();
}
2023-11-25 12:27:56 +04:00
private void buttonMove_Click(object sender, EventArgs e)
{
if (_drawningShip == null)
{
return;
}
string name = ((Button)sender)?.Name ?? string.Empty;
switch (name)
{
case "buttonUp":
_drawningShip.MoveTransport(DirectionType.Up);
break;
case "buttonDown":
_drawningShip.MoveTransport(DirectionType.Down);
break;
case "buttonLeft":
_drawningShip.MoveTransport(DirectionType.Left);
break;
case "buttonRight":
_drawningShip.MoveTransport(DirectionType.Right);
break;
}
Draw();
}
2023-11-25 12:44:21 +04:00
private void ButtonStep_Click(object sender, EventArgs e)
2023-11-25 12:27:56 +04:00
{
2023-11-25 12:44:21 +04:00
if (_drawningShip == null)
{
return;
}
if (comboBoxStrategy.Enabled)
{
_abstractStrategy = comboBoxStrategy.SelectedIndex
switch
{
0 => new MoveToCenter(),
1 => new MoveToBorder(),
_ => null,
};
if (_abstractStrategy == null)
{
return;
}
_abstractStrategy.SetData(new
DrawingObjectShip(_drawningShip), pictureBoxShips.Width,
pictureBoxShips.Height);
comboBoxStrategy.Enabled = false;
}
if (_abstractStrategy == null)
{
return;
}
_abstractStrategy.MakeStep();
2023-11-25 12:27:56 +04:00
Draw();
2023-11-25 12:44:21 +04:00
if (_abstractStrategy.GetStatus() == Status.Finish)
{
comboBoxStrategy.Enabled = true;
_abstractStrategy = null;
}
2023-11-25 12:27:56 +04:00
}
}
}