PIbd-11_Abakarov_A.A_Simple/ProjectFighterJet/FormFighterJet.cs
2024-03-31 23:32:55 +04:00

171 lines
5.5 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 ProjectFighterJet.Drawnings;
using ProjectFighterJet.MovementStrategy;
namespace ProjectFighterJet
{
public partial class FormFighterJet : Form
{
/// <summary>
/// Поле-объект для прорисовки объекта
/// </summary>
private DrawningJet? _drawningJet;
/// <summary>
/// Стратегия перемещения
/// </summary>
private AbstractStrategy? _strategy;
public DrawningJet Setjet
{
set
{
_drawningJet = value;
_drawningJet.SetPictureSize(pictureBoxFighterJet.Width, pictureBoxFighterJet.Height);
comboBoxStrategy.Enabled = true;
_strategy = null;
Draw();
}
}
/// <summary>
/// Конструктор формы
/// </summary>
public FormFighterJet()
{
InitializeComponent();
_strategy = null;
}
/// <summary>
/// Метод прорисовки машины
/// </summary>
private void Draw()
{
if (_drawningJet == null)
{
return;
}
Bitmap bmp = new(pictureBoxFighterJet.Width,
pictureBoxFighterJet.Height);
Graphics gr = Graphics.FromImage(bmp);
_drawningJet.DrawTransport(gr);
pictureBoxFighterJet.Image = bmp;
}
/// <summary>
/// Создание объекта класса-перемещения
/// </summary>
/// <param name="type">Тип создаваемого объекта</param>
private void CreateObject(string type)
{
Random random = new();
switch (type)
{
case nameof(DrawningJet):
_drawningJet = new DrawningJet(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(DrawningFighterJet):
_drawningJet = new DrawningFighterJet(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;
}
_drawningJet.SetPictureSize(pictureBoxFighterJet.Width, pictureBoxFighterJet.Height);
_drawningJet.SetPosition(random.Next(10, 100), random.Next(10, 100));
_strategy = null;
comboBoxStrategy.Enabled = true;
Draw();
}
private void ButtonCreate_Click(object sender, EventArgs e)
{
CreateObject(nameof(DrawningFighterJet));
}
private void buttonCreateJet_Click(object sender, EventArgs e)
{
CreateObject(nameof(DrawningJet));
}
private void ButtonMove_Click(object sender, EventArgs e)
{
if (_drawningJet == null)
{
return;
}
string name = ((Button)sender)?.Name ?? string.Empty;
bool result = false;
switch (name)
{
case "buttonUp":
result =
_drawningJet.MoveTransport(DirectionType.Up);
break;
case "buttonDown":
result =
_drawningJet.MoveTransport(DirectionType.Down);
break;
case "buttonLeft":
result =
_drawningJet.MoveTransport(DirectionType.Left);
break;
case "buttonRight":
result =
_drawningJet.MoveTransport(DirectionType.Right);
break;
}
if (result)
{
Draw();
}
}
private void buttonStrategyStep_Click(object sender, EventArgs e)
{
if (_drawningJet == null)
{
return;
}
if (comboBoxStrategy.Enabled)
{
_strategy = comboBoxStrategy.SelectedIndex switch
{
0 => new MoveToCenter(),
1 => new MoveToBorder(),
_ => null,
};
if (_strategy == null)
{
return;
}
_strategy.SetData(new MoveableJet(_drawningJet), pictureBoxFighterJet.Width, pictureBoxFighterJet.Height);
}
if (_strategy == null)
{
return;
}
comboBoxStrategy.Enabled = false;
_strategy.MakeStep();
Draw();
if (_strategy.GetStatus() == StrategyStatus.Finish)
{
comboBoxStrategy.Enabled = true;
_strategy = null;
}
}
}
}