ISEbd-12_Frolova.A.S._Simple/ProjectCatamaran/ProjectCatamaran/FormCatamaran.cs
2024-03-20 17:46:35 +04:00

179 lines
5.8 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ProjectCatamaran.Drawnings;
using ProjectCatamaran.MovementStrategy;
namespace ProjectCatamaran
{
public partial class FormCatamaran : Form
{
/// <summary>
/// Поле-объект для прорисовки объекта
/// </summary>
private DrawningBoat? _drawningBoat;
/// <summary>
/// Стратегия перемещения
/// </summary>
private AbstractStrategy? _strategy;
/// <summary>
/// Конструктор формы
/// </summary>
public FormCatamaran()
{
InitializeComponent();
_strategy = null;
}
/// <summary>
/// Создание объекта класса-перемещения
/// </summary>
/// <param name="type"></param>
private void CreateObject(string type)
{
Random random = new();
switch (type)
{
case nameof(DrawningBoat):
_drawningBoat = new DrawningBoat(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(DrawningCatamaran):
_drawningBoat = new DrawningCatamaran(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;
}
_drawningBoat.SetPictureSize(pictureBoxCatamaran.Width, pictureBoxCatamaran.Height);
_drawningBoat.SetPosition(random.Next(10, 100), random.Next(10, 100));
_strategy = null;
comboBoxStrategy.Enabled = true;
Draw();
}
/// <summary>
/// Обработка нажатия кнопки "Создать катамаран"
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonCreateCatamaran_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningCatamaran));
/// <summary>
/// Обработка нажатия кнопки "Создать лодку"
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonCreatBoat_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningBoat));
/// <summary>
/// Метод прорисовки катамарана
/// </summary>
private void Draw()
{
if (_drawningBoat == null)
{
return;
}
Bitmap bmp = new(pictureBoxCatamaran.Width,
pictureBoxCatamaran.Height);
Graphics gr = Graphics.FromImage(bmp);
_drawningBoat.DrawTransport(gr);
pictureBoxCatamaran.Image = bmp;
}
/// <summary>
/// Перемещение объекта по форме (нажатие кнопок навигации)
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonMove_Click(object sender, EventArgs e)
{
if (_drawningBoat == null)
{
return;
}
string name = ((Button)sender)?.Name ?? string.Empty;
bool result = false;
switch (name)
{
case "buttonUp":
result = _drawningBoat.MoveTransport(DirectionType.Up);
break;
case "buttonDown":
result = _drawningBoat.MoveTransport(DirectionType.Down);
break;
case "buttonLeft":
result = _drawningBoat.MoveTransport(DirectionType.Left);
break;
case "buttonRight":
result = _drawningBoat.MoveTransport(DirectionType.Right);
break;
}
if (result)
{
Draw();
}
}
/// <summary>
/// обработка нажатия кнопки шаг
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonStrategyStep_Click(object sender, EventArgs e)
{
if (_drawningBoat == null)
{
return;
}
if (comboBoxStrategy.Enabled)
{
_strategy = comboBoxStrategy.SelectedIndex switch
{
0 => new MoveToCenter(),
1 => new MoveToBorder(),
_ => null,
};
if (_strategy == null)
{
return;
}
_strategy.SetData(new MovebleBoat(_drawningBoat), pictureBoxCatamaran.Width, pictureBoxCatamaran.Height);
}
if (_strategy == null)
{
return;
}
comboBoxStrategy.Enabled = false;
_strategy.MakeStep();
Draw();
if (_strategy.GetStatus() == StrategyStatus.Finish)
{
comboBoxStrategy.Enabled = true;
_strategy = null;
}
}
}
}