using ProjectTank.Drawnings; using ProjectTank.Entities; using ProjectTank.MovementStrategy; namespace ProjectTank { /// /// Форма работы с объектом "Танк" /// public partial class FormTank : Form { /// /// Поле-объект для прорисовки объекта /// private DrawningTank2? _drawningTank; /// /// Стратегия перемещения /// private AbstractStrategy? _strategy; public DrawningTank2 SetTank { set { _drawningTank = value; _drawningTank.SetPictureSize(pictureBoxTanks.Width, pictureBoxTanks.Height); СomboBoxStrategy.Enabled = true; _strategy = null; Draw(); } } /// /// Конструктор формы /// public FormTank() { InitializeComponent(); _strategy = null; } /// /// Метод прорисовки танка /// 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(); } } /// /// Обработка нажатия кнопки "Шаг" /// /// /// 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; } } } }