PIBD-14_Lavrova_K.I._Simple/solution/lab1/FormFighter.cs

158 lines
3.8 KiB
C#
Raw Normal View History

using lab1;
using lab1.Drawnings;
using lab1.MovementStrategy;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace lab1;
public partial class FormFighter : Form
{
2024-04-24 22:45:48 +04:00
/// <summary>
/// Поле-объект для прорисовки объекта
/// </summary>
private DrawningTrackedVehicle? _drawningTrackedVehicle;
/// <summary>
/// Стратегия перемещения
/// </summary>
private AbstractSrategy? _strategy;
2024-04-24 22:45:48 +04:00
/// <summary>
/// Получение объекта
/// </summary>
public DrawningTrackedVehicle SetTrackedVehicle
{
set
{
_drawningTrackedVehicle = value;
_drawningTrackedVehicle = value;
_drawningTrackedVehicle.SetPictureSize(pictureBoxFighter.Width, pictureBoxFighter.Height);
comboBoxStrategy.Enabled = true;
_strategy = null;
Draw();
}
}
public FormFighter()
{
InitializeComponent();
_strategy = null;
comboBoxStrategy.Enabled = true;
}
private void Draw()
{
if (_drawningTrackedVehicle == null)
{
return;
}
Bitmap bmp = new(pictureBoxFighter.Width, pictureBoxFighter.Height);
Graphics gr = Graphics.FromImage(bmp);
_drawningTrackedVehicle.DrawTransport(gr);
pictureBoxFighter.Image = bmp;
}
2024-04-24 22:45:48 +04:00
/// <summary>
2024-04-24 22:45:48 +04:00
/// Перемещение объекта по форме (нажатие кнопок навигации)
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonMove_Click(object sender, EventArgs e)
{
if (_drawningTrackedVehicle == null)
{
return;
}
string name = ((Button)sender)?.Name ?? string.Empty;
bool result = false;
switch (name)
{
case "buttonUp":
result = _drawningTrackedVehicle.MoveTransport(DirectionType.Up);
break;
case "buttonDown":
result = _drawningTrackedVehicle.MoveTransport(DirectionType.Down);
break;
case "buttonLeft":
result = _drawningTrackedVehicle.MoveTransport(DirectionType.Left);
break;
case "buttonRight":
result = _drawningTrackedVehicle.MoveTransport(DirectionType.Right);
break;
}
if (result)
{
Draw();
}
}
private void FormFighter_Load(object sender, EventArgs e)
{
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
}
private void pictureBoxFighter_Click(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
if (_drawningTrackedVehicle == null)
{
return;
}
if (comboBoxStrategy.Enabled)
{
_strategy = comboBoxStrategy.SelectedIndex switch
{
0 => new MoveToCenter(),
1 => new MoveToBorder(),
_ => null,
};
if (_strategy == null)
{
return;
}
_strategy.SetData(new MoveableTrackedVehicle(_drawningTrackedVehicle), pictureBoxFighter.Width, pictureBoxFighter.Height);
}
if (_strategy == null)
{
return;
}
comboBoxStrategy.Enabled = false;
_strategy.MakeStep();
Draw();
if (_strategy.GetStatus() == StrategyStatus.Finish)
{
comboBoxStrategy.Enabled = true;
_strategy = null;
}
}
}