117 lines
4.0 KiB
C#
117 lines
4.0 KiB
C#
using ProjectStormtrooper.Drawnings;
|
||
|
||
namespace ProjectStormtrooper;
|
||
/// <summary>
|
||
/// Форма работы с объектом "Штурмовик"
|
||
/// </summary>
|
||
public partial class FormStormtrooper : Form
|
||
{/// <summary>
|
||
/// Поле-объект для прорисовки объекта
|
||
/// </summary>
|
||
private DrawingStormtrooperBase? _drawningStormtrooperBase;
|
||
/// <summary>
|
||
/// Конструктор формы
|
||
/// </summary>
|
||
public FormStormtrooper()
|
||
{
|
||
InitializeComponent();
|
||
}
|
||
|
||
/// <summary>
|
||
/// Метод прорисовки штурмовика
|
||
/// </summary>
|
||
|
||
private void Draw()
|
||
{
|
||
if (_drawningStormtrooperBase == null)
|
||
{
|
||
return;
|
||
}
|
||
|
||
Bitmap bmp = new(pictureBoxStormtrooper.Width, pictureBoxStormtrooper.Height);
|
||
Graphics gr = Graphics.FromImage(bmp);
|
||
_drawningStormtrooperBase.DrawTransport(gr);
|
||
pictureBoxStormtrooper.Image = bmp;
|
||
}
|
||
/// <summary>
|
||
/// Создание объекта класса-перемещения
|
||
/// </summary>
|
||
/// <param name="type"></param>
|
||
private void CreateObject(string type)
|
||
{
|
||
Random random = new();
|
||
switch (type)
|
||
{
|
||
case nameof(DrawingStormtrooper):
|
||
_drawningStormtrooperBase = new DrawingStormtrooper(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)));
|
||
break;
|
||
case nameof(DrawingStormtrooperBase):
|
||
_drawningStormtrooperBase = new DrawingStormtrooperBase(random.Next(100, 300), random.Next(1000, 3000),
|
||
Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)));
|
||
break;
|
||
default:
|
||
return;
|
||
|
||
}
|
||
_drawningStormtrooperBase.SetPictureSize(pictureBoxStormtrooper.Width, pictureBoxStormtrooper.Height);
|
||
_drawningStormtrooperBase.SetPosition(random.Next(10, 100), random.Next(10, 100));
|
||
Draw();
|
||
|
||
}
|
||
/// <summary>
|
||
/// Обработка нажатия кнопки "Создать штурмовик"
|
||
/// </summary>
|
||
/// <param name="sender"></param>
|
||
/// <param name="e"></param>
|
||
private void ButtonCreateStormtrooper_Click(object sender, EventArgs e) => CreateObject(nameof(DrawingStormtrooper));
|
||
/// <summary>
|
||
/// Обработка нажатия кнопки "Создать базовый штурмовик"
|
||
/// </summary>
|
||
/// <param name="sender"></param>
|
||
/// <param name="e"></param>
|
||
private void ButtonCreateStormtrooperBase_Click(object sender, EventArgs e) => CreateObject(nameof(DrawingStormtrooperBase));
|
||
|
||
|
||
/// <summary>
|
||
/// Перемещение объкта по форме(нажатие кнопок навигации)
|
||
/// </summary>
|
||
/// <param name="sender"></param>
|
||
/// <param name="e"></param>
|
||
private void ButtonMove_Click(object sender, EventArgs e)
|
||
{
|
||
if (_drawningStormtrooperBase == null)
|
||
{
|
||
return;
|
||
}
|
||
|
||
string name = ((Button)sender)?.Name ?? string.Empty;
|
||
bool result = false;
|
||
switch (name)
|
||
{
|
||
case "buttonUp":
|
||
result = _drawningStormtrooperBase.MoveTransport(DirectionType.Up);
|
||
break;
|
||
case "buttonDown":
|
||
result = _drawningStormtrooperBase.MoveTransport(DirectionType.Down);
|
||
break;
|
||
case "buttonLeft":
|
||
result = _drawningStormtrooperBase.MoveTransport(DirectionType.Left);
|
||
break;
|
||
case "buttonRight":
|
||
result = _drawningStormtrooperBase .MoveTransport(DirectionType.Right);
|
||
break;
|
||
}
|
||
|
||
if (result)
|
||
{
|
||
Draw();
|
||
}
|
||
|
||
}
|
||
|
||
|
||
}
|