2024-04-18 07:18:47 +04:00

128 lines
3.5 KiB
C#

using ProjectBattleship.MovementStrategy;
using ProjectBattleship.DrawingObject;
namespace ProjectBattleship;
/// <summary>
/// Ôîðìà ðàáîòû ñ îáúåêòîì "Ñïîðòèâíûé àâòîìîáèëü"
/// </summary>
public partial class FormBattleship : Form
{
/// <summary>
/// Ïîëå-îáúåêò äëÿ ïðîðèñîâêè îáúåêòà
/// </summary>
private DrawingWarship? _drawingWarship;
/// <summary>
/// Ñòðàòåãèÿ ïåðåìåùåíèÿ
/// </summary>
private AbstractStrategy? _strategy;
/// <summary>
/// Ïîëó÷åíèå îáúåêòà
/// </summary>
public DrawingWarship SetWarship
{
set
{
_drawingWarship = value;
_drawingWarship.SetPictureSize(pictureBoxBattleship.Width,
pictureBoxBattleship.Height);
comboBoxStrategy.Enabled = true;
_strategy = null;
Draw();
}
}
/// <summary>
/// Êîíñòðóêòîð ôîðìû
/// </summary>
public FormBattleship()
{
InitializeComponent();
_strategy = null;
}
/// <summary>
/// Ìåòîä ïðîðèñîâêè âîåííîãî êîðàáëÿ
/// </summary>
private void Draw()
{
if (_drawingWarship == null)
{
return;
}
Bitmap bmp = new(pictureBoxBattleship.Width,
pictureBoxBattleship.Height);
Graphics gr = Graphics.FromImage(bmp);
_drawingWarship.DrawTransport(gr);
pictureBoxBattleship.Image = bmp;
}
/// <summary>
/// Ïåðåìåùåíèå îáúåêòà ïî ôîðìå (íàæàòèå êíîïîê íàâèãàöèè)
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonMove_Click(object sender, EventArgs e)
{
if (_drawingWarship == null)
{
return;
}
string name = ((Button)sender)?.Name ?? string.Empty;
bool result = false;
switch (name)
{
case "buttonUp":
result = _drawingWarship.MoveTransport(DirectionType.Up);
break;
case "buttonDown":
result = _drawingWarship.MoveTransport(DirectionType.Down);
break;
case "buttonLeft":
result = _drawingWarship.MoveTransport(DirectionType.Left);
break;
case "buttonRight":
result = _drawingWarship.MoveTransport(DirectionType.Right);
break;
}
if (result)
{
Draw();
}
}
/// <summary>
/// Îáðàáîòêà íàæàòèÿ êíîïêè "Øàã"
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonStrategyStep_Click(object sender, EventArgs e)
{
if (_drawingWarship == null)
{
return;
}
if (comboBoxStrategy.Enabled)
{
_strategy = comboBoxStrategy.SelectedIndex switch
{
0 => new MoveToCenter(),
1 => new MoveToBorder(),
_ => null,
};
if (_strategy == null)
{
return;
}
_strategy.SetData(new MoveableWarship(_drawingWarship),
pictureBoxBattleship.Width, pictureBoxBattleship.Height);
}
if (_strategy == null)
{
return;
}
comboBoxStrategy.Enabled = false;
_strategy.MakeStep();
Draw();
if (_strategy.GetStatus() == StrategyStatus.Finish)
{
comboBoxStrategy.Enabled = true;
_strategy = null;
}
}
}