94 lines
3.3 KiB
C#
Raw Normal View History

2023-09-26 19:32:10 +04:00
using Lab.DrawningObjects;
using Lab.MovementStrategy;
using Lab;
2023-09-26 19:15:17 +04:00
namespace Lab
{
public partial class Frame : Form
{
2023-09-26 19:32:10 +04:00
private DrawTanker? _drawingTanker;
private AbstractStrategy? _abstractStrategy;
2023-09-26 19:15:17 +04:00
public Frame()
{
InitializeComponent();
}
private void Draw()
{
2023-09-26 19:32:10 +04:00
if (_drawingTanker == null)
2023-09-26 19:15:17 +04:00
return;
Bitmap bitmap = new(DrawCar.Width, DrawCar.Height);
Graphics g = Graphics.FromImage(bitmap);
2023-09-26 19:32:10 +04:00
_drawingTanker.DrawTransport(g);
2023-09-26 19:15:17 +04:00
DrawCar.Image = bitmap;
}
2023-09-26 19:32:10 +04:00
private void CreateGasolineTankerButton_Click(object sender, EventArgs e)
2023-09-26 19:15:17 +04:00
{
Random rnd = new();
2023-09-26 19:32:10 +04:00
_drawingTanker = new DrawGasolineTanker(rnd.Next(100, 200), rnd.Next(2000, 4000),
2023-09-26 19:15:17 +04:00
Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)),
Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)),
Convert.ToBoolean(rnd.Next(0, 2)), Convert.ToBoolean(rnd.Next(0, 2)), Convert.ToBoolean(rnd.Next(0, 2)),
DrawCar.Width, DrawCar.Height);
2023-09-26 19:32:10 +04:00
_drawingTanker.SetPosition(rnd.Next(10, 100), rnd.Next(10, 100));
Draw();
}
private void CreateCarButton_Click(object sender, EventArgs e)
{
Random rnd = new();
_drawingTanker = new DrawTanker(rnd.Next(100, 200), rnd.Next(2000, 4000),
Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)),
DrawCar.Width, DrawCar.Height);
_drawingTanker.SetPosition(rnd.Next(10, 100), rnd.Next(10, 100));
2023-09-26 19:15:17 +04:00
Draw();
}
private void ButtonMove_Click(object sender, EventArgs e)
{
2023-09-26 19:32:10 +04:00
if (_drawingTanker == null)
2023-09-26 19:15:17 +04:00
return;
string name = ((Button)sender)?.Name ?? string.Empty;
switch (name)
{
case "Up":
2023-09-26 19:32:10 +04:00
_drawingTanker.MoveTransport(Direction.Up); break;
2023-09-26 19:15:17 +04:00
case "Down":
2023-09-26 19:32:10 +04:00
_drawingTanker.MoveTransport(Direction.Down); break;
2023-09-26 19:15:17 +04:00
case "Left":
2023-09-26 19:32:10 +04:00
_drawingTanker.MoveTransport(Direction.Left); break;
2023-09-26 19:15:17 +04:00
case "Right":
2023-09-26 19:32:10 +04:00
_drawingTanker.MoveTransport(Direction.Right); break;
2023-09-26 19:15:17 +04:00
}
Draw();
}
2023-09-26 19:32:10 +04:00
private void ButtonStep_Click(object sender, EventArgs e)
{
if (_drawingTanker == null)
return;
if (comboBoxStrategy.Enabled)
{
_abstractStrategy = comboBoxStrategy.SelectedIndex switch
{
0 => new MoveToCenter(),
1 => new MoveToBorder(),
_ => null
};
if (_abstractStrategy == null)
return;
_abstractStrategy.SetData(new DrawingObjectTanker(_drawingTanker), DrawCar.Width, DrawCar.Height);
comboBoxStrategy.Enabled = false;
}
if (_abstractStrategy == null)
return;
_abstractStrategy.MakeStep();
Draw();
if (_abstractStrategy.GetStatus() == Status.Finish)
{
comboBoxStrategy.Enabled = true;
_abstractStrategy = null;
}
}
2023-09-26 19:15:17 +04:00
}
}