PIbd-21_MalafeevL.S._Cruise.../Cruiser/FormCruiser.cs

145 lines
4.8 KiB
C#

using System;
using Cruiser.Drawing;
using Cruiser.Entities;
using Cruiser.MovementStrategy;
namespace Cruiser
{
public partial class FormCruiser : Form
{
Bitmap bmp;
/// <summary>
/// Ïîëå-îáúåêò äëÿ ïðîðèñîâêè îáúåêòà
/// </summary>
private DrawingCruiser? _drawningCruiser;
/// <summary>
/// Ñòðàòåãèÿ ïåðåìåùåíèÿ
/// </summary>
private AbstractStrategy? _abstractStrategy;
/// <summary>
/// Èíèöèàëèçàöèÿ ôîðìû
/// </summary>
public FormCruiser()
{
InitializeComponent();
bmp = new(pictureBoxCruiser.Width, pictureBoxCruiser.Width);
}
/// <summary>
/// Ìåòîä ïðîðèñîâêè êðåéñåðà
/// </summary>
private void Draw()
{
if (_drawningCruiser == null)
{
return;
}
Graphics gr = Graphics.FromImage(bmp);
gr.Clear(Color.White);
_drawningCruiser.DrawTransport(gr);
pictureBoxCruiser.Image = bmp;
}
/// <summary>
/// Èçìåíåíèå ïîëîæåíèÿ àâòîìîáèëÿ
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonMove_Click(object sender, EventArgs e)
{
if (_drawningCruiser == null)
{
return;
}
string name = ((Button)sender)?.Name ?? string.Empty;
switch (name)
{
case "buttonUp":
_drawningCruiser.MoveTransport(Direction.Up);
break;
case "buttonDown":
_drawningCruiser.MoveTransport(Direction.Down);
break;
case "buttonLeft":
_drawningCruiser.MoveTransport(Direction.Left);
break;
case "buttonRight":
_drawningCruiser.MoveTransport(Direction.Right);
break;
}
Draw();
}
/// <summary>
/// Îáðàáîòêà íàæàòèÿ êíîïêè "Øàã"
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonStep_Click(object sender, EventArgs e)
{
if (_drawningCruiser == null)
{
return;
}
if (comboBoxStrategy.Enabled)
{
_abstractStrategy = comboBoxStrategy.SelectedIndex
switch
{
0 => new MoveToCenter(),
1 => new MoveToBorder(),
_ => null,
};
if (_abstractStrategy == null)
{
return;
}
_abstractStrategy.SetData(new
DrawningObjectCar(_drawningCruiser), pictureBoxCruiser.Width,
pictureBoxCruiser.Height);
comboBoxStrategy.Enabled = false;
}
if (_abstractStrategy == null)
{
return;
}
_abstractStrategy.MakeStep();
Draw();
if (_abstractStrategy.GetStatus() == Status.Finish)
{
comboBoxStrategy.Enabled = true;
_abstractStrategy = null;
}
}
private void buttonCreateLiner_Click(object sender, EventArgs e)
{
Random random = new();
_drawningCruiser = new DrawingCruiser(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)),
pictureBoxCruiser.Width,
pictureBoxCruiser.Height);
_drawningCruiser.SetPosition(random.Next(10, 100), random.Next(10, 100));
Draw();
}
private void buttonCreateProLiner_Click(object sender, EventArgs e)
{
Random random = new();
_drawningCruiser = new DrawingProCruiser(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)),
Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)),
Convert.ToBoolean(random.Next(1, 2)),
Convert.ToBoolean(random.Next(1, 2)),
pictureBoxCruiser.Width,
pictureBoxCruiser.Height);
_drawningCruiser.SetPosition(random.Next(10, 100), random.Next(10, 100));
Draw();
}
}
}