using WarmlyShip.DrawningObjects; using WarmlyShip.MovementStrategy; namespace WarmlyShip { public partial class FormWarmlyShip : Form { /// /// Форма работы с объектом "Теплоход" /// private DrawningShip? _drawningShip; /// /// Стратегия перемещения /// private AbstractStrategy? _abstractStrategy; /// /// Инициализация формы /// public FormWarmlyShip() { InitializeComponent(); } /// /// Метод прорисовки корабля /// private void Draw() { if (_drawningShip == null) { return; } Bitmap bmp = new(pictureBoxWarmlyShip.Width, pictureBoxWarmlyShip.Height); Graphics gr = Graphics.FromImage(bmp); _drawningShip.DrawTransport(gr); pictureBoxWarmlyShip.Image = bmp; } /// /// Обработка нажатия кнопки "Создать теплоход" /// /// /// private void buttonCreateWarmlyShip_Click(object sender, EventArgs e) { Random random = new(); _drawningShip = new DrawningWarmlyShip(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)), pictureBoxWarmlyShip.Width, pictureBoxWarmlyShip.Height); _drawningShip.SetPosition(random.Next(10, 100), random.Next(10, 100)); Draw(); } /// /// Обработка нажатия кнопки "Создать корабль" /// /// /// private void buttonCreateShip_Click(object sender, EventArgs e) { Random random = new(); _drawningShip = new DrawningShip(random.Next(100, 300), random.Next(1000, 3000), Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)), pictureBoxWarmlyShip.Width, pictureBoxWarmlyShip.Height); _drawningShip.SetPosition(random.Next(10, 100), random.Next(10, 100)); Draw(); } /// /// Изменение положения корабля /// /// /// private void ButtonMove_Click(object sender, EventArgs e) { if (_drawningShip == null) { return; } string name = ((Button)sender)?.Name ?? string.Empty; switch (name) { case "buttonUp": _drawningShip.MoveTransport(DirectionType.Up); break; case "buttonDown": _drawningShip.MoveTransport(DirectionType.Down); break; case "buttonLeft": _drawningShip.MoveTransport(DirectionType.Left); break; case "buttonRight": _drawningShip.MoveTransport(DirectionType.Right); break; } Draw(); } /// /// Обработка нажатия кнопки "Шаг" /// /// /// private void buttonStep_Click(object sender, EventArgs e) { if (_drawningShip == null) { return; } if (comboBoxStrategy.Enabled) { _abstractStrategy = comboBoxStrategy.SelectedIndex switch { 0 => new MoveToCenter(), 1 => new MoveToBorder(), _ => null, }; if (_abstractStrategy == null) { return; } _abstractStrategy.SetData(new DrawningObjectShip(_drawningShip), pictureBoxWarmlyShip.Width, pictureBoxWarmlyShip.Height); comboBoxStrategy.Enabled = false; } if (_abstractStrategy == null) { return; } _abstractStrategy.MakeStep(); Draw(); if (_abstractStrategy.GetStatus() == Status.Finish) { comboBoxStrategy.Enabled = true; _abstractStrategy = null; } } } }