using ProjectCruiser.DrawningSamples; using ProjectCruiser.MoveStrategy; namespace ProjectCruiser { public partial class OceanForm1 : Form { // Поле-объект для прорисовки объекта private DrawningBase? _drawningCruiser; // Стратегия перемещения private AbstractStrategy? _strategy; // Получение объекта public DrawningBase SetShip { set { _drawningCruiser = value; _drawningCruiser.SetPictureSize(pictureBoxCr.Width, pictureBoxCr.Height); comboBoxStrategy.Enabled = true; _strategy = null; Draw(); } } public OceanForm1() { InitializeComponent(); _strategy = null; } // Метод прорисовки transport private void Draw() { if (_drawningCruiser == null) { return; } Bitmap bmp = new(pictureBoxCr.Width, pictureBoxCr.Height); Graphics gr = Graphics.FromImage(bmp); _drawningCruiser.DrawTransport(gr); pictureBoxCr.Image = bmp; } // Обработка нажатия кнопок "Create(...)" private void btnCreateBase_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningBase)); private void btnCreateAdvanced_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningCruiser)); // Создание объекта класса-перемещения /// Тип создаваемого объекта private void CreateObject(string type) { Random random = new(); switch (type) { case nameof(DrawningBase): _drawningCruiser = new DrawningBase(random.Next(100, 300), random.Next(1000, 3000), Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256))); break; case nameof(DrawningCruiser): _drawningCruiser = new DrawningCruiser( 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))); break; default: return; } _drawningCruiser.SetPictureSize(pictureBoxCr.Width, pictureBoxCr.Height); _drawningCruiser.SetPosition(random.Next(10, 100), pictureBoxCr.Height - random.Next(10, 100) - _drawningCruiser.getHeight()); _strategy = null; comboBoxStrategy.Enabled = true; Draw(); } private void BtnMove_Click(object sender, EventArgs e) { if (_drawningCruiser == null) { return; } string name = ((Button)sender)?.Name ?? string.Empty; bool result = false; switch (name) { case "btnUpArrow": result = _drawningCruiser.MoveTransport(DirectionType.Up); break; case "btnDownArrow": result = _drawningCruiser.MoveTransport(DirectionType.Down); break; case "btnLeftArrow": result = _drawningCruiser.MoveTransport(DirectionType.Left); break; case "btnRightArrow": result = _drawningCruiser.MoveTransport(DirectionType.Right); break; } if (result) { Draw(); } } private void ButtonStrategyStep_Click(object sender, EventArgs e) { if (_drawningCruiser == null) { return; } if (comboBoxStrategy.Enabled) { _strategy = comboBoxStrategy.SelectedIndex switch { 0 => new MoveToCentre(), 1 => new MoveToBorder(), _ => null, }; if (_strategy == null) { return; } _strategy.SetData(new MoveableTransport(_drawningCruiser), pictureBoxCr.Width, pictureBoxCr.Height); } if (_strategy == null) { return; } comboBoxStrategy.Enabled = false; _strategy.MakeStep(); Draw(); if (_strategy.GetStatus() == StrategyStatus.Finish) { comboBoxStrategy.Enabled = true; _strategy = null; } } } }