94 lines
3.3 KiB
C#

using Lab.DrawningObjects;
using Lab.MovementStrategy;
using Lab;
namespace Lab
{
public partial class Frame : Form
{
private DrawTanker? _drawingTanker;
private AbstractStrategy? _abstractStrategy;
public Frame()
{
InitializeComponent();
}
private void Draw()
{
if (_drawingTanker == null)
return;
Bitmap bitmap = new(DrawCar.Width, DrawCar.Height);
Graphics g = Graphics.FromImage(bitmap);
_drawingTanker.DrawTransport(g);
DrawCar.Image = bitmap;
}
private void CreateGasolineTankerButton_Click(object sender, EventArgs e)
{
Random rnd = new();
_drawingTanker = new DrawGasolineTanker(rnd.Next(100, 200), rnd.Next(2000, 4000),
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);
_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));
Draw();
}
private void ButtonMove_Click(object sender, EventArgs e)
{
if (_drawingTanker == null)
return;
string name = ((Button)sender)?.Name ?? string.Empty;
switch (name)
{
case "Up":
_drawingTanker.MoveTransport(Direction.Up); break;
case "Down":
_drawingTanker.MoveTransport(Direction.Down); break;
case "Left":
_drawingTanker.MoveTransport(Direction.Left); break;
case "Right":
_drawingTanker.MoveTransport(Direction.Right); break;
}
Draw();
}
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;
}
}
}
}