using System;
using Cruiser.Drawing;
using Cruiser.Entities;
using Cruiser.MovementStrategy;
namespace Cruiser
{
public partial class FormCruiser : Form
{
Bitmap bmp;
///
/// Поле-объект для прорисовки объекта
///
private DrawingCruiser? _drawningCruiser;
///
/// Стратегия перемещения
///
private AbstractStrategy? _abstractStrategy;
///
/// Выбранный лайнер
///
public DrawingCruiser? SelectedCruiser { get; private set; }
///
/// Инициализация формы
///
public FormCruiser()
{
InitializeComponent();
bmp = new(pictureBoxCruiser.Width, pictureBoxCruiser.Width);
_abstractStrategy = null;
SelectedCruiser = null;
}
///
/// Метод прорисовки крейсера
///
private void Draw()
{
if (_drawningCruiser == null)
{
return;
}
Graphics gr = Graphics.FromImage(bmp);
gr.Clear(Color.White);
_drawningCruiser.DrawTransport(gr);
pictureBoxCruiser.Image = bmp;
}
///
/// Изменение положения автомобиля
///
///
///
private void ButtonMove_Click(object sender, EventArgs e)
{
if (_drawningCruiser == null)
{
return;
}
string name = ((Button)sender)?.Name ?? string.Empty;
switch (name)
{
case "buttonUp":
_drawningCruiser.MoveTransport(Direction.Up);
break;
case "buttonDown":
_drawningCruiser.MoveTransport(Direction.Down);
break;
case "buttonLeft":
_drawningCruiser.MoveTransport(Direction.Left);
break;
case "buttonRight":
_drawningCruiser.MoveTransport(Direction.Right);
break;
}
Draw();
}
///
/// Обработка нажатия кнопки "Шаг"
///
///
///
private void ButtonStep_Click(object sender, EventArgs e)
{
if (_drawningCruiser == null)
{
return;
}
if (comboBoxStrategy.Enabled)
{
_abstractStrategy = comboBoxStrategy.SelectedIndex
switch
{
0 => new MoveToCenter(),
1 => new MoveToBorder(),
_ => null,
};
if (_abstractStrategy == null)
{
return;
}
_abstractStrategy.SetData(new
DrawningObjectCar(_drawningCruiser), pictureBoxCruiser.Width,
pictureBoxCruiser.Height);
comboBoxStrategy.Enabled = false;
}
if (_abstractStrategy == null)
{
return;
}
_abstractStrategy.MakeStep();
Draw();
if (_abstractStrategy.GetStatus() == Status.Finish)
{
comboBoxStrategy.Enabled = true;
_abstractStrategy = null;
}
}
private void buttonCreateLiner_Click(object sender, EventArgs e)
{
Random random = new();
Color colorFirst = Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256));
ColorDialog dialog = new();
if (dialog.ShowDialog() == DialogResult.OK)
{
colorFirst = dialog.Color;
}
_drawningCruiser = new DrawingCruiser(random.Next(100, 300),
random.Next(1000, 3000),
colorFirst,
Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)),
pictureBoxCruiser.Width,
pictureBoxCruiser.Height);
_drawningCruiser.SetPosition(random.Next(10, 100), random.Next(10, 100));
Draw();
}
private void buttonCreateProLiner_Click(object sender, EventArgs e)
{
Random random = new();
Color colorFirst = Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256));
ColorDialog dialog = new();
if (dialog.ShowDialog() == DialogResult.OK)
{
colorFirst = dialog.Color;
}
Color colorSecond = Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256));
if (dialog.ShowDialog() == DialogResult.OK)
{
colorSecond = dialog.Color;
}
_drawningCruiser = new DrawingProCruiser(random.Next(100, 300),
random.Next(1000, 3000),
colorFirst,
colorSecond,
Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)),
Convert.ToBoolean(random.Next(1, 2)),
Convert.ToBoolean(random.Next(1, 2)),
pictureBoxCruiser.Width,
pictureBoxCruiser.Height);
_drawningCruiser.SetPosition(random.Next(10, 100), random.Next(10, 100));
Draw();
}
///
/// Выбор лайнер
///
///
///
private void ButtonSelectCruiser_Click(object sender, EventArgs e)
{
SelectedCruiser = _drawningCruiser;
DialogResult = DialogResult.OK;
}
private void FormCruiser_Load(object sender, EventArgs e)
{
}
}
}