using ProjectAntiAircraftGun.DrawingObjects; using ProjectAntiAircraftGun.MovementStrategy; namespace ProjectAntiAircraftGun { /// /// Форма работы с объектом "Зенитная установка" /// public partial class FormAntiAircraftGun : Form { /// /// Поле-объект для прорисовки объекта /// private DrawingTank? _drawingTank; /// /// Стратегия перемещения /// private AbstractStrategy? _abstractStrategy; /// /// Инициализация формы /// public FormAntiAircraftGun() { InitializeComponent(); } /// /// Метод прорисовки зенитной установки /// private void Draw() { if (_drawingTank == null) { return; } Bitmap bmp = new(pictureBoxAntiAircraftGun.Width, pictureBoxAntiAircraftGun.Height); Graphics gr = Graphics.FromImage(bmp); _drawingTank.DrawTransport(gr); pictureBoxAntiAircraftGun.Image = bmp; } /// /// Обработка нажатия кнопки "Создать танк" /// /// /// private void ButtonCreateTank_Click(object sender, EventArgs e) { Random random = new(); _drawingTank = new DrawingTank(random.Next(100, 300), random.Next(1000, 3000), 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)), pictureBoxAntiAircraftGun.Width, pictureBoxAntiAircraftGun.Height); _drawingTank.SetPosition(random.Next(10, 100), random.Next(10, 100)); Draw(); } /// /// Обработка нажатия кнопки "Создать зенитную установку" /// /// /// private void ButtonCreateAntiAircraftGun(object sender, EventArgs e) { Random random = new(); _drawingTank = new DrawingAntiAircraftGun(random.Next(100, 300), random.Next(1000, 3000), 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)), Convert.ToBoolean(random.Next(0, 2)), pictureBoxAntiAircraftGun.Width, pictureBoxAntiAircraftGun.Height); _drawingTank.SetPosition(random.Next(10, 100), random.Next(10, 100)); Draw(); } /// /// Обработка нажатий кнопок управления зенитной установкой /// /// /// private void ButtonMove_Click(object sender, EventArgs e) { if (_drawingTank == null) return; string name = ((Button)sender)?.Name ?? string.Empty; switch (name) { case "buttonUp": _drawingTank.MoveTransport(DirectionType.Up); break; case "buttonDown": _drawingTank.MoveTransport(DirectionType.Down); break; case "buttonLeft": _drawingTank.MoveTransport(DirectionType.Left); break; case "buttonRight": _drawingTank.MoveTransport(DirectionType.Right); break; } Draw(); } /// /// Обработка нажатия кнопки "Шаг" /// /// /// private void ButtonStep_Click(object sender, EventArgs e) { if (_drawingTank == null) { return; } if (comboBoxStrategy.Enabled) { _abstractStrategy = comboBoxStrategy.SelectedIndex switch { 0 => new MoveToCenter(), 1 => new MoveToBorder(), _ => null, }; if (_abstractStrategy == null) { return; } _abstractStrategy.SetData(new DrawingObjectTank(_drawingTank), pictureBoxAntiAircraftGun.Width, pictureBoxAntiAircraftGun.Height); comboBoxStrategy.Enabled = false; } if (_abstractStrategy == null) { return; } _abstractStrategy.MakeStep(); Draw(); if (_abstractStrategy.GetStatus() == Status.Finish) { comboBoxStrategy.Enabled = true; _abstractStrategy = null; } } } }