using AirBomber.Drawnings; using AirBomber.MovementStrategy; namespace AirBomber; /// /// Форма для работы с объектом /// public partial class FormAirBomber : Form { /// /// Поле-объект для прорисовки объекта /// private DrawningAirPlane? _drawningAirPlane; /// /// Стратегия перемещения /// private AbstractStrategy? _strategy; public DrawningAirPlane SetAirPlane { set { _drawningAirPlane = value; _drawningAirPlane.SetPictureSize(pictureBoxAirBomber.Width, pictureBoxAirBomber.Height); comboBoxStrategy.Enabled = true; _strategy = null; Draw(); } } public FormAirBomber() { InitializeComponent(); _strategy = null; } private void Draw() { if (_drawningAirPlane == null) { return; } if (pictureBoxAirBomber.Width == 0 || pictureBoxAirBomber.Height == 0) { return; } Bitmap bpm = new(pictureBoxAirBomber.Width, pictureBoxAirBomber.Height); Graphics gr = Graphics.FromImage(bpm); _drawningAirPlane?.DrawTransport(gr); pictureBoxAirBomber.Image = bpm; } private void ButtonMove_Click(object sender, EventArgs e) { if (_drawningAirPlane == null) { return; } string name = ((Button)sender)?.Name ?? string.Empty; bool result = false; switch (name) { case "ButtonUp": result = _drawningAirPlane.MoveTransport(DirectionType.Up); break; case "ButtonDown": result = _drawningAirPlane.MoveTransport(DirectionType.Down); break; case "ButtonLeft": result = _drawningAirPlane.MoveTransport(DirectionType.Left); break; case "ButtonRight": result = _drawningAirPlane.MoveTransport(DirectionType.Right); break; } if (result) { Draw(); } } private void buttonStrategyStep_Click(object sender, EventArgs e) { if (_drawningAirPlane == null) { return; } if (comboBoxStrategy.Enabled) { _strategy = comboBoxStrategy.SelectedIndex switch { 0 => new MoveToCenter(), 1 => new MoveToBorder(), _ => null, }; if (_strategy == null) { return; } _strategy.SetData(new MoveableAirPlane(_drawningAirPlane), pictureBoxAirBomber.Width, pictureBoxAirBomber.Height); } if (_strategy == null) { return; } comboBoxStrategy.Enabled = false; _strategy.MakeStep(); Draw(); if (_strategy.GetStatus() == StrategyStatus.Finish) { comboBoxStrategy.Enabled = true; _strategy = null; } } }