139 lines
4.6 KiB
C#
139 lines
4.6 KiB
C#
using ContainerShip.DrawningObjects;
|
|
using ContainerShip.MovementStrategy;
|
|
|
|
namespace ContainerShip
|
|
{
|
|
public partial class FormShips : Form
|
|
{
|
|
private DrawningShip? _drawningShip;
|
|
private AbstractStrategy? _abstractStrategy;
|
|
public DrawningShip? SelectedShip { get; private set; }
|
|
|
|
public FormShips()
|
|
{
|
|
InitializeComponent();
|
|
_abstractStrategy = null;
|
|
SelectedShip = null;
|
|
}
|
|
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;
|
|
}
|
|
private void ButtonCreateContainerShip_Click(object sender, EventArgs e)
|
|
{
|
|
Random random = new Random();
|
|
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;
|
|
}
|
|
|
|
Color dopColor = Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256));
|
|
if (dialog.ShowDialog() == DialogResult.OK)
|
|
{
|
|
dopColor = dialog.Color;
|
|
}
|
|
|
|
_drawningShip = new DrawingContainerShip(random.Next(200, 400), random.Next(1000, 3000),
|
|
color, dopColor, Convert.ToBoolean(random.Next(0, 2)), Convert.ToBoolean(random.Next(0, 2)),
|
|
pictureBoxShips.Width, pictureBoxShips.Height);
|
|
|
|
_drawningShip.SetPosition(random.Next(10, 200), random.Next(10, 200));
|
|
Draw();
|
|
}
|
|
private void ButtonCreateShip_Click(object sender, EventArgs e)
|
|
{
|
|
Random random = new();
|
|
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;
|
|
}
|
|
_drawningShip = new DrawningShip(random.Next(100, 300),
|
|
random.Next(1000, 3000), color,
|
|
pictureBoxShips.Width, pictureBoxShips.Height);
|
|
_drawningShip.SetPosition(random.Next(10, 100), random.Next(10, 100));
|
|
Draw();
|
|
}
|
|
|
|
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();
|
|
}
|
|
private void ButtonStep_Click(object sender, EventArgs e)
|
|
{
|
|
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();
|
|
Draw();
|
|
if (_abstractStrategy.GetStatus() == Status.Finish)
|
|
{
|
|
comboBoxStrategy.Enabled = true;
|
|
_abstractStrategy = null;
|
|
}
|
|
}
|
|
|
|
private void buttonSelectedContainerShip_Click(object sender, EventArgs e)
|
|
{
|
|
SelectedShip = _drawningShip;
|
|
DialogResult = DialogResult.OK;
|
|
}
|
|
|
|
}
|
|
|
|
}
|