122 lines
3.7 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using ProjectTank.Drawnings;
using ProjectTank.Entities;
using ProjectTank.MovementStrategy;
namespace ProjectTank
{
/// <summary>
/// Форма работы с объектом "Танк"
/// </summary>
public partial class FormTank : Form
{
/// <summary>
/// Поле-объект для прорисовки объекта
/// </summary>
private DrawningTank2? _drawningTank;
/// <summary>
/// Стратегия перемещения
/// </summary>
private AbstractStrategy? _strategy;
public DrawningTank2 SetTank
{
set
{
_drawningTank = value;
_drawningTank.SetPictureSize(pictureBoxTanks.Width, pictureBoxTanks.Height);
СomboBoxStrategy.Enabled = true;
_strategy = null;
Draw();
}
}
/// <summary>
/// Конструктор формы
/// </summary>
public FormTank()
{
InitializeComponent();
_strategy = null;
}
/// <summary>
/// Метод прорисовки танка
/// </summary>
private void Draw()
{
if (_drawningTank == null)
{
return;
}
Bitmap bmp = new(pictureBoxTanks.Width, pictureBoxTanks.Height);
Graphics gr = Graphics.FromImage(bmp);
_drawningTank.DrawTransport(gr);
pictureBoxTanks.Image = bmp;
}
private void ButtonMove_Click(object sender, EventArgs e)
{
if (_drawningTank == null)
{
return;
}
string name = ((Button)sender)?.Name ?? string.Empty;
bool result = false;
switch (name)
{
case "buttonUp":
result = _drawningTank.MoveTransport(DirectionType.Up);
break;
case "buttonLeft":
result = _drawningTank.MoveTransport(DirectionType.Left);
break;
case "buttonRight":
result = _drawningTank.MoveTransport(DirectionType.Right);
break;
case "buttonDown":
result = _drawningTank.MoveTransport(DirectionType.Down);
break;
}
if (result)
{
Draw();
}
}
/// <summary>
/// Обработка нажатия кнопки "Шаг"
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonStrategyStep_Ckick(object sender, EventArgs e)
{
if (_drawningTank == null)
{
return;
}
if (СomboBoxStrategy.Enabled)
{
_strategy = СomboBoxStrategy.SelectedIndex switch
{
0 => new MoveToCenter(),
1 => new MoveToBorder(),
_ => null,
};
if (_strategy == null)
{
return;
}
_strategy.SetData(new MoveableTank(_drawningTank),
pictureBoxTanks.Width, pictureBoxTanks.Height);
}
if (_strategy == null)
{
return;
}
СomboBoxStrategy.Enabled = false;
_strategy.MakeStep();
Draw();
if (_strategy.GetStatus() == StrategyStatus.Finish)
{
СomboBoxStrategy.Enabled = true;
_strategy = null;
}
}
}
}