137 lines
4.1 KiB
C#
137 lines
4.1 KiB
C#
using Cruiser.Drawings;
|
|
using Cruiser.MovementStrategy;
|
|
|
|
namespace Cruiser;
|
|
|
|
public partial class FormCruiser : Form
|
|
{
|
|
private DrawingShip? _drawingShip;
|
|
|
|
private AbstructStrategy? _strategy;
|
|
|
|
public FormCruiser()
|
|
{
|
|
InitializeComponent();
|
|
_strategy = null;
|
|
}
|
|
|
|
private void CreateObject(string type)
|
|
{
|
|
Random random = new();
|
|
switch (type)
|
|
{
|
|
case nameof(DrawingShip):
|
|
_drawingShip = new DrawingShip(random.Next(100, 300), random.Next(1000, 3000),
|
|
Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)));
|
|
break;
|
|
case nameof(DrawingCruiser):
|
|
_drawingShip = 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)),
|
|
Convert.ToBoolean(random.Next(0, 2)), Convert.ToBoolean(random.Next(0, 2)), Convert.ToBoolean(random.Next(0, 2)));
|
|
break;
|
|
default:
|
|
return;
|
|
}
|
|
_drawingShip.SetPictureSize(pictureBoxCruiser.Width, pictureBoxCruiser.Height);
|
|
_drawingShip.SetPosition(random.Next(10, 100), random.Next(10, 100));
|
|
_strategy = null;
|
|
comboBoxStrategy.Enabled = true;
|
|
Draw();
|
|
}
|
|
|
|
private void Draw()
|
|
{
|
|
if (_drawingShip == null)
|
|
{
|
|
return;
|
|
}
|
|
Bitmap bmp = new(pictureBoxCruiser.Width, pictureBoxCruiser.Height);
|
|
Graphics gr = Graphics.FromImage(bmp);
|
|
_drawingShip.DrawTransport(gr);
|
|
pictureBoxCruiser.Image = bmp;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Обработка кнопки "Создать Крейсер"
|
|
/// </summary>
|
|
/// <param name="sender"></param>
|
|
/// <param name="e"></param>
|
|
private void ButtonCreateCruiser_Click(object sender, EventArgs e) => CreateObject(nameof(DrawingCruiser));
|
|
|
|
/// <summary>
|
|
/// Обработка кнопки "Создать Корабль"
|
|
/// </summary>
|
|
/// <param name="sender"></param>
|
|
/// <param name="e"></param>
|
|
private void ButtonCreateShip_Click(object sender, EventArgs e) => CreateObject(nameof(DrawingShip));
|
|
|
|
private void ButtonMove_Click(object sender, EventArgs e)
|
|
{
|
|
if (_drawingShip == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
string name = ((Button)sender)?.Name ?? string.Empty;
|
|
bool result = false;
|
|
|
|
switch (name)
|
|
{
|
|
case "buttonUp":
|
|
result = _drawingShip.MoveTransport(DirectionType.Up);
|
|
break;
|
|
case "buttonDown":
|
|
result = _drawingShip.MoveTransport(DirectionType.Down);
|
|
break;
|
|
case "buttonLeft":
|
|
result = _drawingShip.MoveTransport(DirectionType.Left);
|
|
break;
|
|
case "buttonRight":
|
|
result = _drawingShip.MoveTransport(DirectionType.Right);
|
|
break;
|
|
}
|
|
if (result)
|
|
{
|
|
Draw();
|
|
}
|
|
}
|
|
|
|
private void buttonStrategyStep_Click(object sender, EventArgs e)
|
|
{
|
|
if (_drawingShip == null)
|
|
{
|
|
return;
|
|
}
|
|
if (comboBoxStrategy.Enabled)
|
|
{
|
|
_strategy = comboBoxStrategy.SelectedIndex switch
|
|
{
|
|
0 => new MoveToCenter(),
|
|
1 => new MoveToBorder(),
|
|
_ => null,
|
|
};
|
|
if (_strategy == null)
|
|
{
|
|
return;
|
|
}
|
|
_strategy.SetData(new MoveableShip(_drawingShip), pictureBoxCruiser.Width, pictureBoxCruiser.Height);
|
|
}
|
|
|
|
if (_strategy == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
comboBoxStrategy.Enabled = false;
|
|
_strategy.MakeStep();
|
|
Draw();
|
|
|
|
if (_strategy.GetStatus() == StrategyStatus.Finish)
|
|
{
|
|
comboBoxStrategy.Enabled = true;
|
|
_strategy = null;
|
|
}
|
|
}
|
|
}
|