151 lines
4.9 KiB
C#
151 lines
4.9 KiB
C#
using DumpTruck.DrawingObjects;
|
|
using DumpTruck.MovementStrategy;
|
|
|
|
namespace DumpTruck
|
|
{
|
|
public partial class FormDumpTruck : Form
|
|
{
|
|
private DrawingTruck? _drawingTruck;
|
|
|
|
private AbstractStrategy? _strategy;
|
|
|
|
public DrawingTruck? SelectedTruck { get; private set; }
|
|
|
|
public FormDumpTruck()
|
|
{
|
|
InitializeComponent();
|
|
_strategy = null;
|
|
SelectedTruck = null;
|
|
}
|
|
|
|
private void Draw()
|
|
{
|
|
if (_drawingTruck == null)
|
|
{
|
|
return;
|
|
}
|
|
Bitmap bmp = new(pictureBoxDumpTruck.Width,
|
|
pictureBoxDumpTruck.Height);
|
|
Graphics gr = Graphics.FromImage(bmp);
|
|
_drawingTruck.DrawTransport(gr);
|
|
pictureBoxDumpTruck.Image = bmp;
|
|
}
|
|
|
|
private void ButtonMove_Click(object sender, EventArgs e)
|
|
{
|
|
if (_drawingTruck == null)
|
|
{
|
|
return;
|
|
}
|
|
string name = ((Button)sender)?.Name ?? string.Empty;
|
|
switch (name)
|
|
{
|
|
case "buttonUp":
|
|
_drawingTruck.MoveTransport(Direction.Up);
|
|
break;
|
|
case "buttonDown":
|
|
_drawingTruck.MoveTransport(Direction.Down);
|
|
break;
|
|
case "buttonLeft":
|
|
_drawingTruck.MoveTransport(Direction.Left);
|
|
break;
|
|
case "buttonRight":
|
|
_drawingTruck.MoveTransport(Direction.Right);
|
|
break;
|
|
}
|
|
Draw();
|
|
}
|
|
|
|
private void ButtonCreateDumpTruck_Click(object sender, EventArgs e)
|
|
{
|
|
Random random = new();
|
|
Color bodyColor = Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256));
|
|
ColorDialog dialog = new();
|
|
if (dialog.ShowDialog() == DialogResult.OK)
|
|
{
|
|
bodyColor = dialog.Color;
|
|
}
|
|
|
|
Color dumpBoxColor = Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256));
|
|
ColorDialog secondDialog = new();
|
|
if (secondDialog.ShowDialog() == DialogResult.OK)
|
|
{
|
|
dumpBoxColor = secondDialog.Color;
|
|
}
|
|
|
|
Color tentColor = Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256));
|
|
ColorDialog thirdDialog = new();
|
|
if (thirdDialog.ShowDialog() == DialogResult.OK)
|
|
{
|
|
tentColor = thirdDialog.Color;
|
|
}
|
|
|
|
_drawingTruck = new DrawingDumpTruck(random.Next(100, 300), random.Next(1000, 3000),
|
|
bodyColor,
|
|
Convert.ToBoolean(random.Next(0, 2)), Convert.ToBoolean(random.Next(0, 2)),
|
|
tentColor,
|
|
dumpBoxColor,
|
|
pictureBoxDumpTruck.Width, pictureBoxDumpTruck.Height);
|
|
_drawingTruck.SetPosition(random.Next(10, 100), random.Next(10, 100));
|
|
Draw();
|
|
}
|
|
|
|
private void ButtonCreateTruck_Click(object sender, EventArgs e)
|
|
{
|
|
Random random = new();
|
|
Color bodyColor = Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256));
|
|
ColorDialog dialog = new();
|
|
if (dialog.ShowDialog() == DialogResult.OK)
|
|
{
|
|
bodyColor = dialog.Color;
|
|
}
|
|
_drawingTruck = new DrawingTruck(random.Next(100, 300), random.Next(1000, 3000), bodyColor,
|
|
pictureBoxDumpTruck.Width, pictureBoxDumpTruck.Height);
|
|
_drawingTruck.SetPosition(random.Next(10, 100), random.Next(10, 100));
|
|
Draw();
|
|
}
|
|
|
|
private void ButtonStep_Click(object sender, EventArgs e)
|
|
{
|
|
if (_drawingTruck == null)
|
|
{
|
|
return;
|
|
}
|
|
if (comboBoxStrategy.Enabled)
|
|
{
|
|
_strategy = comboBoxStrategy.SelectedIndex
|
|
switch
|
|
{
|
|
0 => new MoveToCenter(),
|
|
1 => new MoveToBorder(),
|
|
_ => null,
|
|
};
|
|
if (_strategy == null)
|
|
{
|
|
return;
|
|
}
|
|
_strategy.SetData(new
|
|
DrawningObjectTruck(_drawingTruck), pictureBoxDumpTruck.Width,
|
|
pictureBoxDumpTruck.Height);
|
|
comboBoxStrategy.Enabled = false;
|
|
}
|
|
if (_strategy == null)
|
|
{
|
|
return;
|
|
}
|
|
_strategy.MakeStep();
|
|
Draw();
|
|
if (_strategy.GetStatus() == Status.Finish)
|
|
{
|
|
comboBoxStrategy.Enabled = true;
|
|
_strategy = null;
|
|
}
|
|
}
|
|
|
|
private void buttonSelectTruck_Click(object sender, EventArgs e)
|
|
{
|
|
SelectedTruck = _drawingTruck;
|
|
DialogResult = DialogResult.OK;
|
|
}
|
|
}
|
|
} |