using ProjectBattleship.MovementStrategy; using ProjectBattleship.DrawingObject; namespace ProjectBattleship; /// /// Форма работы с объектом "Спортивный автомобиль" /// public partial class FormBattleship : Form { /// /// Поле-объект для прорисовки объекта /// private DrawingWarship? _drawingWarship; /// /// Стратегия перемещения /// private AbstractStrategy? _strategy; /// /// Получение объекта /// public DrawingWarship SetWarship { set { _drawingWarship = value; _drawingWarship.SetPictureSize(pictureBoxBattleship.Width, pictureBoxBattleship.Height); comboBoxStrategy.Enabled = true; _strategy = null; Draw(); } } /// /// Конструктор формы /// public FormBattleship() { InitializeComponent(); _strategy = null; } /// /// Метод прорисовки военного корабля /// private void Draw() { if (_drawingWarship == null) { return; } Bitmap bmp = new(pictureBoxBattleship.Width, pictureBoxBattleship.Height); Graphics gr = Graphics.FromImage(bmp); _drawingWarship.DrawTransport(gr); pictureBoxBattleship.Image = bmp; } /// /// Перемещение объекта по форме (нажатие кнопок навигации) /// /// /// private void ButtonMove_Click(object sender, EventArgs e) { if (_drawingWarship == null) { return; } string name = ((Button)sender)?.Name ?? string.Empty; bool result = false; switch (name) { case "buttonUp": result = _drawingWarship.MoveTransport(DirectionType.Up); break; case "buttonDown": result = _drawingWarship.MoveTransport(DirectionType.Down); break; case "buttonLeft": result = _drawingWarship.MoveTransport(DirectionType.Left); break; case "buttonRight": result = _drawingWarship.MoveTransport(DirectionType.Right); break; } if (result) { Draw(); } } /// /// Обработка нажатия кнопки "Шаг" /// /// /// private void ButtonStrategyStep_Click(object sender, EventArgs e) { if (_drawingWarship == null) { return; } if (comboBoxStrategy.Enabled) { _strategy = comboBoxStrategy.SelectedIndex switch { 0 => new MoveToCenter(), 1 => new MoveToBorder(), _ => null, }; if (_strategy == null) { return; } _strategy.SetData(new MoveableWarship(_drawingWarship), pictureBoxBattleship.Width, pictureBoxBattleship.Height); } if (_strategy == null) { return; } comboBoxStrategy.Enabled = false; _strategy.MakeStep(); Draw(); if (_strategy.GetStatus() == StrategyStatus.Finish) { comboBoxStrategy.Enabled = true; _strategy = null; } } }