2024-03-20 13:21:28 +04:00

106 lines
2.7 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 DrawningTruck SetTruck
{
set
{
_drawningTruck = value;
_drawningTruck.SetPictureSize(pictureBoxDumpTruck.Width, pictureBoxDumpTruck.Height);
comboBoxStrategy.Enabled = true;
_strategy = null;
Draw();
}
}
public FormDumpTruck()
{
InitializeComponent();
_strategy = null;
}
private void Draw()
{
if (_drawningTruck == null) return;
Bitmap bmp = new(pictureBoxDumpTruck.Width,
pictureBoxDumpTruck.Height);
Graphics gr = Graphics.FromImage(bmp);
_drawningTruck.DrawTransport(gr);
pictureBoxDumpTruck.Image = bmp;
}
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), pictureBoxDumpTruck.Width, pictureBoxDumpTruck.Height);
}
if (_strategy == null) return;
comboBoxStrategy.Enabled = false;
_strategy.MakeStep();
Draw();
if (_strategy.GetStatus() == StrategyStatus.Finish)
{
comboBoxStrategy.Enabled = true;
_strategy = null;
}
}
}