PIbd-23_Dyakonov_R_R_Tank/ProjectTank/FormTank.cs

90 lines
3.5 KiB
C#
Raw Permalink Normal View History

2023-10-03 13:32:33 +04:00
using ProjectTank.DrawningObjects;
using ProjectTank.Entities;
using ProjectTank.MovementStrategy;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
2023-10-03 13:19:27 +04:00
namespace ProjectTank
{
public partial class FormTank : Form
{
2023-10-03 13:32:33 +04:00
private DrawningTankBase? DrawningTank;
private AbstractStrategy? AbstractStrategy;
2023-10-03 13:19:27 +04:00
public FormTank()
{
InitializeComponent();
}
private void Draw()
{
2023-10-03 13:32:33 +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-03 13:32:33 +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();
DrawningTank = new DrawningTank(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)), true, true, 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();
DrawningTank = new DrawningTankBase(random.Next(100, 300), random.Next(1000, 3000), Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)), pictureBoxTank.Width, pictureBoxTank.Height);
DrawningTank.SetPosition(random.Next(10, 100), random.Next(10, 100));
Draw();
}
2023-10-03 13:19:27 +04:00
private void moveButton_Click(object sender, EventArgs e)
{
2023-10-03 13:32:33 +04:00
if (DrawningTank == null) return;
2023-10-03 13:19:27 +04:00
2023-10-03 13:32:33 +04:00
string name = ((System.Windows.Forms.Button)sender)?.Name ?? string.Empty;
2023-10-03 13:19:27 +04:00
switch (name)
{
case "buttonUp":
2023-10-03 13:32:33 +04:00
DrawningTank.MoveTransport(DirectionType.Up);
2023-10-03 13:19:27 +04:00
break;
case "buttonDown":
2023-10-03 13:32:33 +04:00
DrawningTank.MoveTransport(DirectionType.Down);
2023-10-03 13:19:27 +04:00
break;
case "buttonLeft":
2023-10-03 13:32:33 +04:00
DrawningTank.MoveTransport(DirectionType.Left);
2023-10-03 13:19:27 +04:00
break;
case "buttonRight":
2023-10-03 13:32:33 +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)
{
if (DrawningTank == null) return;
if (comboBoxStrategy.Enabled)
{
AbstractStrategy = comboBoxStrategy.SelectedIndex
switch
{
0 => new MoveToCenter(),
1 => new MoveToRightBottomCorner(),
_ => null,
};
if (AbstractStrategy == null) return;
AbstractStrategy.SetData(new DrawningObjectTank(DrawningTank), pictureBoxTank.Width, pictureBoxTank.Height);
comboBoxStrategy.Enabled = false;
}
if (AbstractStrategy == null) return;
AbstractStrategy.MakeStep();
Draw();
if (AbstractStrategy.GetStatus() == MovementStrategy.Status.Finish)
{
comboBoxStrategy.Enabled = true;
AbstractStrategy = null;
}
}
2023-10-03 13:19:27 +04:00
}
}