PIbd-14_Pruidze_I.K_Simple_.../ProjectCruiser/OceanForm1.cs

157 lines
5.1 KiB
C#
Raw Normal View History

2024-06-12 19:21:59 +04:00
using ProjectCruiser.DrawningSamples;
using ProjectCruiser.MoveStrategy;
2024-06-12 14:29:35 +04:00
namespace ProjectCruiser
{
public partial class OceanForm1 : Form
{
// Поле-объект для прорисовки объекта
private DrawningBase? _drawningCruiser;
2024-06-12 19:21:59 +04:00
// Стратегия перемещения
private AbstractStrategy? _strategy;
// Получение объекта
public DrawningBase SetShip
{
set
{
_drawningCruiser = value;
_drawningCruiser.SetPictureSize(pictureBoxCr.Width, pictureBoxCr.Height);
comboBoxStrategy.Enabled = true;
_strategy = null;
Draw();
}
}
2024-06-12 14:29:35 +04:00
public OceanForm1()
{
InitializeComponent();
2024-06-12 19:21:59 +04:00
_strategy = null;
2024-06-12 14:29:35 +04:00
}
// Метод прорисовки transport
private void Draw()
{
if (_drawningCruiser == null)
{
return;
}
2024-06-12 19:21:59 +04:00
Bitmap bmp = new(pictureBoxCr.Width, pictureBoxCr.Height);
2024-06-12 14:29:35 +04:00
Graphics gr = Graphics.FromImage(bmp);
_drawningCruiser.DrawTransport(gr);
pictureBoxCr.Image = bmp;
}
2024-06-12 19:21:59 +04:00
// Обработка нажатия кнопок "Create(...)"
private void btnCreateBase_Click(object sender, EventArgs e) =>
CreateObject(nameof(DrawningBase));
private void btnCreateAdvanced_Click(object sender, EventArgs e) =>
CreateObject(nameof(DrawningCruiser));
// Создание объекта класса-перемещения
/// <param name="type">Тип создаваемого объекта</param>
private void CreateObject(string type)
2024-06-12 14:29:35 +04:00
{
Random random = new();
2024-06-12 19:21:59 +04:00
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):
2024-06-13 10:57:01 +04:00
_drawningCruiser = new DrawningCruiser(
random.Next(100, 300), random.Next(1000, 3000),
2024-06-12 19:21:59 +04:00
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)),
2024-06-15 10:03:23 +04:00
Convert.ToBoolean(random.Next(0, 2)), Convert.ToBoolean(random.Next(0, 2)));
2024-06-12 19:21:59 +04:00
break;
2024-06-12 14:29:35 +04:00
2024-06-12 19:21:59 +04:00
default:
return;
}
2024-06-12 14:29:35 +04:00
_drawningCruiser.SetPictureSize(pictureBoxCr.Width, pictureBoxCr.Height);
_drawningCruiser.SetPosition(random.Next(10, 100),
pictureBoxCr.Height - random.Next(10, 100) - _drawningCruiser.getHeight());
2024-06-12 19:21:59 +04:00
_strategy = null;
comboBoxStrategy.Enabled = true;
2024-06-12 14:29:35 +04:00
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();
}
}
2024-06-12 19:21:59 +04:00
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;
}
}
2024-06-12 14:29:35 +04:00
}
}