PIbd-23_Dolgova_D.N._Warmly.../WarmlyShip/FormWarmlyShip.cs

142 lines
5.0 KiB
C#
Raw Normal View History

2023-12-01 18:41:28 +04:00
using WarmlyShip.DrawningObjects;
using WarmlyShip.MovementStrategy;
2023-11-24 22:12:36 +04:00
namespace WarmlyShip
{
public partial class FormWarmlyShip : Form
{
/// <summary>
2023-12-01 18:41:28 +04:00
/// Форма работы с объектом "Теплоход"
2023-11-24 22:12:36 +04:00
/// </summary>
2023-12-01 18:41:28 +04:00
private DrawningShip? _drawningShip;
/// <summary>
/// Стратегия перемещения
/// </summary>
private AbstractStrategy? _abstractStrategy;
2023-11-24 22:12:36 +04:00
/// <summary>
/// Инициализация формы
/// </summary>
public FormWarmlyShip()
{
InitializeComponent();
}
/// <summary>
2023-12-01 18:41:28 +04:00
/// Метод прорисовки корабля
2023-11-24 22:12:36 +04:00
/// </summary>
private void Draw()
{
2023-12-01 18:41:28 +04:00
if (_drawningShip == null)
2023-11-24 22:12:36 +04:00
{
return;
}
Bitmap bmp = new(pictureBoxWarmlyShip.Width, pictureBoxWarmlyShip.Height);
Graphics gr = Graphics.FromImage(bmp);
2023-12-01 18:41:28 +04:00
_drawningShip.DrawTransport(gr);
2023-11-24 22:12:36 +04:00
pictureBoxWarmlyShip.Image = bmp;
}
/// <summary>
2023-12-01 18:41:28 +04:00
/// Обработка нажатия кнопки "Создать теплоход"
2023-11-24 22:12:36 +04:00
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
2023-12-01 18:41:28 +04:00
private void buttonCreateWarmlyShip_Click(object sender, EventArgs e)
2023-11-24 22:12:36 +04:00
{
Random random = new();
2023-12-01 18:41:28 +04:00
_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)),
2023-11-24 22:12:36 +04:00
Convert.ToBoolean(random.Next(0, 2)),
pictureBoxWarmlyShip.Width, pictureBoxWarmlyShip.Height);
2023-12-01 18:41:28 +04:00
_drawningShip.SetPosition(random.Next(10, 100), random.Next(10, 100));
Draw();
}
/// <summary>
/// Обработка нажатия кнопки "Создать корабль"
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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));
2023-11-24 22:12:36 +04:00
Draw();
}
2023-12-01 18:41:28 +04:00
2023-11-24 22:12:36 +04:00
/// <summary>
2023-12-01 18:41:28 +04:00
/// Изменение положения корабля
2023-11-24 22:12:36 +04:00
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonMove_Click(object sender, EventArgs e)
{
2023-12-01 18:41:28 +04:00
if (_drawningShip == null)
2023-11-24 22:12:36 +04:00
{
return;
}
string name = ((Button)sender)?.Name ?? string.Empty;
switch (name)
{
case "buttonUp":
2023-12-01 18:41:28 +04:00
_drawningShip.MoveTransport(DirectionType.Up);
2023-11-24 22:12:36 +04:00
break;
case "buttonDown":
2023-12-01 18:41:28 +04:00
_drawningShip.MoveTransport(DirectionType.Down);
2023-11-24 22:12:36 +04:00
break;
case "buttonLeft":
2023-12-01 18:41:28 +04:00
_drawningShip.MoveTransport(DirectionType.Left);
2023-11-24 22:12:36 +04:00
break;
case "buttonRight":
2023-12-01 18:41:28 +04:00
_drawningShip.MoveTransport(DirectionType.Right);
2023-11-24 22:12:36 +04:00
break;
}
Draw();
}
2023-12-01 18:41:28 +04:00
/// <summary>
/// Обработка нажатия кнопки "Шаг"
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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;
}
}
2023-11-24 22:12:36 +04:00
}
}