124 lines
4.3 KiB
C#
Raw Normal View History

2023-10-31 12:26:25 +04:00
using ProjectTank;
2023-10-03 13:32:33 +04:00
using ProjectTank.DrawningObjects;
using ProjectTank.MovementStrategy;
2023-10-03 13:19:27 +04:00
namespace ProjectTank
{
public partial class FormTank : Form
{
2023-10-31 12:26:25 +04:00
private DrawningTankBase? _drawningTank;
private AbstractStrategy? _strategy;
public DrawningTankBase? SelectedTank { get; private set; }
2023-10-03 13:19:27 +04:00
public FormTank()
{
InitializeComponent();
2023-10-31 12:26:25 +04:00
_strategy = null;
SelectedTank = null;
2023-10-03 13:19:27 +04:00
}
private void Draw()
{
2023-10-31 12:26:25 +04:00
if (_drawningTank == null) return;
2023-10-03 13:19:27 +04:00
Bitmap bmp = new(pictureBoxTank.Width, pictureBoxTank.Height);
Graphics gr = Graphics.FromImage(bmp);
2023-10-31 12:26:25 +04:00
_drawningTank.DrawTransport(gr);
2023-10-03 13:19:27 +04:00
pictureBoxTank.Image = bmp;
}
2023-10-03 13:32:33 +04:00
private void ButtonCreateTank_Click(object sender, EventArgs e)
{
Random random = new();
2023-10-31 12:26:25 +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 dialog = new();
if (dialog.ShowDialog() == DialogResult.OK)
{
bodyColor = dialog.Color;
if (dialog.ShowDialog() == DialogResult.OK)
additionalColor = dialog.Color;
}
_drawningTank = new DrawningTank(random.Next(100, 300), random.Next(1000, 3000), bodyColor,
additionalColor, Convert.ToBoolean(random.Next(0, 2)),
Convert.ToBoolean(random.Next(0, 2)), pictureBoxTank.Width, pictureBoxTank.Height);
_drawningTank.SetPosition(random.Next(10, 100), random.Next(10, 100));
2023-10-03 13:32:33 +04:00
Draw();
}
private void ButtonCreateTankBase_Click(object sender, EventArgs e)
{
Random random = new();
2023-10-31 12:26:25 +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;
}
_drawningTank = new DrawningTankBase(random.Next(100, 300), random.Next(1000, 3000), color,
pictureBoxTank.Width, pictureBoxTank.Height);
_drawningTank.SetPosition(random.Next(10, 100), random.Next(10, 100));
2023-10-03 13:32:33 +04:00
Draw();
2023-10-31 12:26:25 +04:00
2023-10-03 13:32:33 +04:00
}
2023-10-03 13:19:27 +04:00
private void moveButton_Click(object sender, EventArgs e)
{
2023-10-31 12:26:25 +04:00
if (_drawningTank == null) return;
2023-10-03 13:19:27 +04:00
2023-10-31 12:26:25 +04:00
string name = ((Button)sender)?.Name ?? string.Empty;
2023-10-03 13:19:27 +04:00
switch (name)
{
case "buttonUp":
2023-10-31 12:26:25 +04:00
_drawningTank.MoveTransport(DirectionType.Up);
2023-10-03 13:19:27 +04:00
break;
case "buttonDown":
2023-10-31 12:26:25 +04:00
_drawningTank.MoveTransport(DirectionType.Down);
2023-10-03 13:19:27 +04:00
break;
case "buttonLeft":
2023-10-31 12:26:25 +04:00
_drawningTank.MoveTransport(DirectionType.Left);
2023-10-03 13:19:27 +04:00
break;
case "buttonRight":
2023-10-31 12:26:25 +04:00
_drawningTank.MoveTransport(DirectionType.Right);
2023-10-03 13:19:27 +04:00
break;
}
Draw();
}
2023-10-03 13:32:33 +04:00
private void ButtonStep_Click(object sender, EventArgs e)
{
2023-10-31 12:26:25 +04:00
if (_drawningTank == null) return;
2023-10-03 13:32:33 +04:00
if (comboBoxStrategy.Enabled)
{
2023-10-31 12:26:25 +04:00
_strategy = comboBoxStrategy.SelectedIndex
2023-10-03 13:32:33 +04:00
switch
{
0 => new MoveToCenter(),
1 => new MoveToRightBottomCorner(),
_ => null,
};
2023-10-31 12:26:25 +04:00
if (_strategy == null) return;
_strategy.SetData(new DrawningObjectTank(_drawningTank), pictureBoxTank.Width, pictureBoxTank.Height);
2023-10-03 13:32:33 +04:00
comboBoxStrategy.Enabled = false;
}
2023-10-31 12:26:25 +04:00
if (_strategy == null) return;
2023-10-03 13:32:33 +04:00
2023-10-31 12:26:25 +04:00
_strategy.MakeStep();
2023-10-03 13:32:33 +04:00
Draw();
2023-10-31 12:26:25 +04:00
if (_strategy.GetStatus() == MovementStrategy.Status.Finish)
2023-10-03 13:32:33 +04:00
{
comboBoxStrategy.Enabled = true;
2023-10-31 12:26:25 +04:00
_strategy = null;
2023-10-03 13:32:33 +04:00
}
}
2023-10-31 12:26:25 +04:00
private void ButtonSelectTank_Click(object sender, EventArgs e)
{
SelectedTank = _drawningTank;
DialogResult = DialogResult.OK;
}
2023-10-03 13:19:27 +04:00
}
}