2024-03-20 00:56:13 +04:00

141 lines
4.3 KiB
C#

using System.Diagnostics;
using ProjectDumpTruck.Drawnings;
using ProjectDumpTruck.MovementStrategy;
namespace ProjectDumpTruck;
public partial class FormDumpTruck : Form
{
private DrawningTruck? _drawningTruck;
/// <summary>
/// Стратегия перемещения
/// </summary>
private AbstractStrategy? _strategy;
public FormDumpTruck()
{
InitializeComponent();
_strategy = null;
}
private void Draw()
{
if (_drawningTruck == null) return;
Bitmap bmp = new(pictureBoxDumpTruck1.Width,
pictureBoxDumpTruck1.Height);
Graphics gr = Graphics.FromImage(bmp);
_drawningTruck.DrawTransport(gr);
pictureBoxDumpTruck1.Image = bmp;
}
/// <summary>
/// Создание объекта класса-перемещения
/// </summary>
/// <param name="type"></param>
private void CreateObject(string type)
{
Random random = new();
switch (type)
{
case nameof(DrawningTruck):
_drawningTruck = new DrawningTruck(random.Next(100, 300), random.Next(1000, 3000),
Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)));
break;
case nameof(DrawningDumpTruck):
_drawningTruck = new DrawningDumpTruck(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)));
break;
default:
return;
}
_drawningTruck.SetPictureSize(pictureBoxDumpTruck1.Width, pictureBoxDumpTruck1.Height);
_drawningTruck.SetPosition(random.Next(10, 100), random.Next(10, 100));
_strategy = null;
//comboBoxStrategy.Enabled = true;
Draw();
}
/// <summary>
/// Обработка нажатия кнокпки "Создать самосвал"
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonCreateDumpTruck_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningDumpTruck));
/// <summary>
/// Обработка нажатия кнокпки "Создать грузовик"
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonCreateTruck_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningTruck));
private void ButtonMove_Click(object sender, EventArgs e)
{
if (_drawningTruck == null) return;
string name = ((Button)sender)?.Name ?? string.Empty;
bool result = false;
switch (name)
{
case "buttonLeft":
result = _drawningTruck.MoveTransport(DirectionType.Left); break;
case "buttonDown":
result = _drawningTruck.MoveTransport(DirectionType.Down); break;
case "buttonUp":
result = _drawningTruck.MoveTransport(DirectionType.Up); break;
case "buttonRight":
result = _drawningTruck.MoveTransport(DirectionType.Right); break;
}
if (result) Draw();
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonStrategyStep_Click(object sender, EventArgs e)
{
if (_drawningTruck == null) return;
if (comboBoxStrategy.Enabled)
{
_strategy = comboBoxStrategy.SelectedIndex switch
{
0 => new MoveToCenter(),
1 => new MoveToBorder(),
_ => null,
};
if (_strategy == null) return;
_strategy.SetData(new MoveableTruck(_drawningTruck), pictureBoxDumpTruck1.Width, pictureBoxDumpTruck1.Height);
}
if (_strategy == null) return;
comboBoxStrategy.Enabled = false;
_strategy.MakeStep();
Draw();
if (_strategy.GetStatus() == StrategyStatus.Finish)
{
comboBoxStrategy.Enabled = true;
_strategy = null;
}
}
}