using AirBomber.Drawnings; using AirBomber.MovementStrategy; namespace AirBomber; /// /// Форма для работы с объектом /// public partial class FormAirBomber : Form { /// /// Поле-объект для прорисовки объекта /// private DrawningAirPlane? _drawningAirPlane; private AbstractStrategy? _strategy; 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 CreateObject(string type) { Random random = new(); switch (type) { case nameof(DrawningAirPlane): _drawningAirPlane = new DrawningAirPlane(random.Next(30, 100), random.Next(100, 500), Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256))); break; case nameof(DrawningAirBomber): _drawningAirPlane = new DrawningAirBomber(random.Next(30, 100), random.Next(100, 500), Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)), Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)), Convert.ToBoolean(random.Next(0, 2)), Convert.ToBoolean(random.Next(0, 2))); break; default: return; } _drawningAirPlane.SetPictureSize(pictureBoxAirBomber.Width, pictureBoxAirBomber.Height); _drawningAirPlane.SetPosition(random.Next(15, 100), random.Next(15, 100)); _strategy = null; comboBoxStrategy.Enabled = true; Draw(); } /// /// Обработка нажатия кнопки "Создать бомбардировщик" /// /// /// private void ButtonCreateAirBomber_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningAirBomber)); /// /// Обработка нажатия кнопки "Создать военный самолет" /// /// /// private void ButtonCreateAirPlane_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningAirPlane)); 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; } } }