ISEbd-12_Sinelnikova_A.V._S.../ProjectCruiser/FormCruiser.cs

175 lines
5.8 KiB
C#
Raw Permalink Normal View History

2024-03-07 09:46:27 +04:00
using ProjectCruiser.Drawnings;
using ProjectCruiser.MovementStrategy;
namespace ProjectCruiser
2024-02-21 16:56:08 +04:00
{
public partial class FormCruiser : Form
{
/// <summary>
/// Поле-объект для прорисовки объекта
/// </summary>
private DrawningCruiser? _drawningCruiser;
2024-03-07 09:46:27 +04:00
/// <summary>
/// Стратегия перемещения
/// </summary>
private AbstractStrategy? _strategy;
2024-02-21 16:56:08 +04:00
public FormCruiser()
{
InitializeComponent();
2024-03-07 09:46:27 +04:00
_strategy = null;
2024-02-21 16:56:08 +04:00
}
/// <summary>
2024-02-29 13:53:31 +04:00
/// Метод прорисовки круисера
2024-02-21 16:56:08 +04:00
/// </summary>
private void Draw()
{
if (_drawningCruiser == null)
{
return;
}
2024-03-07 09:46:27 +04:00
Bitmap bmp = new(pictureBoxCruiser.Width,
pictureBoxCruiser.Height);
2024-02-21 16:56:08 +04:00
Graphics gr = Graphics.FromImage(bmp);
_drawningCruiser.DrawTransport(gr);
2024-03-07 09:46:27 +04:00
pictureBoxCruiser.Image = bmp;
2024-02-21 16:56:08 +04:00
}
2024-03-07 09:46:27 +04:00
2024-02-21 16:56:08 +04:00
/// <summary>
2024-03-07 09:46:27 +04:00
/// Создание объекта класса-перемещения
2024-02-21 16:56:08 +04:00
/// </summary>
2024-03-07 09:46:27 +04:00
/// <param name="type">Тип создаваемого объекта</param>
private void CreateObject(string type)
2024-02-21 16:56:08 +04:00
{
Random random = new();
2024-03-07 09:46:27 +04:00
switch (type)
{
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)));
break;
case nameof(DrawningMilitaryCruiser):
_drawningCruiser = new DrawningMilitaryCruiser(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)));
break;
default:
return;
}
_drawningCruiser.SetPictureSize(pictureBoxCruiser.Width,
pictureBoxCruiser.Height);
2024-02-29 13:53:31 +04:00
_drawningCruiser.SetPosition(random.Next(10, 100), random.Next(10, 100));
2024-03-07 09:46:27 +04:00
_strategy = null;
comboBoxStrategy.Enabled = true;
2024-02-21 16:56:08 +04:00
Draw();
}
2024-03-07 09:46:27 +04:00
/// <summary>
/// Обработка нажатия кнопки "Создать военный крейсер"
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonCreateCruiser_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningMilitaryCruiser));
/// <summary>
/// Обработка нажатия кнопки "Создать крейсер"
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonCretaeCruiser_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningCruiser));
2024-02-21 16:56:08 +04:00
/// <summary>
/// Перемещение объекта по форме (нажатие кнопок навигации)
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonMove_Click(object sender, EventArgs e)
{
if (_drawningCruiser == null)
{
return;
}
string name = ((Button)sender)?.Name ?? string.Empty;
bool result = false;
switch (name)
{
case "buttonUp":
result =
_drawningCruiser.MoveTransport(DirectionType.Up);
break;
case "buttonDown":
result =
_drawningCruiser.MoveTransport(DirectionType.Down);
break;
case "buttonLeft":
result =
_drawningCruiser.MoveTransport(DirectionType.Left);
break;
case "buttonRight":
result =
_drawningCruiser.MoveTransport(DirectionType.Right);
break;
}
if (result)
{
Draw();
}
}
2024-03-07 09:46:27 +04:00
/// <summary>
/// обработка нажатия кнопки шаг
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonStrategyStep_Click(object sender, EventArgs e)
{
if (_drawningCruiser == null)
{
return;
}
if (comboBoxStrategy.Enabled)
{
_strategy = comboBoxStrategy.SelectedIndex switch
{
0 => new MoveToCenter(),
1 => new MoveToBorder(),
_ => null,
};
if (_strategy == null)
{
return;
}
_strategy.SetData(new MoveableCruiser(_drawningCruiser), pictureBoxCruiser.Width, pictureBoxCruiser.Height);
}
if (_strategy == null)
{
return;
}
comboBoxStrategy.Enabled = false;
_strategy.MakeStep();
Draw();
if (_strategy.GetStatus() == StrategyStatus.Finish)
{
comboBoxStrategy.Enabled = true;
_strategy = null;
}
}
2024-02-21 16:56:08 +04:00
}
}