132 lines
4.7 KiB
C#
132 lines
4.7 KiB
C#
using AircraftCarrier.DrawningObjects;
|
|
using AircraftCarrier.MovementStrategy;
|
|
using System;
|
|
namespace AircraftCarrier
|
|
{
|
|
public partial class FormAircraftCarrier : Form
|
|
{
|
|
private DrawningAircraft? _drawingAircraft;
|
|
|
|
private AbstractStrategy? _abstractStrategy;
|
|
public DrawningAircraft? SelectedAircraft { get; private set; }
|
|
public FormAircraftCarrier()
|
|
{
|
|
InitializeComponent();
|
|
_abstractStrategy = null;
|
|
SelectedAircraft = null;
|
|
}
|
|
private void Draw()
|
|
{
|
|
if (_drawingAircraft == null)
|
|
{
|
|
return;
|
|
}
|
|
Bitmap bmp = new(pictureBox.Width, pictureBox.Height);
|
|
Graphics gr = Graphics.FromImage(bmp);
|
|
_drawingAircraft.DrawTransport(gr);
|
|
pictureBox.Image = bmp;
|
|
}
|
|
private void buttonMove_Click(object sender, EventArgs e)
|
|
{
|
|
if (_drawingAircraft == null)
|
|
{
|
|
return;
|
|
}
|
|
string name = ((Button)sender)?.Name ?? string.Empty;
|
|
switch (name)
|
|
{
|
|
case "buttonUp":
|
|
_drawingAircraft.MoveTransport(Direction.Up);
|
|
break;
|
|
case "buttonDown":
|
|
_drawingAircraft.MoveTransport(Direction.Down);
|
|
break;
|
|
case "buttonLeft":
|
|
_drawingAircraft.MoveTransport(Direction.Left);
|
|
break;
|
|
case "buttonRight":
|
|
_drawingAircraft.MoveTransport(Direction.Right);
|
|
break;
|
|
}
|
|
Draw();
|
|
}
|
|
private void ButtonCreateAircraft_Click(object sender, EventArgs e)
|
|
{
|
|
Random random = new();
|
|
Color color = Color.FromArgb(random.Next(0, 256),
|
|
random.Next(0, 256), random.Next(0, 256));
|
|
ColorDialog dialog = new();
|
|
if (dialog.ShowDialog() == DialogResult.OK)
|
|
{
|
|
color = dialog.Color;
|
|
}
|
|
_drawingAircraft = new DrawningAircraft(random.Next(100, 300),
|
|
random.Next(1000, 3000), color, pictureBox.Width, pictureBox.Height);
|
|
_drawingAircraft.SetPosition(random.Next(10, 100), random.Next(10,
|
|
100));
|
|
Draw();
|
|
}
|
|
private void ButtonCreateAircraftCarrier_Click(object sender, EventArgs e)
|
|
{
|
|
Random random = new();
|
|
Color color = Color.FromArgb(random.Next(0, 256),
|
|
random.Next(0, 256), random.Next(0, 256));
|
|
ColorDialog colorDialog = new ColorDialog();
|
|
if (colorDialog.ShowDialog() == DialogResult.OK)
|
|
{
|
|
color = colorDialog.Color;
|
|
}
|
|
Color dopColor = Color.FromArgb(random.Next(0, 256),
|
|
random.Next(0, 256), random.Next(0, 256));
|
|
if (colorDialog.ShowDialog() == DialogResult.OK)
|
|
{
|
|
dopColor = colorDialog.Color;
|
|
}
|
|
_drawingAircraft = new DrawningAircraftCarrier(random.Next(100, 300), random.Next(1000, 3000),
|
|
color, dopColor, Convert.ToBoolean(random.Next(0, 2)),
|
|
Convert.ToBoolean(random.Next(0, 2)), pictureBox.Width, pictureBox.Height);
|
|
_drawingAircraft.SetPosition(random.Next(10, 100), random.Next(10, 100));
|
|
Draw();
|
|
}
|
|
private void buttonStep_Click(object sender, EventArgs e)
|
|
{
|
|
if (_drawingAircraft == null)
|
|
{
|
|
return;
|
|
}
|
|
if (comboBoxStrategy.Enabled)
|
|
{
|
|
_abstractStrategy = comboBoxStrategy.SelectedIndex
|
|
switch
|
|
{
|
|
0 => new MoveToCenter(),
|
|
1 => new MoveToBorder(),
|
|
_ => null,
|
|
};
|
|
if (_abstractStrategy == null)
|
|
{
|
|
return;
|
|
}
|
|
_abstractStrategy.SetData(_drawingAircraft.GetMoveableObject,
|
|
pictureBox.Width, pictureBox.Height);
|
|
}
|
|
if (_abstractStrategy == null)
|
|
{
|
|
return;
|
|
}
|
|
comboBoxStrategy.Enabled = false;
|
|
_abstractStrategy.MakeStep();
|
|
Draw();
|
|
if (_abstractStrategy.GetStatus() == Status.Finish)
|
|
{
|
|
comboBoxStrategy.Enabled = true;
|
|
_abstractStrategy = null;
|
|
}
|
|
}
|
|
private void buttonSelectAircraft_Click(object sender, EventArgs e)
|
|
{
|
|
SelectedAircraft = _drawingAircraft;
|
|
DialogResult = DialogResult.OK;
|
|
}
|
|
}
|
|
} |