2023-10-31 12:26:25 +04:00

124 lines
4.3 KiB
C#

using ProjectTank;
using ProjectTank.DrawningObjects;
using ProjectTank.MovementStrategy;
namespace ProjectTank
{
public partial class FormTank : Form
{
private DrawningTankBase? _drawningTank;
private AbstractStrategy? _strategy;
public DrawningTankBase? SelectedTank { get; private set; }
public FormTank()
{
InitializeComponent();
_strategy = null;
SelectedTank = null;
}
private void Draw()
{
if (_drawningTank == null) return;
Bitmap bmp = new(pictureBoxTank.Width, pictureBoxTank.Height);
Graphics gr = Graphics.FromImage(bmp);
_drawningTank.DrawTransport(gr);
pictureBoxTank.Image = bmp;
}
private void ButtonCreateTank_Click(object sender, EventArgs e)
{
Random random = new();
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));
Draw();
}
private void ButtonCreateTankBase_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;
}
_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));
Draw();
}
private void moveButton_Click(object sender, EventArgs e)
{
if (_drawningTank == null) return;
string name = ((Button)sender)?.Name ?? string.Empty;
switch (name)
{
case "buttonUp":
_drawningTank.MoveTransport(DirectionType.Up);
break;
case "buttonDown":
_drawningTank.MoveTransport(DirectionType.Down);
break;
case "buttonLeft":
_drawningTank.MoveTransport(DirectionType.Left);
break;
case "buttonRight":
_drawningTank.MoveTransport(DirectionType.Right);
break;
}
Draw();
}
private void ButtonStep_Click(object sender, EventArgs e)
{
if (_drawningTank == null) return;
if (comboBoxStrategy.Enabled)
{
_strategy = comboBoxStrategy.SelectedIndex
switch
{
0 => new MoveToCenter(),
1 => new MoveToRightBottomCorner(),
_ => null,
};
if (_strategy == null) return;
_strategy.SetData(new DrawningObjectTank(_drawningTank), pictureBoxTank.Width, pictureBoxTank.Height);
comboBoxStrategy.Enabled = false;
}
if (_strategy == null) return;
_strategy.MakeStep();
Draw();
if (_strategy.GetStatus() == MovementStrategy.Status.Finish)
{
comboBoxStrategy.Enabled = true;
_strategy = null;
}
}
private void ButtonSelectTank_Click(object sender, EventArgs e)
{
SelectedTank = _drawningTank;
DialogResult = DialogResult.OK;
}
}
}